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.


SystemC code generation by using the command-line interface that 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.

Copy the Design and Test Bench Files Into a Temporary Folder

To copy the design and test bench files into a temporary folder, execute this code:

close all;
design_name = 'mlhdlc_sfir';
testbench_name = 'mlhdlc_sfir_tb';

mlhdlc_demo_dir = fullfile(matlabroot, 'toolbox', 'hdlcoder', 'hdlcoderdemos', 'matlabhdlcoderdemos');
mlhdlc_temp_dir = [tempdir 'mlhdlc_sfir'];

[~, ~, ~] = rmdir(mlhdlc_temp_dir, 's');

copyfile(fullfile(mlhdlc_demo_dir, [design_name,'.m*']), mlhdlc_temp_dir);
copyfile(fullfile(mlhdlc_demo_dir, [testbench_name,'.m*']), mlhdlc_temp_dir);

Create a 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';

Set 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 length. The following code configures the coder to propose types based on a fraction length of 10:

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

Set the 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;

Enable 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;

View the 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 a 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';

Set the Target Language and Synthesis Tool

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

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

Generate SystemC Test Bench Code

Generate a SystemC test bench from your MATLAB® test bench:

hdlcfg.GenerateHDLTestBench = true;

Simulate the Generated SystemC Code

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

hdlcfg.SimulateGeneratedCode = true;

Synthesize the Generated SystemC Code by Using Synthesis Tool

You can synthesize your generated SystemC code by using the synthesis tool Cadence Stratus.

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