Main Content

Guidelines for Writing MATLAB Code to Generate Efficient HDL and HLS Code

MATLAB Design Requirements for HDL and HLS Code Generation

When you generate HDL or High-Level Synthesis (HLS) code from your MATLAB® design, you are converting an algorithm into an architecture that must meet hardware area and speed requirements.

Your MATLAB design has these requirements:

  • MATLAB code within the design must be supported by HDL or HLS code generation.

  • Inputs and outputs must not be matrices or structures.

If you are generating code at the command line, verify your code readiness for code generation by using this command:

If you use the HDL Workflow Advisor to generate code, this check runs automatically.

For a MATLAB language support reference, including supported functions from Fixed-Point Designer™, see Functions Supported for HDL and HLS Code Generation.

Guidelines for Writing MATLAB Code

For more efficient and faster HDL and HLS code generation, design your MATLAB code by using these best practices:

  • Serialize your input and output data. Parallel data processing structures require more hardware resources and a higher pin count.

  • Use add and subtract algorithms instead of algorithms that use functions, such as sine, divide, and modulo. Add and subtract operations use fewer hardware resources.

  • Avoid large arrays and matrices. Large arrays and matrices require more registers and more RAM for storage. Whenever you need to use large arrays for memory, consider using the RAM mapping optimization to map these memories to RAM.

  • Convert your code from floating-point to fixed-point. Floating-point data types are inefficient for hardware realization. HDL Coder™ provides an automated workflow for floating-point to fixed-point conversion.

  • Unroll loops to increase speed at the cost of higher area. For HDL code generation, unroll fewer loops and enable the loop streaming optimization to conserve area at the cost of lower throughput.

  • Optimize generated code by reducing the number of bits used to represent a variable i.e., bitwidth of the operand inside the MATLAB code.

  • Use hardware-friendly rounding and overflow methods. It can be achieved by using hdlfimath function in your MATLAB code.

    function out = add(a,b)​
       out = fi(a+b,1,12,4,hdlfimath);​

Additional guidelines for efficient HLS code generation:

  • Modularize the design into subfunctions as much as possible. Doing so improves the readability of the code and makes it easier to specify constraints, such as coder.hdl.constrainlatency, on a particular region of code.

  • Use a zero-based indexing scheme followed by array access with the addition of 1 as MATLAB uses 1-based indexing: for example, use arrayVar(index+1) instead of arrayVar(index). The extra indexing logic does not need to be inserted by the code generator, thereby reducing overall area.

See Also




Related Topics