Design Guidelines for the MATLAB Function Block - MATLAB & Simulink (original) (raw)
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 %#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.
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.
Use the hdlfimath
Utility for Optimized FIMATH Settings
The 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 byhdlfimath
.
hdlfm = fimath(... 'RoundMode', 'floor',... 'OverflowMode', 'wrap',... 'ProductMode', 'FullPrecision', 'ProductWordLength', 32,... 'SumMode', 'FullPrecision', 'SumWordLength', 32,... 'CastBeforeSum', true);
The HDL division operator supports these rounding modes:
'zero'
— For signed and unsigned fixed point numbers.'floor'
— For unsigned division only.
When the fimath
OverflowMode
property of the fimath
specification is set to 'Saturate'
, HDL code generation is disallowed for the following cases:
SumMode
is set to'SpecifyPrecision'
ProductMode
is set to'SpecifyPrecision'
Use Optimal Fixed-Point Option Settings
Use the default (Fixed-point
) setting for theTreat these inherited signal types as fi objects option.
Clear the Saturate on integer overflow setting for theMATLAB 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:
- Scalar
- Vector
- Complex
- Structure
- Enumeration
When using tunable parameters with the MATLAB Function block:
- The tunable parameter should be a Simulink.Parameter object with the
StorageClass
set toExportedGlobal
.
x = Simulink.Parameter
x.Value = 1
x.CoderInfo.StorageClass = 'ExportedGlobal' - Double-click the MATLAB Function block and open theSymbols 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 theTunable 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 whetherfimath
settings are defined as perhdlfimath
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
orMATLAB 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 theMATLAB 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.
See Also
Check for MATLAB Function block settings