DSD Lab
DSD Lab
DSD Lab
2.1.Introduction
Creating a New Project 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.
Figure 2.1: Xilinx Project Navigator window (snapshot from Xilinx ISE software)
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) Leave
the top level module type as HDL. Example: If the project name were “o_gate”, enter
“o_gate” as the project name and then click “Next”.
Figure 2.2: New Project Initiation window (snapshot from Xilinx ISE software)
Clicking on NEXT should bring up the following window:
Figure 2.3: Device and Design Flow of Project (snapshot from Xilinx ISE software)
For each of the properties given below, click on the ‘value’ area and select from the list of
values that appear.
Device Family: Family of the FPGA/CPLD used. In this laboratory we will be using
the Spartan3 EFPGA’s.
Device: The number of the actual device. For this lab you may enter XC3S250E (this
can be found on the attached prototyping board)
Package: The type of package with the number of pins. The Spartan FPGA used in this
lab is packaged in CP132package.
Speed Grade: The Speed grade is“-4”.
Synthesis Tool: XST[VHDL/Verilog]
Simulator: The tool used to simulate and verify the functionality of the design.
Model sim simulator is integrated in the Xilinx ISE. Hence choose “Model sim-
XE Verilog” as the simulator or even Xilinx ISE Simulator can be used.
Then click on NEXT to save the entries.
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. In order to open an existing project in Xilinx Tools, select File-
>Open Project to show the list of projects on the machine. Choose the project you want
and click OK. Clicking on NEXT on the above window brings up the following window:
Figure 2.4: Create new source window (snapshot from Xilinx ISE software)
If creating a new source file, Click on the NEW SOURCE.
2.1.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 A window pops up as shown in Figure 2. 4. (Note: “Add to
project” option is selected by default. If you do not select it then you will have to add the
new source file to the project manually).
Figure 2.5: Creating Verilog-HDL source file (snapshot from Xilinx ISE software)
Select Verilog Module and in the “File Name:” area, enter the name of the Verilog source
file you are going to create. Also make sure that the option Add to project is selected so
that the source need not be added to the project again. Then click on Next to accept the
entries. This pops up the following window (Figure 2.5). In the Port Name column, enter
the names of all input and output pins and specify the Direction accordingly.
A Vector/Bus can be defined by entering appropriate bit numbers in the
MSB/LSB columns.
Then click on Next>to get a window showing all the new source
information (Figure 2.6). If any changes are to be made, just click on Next > Next >
Finish to continue.
Figure 2.6: Define Verilog Source window (snapshot from Xilinx ISE software)
Figure 2.7: New Project Information window(snapshot from Xilinx ISE software)
Once you click on Finish, the source file will be displayed in the sources window
in the Project Navigator (Figure 1). 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.
Figure 2.8: Verilog Source code editor window in the Project Navigator (from Xilinx ISE
software)
2.2. Functional Simulation of Combinational Designs
2.2.1. Adding the test vectors :
To check the functionality of a design, we have to apply test vectors and
simulate the circuit. In order to apply test vectors, a test bench file is written. Essentially
it will supply all the inputs to the module designed and will check the outputs of the
module.
Example: For the 2 input OR Gate, the steps to generate the test bench are as follows: 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 2.9.
Figure 2.9: Adding test vectors to the design (snapshot from Xilinx ISE software)
Click on ‘Next’ to proceed. In the next window select the source file with which you want
to associate the test bench.
Figure 2.10: Associating a module to a testbench (snapshot from Xilinx ISE software)
Click on Next to proceed. In the next window click on Finish.
You will now be provided with a template for your test bench. If it does not open
automatically click the radio button next to Simulation .
Figure 2.16: Realized logic by the XilinxISE for the verilog code
EXPERIMENT 1
REALIZATION OF A BOOLEAN FUNCTION
OBJECTIVE
Design and simulate the HDL code to realize three and four variable
Boolean functions
RESOURCES
PC installed with Xilinx tool
PROGRAM LOGIC
A multi variable Boolean function can be implemented through Verilog HDL in
two ways. First one is using primitive gates and the second one is using assign statements.
Gate primitives are predefined in Verilog, which are ready to use. They are instantiated
like modules. There are two classes of gate primitives: Multiple input gate primitives and
Single input gate primitives. Multiple input gate primitives include and, nand, or, nor,
xor, and xnor. These can have multiple inputs and a single output. Single input gate
primitives include not, buf, notif1, bufif1, notif0, and bufif0. These have a single input
and one or more outputs. Assign statements are used to define signal values as Boolean
expressions. In the example: ' out AS BS , out is defined by the function ' AS BS ,
but must be written in Verilog using the AND operator ( “&” ), OR operator (“|”), the
XOR operator (“^”) and the NOT operator (“~”). It is important to remember that an
assignment statement is identical to the corresponding schematic with gates wired to the
inputs and outputs to define the Boolean function. In fact, assign statements are known as
“continuous assignments” because, unlike assignment statements in a regular
programming language, they are executed continuously, just like the corresponding gates
in a schematic. 1.4.
PROCEDURE
1. Create a module with required number of variables and mention it’s input/output.
2. Write the description of given Boolean function using operators or by using the built in
primitive gates.
3. Create another module referred as test bench to verify the functionality.
4. Follow the steps required to simulate the design and compare the obtained output with
the corresponding truth table.
PROGRAM CODE
module p1(c,a,b);
input a;
input b;
output [0:6] c;
assign c[0]= a & b;
assign c[1]= a | b;
assign c[2]= ~(a & b);
assign c[3]= ~(a | b);
assign c[4]= a ^ b;
assign c[5]= ~(a ^ b);
assign c[6]= ~ a;
endmodule
//boolean function
module p1(a,b,c,f1,f2);
input a,b,c;
output f1,f2;
assign f1=(~a)&b|a&(~b)&(~c)|a&(~b)&c;
assign f2=((~a)|b)&(a|b|(~c))&(a|(~b)|c);
endmodule
EXPERIMENT 2
FULL ADDER AND FULL SUBTRACTOR DESIGN MODELING
OBJECTIVE
To write a HDL code to describe the functions of a full Adder and subtractor.
RESOURCES
PC installed with Xilinx tool 5.3.
PROGRAM LOGIC
A full adder consists of 3 inputs and 2 outputs. Fig 2.1 shows truth table of full adder.
Use “assign” keyword to represent design in dataflow style. The output signal expressions
can be obtained from the truth table using K-maps.
//full subtractor
module p11(a ,b ,c ,diff ,borrow );
output diff, borrow ;
input a,b,c;
assign diff = a ^ b ^ c;
assign borrow = ((~a) & b) | (b & c) | (c & (~a));
endmodule
Full Adder
Full Subtractor
EXPERIMENT 3
DESIGN OF 8-BIT ARITHMETIC LOGIC UNIT
OBJECTIVE
To design a model to implement 8-bit ALU functionality
RESOURCES
PC installed with Xilinx tool
PROGRAM LOGIC
An arithmetic logic unit (ALU) is a combinational digital electronic circuit
that performs arithmetic and bitwise operations on integer binary numbers. This is in contrast
to a floating-point unit (FPU), which operates on floating point numbers. An ALU is a
fundamental building block of many types of computing circuits, including the central
processing unit (CPU) of computers, FPUs, and graphics processing units (GPUs). A single
CPU, FPU or GPU may contain multiple ALUs
CODE
//8 bit ALU
module p13(z,a,b,sel);
input [7:0]a,b;
input [3:0]sel;
output [7:0]z;
reg [7:0]z;
always@(sel,a,b)
begin
case(sel)
4'b0000: z=a+b;
4'b0001: z=a-b;
4'b0010: z=b-1;
4'b0011: z=a*b;
4'b0100: z=a&&b;
4'b0101: z=a||b;
4'b0110: z=!a;
4'b0111: z=~a;
4'b1000: z=a&b;
4'b1001: z=a|b;
4'b1010: z=a^b;
4'b1011: z=a<<1 ;
4'b1100: z=a>>1;
4'b1101: z=a+1;
4'b1110: z=a-1;
end case
end
end module
EXPERIMENT 4
DESIGN OF CODE CONVERTERS
OBJECTIVE
To Design and simulate the HDL code for the following combinational circuits
a. 4 - Bit binary to gray code converter
b. 4 - Bit gray to binary code converter
RESOURCES
PC installed with Xilinx tool
PROGRAM LOGIC
Binary to gray code converter logic
This conversion method strongly follows the EX-OR gate operation between binary bits. The
steps to perform binary to grey code conversion are given below.
a. To convert binary to grey code, bring down the most significant digit of the given binary
number, because, the first digit or most significant digit of the grey code number is same as
the binary number. b. To obtain the successive grey coded bits to produce the equivalent grey
coded number for the given binary, add the first bit or the most significant digit of binary to
the second one and write down the result next to the first bit of grey code, add the second
binary bit to third one and write down the result next to the second bit of grey code, follow
this operation until the last binary bit and write down the results based on EX-OR logic to
produce the equivalent grey coded binary. Gray to binary code converter logic This
conversion method also follows the EX-OR gate operation between grey & binary bits. The
steps to perform grey code to binary conversion are given below. a. To convert grey code to
binary, bring down the most significant digit of the given grey code number, because, the first
digit or the most significant digit of the grey code number is same as the binary number
b. To obtain the successive second binary bit, perform the EX-OR operation between the first
bit or most significant digit of binary to the second bit of the given grey code. c. To obtain the
successive third binary bit, perform the EX-OR operation between the second bit or most
significant digit of binary to the third MSD (most significant digit) of grey code and so on for
the next successive binary bits conversion to find the equivalent.
PROCEDURE
1. Create a module with required number of variables and mention it’s input/output.
2. Write the description of the code converter using data flow model or gate level model.
3. Create another module referred as test bench to verify the functionality.
4. Follow the steps required to simulate the design and compare the obtained output with the
required one.
CODE
// binary to gray code converter
module p7(b,g);
input [3:0] b;
output [3:0] g;
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
end module
//gray to binary converter
module p8(g,b);
input [3:0] g;
output [3:0] b;
reg [3:0] b;
always@(g)
begin
b[3]=g[3];
b[2]=b[3]^g[2];
b[1]=b[2]^g[1];
b[0]=b[1]^g[0];
end
end module
EXPERIMENT 6
1:8 DEMUX , 3:8 DECODER , 2 BIT COMPARATOR
OBJECTIVE
To write HDL codes for SR, JK, D, T flip flops and verify its functionality.
RESOURCES
PC installed with Xilinx tool
PROGRAM LOGIC
De-multiplexer
A De-multiplexer is a combinational circuit that has only 1 input line and 2 N output lines.
Simply, the multiplexer is a single-input and multi-output combinational circuit. The
information is received from the single input lines and directed to the output line. On the
basis of the values of the selection lines, the input will be connected to one of these outputs.
De-multiplexer is opposite to the multiplexer.
Unlike encoder and decoder, there are n selection lines and 2 n outputs. So, there is a total of
2n possible combinations of inputs. De-multiplexer is also treated as De-mux.
1×2 De-multiplexer:
In the 1 to 2 De-multiplexer, there are only two outputs, i.e., Y 0, and Y1, 1 selection lines, i.e.,
S0, and single input, i.e., A. On the basis of the selection value, the input will be connected to
one of the outputs. The block diagram and the truth table of the 1×2 multiplexer are given
below.
The logical expression of the term Y is as follows:
Y0=S0'.A
Y1=S0.A
//JK flipflop
module p15(j,k,clk,q,qb);
input j,k,clk;
output q,qb;
reg q,qb;
reg [1:0]jk;
wire qp=1'b0;
always@(posedge clk)
begin jk={j,k};
begin
case (jk)
2'd0:q=qp;
2'd1:q=1'b0;
2'd2:q=1'b1;
2'd3:q=~q;
end case
end
qb=~q;
end
endmodule
//D flipflop
module p16(q,din,clk);
output q;
reg q;
input din ;
wire din ;
input clk ;
always @ (posedge (clk))
begin
q = din ;
end
end module
//T flipflop
module p17(q,t,clk);
output q;
reg q;
input t ;
input clk ;
always @ (posedge (clk))
begin
q = ~t;
end
end module
T FF
S R FF
JK FF
EXPERIMENT 6
DESIGN OF COUNTERS
OBJECTIVE
To write HDL codes for the following counters :
a. Binary counter
b. BCD counter (Synchronous reset and asynchronous reset)
RESOURCES
PC installed with Xilinx tool
PROGRAM LOGIC
Counter is a sequential circuit. A digital circuit which is used for counting
pulses is known as counter. Counter is the widest application of flip-flops. It is a group of
flipflops with a clock signal applied. Counters are of two types.
Asynchronous or ripple counters.
Synchronous counters.
Asynchronous counters are called as ripple counters, the first flip-flop is
clocked by the external clock pulse and then each successive flip-flop is clocked by the
output of the preceding flip-flop. The term asynchronous refers to events that do not have a
fixed time relationship with each other. An asynchronous counter is one in which the flip-
flops within the counter do not change states at exactly the same time because they do not
have a common clock pulse In synchronous counters, the clock inputs of all the flip-flops are
connected together and are triggered by the input pulses. Thus, all the flip-flops change state
simultaneously (in parallel).
A counter is a register capable of counting the number of clock pulses
arriving at its clock input. Count represents the number clock pulses arrived. A specified
sequence of states appears as the counter output. The name counter is generally used for
clocked sequential circuit whose state diagram contains a single cycle. The modulus of a
counter is the number of states in the cycle. A counter with m states is called a modulo-m
counter or divide-by-m counter.
A counter with a non-power-of-2 modulus has extra states that are not used
in normal operation. There are two types of counters, synchronous and asynchronous. In
synchronous counter, the common clock is connected to all the flip-flops and thus they are
clocked simultaneously.
Fig. 6.1 General structure of a counter’s state diagram – a single cycle
Asynchronous Decade Counters
The modulus is the number of unique states through which the counter will sequence. The
maximum possible number of states of a counter is 2nwhere n is the number of flip-flops.
Counters can be designed to have a number of states in their sequence that is less than the
maximum of 2n . This type of sequence is called a truncated sequence. One common
modulus for counters with truncated sequences is 10 (Modules10). A decade counter with a
count sequence of zero (0000) through 9 (1001) is a BCD decade counter because its 10-state
sequence produces the BCD code. To obtain a truncated sequence, it is necessary to force the
counter to recycle before going through all of its possible states. A decade counter requires
4flip-flops. One way to make the counter recycle after the count of 9 (1001) is to decode
count 10 (1010) with a NAND gate and connect the output of the NAND gate to the clear
(CLR) inputs of the flip-flops, as shown in Figure 6.1
CODE
// binary counter
module p18(clk,count );
output [3:0] count ;
reg [3:0] count ;
input clk ;
wire clk ;
initial count = 0;
always @ (posedge (clk))
begin
count <= count + 1;
end
end module
//BCD counter
module p19(clk ,reset ,dout );
output [3:0] dout ;
reg [3:0] dout ;
input clk ;
wire clk ;
input reset ;
wire reset ;
initial
dout = 0 ;
always @ (posedge (clk))
begin
if (reset)
dout <= 0;
else if (dout<=9)
begin
dout <= dout + 1;
end
else if (dout==9)
begin
dout <= 0;
end
end
end module