Cosimulation for Testing Filter Component Using MATLAB Test Bench
This example shows how to run either Mentor Graphics® ModelSim® and Questa® or Cadence® Xcelium™ with MATLAB® in batch mode to test an HDL component using a MATLAB test bench, 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 test benches for cosimulation, see Create a MATLAB Test Bench.
In this example, you create a MATLAB test bench for an HDL design under test (DUT). You call the test bench from within the HDL simulator. You run the test bench 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.
cosim_lowpass_filter.vcontains 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.
cosim_lowpass_filtertb.mcontains the MATLAB test bench for this lowpass filter. The test bench uses variables
frequencyto create a sinusoid to pass into the filter before interpreting and plotting the outputs.
qcommands_cosim.tclcontains the TCL commands that are sent into ModelSim/QuestaSim for cosimulation to occur.
tclcmd_cosim_xc.mcontains 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.
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.
matlab -nodesktop -r "hdldaemon"
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 test bench, 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.
Windows and Linux:
Launch ModelSim/QuestaSim and execute TCL commands in the simulator by entering this command.
1. Run the MATLAB script
tclcmd_cosim_xc.m by entering this command at the MATLAB command prompt.
2. Launch Xcelium in batch mode by entering this command at the MATLAB command prompt.
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:
frequency. After you change
frequency to your desired value and reset
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.
Observe that the amplitude of the higher frequency (25 MHz) sinusoid signal is reduced by the low-pass filter implemented in Verilog.
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.
Copyright 2009-2023 The MathWorks, Inc.