Main Content

Get Started with MATLAB to High-Level Synthesis Workflow Using the Command Line Interface

This example shows how to use the HDL Coder™ command-line interface to generate High-Level Synthesis (HLS) code from MATLAB® code, including floating-point to fixed-point conversion.


High-Level Synthesis (HLS) code generation using the command-line interface has the following basic steps:

  1. Set up the HLS tool path for HLS 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 HLS 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 HLS 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 HLS code.

Set up the FIR model and test bench for this example. Ensure that the file path includes no spaces.


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 HLS Code Generation Configuration Object

To generate HLS 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 HLS code, specify the workflow as High Level Synthesis and the synthesis tool as Cadence Stratus.

hdlcfg.Workflow = 'High Level Synthesis';
hdlcfg.SynthesisTool = 'Cadence Stratus';

Generation of HLS Test Bench Code

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

hdlcfg.GenerateHDLTestBench = true;

Simulation of the Generated HLS Code

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

hdlcfg.SimulateGeneratedCode = true;

Synthesis Tool for Generated HLS Code

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

hdlcfg.SynthesizeGeneratedCode = true;

Run HLS 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 HLS code:

codegen -float2fixed fixptcfg -config hdlcfg mlhdlc_sfir -report

HLS 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 HLS 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, HLS Code Generation Report.


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

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

  • Systems objects are not supported for HLS code generation.

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

See Also