System Design with HDL Code Generation from MATLAB and Simulink
This example shows how to generate a MATLAB Function block from a MATLAB® design for system simulation, code generation, and FPGA programming in Simulink®.
HDL Coder can generate HDL code from both MATLAB and Simulink. The coder can also generate a Simulink component, the MATLAB Function block, from your MATLAB code.
This capability enables you to:
Design an algorithm in MATLAB;
Generate a MATLAB Function block from your MATLAB design;
Use the MATLAB component in a Simulink model of the system;
Simulate and optimize the system model;
Generate HDL code; and
Program an FPGA with the entire system design.
In this example, you will generate a MATLAB Function block from MATLAB code that implements a FIR filter.
The MATLAB code used in the example is a simple FIR filter. The example also shows a MATLAB testbench that exercises the filter.
design_name = 'mlhdlc_fir'; testbench_name = 'mlhdlc_fir_tb';
Test Bench: mlhdlc_fir_tb
Simulate the Design
To simulate the design with the test bench prior to code generation to make sure there are no runtime errors, enter the following command:
Create a New Project
To create a new HDL Coder project, enter the following command:
coder -hdlcoder -new fir_project
Next, add the file
mlhdlc_fir.m to the project as the MATLAB Function and
mlhdlc_fir_tb.m as the MATLAB Test Bench.
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 the following command returns
1, Simulink is available:
In the HDL Workflow Advisor, select the HDL Code Generation task. On the right-side, 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
An untitled model opens after HDL code generation. It has a MATLAB Function block containing the fixed-point MATLAB code from your MATLAB HDL design. 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 the following command:
You can rename and save the new block to use in a larger Simulink design.