Main Content

Implement Spectrum Display Component in MATLAB

This example shows how to run MATLAB® with Mentor Graphics® ModelSim®/Questasim® to visualize the output of a larger HDL project, by using the HDL Verifier™ function matlabcp. matlabcp needs a MATLAB function modeling the component behavior, along with an empty HDL component with input and output ports declared. These files are necessary as matlabcp uses the empty HDL component as a shell for communication between MATLAB and the HDL simulator, while the MATLAB function provides the functionality of the component. Using matlabcp allows MATLAB to process the simulator data as the simulation progress, saving time needed to transform the original outputs into a visual format. For information on creating the necessary files for matlabcp, see Create a MATLAB Component Function.

In this example, you use matlabcp to visualize the spectrum of a signal from a VHDL® project, which consists of a sine wave generator, a lowpass filter, and an empty HDL file representing the spectrum display component. matlabcp uses the empty HDL file to send the project output into a MATLAB function, which buffers the data into frames of 128 samples. Each time a frame is completed, the MATLAB function plots the spectrum of the unfiltered and filtered data in the frame. This use of matlabcp for reading the HDL data streamlines the transition between raw simulator data and visual plot, as MATLAB performs the data processing and graphic creation in parallel with the HDL simulator. This diagram shows an overview of the cosimulation system.

This example considers the point of view of an HDL developer in mind, so the main emphasis of this example is on using the HDL simulator and command-line terminal to perform testing and using MATLAB to manipulate and visualize the data from the HDL simulation.

Overview of Design and Script Files

This example uses two types of files: design files and script files.

Design Files

  • The file sine_waves.vhd contains a frequency hopping sine wave implemented in VHDL.

  • The file spec_lowpass_filter.vhd contains a Filter Design HDL Coder™ product generated lowpass filter.

  • The file spec_display.vhd contains the empty component that matlabcp uses to feed the VHDL data into the MATLAB code.

  • The file specdisp_top.vhd contains the top-level wiring between the signal source, lowpass filter, and empty component for matlabcp.

  • The file specdisplay.m contains the MATLAB code that processes the inputs from the HDL simulator and plots their spectrum.

Script Files

  • The file qcommands_spec_w.tcl contains the commands that are sent into ModelSim/Questasim on Windows® for cosimulation to occur.

  • The file qcommands_spec_l.tcl contains the commands that are sent into ModelSim/Questasim on Linux® for cosimulation to occur.

Additionally, this example requires ModelSim/Questasim and MATLAB to be on the system path, as you must be able to run these products from the terminal.

Adjust the Script Files

This example uses the script files provided to link MATLAB and the HDL simulator through the use of two commands. These commands are a command that invokes the HDL simulator, and a command that uses a MATLAB shared library to create the connection for cosimulation. For this example to run, you need to change the command that invokes the HDL simulator, because the default command relies on an absolute path to the MATLAB shared library. Adjust the script files based on the operating system.

  • Windows: This figure shows the two required commands for cosimulation with Modelsim/Questasim on Windows. The file qcommands_spec_w.tcl contains these two commands. Line 3 is the call to open ModelSim/Questasim, where the -foreign supplies the path to the MATLAB shared library, such that ModelSim/Questasim loads in the shared library. Line 4 is the call to a function in the shared library that connects the two programs together.

For this example to work, you must change line 3 in qcommands_spec_w.tcl. In this command, replace matlabroot with the installation location of MATLAB so that the shared library can be located and loaded.

  • Linux: This figure shows the two required commands for cosimulation with Modelsim/Questasim on Linux. The file qcommands_spec_l.tcl contains these two commands. Line 3 is the call to open ModelSim/Questasim, where the -foreign supplies the path to the MATLAB shared library, such that ModelSim/Questasim loads in the shared library. Line 4 is the call to a function in the shared library that connects the two programs together.

For this example to work, you must change line 3 in qcommands_spec_l.tcl. In this command, replace matlabroot with the installation location of MATLAB so that the shared library can be located and loaded.

Run the Cosimulation

To start cosimulation, open MATLAB and run the HDL Link MATLAB server 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 the applicable command based on operating system into a system terminal.

  • Windows: vsim -c -do qcommands_spec_w.tcl

  • Linux: vsim -c -do qcommands_spec_l.tcl

This command starts the cosimulation. Over the course of the simulation, MATLAB captures, processes, and displays multiple frames. These plots show the first and last frames of the simulation.

Summary

This example shows how to use MATLAB to interpret and display results after the HDL simulation has finished processing. Even though the simulator can display the unmodified results of the project, applying transformations to the results for visualization can be difficult. In this example, the spectrum of the frequency hopping sine wave was desired, but to obtain this spectrum, a Fourier transform is required. This requirement significantly complicates the process of coding, simulating, and verifying the results.

Using MATLAB and matlabcp can significantly speed up this visualization, as MATLAB can read data from the simulation and utilize built-in functionality to apply the Fourier transform and display the results. By using MATLAB in the verification process, the complexity of visualizing the data is reduced, streamlining the process of converting simulation results into visualized data.

See Also

matlabcp | hdldaemon | vsim | nclaunch

Related Topics

Copyright 2005-2021 The MathWorks, Inc.