Modelsim Short Tutorial
Modelsim Short Tutorial
Winter 2002-2003
Tutorial
ModelSim SE
A. Creating a Project
The goals for this lesson are:
- Create a project
A project is a collection entity for an HDL design under specification or test. Projects ease interaction
with the tool and are useful for organizing files and simulation settings. At a minimum, projects have a
work library and a session state that is stored in a .mpf file. A project may also consist of:
o HDL source files or references to source files
o other files such as READMEs or other project documentation
o local libraries
o references to global libraries
For more information about using project files, see the ModelSim User’s Manual.
1. Start ModelSim:
Upon opening ModelSim for the first time, you will see the Welcome to ModelSim dialog. (If this
screen is not available, you can display it by selecting Help > Welcome
Menu from the Main window.)
from the Welcome dialog, or File > New > Project (Main window). In the Create Project dialog box,
enter "test" as the Project Name and select a directory where the project file will be stored. Leave the
Default Library Name set to "work."
Upon selecting OK, you will see a blank Project tab in the workspace area of the Main window and
the Add Items to the Project dialog.
Page 1 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
3. The next step is to add the files that contain your design units.
Click Add Existing File in the Add Items to Project dialog. For this exercise, we’ll add two Verilog
files. Click the Browse button in the Add file to Project dialog box and open the examples directory in
your ModelSim installation. Select tcounter.v and counter. v. Select Reference from current location
and then click OK.
Page 2 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
Click on the Library tab and expand the work library by clicking the "+" icon. You’ll see the compiled
design units listed.
The last step in this exercise is to load one of the design units. Double-click counter on the Library
page. You’ll see a new page appear in the Workspace that displays the structure of the counter design
unit.
Page 3 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
At this point, you would generally run the simulation and analyze or debug your design. We’ll do just
that in the upcoming lessons. For now, let’s wrap up by ending the simulation and closing the project.
Select Simulate > End Simulation and confirm that you want to quit simulating. Next, select File >
Close > Project, confirm that you want to close the project, and select Yes to update your project file
with the changes you made during this session.
Note that a test.mpf file has been created in your working directory. This file contains information
about the project test that you just created. ModelSim will open this project automatically the next
time you invoke the tool.
Page 4 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
c) Invoke ModelSim:
from a Windows shortcut icon, from the Start menu
d) Create library
Before you compile any HDL code, you’ll need a design library to hold the compilation results. To
create a new design library, make this menu selection in the Main window: File > New > Library.
Make sure Create: a new library and a logical mapping to it is selected.
Type "work" in the Library Name field and then select OK. This creates a subdirectory named work -
your design library - within the current directory. ModelSim saves a special file named _info in the
subdirectory.
(PROMPT: vlib work vmap work work)
Page 5 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
In the next step you’ll compile the Verilog design. The example design consists of two Verilog source
files, each containing a unique module. The file counter.v contains a module called counter, which
implements a simple 8-bit binary up-counter. The other file, tcounter. v, is a testbench module
(test_counter) used to verify counter.
Under simulation you will see that these two files are configured hierarchically with a single instance
(instance name du t) of module counter instantiated by the testbench.
You'll get a chance to look at the structure of this code later. For now, you need to compile both files
into the work design library.
Note: Do not create a Library directory using Windows commands, because
the _info file will not be created. Always use the File menu or the vlib command from
either the ModelSim or UNIX/DOS prompt.)
e) Compile
Compile the counter.v, and tcounter.v files into the work library by selecting Compile > Compile
from the menu.
(PROMPT: vlog counter.v tcounter.v)
This opens the Compile HDL Source Files dialog box.
Select counter.v and tcounter.v (use Ctrl + click) and then choose Compile and then Don e.
Page 6 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
Note: The order in which you compile the two Verilog modules is not important (other than the
source-code depende ncies created by compiler directives). So it doesn’t matter here if you choose to
compile counter.v before or after tcounter. v.
The Simulate dialog appears. Click the "+" sign next to ’work’ to see the counter and test_counter
design units. (You won’t see this dialog box if you invoke vsim with test_counter from the command
line.)
The Simulate dialog allows you to select a design unit to load from the specified library. You can also
select the resolution limit for the simulation. The default resolution is 1 ns.
Select test_counter and click Load to accept these settings.
by entering the following command at the VSIM prompt within the Main window : view signals
source wave
(Main MENU: View > <window name>)
c) Add signals
Now let’s add signals to the Wave window with ModelSim’s drag and drop feature. In the Signals
window, select Edit > Select All to select the three signals. Drag the signals to either the pathname or
the values pane of the Wave window.
Page 7 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
HDL items can also be copied from one window to another (or within the Wave and List windows)
with the Edit > Copy and Edit > Paste menu selections.
d) Structure pane
You may have noticed when you loaded the design in Step 1 that a new tab appeared in the workspace
area of the Main window.
The Structure tab shows the hierarchical structure of the design. By default, only the top level of the
hierarchy is expanded. You can navigate within the hierarchy by clicking on any line with a "+"
Page 8 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
(expand) or "-" (contract) symbol. The same navigation technique works anywhere you find these
symbols within ModelSim.
By clicking the "+" next to dut: counter you can see all three hierarchical levels: test_counter,
counter and a function called increment. (If test_counter is not displayed you simulated counter
instead of test_counter.)
Click on Function increment and notice how other ModelSim windows are automatically updated as
appropriate. Specifically, the Source window displays the Verilog code at the hierarchical level you
selected in the Structure window, and the Signals window displays the appropriate signals. Using the
Structure tab in this way is analogous to scoping commands in interpreted Verilogs.
For now, make sure the test_counter module is showing in the Source window by clicking on the top
line in the Structure pane.
Now you will exercise different Run functions from the toolbar.
a) Run
Select the Run button on the Main window toolbar. This causes the simulation to run and then stop
after 100 ns (the default simulation length).
(PROMPT: run) (MENU: Simulate > Run > Run 100 ns)
Now the simulation has run for a total of 600ns (the default 100ns plus the 500 you just asked for).
The status bar at the bottom of the Main window displays this information.
The last command you executed (run 500) caused the simulation to advance for 500ns. You can also
advance simulation to a specific time. Type: run @ 3000
This advances the simulation to time 3000ns. Note that the simulation actually ran for an additional
2400ns (3000 - 600).
Page 9 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
4. Debugging
Next we’ll take a brief look at an interactive debugging feature of the ModelSim environment.
a) Set a breakpoint
Let’s set a breakpoint at line 30 in the counter.v file (which contains a call to the Verilog function
increment). To do this, select dut: counter in the Structure pane of the Workspace. Move the cursor to
the Source window and scroll the window to display line 30. Click on or near line number 30 to set a
breakpoint. You should see a red dot next to the line number where the breakpoint is set.
The breakpoint can be toggled between enabled and disabled by clicking it. When a breakpoint is
disabled, the dot appears open. To delete the breakpoint, click the line number with your right mouse
button and select Remove Breakpoint.
b) Restart
Select the Restart button to reload the design elements and reset the simulation time to zero.
Page 10 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
Note: Breakpoints can be set only on executable lines, denoted by blue line numbers.
Make sure all items in the Restart dialog box are selected, then click Restart.
(PROMPT: run -all) (Main MENU: Simulate > Run > Run -All)
When the simulation hits the breakpoint, it stops running, highlights the line with an arrow in the
Source window, and issues a Break message in the Main window.
Page 11 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
- select the count variable in the Source window, right-click it, and select Examine from the
context menu
- use the examine command to output the value to the Main window transcript: examine count
d) Step
Let’s move through the Verilog source functions with ModelSim’s Step command.
Click Step on the toolbar.
e) Hands-on
Experiment by yourself for awhile. Set and clear breakpoints and use the Step and Step Over
commands until you feel comfortable with their operation. When you’re done, quit the simulator by
entering the command: quit –force
System Results
Stimulus under Analysis
Verification
Page 12 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
We will now analyse the structure of the program to understand the different commands.
------------------------------------------------------------------------------
module test_counter;
In th is code fragment, the stimulus and response capture are going to be coded using
a couple of initial blocks. An initial block can contain sequential statements that can
be used to describe the behaviour of signals in a test bench. In the Stimulus initial
block, we need to generate waveforms on the clock and reset inputs. Thus:
Everytime there is a pound sign followed by a number n, it means that the simulator
advances by n times of simulation and then does whatever assignment is specified.
The $stop command is a Verilog built-in command that stops the simulation. In this
case, the simulation will stop after 50009 simulation times.
Concerning the clock, its behavior is easy to understand. At the beginning, its value is
set to zero ; then, every 10 seconds and for ever, its value is inverted.
------------------------------------------------------------------------------
counter#(5,10) dut (count,clk,rst);
Page 13 sur 14
EE 108 – Digital systems I Modelsim Tutorial
Winter 2002-2003
The Response initial block can be described very easily in Verilog as we can benefit
from a built-in Verilog system task. Indeed, $monitor is a system task that is part of
the Verilog language. Its mission in life is to print values to the screen. The values it
prints are those corresponding to the arguments that you pass to the task when it is
executed. The $monitor task is executed whenever any one of its arguments changes,
with one or two notable exceptions :
$time is a system function (as opposed to a system task). It returns the current
simulation time. In the above example, $time is an argument to $monitor. However,
$time changing does not cause $monitor to execute — $monitor is clever enough to
know that you wouldn't really want to print to the screen the values of all of the
arguments every time the simulation time changed.
The succession of two commas in the argument list ensures that a space is printed to
the screen after the value of $time each time $monitor is executed. This is a simple
method of formatting the screen output.
Finally we come to the signal arguments themselves. Each time one of these signals
changes value, $monitor will execute. When $monitor executes it will print all of the
argument values to the screen, including $time. This is the output created by $monitor
in our counter test bench:
# 000 x
# 510 x
# 900 x
# 15 0 0 0
# 20 0 1 0
# 25 0 1 1
# 30 0 0 1
# 40 0 1 1
# 45 0 1 2
# 50 0 0 2
# 60 0 1 2
# 65 0 1 3
# 70 0 0 3
# 80 0 1 3
# 85 0 1 4
# 90 0 0 4
endmodule
A last note on this test bench ; to access and monitor a signal which is in a lower hierarchical
level, you just have to call the signal by the module name, followed by a dot, followed by the
signal name.
Page 14 sur 14