Skip to Main Content Skip to Search
Product Documentation

Generating a Simulink Model for Cosimulation with an HDL Simulator

Overview

Simulink HDL Coder supports automatic generation of a cosimulation model as a part of the test bench generation process. Automated cosimulation model generation provides a Simulink model, configured for both Simulink simulation and cosimulation of your design with an HDL simulator. The generated model includes:

In addition to the generated model, the coder generates a TCL script that launches and configures your cosimulation tool. Comments in the script file document clock, reset, and other timing signal information defined by the coder for the cosimulation tool.

Generating a Cosimulation Model from the GUI

This example demonstrates the process for generating a cosimulation model. The example model, hdl_cosim_demo1, implements a simple multiply and accumulate (MAC) algorithm. Open the model by entering the name at the MATLAB command line:

hdl_cosim_demo1

The following figure shows the top-level model.

The DUT is the MAC subsystem.

Cosimulation model generation takes place during generation of the test bench. As a best practice, generate HDL code before generating a test bench, as follows:

  1. In the HDL Code Generation pane of the Configuration Parameters dialog box, select the DUT for code generation. In this case, it is hdl_cosim_demo1/MAC.

  2. Click Apply.

  3. Click Generate. The coder displays progress messages, as shown in the following listing:

    ### Applying HDL Code Generation Control Statements
    ### Starting HDL Check.
    ### HDL Check Complete with 0 error, 0 warning and 0 message.
    
    ### Begin VHDL Code Generation
    ### Working on hdl_cosim_demo1/MAC as hdlsrc\MAC.vhd
    ### HDL Code Generation Complete.

Next, configure the test bench options to include generation of a cosimulation model:

  1. Select the HDL Code Generation > Test Bench pane of the Configuration Parameters dialog box.

  2. Select the Cosimulation model for use with: option. Selecting this check box enables the pulldown menu to the right.

  3. Select the desired cosimulation tool from the drop-down menu.

  4. Configure any required test bench options. The coder documents all relevant option settings in a generated script file (see The Cosimulation Script File).

  5. Click Apply.

Next, generate test bench code and the cosimulation model:

  1. Click Generate Test Bench. The coder displays progress messages as shown in the following listing:

    ### Begin TestBench Generation
    ### Generating new cosimulation model: gm_hdl_cosim_demo1_mq0.mdl
    ### Generating new cosimulation tcl script: hdlsrc/gm_hdl_cosim_demo1_mq0_tcl.m
    ### Cosimulation Model Generation Complete.
    
    ### Generating Test bench: hdlsrc\MAC_tb.vhd
    ### Please wait ...
    ### HDL TestBench Generation Complete.

    When test bench generation completes, the coder opens the generated cosimulated model. The following figure shows the generated model.

  2. Save the generated model. The generated model exists only in memory unless you save it.

As indicated by the code generation messages, the coder generates the following files in addition to the usual HDL test bench file:

Generated file names derive from the model name, as described in Naming Conventions for Generated Cosimulation Models and Scripts.

The next section, Structure of the Generated Model, describes the features of the model. Before running a cosimulation, become familiar with these features.

Structure of the Generated Model

You can set up and launch a cosimulation using controls located in the generated model. This section examines the model generated from the example MAC subsystem.

Simulation Path

The model comprises two parallel signal paths. The simulation path, located in the upper half of the model window, is nearly identical to the original DUT. The purpose of the simulation path is to execute a normal Simulink simulation and provide a reference signal for comparison to the cosimulation results. The following figure shows the simulation path.

The two subsystems labelled ToCosimSrc and ToCosimSink do not change the performance of the simulation path in any way. Their purpose is to capture stimulus and response signals of the DUT and route them to and from the HDL cosimulation block (see Signal Routing Between Simulation and Cosimulation Paths).

Cosimulation Path

The cosimulation path, located in the lower half of the model window, contains the generated HDL Cosimulation block. The following figure shows the cosimulation path.

The FromCosimSrc subsystem receives the same input signals that drive the DUT. In the gm_hdl_cosim_demo1_mq0 model, the subsystem simply passes the inputs on to the HDL Cosimulation block. Signals of some other data types require further processing at this stage (see Signal Routing Between Simulation and Cosimulation Paths).

The Compare subsystem at the end of the cosimulation path compares the cosimulation output to the reference output produced by the simulation path. If the comparison detects any discrepancy, an Assertion block in the Compare subsystem displays a warning message. If desired, you can disable assertions and control other operations of the Compare subsystem. See Controlling Assertions and Scope Displays for details.

The coder populates the HDL Cosimulation block with the compiled I/O interface of the DUT. The following figure shows the Ports pane of the Mac_mq HDL Cosimulation block.

The coder sets the Full HDL Name, Sample Time, Data Type, and other fields as required by the model. The coder also configures other HDL Cosimulation block parameters under the Timescales and Tcl panes.

Start Simulator Control

When you double-click the Start Simulator control, it launches the selected cosimulation tool and passes in a startup command to the tool. The Start Simulator icon displays the startup command, as shown in the following figure.

The commands executed when you double-click the Start Simulator icon launch and set up the cosimulation tool, but they do not start the actual cosimulation. Launching a Cosimulation describes how to run a cosimulation with the generated model.

Signal Routing Between Simulation and Cosimulation Paths

The generated model routes signals between the simulation and cosimulation paths using Goto and From blocks. For example, the Goto blocks in the ToCosimSrc subsystem route each DUT input signal to a corresponding From block in the FromCosimSrc subsystem. The following figures show the Goto and From blocks in each subsystem.

The preceding figures show simple scalar inputs. Signals of complex and vector data types require further processing. See Complex and Vector Signals in the Generated Cosimulation Model for further information.

Controlling Assertions and Scope Displays

The Compare subsystem lets you control the display of signals on scopes, and warning messages from assertions. The following figure shows the Compare subsystem for the gm_hdl_cosim_demo1_mq0 model.

For each output of the DUT, the coder generates an assertion checking subsystem (Assert_OutN ). The subsystem computes the difference (err) between the original DUT output (dut ref) and the corresponding cosimulation output (cosim). The subsystem routes the comparison result to an Assertion block. If the comparison result is not zero, the Assertion block reports the discrepancy.

The following figure shows the Assert_Out1 subsystem for the gm_hdl_cosim_demo1_mq0 model.

This subsystem also routes the dut ref, cosim, and err signals to a Scope for display at the top level of the model.

By default, the generated cosimulation model enables all assertions and displays all Scopes. Use the buttons on the Compare subsystem to disable assertions or hide Scopes.

Launching a Cosimulation

To run a cosimulation with the generated model:

  1. Double-click the Compare subsystem to configure Scopes and assertion settings.

    If you want to disable Scope displays or assertion warnings before starting your cosimulation, use the buttons on the Compare subsystem (shown in the following figure).

  2. Double-click the Start Simulator control.

    The Start Simulator control launches your HDL simulator (in this case, EDA Simulator Link for use with Mentor Graphics ModelSim).

    The HDL simulator in turn executes a startup script. In this case the startup script consists of the TCL commands located in gm_hdl_cosim_demo1_mq0_tcl.m. When the HDL simulator finishes executing the startup script, it displays a message like the following.

    # Ready for cosimulation...
  3. In the Simulink Editor for the generated model, start simulation.

    As the cosimulation runs, the HDL simulator displays messages like the following.

    # Running Simulink Cosimulation block.
    # Chip Name: --> hdl_cosim_demo1/MAC
    # Target language: --> vhdl
    # Target directory: --> hdlsrc
    # Fri Jun 05 4:26:34 PM Eastern Daylight Time 2009
    # Simulation halt requested by foreign interface.
    # done

    At the end of the cosimulation, if you have enabled Scope displays, the compare scope displays the following signals:

    • cosim: The result signal output by the HDL Cosimulation block.

    • dut ref: The reference output signal from the DUT.

    • err: The difference (error) between these two outputs.

    The following figure shows these signals.

The Cosimulation Script File

The generated script file has two sections:

Header Comments Section

The following listing shows the comment section of a script file generated for the hdl_cosim_demo1 model:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Auto generated cosimulation 'tclstart' script 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Source Model         : hdl_cosim_demo1.mdl
%  Generated Model      : gm_hdl_cosim_demo1.mdl
%  Cosimulation Model   : gm_hdl_cosim_demo1_mq.mdl
%
%  Source DUT           : gm_hdl_cosim_demo1_mq/MAC
%  Cosimulation DUT     : gm_hdl_cosim_demo1_mq/MAC_mq
%
%  File Location        : hdlsrc/gm_hdl_cosim_demo1_mq_tcl.m
%  Created              : 2009-06-16 10:51:01
%
%  Generated by MATLAB 7.9 and Simulink HDL Coder 1.6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  ClockName           : clk
%  ResetName           : reset
%  ClockEnableName     : clk_enable
%
%  ClockLowTime        : 5ns
%  ClockHighTime       : 5ns
%  ClockPeriod         : 10ns
%
%  ResetLength         : 20ns
%  ClockEnableDelay    : 10ns
%  HoldTime            : 2ns
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  ModelBaseSampleTime   : 1
%  OverClockFactor     : 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Mapping of DutBaseSampleTime to ClockPeriod
%
%  N = (ClockPeriod / DutBaseSampleTime) * OverClockFactor
%  1 sec in Simulink corresponds to 10ns in the HDL 
%  Simulator(N = 10)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  ResetHighAt          : (ClockLowTime + ResetLength + HoldTime)
%  ResetRiseEdge        : 27ns
%  ResetType            : async
%  ResetAssertedLevel   : 1
%
%  ClockEnableHighAt    : (ClockLowTime + ResetLength + ClockEnableDelay + HoldTime)
%  ClockEnableRiseEdge  : 37ns
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The comments section comprises the following subsections:

TCL Commands Section

The following listing shows the TCL commands section of a script file generated for the hdl_cosim_demo1 model:

function tclCmds = gm_hdl_cosim_demo1_mq_tcl
tclCmds = {
    'do MAC_compile.do',...% Compile the generated code
    'vsimulink work.MAC',...% Initiate cosimulation
    'add wave  /MAC/clk',...% Add wave commands for chip input signals
    'add wave  /MAC/reset',...
    'add wave  /MAC/clk_enable',...
    'add wave  /MAC/In1',...
    'add wave  /MAC/In2',...
    'add wave  /MAC/ce_out',...% Add wave commands for chip output signals
    'add wave  /MAC/Out1',...
    'set UserTimeUnit ns',...% Set simulation time unit
    'force /MAC/clk 0 0ns, 1 5ns -r 10ns;',...% Clock force command
    'force /MAC/clk_enable 0 0ns, 1 37ns;',...% Clock enable force command
    'force /MAC/reset 1 0ns, 0 27ns;',...% Reset force command
    'puts "Note: Running pre-simulation for 40ns  to reset the chip"',...
    'run 40ns;',...% Run simulation to reset the chip
    'puts ""',...
    'puts "Ready for cosimulation..."',...
};
end

Complex and Vector Signals in the Generated Cosimulation Model

Input signals of complex or vector data types require insertion of additional elements into the cosimulation path. this section describes these elements.

Complex Signals

The generated cosimulation model automatically breaks complex inputs into real and imaginary parts. The following figure shows a FromCosimSrc subsystem that receives two complex input signals. The subsystem breaks the inputs into real and imaginary parts before passing them to the subsystem outputs.

The model maintains the separation of real and imaginary components throughout the cosimulation path. The Compare subsystem performs separate comparisons and separate scope displays for the real and imaginary signal components.

Vector Signals

The generated cosimulation model flattens vector inputs. The following figure shows a FromCosimSrc subsystem that receives two vector input signals of dimension 2. The subsystem flattens the inputs into scalars before passing them to the subsystem outputs.

Generating a Cosimulation Model from the Command Line

To generate a cosimulation model from the command line, pass the GenerateCosimModel property to the makehdltb function. GenerateCosimModel takes one of the following property values:

In the following command, makehdltb generates a cosimulation model configured for EDA Simulator Link for use with Mentor Graphics ModelSim.

makehdltb('hdl_cosim_demo1/MAC','GenerateCosimModel','ModelSim');

Naming Conventions for Generated Cosimulation Models and Scripts

The naming convention for generated cosimulation models is

prefix_modelname_toolid_ suffix.mdl, where:

The naming convention for generated cosimulation scripts is the same as that for models, except that the file name extension is .m.

Limitations for Cosimulation Model Generation

When you configure a model for cosimulation model generation, observe the following limitations:

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS