Contenuto principale

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:

  1. Create a MATLAB HDL Coder project for the design and test bench files.

  2. Use the HDL Workflow Advisor to convert the floating-point MATLAB design to a fixed-point design.

  3. 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:

  1. 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 the mlhdlc_counter.prj project in the specified directory.

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

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

  4. Click Workflow Advisor.

Default view on launching the MATLAB HDL Coder App. The interface includes sections for adding a MATLAB function, a MATLAB test bench, auto-defining types, and starting the 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:

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

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

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

View of the HDL Coder Workflow Advisor after executing the HDL Code Generation step. The process completed, as indicated by the output in the message viewer at the bottom of the application.

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:

  1. In the Workflow Advisor, in the HDL Code Generation task, click the Target tab. In the Report Settings section, select Generate report.

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

View of the HDL Coder Workflow Advisor after completing the HDL Code Generation step with report generation options enabled. The message viewer at the bottom of the application provides a hyperlink to the code generation report.

To review the HDL code generation report:

  1. In the HDL Coder Report Viewer window, in the Generated Code pane, click mlhdlc_counter_fixpt.vhd to examine the generated HDL code and source MATLAB code.

    View of the HDL Coder Report Viewer. The upper left section shows the MATLAB source files. The bottom left displays the generated HDL files. The middle panel displays the contents of a highlighted file. The bottom panel provides a summary of the code generation process, featuring messages, code insights, and variables in successive tabs.

  2. In the right pane, in the Code Insights tab, there is a lack of potential issues or suggestions for algorithm improvement.

    View of the Code Insights tab at the HDL Coder Report Viewer. There is a lack of potential issues or recommendations for improvement for the MATLAB algorithm.

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.

View of the HDL Coder Workflow Advisor after completing the Verify with HDL Test Bench step. The message window at the bottom of the Workflow Advisor displays a simulation successful message. Previous steps in the Workflow Advisor are complete.

If you have HDL Verifier™ installed, you can also verify generated HDL code by using these tasks in the Workflow Advisor:

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.

See Also

Apps

Tools

Topics