Design Guidelines for the MATLAB Function Block
Use Compiled External Functions With MATLAB Function Blocks
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
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.
Build the MATLAB Function Block Code First
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, click the Build Model button in the function editor.
hdlfimath Utility for Optimized FIMATH Settings
hdlfimath function is a utility that defines a FIMATH
specification that is optimized for HDL code generation.
The following listing shows the
fimath setting defined by
hdlfm = fimath(... 'RoundMode', 'floor',... 'OverflowMode', 'wrap',... 'ProductMode', 'FullPrecision', 'ProductWordLength', 32,... 'SumMode', 'FullPrecision', 'SumWordLength', 32,... 'CastBeforeSum', true);
The HDL division operator does not support
'round' mode or change the signed integer division
operations to unsigned integer division.
OverflowMode property of the
specification is set to
'Saturate', HDL code generation is
disallowed for the following cases:
SumModeis set to
ProductModeis set to
Use Optimal Fixed-Point Option Settings
Use the default (
Fixed-point) setting for the
Treat these inherited signal types as fi objects
Clear the Saturate on integer overflow setting for the MATLAB Function block.
Set the Output Data Type of MATLAB Function Blocks Explicitly
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.
Using Tunable Parameters
HDL Coder supports both tunable and non-tunable parameters with the following data types:
When using tunable parameters with the MATLAB Function block:
The tunable parameter should be a Simulink.Parameter object with the
x = Simulink.Parameter x.Value = 1 x.CoderInfo.StorageClass = 'ExportedGlobal'
Double-click the MATLAB Function block and open the Symbols pane and Property Inspector in the Design section in the Modeling tab. In the Symbols Pane, select the tunable parameter. In the Property Inspector, select the Tunable check box.
For details, see Generate DUT Ports for Tunable Parameters.
Run HDL Model Check for MATLAB Function Blocks
When your design contains MATLAB Function blocks, before you
generate HDL code, you can run the check Check for MATLAB Function block settings in the HDL
Code Advisor. This check verifies whether you use the recommended MATLAB
Function blocks for HDL code generation. The settings include whether
fimath settings are defined as per
hdlfimath and Saturate on integer
overflow check box is cleared.
See also Check HDL Compatibility of Simulink Model Using HDL Code Advisor.
Use MATLAB Datapath Architecture for Enhanced HDL Optimizations
In the HDL Block Properties dialog box for the MATLAB Function
blocks, you can specify whether to use
MATLAB Function or
MATLAB Datapath as the HDL architecture. Floating-point
models use the
MATLAB Datapath architecture even if you specify
the HDL architecture as
MATLAB Function on the block. Fixed-point
models use the
MATLAB Function architecture by default.
To perform various speed and area optimizations such as sharing and distributed
pipelining inside the MATLAB Function block and across the
MATLAB Function block boundary with other Simulink® blocks, use the
MATLAB Datapath architecture. When
you use this architecture, the code generator treats the MATLAB
Function block like a regular Subsystem block. This
capability enables you to optimize the algorithm inside the MATLAB
Function block and across the MATLAB Function block with
other blocks in your model.
See HDL Optimizations Across MATLAB Function Block Boundary Using MATLAB Datapath Architecture.
Check for MATLAB Function block settings