Skip to Main Content Skip to Search
Product Documentation

Recommended Practices

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 Setting Optimal Fixed-Point Options for the MATLAB Function Block for an example.

Use Compiled External Functions With MATLAB Function Blocks

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.

Build the MATLAB Function Block Code First

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

Use the hdlfimath Utility for Optimized FIMATH Settings

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);

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.

  


Related Products & Applications

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