This section describes recommended practices when using the MATLAB Function block for HDL code generation.
By setting MATLAB Function block options as described in this section, you can significantly increase the efficiency of generated HDL code. See Set Fixed-Point Options for the MATLAB Function Block for an example.
The HDL Coder™ software supports HDL code generation from MATLAB Function blocks that include compiled external functions. This feature enables you to write reusable MATLAB® code and call it from multiple MATLAB Function blocks.
Such functions must be defined in files that are on the MATLAB
Function block path. Use the %#codegen
compilation
directive to indicate that the MATLAB code is suitable for code
generation. See Function Definition for information on how to create,
compile, and invoke external functions.
Before generating HDL code for a subsystem containing a MATLAB Function block, build the MATLAB Function block code to check for errors. To build the code, select Build from the Tools menu in the MATLAB Function Block Editor (or press CTRL+B).
hdlfimath
Utility for Optimized FIMATH SettingsThe hdlfimath
function is a utility that
defines a FIMATH specification that is optimized for HDL code generation.
Replace the default MATLAB Function block fimath specification
with a call to the hdlfimath
function, as shown
in the following figure.
The following listing shows the fimath
setting
defined by hdlfimath
.
hdlfm = fimath(... 'RoundMode', 'floor',... 'OverflowMode', 'wrap',... 'ProductMode', 'FullPrecision', 'ProductWordLength', 32,... 'SumMode', 'FullPrecision', 'SumWordLength', 32,... 'CastBeforeSum', true);
Note:
Use of |
Note:
When the
|
Use the default (Fixed-point
) setting
for the Treat these inherited signal types as fi objects option,
as shown in the following figure.
By setting the output data type of a MATLAB Function block explicitly, you enable optimizations for RAM mapping and pipelining. Avoid inheriting the output data type for a MATLAB Function block for which you want to enable optimizations.