Introduction

HDL Coder™ by default inlines the body of all MATLAB functions called in the top level design and generates HDL code in a single file. However turning on the advanced coding option 'Generate instantiable code for functions' helps you generate module code in multiple files.

MATLAB Design

The MATLAB design used in the example is an implementation of an LMS filter. The LMS filter is used to identify an FIR filter signal embedded in the noise.

Run Fixed-Point Conversion and HDL Code Generation

Launch the Workflow Advisor from the Build tab and right click on the 'Code Generation' step and choose the option 'Run to selected task' to run all the steps from the beginning through the HDL code generation.

Examine the generated HDL code by clicking on the hyperlinks in the Code Generation Log window. Notice that the HDL code for all the functions in the design is inlined into a single file 'mlhdlc_lms_fcn_FixPt.vhd'

Generate Instantiable Code

To generate modular code for all the functions in separate files please turn on this advanced coding option 'Generate instantiable code for functions'

Rerun Code Generation Step

Now reset the code generation task and rerun the HDL code generation step to see how code is generated in multiple files for the functions

Control Inlining Per Function

Sometimes it is preferable to inline code for helpers and utilities and instantiate them. To locally control inlining of such functions use the 'coder.inline' pragma in the MATLAB code.

coder.inline('always')
coder.inline('never')
coder.inline('default')

Type the following command to see how to use coder.inline pragma

help coder.inline

Known Limitations

The following limitations exist when instantiating HDL code from functions.

Function calls inside conditional expressions and for loops get inlined and do not get instantiated.

Functions with states get inlined.

Clean up the Generated Files

You can run the following commands to clean up the temporary project folder.

MATLAB and Simulink are registered trademarks of The MathWorks, Inc. Please see www.mathworks.com/trademarks for a list of other trademarks owned by The MathWorks, Inc. Other product or brand names are trademarks or registered trademarks of their respective owners.