Batch Mode Cosimulation for Testing a Filter Component

This example shows how to run Incisive® HDL Simulator in batch mode to test an HDL component with a MATLAB® test bench. In this example, we compile an HDL low-pass filter - designed and generated with the Filter Design HDL Coder™ - and then test its response using the HDL Verifier™ function matlabtb. The entire cosimulation is executed in batch mode.

In this example:

  • The filter has a sample time of 10 ns (a sample rate of 100 MHz) with a passband Fpass=20 MHz and a stopband Fstop=25 MHz.

  • The file lowpass_filter.v contains the low-pass filter generated by the Filter Design HDL Coder.

  • We create a temporary directory for the compiled and elaborated HDL files.

You must have the HDL simulator executables on your system path. Note that we do not run the Filter Design HDL Coder during this example.

Run the Entire Batch Mode Simulation

This example is divided into several steps that show how to write a script that performs the cosimulation between MATLAB and HDL simulator. However, for many batch-mode applications, you would run a single script to execute multiple simulation runs. You can run this entire example like a regression script. The rest of the example breaks down the script into sections.

Create Project Directory

Create a temporary working directory for compiling the project.

projdir = tempname;

Start the MATLAB Server

Start the MATLAB server using the HDL Verifier function hdldaemon. Set the 'socket' property to specify socket communication.

status = hdldaemon('socket',0);
socket_port = status.ipc_id; %Socket number to be used for cosimulation
HDLDaemon socket server is running on port 56463 with 0 connections

Specify Tcl Commands

Specify the Tcl commands to execute in the HDL simulator. These commands will compile the HDL design, load the cosimulation library, schedule the callback function, and drive the HDL signals.

srcfile = fullfile(matlabroot,'toolbox','edalink','extensions',...
unixsrcfile = ['"' strrep(srcfile,'\','/') '"'];
tclcmd = {  ['cd ',projdir],...
            ['exec ncvlog ' unixsrcfile],...
            'exec ncelab -access +wc lowpass_filter',...
            ['hdlsimmatlab lowpass_filter ', ...
            [' -input "{@matlabtb lowpass_filter 10ns -repeat 10ns -mfunc filtertb_batch_in -socket ' socket_port '}"'],...
             ' -input "{@force lowpass_filter.clk_enable 1 -after 0ns}"',...
             ' -input "{@force lowpass_filter.reset 1 -after 0ns 0 -after 22ns}"',...
             ' -input "{@force lowpass_filter.clk 1 -after 0ns 0 -after 5ns -repeat 10ns}"',...
             ' -input "{@deposit lowpass_filter.filter_in 0}"'...

Add 'run' and 'notifyMatlabServer' Commands

Since we are starting the HDL simulator in batch mode, we cannot issue the run command interactively. Instead, we provide the run command in the 'tclstart' property. When the cosimulation finishes in the HDL simulator, we send event ID 1 to hdldaemon through the notifyMatlabServer command. In MATLAB, the waitForHdlClient command waits to receive this event ID. (You can also use the notifyMatlabServer and waitForHdlClient commands when the cosimulation is started in CLI or GUI modes.)

tclcmd{end} = [tclcmd{end} ...
    ' -input "{@run 10000 ns}"' ...
    ' -input "{@notifyMatlabServer 1 -socket ' socket_port '}"'];

Prepare Test Bench Data

Declare the frequency of input signal as global so that it can be accessed both inside and outside the callback function. For the first cosimulation run, the input to filter is a 1 MHz sine wave.

global frequency;   % Frequency of input signal
global ticks;       % Number of callbacks executed

frequency = 1e6;    % 1 MHz
ticks     = 0;

Start HDL simulator in Batch Mode for First Cosimulation Run

Start the HDL simulator in batch mode by setting the 'runmode' property to the HDL Verifier nclaunch command. The HDL simulator process starts in the background. Next, use the waitForHdlClient command to wait for event ID 1 sent by the notifyMatlabServer command in the HDL simulator. Receiving this ID indicates that the cosimulation is over and we can go to the next step. If this event ID is not received within 120 seconds, an error is reported.

nclaunch('tclstart',tclcmd, 'rundir',projdir,...
     'runmode','Batch'); % Start HDL simulator in batch mode
waitForHdlClient(120,1); % Waiting for cosimulation to finish

Prepare the Next Cosimulation Run

In the second cosimulation run, the input signal is a 25 MHz sine wave.

frequency = 25e6; % 25 MHz
ticks = 0;

Start HDL Simulator in Batch Mode for Second Cosimulation Run

Start the HDL simulator in batch mode again and wait for the cosimulation run to complete using the waitForHdlClient command.

nclaunch('tclstart',tclcmd, 'rundir',projdir,...
     'runmode','Batch'); % Start HDL simulator in batch mode
waitForHdlClient(120,1); % Waiting for cosimulation to finish

When the simulation is completed, two MATLAB figures open. The first plot shows the 1 MHz input signal and its output. The second plot shows the 25 MHz input signal and the corresponding output.

This concludes this example.

Was this topic helpful?