ModelSim Tutorial


Learning Objectives

  1. Learn the basic use of ModelSim
  2. Learn how to create a .do file
  3. See how ModelSim reports errors

Learning Resources

  1. Course text book.
  2. 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

  1. Create a project directory for your lab. For example Z:\Lab1

The Tutorial - Quick Steps

  1. Create the xor2.vhd file in the project directory.
  2. Create the xor3.vhd file in the project directory.
  3. Create the xor3.do file. A sample .do file can be found in appendix C of this document.
  4. Start Modelsim and have it model your circuit.
    1. Double click the ModelSim icon on the desktop.
    2. Click on ModelSim's File menu option and select "Change Directory..."
    3. The "Chose a Directory" dialog box will appear.
    4. Double click on your project folder, then click the open button.
    5. Click on ModelSim's Tools memu and select "TCL -> Execute Macro..." option.
    6. The "Execute Do File" dialog box will appear.
    7. Select your XOR3.do file and click the open button.
  5. Refine your design.
    1. If there were errors, the error message will be in the ModelSim main GUI window.
    2. Review the signal window to verify correct behavior.
    3. Make appropriate changes to your .vhd files and model again.


The Tutorial - Detailed Steps

  1. Create the xor2.vhd file.
    1. Double click the Emacs icon on the desktop.
    2. Type (or cut and paste) the contents of xor2.vhd into Emacs.
    3. In the Emacs GUI, click on the Files menu and select the "Save Buffer As..." option.
    4. 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

  2. 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.

    1. 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.
    2. 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
    3. Use an Emacs plug-in to paste an existing component into your current .vhd file.
      1. In Emacs click on the Buffers menu option and select the xor2.vhd buffer.
      2. 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.
      3. Click on the VHDL menu option and select Port, then select Copy.
      4. Click on the Buffers menu option and select xor3.vhd.
      5. 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.
      6. Click on the VHDL menu option and select Port, then select "Paste As Component".
      7. If the steps were performed correctly, you should now see a component statement for the xor2 component.
      8. Cut and paste the following statements after the component you just created:
        	  signal y : std_logic;
          
        	begin
        
        
        	end structure;
    4. Use an Emacs plug-in to paste an existing instance into your current .vhd file
      1. Click on the Buffers menu option and select the xor2.vhd buffer.
      2. 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.
      3. Click on the VHDL menu option and select Port, then select Copy.
      4. Click on the Buffers menu option and select xor3.vhd.
      5. 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.
      6. Click on the VHDL menu option and select Port, then select "Paste As Instance".
      7. Press the enter key to create a blank line following the instance you just created.
      8. Repeat step f to create a second instance.
      9. 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);
      10. 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);
    5. On the File menu option, select Save Buffer.

  3. 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.
    1. Create a work directory for ModelSim to store libraries and other files.
      	# 1) Create a work directory for modelsim
      	vlib ./work
      
    2. Compile your .vhd files.
      	# 2) compile stuff in defined use order
      	vcom -93 -work work xor2.vhd
      	vcom -93 -work work xor3.vhd
      
    3. Tell ModelSim to model your top level .vhd file.
      	# 3) use xor3 entity to simulate
      	vsim xor3
      
    4. Tell ModelSim which windows to open during the simulation.
      	# 4) Open some selected windows for viewing
      	view structure
      	view signals
      	view wave
      
    5. 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
      
    6. 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
      

  4. Tell ModelSim to actually simulate the circuit.
    1. Start ModelSim by double clicking the ModelSim icon on the desktop.
    2. In ModelSim, click on the File menu item and select Change Directory. Then select your project directory. Click Open.
    3. In ModelSim, click on the Tools menu and select Execute Macro (under the "TCL" command).
    4. In the "Execute Do File" dialog box, drill down to your project directory and select your .do file.
    5. Click the Open button.

  5. 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