| Contents | Index |
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 Setting Optimal Fixed-Point Options for the MATLAB Function Block for an example.
The coder supports HDL code generation from MATLAB Function blocks that include compiled external functions. This feature lets you 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, and must include the %#codegen compilation directive. See Adding the Compilation Directive %#codegen for information on how to create, compile, and invoke external functions.
Before generating HDL code for a subsystem containing a MATLAB Function block, it is strongly recommended that you 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).
The hdlfimath.m function is a utility that defines a FIMATH specification that is optimized for HDL code generation. It is strongly recommended that you 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 'floor' rounding mode for signed integer division will cause an error at code generation time. The HDL division operator does not support 'floor' rounding mode. Use 'round' mode, or else change the signed integer division operations to unsigned integer division. |
Note When the FIMATH OverflowMode property of the FIMATH specification is set to 'Saturate', HDL code generation is disallowed for the following cases:
|
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.
![]() | Distributed Pipeline Insertion for MATLAB Function Blocks | Language Support | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |