Guidelines for Writing MATLAB Code to Generate Efficient HDL and SystemC Code
MATLAB Design Requirements for HDL and SystemC Code Generation
When you generate HDL or SystemC 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 SystemC 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:
For a MATLAB language support reference, including supported functions from Fixed-Point Designer™, see Functions Supported for HDL and SystemC Code Generation.
Guidelines for Writing MATLAB Code
For more efficient and faster HDL and SystemC 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
hdlfimathfunction in your MATLAB code.
function out = add(a,b) out = fi(a+b,1,12,4,hdlfimath); end
Additional guidelines for efficient SystemC™ 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). The extra indexing logic does not need to be inserted by the code generator, thereby reducing overall area.