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:
coder.screener('design_function_name')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.
Optimize generated code by reducing the number of bits used to represent a variable i.e., bit width of the operand inside the MATLAB code.
Use hardware-friendly rounding and overflow methods. It can be achieved by using
hdlfimathfunction in your MATLAB code.function out = add(a,b) out = fi(a+b,1,12,4,hdlfimath); end
Loop unrolling and streaming guidelines:
By default, for loops in MATLAB code are implemented as serial loops in generated HDL, which is area-efficient but may result in lower throughput.
To increase throughput, you can use the LoopUnrolling option to unroll loops, which replicates hardware and increases parallelism. However, this comes at the cost of higher area usage.
Alternatively, you can use the
coder.hdl.loopspecwith thestreamoption to enable loop streaming, which reuses hardware across loop iterations. This provides a balance between area and throughput.
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 ofarrayVar(index). The extra indexing logic does not need to be inserted by the code generator, thereby reducing overall area.