DSDV Lab @vtudeveloper.in
DSDV Lab @vtudeveloper.in
DSDV Lab @vtudeveloper.in
)
Smt. Kamala & Sri. Venkappa M Agadi College of Engineering & Technology
Lakshmeshwar 582116 Dist: Gadag
Department of Electronics and Communication Engineering
Sl. PAGE
TOPIC
No NO
1. Introduction to Lab
2. Experiments
PART-A
PART-B: Use FPGA/CPLD kits for down loading Verilog codes and check the output for interfacing
experiments
Page No
Sl.No Topic
1 Verilog Program to interface a Stepper motor to the FPGA/CPLD and rotate the motor
in the specified direction (by N steps)
2 Verilog programs to interface Switches and LEDs to the FPGA/CPLD and
demonstrate its working
Introduction:
In electronics, a hardware description language or HDL is any language from a class of Computer
languages for formal description of electronic circuits. It can describe the circuit's operation, its design and
organization, and tests to verify its operation by means of simulation
HDLs are standard text-based expressions of the spatial, temporal structure and behavior of electronic
systems. In contrast to a software programming language, HDL syntax, semantics include explicit notations
for expressing time and concurrency, which are the attributes of hardware. Languages whose only characteristic
is to express circuit connectivity between hierarchies of blocks are properly classified as net list languages.
HDLs are used to write executable specifications of some piece of hardware. A simulation program,
designed to implement the underlying semantics of the language statements, coupled with simulating the
progress of time, provides the hardware designer with the ability to model a piece of hardware before
it is created physically. It is this executes ability that gives HDLs the illusion of being programming
languages. Simulators capable of supporting discrete-event and continuous-time (Analog) modelling exist, and
HDLs targeted for each are available.
It is certainly possible to represent hardware semantics using traditional programming languages such
as C++, although to function such programs must be augmented with extensive and unwieldy class libraries.
Primarily, however, software programming languages function as a hardware description language
1. VHDL (VHSICHDL)
2. Verilog
1.VHDL (Very High Speed Integrated Circuit Hardware Description Language) is commonly used as
a design-entry language for field-programmable gate arrays and application-specific integrated circuits in
electronic design automation of digital circuits.
VHDL is a fairly general-purpose language, and it doesn’t require a simulator on which to run the code.
There are a lot of VHDL compilers, which build executable binaries. It can read and write files on the host
computer, so a VHDL program can be written that generates another VHDL program to be incorporated in the
design being developed. Because of this general-purpose nature, it is possible to use VHDL to write a test
bench that verifies with the user, and compares results with those expected. This is similar to the capabilities
of the Verilog language
VHDL is not a case sensitive language. One can design hardware in a VHDL IDE (such as Xilinx or
Quartus) to produce the RTL schematic of the desired circuit. After that, the generated schematic can be
verified using simulation software (such as ModelSim) which shows the waveforms of inputs and outputs
of the circuit after generating the appropriate test bench. To generate an appropriate test bench for a
particular circuit or VHDL code, the inputs have to be defined correctly. For example, for clock input, a
Dept of ECE, SKSVMACET, Lakshmeshwar Page 2
SKSVMA Charitable Trust (Regd.)
Smt. Kamala & Sri. Venkappa M Agadi College of Engineering & Technology
Lakshmeshwar 582116 Dist: Gadag
Department of Electronics and Communication Engineering
Xilinx ISE means Xilinx® Integrated Software Environment (ISE), i.e programmable logic design tool
in electronics industry. This Xilinx ® design software suite allows taking design from design entry through Xilinx
device programming. The ISE Project Navigator manages and processes design through several steps in the ISE
design flow. These steps are Design Entry, Synthesis, Implementation, Simulation/Verification, and Device
Configuration. Xilinx is one of most popular software tool used to synthesize VHDL code.
Steps for HDl programs :
3. In the 2nd window provide the name for the project , click on next.
6. Click on finish.
8. Select Verilog module and provide the file name then click on next.
11. Go to file select the save option and save the program.
Simulation using all the modeling styles and Synthesis of all the logic gates using Verilog HDL
Logic gates:
A logic gate is an idealized or physical device implementing a Boolean function; that is, it performs a
logical operation on one or more binary inputs, and produces a single binary output
AND Gate - Block Diagram: OR Gate Block Diagram NAND Gate - Block Diagram:
AND gate - Truth Table OR gate - Truth Table NAND Gate -Truth Table
A B Y A B Y A B Y
0 0 0 0 0 0 0 0 1
0 1 0 0 1 1 0 1 1
1 0 0 1 0 1 1 0 1
1 1 1 1 1 1 1 1 0
NOR Gate Block Diagram: XOR Gate Block Diagram XNOR Block Diagram:
Output Waveforms:
Verilog Program for NAND Gate: Verilog Program for NOR Gate:
module nandgate (a,b,y); module norgate (a,b,y);
input a,b; input a,b;
output y; output y;
assign y= ~(a&b); assign y=~(a|b);
endmodule endmodule
Output Waveforms:
Verilog Program for XOR Gate: Verilog Program for XNOR Gate:
module xorgate (a,b,y); module xnorgate (a,b,y);
input a,b; input a,b;
output y; output y;
assign y= a^b; assign y=~(a^b);
endmodule endmodule
Output Waveforms:
Output Waveform:
Verilog Program:
Output Waveform:
Expt:No.1:To simplify the given Boolean expressions and realize using Verilog program
Theory: Boolean expression is an expression used in programming languages that produces a Boolean value
when evaluated. A logical statement that results in a Boolean value, either be True or False, is a Boolean
expression. Sometimes, synonyms are used to express the statement such as 'Yes' for 'True' and 'No' for
'False'. Also, 1 and 0 are used for digital circuits for True and False, respectively.
Truth Table:
Inputs Output
A B C D Y
0 0 0 0 0
0 0 0 1 0
0 0 1 0 1
0 0 1 1 0
0 1 0 0 0
0 1 0 1 0
0 1 1 0 1
0 1 1 1 0
1 0 0 0 1
1 0 0 1 1
1 0 1 0 1
1 0 1 1 1
1 1 0 0 0
1 1 0 1 0
1 1 1 0 1
1 1 1 1 0
Verilog Code:
module Booleanexp (a,b,c,d,y);
input a,b,c,d;
output y;
assign y=a&b|c&d;
endmodule
Output Waveform:
a Sum
Half Adder
b Carry
Inputs Outputs
A B Sum Carry
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
Verilog Program:
module halfadder (s, c, a, b);
input a, b;
output s, c;
assign s = a ^ b;
assign c= a & b;
endmodule
Output Waveform:
Full Adder:
Theory: Full Adder is the adder that adds three inputs and produces two outputs. The first two inputs are
A and B and the third input is an input carry as C-IN. The output carry is designated as C-OUT and the
normal output is designated as S which is SUM. The C-OUT is also known as the majority 1’s detector,
whose output goes high when more than one input is high.
a Sum
b Full Adder
Cin Carry
Truth Table:
Inputs Outputs
a b Cin Sum Carry
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
Verilog Program:
module fulladder (s, c, a, b, cin)
input a, b, cin;
output s, c;
assign s=a ^b ^ cin;
assign c=(a &b) | (b & cin) |(cin &a);
endmodule
Output Waveform:
Half Subtractor:
Theory: A half subtractor is a digital logic circuit that performs binary subtraction of two single-bit binary
numbers. It has two inputs, A and B, and two outputs, DIFFERENCE and BORROW. The DIFFERENCE
output is the difference between the two input bits, while the BORROW output indicates whether borrowing
was necessary during the subtraction.
Fig.Block Diagram
Truth Table:
Inputs Outputs
A B Difference Borrow
0 0 0 0
0 1 1 1
1 0 1 0
1 1 0 0
Verilog Code:
module halfsub (a,b,diff,borrow);
input a,b;
output diff,borrow;
assign diff=a^b;
assign borrow=(~a)&&b;
endmodule
Output Waveform:
Full Subtractor:
Theory: A full subtractor is a combinational circuit that performs subtraction of two bits, one is minuend
and other is subtrahend, taking into account borrow of the previous adjacent lower minuend bit. This circuit has
three inputs and two outputs. The three inputs A, B and Bin, denote the minuend, subtrahend, and previous
borrow, respectively.
Fig.Block Diagram
Truth Table:
Verilog code:
module fullsub (a,b,c,diff,borrow);
input a,b,c;
output diff,borrow;
assign diff=(a^b)^c;
assign borrow= ((~a)&b)|(b&c)|((~a)&c);
endmodule
Output Waveform:
Verilog Code:
module alu(a, b, opcode,y);
input [3:0] a;
input [3:0] b;
input [2:0] opcode;
output [3:0] y;
reg [3:0] y;
always @(a, b , opcode)
begin if (en==1)
case (opcode)
3'b000:y=a+b;
3'b001:y=a-b;
3'b010:y=~a;
3'b011:y=a*b;
3'b100:y= a&b;
3'b101:y=a|b;
3'b110:y=~(a&b);
3'b111:y=a^b;
default:begin
end
endcase
end
endmodule
Output Waveform:
Expt.No.4:Aim: To realize the following Code converters using Verilog Behavioral description
a)Gray to binary and vice versa b)Binary to excess3 and vice versa.
The logical circuit which converts binary code to equivalent gray code is known as binary to gray code
converter. The gray code is a non-weighted code. The successive gray code differs in one bit position only
that means it is a unit distance code. It is also referred as cyclic code. It is not suitable for arithmetic operations.
It is the most popular of the unit distance codes. It is also a reflective code. An n-bit Gray code can be obtained
by reflecting an n-1 bit code about an axis after 2n-1 rows, and putting the MSB of 0 above the axis and the
MSB of 1 below the axis. Reflection of Gray codes is shown below. The 4 bits binary to gray code conversion
table is given below,
Block Diagram:
b(0) g(0)
b(1) Binary to Gray g(1)
b(2) g(2)
b(3) g(3)
Truth Table:
Verilog Code:
module bintogray_4_bit (g,b);
input [3:0] b;
input [3:0] g;
assign g[3]=b[3];
assign g[2]=b[3]^b[2];
assign g[1]=b[2]^b[1];
assign g[0]=b[1]^ b[0];
endmodule
Output Waveform:
Theory:
Gray Code system is a binary number system in which every successive pair of numbers differs in only one bit.
It is used in applications in which the normal sequence of binary numbers generated by the hardware may
produce an error or ambiguity during the transition from one number to the next
Block Diagram:
Truth Table:
Verilog Code:
module gtob (input [3:0] g, output [3:0] b);
assign b[3] = g[3];
assign b[2] = g[3]^g[2];
assign b[1] = g[2]^g[1];
assign b[0] = g[1]^g[0];
endmodule
Output Waveform:
Verilog Code:
module bcd_ex3_Dataflow(
input a,b,c,d
output w,x,y,z);
assign w = (a | (b & c) | (b & d));
assign x = (((~b) & c) | ((~b) & d) | (b & (~c) & (~d)));
assign y = ((c & d) | ((~c) & (~d)));
assign z = ~d;
endmodule
Output Waveform:
Verilog Code:
module ex3_to_bcd(
input w,x,y,z
output a,b,c,d);
assign a = ((w & x) | (w & y & z));
assign b = (((~x) & (~y)) | ((~x) & (~z)) | (x & y & z));
assign c = (((~y) & z) | (y & (~z)));
assign d = ~z;
endmodule
Output Waveform:
Result: Verified and observe the output of Gray to Binary and Binary to
Gray and Binary to Excess-3 Code converters.
The truth table for an 8-to1 multiplexer is given below with eight combinations of inputs to
generate each output corresponds to input.
I(0)
I(1)
I(2)
I(3) Y
I(4) 8:1 MUX
I(5)
I(6)
I(7)
For example, if S2= 0, S1=1 and S0=0 then the data output Y is equal to I2. Similarly the data outputs i0
to i7 will be selected through the combinations of S2, S1 and S0 as shown in above figure.
Output Waveform:
THEORY:
An encoder is a combinational logic circuit that essentially performs a“ reverse” of decoder functions. An
encoder has 2^N input lines and N output lines. In encoder the output lines generate the binary code
corresponding to input value. An encoder accepts an active level on one of its inputs, representing digit, such
as a decimal or octal digits, and converts it to a coded output such as BCD or binary. Encoders can also be
devised to encode various symbols and alphabetic characters. The process of converting from familiar symbols
or numbers to a coded format is called encoding. An encoder has a number of input lines, only one of which
input is activated at a given time and produces an N-bit output code, depending on which input is activated.
For an 8-to-3 binary encoder with inputs I0-I7 the logic expressions of the outputs Y0-Y2
are: Y0 = I1 + I3 + I5 + I7
Y1= I2 + I3 + I6 +
I7 Y2 = I4 + I5 +
I6 +I7
Block diagram and Truth Table:
En I(7) I(6) I(5) I(4) I(3) I(2) I(1) I(0) Y(2) Y(1) Y(0)
0 x x x x x x x x 0 0 0
1 0 0 0 0 0 0 0 1 0 0 0
1 0 0 0 0 0 0 1 0 0 0 1
1 0 0 0 0 0 1 0 0 0 1 0
1 0 0 0 0 1 0 0 0 0 1 1
1 0 0 0 1 0 0 0 0 1 0 0
1 0 0 1 0 0 0 0 0 1 0 1
1 0 1 0 0 0 0 0 0 1 1 0
1 1 0 0 0 0 0 0 0 1 1 1
Fig.b.Truth Table
Verilog Code:
module encoder( input [7:0] a, input en, output reg [2:0] y);
always@(en,a)
if(en==1)
case(a)
8'b10000000 : y = 3'b111;
8'b01000000 : y = 3'b110;
8'b00100000 : y = 3'b101;
8'b00010000 : y = 3'b100;
8'b00001000 : y = 3'b011;
8'b00000100 : y = 3'b010;
8'b00000010 : y = 3'b001;
8'b00000001 : y = 3'b000;
default : y = 3'bxxx;
endcase
else
y = 3'bzzz;
endmodule
Output Waveform:
Fig.a.Block Diagram
En I(7) I(6) I(5) I(4) I(3) I(2) I(1) I(0) Y(2) Y(1) Y(0)
0 x x x x x x x x 0 0 0
1 x x x x x x x 1 0 0 0
1 x x x x x x 1 0 0 0 1
1 X x x x x 1 0 0 0 1 0
1 x x x x 1 0 0 0 0 1 1
1 x x x 1 0 0 0 0 1 0 0
1 x x 1 0 0 0 0 0 1 0 1
1 x 1 0 0 0 0 0 0 1 1 0
1 1 0 0 0 0 0 0 0 1 1 1
Fig.b.Truth Table
Verilog Code:
module encwithpriority(input [7:0] a,
input en,
output reg [2:0] y );
always@(en,a)
if(en==1)
casex(a)
8'b1xxxxxxx : y = 3'b111;
8'b01xxxxxx : y = 3'b110;
8'b001xxxxx : y = 3'b101;
8'b0001xxxx : y = 3'b100;
8'b00001xxx : y = 3'b011;
8'b000001xx : y = 3'b010;
8'b0000001x : y = 3'b001;
8'b00000001 : y = 3'b000;
default : y = 3'bxxx;
endcase
else
y = 3'bzzz;
endmodule
Output Waveform:
Result: Verified the output of 8:1 MUX,8:3 Encoder and Priority encoder.
Expt.N0.6
Aim:To realize using behavioral description 1:8 Demux,3:8 Decoder, 2-bit Comparator
1:8 Demux
Theory: A 1 to 8 demultiplexer consists of one input line, 8 output lines and 3 select lines. Let the
input be D, S1 and S2 are two select lines and eight outputs from Y0 to Y7. It is also called as 3 to 8
demux because of the 3 selection lines. Below is the block diagram of 1 to 8 demux.
Fig.Block Diagram
Truth Table:
Verilog Code:
module demux(y,s,d);
input d;
input [2:0]s;
output reg [7:0]y;
always @(d,s)
begin
case(s)
3'b000: y[0]=d;
3'b001: y[1]=d;
3'b010: y[2]=d;
3'b011: y[3]=d;
3'b100: y[4]=d;
3'b101: y[5]=d;
3'b110: y[6]=d;
3'b111: y[7]=d;
endcase
end
endmodule
Output Waveform:
3:8 Decoder
Theory: Decoder is a combinational logic circuit that is used to change the code into a set of signals. It is
the reverse process of an encoder. A decoder circuit takes multiple inputs and gives multiple outputs. A decoder
circuit takes binary data of ‘n’ inputs into ‘2^n’ unique output. In addition to input pins, the decoder has a enable
pin. This enables the pin when negated, to make the circuit inactive. in this article, we discuss 3 to 8 line Decoder
and demultiplexer.
A B C D0 D1 D2 D3 D4 D5 D6 D7
0 0 0 1 0 0 0 0 0 0 0
0 0 1 0 1 0 0 0 0 0 0
0 1 0 0 0 1 0 0 0 0 0
0 1 1 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 1 0 0 0
1 0 1 0 0 0 0 0 1 0 0
1 1 0 0 0 0 0 0 0 1 0
1 1 1 0 0 0 0 0 0 0 1
Verilog Code:
module decoder3_to_8(y,d);
input [2:0] d;
output reg[7:0] y;
always @(d)
begin
y=0;
case (d)
3'b000: y[0]=1'b1;
3'b001: y[1]=1'b1;
3'b010: y[2]=1'b1;
3'b011: y[3]=1'b1;
3'b100: y[4]=1'b1;
3'b101: y[5]=1'b1;
3'b110: y[6]=1'b1;
3'b111: y[7]=1'b1;
default: y=8'b0;
endcase
end
endmodule
Output Waveform:
2-Bit Comparator:
Theory: 2-bit Comparator is a combinational circuit used to compare two binary number consiting of two
bits. When two binary numbers A & B are compared the output can be any of these three cases i.e. A >
B, A = B and A < B.
Verilog Code:
module twobit(agb,aeb,alb,a,b);
input [1:0] a,b;
output reg agb,aeb,alb;
always @(a or b)
begin
agb=0;
aeb=0;
alb=0;
if(a==b)
aeb=1;
else if(a>b)
agb=1;
else
alb=1;
end
endmodule
Output Waveform:
Result: Verified and observe the output of 1:8 Demux,3:8 Decoder and 2-Bit Comparator.
Expt.No.7
Aim: Write Verilog code for SR, D and JK and verify the flip flop.
Theory: flip-flop (FF) is predominately controlled by a clock and its output state is determined by its
excitation input signals. Note that if the clock is replaced by a gated control signal, the flip-flop
becomes a gated latch.
SR Flipflop:
The major problem of RS latch is its susceptibility to voltage noise which could change the output
states of the FF. With the clocked RS FF, the problem is remedied. With the clock held low, [S] & [R]
held low, the output remains unchanged. With the clock held high, the output follows R & S.
set
S Q
clk
SR Flipflop
R
Qbar
clr
Fig. a. Block Diagram
Truth Table:
Inputs Outputs
clk Clr Set S R Q Qbar
X X X X NC
1 X X X 0 1
0 1 X X 1 0
0 0 0 0 NC
0 0 0 1 0 1
0 0 1 0 1 0
0 0 1 1 Not Defined
Verilog Code:
module srff (q,qbar,set,clr,s,r,clk);
input set,clr,s,r,clk;
output q,qbar;
reg q,qbar;
always @ (posedge clk,posedge set,posedge clr)
begin
if (clr)
q=1’b0;
else if (set)
q=1’b1;
else
begin
case({s,r)}
2’b00:q=q;
2’b01:q=0;
2’b10:q=1;
2’b11:q=1’bz;
endcase
end
qbar=~q;
end
endmodule
Output Waveform:
D Flip-Flop:
The D-type FF remedies the indeterminate state problem that exists when both inputs to a clocked
RSFF are high. The schematic is identical to a RS FF except that an inverter is used to produce a pair
of complementary input.
D Q
D Flip-Flop
Clk Qbar
Truth Table:
Clk D Q Qbar
0 0 No Change No Change
0 0 1
1 1 0
Verilog Code:
module dff (q,qbar,set,clr,d,clk);
input set,clr,d,clk;
output q,qbar;
reg q,qbar;
always @ (posedge clk,posedge set,posedge clr)
begin
if (clr)
q=1’b0;
else if (set)
q=1’b1;
else
begin
case({d)}
2’b00:q=q;
2’b01:q=0;
2’b10:q=1;
2’b11:q=1’bz;
endcase
end
qbar=~q;
end
endmodule
Output Waveform:
JK Flip-Flop:
The JK flip flop is basically a gated SR flip-flop with the addition of a clock input circuitry that prevents the illegal or
invalid output condition that can occur when both inputs S and R are equal to logic level “1”. Due to this additional clocked
input, a JK flip-flop has four possible input combinations, “logic 1”, “logic 0”, “no change” and “toggle”. The symbol for
a JK flip flop is similar to that of an SR Bistable Latch as seen in the previous tutorial except for the addition of a clock
input.
Verilog Code:
module jkff (q,qbar,set,clr,j,k,clk);
input set,clr,j,k,clk;
output q,qbar;
reg q,qbar;
always @ (posedge clk,posedge set,posedge clr)
begin
if (clr)
q=1’b0;
else if (set)
q=1’b1;
else
begin
case({j,k)}
2’b00:q=q;
2’b01:q=0;
2’b10:q=1;
2’b11:q=1’bz;
endcase
end
qbar=~q;
end
endmodule
Output Waveform:
Aim: To realize counters Up/Down (BCD and Binary) using Verilog behavioral description.
Apparatus required:- Electronics Design Automation Tools used:-Xilinx ISE 14.7 Simulator tool
Theory:
The counters which use clock signal to change their transition are called “Synchronous counters”. This
means the synchronous counters depends on the clock input to changes values. In synchronous counters, all
flip flops are connected to the same clock signal and all flip flops will trigger at the same time. Synchronous
counters are also known as ‘Simultaneous counters ’.There is no propagation delay and no ripple effect in
synchronous counters.
Logic Symbol:
Clk
BCD Counter q
Clr
Truth Table:
Clk Clr Q
1 0000
0 0001
0 0010
0 0011
0 0100
0 0101
0 0110
0 0111
0 1000
0 1001
Verilog Code:
BCD Up counter
module bcdcounter(bcddigit,clk,rst);
input clk, rst;
output reg [3:0] bcddigit=0;
always @(posedge clk)
begin
if((rst==1)|(bcddigit==4'b1001))
begin
bcddigit=4'b0000;
end
else
bcddigit=bcddigit+1;
end
endmodule
Output Waveform:
Output Waveform:
Binary UP Counter:
module binarycounter1(binarydigit,clk,rst);
input clk, rst;
output reg [3:0] binarydigit=4'b0000;
always @(posedge clk)
begin
if((rst==1)|(binarydigit==4'b1111))
begin
binarydigit=4'b0000;
end
else
binarydigit=binarydigit+1;
end
endmodule
Output Waveforms:
module binarycounter1(binarydigit,clk,rst);
input clk, rst;
output reg [3:0] binarydigit=4'b1111;
always @(posedge clk)
begin
if((rst==1)|(binarydigit==4'b0000))
begin
binarydigit=4'b1111;
end
else
binarydigit=binarydigit-1;
end
endmodule
Output Waveform: