CPPC 12 Lab Notes 2 ModelSim Tutorial PDF
CPPC 12 Lab Notes 2 ModelSim Tutorial PDF
1. Conceptual Overview
Modelsim is a program created by Mentor Graphics, verification and simulation tool for VHDL,
Verilog, SystemVerilog, SystemC, and mixed-language designs. It is the most widely use
simulation program in business and education.
Simulation is a critical step of designing FPGAs and ASICs. Simulation allows the designer to
stimulate his or her design and see how the code that they wrote reacts to the stimulus. A great
simulation will exercise all possible states of the design to ensure that all input scenarios will be
handled appropriately. Did you forget if statement somewhere? Did you remember to give
every possible case statement assignment? These are the types of errors that are very easy to
make when you do not simulate your design. Let's get started.
This lesson provides a brief conceptual overview of the ModelSim simulation environment. It is
divided into five topics, which you will learn more about in subsequent lessons.
• Design Optimizations
• Basic simulation flow
• Project flow
• Multiple library flow
• Debugging tools
Design Optimizations
Before discussing the basic simulation flow, it is important to understand design optimization. By
default, ModelSim optimizations are automatically performed on all designs. These
optimizations are designed to maximize simulator performance, yielding improvements up to
10X, in some Verilog designs, over non-optimized runs.
Global optimizations, however, may have an impact on the visibility of the design simulation
results you can view – certain signals and processes may not be visible. If these signals and
processes are important for debugging the design, it may be necessary to customize the
simulation by removing optimizations from specific modules.
Debug results
Project Flow
A project is a collection mechanism for an HDL design under specification or test. Even though
you don’t have to use projects in ModelSim, they may ease interaction with the tool and are
useful for organizing files and specifying simulation settings.
The following diagram shows the basic steps for simulating a design within a ModelSim project.
Create a project
Run simulation
Debug results
As you can see, the flow is similar to the basic simulation flow. However,
there are two important differences:
You specify which resource libraries will be used when the design is compiled, and there are
rules to specify in which order they are searched. A common example of using both a working
library and a resource library is one where your gate-level design and test bench are compiled
into the working library, and the design references gate-level models in a separate resource
library.
The diagram below shows the basic steps for simulating with multiple libraries.
Run simulation
Debug results
You can also link to resource libraries from within a project. If you are using a project, you would
replace the first step above with these two steps: create the project and add the test bench to
the project.
Debugging Tools
ModelSim offers numerous tools for debugging and analyzing your design. Several of these
tools are covered in subsequent lessons, including:
• Using projects
• Working with multiple libraries
• Simulating with SystemC
• Setting breakpoints and stepping through the source code
• Viewing waveforms and measuring time
CPPC 12 Introduction to HDL Laboratory Notes 2
In this lesson you will go step-by-step through the basic simulation flow:
tcounter.v
tcounter.vhd
This lesson uses the Verilog files counter.v and tcounter.v. If you have a VHDL license,
use
counter.vhd and tcounter.vhd instead. Or, if you have a mixed license, feel free to use the
Verilog test bench with the VHDL counter or vice versa.
1. Create a new directory and copy the design files for this lesson into it. Start by
creating a new directory for this exercise (in case other users will be working with
these lessons).
Start by creating a new directory for this exercise (in case other users will be
working with these lessons).
Verilog: Copy counter.v and tcounter.v files from
/<install_dir>/examples/tutorials/verilog/basicSimulation to the new directory.
VHDL: Copy counter.vhd and tcounter.vhd files from
/<install_dir>/examples/tutorials/vhdl/basicSimulation to the new directory.
CPPC 12 Introduction to HDL Laboratory Notes 2
b. Type work in the Library Name field (if it isn’t already entered automatically).
c. Click OK.
ModelSim creates a directory called work and writes a specially-formatted file named
_info into that directory. The _info file must remain in the directory to distinguish it as a
ModelSim library. Do not edit the folder contents from your operating system; all
changes should be made from within ModelSim.
ModelSim also adds the library to the Library window (Figure 3-2) and records the library
mapping for future reference in the ModelSim initialization file (modelsim.ini).
CPPC 12 Introduction to HDL Laboratory Notes 2
When you pressed OK in step 3c above, the following was printed to the Transcript
window:
vlib work
vmap work work
These two lines are the command-line equivalents of the menu selections you made.
Many command-line equivalents will echo their menu-driven functions in this fashion.
You can compile by using the menus and dialogs of the graphic interface, as in the
Verilog example below, or by entering a command at the ModelSim> prompt.
The +acc switch provides visibility into the design for debugging purposes.
The -o switch allows you designate the name of the optimized
design file (testcounter_opt).
Note
You must provide a name for the optimized design file when you use the vopt
command.
Figure 3-7. Using the Popup Menu to Add Signals to Wave Window
c. Click the Run -All icon on the Main or Wave window toolbar.
The simulation continues running until you execute a break command or it hits
a statement in your code (e.g., a Verilog $stop statement) that halts the
simulation.
The Restart dialog that appears gives you options on what to retain during the
restart (Figure 3-10).
When a breakpoint is reached, typically you want to know one or more signal
values. You have several options for checking values:
• set your mouse pointer over a variable in the Source window and a yellow
box will appear with the variable name and the value of that variable at the
time of the selected cursor in the Wave window
Lesson Wrap-Up
This concludes this lesson. Before continuing we need to end the current simulation.
Creating a Project
At a minimum, projects contain a work library and a session state that is stored in an .mpf file. A
project may also consist of:
This lesson uses the Verilog files tcounter.v and counter.v. If you have a VHDL license, use
tcounter.vhd and counter.vhd instead.
This opens the Create Project dialog where you can enter a Project Name,
Project Location (i.e., directory), and Default Library Name (Figure 4-1). You can
also reference library settings from a selected .ini file or copy them directly into
the project. The default library is where compiled design units will reside.
b. Type test in the Project Name field.
c. Click the Browse button for the Project Location field to select a directory
where the project file will be stored.
d. Leave the Default Library Name set to work.
e. Click OK.
b. Click the Browse button for the File Name field. This opens
the “Select files to add to project” dialog and displays the
contents of the current directory.
c. Verilog: Select counter.v and tcounter.v and click Open.
VHDL: Select counter.vhd and tcounter.vhd and click Open.
This closes the “Select files to add to project” dialog and displays the selected
files in the “Add file to Project” dialog (Figure 4-3).
d. Click OK to add the files to the project.
e. Click Close to dismiss the Add items to the Project dialog.
You should now see two files listed in the Project window (Figure 4-4). Question-
mark icons in the Status column indicate that the file has not been compiled or
that the source file has changed since the last successful compile. The other
columns identify file type (e.g., Verilog or VHDL), compilation order, and modified
date.
Figure 4-4. Newly Added Project Files Display a ’?’ for Status
CPPC 12 Introduction to HDL Laboratory Notes 2
ModelSim compiles both files and changes the symbol in the Status column to a green check
mark. A check mark means the compile succeeded. If compile fails, the symbol will be a red ’X’,
and you will see an error message in the Transcript window.
2. View the design units.
a. Click the Library tab (Figure 4-6).
b. Click the ’+’ icon next to the work library.
You should see two compiled design units, their types (modules in this case), and the path to the
underlying source files.
The +acc switch provides visibility into the design for debugging
purposes.
The -o switch allows you designate the name of the optimized design file
(testcounter_opt).
Note
You must provide a name for the optimized design file when you use the vopt command.
At this point you would typically run the simulation and analyze or debug your design like
you did in the previous lesson. For now, you’ll continue working with the project.
However, first you need to end the simulation that started when you loaded test_counter.
2. End the simulation.
a. Select Simulate > End Simulation.
b. Click Yes.
CPPC 12 Introduction to HDL Laboratory Notes 2
Add Folders
As shown previously in Figure 4-2, the Add items to the Project dialog have an option for adding
folders. If you have already closed that dialog, you can use a menu command to add a folder.
c. Click OK.
The new Design Files folder is displayed in the Project window (Figure 4-9).
2. Add a sub-folder.
CPPC 12 Introduction to HDL Laboratory Notes 2
a. Right-click anywhere in the Project window and select Add to Project >
Folder.
b. Type HDL in the Folder Name field (Figure 4-10).
c. Click the Folder Location drop-down arrow and select Design Files.
d. Click OK.
A ’+’ icon appears next to the Design Files folder in the Project window (Figure 4-
11).
Figure 4-12. Changing File Location via the Project Compiler Settings Dialog
The selected files are moved into the HDL folder. Click the ’+’ icon next to the HDL folder to see
the files.
The files are now marked with a ’?’ in the Status column because you moved the files. The
project no longer knows if the previous compilation is still valid.
Simulation Configurations
A Simulation Configuration associates a design unit(s) and its simulation options. For example,
let’s say that every time you load tcounter.v you want to set the simulator resolution to
picoseconds (ps) and enable event order hazard checking. Ordinarily, you would have to specify
those options each time you load the design. With a Simulation Configuration, you specify
options for a design and then save a “configuration” that associates the design and its options.
The configuration is then listed in the Project window and you can double-click it to load
tcounter.v along with its options.
The Project window now shows a Simulation Configuration named counter in the
HDL folder (Figure 4-14).
Lesson Wrap-Up
This concludes this lesson. Before continuing you need to end the current simulation and
close the current project.
If you do not close the project, it will open automatically the next time you start ModelSim.
CPPC 12 Introduction to HDL Laboratory Notes 2
Laboratory Exercise 1
The code that we will be simulating is the VHDL design below. The actual code is not important,
so if you are learning Verilog that's OK! You don't need to know VHDL for this tutorial. The
VHDL code creates a simple and Gate and provides some inputs to it via a test bench. Copy
the code below to and_gate.vhd and the testbench to and_gate_tb.vhd.
and_gate.vhd:
1
2 library ieee;
3 use ieee.std_logic_1164.all;
4
entity and_gate is
5 port (
6
input_1 : in std_logic;
7
input_2 : in std_logic;
8 and_result : out std_logic
9 );
10 end and_gate;
11
12 architecture rtl of and_gate is
signal and_gate : std_logic;
13 begin
14 and_gate <= input_1 and input_2;
15 and_result <= and_gate;
16 end rtl;
17
and_gate_tb.vhd:
1 library ieee;
2 use ieee.std_logic_1164.all;
3
4 entity and_gate_tb is
end and_gate_tb;
5
6 architecture behave of and_gate_tb is
7 signal r_SIG1 : std_logic := '0';
8 signal r_SIG2 : std_logic := '0';
9 signal w_RESULT : std_logic;
10
11 component and_gate is
port (
12
input_1 : in std_logic;
13
input_2 : in std_logic;
14 and_result : out std_logic);
15 end component and_gate;
16
17 begin
18
19 and_gate_INST : and_gate
20 port map (
CPPC 12 Introduction to HDL Laboratory Notes 2
Let's open Modelsim. You are greeted with a window that looks like this
In order to run your simulation, you need to create a project. Click File -> New -> Project. You
will see the window presented on the left. Choose a location for your new project and give it the
name and_gate. Projects in Modelsim have the file extension .prj. Leave the other settings to
their default. This just says that all code will be compiled into the library "work".
Click on Add Existing File as shown in the picture to the right. Navigate to the location where you
downloaded and_gate.vhd and and_gate_tb.vhd and add both of those to your project. Keep other
settings at their default. Click OK when done.
CPPC 12 Introduction to HDL Laboratory Notes 2
Notice now that the files have been added successfully to your project. See those two blue question
marks in the Modelsim Project Window Figure above? That means that Modelsim has not compiled
the files yet. You will need to compile the source files. To do this, right click on and_gate.vhd, click
on Compile, then click on Compile All. You should see messages in the Console window appear in
green that the compile was successful as shown in the screenshot below.
To start your simulation, click on Simulate in the Menu Bar, then click Start Simulation. This opens
the Start Simulation Window. Click on the plus sign next to work, then click on the plus sign next to
and_gate_tb. Make sure you select and_gate_tb and not and_gate as we want to simulate the
design at the test bench level. Once and_gate_tb is highlighted, click OK.
CPPC 12 Introduction to HDL Laboratory Notes 2
Almost there! The simulation is ready and waiting. Now, the majority of the time that you use
Modelsim will be spent looking at the waveform view. The waveform view contains waves (binary 0's
and 1's, hexadecimal digits, binary digits, enumerated types, etc) for all of the signals in your design.
It shows how your module reacts to different stimulus. The next figure shows you what your
waveform view looks like, but first you need to add some signals to monitor. In this example, we will
monitor all of the signals in the test bench. To do this, right click on and_gate_tb in the sim window
and click Add Wave. You can also click and drag signals to the waveform window from other
windows in Modelsim.
CPPC 12 Introduction to HDL Laboratory Notes 2
Here is your waveform window. All of the test bench signals have been added as signals your can
monitor. To run the simulation, click the Icon with a little piece of paper and a down arrow next to the
100 ns time. This will run your simulation for 100 nanoseconds. Watch how the signals change!
Congratulations! You've created your first Modelsim simulation!
This tutorial showed you how to create your own project in ModelSim, add files to your project,
compile your source files, start your simulation, and view your waveforms.
References:
ModelSim User’s Manual