
ModelSim Tutorial
Learning Objectives
- Learn the basic use of ModelSim
- Learn how to create a .do file
- See how ModelSim reports errors
Learning Resources
- Course text book.
- ModelSim help files.
Tutorial Overview
For this course, we will be using a subset of the ModelSim functionality.
The following tutorial will walk you through setting ModelSim up for a
typical lab assignment.
Preparing for the Tutorial
- Create a project directory for your lab. For example Z:\Lab1
The Tutorial - Quick Steps
- Create the xor2.vhd file in the project directory.
- Create the xor3.vhd file in the project directory.
- Create the xor3.do file. A sample .do file can be found in appendix C of this document.
- Start Modelsim and have it model your circuit.
- Double click the ModelSim icon on the desktop.
- Click on ModelSim's File menu option and select "Change Directory..."
- The "Chose a Directory" dialog box will appear.
- Double click on your project folder, then click the open button.
- Click on ModelSim's Tools memu and select "TCL -> Execute Macro..." option.
- The "Execute Do File" dialog box will appear.
- Select your XOR3.do file and click the open button.
- Refine your design.
- If there were errors, the error message will be in the ModelSim main GUI window.
- Review the signal window to verify correct behavior.
- Make appropriate changes to your .vhd files and model again.
The Tutorial - Detailed Steps
- Create the xor2.vhd file.
- Double click the Emacs icon on the desktop.
- Type (or cut and paste) the contents of xor2.vhd into Emacs.
- In the Emacs GUI, click on the Files menu and select the "Save Buffer As..." option.
- The Emacs buffer will be saved in the directory and file specified on the very bottom line in the Emacs GUI. Make sure the directory and file name are correct and then press the enter key - ie. Write file: Z:\lab1\xor2.vhd
- Create the xor3.vhd file.
Note: This step will walk you through the procedure of copying an entity and
pasting the entity as a VHDL component and/or instance.
- Create the xor3.vhd file. In Emacs, enter ^X ^F (control X control F). You will be prompted for a filename. Enter the name of the file you want to open or create. If the file exists, it will be opened. If the file does not exist, it will be created.
- Type (or cut and paste) the following into Emacs.
library ieee;
use ieee.std_logic_1164.all;
entity xor3 is
port (
a : in std_logic;
b : in std_logic;
c : in std_logic;
x : out std_logic);
end xor3;
architecture structure of xor3 is
- Use an Emacs plug-in to paste an existing component into your current .vhd file.
- In Emacs click on the Buffers menu option and select the xor2.vhd buffer.
- Position the mouse on the entity xor2 line in the current buffer and click the left mouse button to position the cursor on the entity line.
- Click on the VHDL menu option and select Port, then select Copy.
- Click on the Buffers menu option and select xor3.vhd.
- Position the mouse on the line under the line which reads architecture structure of xor3 is and click the left mouse button to position the cursor on the blank line.
- Click on the VHDL menu option and select Port, then select "Paste As Component".
- If the steps were performed correctly, you should now see a component statement for the xor2 component.
- Cut and paste the following statements after the component you just created:
signal y : std_logic;
begin
end structure;
- Use an Emacs plug-in to paste an existing instance into your current .vhd file
- Click on the Buffers menu option and select the xor2.vhd buffer.
- Position the mouse on the entity xor2
line in the current buffer and click the left mouse button to position the
cursor on the entity line.
- Click on the VHDL menu option and select Port, then select Copy.
- Click on the Buffers menu option and select xor3.vhd.
- Position the mouse on blank line following the line which reads
begin and then click the
left mouse button to position the cursor on the blank line.
- Click on the VHDL menu option and select Port, then select
"Paste As Instance".
- Press the enter key to create a blank line following the instance
you just created.
- Repeat step f to create a second instance.
- Change the mapping of the xor2_1 instance of the xor2 component to
read as follows
xor2_1: xor2
port map (
a => a,
b => b,
x => y);
- Change the mapping of the xor2_2 instance of the xor2 component to read as follows
xor2_2: xor2
port map (
a => y,
b => c,
x => x);
- On the File menu option, select Save Buffer.
- Create the xor3.do file. Use Emacs (or an editor of your choice)
to create the xor3.do file. There are 6 basic steps you should perform in your .do file.
The following goes through each of the 6 steps. The text under each section can be
pasted into you .do file, or a complete .do file can be found in appendix C.
- Create a work directory for ModelSim to store libraries and other files.
# 1) Create a work directory for modelsim
vlib ./work
- Compile your .vhd files.
# 2) compile stuff in defined use order
vcom -93 -work work xor2.vhd
vcom -93 -work work xor3.vhd
- Tell ModelSim to model your top level .vhd file.
# 3) use xor3 entity to simulate
vsim xor3
- Tell ModelSim which windows to open during the simulation.
# 4) Open some selected windows for viewing
view structure
view signals
view wave
- If you are using the signal window, specify the signals you want displayed
# 5) Show some of the signals in the wave window
add wave -noupdate -divider -height 32 Inputs
add wave a
add wave b
add wave c
add wave -noupdate -divider -height 32 Internal Signals
add wave y
add wave -noupdate -divider -height 32 Outputs
add wave x
- Set key signals to known values and observe how the circuit behaves.
To set a signal to a known value, you use the force command. The syntax
of the force command is force item-name value time To set
the value of signal xyz to a zero at time zero, the syntax would be:
force -deposit xyz 0 0
# 6) Set some test patterns
force -deposit a 0 0
force -deposit b 0 0
force -deposit c 0 0
force -deposit a 0 10
force -deposit b 0 10
force -deposit c 1 10
force -deposit a 0 20
force -deposit b 1 20
force -deposit c 0 20
force -deposit a 0 30
force -deposit b 1 30
force -deposit c 1 30
force -deposit a 1 40
force -deposit b 0 40
force -deposit c 0 40
force -deposit a 1 50
force -deposit b 0 50
force -deposit c 1 50
force -deposit a 1 60
force -deposit b 1 60
force -deposit c 0 60
force -deposit a 1 70
force -deposit b 1 70
force -deposit c 1 70
run 80
Note: comment lines in a .do file start with the # character
- Tell ModelSim to actually simulate the circuit.
- Start ModelSim by double clicking the ModelSim icon on the desktop.
- In ModelSim, click on the File menu item and select Change Directory. Then select your project directory. Click Open.
- In ModelSim, click on the Tools menu and select Execute Macro (under the "TCL" command).
- In the "Execute Do File" dialog box, drill down to your project directory and select your .do file.
- Click the Open button.
- Refine your design. If there were errors, the error message will be in the ModelSim main GUI window. Review the signal window to verify correct behavior. Make appropriate changes to your .vhd files and model again. The following is what your signal window should look like:
Appendix - Useful information
How to create a new file in Emacs without exiting first.
enter ^X^F (control X control F)
You will be prompted for a filename.
Enter the name of the file you want to open or create
If the file exists, it will be opened
If the file does not exist, it will be created.
How to copy a component
Note for this to work, both the source file and target file must both be open in the same instance of Emacs
Click the Buffers menu option and select the buffer with the .vhd code for the entity you want to copy.
Position the cursor on the entity line (or any where within the entity definition).
Click the VHDL menu option and select Port, then select Copy.
How to paste a component
Copy the component first (see How to copy a component)
Position the cursor on a blank where you want to paste the component.
Click the VHDL menu option and select Port, then select Paste As Component.
How to paste an instance
Copy the component first (see How to copy a component)
Position the cursor on a blank where you want to paste the instance.
Click the VHDL menu option and select Port, then select Paste As Instance.
How to add a divider between signals in the signal window
add wave -noupdate -divider -height 32 Inputs
How to specify a generic value when simulating. Assume your VHDL code has a generic named SIZE
vsim RippleAdder -gSIZE=4
Created: June 10, 2004
Last modified: July 26, 2004