RTL Simulation Lab Manual
RTL Simulation Lab Manual
RTL Simulation Lab Manual
Department Of
ELECTRONICS & COMMUNICATION ENGINEERING
VIGNAN INSTITUTE OF TECHNOLOGY AND
SCIENCE
VIGNAN HILLS, DESHMUKHI VILLAGE, POCHAMPALLY (MANDAL)
NALGONDA (DISTRICT) - 508284
Sponsored by
Lavu Educational Society
(Approved by AICTE and Affiliated to JNT University, Hyderabad)
Introduction
Xilinx Tools is a suite of software tools used for the design of digital circuits
implemented using Xilinx Field Programmable Gate Array (FPGA) or Complex
Programmable Logic Device (CPLD). The design procedure consists of (a) design
entry, (b) synthesis and implementation of the design, (c) functional simulation and
(d) testing and verification. Digital designs can be entered in various ways using
the above CAD tools: using a schematic entry tool, using a hardware description
language (HDL) – Verilog or VHDL or a combination of both. In this lab we will
only use the design flow that involves the use of Verilog HDL.
All your designs for this lab must be specified in the above Verilog input
format. Note that the state diagram segment does not exist for combinational logic
designs.
Xilinx Tools can be started by clicking on the Project Navigator Icon on the
Windows desktop. This should open up the Project Navigator window on your
screen. This window shows (see Figure 1) the last accessed project.
3.1 Opening a project
Select File->New Project to create a new project. This will bring up a new
project window (Figure 2) on the desktop. Fill up the necessary entries as follows:
Project Name: Write the name of your new project.
Project Location: The directory where you want to store the new project
(Note: DO NOT specify the project location as a folder on Desktop or a folder in
the Xilinx\bin directory. Your H: drive is the best place to put it. The project
location path is NOT to have any spaces in it eg: C:\Nivash\TA\new lab\sample
exercises\o_gate is NOT to be used)
Example: If the project name were “o_gate”, enter “o_gate” as the project name
and then click “Next”. Clicking on NEXT should bring up the following window:
For each of the properties given below, click on the ‘value’ area and select from
the list of values that appear.
Device: The number of the actual device. For this lab you may enter XC3S500E
(this can be found on the attached prototyping board)
Package o: The type of package with the number of pins. The Spartan FPGA used
in this lab is packaged in FG320 package.
All project files such as schematics, netlists, Verilog files, VHDL files, etc., will be
stored in a subdirectory with the project name. A project can only have one top
level HDL source file (or schematic). Modules can be added to the project to create
a modular, hierarchical design (see Section 9).
3.2 Creating a Verilog HDL input file for a combinational logic design
In this lab we will enter a design using a structural or RTL description using the
Verilog HDL. You can create a Verilog HDL input file (.v file) using the HDL Editor
available in the Xilinx ISE Tools (or any text editor). In the previous
window, click on the NEW SOURCE
If a source has to be removed, just right click on the source file in the
Sources in Project window in the Project Navigator and select Remove in that.
Then select Project -> Delete Implementation Data from the Project Navigator
menu bar to remove any related files.
3.3 Editing the Verilog source file
The source file will now be displayed in the Project Navigator window
(Figure 8). The source file window can be used as a text editor to make any
necessary changes to the source file. All the input/output pins will be displayed.
Save your Verilog program periodically by selecting the File->Save from the
menu. You can also edit Verilog programs in any text editor and add them to the
project directory using “Add Copy Source”.
The Verilog source code template generated shows the module name, the list
of ports and also the declarations (input/output) for each port. Combinational logic
code can be added to the verilog code after the declarations and before the
endmodule line.
module or_gate(a,b,z);
input a;
input b;
output z;
reg z;
always @(a or b)
begin
case ({a,b})
00: z = 1'b0;
01: z = 1'b1;
10: z = 1'b1;
11: z = 1'b1;
endcase
end
endmodule
Suppose we want to describe an OR gate. It can be done using the logic equation as
shown in Figure 9a or using the case statement (describing the truth table) as
shown in Figure 9b. These are just two example constructs to design a logic
function. Verilog offers numerous such constructs to efficiently model designs. A
brief tutorial of Verilog is available in Appendix-A.
4. Synthesis and Implementation of the Design
The design has to be synthesized and implemented before it can be checked
for correctness, by running functional simulation or downloaded onto the
prototyping board. With the top-level Verilog file opened (can be done by double-
clicking that file) in the HDL editor window in the right half of the Project
Navigator, and the view of the project being in the Module view , the implement
design option can be seen in the process view. Design entry utilities and Generate
Programming File options can also be seen in the process view. The former can be
used to include user constraints, if any and the latter will be discussed later.
To synthesize the design, double click on the Synthesize Design option in the
Processes window.
To implement the design, double click the Implement design option in the
Processes window. It will go through steps like Translate, Map and Place & Route.
If any of these steps could not be done or done with errors, it will place a X mark
in front of that, otherwise a tick mark will be placed after each of them to indicate
the successful completion. If everything is done successfully, a tick mark will be
placed before the Implement Design option. If there are warnings, one can see
mark in front of the option indicating that there are some warnings. One can look
at the warnings or errors in the Console window present at the bottom of the
Navigator window. Every time the design file is saved; all these marks disappear
asking for a fresh compilation.
By double clicking it opens the top level module showing only input(s) and
output(s) as shown below.
Figure 13: Realized logic by the XilinxISE for the verilog code
In the Sources window (top left corner) right click on the file that you want to
generate the test bench for and select ‘New Source’
Provide a name for the test bench in the file name text box and select ‘Verilog
test fixture’ among the file types in the list on the right side as shown in figure 11.
module o_gate_tb_v;
// Inputs
reg a;
reg b;
// Outputs
wire z;
o_gate uut (
.a(a),
.b(b),
.z(z)
);
initial begin
// Initialize Inputs
a = 0;
b = 0;
#100;
End
endmodule
The Xilinx tool detects the inputs and outputs of the module that you are going to
test and assigns them initial values. In order to test the gate completely we shall
provide all the different input combinations. ‘#100’ is the time delay for which the
input has to maintain the current value. After 100 units of time have elapsed the
next set of values can be assign to the inputs. Complete the test bench as shown
below:
module o_gate_tb_v;
// Inputs
reg a;
reg b;
// Outputs
wire z;
o_gate uut (
.a(a),
.b(b),
.z(z)
);
initial begin
// Initialize Inputs
a=0;
b=0;
a = 0;
b = 1;
#100;
a = 1;
b = 0;
#100;
a=1;
b=1;
#100;
End
Endmodule
Note that by default, the waveform is “zoomed in” to the nanosecond level.
Use the zoom controls to display the entire waveform..
Else a normal print screen option can be used on the waveform window and
subsequently stored in Paint.
For taking printouts for the lab reports, convert the black background to white in
Tools -> Edit Preferences. Then click Wave Windows -> Wave Background
attribute.
5. Once the program is working properly it is time to create a UCF (user constraint file) and
assign the FPGA pins to the program inputs and outputs. Pin assignments can be found in the
Spartan 3E Starter User Guide.
6. Select “Synthesis/Implementation” from the pull down menu on the Sources window. In
the Processes window expand the “User Constraints” toolbox and double click on “Assign
Package Pins”. Choose “Yes” when Project Navigator asks to add a UCF file to the project.
7. After the Xilinx PACE program starts, resize the Design Object List - I/O Pins window until
the Termination column is visible. Enter the pin assignments in the Loc column. The Spartan
board’s buttons need the “PULLDOWN” constraint to function properly. Enter
“PULLDOWN” in the Termination column for ‘clk’ and ‘reset’
8. Click the save button when the pins assignments have been entered. When the Bus Delimiter
window comes up, make sure “XST Default: <>” is selected and press “OK”. Close the
Xilinx PACE program
9. Plug the Spartan 3E board into your computer and turn the board’s power on. Expand the
“Generate Programming File” process in the Processes window and double click “Configure
Device (iMPACT).
10. All the processes will run (this may take a minute or two). Ignore the warning on the
“Implement Design” process. When all the processes have finished, iMPACT will start.
Select the top radio button and click “Finish”.
11. iMPACT will run a boundary scan that will appear in the ISE workspace. Assign count.bit to
the FPGA (xc3s500e) and bypass the other two devices.
12. Highlight the FPGA icon, right click the white space inside the ISE workspace, and select
“Program…” Click “OK” on the Programming Properties window
13. The Spartan board should now be programmed. lose iMPACT by closing the “Boundary
Scan” in the ISE workspace (do not save changes when prompted).
EXPERIEMENT NO. 1
HDL CODE TO REALIZE ALL THE LOGIC GATES
AIM: Perform the Simulation of all the logic gates written in behavioral and dataflow style in
Verilog using a Test bench. then, Synthesize each one of them using EDA tool.
Block Diagram:
Verilog Code (In different modeling styles):
module nandg_tst_v;
reg a;
reg b;
wire c;
nandg uut (
); initial begin
a = 0; b = 0;
#100 a = 0; b = 1;
#100 a = 1; b = 0;
#100 a = 1; b = 1;
end
endmodule
Simulation Waveform:
AND Gate:
OR Gate:
NAND Gate:
NOR Gate:
XOR Gate:
XNOR Gate:
Result: Thus the OUTPUT‟s of all logic gates are verified by synthesizing and
simulating the VERILOG code
EXPERIEMENT NO. 2
DESIGN OF 8-TO-1 MULTIPLEXER/DEMULTIPLEXER
Aim: To implement Multiplexer & Demultiplexer using Verilog HDL
APPARATUS REQUIRED:
PC with Windows XP
. XILINX 9.2i
FPGA-SPARTAN-3 KIT
PARALLEL TO JTAG CABLE
Block Diagram:
8:1Multiplexer
8:1
Multiplexer
A[7:0] MUX OUT
SEL[2:0]
module mux81_tst_v;
reg [7:0] a;
reg [2:0] s;
wire c;
mux81 uut ( .a(a), .s(s), .c(c) );
initial begin
a = 8'b10100101; s = 3'b000; #100 s = 3'b001; #100 s = 3'b101; #100 s = 3'b101;
#100 s = 3'b111; end
endmodule
Simulation Waveform:
Demultiplexer:
Block diagram:
Truth table:
VERILOG VHDL CODE:
module 1_8_DEMUX(
input i,
input s2, s1, s0,
output [7:0]out
);
reg [7:0]out;
always @ (i or s0 or s1 or s2)
case ({s2,s1,s0})
0: out0 = i;
1: out1 = i;
2: out2 = i;
3: out3 = i;
4: out4 = i;
5: out5 = i;
6: out6 = i;
7: out7 = i;
default: out = 8'bxxxxxxx;
endcase
endmodule
initial begin
// Initialize Inputs
i = 1;s2 = 0;s1 = 0;s0 = 0;
// Wait 100 ns for global reset to finish
#100;
// Add stimulus here
#100; s2=0; s1=0; s0=1;
#100; s2=0; s1=1; s0=0;
#100; s2=0; s1=1; s0=1;
#100; s2=1; s1=0; s0=0;
#100; s2=1; s1=0; s0=1;
#100; s2=1; s1=1; s0=0;
#100; s2=1; s1=1; s0=1;
end
initial begin
#100;
$monitor(“I=%b, s2=%b, s1=%b, s0=%b, out=%b”, I, s2,
s1, s0, out);
end
endmodule
Simulation Waveform:
AIM: Perform Simulation of 1-bit Full adder written in behavioral, dataflow and
structural modeling style in VERILOG HDL using a Test bench. Then, Synthesize
each one of them using EDA tool.
APPARATUS REQUIRED:
PC with Windows XP
. XILINX 9.2i
FPGA-SPARTAN-3 KIT
PARALLEL TO JTAG CABLE
BLOCK DIAGRAM:
module fa_tst_v;
reg a, b cin;
wire sum, cout;
fa uut ( .a(a), .b(b), .cin(cin), .sum(sum), .cout(cout) );
initial
begin a = 0; b = 0; cin = 0; #100
a = 0; b = 0; cin = 1; #100
a = 0; b = 1; cin = 0; #100
a = 0; b = 1; cin = 1; #100
a = 1; b = 0; cin = 0; #100
a = 1; b = 0; cin = 1; #100
a = 1; b = 1; cin = 0; #100
a = 1; b = 1; cin = 1;
End
endmodule
Simulation Waveform:
Full Adder:
APPARATUS REQUIRED:
PC with Windows XP
. XILINX 9.2i
FPGA-SPARTAN-3 KIT
PARALLEL TO JTAG CABLE
Block diagram:
Truth table:
Verilog module:
Verilog code for 8-bit magnitude comparator:
1. module magComp ( In1,
2. In2,
3. Gt,
4. Lt,
5. Eq
6. );
9. output Gt,
10. Lt,
13. Lt,
14. Eq;
15. always @ (In1 or In2) //Check the state of the input lines
16. begin
20. end
21. endmodule
Verilog test bench:
1. timescale 1ns / 1ps
2. module magComp_tb;
3. // Inputs
6. // Outputs
7. wire Gt;
8. wire Lt;
9. wire Eq;
12. .In1(In1),
13. .In2(In2),
14. .Gt(Gt),
15. .Lt(Lt),
16. .Eq(Eq)
17. );
23. #100;
24.
28. #20;
31. #20;
34. #20;
37. #20;
40. #20;
41. end
42. endmodule
simulation waveform:
Aim: Perform Zero Delay Simulation 2:4 Decoder and 8:3 Encoder in Verilog
HDL using a Test bench. Then, Synthesize on two different EDA tools.
APPARATUS REQUIRED:
PC with Windows XP
. XILINX 9.2i
FPGA-SPARTAN-3 KIT
PARALLEL TO JTAG CABLE
2:4 decoder:
Block Diagram:
Truth Table:
A Y
00 0001
01 0010
10 0100
11 1000
VERILOG HDL Code:
Simulation Waveform:
DESIGN OF 8-TO-3 ENCODER (WITHOUT AND WITH PRIORITY)
Block Diagram:
Without priority
module encoder(Z7,Z6,Z5,Z4,Z3,Z2,Z1,Z0,en,A0,A1,A2);
input Z7,Z6,Z5,Z4,Z3,Z2,Z1,Z0,en;
output A0,A1,A2;
wire A0,A1,A3;
assign A0 = Z1 | Z3 | Z5| Z7|en;
assign A1= Z2 |Z3 | Z6 | Z7|en;
assign A2 = Z4 | Z5 | Z6 |Z7|en;
endmodule
With Priority
Module priority_encoder(din,dout);
output[2:0]dout;
input [7:0] din;
always @ (din)
begin
casex (din)
8'b1xxxxxxx : dout=7;
8'b01xxxxxx : dout=6;
8'b001xxxxx : dout=5
8'b0001xxxx : dout=4;
8'b00001xxx : dout=3;
8'b000001xx : dout=2;
8'b0000001x : dout=1;
8'b00000001 : dout=0;
dafault : dout=3'bx;
endcase
endmodule
Result: Thus the OUTPUT‟s of encoder(with and without priority) are verified by
synthesizing and simulating the VERILOG code
Exp no 6
DESIGN OF FLIP FLOP
Aim: Perform Zero Delay Simulation of D flip flop in VERILOG HDL using a
Test bench. Then, Synthesize on EDA tool.
Block Diagram:
D-flip flop:
Simulation Waveform:
Result: Thus the OUTPUT of Flip Flop are verified by synthesizing and simulating
the VERILOG code
EXP NO 6
BLOCK DIAGRAM:
VERILOG HDL Code:
module piso(clk,rst,a,q);
input clk,rst;
input [3:0]a;
output q;
reg q;
reg [3:0]temp;
always@(posedge clk,posedge rst)
begin
if(rst==1'b1)
begin
q<=1'b0;
temp<=a;
end
else
begin
q<=temp[0];
temp <= temp>>1'b1;
end
end
endmodule
VERILOG HDL Test Bench:
Simulation Waveform:
SISO:
SIPO:
PISO:
Result: Thus the OUTPUT‟s of Shift registers are verified by synthesizing and
simulating the VERILOG code.
EXP NO 8
Aim: To write verilog code for synchronous counter circuit and its test bench for
verification, observe the waveform and synthesize the code with technological
library with given Constraints.
Apparatus required:
Cadence
Verilog PROGRAM :
module counter(clk,rst,count);
input clk,rst;
output[2:0] count;
always@(posedge.clk)
begin
if(rst)
count<=3’b000;
else
count<=count+3’b001;
end
endmodule
test bench:
module counter_tb();
reg clk,rst;
wire[3:0]count;
counter counter_ins(clk,rst,count);
initial
begin
clk=0;
forever #5 clk=~clk;
end
initial
begin
end
endmodule
Logic Diagram:
module b2g(B,G);
input [3:0]B;
output reg [3:0]G;
always@(B)
begin
G[3]<=B[3];
G[2]<=B[3]^B[2];
G[1]<=B[2]^B[1];
G[0]<=B[1]^B[0];
end
endmodule
VERILOG HDL Test Bench:
module tb_b2g_v;
reg [3:0] B;
wire [3:0] G;
b2g uut (.B(B), .G(G));
initial
B =4'd0;
always #2 B=B+4'd1;
endmodule
Simulation Waveform
Parity generator
Aim: Perform Simulation of parity generatorwritten in Gate level modeling
style in VERILOG HDL using a Test bench. Then, Synthesize each one of them
using EDA tool.
Apparatus required:
PC with Windows XP
. XILINX 9.2i
FPGA-SPARTAN-3 KIT
PARALLEL TO JTAG CABLE
Block diagram:
A parity bit is used for the purpose of detecting errors during the
transmission of binary information. A parity bit is an extra bit included
with the binary message to make the number of ones either even or odd.
An error is detected if the checked parity doesn’t correspond with the one
transmitted.
Truth table:
The circuit that generates the parity bit is called parity generator. So with
this explanation, let us design an even parity generator. Here is the truth
table of an even parity generator.
As seen from the truth table, in this even parity generator, if the number
of 1’s in the input are odd, the output is 1 making the total numbers of
‘1’ be even. If the number of 1’s in input is even, the output is 0 since
the number of input ‘1’ is already even. By closely observing the truth
table, it can be understood that the output is a mere XOR of input bits.
The three bits in the message, together with the parity bit are transmitted
to their destination, where they are applied to a parity checker circuit to
check for possible errors.
VERILOG CODE:
module parity(
input x,y,z,
output result);
TEST BENCH:
initial begin
// Initialize Inputs
x = 0;
y = 0;
z = 0;
#100;
y = 0;
z = 1;
#100;
x = 0;
y = 1;
z = 0;
#100;
x = 0;
y = 1;
z = 1;
#100;
x = 1;
y = 0;
z = 0;
#100;
x = 1;
y = 0;
z = 1;
#100;
x = 1;
y = 1;
z = 0;
#100;
x = 1;
y = 1;
z = 1;
#100;
end
endmodule
OUTPUT:
Result: Thus the OUTPUT‟s of parity generator are verified by synthesizing and
simulating the VERILOG code
EXPERIEMENT NO. 9
SEQUENCE DETECTOR
Aim: Perform Zero Delay Simulation of Sequence Detector (Moore and Mealy
state machines ) in VERILOG HDL using a Test bench. Then, Synthesize on EDA
tools.
Apparatus required:
PC with Windows XP
. XILINX 9.2i
FPGA-SPARTAN-3 KIT
PARALLEL TO JTAG CABLE
Block diagram:
Moore Machine
end
test2;
end
task test2;
for( i = 0; i <= 15; i = i + 1)
begin
inp = $random % 2;
#2 clk = 1;
#2 clk = 0;
$display("State = ", dut.state, " Input = ", inp, ", Output = ", outp);
end
endtask
endmodule
2'b01: begin
if( inp ) begin
state <= 2'b00;
outp <= 1;
end
else begin
state <= 2'b10;
outp <= 0;
end
end
2'b10: begin
if( inp ) begin
state <= 2'b01;
outp <= 0;
end
else begin
state <= 2'b00;
outp <= 1;
end
end
default: begin
state <= 2'b00;
outp <= 0;
end
endcase
end
end
endmodule
VERILOG HDL TEST BENCH:
module fsm1_test;
reg clk, rst, inp;
wire outp;
reg[15:0] sequence;
integer i;
mealy dut( clk, rst, inp, outp);
initial
begin
clk = 0;
rst = 1;
sequence = 16'b0101_0111_0111_0010;
#5 rst = 0;
end
test2;
end
task test2;
for( i = 0; i <= 15; i = i + 1)
begin
inp = $random % 2;
#2 clk = 1;
#2 clk = 0;
$display("State = ", dut.state, " Input = ", inp, ", Output = ", outp);
end
endtask
endmodule
Simulation Wave form
Result: Thus the OUTPUT‟s of Sequence Detector using Mealy & Moore
Machines are verified by synthesizing and simulating the VERILOG code
EXPERIMENT NO 3