System Design with HDL Code Generation from MATLAB and Simulink
This example shows you how to generate a Simulink® model and MATLAB Function block from a MATLAB® algorithm. You can then create HDL code from this model.
HDL Coder® can generate HDL code from both MATLAB and Simulink. When you generate HDL code for a function in MATLAB, you can also generate a Simulink model and a MATLAB Function block from your MATLAB code. You can use this capability to integrate your MATLAB algorithm into a Simulink model. You can then simulate and optimize the complete system, generate HDL code from the model, and program an FPGA with the entire design.
In this example, you generate a MATLAB Function block from MATLAB code that implements a FIR filter.
Examine MATLAB Algorithm
The MATLAB code in mlhdlc_fir
is a simple FIR filter. The MATLAB test bench mlhdlc_fir_tb
exercises the filter.
design_name = "mlhdlc_fir"; testbench_name = "mlhdlc_fir_tb";
Use Test Bench to Verify Algorithm Results
To ensure there are no runtime errors when you generate the HDL code, use the test bench to verify algorithm results by entering this command:
mlhdlc_fir_tb
Create a New Project
To create a new HDL Coder project, enter this command:
coder -hdlcoder -new fir_project
Click Add MATLAB function and select mlhdlc_fir.m
. Then, in the MATLAB Test Bench section, click Add files and select mlhdlc_fir_tb.m
.
Click the Workflow Advisor button to launch the HDL Workflow Advisor.
Enable the MATLAB Function Block Option
To generate a MATLAB Function block from a MATLAB HDL design, you must have a Simulink license. If this command returns 1
, Simulink is available:
license("test","Simulink")
In the HDL Workflow Advisor, select the HDL Code Generation task. On the right pane, click the Advanced tab and select the check box Generate Simulink model (Simulink license is required).
Run Floating-Point to Fixed-Point Conversion and Generate Code
To generate a MATLAB Function block, convert your design from floating point to fixed point.
Right-click the HDL Code Generation step and choose the option Run to selected task to run all the steps from the beginning through HDL code generation.
Examine the Generated MATLAB Function Block
The generated model opens after HDL code generation completes. It has a MATLAB Function block that contains the fixed-point MATLAB code from your MATLAB algorithm. HDL Coder automatically applies settings to the model and MATLAB Function block so that they can simulate in Simulink and generate HDL code.
To generate HDL code from the MATLAB Function block, enter this command:
makehdl("mlhdlc_fir_fixpt_MLFcnModel");
You can rename and save the new block to use in a larger Simulink model.