Main Content

Get Started with MATLAB to SystemC Workflow Using the Command Line Interface

This example shows how to use the HDL Coder™ command-line interface to generate SystemC code from MATLAB® code, including floating-point to fixed-point conversion.

Overview

SystemC code generation using the command-line interface has the following basic steps:

  1. Set up the high-level synthesis (HLS) tool path for SystemC code generation by using the function hdlsetuphlstoolpath.

  2. Create a fixpt coder configuration object.

  3. Create an hdl coder configuration object.

  4. Set configuration object parameters.

  5. Run the codegen command to generate SystemC code.

The HDL Coder command-line interface can use two coder configuration objects with the codegen command. The fixpt coder configuration object configures the floating-point to fixed-point conversion of your MATLAB code. The hdl coder configuration object configures SystemC code generation and programming options.

The example code implements a simple symmetric finite impulse response (FIR) filter and its test bench. Using this example, you can configure floating-point to fixed-point conversion and generate SystemC code.

Set up the FIR model and test bench for this example.

mlhdlc_demo_setup('sfir');

Create Floating-Point to Fixed-Point Conversion Configuration Object

If your design already uses fixed-point types and functions, then you can skip the fixed-point conversion step.

To perform floating-point to fixed-point conversion, you need a fixpt configuration object.

Create a fixpt configuration object and specify your test bench name.

fixptcfg = coder.config('fixpt');
fixptcfg.TestBenchName = 'mlhdlc_sfir_tb';

Fixed-Point Conversion Type Proposal Options

The code generator can propose fixed-point types based on your choice of either word length or fraction length. These two options are mutually exclusive.

Base the proposed types on a word length of 24:

fixptcfg.DefaultWordLength = 24;
fixptcfg.ProposeFractionLengthsForDefaultWordLength = true;

Alternatively, you can base the proposed fixed-point types on fraction lengths. The following code configures the coder to propose types based on a fraction length of 10:

fixptcfg.DefaultFractionLength = 10;
fixptcfg.ProposeWordLengthsForDefaultFractionLength = true;

Safety Margin

The code generator increases the simulation data range on which it bases its fixed-point type proposal by the safety margin percentage. For example, the default safety margin is 4, which increases the simulation data range used for fixed-point type proposal by 4%.

Set the SafetyMargin to 10%:

fixptcfg.SafetyMargin = 10;

Data Logging

The code generator runs the test bench with the design before and after a floating-point to fixed-point conversion. You can enable simulation data logging to plot the quantization effects of the new fixed-point data types.

Enable data logging in the fixpt configuration object:

fixptcfg.LogIOForComparisonPlotting = true;

Numeric Type Proposal Report

Configure the code generator to start the type proposal report once the fixed-point types have been proposed:

fixptcfg.LaunchNumericTypesReport = true;

Create SystemC Code Generation Configuration Object

To generate SystemC code, you must create an hdl configuration object and set your test bench name:

hdlcfg = coder.config('hdl');
hdlcfg.TestBenchName = 'mlhdlc_sfir_tb';

Target Language and Synthesis Tool

To generate SystemC code, specify the target language as SystemC and the synthesis tool as Cadence Stratus.

hdlcfg.TargetLanguage = 'SystemC';
hdlcfg.SynthesisTool = 'Cadence Stratus';

Generation of SystemC Test Bench Code

Configure the code generator to generate a SystemC test bench from your MATLAB® test bench:

hdlcfg.GenerateHDLTestBench = true;

Simulation of the Generated SystemC Code

If you want to simulate your generated SystemC code, you must also configure the code generator to generate a SystemC test bench.

hdlcfg.SimulateGeneratedCode = true;

Synthesis Tool for Generated SystemC Code

You can synthesize your generated SystemC code by using the synthesis tool Cadence Stratus. Configure the code generator to use this tool.

hdlcfg.SynthesizeGeneratedCode = true;

Run SystemC Code Generation

Now that you have your fixpt and hdl configuration objects set up, run the codegen command to perform floating-point to fixed-point conversion and generate SystemC code:

codegen -float2fixed fixptcfg -config hdlcfg mlhdlc_sfir -report

SystemC Code Generation Report

At the MATLAB command line window, you can check the code generation report by clicking View Report.

The code generation report helps you to:

  • Debug code generation issues and verify that your MATLAB code is suitable for code generation.

  • View generated SystemC code.

  • Access additional reports like a conformance report and resource utiliztion report.

  • See how the code generator determines and propagates type information for variables and expressions in your MATLAB code.

To view a MATLAB function in the code pane, click the name of the function in the MATLAB Source pane. In the code pane, when you pause on a variable or expression, a tooltip displays information about its size, type, and complexity.

For more information see, SystemC Code Generation Report.

Limitations

  • Only Cadence Stratus is supported as the high level synthesis tool. Cadence Stratus supports only point to point (p2p) communication for SystemC code generation.

  • Generating SystemC code that runs on multiple threads is not supported.

  • Real data types and Systems objects are not supported for SystemC code generation.

  • Structures and enumerations are not supported as inputs and outputs at the top-level DUT ports for SystemC code generation.

See Also