Accelerating the pace of engineering and science

# Documentation Center

• Trials
• Product Updates

## Timescales: Absolute, Relative and Automatic

This example illustrates the various Timescale settings within the HDL Cosimulation block and explains how these affect the timing relationship of Simulink and the HDL simulator. We use a simple Verilog parity check model to illustrate the timing relationship of Simulink and the HDL simulator (Incisive) used for cosimulation.

Model Functionality

The parity_check.mdlparity_check.mdl illustrates cosimulation of an HDL implementation of a simple even parity generator. This model showcases the various options available in the Timescales pane of the HDL Cosimulation block. You can switch between these modes using the clickable annotations in the model.

Verilog Code Used for Cosimulation

Parity checking is a method of adding a parity bit to a data stream in order to check the data for any errors. In this example we will use "even parity bit" scheme in which the parity bit is set to 1 if the number of ones in a given set of bits is odd (making the total number of ones, including the parity bit, even).

We select the input to the parity checker to be 8-bits and output as 1 bit. The Verilog code used for this example is paritychecker.vparitychecker.v.

Preparing the Model for Cosimulation

In order to guide you through the various steps of this example, we have provided numbered steps which you can follow. Each numbered step is followed by a brief explanation of the same.

1. Launch Incisive

Before running the model, you must first launch the HDL simulator. Use the startup command provided within the model for this. This also sets the HDL simulator resolution to 1ns.

2. Observe the settings within the HDL Cosimulation block's Ports pane

Double-click on the HDL Cosimulation block to edit the cosimulation parameters. Select the Ports pane.

• The sample time of the output signal is set to Ts - which is the sampling rate of the model. There are numerous ways to specify the value of Ts. We have set Ts for this model within the PreLoadFcn callbackPreLoadFcn callback of the model. You can set the value of Ts at the MATLAB Command prompt as well.

• Note that the option for allowing direct feedthrough has been checked - this is because our Verilog code is purely combinational.

This model is set up to work in three timing modes: Absolute, Relative, and Auto Timescale. You can read more about these modes in the HDL Verifier™ User Guide on the page Understanding the Representation of Simulation TimeUnderstanding the Representation of Simulation Time.

Absolute Timescale

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.

3. Click on the Absolute Mode annotation in the model to set the following:

• Sample time Ts = 10

• Set Timescales pane of HDL Cosimulation block to "1s in Simulink corresponds to 1ns in the HDL simulator":

```        set_param('parity_check/HDL Cosimulation','TimingMode','ns');
set_param('parity_check/HDL Cosimulation','TimingScaleFactor','1');```

The absolute time units in this case are 'ns' and the scale factor is '1'.

4. Run the model

The HDL simulator waveform shows the simulation run for 100ns, whereas the Simulink scope depicts how the model has run for 100s. This is expected for a Timescale setting of 1s in Simulink = 1ns in the HDL simulator.

Relative Timescale

Relative timing mode lets you define the timing relationship between Simulink and the HDL simulator in relative terms - that is, as some number of HDL simulator ticks.

5. Close all the HDL simulator windows and repeat step 1

6. Click on the Relative Mode annotation in the model to set the following:

• Sample time Ts = 10

• Set Timescales pane of HDL Cosimulation block to "1s in Simulink corresponds to 1 Tick in the HDL simulator":

```        set_param('parity_check/HDL Cosimulation','TimingMode','Tick');
set_param('parity_check/HDL Cosimulation','TimingScaleFactor','1');```

The time units in this case are HDL simulator ticks and the scale factor is '1'.

7. Run the model

The HDL simulator waveform shows the simulation run for 100ns, whereas the Simulink scope depicts how the model has run for 100s. This is expected for a Timescale setting of 1s in Simulink = 1 Tick in the HDL simulator (and the resolution of the simulator is set to 1ns). The waveforms obtained are similar to those we got in the absolute timescaling mode.

8. Change HDL Simulator resolution

If you were to set the resolution of the HDL simulator to 1ps, and run the above model, you will find that the HDL simulator would run for 100ps (since the Timescale is set to 1s in Simulink = 1 Tick in the HDL simulator). Note that in order to try this, you will need to close all HDL simulator windows, and change the following line of code within paritycmds_in.mparitycmds_in.m:

`   'exec ncelab -TIMESCALE 1ns/1ns -access +wc paritychecker',... % Elaborate design`

to:

`   'exec ncelab -TIMESCALE 1ps/1ps -access +wc paritychecker',... % Elaborate design`

In order to continue with this example, close the HDL simulator, and start a new instance of the simulator (with resolution set back to 1ns) using the Startup command provided in the model:

nclaunch('tclstart',paritycmds_in)

Auto Timescale

Within the Timescales pane of the HDL Cosimulation block the Determine Timescale Now pushbutton calculates a timing relationship between Simulink and the HDL simulator. Please note that the link needs to find the resolution of the HDL simulator, and hence you will need to have the HDL simulator up and running.

When you allow the link software to define the timing relationship, it attempts to set the timescale factor between the HDL simulator and Simulink to be as close as possible to 1 second in the HDL simulator = 1 second in Simulink (absolute timescale mode). If this setting is not possible, the HDL Verifier attempts to set the signal rate on the Simulink model port to the lowest possible number of HDL simulator ticks (relative timescale mode).

Now we will try using the Auto Timescale within the model as illustrated in the next step:

9. Click on Determine Timescale Now button in the Timescales pane

This will attempt to set the Timescale to "1s in Simulink = 1e-10s in the HDL simulator". An info pane will be displayed:

Notice how the HDL Verifier software attempted to first achieve a 1:1 timescale setting, but failed to do so because the resolution of the HDL simulator is set to 1ns, and representing Ts=10 seconds would amount to 10e9 number of ticks which is greater than the 2^31 - 1 (=2.1475e9) allowable limit. The number 2^31 - 1 is the maximum value for an int32.

Once the HDL Verifier cannot establish an absolute 1:1 timescale it switches to relative timescale mode in which it will equate the fundamental sample time (Ts=10s) with 1 HDL tick. Hence 10s in Simulink will correspond to 1 HDL tick, that is, 1s in Simulink corresponds to 1e-10s in the HDL simulator.

Apply the changes suggested by the HDL Cosimulation block.

11. Run the model

The waveforms within the HDL simulator and from the Simulink scope are shown below. Note how the HDL simulator progresses through 10ns whereas the Simulink model runs for 100s:

It is important to understand why the HDL Verifier did not choose a 1:1 mapping. If you were to set the Timescales setting to "1s in Simulink corresponds to 1s in the HDL simulator", apply this change, and run the model, Simulink will give you the following error:

12. Set an irrational sampling time (Ts = pi*1e-9)

If you were to use irrational sample times within Simulink, for example, Ts = pi*1e-9, the Auto Timescale will not be able to maintain an exact relationship between Simulink simulation and HDL simulation time. In this case, although the results of cosimulation are correct, the time axes of the Simulink scope and HDL simulator will not have a 1:1 relation.

13. Set Timescale to '1s in Simulink = 1s in HDL simulator'

In order to understand why 1:1 relationship is not possible, set the Timescale to '1s in Simulink = 1s in HDL simulator', and run the model (after restarting the simulation in the HDL simulator). A 1:1 timing relationship is not possible in this case as pi is an irrational number and is not an integral multiple of the 1ns (the HDL simulator resolution). Simulink will give you an error message in this case, as shown in the following image:

Note: Each time you change the Timescale setting within the HDL Cosimulation block, you will need to restart the simulation run within the HDL simulator.

Was this topic helpful?