Documente Academic
Documente Profesional
Documente Cultură
Engineers who use Model-Based Design to target FPGAs or ASICs can design and simulate
systems with MATLAB®, Simulink®, and Stateflow® and then generate bit-true, cycle-accurate,
synthesizable Verilog® and VHDL® code using Simulink HDL Coder™.
Alternatively, engineers who specifically target Xilinx FPGAs can use a Xilinx library of bit- and
cycle-true blocks to build a model in Simulink. They can then use Xilinx® System Generator for
DSP, a plug-in to Simulink code generation software, to automatically generate synthesizable
hardware description language (HDL) code mapped to pre-optimized Xilinx algorithms.
Used independently, each approach provides an effective FPGA design flow. Some projects,
however, benefit from a mixture of approaches – a workflow that combines the native Simulink
workflow, device- independent code, and code readability offered by Simulink HDL Coder,
with the Xilinx FPGA-specific features and optimizations offered by Xilinx System Generator.
2 Application Guideline
About this Guide
This guide describes an integrated workflow for engineers who want to:
• Integrate a Xilinx System Generator design into a Simulink model for HDL code generation
• Implement part of a Simulink design, intended for HDL code generation, in Xilinx System
Generator
Appendix A, “Using Verilog for the Integration Workflow,” describes changes to the workflow needed
to generate Verilog code instead of VHDL.
Appendix B, “Synthesizing the Design Using Synplify Pro,” explains how to use Synplicity® Synplify
Pro® instead of Xilinx Synthesis Technology (XST) to synthesize the design.
For more information on Simulink HDL Coder, refer to the product documentation: www.mathworks.
com/products/slhdlcoder/
Required Software
The example model and related scripts are distributed with this guide in a .zip file,
hdlcoderrecon_xsg_files_8a.zip. Simulation and code generation from the model have been
tested with the following versions of the software:
• Simulink 7.1
• Simulink HDL Coder 1.3 (requires Fixed-Point Toolbox™ 2.2 and Simulink Fixed Point™ 5.6)
• Xilinx® System Generator for DSP Version 10.1.3 (note: 10.1 Service Pack 3 or later is required for
the integration)
To simulate, synthesize, and implement HDL code generated from the model, the following software
is also required:
The synthesis workflow described in Appendix B has been tested with the following version of software:
The top level of the example model is shown in Figure 1. This guide focuses on the image reconstruc-
tion subsystem shown in Figure 2. This subsystem is the device under test (DUT), from which HDL
code is to be generated. In the DUT, the counters and back projection subsystems are implemented
using Simulink blocks, while the filtering_xil subsystem is implemented using Xilinx System
Generator blocks.
4 Application Guideline
The filtering_xil subsystem is shown in Figure 3. When Simulink HDL Coder generates code for
the DUT, it implements the filtering_xil subsystem in HDL as a black box. Instead of generating
code for the content of the filtering_xil subsystem, Simulink HDL Coder creates a black box
interface — a VHDL component or Verilog module that includes only the HDL input/output port
definitions for the subsystem.
• Fixed-point settings in Xilinx Gateway In blocks are independent of the Simulink fixed-point data type.
When you route signals to and from the Xilinx subsystem through the Gateway blocks, you must verify
manually that the data types of Simulink and Xilinx fixed-point signals are consistent. For example, in
Figure 4, the data type of the Gateway In block, xproj_data, has been set to Fix_17_11 to match the
Simulink fixed-point type sfix17_En11 (a 17-bit signed fixed-point number with 11 fractional bits) of
the Inport block, proj_data.
6 Application Guideline
To help you verify that the Simulink and Xilinx data types are consistent across each Gateway block of
the Xilinx subsystem, a data type report is printed in the command window during code generation.
Figure 5 shows the report for the example model.
Figure 5. Data type report provided by Simulink HDL Coder during code generation.
1. Set Compilation to NGC Netlist. This setting creates a standalone Xilinx NGC binary netlist file,
which contains all the generated HDL code, cores, and constraint file information.
Use the default values (set to on) for the Include Clock Wrapper and Include Constraints File
options in the Settings subdialog.
2. Set Multirate implementation in Clocking Options to Clock Enables. Currently only single rate
models are supported for the integration workflow.
3. Set Simulink system period to match the value of the sample period of your Simulink model.
4. Select the Create testbench option (as shown) to generate test bench code for the Xilinx subsystem.
See the “HDL Simulation and Verification” section of this guide for further information.
In addition, the settings in Part, Synthesis tool, and Hardware description language should match
the corresponding Simulink HDL Coder property settings. A helper function is available in Simulink
HDL Coder to help you match the settings. See “Setting Code Generation Properties Using Helper
Function hdlsettingsxsg.m” in the next section for more details.
To incorporate Xilinx System Generator blocks in a Simulink HDL Coder design, you must encap-
sulate all Xilinx System Generator blocks in a subsystem, and specify the black box implementation
XilinxBlackBoxHDLInstantiation for the subsystem in a control file. The implementation
instructs Simulink HDL Coder software to create only an HDL interface to a subsystem, without gen-
erating any code for the contents of the subsystem. To match the HDL entity definition generated by
Xilinx System Generator, Simulink HDL Coder does the following:
1. Automatically matches the HDL I/O port names and component name
hen generating a black box interface, the names of the Gateway blocks are used to create the HDL
W
I/O port names. To match the clock wrapper entity created from NGC netlist compilation, a postfix
“_cw” is added to the Xilinx System Generator HDL component name.
Using NGC netlist, the subsystem implemented in Xilinx System Generator is presynthesized during
code generation. Therefore the subsystem should be treated as a black box during synthesis. To specify
that the Xilinx HDL component is a black box, VHDL attributes for XST and Synplify are inserted in
the generated code.
3. Provides implementation parameters to match the HDL global ports, and turn off VHDL configu-
ration statement
8 Application Guideline
The Xilinx black box implementation provides a number of implementation parameters for
customizing the generated HDL interface. You can add or remove global port(s), customize the HDL
entity name and global port names, turn on or off VHDL configuration statement, etc. You can use
the function hdlnewforeach to see all the available parameters.
To match the global ports of the Xilinx System Generator HDL entity, set the implementation
parameters in Table 1 to the values shown.
Figure 7. Specifying Xilinx black box implementation for the filtering_xil subsystem.
After specifying the Xilinx black box implementation, you need to specify code generation proper-
ties in the control file. The values of these properties should be consistent with the settings in the
Xilinx System Generator dialog box, and other settings in the generated HDL from System Generator.
To help you select correct code generation settings, Simulink HDL Coder provides a helper func-
tion called hdlsettingsxsg.m. The function takes the path to the Xilinx System Generator block
as input argument, programmatically obtains parameter settings in the block, and then specifies
Simulink HDL Coder code generation properties that are compatible with System Generator. The
function helps you do the following:
The property TargetLanguage is set to match the Hardware description language setting in the
Xilinx System Generator dialog box.
2. Specify synthesis script settings for the target synthesis tool and device
When necessary, the test bench properties ClockHighTime and ClockLowTime are set according
to the FPGA clock period setting in the Xilinx System Generator dialog box. Note that since the
value of ClockHighTime and ClockLowTime must be integer, they are only set when FPGA clock
period is an even integer. Otherwise, a warning is displayed.
In addition, the simulation script generated by Xilinx System Generator contains certain settings
required for HDL simulation. The properties HDLSimCmd and HDLSimTerm are set to incorporate
these settings to the simulation script generated by Simulink HDL Coder.
See the “HDL Simulation and Verification” section of this guide for further information on these
property settings.
After running the function hdlsettingsxsg.m, the code generation properties are printed to the
MATLAB command window, as shown in Figure 8. You can then copy and paste the property settings
to your control file, and attach the control file to your Simulink model.
10 Application Guideline
Figure 8. Output of helper function hdlsettingsxsg.m from the example model.
Simulink HDL Coder will not generate code for the Xilinx subsystem filtering_xil. Use the Xilinx
System Generator dialog box to generate this code separately with Xilinx System Generator.
Note: If you have modified settings in the Xilinx System Generator block, you may need to re-run
hdlsettingsxsg.m to create the latest code generation properties for Simulink HDL Coder, and
apply those properties to your control file again.
When the Synthesis tool option in the Xilinx System Generator block is set to XST, the helper
function hdlsettingsxsg.m sets the properties HDLSynthFilePostfix, HDLSynthInit,
HDLSynthCmd, and HDLSynthTerm to create a Tcl script for use with XST. The generated Tcl script
does the following:
2. Adds HDL files generated by Simulink HDL Coder to the ISE project
3. Sets target device according to settings in the Xilinx System Generator block
To synthesize the design, run the generated synthesis script in the Xilinx Tcl Shell (xtclsh), either
from ISE Project Navigator or from the command line. For more details on the Xilinx Tcl Shell, refer
to the Xilinx ISE documentation. You can also create a new project in ISE Project Navigator using the
New Project Wizard, and synthesize the design manually.
Note that the resources reported by ISE after synthesis does not include the Xilinx component
(filtering_xil_cw), because the component is synthesized as a black box.
12 Application Guideline
To implement the design after synthesis, the netlist for the black box component filtering_xil_cw
must be provided to ISE. You can either set the Translate property Macro Search Path to the location of
the NGC netlist filtering_xil_cw.ngc generated by Xilinx System Generator, or copy the file to your
ISE project directory.
When the design is ready to be implemented, run Implement Design to execute Translate, Map, and
Place & Route.
The Xilinx ISE design summary shown in Figure 10 shows the logic utilization and performance of
the example design after implementation.
Simulink HDL Coder generates a test bench compilation script to compile the design for HDL simula-
tion. The helper function hdlsettingsxsg.m adds the necessary settings to the compilation script
by specifying the properties HDLCompileInit and HDLCompileVHDLCmd / HDLCompileVerilogCmd.
The generated compilation script does the following:
To compile and simulate the portion of the design implemented with System Generator, the
locations of the Xilinx libraries Unisim, Simprims, and XilinxCoreLib must be set in ModelSim
via the vmap command. In the compilation script, these libraries are mapped to the default paths –
for example, C:/Xilinx/vhdl/mti_se/unisim for the Unisim library in VHDL.
To match the library paths to those in your working environment, copy the settings generated by
hdlsettingsxsg.m to your control file, and modify the paths among those settings before gener-
ating code from your design.
The compilation script vcom.do generated by Xilinx System Generator is used to compile the
Xilinx HDL files. Before vcom.do is run, the current directory is changed to the System Generator
target directory, in order to access other Xilinx files (such as .mif and .coe) during simulation.
The Simulink HDL Coder target directory is saved in the Tcl variable $hdlsrcdir.
Lastly, the HDL files generated by Simulink HDL Coder are compiled. Because the current direc-
tory is the System Generator target directory, the path to the HDL files is added to the compile
command, such as:
vcom $hdlsrcdir/image_reconstruction.vhd
Figure 11 shows the generated test bench compilation script of the example design.
14 Application Guideline
Figure 12 shows the generated test bench simulation script of the example design. The settings incor-
porated from vsim.do are highlighted.
onbreak resume
onerror resume
vsim -novopt -t ps work.image_reconstruction_tb
add wave sim:/image_reconstruction_tb/u_image_reconstruction/clk
add wave sim:/image_reconstruction_tb/u_image_reconstruction/reset
add wave sim:/image_reconstruction_tb/u_image_reconstruction/clk_enable
add wave sim:/image_reconstruction_tb/u_image_reconstruction/proj_data
add wave sim:/image_reconstruction_tb/u_image_reconstruction/theta_reset
add wave sim:/image_reconstruction_tb/u_image_reconstruction/ce_out
add wave sim:/image_reconstruction_tb/u_image_reconstruction/image_data
add wave sim:/image_reconstruction_tb/image_data_ref
set NumericStdNoWarnings 1
run 0
set NumericStdNoWarnings 0
run -all
The function hdlsettingsxsg.m helps you setup many code generation properties that are neces-
sary to simulate the HDL files generated by Simulink HDL Coder and Xilinx System Generator. In
some cases, you may need to set additional properties to simulate the design successfully.
It is recommended to use the –novopt option with the ModelSim command vsim for HDL simula-
tion. You can add the option to the HDLSimCmd setting generated by hdlsettingsxsg.m, as shown
in Figure 13.
Figure 13. Adding -novopt flag to the simulation script property HDLSimCmd.
By default, test bench inputs generated by Simulink HDL Coder are driven with the value ‘X’
before reset. If test bench inputs are directly connected to registers in the Xilinx subsystem, simu-
lation mismatch may occur at the beginning of HDL simulation, until the initial undefined values
are propagated out of the registers. Setting InitializeTestBenchInputs to on can prevent this
temporary mismatch.
Table 2 summarizes the additional property settings you may need for your design.
T
he example model requires a long simulation time, resulting in a large test bench file. To reduce
the size of the test bench file, consider setting the total simulation time to a smaller value before
generating HDL and test benches. In this example, a simulation time of 50,000 clock cycles (500
microseconds) is used for a good balance between run time and HDL functionality verification.
To generate a test bench from a Simulink model, type makehdltb in the MATLAB command window
or use the Simulink Configuration Parameters GUI. If you modify the simulation time, you must run
makehdl again (before running makehdltb) for the new simulation time to take effect in the test
bench.
Generate the Xilinx subsystem test bench, simulation files and scripts separately using Xilinx System
Generator. Select the Create testbench option, as shown previously in Figure 6.
To compile the design for simulation, run the test bench compilation script in the ModelSim
Transcript window:
do image_reconstruction_tb_compile.do
To simulate the design, run the test bench simulation script. Since the current directory has been
changed to the Xilinx System Generator target directory during compilation, the path of the simu-
lation script must be included when the script is run:
do $hdlsrcdir/image_reconstruction_tb_sim.do
Figure 14 shows the example model simulation results from ModelSim SE.
16 Application Guideline
Figure 14. ModelSim simulation window.
For design efforts that can benefit from a combination of Simulink and Xilinx capabilities, the inte-
gration of Simulink HDL Coder and Xilinx System Generator enables an effective workflow that
spans HDL code generation, synthesis and implementation, and simulation and verification.
The easiest way to create the empty module declaration is to copy the module declaration generated by
Xilinx System Generator and then remove from it everything but the port definitions. In this example, an
empty module declaration of the Xilinx subsystem filtering_xil_cw is needed. Figure 15 shows the
contents of the wrapper file filtering_xil_cw.v.
module filtering_xil_cw (
clk,
ce,
xlast_pixel,
xproj_data,
xqt,
xqt1,
xt,
xt1
);
input clk;
input ce;
input [0:0] xlast_pixel;
input [16:0] xproj_data;
input [7:0] xt;
input [7:0] xt1;
output [16:0] xqt;
output [16:0] xqt1;
endmodule
18 Application Guideline
After creating the wrapper file, you can add it to the synthesis script automatically using the con-
trol file property HDLSynthInit. Append the appropriate command to the HDLSynthInit setting
generated by the helper function hdlsettingsxsg.m, as shown in Figure 16. Alternatively, you can
manually add the wrapper file to the generated synthesis script.
Note that if the interface of the Xilinx subsystem in your Simulink model is changed, such as the
name of the subsystem or I/O port definition, you must modify the HDLSynthInit setting and
the wrapper file accordingly, and re-run code generation from Simulink HDL Coder and Xilinx
System Generator if necessary.
synthesis_dir = 'iseprj';
c.set('HDLSynthFilePostfix', '_xst.tcl',...
'HDLSynthInit', ['set src_dir ', target_dir, '\n',...
'set prj_dir ', synthesis_dir, '\n',...
'file mkdir ../$prj_dir\n',...
'cd ../$prj_dir\n',...
'project new %s.ise\n',...
'xfile add "../$src_dir/filtering_xil_cw.v"\n'],...
'HDLSynthCmd', 'xfile add "../$src_dir/%s"\n',...
'HDLSynthTerm', ['project set family virtex4\n',...
'project set device xc4vlx80\n',...
'project set package ff1148\n',...
'project set speed -10\n',...
'process run "Synthesize - XST"\n']);
Figure 16. Adding filtering_xil_cw.v to the synthesis .tcl script using control file property
HDLSynthInit.
Figure 17. Adding -novopt flag to the simulation script property HDLSimCmd for Verilog.
1. Creates a new project, and adds HDL files generated by Simulink HDL Coder to the project
2. Sets target device according to settings in the Xilinx System Generator block
3. Sets synthesis options, such as auto frequency, and turn off constraint file generation
4. Runs synthesis
The synthesis option write_apr_constraint is set to 0 in the synthesis Tcl script. When the option
is set to 1, Synplify Pro creates a Xilinx .ncf constraint file together with the .edf output netlist.
When the .edf netlist is added to an ISE project, ISE detects the .ncf file and automatically extracts
the constraint information contained in it.
In some cases, the extracted constraints may conflict with the timing constraints embedded in the
NGC netlist generated by System Generator. Setting write_apr_constraint to 0 prevents Synplify
Pro from writing the .ncf constraint file, and avoids any conflict. During implementation, the clock
period constraint embedded in the NGC netlist will be used by ISE to analyze sequential paths in the
entire design.
20 Application Guideline
To synthesize the design, run the generated synthesis script in Synplify Pro.
Figure 19 shows the Resource Usage report section from the Synplify Pro log file. Note that the
resources needed by the Xilinx component (filtering_xil_cw) are not included because the com-
ponent is synthesized as a black box.
Figure 19. The Resource Usage report in the Synplify Pro log file window.
The same can also be done programmatically using a .tcl script. The
image_recon_par.tcl script in the example files accomplishes this, as shown in Figure 20.
Figure 20. image_recon_par.tcl – Implement the design in ISE using Tcl script.
22 Application Guideline
Revision History