This tutorial guides you through the basic steps for setting up an HDL Verifier™ cosimulation that uses MATLAB® and the HDL Simulator. This cosimulation verifies an HDL design using a MATLAB component as the test bench. In this tutorial, you perform the steps to cosimulate MATLAB with the HDL simulator to verify the suitability of a raised cosine filter written in Verilog®.
Note
This tutorial requires MATLAB, HDL Verifier, Fixed-Point Designer™, and ModelSim® or Incisive® HDL simulator. This tutorial also assumes that you have read Import HDL Code for MATLAB Function.
The HDL test bench instantiates two raised-cosine filter components: one is implemented in HDL, and the other is associated with a MATLAB callback function. The test bench also generates stimulus to both filters and compares their outputs.
To help others access copies of the tutorial files, set up a folder for your own tutorial work by following these instructions:
Create a folder outside the scope of your MATLAB installation
folder into which you can copy the tutorial files. The folder must
be writable. This tutorial assumes that you create a folder named MyTests
.
Copy all the files located in the following MATLAB folder to the folder you created:
matlabroot\toolbox\edalink\foundation\hdllink\demo_src\tutorial
where matlabroot
is the MATLAB root
directory on your system.
You now have the following files in your working folder:
filter_tb.v
mycallback_solution.m
rcosflt_beh.v
rcosflt_rtl.v
rcosflt_tb.mdl
(not used in this
tutorial)
Start MATLAB.
Set the folder you created in Tutorial: Set Up Tutorial Files (MATLAB) as your current folder in MATLAB.
At the MATLAB command prompt, enter:
>>cosimWizard
This command launches the Cosimulation Wizard.
This tutorial leads you through the following wizard pages, designed to assist you in creating an HDL Verifier component function:
In the Cosimulation Type page, perform the following steps:
Change HDL cosimulation with option
set to MATLAB
.
If you are using ModelSim, leave HDL
Simulator option as ModelSim
.
If you are using Incisive, change HDL Simulator option
to Incisive
.
Leave the default option Use HDL simulator executables on the system path option if the HDL simulator executables appear on your system path.
If the executables do not appear in the path, specify the HDL simulator path as described in Cosimulation Type—MATLAB Function.
Click Next to proceed to the HDL Files page.
In the HDL Files page, perform the following steps:
Add HDL files to file list.
Click Add and browse to the directory you created in Tutorial: Set Up Tutorial Files (MATLAB).
Select the Verilog files filter_tb.v
, rcosflt_rtl.v
,
and rcosflt_beh.v
. You can select multiple files
in the file browser by holding down the CTRL key
while selecting the files with the mouse.
Review the file in the file list with the file type identified as you expected.
Click Next to proceed to the HDL Compilation page.
Cosimulation Wizards lists the default commands in the Compilation Commands window. You do not need to change these defaults for this tutorial.
Examine compilation commands.
ModelSim users: Your HDL Compilation pane looks similar to the following.
Incisive users: Your HDL Compilation commands will look similar to the following:
ncvlog -64bit -update "/mathworks/home/user/MyTests/filter_tb.v" ncvlog -64bit -update "/mathworks/home/user/MyTests/rcosflt_beh.v" ncvlog -64bit -update "/mathworks/home/user/MyTests/rcosftl_rtl.v"
Click Next to proceed to the HDL Modules pane.
The MATLAB console displays the compilation log. If an error occurs during compilation, that error appears in the Status area. Change whatever settings you can to remove the error before proceeding to the next step.
In the HDL Modules pane, perform the following steps:
Specify the name of the HDL module/entity for cosimulation.
At Name of HDL module to cosimulate with,
select filter_tb
from the drop-down list
to specify the Verilog module you will use for cosimulation.
If you do not see filter_tb
in the drop-down
list, you can enter it manually.
For Connection method, select Shared
Memory
if your firewall policy does not allow TCP/IP
socket communication.
Click Next to proceed to the Callback Schedule page.
Cosimulation Wizard launches the HDL simulator in the background console using the specified HDL module and simulation options. After the wizard launches the HDL simulator, the Callback Schedule page appears. On Windows® systems, the console remains open. Do not close the console; the application closes this window upon completion.
In the Callback Schedule page, perform the following steps:
Leave Callback type as matlabcp
(default).
This type instructs the Cosimulation Wizard to create a MATLAB callback
function as a component for cosimulation with the HDL simulator.
Leave Callback function name as callback_fcn
.
The wizard gives this name to the generated MATLAB callback function.
For HDL component, click Browse.
Click the expander icon next to filter_tb
to
expand the selection. Select u_rcosflt_beh
,
and click OK. You have specified to the Cosimulation
Wizard that the HDL simulator associate this component with the MATLAB callback
function.
Set Trigger mode to Rising
Edge
.
For Trigger Signal, click Browse.
Click the expander icon next to filter_tb
to
expand the selection. Select u_rcosflt_beh
.
In the ports list on the right, select clk
.
Click OK.
Click Add. The Cosimulation
Wizard generates the corresponding matlabcp
command
that associates the HDL module u_rcosflt_beh with
the MATLAB function callback_fcn, as shown
in the following image:
For more information on the callback parameters, see the reference
page for matlabcp
.
Click Next to proceed to the Generate Script page.
Leave Launch HDL simulator after exiting this dialog selected.
Click Finish to complete the Cosimulation Wizard session and generate scripts.
After you click Finish in the Cosimulation Wizard, the application generates three HDL files in the current directory:
compile_hdl_design.m
: For recompiling
the HDL design
launch_hdl_simulator.m
: To relaunch
the MATLAB server and start the HDL simulator.
callback_fcn.m
: The MATLAB callback
function
In addition to launching the HDL simulator, HDL Verifier software
opens the MATLAB Editor and loads callback_fcn.m
(partial
image shown).
The generated template comprises four parts:
Initialize internal state(s) of callback function
Read signal from HDL component
Write signal to HDL component
Update internal state(s)
You modify this template to model a raised cosine filter in MATLAB following the instructions as shown in the following sections.
Note
You can find a completed modified callback function in mycallback_solution.m
.
This function resides in the directory you copied the tutorial files
into. You can use this file to overwrite the one in your current directory.
Name the file "callback_fcn.m
", and change the
function name to callback_fcn
.
Define two internal states: a 49-element vector to hold filter inputs and a vector of filter coefficients.
Edit callback_fcn.m
so that the internal
state section contains the following code:
Read the filter input and convert it to a decimal number in MATLAB.
Edit callback_fcn.m
so that the read signal
section contains the following code:
The input "reset" signal controls the filter output. If reset is low, then the output is the product of previous inputs and filter coefficients. MATLAB converts the decimal result to a multivalued logic output of the HDL component.
Edit callback_fcn.m
so that the write signal
section contains the following code:
Use the filter input to update the internal 49-element state.
Edit callback_fcn.m
so that the update internal
states section contains the following code:
Switch to the HDL simulator and enter the following command in the HDL simulator console:
run 200 ns
You see the following output displayed in the HDL simulator:
These messages indicate that the output of the HDL component matches the behavioral output of the MATLAB component.