Cosimulation for Testing Filter Component Using MATLAB Testbench
This example shows how to run either Siemens® ModelSim™ and Questa™ or Cadence® Xcelium™ with MATLAB® in batch mode to test an HDL component using a MATLAB testbench, by using the HDL Verifier™ function matlabtb
. The function matlabtb
facilitates this testing by feeding MATLAB-generated input data into the HDL component and feeding the output from the component back into MATLAB. When you generate complex waveforms in HDL, using matlabtb
enables you to spend more time running tests than writing test waveforms. matlabtb
also reads the results, so the testing process can leverage MATLAB to apply data transformations to the output before plotting. For more information on writing testbenches for cosimulation, see Create a MATLAB Testbench.
Design Task
In this example, you create a MATLAB testbench for an HDL design under test (DUT). You call the testbench from within the HDL simulator. You run the testbench from the command line in the HDL simulator and MATLAB.
MATLAB generates a sine wave with an adjustable frequency, which is fed into a low-pass filter implemented in Verilog®. matlabtb
passes the filter outputs to MATLAB for plotting. This process of testing is possible with MATLAB and with the HDL simulator in command-line mode. This diagram shows an overview of the simulation.
This example considers the point of view of an HDL developer, so the main emphasis of the example is on using the HDL simulator and command-line terminal to perform testing and using MATLAB as an aid for supplying inputs and interpreting outputs.
Overview of Design and Script Files
This example uses two types of files: design files and script files.
Design Files
The file
cosim_lowpass_filter.v
contains a low-pass filter with a sample time of 10 ns (a sample rate of 100 MHz), a passband Fpass = 20 MHz and a stopband Fstop = 25 MHz.The file
cosim_lowpass_filtertb.m
contains the MATLAB testbench for this lowpass filter. The testbench uses variablesticks
andfrequency
to create a sinusoid to pass into the filter before interpreting and plotting the outputs.
Script Files
The file
qcommands_cosim.tcl
contains the TCL commands that are sent into ModelSim/QuestaSim for cosimulation to occur.The file
tclcmd_cosim_xc.m
contains the TCL commands that are sent into Xcelium for cosimulation to occur.
In this example, because you run the HDL simulator executables and MATLAB from a terminal, you must include them on the system path for this example to work.
Run Cosimulation
To start the cosimulation, open MATLAB and run the hdldaemon
command for communication between the HDL simulator and MATLAB. To complete these actions, use these commands based on the operating system.
Windows:
matlab -nodesktop -r "hdldaemon"
Linux:
xterm -e "matlab -nodesktop -r "hdldaemon"" &
This command opens MATLAB in a separate terminal, in headless mode, while the HDL Link MATLAB server starts. The terminal that is running MATLAB displays this message:
"HDLDaemon shared memory server is running with 0 connections"
After this message appears, enter these commands at the MATLAB command prompt.
global frequency; global ticks; frequency = 1e6; ticks = 0;
These commands define and initialize the two variables that control the testbench file cosim_lowpass_filtertb.m
. The variable frequency
represents the frequency of the input wave sent by the testbench, and the variable ticks
indicates how much simulator time has passed since the simulation started and is used to plot the output.
Next, start the cosimulation. Enter the applicable command (based on the simulator and operating system) into a MATLAB command window.
ModelSim/QuestaSim
Windows and Linux:
Launch ModelSim/QuestaSim and execute TCL commands in the simulator by entering this command.
vsim('tclstart','do qcommands_cosim.tcl');
Xcelium
Linux:
1. Run the MATLAB script tclcmd_cosim_xc.m
by entering this command at the MATLAB command prompt.
tclcmd_cosim_xc;
2. Launch Xcelium in batch mode by entering this command at the MATLAB command prompt.
nclaunch('tclstart', tclcmds);
After the cosimulation is complete, MATLAB should plot the filtered and unfiltered results, similar to these figures.
Observe that the output of the low-pass filter implemented in Verilog is almost the same as the input signal with only a small drop in amplitude. This is expected as low frequency (1 MHz) sinusoid is passed through the low-pass filter implemented in Verilog.
Adjust Parameters and Rerun Cosimulation
To run the simulation again with a different input from the MATLAB test bench, you do not need to restart the HDL simulator or MATLAB itself. Instead, adjust the two variables defined earlier: ticks
and frequency
. After you change frequency
to your desired value and reset ticks
to 0
, run the simulation from within the HDL terminal window to produce the new results.
For example, change frequency
and reset ticks
by entering these commands at the MATLAB command prompt.
frequency = 25e6; ticks = 0;
Then, in the terminal running the HDL simulator, enter this command.
run 10000ns
Observe that the amplitude of the higher frequency (25 MHz) sinusoid signal is reduced by the low-pass filter implemented in Verilog.
Summary
This example shows how to use ModelSim/QuestaSim or Xcelium with MATLAB to update and run simulations by adjusting the characteristics of the input wave. MATLAB defines and controls the characteristics of the input, and displays the output, even when you run MATLAB in a terminal. Using MATLAB in conjunction with an HDL simulator enables you to quickly run through many tests.
In this example, only the variable frequency
controls the input. However, you can use MATLAB to generate a large number of complex test waveforms before using these waveforms in cosimulation tests and to display the results. Using MATLAB to create these test waveforms streamlines the design process, by cutting down the time needed to construct and add in various test waveforms, and removing the time needed to process the simulation results into visual data.
See Also
matlabtb
hdldaemon
vsim
nclaunch
Related Topics
Copyright 2009-2023 The MathWorks, Inc.