Generate HDL Code from MATLAB Algorithms
HDL Coder™ converts compatible high-level MATLAB® algorithms into HDL code. In this example, you create an HDL Coder project and generate HDL code for a MATLAB counter algorithm. The MATLAB counter algorithm is compatible with HDL code generation. To learn about how to make a MATLAB algorithm compatible with HDL code generation, see Create HDL- or HLS-Compatible MATLAB Algorithms. To generate HDL code from a Simulink model, see Generate HDL Code from Simulink Model.
In this example, you:
Create a MATLAB HDL Coder project for the design and test bench files.
Use the HDL Workflow Advisor to convert the floating-point MATLAB design to a fixed-point design.
Use the HDL Workflow Advisor to generate HDL code.
Once you have converted your design to HDL code, you can perform FPGA synthesis, implementation, and programming. To perform FPGA synthesis and implementation, see Synthesize Generated HDL Code from MATLAB Algorithms.
MATLAB Counter Algorithm
Open this example to obtain the MATLAB counter function and test bench for this tutorial.
open mlhdlc_counter open mlhdlc_counter_tb
The MATLAB function mlhdlc_counter is a model of a 4-bit synchronous up counter. When the input argument, enable_ctr, is a nonzero value, the persistent variable, count_val, increases by one. The value of count_val increases by one until the count reaches 15. Then the counter returns to zero. To learn more about this function and the associated test bench, see Create HDL- or HLS-Compatible MATLAB Algorithms.
Create a MATLAB HDL Coder Project
To create a MATLAB HDL Coder project:
In the MATLAB Editor, in the Apps tab, select HDL Coder. In the MATLAB HDL Coder Project window, set Name to
mlhdlc_counter. Set Location to the current working directory. HDL Coder creates themlhdlc_counter.prjproject in the specified directory.In the HDL Code Generation window, in the MATLAB Function section, click Add MATLAB function and select the function file for the up counter algorithm,
mlhdlc_counter.m. In the MATLAB Test Bench section, click Add files and add the MATLAB test bench,mlhdlc_counter_tb.m.You must add a MATLAB test bench unless the design does not need fixed-point conversion and you do not want to generate a register transfer level test bench.
Note
Add only the top-level MATLAB function. Do not use spaces in file names or paths to prevent code generation failures.
In the MATLAB Function section, click Autodefine types and then click Run. HDL Coder infers the input types by executing the MATLAB test bench. Click Use These Types to use the recommended types for the MATLAB design. If you do not add a test bench, you must define the inputs to the top-level MATLAB function. For more information, see Specify Properties of Entry-Point Function Inputs.
Click Workflow Advisor.

Generate HDL Code
In the Workflow Advisor, right-click the HDL Code Generation task, and select Run to Selected Task.
HDL Coder runs these Workflow Advisor tasks to generate HDL code for the MATLAB algorithm:
In the Fixed Point Conversion task, HDL Coder translates the floating-point MATLAB design to a fixed-point design. For details, see Floating-Point to Fixed-Point Conversion.
In the Select Code Generation Target task, HDL Coder determines the available options and downstream tasks in the Workflow Advisor based on the workflow you select. Because the default setting of Workflow is
Generic ASIC/FPGA, HDL Coder prepares to target a generic ASIC or FPGA.In the HDL Code Generation task, HDL Coder generates HDL code from the fixed-point MATLAB design. By default, HDL Coder generates VHDL® code. For more information about code generation options, see MATLAB to HDL Code and Synthesis.

To generate HLS code from a compatible MATLAB algorithm, see Get Started with MATLAB to High-Level Synthesis Workflow Using HDL Coder App.
Review HDL Code Generation Report
HDL Coder provides a code generation report to identify the mapping between the MATLAB source design and the generated HDL code. To generate this report:
In the Workflow Advisor, in the HDL Code Generation task, click the Target tab. In the Report Settings section, select Generate report.
Click the Coding Style tab. In the Generated Code Comments section, select the Include MATLAB source code as comments parameter, then click Run.
In the HDL Code Generation task, in the bottom of the
right pane, click the hyperlink View report to open the HDL
code generation report.

To review the HDL code generation report:
In the HDL Coder Report Viewer window, in the Generated Code pane, click
mlhdlc_counter_fixpt.vhdto examine the generated HDL code and source MATLAB code.
In the right pane, in the Code Insights tab, there is a lack of potential issues or suggestions for algorithm improvement.

For more information on HDL code generation reports, see Code Generation Reports.
Verify Generated HDL Code
Before you deploy your generated HDL code, you can verify the generated HDL code by using an HDL test bench. HDL Coder verifies the generated HDL code by using an HDL simulation tool to test vectors from a generated HDL test bench. For more information, see Verify Code with HDL Test Bench.
You must install an HDL simulator to simulate the generated HDL test bench. For more information, see HDL Simulator Setup.
To verify code, in the Workflow Advisor, in the Verification task, select the Verify with HDL Test Bench task. In the Output Settings tab, select Generate test bench and Simulate generated test bench, then click Run.
HDL Coder generates an HDL test bench and opens the HDL simulator in the background to simulate the design.

If you have HDL Verifier™ installed, you can also verify generated HDL code by using these tasks in the Workflow Advisor:
Verify with Cosimulation. For more information about verifying generated HDL code with cosimulation, see Automatic Verification of Generated HDL Code from MATLAB (HDL Verifier).
Verify with FPGA-in-the-Loop. For more information about verifying generated HDL code with FPGA-in-the-loop, see FIL Simulation with HDL Workflow Advisor for MATLAB (HDL Verifier).
After you generate and validate the HDL code, you can synthesize the HDL code for a target platform. For more information about synthesis, see Synthesize Generated HDL Code from MATLAB Algorithms.