Professional Documents
Culture Documents
New DSD Manual Rvitm (4-7)
New DSD Manual Rvitm (4-7)
RashtreeyaSikshanaSamithi Trust
RV Institute of Technology and Management®
(Affiliated to VTU, Belagavi)
JP Nagar, Bengaluru – 560076
Laboratory Experiments
PART A : Programming
1 To simplify the given Boolean expressions and realize using Verilog program.
2 To realize Adder/Subtractor (Full/half) circuits using Verilog data flow description.
3 To realize 4-bit ALU using Verilog program.
4 To realize the following Code converters using Verilog Behavioral description a) Gray to
binary and vice versa b) Binary to excess3 and vice versa
5 To realize using Verilog Behavioral description: 8:1 mux, 8:3 encoder, Priority encoder
6 To realize using Verilog Behavioral description: 1:8 Demux, 3:8 decoder, 2-bit Comparator
7 To realize using Verilog Behavioral description: Flip-flops: a) JK type b) SR type c) D type
8 To realize Counters - up/down (BCD and binary) using Verilog Behavioral description.
Demonstration Experiments
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 a Relay or ADC to the FPGA/CPLD and demonstrate its
working.
3 Verilog programs to interface DAC to the FPGA/CPLD for Waveform generation.
4 Verilog programs to interface Switches and LEDs to the FPGA/CPLD and demonstrate its
working
General description
VFPGA-F-2.0 provides a hardware platform for developing embedded system using
SPARTAN6 XC6SLX4 device.
These features make VFPGA-F-2.0 board ideal for instrumentation, communication and
other demanding application areas where flexibility and in-circuit hardware upgradeability
is of paramount important.
VFPGA-2.0 board comes with complete drivers for windows98/Me/2000/XP/2007. 8051
code examples and windows DLL interface which can be used to interface it to most
common programming language. As it comes with complete with all XILINX ISE14.7
development software required and example code.it is also ideal as classroom training too
for colleges and universities as well as engineers wanting to learn more about hardware
development using SPARTAN6.
Key Switch
EEPROM
7segment Display
Flash Programmer
To start the ISE software, double-click the ISE Project Navigator icon on your desktop, or select
Start > All Programs > Xilinx ISE Design Suite 12.1 > ISE Design Tools > Project Navigator.
To create a new project using the New Project Wizard, do the following:
1. From Project Navigator, select File > New Project.
2. In the Location field, browse to D:\vhdl ex\test\and.vhd or to the directory in which you
installed the project.
3. In the Name field, enter and.vhdor file name you want to creat.
4. Verify that HDL is selected as the Top-Level Source Type, and click Next.
Select the following values in the New Project Wizard—Device Properties page:
Product Category: All
Family: Spartan6
Device: XC6SLX4
Package: TQG144
Speed: -3
Synthesis Tool: XST (VHDL/Verilog)
Simulator: ISim (VHDL/Verilog)
Preferred Language: VHDL or Verilog depending on preference. This will determine the
default language for all processes that generate HDL files.
Other properties can be left at their default values.
3. In the File Name field, enter file name for example and.
4. Click Next.
5. In the Define Module page, enter two input ports named a and b and an output Port named
cfor the debounce component as follows:
a. In the first three Port Name fields, enter sig_in, clkand sig_out.
b. Set the Direction field to input for a andb and to output for c.
7. Click Finish to open the empty HDL file in the ISE Text Editor.Following is an example
VHDL file.
8. Now double click on Synthesize-xst in the process pane and check for syntax errors.if any
errors are there correct them and resynthesize.
Fig.10Synthesis-XST page
9. Now expand the user constraints button and double click on I/O Pin planning(Plan Ahead)-
post synthesis.
10. Now ISE project Navigator window will open and ask for opening plan ahead. Click on yes
13. Now select the proper input and output pin no‘s and save the changes.
14. Now close the Plan Ahead window and Double click on Generate Programming file. Now
the ISE project Navigator will generate and.bit file that will be used for downloading to FPGA
trainer kit.
Experiment 1
Simplify the given Boolean expressions and realize using Verilog program
Aim:To simplify the given Boolean expressions and realize using Verilog program.
Theory:
A Boolean function is a special kind of mathematical function f:Xn→Xf: Xn→X of degree n,
where X={0,1}X={0,1} is a Boolean domain and n is a non-negative integer. It describes the way
how to derive Boolean output from Boolean inputs. A Boolean expression always produces a
Boolean value. A Boolean expression is composed of a combination of the Boolean constants (True
or False), Boolean variables and logical connectives. Each Boolean expression represents a Boolean
function.
1 - 0001
3 - 0011
4 - 0100
5 - 0101
10 - 1010
12 - 1100
13 – 1101
Verilog Code
Verilog Code
Expected Waveform
Experiment 2
Adder (Full/half) circuits using Verilog data flow description
Aim: To Realize Adder (Full/half) circuits using Verilog data flow description
Theory:
Half adder is the simplest of all adder circuits. Half adder is a combinational arithmetic circuit that
adds two numbers and produces a sum bit (s) and carry bit (c) both as output. The addition of 2 bits
is done using a combination circuit called a Half adder. The input variables are augend and addend
bits and output variables are sum & carry bits. A and B are the two input bits.
Full adders hold the ability to add a carry bit which is the resulting from the previous addition
With full adder, crucial circuits such as adder, multiplexer, and many others can be
implemented
The full adder circuits consume minimal power
The advantages of a full adder over a half adder are, a full adder is used to overcome the
drawback of a half adder because; half adder is mainly used to add two 1-bit numbers. Half
adders don‘t add the carry bit, so to overcome this full adder is employed. In Full adder, the
addition of three bits can be done and generates two outputs.
Designing of adders is simple and it is a basic building block so that one-bit addition can be
easily understood.
This adder can be converted to half subtractor by adding an inverter.
By using a full adder, high output can be obtained.
High speed
Very strong to supply voltage scaling
A full adder can be used as a part of many other larger circuits like
Ripple carry adder, it adds n-bits at a time When connected with other full adders, say n
number of full adders, it can be used to create ripple adders of n-bits that perform addition in
series.
carryout Multiplication -the dedicated multiplication circuit uses it.
ALU- Arithmetic Logic Unit (one of the circuit is a full adder).
to generate memory addresses inside a computer and to make the Program Counter point to
next instruction, the ALU makes use of this adder.
For graphics related applications, where there is a very much need of complex computations,
the GPU uses optimized ALU which is made up of full adders, other circuits as well….
Basically, it is used in designing ALU and this ALU is used for wide variety of applications
(from designing CPU to GPU).
Truth Table:
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
Program:
modulehalfaddert_b;
reg a;
reg b;
wire sum;
wire carry;
hauut ( .a(a),.b(b),.sum(sum), .carry(carry));
initial begin
#10 a=1'b0;b=1'b0;
#10 a=1'b0;b=1'b1;
#10 a=1'b1;b=1'b0;
#10 a=1'b1;b=1'b1;
end
endmodule
#10 a=1'b1;b=1'b0;c=1'b0;
#10 a=1'b1;b=1'b0;c=1'b1;
#10 a=1'b1;b=1'b1;c=1'b0;
#10 a=1'b1;b=1'b1;c=1'b1;
#10 $stop;
end
endmodule
Expected Waveform:
Half Adder
Full Adder
Result:Adder (Full/half) circuits are realized using Verilog data flow description
Experiment 3
4-bit ALU using Verilog program.
Theory:
ALU is a main component of the central processing unit, which stands for arithmetic logic unit and
performs arithmetic and logic operations.
o Logical Operations: The logical operations consist of NOR, NOT, AND, NAND, OR,
XOR, and more.
o Bit-Shifting Operations: It is responsible for displacement in the locations of the bits to the
by right or left by a certain number of places that are known as a multiplication operation.
o Arithmetic Operations: Although it performs multiplication and division, this refers to bit
addition and subtraction. But multiplication and division operations are more costly to make.
In the place of multiplication, addition can be used as a substitute and subtraction for
division.
Truth Table:
endcase
ack<=1;
end
endmodule
//Test bench
moduletest_ALU;
// Inputs
reg [31:0] a; reg [31:0] b;
reg en;
reg [2:0] opcode;
// Outputs
wire [31:0] y;
wire [31:0] ack;
// Instantiate the Unit Under Test (UUT)
ALU uut (
.a(a),
.b(b),
.en(en),
.opcode(opcode),
.y(y),
.ack(ack)
);
initial begin en=1'b0;
#10 en=1'b1; a=32'h00000002; b=32'h00000001; opcode=3'b000;
#10 opcode=3'b001;
#10 opcode=3'b010;
#10 opcode=3'b011;
#10 opcode=3'b100;
#10 opcode=3'b101;
#10 opcode=3'b110;
#10 opcode=3'b111;
#10 $finish;
end
endmodule
Expected Waveform:
4 bit ALU
32 bit ALU
Experiment 4
Gray to binary and vice versa
Binary to excess3 and vice versa
Aim: Write a Verilog code for Gray to binary and vice versa b) Binary to excess3 and vice versa using
verilog program
Theory:
0 000 000
1 001 001
2 010 011
3 011 010
4 100 110
5 101 111
6 110 101
7 111 100
The most significant bit (MSB) of the Gray code is always equal to the MSB of the given Binary code.
Other bits of the output Gray code can be obtained by XORing binary code bit at the index and previous
index.
For example, for 3-bit binary number, let Binary digits are b 2 , b1 , b0, where b2 is the most significant bit
(MSB) and b0 is the least significant bit (LSB) of Binary. Gray code digits are g 2 , g1 , g0, where g2 is the
most significant bit (MSB) and g0 is the least significant bit (LSB) of Gray code.
000 000
001 001
010 011
011 010
100 110
101 111
110 101
111 100
Therefore, you solve boolean expression using k-map, you will get g2=b2, g1=b1⊕b2, and g0=b0⊕b1.
Similarly, you can convert n-bit (bnb(n-1)...b2b1b0) Binary number into Gray code (gng(n-1)...g2g1g0). For least
significant bit (LSB) g0=b0⊕b1, g1=b1⊕b2, g2=b1⊕b2 , …. g(n-1)=b(n-1)⊕bn, gn=bn.
Example −Convert Binary number 111010 into Gray code.
So, according above algorithm,
g0=b0⊕b1 = 0⊕1 = 1
g1=b1⊕b2 = 1⊕0 = 1
g2=b2⊕b3 = 0⊕1 = 1
g3=b3⊕b4 = 1⊕1 = 0
g4=b4⊕b5 = 1⊕1 = 0
g5=b5 = 1 = 1
So, Gray will be 100111.
0 000 000
1 001 001
2 011 010
3 010 011
4 110 100
5 111 101
6 101 110
7 100 111
The Most Significant Bit (MSB) of the binary code is always equal to the MSB of the given binary number.
Other bits of the output binary code can be obtained by checking gray code bit at that index. If current gray
code bit is 0, then copy previous binary code bit, else copy invert of previous binary code bit.
For example, for 3-bit binary number, let Binary digits are b 2 , b1 , b0, where b2 is the most significant bit
(MSB) and b0 is the least significant bit (LSB) of Binary. Gray code digits are g 2 , g1 , g0, where g2 is the
most significant bit (MSB) and g0 is the least significant bit (LSB) of Gray code.
000 000
001 001
011 010
010 011
110 100
111 101
101 110
100 111
Therefore, you solve boolean expression using k-map, you will get b2=g2, b1=g1⊕g2, and b0=g0⊕g1⊕g2.
Similarly, you can convert n-bit (bnb(n-1)...b2b1b0) Binary number into Gray code (gng(n-1)...g2g1g0). For least
significant bit (LSB) bn=gn, b(n-1)=g(n-1)⊕gn, …. b1=g1⊕g2⊕g3...⊕gn and b0=g0⊕g1⊕g2⊕g3...⊕gn.
Example − Convert Gray code 100111 into Binary number.
So, according above algorithm,
b5=g5=1=1
b4=g5⊕g4 =1⊕0 =1
b3=b4⊕g3 =1⊕0 =1
b2=b3⊕g2 =1⊕1 =0
b1=b2⊕g1 =0⊕1 =1
b0=b1⊕g0 =1⊕1 =0
So, Binary number will be 111010.
Excess-3, as its name says, there is an excess of number 3. That means we add 3 to a decimal digit
to get the corresponding Excess-3 number. We get the Excess-3 code of a corresponding decimal
number by adding 3 to its individual digits.
If we want to convert a binary number to Excess-3 code or vice versa, then we need to take decimal
as the intermediary. This means for, binary to Excess-3 code conversion, we need to convert the
binary number to decimal number first, then convert the decimal number to final Excess-3 code as
done above.
Excess-3 code is beneficial for the arithmetic operation where the 8421 BCD addition
exceeds the value 9.
Excess-3 representation can be used as codeword representation for data transmitted on
transmission line as it does not use both 0000 and 1111, which are more prone to
transmission error.
Excess-3 code is self-complementing. Therefore, it is beneficial in case of subtraction
module tb;
// Inputs
reg [3:0] bin;
// Outputs
wire [3:0] G;
always begin
// Initialize Inputs
bin<=0; #10;
bin<=1; #10;
bin<=2; #10;
bin<=3; #10;
bin<=4; #10;
bin<=5; #10;
bin<=6; #10;
bin<=7; #10;
bin<=8; #10;
bin<=9; #10;
bin<=10; #10;
bin<=11; #10;
bin<=12; #10;
bin<=13; #10;
bin<=14; #10;
bin<=15; #10;
#100; $stop;
end
endmodule
integer i = N-2;
endmodule
module tb;
parameter N = 4;
initial begin
gr = 0;
repeat (2**N) begin
#10;
gr = gr + 1;
end
end
gr2bi #(
.N (N)
) UUT (
.gr (gr),
.bi (bi)
);
endmodule
Binary to excess3
module Bin_excess(b,e);
input [2:0] b;
output wire [2:0] e;
assign e[2]=b[0]|b[1]|b[2];
assign e[1]=b[2]|~(b[1]^b[0]);
assign e[0]=~b[0];
endmodule
//TEST BENCH
module BIN_EXCESS3_tb;
// Inputs
reg [2:0] b;
// Outputs
wire [2:0] e;
// Instantiate the Unit Under Test (UUT)
Bin_excess uut (
.b(b),
.e(e)
);
initial begin
// Initialize Inputs
b = 0;
// Wait 100 ns for global reset to finish
#100;
// Add stimulus here
b=3’b000;
#100;
b=3’b001;
#100;
b=3’b010;
#100;
b=3’b011;
#100;
end
endmodule
Result: Verilog code forGray to binary and vice versa b) Binary to excess3 and vice versausing verilog
programis verified.
Experiment 5
8:1 mux, 8:3 encoder, Priority encoder
Aim: Write a Verilog code for 8:1 mux, 8:3 encoder, Priority encoder
Theory:
The multiplexer or MUX is a digital switch, also called as data selector. Multiplexer is a
combinational circuit that has maximum of 2n data inputs, ‗n‘ selection lines and single output line.
One of these data inputs will be connected to the output based on the values of selection lines. Since
there are ‗n‘ selection lines, there will be 2n possible combinations of zeros and ones. So, each
combination will select only one data input. Multiplexer is also called as Mux. 8x1 Multiplexer has 8
data inputs, 3 selection lines and one output. These are mostly used to form a selected path between
multiple sources and a single destination. A basic multiplexer has various data input lines and a
single output line. These are found in many digital system applications such as data selection and
data routing, logic function generators, digital counters with multiplexed displays, telephone network,
communication systems, waveform generators, etc. In this article we are going to discuss about types
of multiplexers and its design. In all types of digital system applications, multiplexers find its
immense usage. Since these allows multiple inputs to be connected independently to a single output,
these are found in variety of applications including data routing, logic function generators, control
sequencers, parallel-to serial converters, etc. Data Routing: Multiplexers are extensively used in data
routing applications to route the data to a one particular destination from one of several sources.
S2 S1 S0 Y
0 0 0 10
0 0 1 11
0 1 0 12
0 1 1 13
1 0 0 14
1 0 1 15
1 1 0 16
1 1 1 17
An Encoder is a combinational circuit that performs the reverse operation of Decoder. It has
maximum of 2n input lines and ‘n’ output lines; hence it encodes the information from 2 n inputs into
an n-bit code. It will produce a binary code equivalent to the input, which is active High. Therefore,
the encoder encodes 2n input lines with ‘n‘ bits. Encoders are very common electronic circuits used
in all digital systems. Encoders are used to translate the decimal values to the binary in order to
perform the binary functions such as addition, subtraction, multiplication, etc. One of the main
disadvantages of standard digital encoders is that they can generate the wrong output code when
there is more than one input present at logic level ―1‖. One simple way to overcome this problem
is to ―Prioritise‖ the level of each input pin. if there is more than one input at logic level ―1‖ at the
same time, the actual output code would only correspond to the input with the highest designated
priority. Then this type of digital encoder is known commonly as a Priority Encoder or P-encoder
for short. The Priority Encoder solves the problems mentioned above by allocating a priority level
to each input. The priority encoders output corresponds to the currently active input which has the
highest priority. when an input with a higher priority is present, all other inputs with a lower priority
will be ignored. Priority encoders can be used to reduce the number of wires needed in a particular
circuits or application that have multiple inputs. Other applications especially for Priority Encoders
may include detecting interrupts in microprocessor applications.
Truth Table:
En D7 D6 D5 D4 D3 D2 D1 D0 Q2 Q1 Q0
0 X X X X X X X X Z Z Z
1 0 0 0 0 0 0 0 1 0 0 0
1 0 0 0 0 0 0 1 X 0 0 1
1 0 0 0 0 0 1 X X 0 1 0
1 0 0 0 0 1 X X X 0 1 1
1 0 0 0 1 X X X X 1 0 0
1 0 0 1 X X X X X 1 0 1
1 0 1 X X X X X X 1 1 0
1 1 X X X X X X X 1 1 1
1 1 X X X X X X X Z Z Z
Verilog Code for 8:1 Multiplexer
module m81(out, D0, D1, D2, D3, D4, D5, D6, D7, S0, S1, S2);
input wire D0, D1, D2, D3, D4, D5, D6, D7, S0, S1, S2;
output reg out;
always@(*)
begin
case(S0 & S1 & S2)
3'b000: out=D0;
3'b001: out=D1;
3'b010: out=D2;
3'b011: out=D3;
3'b100: out=D4;
3'b101: out=D5;
3'b110: out=D6;
3'b111: out=D7;
default: out=1'b0;
endcase
end
endmodule
//Testbench
module top;
wire out;
reg D0, D1, D2, D3, D4, D5, D6, D7, D8, S0, S1, S2;
m81
uut(.D0(D0), .D1(D1), .D2(D2), .D3(D3), .D4(D4), .D5(D5), .D6(D6), .D7(D7), .S0(
S0), .S1(S1), .S2(S2), .out(out));
initial
begin
D0=1'b0; D1=1'b0; D2=1'b0; D3=1'b0; D4=1'b0; D5=1'b0; D6=1'b0; D7=1'b0;S0=1'b0;
S1=1'b0; S2=1'b0;
#500 $finish;
end
always #1 D0=~D0;
always #2 D1=~D1;
always #3 D2=~D2;
always #4 D3=~D3;
always #5 D4=~D4;
always #6 D5=~D5;
always #7 D6=~D6;
always #8 D7=~D7;
always #9 S0=~S0;
always #10 S1=~S1;
always #11 S2=~S2;
always@(D0 or D1 or D2 or D3 or D4 or D5 or D6 or D7 or S0 or S1 or S2)
$monitor("At time = %t, Output = %d", $time, out);
Endmodule
always @(din)
begin
if (din ==8'b00000001) dout=3'b000;
else if (din==8'b00000010) dout=3'b001;
else if (din==8'b00000100) dout=3'b010;
else if (din==8'b00001000) dout=3'b011;
else if (din==8'b00010000) dout=3'b100;
else if (din ==8'b00100000) dout=3'b101;
else if (din==8'b01000000) dout=3'b110;
else if (din==8'b10000000) dout=3'b111;
else dout=3'bX;
end
endmodule
module encodert_b;
reg [0:7] din;
wire [2:0] dout;
encoder uut (.din(din), .dout(dout));
initial begin
#10 din=8'b10000000;
#10 din=8'b01000000;
#10 din=8'b00100000;
#10 din=8'b00010000;
#10 din=8'b00001000;
#10 din=8'b00000100;
#10 din=8'b00000010;
#10 din=8'b00000001;
#10 $stop;
end
endmodule
module prio_enco_8x3_tst;
reg [7:0] D;
reg En;
wire[2:0] Q;
encoder_wp uut (.D(D),.Q(Q),.En(En));
initial
begin
En=1'b0;
#10 En=1'b1;
#10 D=8'b00000001;
#10 D=8'b00000010;
#10 D=8'b00000110;
#10 D=8'b00001010;
#10 D=8'b00010000;
#10 D=8'b00100010;
#10 D=8'b01000000;
#10 D=8'b10000110;
#10 D=8'b10001000;
#10 $stop;
end
endmodule
Result:Verilog code for 8:1 mux, 8:3 encoder, Priority encoder are realized.
Experiment 6
1:8Demux, 3:8 decoder, 2 bit comparator
Aim: Write a Verilog code for 1:8 Demux, 3:8 decoder, 2 bit comparator
Theory:
The 1:8 Demux consists of 1 data input bit, 3 control bits and 8 output bits. I 0, I1, I2, I3, I4, I5, I6,
I7 are the eight output bits, S0, S1 and S2 are the control bits and input D.
The applications include: Demultiplexers are used in clock data recovery solutions. Demultiplexer
along with multiplexer is necessary for any communication system for data transmission.
Demultiplexers are used in ATM packets broadcasting. The output of Arithmetic Logic Unit is
stored in respective registers using Demultiplexers.
Decoder- In this tutorial, you learn about the Decoder which is one of the most important topics
in digital electronics. In this article we will talk about the Decoder itself, we will have a look at
the 3 to 8 decoder, 3 to 8 line decoder designing steps, a technique to simplify the Boolean
function, and in the end, we will draw a logic diagram of the 3 to 8 decoder.
A digital decoder converts a set of digital signals into corresponding decimal code. A decoder is also
the most commonly used circuit prior to the use of an encoder. The encoded data is decoded for the
user interface in most of the output devices like monitors, calculator displays, printers, etc.
A magnitude digital Comparator is a combinational circuit that compares two digital or binary
numbers in order to find out whether one binary number is equal, less than, or greater than the
other binary number. We logically design a circuit for which we will have two inputs one for A and
the other for B and have three output terminals, one for A > B condition, one for A = B condition,
and one for A < B condition.
module Demultiplexer(in,s0,s1,s2,d0,d1,d2,d3,d4,d5,d6,d7);
input in,s0,s1,s2;
output d0,d1,d2,d3,d4,d5,d6,d7;
assign d0=(in & ~s2 & ~s1 &~s0),
d1=(in & ~s2 & ~s1 &s0),
d2=(in & ~s2 & s1 &~s0),
d3=(in & ~s2 & s1 &s0),
d4=(in & s2 & ~s1 &~s0),
d5=(in & s2 & ~s1 &s0),
d6=(in & s2 & s1 &~s0),
d7=(in & s2 & s1 &s0);
endmodule
module TestModule;
// Inputs
reg in;
reg s0;
reg s1;
reg s2;
// Outputs
wire d0;
wire d1;
wire d2;
wire d3;
wire d4;
wire d5;
wire d6;
wire d7;
module Decoder(a,b,c,d0,d1,d2,d3,d4,d5,d6,d7);
input a,b,c;
output d0,d1,d2,d3,d4,d5,d6,d7;
assign d0=(~a&~b&~c),
d1=(~a&~b&c),
d2=(~a&b&~c),
d3=(~a&b&c),
d4=(a&~b&~c),
d5=(a&~b&c),
d6=(a&b&~c),
d7=(a&b&c);
endmodule
module TestModule;
// Inputs
reg a;
reg b;
reg c;
// Outputs
wire d0;
wire d1;
wire d2;
wire d3;
wire d4;
wire d5;
wire d6;
wire d7;
module comparator(
Data_in_A, //input A
Data_in_B, //input B
less, //high when A is less than B
equal, //high when A is equal to B
greater //high when A is greater than B
);
//Internal variables
reg less;
reg equal;
reg greater;
module tb_tm;
// Inputs
reg [3:0] Data_in_A;
reg [3:0] Data_in_B;
// Outputs
wire less;
wire equal;
wire greater;
initial begin
//Apply inputs
Data_in_A = 10;
Data_in_B = 12;
#100;
Data_in_A = 15;
Data_in_B = 11;
#100;
Data_in_A = 10;
Data_in_B = 10;
#100;
end
endmodule
Experiment 7
SR flip flop , JK flip flop, D flip flop
Aim: Write a Verilog code for SR,JK,D flip flop and verify the functionality using FPGA board.
Theory:
A flip flop is an electronic circuit with two stable states that can be used to store binary data. The
stored data can be changed by applying varying inputs. Flip-flops and latches are fundamental
building blocks of digital electronics systems used in computers, communications, and many other
types of systems. Flip-flops and latches are used asdata storageelements. It is the basic storage
element in sequential logic. The basic building block that makes computer memories possible and
is also used in many sequential logic circuits is the flip-flop or bistable circuit. Just two inter-
connected logic gates make up the basic form of this circuit whose output has two stable output
states.
The SR flip-flop can be considered as a 1-bit memory since it stores the input pulse even after it
has passed. Flipflops (or bi-stables) of different types can be made from logic gates and, as with
other combinations of logic gates, the NAND and NOR gates are the most versatile, the NAND
being most widely used. This is because, as well as being universal, i.e. it can be made to mimic
any of the other standard logic functions, it is also cheaper to construct. Other, more widely used
types of flip-flop are the JK, the D type and T type. Applications of flipflops include Registers,
Counters, Event Detectors, Data Synchronizers, Frequency Dividers.
The SR (Set-Reset) flip-flop is one of the simplest sequential circuits. The output of each gate is
connected to one of the inputs of the other gate, giving a form of positive feedback or ‗cross-
coupling‘. This SR flip flop circuit has a
|
set input (S) and a reset input (R). In this circuit when ―S‖ is active the output ―Q‖ would be
high and ―Q ‖ will be low. The indeterminate or uncertain logic state only occurs if the inputs
change from 0,0 to 1,1 together. This should be avoided in normal operation but is likely to
happen when power is first applied. This could lead to uncertain results, but the flip-flop will work
normally once an input pulse is applied to either input. The SR Flip-flop is therefore, a simple 1-
bit memory.
Truth Table:
0 0 X Q(t) No change
0 1 0 Reset
1 0 1 Set
1 1 ? Invalid
input s,r,clk;
output reg q, qbar;
always@(posedge clk)
begin
if(s == 1)
begin
q = 1;
qbar = 0;
end
else if(r == 1)
begin
q = 0;
qbar =1;
end
else if(s == 0 & r == 0)
begin
q <= q;
qbar <= qbar
end
end
endmodule
module dff_test;
reg S,R, CLK;
wire Q, QBAR;
Expected Waveform
module DFF( Q,
Qbar,
D,
Clk,
Reset
);
output reg Q;
output Qbar;
input D,
Clk,
Reset;
begin
Q = 1'b0;
else
Q = D;
end
endmodule
module DFF_tb;
// Inputs
reg D;
reg Clk;
reg Reset;
// Outputs
wire Q;
wire Qbar;
DFF uut (
.Q(Q),
.Qbar(Qbar),
.D(D),
.Clk(Clk),
.Reset(Reset)
);
initial begin
// Initialize Inputs
D = 1'b0;
Clk = 1'b0;
Reset = 1'b1;
#100;
Reset = 1'b0;
#20;
forever #40 D = ~ D;
end
endmodule
Expected Waveform:
module jkff_behave(clk,j,knq,qbar);
input clk,j,k;
output reg q,qbar;
always@(posedge clk)
begin
if(k = 0)
begin
q <= 0;
qbar <= 1;
end
always@(posedge clk)
begin
if(k = 0)
begin
q <= 0;
qbar <= 1;
end
else if(j = 1)
begin
q <= 0;
qbar <= 0;
end
else if(j = 0 & k = 0)
begin
q <= q;
qbar <= qbar;
end
else if(j = 1 & k = 1)
begin
q <= ~q;
qbar <= ~qbar;
end
end
endmodule
module jkff_test;
reg J,K, CLK;
wire Q, QBAR;
clk=0;
forever #10 clk = ~clk;
end
initial begin
J= 1; K= 0;
#100; J= 0; K= 1;
#100; J= 0; K= 0;
#100; J= 1; K=1;
end
endmodule
Expected Waveform:
Experiment 8
BCD synchronous counter,up/down binary counter
Aim: Write a Verilog code for 4-bit BCD synchronous counter, up/down binary counter and verify
the functionality using FPGA board.
Theory:
In digital electronics, counter is a sequential logic circuit consisting of a series of flip-flops which is
used to counts the number of occurrences of input in terms of negative or positive edge transitions.
Countersare of two types depending upon clock pulse applied. These counters are: Asynchronous
counter, and Synchronous counter. In Asynchronous Counter is also known asRippleCounter,
different flip flops are triggered with different clock, not simultaneously. While in Synchronous
Counter, all flip flops are triggered with same clock simultaneously and Synchronous Counter is
faster than asynchronous counter in operation. In synchronous counter, all flip flops are triggered
with same clock simultaneously. Synchronous Counter is faster than asynchronous counter in
operation. Synchronous Counter does not produce any decoding errors. Synchronous Counter is
also called Parallel Counter. Synchronous Counter designing as well implementation are complex
due to increasing the number of states. Synchronous Counter will operate in any desired count
sequence. Synchronous Counter examples are:Ringcounter,Johnsoncounter. In synchronous
counter, propagation delay is less.
Synchronous counters are used in Alarm Clock, Set AC Timer, Set time in camera to take the
picture, flashing light indicator in automobiles, car parking control etc. Counting the time allotted
for special process or event by the scheduler. The one advantage of synchronous counter over
asynchronous counter is, it can operate on higher frequency than asynchronous counter as it does
not have cumulative delay because of same clock is given to each flip flop.
Synchronous Counter:
modulebcd_counter(clk,rst,q);
inputrst; input clk;
output [3:0]q;
reg[3:0]q=4'b0000;
always@(posedgeclk)
begin if(rst==1 ||
q==4'b1001)
q=4'b0000; else
q=q+1; end
endmodule
moduletest_bcd_counter;
// Inputs
regclk; regrst;
// Outputs
wire [3:0] q;
initial begin
clk = 0;
rst = 1;
#5 rst=0;
#150 rst=1;
#10 rst=0; #200
$finish;
end
always #5 clk=~clk;
endmodule
Verilog Code:
modulebcd_counter_synt(clk,rst,q);
inputrst,clk; output [3:0]q;
reg [3:0]q=4'b0000; reg
[31:0] clkdiv=25'd0;
always@(posedgeclk)
clkdiv=clkdiv+1;
always@(posedgeclkdiv[24])
begin
if(rst==1 || q==4'b1001)
q=4'b0000; else
q=q+1; end endmodule
// down counter
always @(posedgeclkorposedge reset)
begin
if(reset)
counter_up_down<=4'h0;
elseif(~up_down)
counter_up_down<=counter_up_down+4'd1;
else
counter_up_down<=counter_up_down-4'd1;
end
assign counter =counter_up_down;
endmodule
reset=1;
up_down=0;
#20;
reset=0;
#200;
up_down=1;
end
endmodule
Expected Waveform:
BCD Counter:
Binary Counter
Verilog code for 4-bit BCD synchronous counter, up/down binary counterare verified using
Result:
FPGA board.
PART-B
Interfacing and Debugging
Experiment 9
Verilog Program to interface a Stepper motor to the FPGA and rotate the motor
in the specified direction (by N steps).
Aim:Interface a Stepper motor to FPGA and write Verilog code to control the Stepper motor
rotation
Theory:
A stepper motor is a special type of electric motor that moves in increments, or steps, rather than
turning smoothly as a conventional motor does. Typical increments are 0.9 or 1.8 degrees, with 400
or 200 increments thus representing a full circle. The speed of the motor is determined by the time
delay between each incremental movement.
U8 is a Driver Buffer (ULN2003) device connected to LPC-1768 Device and can be used for
driving Stepper Motor. On the LPC-1768, P1.22 to P1.25 is used to generate the pulse sequence
required to run the stepper Motor. Also, the Stepper Motor is powered by its own power supply pin
(COM), which is connected to a 12V supply. Table shows connections for stepper Motor.
Construction & Working Principle
The construction of a stepper motor is fairly related to a DC motor. It includes a permanent magnet
like Rotor which is in the middle & it will turn once force acts on it. This rotor is enclosed through a
no. of the stator which is wound through a magnetic coil all over it. The stator is arranged near to
rotor so that magnetic fields within the stators can control the movement of the rotor.
The stepper motor can be controlled by energizing every stator one by one. So the stator will
magnetize & works like an electromagnetic pole which uses repulsive energy on the rotor to move
forward. The stator’s alternative magnetizing as well as demagnetizing will shift the rotor
gradually &allows it to turn through great control. The stepper motor working principle is
Electro-Magnetism. It includes a rotor which is made with a permanent magnet whereas a stator is
with electromagnets. Once the supply is provided to the winding of the stator then the magnetic
field will be developed within the stator. Now rotor in the motor will start to move with the
rotating magnetic field of the stator. So this is the fundamental working principle of this motor
4. Consumer Electronics – Stepper motors in cameras for automatic digital camera focus and zoom
functions.
And also have business machines applications, computer peripherals applications
Block Diagram:
Verilog Program:
module step_motor(input clk, output reg[3:0]stp, input dipsw1, input dipsw2, input
dipsw3); reg[20:0]count=21'd0; reg[3:0]stpval=4'b0001; always@(posedgeclk)
begin
count=count+1;
if(count==21'd20000000)
begin stp=stpval;
if(dipsw1==1)
begin
stpval=stpval>>1;
if(stpval==4'b0000)stpval=4'b1000;
end
else if(dipsw2==1)
begin
stpval=stpval>>1;
if(stpval==4'b0000)stpval=4'b1000;
end
else if(dipsw3==1)
begin count=0;
stpval=stpval<<1;
if(stpval==4'b0000)stpval=4'b0001;
end
endend
endmodule
UCF
Result:Verilog code to control the Stepper motor rotation is written and verified.
Experiment 10
Verilog Program to interface aADC to FPGA
Aim:Write Verilog code to convert an analog input of a sensor to digital form and to display the
same on a suitable display like set of simple LEDs.
Theory:
Transducers are also used to convert the input analog variables in the form of currents or voltages.
Basically, the digital numbers used here are binary i,e ‘0’ and ‘1’. The ‘0’ indicates the ‘off’ state
and ‘1’ represents the ‘on’ state. Hence all the analog values are converted into digital binary values
by an ADC. For example, if we have to install an alarm in our house or at some facility, whose
function is to set off in case of fire or overheating. Our whole alarm system will be electronic but
the temperature sensor will give analog values at the output after sensing the temperature. Therefore
to convert the varying values of temperature in digital or discrete values, we have to use an analog
to digital converter.
Block Diagram:
Clk Write_en
Logic Int_pin Analog to
Circuit Digital
Converter
(8-bit) Digital-
8
bit
Verilog Code:
await=1; end
if(count==5)
begin
write_en=1;
await=0; end
if(await==0)
begin
if(int_pin==0)
begin
led_display=di
gital_data;
await=1;
endend
if(await==1)co
unt=count+1;
if(count==10)c
ount=0;
endendmodul
e
UCF
# PlanAhead Generated physical constraints
Result:Verilog code is written to convert an analog input of a sensor to digital form and to displayed
the same on a suitable display.
Experiment 11
Verilog Program to interface a DAC to FPGA
Aim: To Interface a DAC to FPGA and write Verilog code to generate Sine wave of frequency F KHz
(eg.20 KHz) frequency.
Block Diagram:
Verilog Code:
i=0; end
if(count==1)wr=1;
if(count==4)wr=0; count=count+1;
if(count==10)count=0;
end
endmodule
UCF
Expected Waveform
Experiment 12
Verilog Program to interface Switches and LEDsto FPGA
Aim: Write a Verilog programs to interface Switches and LEDs to the FPGA/CPLD and
demonstrate its working.
Block Diagram:
A digital circuit contains a power supply, devices, and conduction nets. Some nets provide the
circuit with inputs from the “outside world,” while others, conversely, provide information from the
circuit to the outside world. The nets that provide an interface between the circuit and the outside
world are called ports.
Circuits need input devices to generate input signals. Input devices can take many forms, among
them keyboards (such as on a PC), buttons, and switches. Circuits also need output devices to
communicate their state to the user. In this project, a switch is used as the input device and an LED
is used as the output device. The digital circuit we will build is called the “led_sw,” as shown in
Fig. 1 below. The circuit created in this project will be implemented inside the FPGA board. The
board has an input port called “sw,” which receives an input signal from the external switch in the
circuit and an output port called “led,” which drives the external LED in the circuit. The “led_sw” is
a simple circuit that bypasses the signal on the input port and directly sends information to the
output port. You can view this as a direct wired connection between the net “sw” and the net “led.”
The circuit will be implemented using Verilog HDL. On different FPGA boards, switches and
LEDs are connected to different pins on an FPGA chip. Thus, a user constraint file (XDC) is needed
to map the input and output net of the circuit to the physical pin location on the FPGA chip. Take
Nexys3 as an example, the Slide Switch 0 (SW0) is connected to FPGA pin T10, and FPGA pin
U16 drives LED 0 (LD0). When you slide the switch to the ON position, a high voltage will be
placed on FPGA pin T10, which is mapped to the input port of the circuit “led_sw.” The digital
circuit then transmits the signal onto the output port LED, which is connected to FPGA pin U16.
The high voltage on the output port “led” will cause a voltage drop between node A and node B.
This voltage drop will drive current through the LED, which will light the LED and inform the user
that the switch is on.
Verilog Code:
module example (
inputclk,
inputrst,
input push,
outputregled_on
);
reg on;
reg off;
regt_on_off;
regt_off_on;
always @* begin
t_on_off = on & (push);
end
always @* begin
t_off_on = off & (push);
end
always @* begin
led_on = on;
end
endmodule
Result: Verilog programs to interface Switches and LEDs using the FPGA is verified.