Design Guidelines for the MATLAB Function Block

Introduction

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.

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, select Build from the Tools menu in the MATLAB Function Block Editor (or press CTRL+B).

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. 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:

    • SumMode is set to 'SpecifyPrecision'

    • ProductMode is set to 'SpecifyPrecision'

Use Optimal Fixed-Point Option Settings

Use the default (Fixed-point) setting for the Treat these inherited signal types as fi objects option, as shown in the following figure.

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.

Was this topic helpful?