HDL Code Generation Overview
HDL Code Generation Overview
R2017a
How to Contact MathWorks
Phone: 508-647-7000
v
Tutorials
3
HDL Code Generation from a MATLAB Algorithm . . . . . . . . 3-2
About the Algorithm in This Example . . . . . . . . . . . . . . . . . . 3-2
Copying Files Locally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3
Checking Your Synthesis Tool Setup . . . . . . . . . . . . . . . . . . . 3-4
Testing the Original MATLAB Algorithm . . . . . . . . . . . . . . . 3-4
Setting Up an HDL Coder Project . . . . . . . . . . . . . . . . . . . . . 3-5
Creating Fixed-Point Versions of the Algorithm and Test
Bench . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8
Generating HDL Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
vi Contents
1
HDL Coder™ generates portable, synthesizable VHDL® and Verilog® code from
MATLAB® functions, Simulink® models, and Stateflow® charts. The generated HDL code
can be used for FPGA programming or ASIC prototyping and design.
HDL Coder provides a workflow advisor that automates the programming of Xilinx® and
Altera® FPGAs. You can control HDL architecture and implementation, highlight critical
paths, and generate hardware resource utilization estimates. HDL Coder provides
traceability between your Simulink model and the generated Verilog and VHDL code,
enabling code verification for high-integrity applications adhering to DO-254 and other
standards.
Key Features
• Target-independent, synthesizable VHDL and Verilog code
• Code generation support for MATLAB functions, System objects and Simulink blocks
• Mealy and Moore finite-state machines and control logic implementations using
Stateflow
• Workflow advisor for programming Xilinx and Altera application boards
• Resource sharing and retiming for area-speed tradeoffs
• Code-to-model and model-to-code traceability for DO-254
• Legacy code integration
1-2
Supported Third-Party Tools and Hardware
In this section...
“Third-Party Synthesis Tools and Version Support” on page 1-3
“FPGA-in-the-Loop Hardware” on page 1-3
“Simulink Real-Time FPGA I/O Hardware” on page 1-4
“FPGA Turnkey Hardware” on page 1-4
To use third-party synthesis tools with HDL Coder, a supported synthesis tool must be
installed, and the synthesis tool executable must be on the system path. For details, see
“Tool Setup” on page 2-2.
FPGA-in-the-Loop Hardware
The FPGAs supported for FPGA-in-the-loop simulation with HDL Verifier™ are listed in
the HDL Verifier documentation.
You can also add custom FPGA boards using the FPGA Board Manager. See “FPGA
Board Customization” for details.
For FPGA-in-the-Loop or Customization for USRP® Device using the HDL Workflow
Advisor, a supported synthesis tool must be installed, and the synthesis tool executable
must be on the system path. For details, see “Tool Setup” on page 2-2.
1-3
1 About HDL Coder
For FPGA development boards that have more than one FPGA device, only one such
device can be used with FPGA Turnkey.
You can also add custom FPGA boards using the FPGA Board Manager. HDL Coder
supports the following FPGA device families for board customization; that is, when you
create your own board definition file. See “FPGA Board Customization” (HDL Verifier).
1-4
Supported Third-Party Tools and Hardware
Device Family
Xilinx Kintex7
Spartan-3A DSP
Spartan3
Spartan3A and Spartan3AN
Spartan3E
Spartan6
Virtex4
Virtex5
Virtex6
Virtex7
Altera Cyclone III
Cyclone IV
Arria II
Stratix® IV
Stratix V
More About
• “Tool Setup” on page 2-2
1-5
1 About HDL Coder
1-6
HDL Coder Supported Hardware
For details, see “Supported Third-Party Tools and Hardware” on page 1-3.
1-7
2
Tool Setup
In this section...
“Synthesis Tool Path Setup” on page 2-2
“HDL Simulator Setup” on page 2-3
“Xilinx System Generator Setup for ModelSim Simulation” on page 2-4
“Altera DSP Builder Setup” on page 2-4
“FPGA Simulation Library Setup” on page 2-5
“C/C++ Compiler Setup” on page 2-5
hdlsetuptoolpath Function
To use HDL Coder with one of the supported third-party FPGA synthesis tools, add the
tool to your system path using the hdlsetuptoolpath function. Add the tool to your
system path before opening the HDL Workflow Advisor. If you already have the HDL
Workflow Advisor open, see “Add Synthesis Tool for Current HDL Workflow Advisor
Session” on page 2-2.
1 At the MATLAB command line, use the hdlsetuptoolpath function to add the
synthesis tool.
2 In the HDL Workflow Advisor, in the Set Target > Set Target Device and
Synthesis Tool step, to the right of Synthesis tool, click Refresh.
2-2
Tool Setup
1 At the MATLAB command line, use the hdlsetuptoolpath function to add the
synthesis tool.
2 In the HDL Workflow Advisor, in the Select Code Generation Target step, to the
right of Synthesis tool, click Refresh list.
To check your Altera Quartus synthesis tool setup in MATLAB, try launching the tool
with the following command:
!quartus
To check your Xilinx Vivado synthesis tool setup in MATLAB, try launching the tool with
the following command:
!vivado
To check your Xilinx ISE synthesis tool setup in MATLAB, try launching the tool with
the following command:
!ise
For supported tool versions, see “Third-Party Synthesis Tools and Version Support” on
page 1-3.
2-3
2 Getting Started with HDL Coder
To generate ModelSim simulation scripts, you must have the following compiled Xilinx
simulation libraries for your EDA simulator and target language:
• unisim
• simprim
• xilinxcorelib
Specify the path to your compiled Xilinx simulation libraries by setting the
XilinxSimulatorLibPath parameter for your model.
2-4
Tool Setup
To learn how to compile this library, refer to the Xilinx compxlib documentation .
• Specify the path to your compiled Altera or Xilinx simulation libraries. Altera
provides the simulation model files in \quartus\eda\sim_lib folder. Set the
SimulationLibPath parameter for your DUT.
myDUT = gcb;
libpath = '/apps/Xilinx_ISE/XilinxISE-13.4/Linux/ISE_DS/ISE/vhdl/
mti_se/6.6a/lin64/xilinxcorelib';
hdlset_param (myDUT, 'SimulationLibPath', libpath);
You can also specify the simulation library path from the HDL Code Generation >
Test Bench pane in the Configuration Parameters dialog box.
More About
• “Third-Party Synthesis Tools and Version Support” on page 1-3
2-5
2 Getting Started with HDL Coder
• To check your model for compatibility with HDL code generation, use the checkhdl
function.
When you create the model, you can save time by using a filtered view in the Library
Browser that shows only blocks that are compatible with HDL code generation. To
show this filtered view, use the hdllib command.
Code generation does not support all available combinations of supported blocks,
block implementations, block properties, and HDL optimizations. The checkhdl
function can find many code generation incompatibilities. However, you can modify
your model later to generate code that meets your hardware requirements.
• If you want to synthesize your generated HDL code, convert your model to fixed point.
If your synthesis tool and hardware support floating-point constructs, you can
synthesize some of those constructs. To learn more, see “FPGA Floating-Point
Libraries”.
See Also
Functions
checkhdl | hdllib | hdlsetup
Related Examples
• “View HDL-Specific Block Documentation”
More About
• “Supported Blocks”
2-6
3
Tutorials
• mlhdlc_sfir.m — Simple filter function from which you generate HDL code.
• mlhdlc_sfir_tb.m — Test bench that the HDL Coder project uses to exercise the
filter using a representative input range.
%#codegen
function [y_out, delayed_xout] = mlhdlc_sfir(x_in, h_in1, h_in2, h_in3, h_in4)
% Symmetric FIR Filter
3-2
HDL Code Generation from a MATLAB Algorithm
a5 = m1 + m2; a6 = m3 + m4;
% filtered output
y_out = a5 + a6;
% delayout input signal
delayed_xout = ud8;
end
The mlhdlc_sfir_tb test bench creates an input signal and calls the mlhdlc_sfir
filter, passing in the input data.
clear all;
% filter coefficients
h1 = -0.1339; h2 = -0.0838; h3 = 0.2026; h4 = 0.4064;
len = length(x_in);
y_out = zeros(1,len);
x_out = zeros(1,len);
for ii=1:len
data = x_in(ii);
% call to the design 'mlhdlc_sfir' that is targeted for hardware
[y_out(ii), x_out(ii)] = mlhdlc_sfir(data, h1, h2, h3, h4);
end
3-3
3 Tutorials
1 Start MATLAB.
2 Create a folder named filter_sfir, for example:
mkdir filter_sfir
The folder must not be within the MATLAB directory structure. You must be able to
write to this folder.
3 Copy the tutorial files, mlhdlc_sfir.m and mlhdlc_sfir_tb.m, to this folder.
To check your Xilinx ISE synthesis tool setup, try launching the tool with the following
command:
!ise
To check your Altera Quartus synthesis tool setup, try launching the tool with the
following command:
!quartus
If the tool does not open, or opens the wrong version, see “Synthesis Tool Path Setup” on
page 2-2.
cd filter_sfir
2 Run the test bench. At the MATLAB command line, enter:
mlhdlc_sfir_tb
The test bench runs and plots the input signal and the filtered output.
3-4
HDL Code Generation from a MATLAB Algorithm
3-5
3 Tutorials
HDL Coder creates the project, [Link], in the local working folder, and
opens the project in the right side of the MATLAB workspace.
3-6
HDL Code Generation from a MATLAB Algorithm
3-7
3 Tutorials
You are now ready to convert the code from floating-point to fixed-point.
3-8
HDL Code Generation from a MATLAB Algorithm
HDL Coder simulates the algorithm and test bench, and automatically defines input
types.
3 On the left, select the Fixed-Point Conversion task. The Fixed-Point Conversion
tool opens in the right pane.
3-9
3 Tutorials
After the simulation, each input, output, and persistent variable has a Sim Min,
Sim Max, and Proposed Type in the table.
When proposing fraction lengths for floating-point data types, HDL Coder uses
the Default word length. In this tutorial, the Default word length is 14. The
advisor provides a default Safety Margin for Simulation Min/Max of 0%. The
3-10
HDL Code Generation from a MATLAB Algorithm
advisor adjusts the range of the data by this safety factor. For example, a value
of 4 specifies that you want a range of at least 4 percent larger.
In this example, we use only the simulation ranges to infer fixed-point types.
Compute Derived Ranges gives you the option of using static range analysis.
To learn more about options in the fixed-point conversion workflow, see
“Automated Fixed-Point Conversion”.
b At the top, in the Verification section, click Validate Types.
HDL Coder validates the build with the proposed fixed-point types and
generates a fixed-point design.
c At the top, in the Verification section, click the down-arrow for Test Numerics
and select Log inputs and outputs for comparison plots. Click the top part
of the Test Numerics button.
HDL Coder simulates the fixed-point design with the original test bench
compares the output to the original floating-point design output.
d Click the down-arrow to the right of the Verification Output tab and select
Type Validation Output. Click mlhdlc_sfir_fixpt to see the fixed-point
MATLAB code for the mlhdlc_sfir function.
%#codegen
function [y_out,delayed_xout] = mlhdlc_sfir_fixpt(x_in,h_in1,h_in2,h_in3,h_in4)
3-11
3 Tutorials
You can explore the fixed-point code further in the Code Generation Report.
The message window has a links to the generated HDL code and the resource report.
Click the links to view the code and resource report.
Tip: You can use the Target, Coding Style, Clocks and Ports, Optimizations,
Advanced, and Script Options tabs to set code generation options. To learn about
the options, click the ? button.
2 In the HDL Workflow Advisor left pane, select HDL Verification > Verify with
HDL Test Bench.
3 Enable Generate HDL test bench and disable Skip this step. Enable Simulate
generated HDL test bench and select a simulation tool. Click Run.
The task generates an HDL test bench, then simulates the fixed-point design using
the selected simulation tool, and generates a compilation report and a simulation
report.
3-12
HDL Code Generation from a MATLAB Algorithm
This task creates a synthesis project for the HDL code. HDL Coder uses this
project in the next task to synthesize the design.
5 Select and run Run Logic Synthesis.
3-13
3 Tutorials
This task:
This task:
3-14
HDL Code Generation from a Simulink Model
• Before generating code, use the hdlsetup utility (described in “Initializing Model
Parameters with hdlsetup” on page 3-23) to set up your model for HDL code
generation quickly and consistently.
• Use the hdllib utility to create a library of blocks that are currently supported
for HDL code generation, as described in “Show Blocks Supported for HDL Code
Generation”. By constructing models with blocks from this library, your models will be
HDL compatible.
The set of supported blocks will change in future releases, so you should rebuild your
supported blocks library each time you install a new version of this product.
• Use the Run Compatibility Checker option (described in “Selecting and Checking
a Subsystem for HDL Compatibility” on page 3-35) to check HDL compatibility of
your model or DUT and generate an HDL Code Generation Check Report.
Alternatively, you can invoke the checkhdl function (see checkhdl) to run the
compatibility checker.
Overview of Exercises
HDL Coder supports HDL code generation in your choice of environments:
3-15
3 Tutorials
• The MATLAB Command Window supports code generation using the makehdl,
makehdltb, and other functions.
• The Simulink GUI (the Configuration Parameters dialog box and/or Model Explorer)
provides an integrated view of the model simulation parameters and HDL code
generation parameters and functions.
The hands-on exercises in this chapter introduce you to the mechanics of generating and
simulating HDL code, using the same model to generate code in both environments. In a
series of steps, you will
The blocks in this example model support HDL code generation, and the model
parameters have been configured for HDL code generation. To learn more about
preparing your model for code generation, see “Prepare Simulink Model For HDL Code
Generation” on page 2-6.
3-16
HDL Code Generation from a Simulink Model
The top-level model generates 16-bit fixed-point input signals for the symmetric_fir
subsystem. The Signal From Workspace block generates a test input (stimulus) signal for
the filter. The four Constant blocks provide filter coefficients.
The Scope blocks are used in simulation only. They are virtual blocks, and do not
generate HDL code.
3-17
3 Tutorials
The fixed-point data types propagate through the subsystem. Inputs inherit the data
types of the signals presented to them. Where required, internal rules of the blocks
determine the output data type, given the input data types and the operation performed
(for example, the Product blocks).
The filter outputs a fixed-point result at the y_out port, and also replicates its input
(after passing it through several delay stages) at the delayed_x_out port.
In the exercises that follow, you generate VHDL code that implements the
symmetric_fir subsystem as an entity. You then generate a test bench from the top-
level model. The test bench drives the generated entity, for the required number of clock
steps, with stimulus data generated from the Signal From Workspace block.
3-18
HDL Code Generation from a Simulink Model
The model you use in this example, sfir_fixed, is already prepared for code
generation.
This example uses the Xilinx ISE synthesis tool, and assumes your synthesis tool path is
set up. You can also follow this example using Altera Quartus II.
1 Start MATLAB.
2 Create a folder named sl_hdlcoder_work. For example:
mkdir C:\work\sl_hdlcoder_work
You will use sl_hdlcoder_work to store a local copy of the example model and to
store folders and code generated by HDL Coder. The location of the folder does not
matter, except that it should not be within the MATLAB folder tree.
3 Make the sl_hdlcoder_work folder your working folder. For example:
cd C:\work\sl_hdlcoder_work
4 Open the sfir_fixed model.
sfir_fixed
5 Save a copy of sfir_fixed in your sl_hdlcoder_work folder.
1 Right-click the symmetric_fir subsystem and select HDL Code > HDL
Workflow Advisor.
2 In the Set Target > Set Target Device and Synthesis Tool step, for Synthesis
tool, select Xilinx ISE and click Run This Task.
3-19
3 Tutorials
3 Right-click Prepare Model For HDL Code Generation and select Run All. The
HDL Workflow Advisor checks the model for code generation compatibility.
4 In the HDL Code Generation > Set Code Generation Options > Set Basic
Options step, select the following options, then click Apply:
3-20
HDL Code Generation from a Simulink Model
The code generation report, which includes the resource utilization and traceability
reports, opens automatically. The resource utilization report shows the hardware
resources your design implementation is using. The traceability report enables you
to navigate between your model and the generated code.
1 In the FPGA Synthesis and Analysis > Perform Synthesis and P/R > Perform
Place and Route task, unselect Skip this task and click Apply.
2 Right-click Annotate Model with Synthesis Result and select Run to Selected
Task.
3 View the annotated critical path in the model.
3-21
3 Tutorials
3-22
HDL Code Generation from a Simulink Model
Overview
This exercise provides a step-by-step introduction to code and test bench generation
commands, their arguments, and the files created by the code generator. The exercise
assumes that you have familiarized yourself with the example model (see “The sfir_fixed
Model” on page 3-16).
Make a local copy of the example model and store it in a working folder, as follows.
mkdir C:\work\sl_hdlcoder_work
The sl_hdlcoder_work folder will store a local copy of the example model and to
store folders and code generated by HDL Coder. The location of the folder does not
matter, except that it should not be within the MATLAB tree.
3 Make the sl_hdlcoder_work folder your working folder, for example:
cd C:\work\sl_hdlcoder_work
4 To open the example model, type the following command at the MATLAB prompt:
sfir_fixed
5 In Simulink, select File > Save As and save a local copy of the sfir_fixed model
to your working folder.
6 Leave the sfir_fixed model open and proceed to the next section.
Before generating code, you must configure the model. You can use the hdlsetup
command instead of configuring the model manually. The hdlsetup command uses the
set_param function to set up models for HDL code generation quickly and consistently.
hdlsetup('sfir_fixed')
2 Select Save from the File menu, to save the model with its new settings.
3-23
3 Tutorials
Before continuing with code generation, consider the settings that hdlsetup applies to
the model.
hdlsetup configures the Solver options that are recommended or required by HDL
Coder. These are
hdlsetup also configures the model start and stop times and fixed-step size as follows:
If Fixed step size is set to auto the step size is chosen automatically, based on the
sample times specified in the model. In the example model, only the Signal From
Workspace block specifies an explicit sample time (1 s); the other blocks inherit this
sample time.
The model start and stop times determine the total simulation time. This in turn
determines the size of data arrays that are generated to provide stimulus and output
data for generated test benches. For the example model, computation of 10 seconds of
test data does not take a significant amount of time. Computation of sample values for
more complex models can be time consuming. In such cases, you may want to decrease
the total simulation time.
The remaining parameters set by hdlsetup control error severity levels, data logging,
and model display options. If you want to view the complete set of model parameters
affected by hdlsetup, open hdlsetup.m in the MATLAB Editor.
The model parameter settings provided by are intended as useful defaults, but they may
not be optimal for your application. For example, hdlsetup sets a default Simulation
stop time of 10 s. A total simulation time of 1000 s would be more realistic for a test of
3-24
HDL Code Generation from a Simulink Model
the sfir_fixed example model. If you would like to change the simulation time, enter
the desired value into the Simulation stop time field of the Simulink window.
See the “Model Parameters” table in the “Model and Block Parameters” section of the
Simulink documentation for a summary of model parameters.
In this section, you will use the makehdl function to generate code for a VHDL entity
from the symmetric_fir subsystem of the example model. makehdl also generates
script files for third-party HDL simulation and synthesis tools.
makehdl lets you specify numerous properties that control various features of the
generated code. In this example, you will use the makehdl property defaults.
Before generating code, make sure that you have completed the steps described in
“Creating a Folder and Local Model File” on page 3-23 and “Initializing Model
Parameters with hdlsetup” on page 3-23.
To generate code:
1 Select Current Folder from the Desktop menu in the MATLAB window. This
displays the MATLAB Current Folder browser, which lets you easily access your
working folder and the files that will be generated within it.
2 At the MATLAB prompt, type the command
makehdl('sfir_fixed/symmetric_fir')
This command directs HDL Coder to generate code from the symmetric_fir
subsystem within the sfir_fixed model, using default property values.
3 As code generation proceeds, HDL Coder displays progress messages. The process
should complete with the message
### HDL Code Generation Complete.
Observe that the names of generated files in the progress messages are hyperlinked.
After code generation completes, you can click these hyperlinks to view the files in
the MATLAB Editor.
makehdl compiles the model before generating code. Depending on model display
options (such as port data types, etc.), the appearance of the model may change after
code generation.
3-25
3 Tutorials
4 By default, makehdl generates VHDL code. Code files and scripts are written to a
target folder. The default target folder is a subfolder of your working folder, named
hdlsrc.
A folder icon for the hdlsrc folder is now visible in the Current Folder browser. To
view generated code and script files, double-click the hdlsrc folder icon.
5 The files that makehdl has generated in the hdlsrc folder are
In this section, you use the test bench generation function, makehdltb, to generate a
VHDL test bench. The test bench is designed to drive and verify the operation of the
symmetric_fir entity that was generated in the previous section. A generated test
bench includes
• Stimulus data generated by signal sources connected to the entity under test.
• Output data generated by the entity under test. During a test bench run, this data is
compared to the outputs of the VHDL model, for verification purposes.
• Clock, reset, and clock enable inputs to drive the entity under test.
• A component instantiation of the entity under test.
• Code to drive the entity under test and compare its outputs to the expected data.
3-26
HDL Code Generation from a Simulink Model
This exercise assumes that your working folder is the same as that used in the previous
section. This folder now contains an hdlsrc folder containing the previously generated
code.
makehdltb('sfir_fixed/symmetric_fir')
This command generates a test bench that is designed to interface to and validate
code generated from symmetric_fir (or from a subsystem with a functionally
identical interface). By default, VHDL test bench code, as well as scripts, are
generated in the hdlsrc target folder.
2 As test bench generation proceeds, HDL Coder displays progress messages. The
process should complete with the message
• symmetric_fir_tb.vhd: VHDL test bench code and generated test and output
data.
• symmetric_fir_tb_compile.do: Mentor Graphics ModelSim
compilation script (vcom commands). This script compiles and loads both
the entity to be tested (symmetric_fir.vhd) and the test bench code
(symmetric_fir_tb.vhd).
• symmetric_fir_tb_sim.do: Mentor Graphics ModelSim script to initialize the
simulator, set up wave window signal displays, and run a simulation.
4 If you want to view the generated test bench code in the MATLAB Editor, double-
click the symmetric_fir.vhd file icon in the Current Folder browser. You may
want to study the code while referring to the makehdltb reference documentation,
which describes the default actions of the test bench generator.
3-27
3 Tutorials
5 Before proceeding to the next section, close files you have opened in the editor. Then,
click the Go Up One Level button in the Current Folder browser, to set the current
folder back to your sl_hdlcoder_work folder.
You can now take the previously generated code and test bench to an HDL simulator for
simulated execution and verification of results. See “Simulating and Verifying Generated
HDL Code” on page 3-39 for an example of how to use generated test bench and script
files with the Mentor Graphics ModelSim simulator.
The procedures for generating Verilog code differ only slightly from those for generating
VHDL code. This section provides an overview of the command syntax and the generated
files.
By default, makehdl generates VHDL code. To override the default and generate Verilog
code, you must pass in a property/value pair to makehdl, setting the TargetLanguage
property to 'verilog', as in this example.
makehdl('sfir_fixed/symmetric_fir','TargetLanguage','verilog')
The previous command generates Verilog source code, as well as scripts for the
simulation and the synthesis tools, in the default target folder, hdlsrc.
3-28
HDL Code Generation from a Simulink Model
The makehdltb syntax for overriding the target language is exactly the same as that for
makehdl. The following example generates Verilog test bench code to drive the Verilog
module, symmetric_fir, in the default target folder.
makehdltb('sfir_fixed/symmetric_fir','TargetLanguage','verilog')
• symmetric_fir_tb.v: Verilog test bench code and generated test and output data.
• symmetric_fir_tb_compile.do: Mentor Graphics ModelSim compilation script
(vlog commands). This script compiles and loads both the entity to be tested
(symmetric_fir.v) and the test bench code (symmetric_fir_tb.v).
• symmetric_fir_tb_sim.do: Mentor Graphics ModelSim script to initialize the
simulator, set up wave window signal displays, and run a simulation.
The following listing shows the commands and responses from a test bench session using
the generated scripts:
ModelSim> do symmetric_fir_tb_compile.do
# Model Technology ModelSim SE vlog 6.0 Compiler 2004.08 Aug 19 2004
# -- Compiling module symmetric_fir
#
# Top level modules:
# symmetric_fir
# Model Technology ModelSim SE vlog 6.0 Compiler 2004.08 Aug 19 2004
# -- Compiling module symmetric_fir_tb
#
# Top level modules:
# symmetric_fir_tb
ModelSim>do symmetric_fir_tb_sim.do
# vsim work.symmetric_fir_tb
# Loading work.symmetric_fir_tb
# Loading work.symmetric_fir
# **** Test Complete. ****
# Break at
C:/work/sl_hdlcoder_work/vlog_code/symmetric_fir_tb.v line 142
# Simulation Breakpoint:Break at
C:/work/sl_hdlcoder_work/vlog_code/symmetric_fir_tb.v line 142
# MACRO ./symmetric_fir_tb_sim.do PAUSED at line 14
3-29
3 Tutorials
You can view and edit options and parameters that affect HDL code generation in the
Configuration Parameters dialog box, or in the Model Explorer.
The following figure shows the top-level HDL Code Generation pane in the
Configuration Parameters dialog box.
3-30
HDL Code Generation from a Simulink Model
The following figure shows the top-level HDL Code Generation options pane in the
Model Explorer.
In the code generation exercises that follow, you use the Configuration Parameters
dialog box to view and set HDL Coder options and controls. The exercises use the
sfir_fixed model (see “The sfir_fixed Model” on page 3-16) in basic code generation
and verification steps.
In this section you will setup the folder and a local copy of the example model.
Creating a Folder
1 Start MATLAB.
2 Create a folder named sl_hdlcoder_work, for example:
3-31
3 Tutorials
mkdir C:\work\sl_hdlcoder_work
You will use sl_hdlcoder_work to store a local copy of the example model and to
store folders and code generated by HDL Coder. The location of the folder does not
matter, except that it should not be within the MATLAB folder tree.
3 Make the sl_hdlcoder_work folder your working folder, for example:
cd C:\work\sl_hdlcoder_work
1 To open the model, type the following command at the MATLAB prompt:
sfir_fixed
2 Save a local copy of the sfir_fixed model to your working folder.
3 Leave the sfir_fixed model open and proceed to the next section.
HDL Coder option settings are displayed as a category of the model's active configuration
set. You can view and edit these options in the Configuration Parameters dialog box, or
in the Model Explorer. This discussion uses the Configuration Parameters dialog box.
3-32
HDL Code Generation from a Simulink Model
The HDL Code Generation pane contains top-level options and buttons that
control the HDL code generation process. Several other categories of options are
available under the HDL Code entry. This exercise uses a small subset of these
options, leaving the others at their default settings.
Before generating code, you must set some parameters of the model. Rather than
doing this manually, use the hdlsetup command. The hdlsetup command uses the
set_param function to set up models for HDL code generation quickly and consistently.
3-33
3 Tutorials
hdlsetup('sfir_fixed')
2 Save the model with its new settings.
Before continuing with code generation, consider the settings that hdlsetup applies to
the model.
hdlsetup configures Solver options that are recommended or required by HDL Coder.
These options are:
hdlsetup also configures the model start and stop times and fixed-step size as follows:
If Fixed step size is set to auto the step size is chosen automatically, based on the
sample times specified in the model. In the example model, only the Signal From
Workspace block specifies an explicit sample time (1 s); the other blocks inherit this
sample time.
The model start and stop times determine the total simulation time. This in turn
determines the size of data arrays that are generated to provide stimulus and output
data for generated test benches. For the example model, computation of 10 seconds of
test data does not take a significant amount of time. Computation of sample values for
more complex models can be time consuming. In such cases, you may want to decrease
the total simulation time.
3-34
HDL Code Generation from a Simulink Model
The remaining parameters set by hdlsetup control error severity levels, data logging,
and model display options. If you want to view the complete set of model parameters
affected by hdlsetup, open hdlsetup.m in the MATLAB Editor.
The model parameter settings provided by hdlsetup are intended as useful defaults,
but they may not be optimal for your application. For example, hdlsetup sets a default
Simulation stop time of 10 s. A total simulation time of 1000 s would be more realistic
for a test of the sfir_fixed example model. If you would like to change the simulation
time, enter the desired value into the Simulation stop time field of the Simulink
Editor.
See the “Model Parameters” table in the “Model and Block Parameters” section of the
Simulink documentation for a summary of model parameters.
HDL Coder generates code from either the current model or from a subsystem at the root
level of the current model. You use the Generate HDL for menu to select the model or
subsystem from which code is to be generated. Each entry in the menu shows the full
path to the model or one of its subcomponents.
The top-level HDL Code Generation options are now set as follows:
3-35
3 Tutorials
Before generating code, select Current Folder from the Desktop menu in the MATLAB
window. This displays the Current Folder browser, which lets you access your working
folder and the files that will be generated within it.
To generate code:
3-36
HDL Code Generation from a Simulink Model
Observe that the names of generated files in the progress messages are hyperlinked.
After code generation completes, you can click these hyperlinks to view the files in
the MATLAB Editor.
HDL Coder compiles the model before generating code. Depending on model display
options (such as port data types, etc.), the appearance of the model may change after
code generation.
3 A folder icon for the hdlsrc folder is now visible in the Current Folder browser. To
view generated code and script files, double-click the hdlsrc folder icon.
4 The files that were generated in the hdlsrc folder are:
At this point, the Generate HDL for, Language, and Folder fields are set as they
were in the previous section. Accordingly, you can now generate VHDL test bench code
to drive the VHDL code generated previously for the sfir_fixed/symmetric_fir
subsystem. The code will be written to the same target folder as before.
3-37
3 Tutorials
• symmetric_fir_tb.vhd: VHDL test bench code, with generated test and output
data.
3-38
HDL Code Generation from a Simulink Model
You can now take the generated code and test bench to an HDL simulator for simulated
execution and verification of results. See “Simulating and Verifying Generated HDL
Code” on page 3-39 for an example of how to use generated test bench and script files
with the Mentor Graphics ModelSim simulator.
The procedure for generating Verilog code is the same as for generating VHDL code (see
“Generating a VHDL Entity from a Subsystem” on page 3-25 and “Generating VHDL
Test Bench Code” on page 3-26), except that you select Verilog from the Language
field of the HDL Code Generation options.
Note: This section requires the use of the Mentor Graphics ModelSim simulator.
This section assumes that you have generated code from the sfir_fixed model as
described in either of the following exercises:
In this section you compile and run a simulation of the previous generated model and
test bench code. The scripts generated by HDL Coder let you do this with just a few
simple commands. The procedure is the same, whether you generated code in the
command line environment or in the GUI.
3-39
3 Tutorials
2 Set the working folder to the folder in which you previously generated code.
ModelSim>cd C:/work/sl_hdlcoder_work/hdlsrc
3 Use the generated compilation script to compile and load the generated model and
text bench code. The following listing shows the command and responses.
ModelSim>do symmetric_fir_tb_compile.do
# Model Technology ModelSim SE vcom 6.0 Compiler 2004.08 Aug 19 2004
# -- Loading package standard
# -- Loading package std_logic_1164
# -- Loading package numeric_std
# -- Compiling entity symmetric_fir
# -- Compiling architecture rtl of symmetric_fir
# Model Technology ModelSim SE vcom 6.0 Compiler 2004.08 Aug 19 2004
# -- Loading package standard
# -- Loading package std_logic_1164
# -- Loading package numeric_std
# -- Compiling package symmetric_fir_tb_pkg
# -- Compiling package body symmetric_fir_tb_pkg
# -- Loading package symmetric_fir_tb_pkg
# -- Loading package symmetric_fir_tb_pkg
# -- Compiling entity symmetric_fir_tb
# -- Compiling architecture rtl of symmetric_fir_tb
# -- Loading entity symmetric_fir
4 Use the generated simulation script to execute the simulation. The following listing
shows the command and responses. The warning messages are benign.
ModelSim>do symmetric_fir_tb_sim.do
# vsim work.symmetric_fir_tb
# Loading C:\Applications\ModelTech_6_0\win32/../[Link]
# Loading C:\Applications\ModelTech_6_0\win32/../ieee.std_logic_1164(body)
# Loading C:\Applications\ModelTech_6_0\win32/../ieee.numeric_std(body)
# Loading work.symmetric_fir_tb_pkg(body)
# Loading work.symmetric_fir_tb(rtl)
# Loading work.symmetric_fir(rtl)
# ** Warning: NUMERIC_STD."<": metavalue detected, returning FALSE
# Time: 0 ns Iteration: 0 Instance: /symmetric_fir_tb
.
.
.
# ** Warning: NUMERIC_STD.TO_INTEGER: metavalue detected, returning 0
# Time: 0 ns Iteration: 1 Instance: /symmetric_fir_tb
# ** Note: **************TEST COMPLETED **************
# Time: 140 ns Iteration: 1 Instance: /symmetric_fir_tb
The test bench termination message indicates that the simulation has run to
completion without comparison errors.
3-40
HDL Code Generation from a Simulink Model
6 Exit the Mentor Graphics ModelSim simulator when you finish viewing signals.
7 Close files you have opened in the MATLAB Editor. Then, click the Go Up One
Level button in the Current Folder browser, to set the current folder back to your
work folder.
3-41