Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Study of MODELSIM
Aim: To Study the ModelSim tool.
ModelSim is a simulation and debugging tool for Verilog, VHDL, and mixed-language designs.
The following diagram shows the basic steps for simulating a design in ModelSim.
Run simulation
Debug Results
b. Project flow
A project is a collection mechanism for an HDL design under specification or test. Even though
you dont 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 do not have to create a working library in the project flow; it is done for you
automatically.
Projects are persistent. In other words, they will open every time you invoke ModelSim
unless you specifically close them.
ModelSim uses libraries in two ways: 1) as a local working library that contains the compiled
version of your design; 2) as a resource library. The contents of your working library will
change as you update your design and recompile. A resource library is typically static and
serves as a parts source for your design. You can create your own resource libraries, or they
may be supplied by another design team or a third party (e.g., a silicon vendor).
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
The diagram 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.
d. Debugging tools
ModelSim offers numerous tools for debugging and analyzing your design. Several of these
tools are covered in subsequent lessons, including:
Setting breakpoints and stepping through the source code
Viewing waveforms and measuring time
Viewing and initializing memories
Before you can simulate a design, you must first create a library and compile the source code
into that library.
1. Create a new directory and copy the tutorial files into it.
Start by creating a new directory for this exercise (in case other users will be working with
these lessons).
When you pressed OK in step c above, three lines were printed to the Main window Transcript
pane:
vlib work
vmap work work
# Modifying modelsim.ini
The first two lines are the command-line equivalent of the menu commands you invoked. Most
menu driven functions will echo their command-line equivalents in this fashion. The third line
notifies you that the mapping has been recorded in the ModelSim initialization file.
With the working library created, you are ready to compile your source files.
You can compile by using the menus and dialogs of the graphic interface, as in the Verilog
example below.
When the design is loaded, you will see a new tab named sim that displays the
hierarchical structure of the design (Fig. 6). You can navigate within the hierarchy
by clicking on any line with a + (expand) or - (contract) icon. You will also see
a tab named Files that displays all files included in the design.
Next you will take a brief look at one interactive debugging feature of the ModelSim
environment. You will set a breakpoint in the Source window, run the simulation, and then step
through the design under test. Breakpoints can be set only on lines with red line numbers.
1. Open counter.v in the Source window.
a) Select the Files tab in the Main window Workspace.
b) Double-click counter.v to add it to the Source window.
2. Set a breakpoint on line 31 of counter.v.
a) Scroll to line 31 and click on the line number.
A red ball appears next to the line (Fig. 9) indicating that a breakpoint has been set.
3. Disable, enable, and delete the breakpoint.
a) Click the red ball to disable the breakpoint. It will become a black circle.
Fig. 11: Resting the mouse pointer on a variable in the Source view
Introduction
In this lesson you will practice creating a project. 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:
HDL source files or references to source files
other files such as READMEs or other project documentation
local libraries
references to global libraries
This lesson uses the Verilog files tcounter.v and counter.v in the examples.
Fig. 18: The structure tab for the counter design unit
Simulation Configurations
A Simulation Configuration associates a design unit(s) and its simulation options.
For example, say 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 tab and you can double-click it to load counter.v
along with its options.
Before continuing you need to end the current simulation and close the current project.
1. Select Simulate > End Simulation. Click Yes.
2. Select the Project tab in the Main window Workspace.
3. Right-click the test project to open a context popup menu and select Close Project.
If you do not close the project, it will open automatically the next time you start
ModelSim.
RESULT:
Thus the design and simulation procedure for ModelSim tool is studied.
Aim:
To simulate the following basic modules using gate level modelling.
I. Half adder
II. Full adder
III. Half subtractor
IV. Full subtractor
Verilog code:
I. Half adder:
Source code:
module halfadder(cout,sum,a,b);
input a,b;
output cout,sum;
xor(sum,a,b);
and(cout,a,b);
endmodule
Stimulus:
module halfaddstim;
reg a,b;
wire cout,sum;
halfadder h1(cout, sum,a,b);
initial
begin
$monitor($time,"a=%b,b=%b,sum=%b,cout=%b",a,b,sum,cout);
end
initial
begin
a=1'b0;b=1'b0;
#5 a=1'b0;b=1'b1;
#5 a=1'b1;b=1'b0;
#5 a=1'b1;b=1'b1;
end
endmodule
Observation:
Waveforms:
Source code:
module fulladder(sum,cout,a,b,c);
output sum,cout;
input a,b,c;
wire s1,c1,c2,c3;
xor(s1,a,b);
xor(sum,s1,c);
and(c1,a,b);
and(c2,b,c);
and(c3,a,c);
or(cout,c1,c2,c3);
endmodule
Stimulus:
module fulladdstim;
reg a,b,c;
wire sum,cout;
fulladder fa(sum,cout,a,b,c);
initial
begin
$monitor($time, "a=%b, b=%b, c=%b, sum=%b,
cout=%b",a,b,c,sum,cout);
end
initial
begin
a=1'b0;b=1'b0;c=1'b0;
#5 a=1'b0;b=1'b0;c=1'b1;
#5 a=1'b0;b=1'b1;c=1'b0;
#5 a=1'b0;b=1'b1;c=1'b1;
#5 a=1'b1;b=1'b0;c=1'b0;
#5 a=1'b1;b=1'b0;c=1'b1;
#5 a=1'b1;b=1'b1;c=1'b0;
#5 a=1'b1;b=1'b1;c=1'b1;
Observation:
Waveforms:
Source code:
module halfsubtractor(d,b,x,y);
input x,y;
output d,b;
wire i1;
xor(d,x,y);
not(i1,x);
and(b,i1,y);
endmodule
Stimulus:
module halfsubstim;
reg x,y;
wire d,b;
halfsubtractor h1(d, b,x,y);
initial
begin
$monitor($time,"a=%b,b=%b,sum=%b,cout=%b",x,y,d,b);
end
initial
begin
x=1'b0;y=1'b0;
#5 x=1'b0;y=1'b1;
Observation:
Source code:
module fullsub(d,b,x,y,z);
output d,b;
input x,y,z;
wire x1,a1,i1,o1,a2;
xor(x1,x,y);
xor(d,x1,z);
and(a1,y,z);
not(i1,x);
or(o1,y,z);
and(a2,i1,o1);
or(b,a2,a1);
endmodule
Stimulus:
module fullsubstim;
reg x,y,z;
wire d,b;
fullsub fa(d,b,x,y,z);
initial
begin
$monitor($time, "x=%b, y=%b, z=%b, d=%b, b=%b",x,y,z,d,b);
end
initial
begin
x=1'b0;y=1'b0;z=1'b0;
Observation:
Waveforms:
Aim:
To simulate the following basic modules using behavioural modelling.
I. SR FLIP-FLOP
II. JK FLIP-FLOP
III. D FLIP-FLOP
IV. T FLIP-FLOP
Verilog code:
I. SR FLIP-FLOP:
Source code:
Stimulus:
module srf_stim;
reg s,r,clear,reset,clk;
wire q,q1;
always
#5 clk=~clk;
srf s1(q,q1,s,r,clear,reset, clk);
initial
$monitor($time,"clk=%b,clear=%b,reset=%b,s=%b,r=%b,q=%b,q1=%b",clk,clear,rese
t,s,r,q,q1);
initial
begin
Observation:
Waveforms
II. JK FLIP-FLOP:
Source code:
module jkf(q,q1,j,k,clear,reset,clk);
input j,k,clear,reset,clk;
output q,q1;
reg q=1'b0,q1=1'b1;
always@(j or k or clk or reset or clear)
begin
if(clear==1'b1)
{q,q1}=2'b01;
else if(reset==1'b1)
{q,q1}=2'b10;
else
begin
case({j,k})
2'b10:{q,q1}=2'b10;
2'b01:{q,q1}=2'b01;
Stimulus:
module jkf_stim;
reg j,k,clear,reset,clk=1'b1;
wire q,q1;
always
#5 clk=~clk;
jkf s1(q,q1,j,k,clear,reset, clk);
initial
$monitor($time,"clk=%b, clear=%b, reset=%b, j=%b, k=%b, q=%b, q1=%b", clk,
clear,reset,j,k,q,q1);
initial
begin
clear=1'b1;
#15 clear=1'b0; reset=1'b1;
#15 clear=1'b0; reset=1'b0; j=1'b1; k=1'b0;
#15 j=1'b0; k=1'b1;
#15 j=1'b0; k=1'b0;
#15 j=1'b1; k=1'b1;
#15 j=1'b1; k=1'b0;
#15 j=1'b0; k=1'b0;
end
endmodule
Observation:
Waveforms
III. D FLIP-FLOP:
Source code:
module df(q,q1,d,clear,reset,clk);
input d,clear,reset,clk;
output q,q1;
reg q=1'b0,q1=1'b1;
Stimulus:
module df_stim;
reg d,clear,reset,clk=1'b1;
wire q,q1;
always
#5 clk=~clk;
df d1(q,q1,d,clear,reset,clk);
initial
$monitor($time,"clk=%b, clear=%b, reset=%b, d=%b, q=%b, q1=%b",
clk, clear, reset, d, q, q1);
initial
begin
clear=1'b1;
#15 clear=1'b0; reset=1'b1;
#15 clear=1'b0; reset=1'b0;
Observation:
IV. T FLIP-FLOP:
Source code:
module tf(q,q1,t,clear,reset,clk);
input t,clear,reset,clk;
output q,q1;
reg q=1'b0,q1=1'b1;
always@(t or clk or reset or clear)
begin
if(clear==1'b1)
{q,q1}=2'b01;
else if(reset==1'b1)
{q,q1}=2'b10;
else
begin
if(t==1)
{q,q1}=~{q,q1};
else
{q,q1}={q,q1};
end
end
endmodule
Stimulus:
initial
begin
$monitor($time,"clear=%b, reset=%b, t=%b, q=%b,
q1=%b",clk,clear,reset,t,q,q1);
clear=1'b1; clk=1'b1;
#15 clear=1'b0; reset=1'b1;
#15 clear=1'b0; reset=1'b0;
#15 t=1'b0;
#15 t=1'b1;
#15 t=1'b0;
#15 t=1'b1;
end
endmodule
Observation:
Waveforms
Result:
Thus the basic modules were simulated using behavioural model and their
outputs are verified.
Verilog code:
Source code:
Stimulus:
module counter_stim;
reg CLR, up_down;
wire[3:0] Q;
reg CLK=1'b0;
always
#1 CLK=~CLK;
counter c1(Q, CLK, CLR, up_down);
initial
$monitor($time, " clr=%b, up-down=%b, q=%b", CLR, up_down, Q);
initial
begin
CLR=1'b1;
#2 CLR =1'b0; up_down=1'b1;
#30 CLR =1'b0; up_down=1'b0;
#30 CLR=1'b1;
end
endmodule
Observation:
Waveforms:
I. Getting Started
Software Requirements:
To use this tutorial, you must install the following software:
ISE 9.1i
Hardware Requirements:
To use this tutorial, you must have the following hardware:
Spartan-3 Startup Kit, containing the Spartan-3 Startup Kit Demo Board
Accessing Help
To open Help, do either of the following:
Press F1 to view Help for the specific tool or function that you have selected or
highlighted.
Launch the ISE Help Contents from the Help menu. It contains information
about creating and maintaining your complete design flow in ISE.
3. Enter or browse to a location (directory path) for the new project. A tutorial
subdirectory is created automatically.
The source file containing the counter module displays in the Workspace, and the
counter displays in the Sources tab, as shown below:
Use a simple counter code example from the ISE Language Templates and
customize it for the counter design.
1) Place the cursor on the line below the output [3:0] COUNT_OUT; statement.
2) Open the Language Templates by selecting Edit Language
Templates Note: You can tile the Language Templates and the counter
file by selecting Window Tile Vertically to make them both visible.
3) Using the + symbol, browse to the following code example:
Verilog Synthesis Constructs Coding Examples Counters
Binary Up/Down Counters Simple Counter
4) With Simple Counter selected, select Edit Use in File, or select the Use
Template in File toolbar button. This step copies the template into the
counter source file.
5) Close the Language Templates.
When you are finished, the code for the counter will look
like the following: module counter(CLOCK,
DIRECTION, COUNT_OUT);
input CLOCK; input DIRECTION;
output [3:0] COUNT_OUT; reg [3:0] count_int = 0;
always@(posedge CLOCK) if (DIRECTION)
count_int <= count_int + 1;
else
count_int <=
count_int - 1; assign COUNT_OUT =
count_int;
endmodule
You have now created the Verilog source for the tutorial project.
Note: You must correct any errors found in your source files. You can check for errors
in the Console tab of the Transcript window. If you continue without valid syntax, you
will not be able to simulate or synthesize your design.
5) Close the HDL file.
7) Click OK.
8) Select the Pad to Setup toolbar button or double-click the empty Pad to Setup field
to display the Pad to Setup dialog box.
9) Enter 10 ns in the OFFSET field to set the input offset constraint.
The constraints are displayed in the Constraints (read-write) tab, as shown below:
Implement the design and verify that it meets the timing constraints specified in the
previous section.
Implementing the Design
1) Select the counter source file in the Sources window.
2) Open the Design Summary by double-clicking the View Design Summary
process in the Processes tab.
3) Double-click the Implement Design process in the Processes tab.
4) Notice that after Implementation is complete, the Implementation processes have a green
5) Select File Save. You are prompted to select the bus delimiter type based
on the synthesis tool you are using. Select XST Default <> and click OK.
6) Close PACE.
Notice that the Implement Design processes have an orange question mark next to
them, indicating they are out-of-date with one or more of the design files. This is
because the UCF file has been modified.
This is the last step in the design verification process. This section provides simple
instructions for
downloading the counter design to the Spartan-3 Starter Kit demo board.
1) Connect the 5V DC power cable to the power input on the demo board (J4).
2) Connect the download cable between the PC and demo board (J7).
3) Select Synthesis/Implementation from the drop-down list in the Sources
window.
only and click OK. iMPACT opens and the Configure Devices dialog
box is displayed.
Kit Specifications
1. FPGA Specifications:
1.1 Hardware:
Family: Spartan3E
Device: XC3S250E
Package: PQ208
Speed Grade: -4
1.2 Software:
Synthesis Tool: XST (VHDL/Verilog)
Simulator: ISE Simulator (VHDL/Verilog)
2. Other hardware:
Switch S2
Signal S2-1 S2-2 S2-3 S2-4 S2-5 S2-6 S2-7 S2-8
Pin 159 169 174 175 183 184 194 204
Switch
S3
Signal S3-1 S3-2 S3-3 S3-4 S3-5 S3-6 S3-7 S3-8
Pin 159 169 174 175 183 184 194 204
When the switch is ON position the output will be 0 level, which is fed to FPGA as
input. When the switch is OFF position the output of this will be 1 level. The RC is
used to limit the current, while connecting to ground point.
2.3 Outputs:
The FPGA device outputs are connected to bar-graph LEDs which shows the output
level. The output is 1 level the LED will be glowing and when the output is at 0 level
the LED will be in off.
Switch S1
Signal S1-1 S1-2 S1-3 S1-4 S1-5 S1-6 S1-7 S1-8
Pin 106 107 108 109 112 113 115 116
2.6 Keyboard:
The trainer kit has a 4*4 key matrix connected to the FPGA I/O lines. The connection
details are given below:
2.8 LCD:
The trainer kit has one 16*2 LCD display. The connection details are given below:
FRC Pin 1 2 3 4 5 6 7 8 9 10 11 12 13
FPGA
Pin 2 3 4 5 8 9 11 12 15 16 18 19 33
FRC Pin 14 15 16 17 18 19 20 21 22 23 24 25 26
FPGA
Pin 34 35 36 39 40 41 42 45 47 129 132 +5V GND
Aim:
To design a 4-bit Ripple carry adder and demonstrate its working on a Xilinx FPGA
Trainer Kit.
Tools Required:
Hardware:
Family: SPARTAN 3E
Device: XC3S250E
Package: PQ208
Speed: 4
Software:
Xilinx 9.1i
PROCEDURE:
1. Simulation :
Program:
module ripple(sum,cout,a,b,cin);
output [3:0] sum;
output cout;
input [3:0] a,b;
input cin;
wire c1,c2,c3;
fulladd f1(sum[0],c1,a[0],b[0],cin);
fulladd f2(sum[1],c2,a[1],b[1],c1);
fulladd f3(sum[2],c3,a[2],b[2],c2);
fulladd f4(sum[3],cout,a[3],b[3],c3);
endmodule
module fulladd(sum,cout,a,b,c);
output sum,cout;
input a,b,c;
assign sum=a^b^c;
assign cout=(a&b)|(b&c)|(c&a);
endmodule
Simulation Result:
2. SCHEMATIC EDITOR:
Load Xilinx project navigator.
Create new project.
Create new source (schematic).
Draw the given circuit diagram using schematic editor.
View the RTL level schematic.
Synthesis project and verify the results in modelsim.
Synthesis Report:
Result:
Thus the ripple carry adder circuits are designed using verilog and their outputs were
demonstrated in Xilinx FPGA trainer.
Aim:
To design a booth multiplier and demonstrate its working on a Xilinx FPGA Trainer
Kit.
Tools Required:
Hardware:
Family: SPARTAN 3E
Device: XC3S250E
Package: PQ208
Speed: 4
Software:
Xilinx 9.1i
PROCEDURE:
3. Simulation :
Program:
module multiplier(prod, busy, mc, mp, clk);
output [15:0] prod;
output busy;
input [7:0] mc, mp;
input clk;
reg [7:0] A, Q, M;
reg Q_1;
reg [3:0] count=3'b000;
wire [7:0] sum, difference;
always @(negedge clk)
begin
if (count==0) begin
A <= 8'b0;
M <= mc;
Q <= mp;
Q_1 <= 1'b0;
count <= 8;
end
else begin
case ({Q[0], Q_1})
2'b0_1 : {A, Q, Q_1} <= {sum[7], sum, Q};
2'b1_0 : {A, Q, Q_1} <= {difference[7], difference, Q};
default: {A, Q, Q_1} <= {A[7], A, Q};
endcase
count <= count - 1'b1;
end
end
alu adder (sum, A, M, 1'b0);
4. SCHEMATIC EDITOR:
BLOCK DIAGRAM:
RTL SCHEMATIC:
SYNTHESIS REPORT:
Result:
Thus the BOOTH MULTIPLIER circuit is designed using verilog and the output was
demonstrated in Xilinx FPGA trainer.
Moore FSM
1011 overlapping sequence detector. Output becomes 1 when sequence is detected in state S4 else it
remains 0 for other states.
endmodule // moore_mac