| EDA Simulator Link™ MQ | ![]() |
The EDA Simulator Link™ MQ HDL Cosimulation block serves as a bridge between the Simulink® and the HDL simulator domains. The block represents an HDL component model within the Simulink software. Using the block, Simulink software writes (drives) signals to and reads signals from the HDL model under simulation in the ModelSim® simulator. Signal exchange between the two domains occurs at regularly scheduled time steps defined by the Simulink sample time.
As you develop an EDA Simulator Link MQ cosimulation application, you should be familiar with how signal values are handled across the simulation domains with respect to the following cases:
How Simulink drives cosimulation signals
Representation of simulation time
Handling multirate signals
Handling Frame-based signals
Avoiding race conditions ()
Block simulation latency
Interfacing with continuous time signals
Setting Simulink configuration parameters
Setting the communication link
Starting the HDL simulator
Although you can bind the output ports of an HDL Cosimulation block to any signal in an HDL model hierarchy, you must use some caution when connecting signals to input ports. Ensure that the signal you are binding to does not have other drivers. If it does, use resolved logic types; otherwise you may get unpredictable results.
If you need to use a signal that has multiple drivers and it is resolved (for example, it is of VHDL type STD_LOGIC) , Simulink applies the resolution function at each time step defined by the signal's Simulink sample rate. Depending on the other drivers, the Simulink value may or may not get applied. Furthermore, Simulink has no control over signal changes that occur between its sample times.
Note Make sure that signals being used in cosimulation have read/write access (this is done through the HDL simulator—see product documentation for details). This rule applies to all signals on the Ports, Clocks, and Tcl panes. |
The representation of simulation time differs significantly between the HDL simulator and Simulink.
In ModelSim SE/PE, the unit of simulation time is referred to as a tick. The duration of a tick is defined by the HDL simulator resolution limit. The default resolution limit is 1 ns.
To determine current ModelSim resolution limit, enter echo $resolution or report simulator state at the ModelSim prompt. You can override the default resolution limit by specifying the -t option on the ModelSim command line, or by selecting a different Simulator Resolution in the ModelSim Simulate dialog box. Available resolutions in ModelSim are 1x, 10x, or 100x in units of fs, ps, ns, us, ms, or sec. See the ModelSim documentation for further information.
Simulink maintains simulation time as a double-precision value scaled to seconds. This representation accommodates modeling of both continuous and discrete systems.
The relationship between Simulink and the HDL simulator timing affects the following aspects of simulation:
Total simulation time
Input port sample times
Output port sample times
Clock periods
During a simulation run, Simulink communicates the current simulation time to the HDL simulator at each intermediate step. (An intermediate step corresponds to a Simulink sample time hit. Upon each intermediate step, new values are applied at input ports, or output ports are modified.) To bring the HDL simulator up-to-date with Simulink during cosimulation, sampled Simulink time must be converted to HDL simulator time (ticks) and the HDL simulator must run for the computed number of ticks.
Caution If you specify a Simulink sample time that cannot be expressed as a whole number of HDL ticks, you will get an error. |
The EDA Simulator Link MQ cosimulation interface provides controls that let you configure the timing relationship between the HDL simulator and Simulink and avoid timing errors caused by differences in timing representation.
The Timescales pane of the HDL Cosimulation block parameters dialog lets you choose an optimal timing relationship between Simulink and the HDL simulator. The figure below shows the default settings of the Timescales pane.

The Timescales pane defines a correspondence between one second of Simulink time and some quantity of HDL simulator time. This quantity of HDL simulator time can be expressed in one of the following ways:
In relative terms (i.e., as some number of HDL simulator ticks). In this case, the cosimulation is said to operate in relative timing mode. Relative timing mode is the default.
In absolute units (such as milliseconds or nanoseconds). In this case, the cosimulation is said to operate in absolute timing mode.
The following sections discuss these two timing modes.
Relative timing mode defines the following one-to-one correspondence between simulation time in Simulink and the HDL simulator:
One second in Simulink corresponds to N ticks in the HDL simulator, where N is a scale factor.
This correspondence holds regardless of the HDL simulator timing resolution.
The following pseudocode shows how Simulink time units are quantized to HDL simulator ticks:
InTicks = N * tInSecs
where InTicks is the HDL simulator time in ticks, tInSecs is the Simulink time in seconds, and N is a scale factor.
Operation of Relative Timing Mode. By default, the HDL Cosimulation block is configured for relative mode, with a scale factor of 1. Thus, 1 Simulink second corresponds to 1 tick in the HDL simulator. In the default case:
If the total simulation time in Simulink is specified as N seconds, then the ModelSim SE/PE HDL simulation will run for exactly N ticks (i.e., N ns at the default resolution limit).
Similarly, if Simulink computes the sample time of an HDL Cosimulation block input port as Tsi seconds, new values will be deposited on the HDL input port at exact multiples of Tsi ticks. If an output port has an explicitly specified sample time of Tso seconds, values will be read from the HDL simulator at multiples of Tso ticks.
To understand how relative timing mode operates, review cosimulation results from the following example model.

The model contains an HDL Cosimulation block (labeled VHDL Cosimulation INVERTER) simulating an 8-bit inverter that is enabled by an explicit clock. The inverter has a single input and a single output. The VHDL code for the inverter is listed below:
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY inverter IS PORT (
inport : IN std_logic_vector := "11111111";
outport: OUT std_logic_vector := "00000000";
clk:IN std_logic
);
END inverter;
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ARCHITECTURE behavioral OF inverter IS
BEGIN
PROCESS(clk)
BEGIN
IF (clk'EVENT AND clk = '1') THEN
outport <= NOT inport;
END IF;
END PROCESS;
END behavioral;
A cosimulation of this model might have the following settings:
Simulation parameters in Simulink:
Timescales parameters: default (relative timing with a scale factor of 1)
Total simulation time: 60 s
Input port (/inverter/inport) sample time: 24 s
Output port (/inverter/outport ) sample time: 12 s
Clock (inverter/clk) period: 10 s
ModelSim resolution limit: 1 ns
The figure below shows the ModelSim wave window after a cosimulation run of the example Simulink model for 60 ns. The wave window shows that ModelSim simulated for 60 ticks (60 ns). The inputs change at multiples of 24 ns and the outputs are read from ModelSim at multiples of 12 ns. The clock is driven low and high at intervals of 5 ns.

Now consider a cosimulation of the same model, this time configured with a scale factor of 100 in the Timescales pane.
The ModelSim wave window below shows that Simulink port and clock times were scaled by a factor of 100 during simulation. ModelSim simulated for 6 microseconds (60 * 100 ns). The inputs change at multiples of 24 * 100 ns and outputs are read from ModelSim at multiples of 12 * 100 ns. The clock is driven low and high at intervals of 500 ns.

Absolute timing mode lets you define the timing relationship between Simulink and the HDL simulator in terms of absolute time units and a scale factor:
One second in Simulink corresponds to (N * Tu) seconds in the HDL simulator, where Tu is an absolute time unit (e.g., ms, ns, etc.) and N is a scale factor.
To configure the Timescales parameters for absolute timing mode, you select a unit of absolute time, rather than Tick.
In absolute timing mode, all sample times and clock periods in Simulink are quantized to HDL simulator ticks. The following pseudocode illustrates the conversion:
tInTicks = tInSecs * (tScale / tRL)
where:
tInTicks is the HDL simulator time in ticks.
tInSecs is the Simulink time in seconds.
tScale is the timescale setting (unit and scale factor) chosen in the Timescales pane of the HDL Cosimulation block.
tRL is the HDL simulator resolution limit.
For example, given a Timescales pane setting of 1 s and an HDL simulator resolution limit of 1 ns, an output port sample time of 12 ns would be converted to ticks as follows:
tInTicks = 12ns * (1s / 1ns) = 12
Operation of Absolute Timing Mode. To understand the operation of absolute timing mode, we will again consider the example model discussed in Operation of Relative Timing Mode. Suppose that the model is reconfigured as follows:
Simulation parameters in Simulink:
Timescale parameters: 1 s of Simulink time corresponds to 1 s of HDL simulator time.
Total simulation time: 60e-9 s (60ns)
Input port (/inverter/inport) sample time: 24e-9 s (24 ns)
Output port (/inverter/outport ) sample time: 12e-9 s (12 ns)
Clock (inverter/clk) period: 10e-9 s (10 ns)
HDL simulator resolution limit: 1 ns
Given these simulation parameters, Simulink will cosimulate with the HDL simulator for 60 ns. Inputs are sampled at a intervals of 24 ns and outputs are updated at intervals of 12 ns. Clocks are driven at intervals of 10 ns.
The figure below shows the ModelSim wave window after a cosimulation run.

The following restrictions apply to the use of absolute and relative timing modes:
When multiple HDL Cosimulation blocks in a model are communicating with a single instance of the HDL simulator, all HDL Cosimulation blocks must have the same Timescales pane settings.
If you change the Timescales pane settings in an HDL Cosimulation block between consecutive cosimulation runs, you must restart the simulation in the HDL simulator.
In general, Simulink handles the sample time for the ports of an HDL Cosimulation block as follows:
If an input port is connected to a signal that has an explicit sample time, based on forward propagation, Simulink applies that rate to that input port.
If an input port is connected to a signal that does not have an explicit sample time, Simulink assigns a sample time that is equal to the least common multiple (LCM) of all identified input port sample times for the model.
After Simulink sets the input port sample periods, it applies user-specified output sample times to all output ports. Sample times must be explicitly defined for all output ports.
If you are developing a model for cosimulation in relative timing mode, consider the following sample time guideline:
Specify the output sample time for an HDL Cosimulation block as an integer multiple of the resolution limit defined in the HDL simulator. Use the HDL simulator command report simulator state to check the resolution limit of the loaded model. If the HDL simulator resolution limit is 1 ns and you specify a block's output sample time as 20, Simulink interacts with the HDL simulator every 20 ns.
EDA Simulator Link MQ software supports the use of multirate signals, signals that are sampled or updated at different rates, in a single HDL Cosimulation block. An HDL Cosimulation block exchanges data for each signal at the Simulink sample rate for that signal. For input signals, an HDL Cosimulation block accepts and honors all signal rates.
The HDL Cosimulation block also lets you specify an independent sample time for each output port. You must explicitly set the sample time for each output port, or accept the default. This lets you control the rate at which Simulink updates an output port by reading the corresponding signal from the HDL simulator.
This section discusses how to improve the performance of your cosimulation by using frame-based signals. An example is provided.
The HDL Cosimulation block supports processing of single-channel frame-based signals.
A frame of data is a collection of sequential samples from a single channel or multiple channels. One frame of a single-channel signal is represented by a M-by-1 column vector. A signal is frame- based if it is propagated through a model one frame at a time.
Frame-based processing requires the Signal Processing Blockset software. Source blocks from the Signal Processing Sources library let you specify a frame-based signal by setting the Samples per frame block parameter. Most other signal processing blocks preserve the frame status of an input signal. You can use the Buffer block to buffer a sequence of samples into frames.
Frame-based processing can improve the computational time of your Simulink models, because multiple samples can be processed at once. Use of frame-based signals also lets you simulate the behavior of frame-based systems more accurately.
See "Working with Signals" in the Signal Processing Blockset documentation for detailed information about frame-based processing.
You do not need to configure the HDL Cosimulation block in any special way for frame-based processing. To use frame-based processing in a cosimulation, connect one or more single-channel frame-based signals to the input port(s) of the HDL Cosimulation block. All such signals must meet the requirements described in Frame-Based Processing Requirements and Restrictions. The HDL Cosimulation block automatically configures its output(s) for frame-based operation at the appropriate frame size.
Note that use of frame-based signals affects only the Simulink side of the cosimulation. The behavior of the HDL code under simulation in the HDL simulator does not change in any way. Simulink assumes that HDL simulator processing is sample-based. Samples acquired from the HDL simulator are assembled into frames as required by Simulink. Conversely, output data framed by Simulink is transmitted to the HDL simulator in frames, which are unpacked and processed by the HDL simulator one sample at a time.
Frame-Based Processing Requirements and Restrictions. Observe the following restrictions and requirements when connecting frame-based signals in to an HDL Cosimulation block:
Connection of mixed frame-based and sample-based signals to the same HDL Cosimulation block is not supported.
Only single-channel frame-based signals can be connected to the HDL Cosimulation block. Use of multichannel (matrix) frame-based signals is not supported in this release.
All frame-based signals connected to the HDL Cosimulation block must have the same frame size.
Frame-based processing in the Simulink model is transparent to the operation of the HDL model under simulation in the HDL simulator. The HDL model is presumed to be sample-based. The following constraint also applies to the HDL model under simulation in the HDL simulator:
VHDL signals should be specified as scalars, not vectors or arrays (with the exception of bit vectors, as VHDL and Verilog bit vectors are converted to the appropriately sized fixed-point scalar data type by the HDL Cosimulation block).
This example shows the use of the HDL Cosimulation block to cosimulate a VHDL implementation of a simple lowpass filter. In the example, you will compare the performance of the simulation using frame-based and sample-based signals.
The example files are:
matlabroot\toolbox\modelsim\modelsimdemos\frame_filter_cosim.mdl: the example model.
matlabroot\toolbox\modelsim\modelsimdemos\VHDL\frame_demos\
lp_fir_8k.vhd:
VHDL code for the filter
to be cosimulated. The filter was designed with FDATool and the code
was generated by the Filter Design HDL Coder.
The example uses the data file matlabroot\toolbox\signal\signal\mtlb.mat as an input signal. This file contains a speech signal. The sample data is of data type double, sampled at a rate of 8 kHz.
The figure below shows the frame_filter_cosim.mdl model.

The Audio Source Signal From Workspace block provides an input signal from the workspace variable mtlb. The block is configured for an 8 kHz sample rate, with a frame size of 80, as shown in this figure.

The sample rate and frame size of the input signal propagate throughout the model.
The VHDL code file lp_fir_8k.vhd implements a simple lowpass FIR filter with a cutoff frequency of 1500 Hz. The HDL Cosimulation block simulates this HDL module. The HDL Cosimulation block ports and clock signal are configured to match the corresponding signals on the VHDL entity.
Note that for the ModelSim simulation to execute correctly, the clk_enable signal of the lp_fir_8k entity must be forced high. The signal is forced by a pre-simulation command transmitted by the HDL Cosimulation block. The command has been entered into the Tcl pane of the HDL Cosimulation block, as shown in the figure below.

Output data from the HDL Cosimulation block is returned the workspace variable audiobuff1 via the Filtered Signal To Workspace block.
To run the cosimulation, perform the following steps:
Start MATLAB and make it your active window.
Set up and change to a writable working directory that is outside the context of your MATLAB installation directory.
Add the demo directory (matlabroot\toolbox\modelsim\modelsimdemos\frame_cosim) to the MATLAB path.
Copy the demo VHDL file lp_fir_8k.vhd to your working directory.
Open the example model.
open frame_filter_cosim.mdl
Load the source speech signal, which will be filtered, into the MATLAB workspace.
load mtlb
If you have a compatible sound card, you can play back the source signal by typing the following commands at the MATLAB command prompt:
a = audioplayer(mtlb,8000); play(a);
Start ModelSim by typing the following command at the MATLAB command prompt:
vsim
The ModelSim window should now be active. If not, start it.
At the ModelSim prompt, create a design library, and compile the VHDL filter code from the source file lp_fir_8k.vhd, by typing the following commands:
vlib work vmap work work vcom lp_fir_8k.vhd
The lowpass filter to be simulated is defined as the entity lp_fir_8k. At the ModelSim prompt, load the instantiated entity lp_fir_8k for cosimulation:
vsimulink lp_fir_8k
ModelSim is now set up for cosimulation.
Start MATLAB. Run a simulation and measure elapsed time as follows:
t = clock; sim(gcs); etime(clock,t)
ans =
2.7190The timing above is typical for a run of this model given a simulation Stop time of 1 second and a frame size of 80 samples. Timings are system-dependent and will vary slightly from one simulation run to the next.
Take note of the timing you obtained. For the next simulation run, you will change the model to sample-based operation and obtain a comparative timing.
The filtered audio signal returned from ModelSim is stored in the workspace variable audiobuff1. If you have a compatible sound card, you can play back the filtered signal to hear the effect of the lowpass filter. Play the signal by typing the following commands at the MATLAB command prompt:
b = audioplayer(audiobuff1,8000); play(b);
Open the block parameters dialog of the Audio Source Signal From Workspace block and set the Samples per frame property to 1, as shown in this figure.

Close the dialog and select the Simulink window. Select Update diagram from the Edit menu.
The block diagram now indicates that the source signal (and all signals inheriting from it) is a scalar, as shown in the following figure.

Start ModelSim. At the ModelSim prompt, type
restart
Start MATLAB. Run a simulation and measure elapsed time as follows:
t = clock; sim(gcs); etime(clock,t)
ans =
3.8440Observe that the elapsed time has increased significantly with a sample-based input signal. The timing above is typical for a sample-based run of this model given a simulation Stop time of 1 second. Timings are system-dependent and will vary slightly from one simulation run to the next.
Close down the simulation in an orderly way. In ModelSim, stop the simulation by selecting Simulate > End Simulation, and quit ModelSim. Then close the Simulink model window.
In ModelSim SE/PE, it is not possible to guarantee the order in which clock signals (rising-edge or falling-edge) defined in the HDL Cosimulation block are applied, relative to the data inputs driven by these clocks. Therefore, if care is not taken to ensure the relationship between the data and active edges of the clock, race conditions could create non-deterministic cosimulation results.
For more on race conditions in hardware simulators, see Race Conditions in HDL Simulators.
Simulink and the EDA Simulator Link MQ Cosimulation blocks supplement the hardware simulator environment, rather than operate as part of it. During cosimulation, Simulink does not participate in the HDL simulator delta-time iteration. From the Simulink perspective, all signal drives (reads) occur during a single delta-time cycle. For this reason, and due to fundamental differences between the HDL simulator and Simulink with regard to use and treatment of simulation time, some degree of latency is introduced when you use EDA Simulator Link MQ Cosimulation blocks. The latency is a time lag that occurs between when Simulink begins the deposit of a signal and when the effect of the deposit is visible on cosimulation block output.
As the following figure shows, Simulink cosimulation block input affects signal values just after the current HDL simulator time step (t+δ) and block output reflects signal values just before the current HDL simulator step time (t-δ) .

Regardless of whether your HDL code is specified with latency, the cosimulation block has a minimum latency that is equivalent to the cosimulation block's output sample time. For large sample times, the delay can appear to be quite long, but this is an artifact of the cosimulation block, which exchanges data with the HDL simulator at the block's output sample time only. This may be reasonable for a cosimulation block that models a device that operates on a clock edge only, such as a register-based device.
For cosimulation blocks that model combinatorial circuits, you may want to experiment with a faster sample frequency for output ports. For cosimulation blocks that model combinatorial circuits, you may want to experiment with a faster sampling frequency for output ports in order to reduce this latency.runn
To visualize cosimulation block latency, consider the following VHDL code and Simulink model. The VHDL code represents an XOR gate:
-- edgedet.vhd LIBRARY IEEE; USE IEEE.std_logic_1164.all; ENTITY edgedet IS END edgedet; ARCHITECTURE behavioral OF edgedet IS SIGNAL a : std_logic; SIGNAL b : std_logic; SIGNAL y : std_logic; BEGIN y <= a XOR b; END behavioral;

In the Simulink model, the cosimulation block VHDL Edge Detector contains an XOR circuit. The second cosimulation block, VHDL Fast Output, simply reads the same XOR output. The first block is driven by a signal generated by the Pulse Generator block. The Data Type Conversion block converts the signal to a boolean value. The signal is then treated three different ways:
A Unit Delay block applies a sample and hold to the signal and drives block input port a. The delay is equal to one period of the signal's Simulink sample time. When the delay is applied to the XOR, the pulse equals the period specified by the delay block after any edges.
The signal without a delay drives block input port b.
The third signal bypasses the cosimulation block and goes directly to the Scope block for display.
The second cosimulation block, VHDL Fast Output, is a source block that reads the output of the XOR circuit and passes it on to the Scope block for display.
Now, assume that ModelSim is set up with a resolution limit of 100 ns and an iteration limit of 5000, and that the sample times for the blocks in the Simulink model are as follows:
| Block | Sample Time | Value |
|---|---|---|
| Pulse Generator | Sample time | 100 |
| Data Type Conversion block | Sample time | Inherited from Pulse Generator block |
| Unit Delay block | Sample time | Inherited from Data Type Conversion block |
| HDL Cosimulation block—Edge Detector | Input sample time | Inherited from Unit Delay block |
| Output sample time | 100 | |
| HDL Cosimulation block—Fast Output (source) | Output sample time | 100 |
After the simulation runs, the ModelSim wave window appears as follows.

Note the following:
Signal a gets asserted high after a 100 ns delay. This is due to the unit delay applied by the Simulink model.
Signal b gets asserted high immediately.
Signal y experiences a falling edge as a result of the XOR computation.
The following figure highlights the individual signal paths that get appear in the Simulink Scope window.

The signal that bypasses the cosimulation blocks rises at t=1000. That signal stays high for the duration of the sample period. However, the signals that are read from output port y of the two cosimulation blocks, display in the Scope window as follows:
After a one time step delay, the signals rise in response to step generator. The delay occurs because the values that the step generator deposit on the cosimulation block's signal paths do not propagate to the block's output until the next Simulink cycle.
After the next time step, the signal value falls due to the VHDL XOR operation.
For cosimulation blocks that model combinatorial circuits, such as the one in the preceding example, you may want to experiment with a faster sample frequency for output ports. For example, suppose you change the Output sample time for the VHDL Fast Output cosimulation block from 100 to 20. The following figure highlights the individual signal paths that appear in the Scope window for this scenario.

In this case, the signal that bypasses the cosimulation blocks and the output signal read from the VHDL Edge Detect block remain the same. However, the delay for the signal read from the VHDL Fast Output block is 20 ticks instead of 100. Although the size of the time step is still tied to the ModelSim resolution limit, the delay that occurs before the VHDL code is processed is significantly reduced and the time of execution more closely reflects simulation time in ModelSim.
Note Although this type of parameter tuning can increase simulation performance, it can make a model more difficult to debug. For example, it might be necessary to adjust the output sample time for each cosimulation block. |
Use the Simulink Zero-Order Hold block to apply a zero-order hold (ZOH) on continuous signals that are driven into an HDL Cosimulation block.
When you create a Simulink model that includes one or more EDA Simulator Link MQ Cosimulation blocks, you might want to adjust certain Simulink parameter settings to best meet the needs of HDL modeling. For example, you might want to adjust the value of the Stop time parameter in the Solver pane of the Configuration Parameters dialog box.
You can adjust the parameters individually or you can use the M-file dspstartup, which lets you automate the configuration process so that every new model that you create is preconfigured with the following relevant parameter settings:
| Parameter | Default Setting |
|---|---|
| 'SingleTaskRateTransMsg' | 'error' |
| 'Solver' | 'fixedstepdiscrete' |
| 'SolverMode' | 'singletasking' |
| 'StartTime' | '0.0' |
| 'StopTime' | 'inf' |
| 'FixedStep' | 'auto' |
| 'SaveTime' | 'off' |
| 'SaveOutput' | 'off' |
| 'AlgebraicLoopMsg' | 'error' |
The default settings for 'SaveTime' and 'SaveOutput' improve simulation performance.
You can use dspstartup by entering it at the MATLAB command line or by adding it to the Simulink startup.m file. You also have the option of customizing dspstartup settings. For example, you might want to adjust the 'StopTime' to a value that is optimal for your simulations, or set 'SaveTime' to 'on' to record simulation sample times.
For more information on using and customizing dspstartup, see the Signal Processing Blockset documentation. For more information about automating tasks at startup, see the description of the startup command in the MATLAB documentation.
If you take the approach of designing a Simulink model first, run and test your model thoroughly before replacing or adding hardware model components as EDA Simulator Link MQ Cosimulation blocks.
Select shared memory or socket communication. See Communicating with MATLAB or Simulink and the HDL Simulator.
See Starting the HDL Simulator.
![]() | Introduction to Cosimulation | Incorporating Hardware Designs into a Simulink® Model | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |