Verilog Codes
Verilog Codes
SURE TRUST
By
ARTI TYAGI
( Arti Tyagi)
CONTENTS
1. Combinational Logic Circuit - I
Binary Adder
The most basic arithmetic operation is addition. The circuit, which performs the
addition of two binary numbers is known as Binary adder. First, let us implement an
adder, which performs the addition of two bits.
1.1 Adder
Adder are use to perform the binary addition of two binary numbers.Adders
are used for signed or unsigned addition operations.
1.1.1 HALF ADDER :
Half adder is a combinational circuit, which performs the addition of two bits A
and B are of single bit numbers. It produces two outputs sum & carry as
Outputs.
BLOCK DIAGRAM :
TRUTH TABLE :
BOOLEAN EXPRESSIONS :
SUM = A ^ B = AB`+A`B
CARRY = A & B = AB
VERILOG CODE AND TESTBENCH CODE:
module ha_bhv(a,b,sum,carry);
input a,b;
output reg sum,carry;
module hf_tb();
always@(a or b)begin reg t_a,t_b;
if(a==0 && b ==0) begin wire s,c;
sum = 0; half_adder
carry = 0; dut(.a(t_a), .b(t_b), .s(s), .c(c));
end
initial begin
else if(a==1 && b==1) begin t_a=0; t_b=0;
#10
sum = 0;
t_a=0;t_b=1;
carry = 1;
#10
end
t_a=1;t_b=0;
#10
else begin t_a=1;t_b=1;
(a==1 && b==0) or (a==0 && b==1) #10
sum = 1; $stop;
carry = 0; end
end endmodule
end
endmodule
RTL IMPLEMENTATION :
SIMULATION RESULTS:
TCL CONSOLE :
1.2 FULL ADDER
A full adder is a combinational logic circuit that forms the arithmetic sum of three bits.
it consists of three inputs and two outputs. which performs the addition of three
bits A ,B and Carrry and It produces sum & carry as Outputs.
Half adder have no scope of adding the carry bit ,to overcome this drawback,full adder
comes into play.
BLOCK DIAGRAM :
BOOLEAN EXPRESSION:
SUM = A ^ B ^ C
CARRY = (A & B) | (B & C) | (A & C)
VERILOG RTL CODE AND TESTBENCH CODE FOR FULL ADDER :
a=1'b0;
b=1'b1;
c=1'b1;
#10;
a=1'b1;
b=1'b1;
c=1'b0;
#10;
a=1'b1;
b=1'b01;
c=1'b1;
#10;
$finish;
end
endmodule
RTL IMPLEMENTATION :
SIMULATION RESULT :
1.2 SUBTRACTOR
Subtractors are used to perform the binary subtraction of two binary numbers.this
section describes about
the half and full subtractors.
BLOCK DIAGRAM :
TRUTH TABLE :
BOOLEAN EXPRESSIONS :
Diff= A'B+AB'
Borrow = A'B
VERILOG RTL CODE AND TESTBENCH CODE FOR FULL ADDER :
module HS_gate(a,b,diff,borrw);
input a,b;
output diff,borrw;
wire x;
xor g1(diff,a,b);
not(x,a);
and g2(borrw,x,b);
endmodule
RTL IMPLEMENTATION :
SIMULATION RESULT :
1.1.1 FULL SUBTRACTOR
BLOCK DIAGRAM :
TRUTH TABLE:
BOOLEAN EXPRESSION :
Difference = A ⊕ B ⊕ Bin
Bout = A' Bin + A' B + B Bin
Module full_sub(a,b,bin,diff,borrw);
input a,b,bin;
output diff,borrw;
endmodule
module testbench;
reg a,b,c;
wire diff,borrw;
initial begin
a=0; b=0; c=0;
#5 a=0; b=0; c=1;
#5 a=0; b=1; c=0;
#5 a=0; b=1; c=1;
#5 a=1; b=0; c=0;
#5 a=1; b=0; c=1;
#5 a=1; b=1; c=0;
#5 a=1; b=1; c=1;
$display($time, "a=%b, b=%b, c=%b, diff=%diff, borrw=%borrw",
a,b,c,diff,borrw);
end
endmodule
RTL IMPLEMENTATION:
SIMULATION RESULT :
1.4 MULTI-BIT ADDERS
Multi-bit adders and subtractors are used in the design of arithmatic units for the
processors.
The logic density depends upon the number of input bits of adder or subtractor.
Ripple Carry Adder works in different stages .each full adder takes the carry-in as input
and produces carry-out and sum bit as output.
These carry adders are used mostly in addition to n-bit input sequences.
These carry adders are applicable in the digital signal
processing and microprocessors.
VERILOG RTL CODE AND TESTBENCH CODE FOR RIPPLE
CARRY ADDER :
module FA(a,b,cin,sum,cout);
input a,b,cin;
output sum,cout;
assign sum=a^b^cin;
assign cout=(a^b)&cin|a&b;
endmodule
module Parallel_adder(a,b,cin,sum,cout);
input[3:0]a,b;
input cin;
output[3:0]sum;
output cout;
wire c1,c2,c3;
//Specify the function of full adder...
FA f0(a[0],b[0],cin,sum[0],c1);
FA f1(a[1],b[1],c1,sum[1],c2);
FA f2(a[2],b[2],c2,sum[2],c3);
FA f3(a[3],b[3],c3,sum[3],cout);
endmodule
module testbench;
In most of the practical scenarios; Magnitude Comparators are used to compare the
equality of two binary numbers.
MAGNITUDE COMPARATORS :
The comparison of two binary numbers is an operation that determines whether one
number is greater than
or lesser than or equal to the other number.A magnitude comparator is a combinational
circuit that compares
two numbers A and B and determines their relative magnitudes. 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 A < B condition.
TRUTH TABLE :
A>B: AB'
A<B: A'B
A=B: A'B' + AB
1-BIT MAGNITUDE COMPARATOR LOGIC GATES DIAGRAM :
module module
comp_data(a,b,equal,greater,lower); comp_bhv(a,b,equal,greater,lower
output equal; );
output greater; input a;
output lower; input b;
input a; output reg equal;
input b; output reg greater;
assign equal = a~^b; output reg lower;
assign lower = (~a)&b;
assign greater = a&(~b); always@(*) begin
endmodule equal=0; greater=0; lower=0;
if(a>b)
begin
greater = 1'b1;
end
else if(a<b)
begin
lower = 1'b1;
end
else(a=b);
equal = 1'b1;
end
endmodule
//1 bit_comparator using gate modeling
module comp_g(a,b,equal,greater,lower);
output equal;
output greater;
output lower;
input a;
input b;
wire w1,w2;
not g1(w1,a);
not g2(w2,b)
and g3(w1,a,b);
and g4(w2,a,b);
xnor g5(E,w1,w2)
endmodule
module testbench;
reg a,b;
wire equal,greater,lower;
comp_bhv comp_1(a,b,equal,greater,lower);
initial begin
repeat(5) begin
a=$random;
b=$random;
#5;
end
$display("a=%b,b=%b, ___ equal = %b,greater = %b,lower
= %b",a,b,equal,greater,lower);
end
endmodule
RTL IMPLEMENTATION :
SIMULATION RESULT :
4-BIT MAGNITUDE COMPARATOR :
A comparator used to compare two binary numbers each of four bits is called
a 4-bit magnitude
comparator. It consists of eight inputs each for two four-bit numbers and
three outputs to generate less than, equal to, and greater than between
two binary numbers.
TRUTH TABLE :
The truth table for a 4-bit comparator would have 4^4 = 256 rows. So we will do
Things a bit differently here. We will compare each bit of the two 4-bit numbers,
BOOLEAN EXPRESSION :
A=B :- The equation for the A=B condition was A B. Let’s call this x. Taking a
look at the
truth table above, A=B is true only when (A3=B3 and A2=B2 and A1=B1 and
A0=B0). Hence
Z (A=B) = A3B3 . A2B2 . A1.B1 . A0.B0 = x3x2x1x0
A>B
X(A>B) = A3B3′ + x3A2B2′ + x3x2A1B1′ + x3x2x1A0B0′
A<B
Employing the same principles we used above, we get the following equation
Y(A<B) = A3’B3 + X3A2’B2 + X3X2A1’B1 + X3X2X1A0’B0
module comp_data(a,b,equal,greater,less);
// 4 bit_comparator using Bhaviour
output equal;
modeling
output greater;
output less;
module
comparator_4bit(a,b,equal,greater,les
input [3:0] a;
s);
input [3:0] b;
input [3:0] a;
assign greater = ~(a[3] & ~b[3] | ~a[3] & b[3])
input [3:0]b;
& a[2] & ~b[2] | a[3] & ~b[3];
output reg equal;
output reg greater;
assign equal = ~(a[2] & ~b[2] | ~a[2] & b[2]) &
output reg less;
~(a[3] & ~b[3] | ~a[3] & b[3]);
always @(*) begin
assign less = ~(a[3] & ~b[3] | ~a[3] & b[3]) &
equal=0; greater=0; less=0;
~a[2] & b[2] | ~a[3] & b[3];
if(a>b)
endmodule
begin
equal = 0;
less = 0;
greater = 1;
end
else if(a<b)
begin module testbench;
equal = 0; reg [3:0] a;
less = 1; reg [3:0]b;
greater = 0; wire equal;
end wire greater;
else begin wire less;
equal = 1; integer i;
less = 0;
greater = 0; comp_data
end dut(.a(a), .b(b), .equal(equal), .greater(greater)
end , .less(less));
endmodule
initial begin
for(i=0;i<8;i=i+1)
begin
a=$random;
b=$random;
#10;
end
end
initial begin
$monitor("a=%b, b=%b, equal=%b,
greater=%b, less=%b" , a,b,equal,greater,less);
#100 $finish;
end
endmodule
RTL IMPLEMENTATION :
SIMULATION RESULT :
1.6 CODE CONVERTER
As name itself indicates the code converters are used to convert the code from
one number system
to another number system. In the practical scenarios, binary to gray and gray to
binary converters are used.
Base of binary number system is 2, for any multi-bit binary number one or more than
one bit changes at a time.
Lets take a input string and we have to convert into gray code.
B2 ⊕ B3 = G2
B1 ⊕ B2 = G1
B0 ⊕ B1 = G0
WORKING : HOW TO CONVERT BINARY TO GRAY CODE:
In the Gray code,the MSB(Most significant Bit) will always be the same as the
1'st bit of the given binary number.
In order to perform the 2nd bit of the gray code, we perform the exclusive-or
(XOR) of the 1'st and 2nd bit of the binary number. It means that if both the bits
are different, the result will be one else the result will be 0.
In order to get the 3rd bit of the gray code, we need to perform the exclusive-or
(XOR) of the 2nd and 3rd bit of the binary number. The process remains the same
for the 4th bit of the Gray code. Let's take an example to understand these steps.
//verilog code for 4-bit Binary to Gray //verilog code for 4-bit Binary
code converter using data flow to Gray code converter using
modeling... bhaviour modeling...
SIMULATION RESULT
1.6.2 GRAY TO BINARY CODE CONVERSION
The Gray to Binary code converter is a logical circuit that is used to convert the
gray code into its equivalent binary code.
module gray_binary1(G,bin);
input [3:0]G;
output [3:0]bin;//binary input
endmodule
module testbench_gray_binary;
reg [3:0]G;
wire [3:0]bin;
initial begin
G = 4'b0000;
#1000;
$finish;
End
endmodule
RTL IMPLEMENTATION
SIMULATION RESULT :
COMBINATIONAL LOGIC CIRCUITS - II
2.1 MULTIPLEXERS :
2:1 MULTIPLEXERS :
A 2:1 MUX has two input lines, one select line and one output line.
A 2-to-1 multiplexer consists of two inputs I0 and I1, one select input Sel and one
output out. Depending on the select signal, the output is connected to either of the inputs.
Since there are two input signals, only two ways are possible to connect the inputs to
the outputs.
If the select line is low, then the output will be switched to D0 input, whereas if select
line is
high, then the output will be switched to D1 Input.
BOOLEAN EXPRESSION OF 2 to 1 :
ADVANTAGES OF MULTIPLEXERS :
ADVANTAGES :
It reduce the no of wires
It reduces the cost of circuit and complexity of the circuit.
It makes transmission circuit economical and les complex
VERILOG RTL CODE AND TESTBENCH CODE FOR 2x1
MULTIPLEXERS :
module testbench;
reg sel,i0,i1;
wire out;
integer i;
mux_2x1 dut(sel,i0,i1,out);
initial begin
for(i=0;i<8;i=i+1)
begin
{sel,i0,i1} = i;
#10;
end
end
endmodule
RTL IMPLEMENTATION :
SIMULATION RESULT :
2.1.2 4x1 MULTIPLEXERS :
In the 4×1 multiplexer, there is a total of four inputs, i.e., i0, i1, i2, and i3, 2 selection
lines, i.e., S0 and
S1 and single output, i.e.Y. On the basis of the combination of inputs that are present
at the selection
lines S0 and S1, one of these 4 inputs are connected to the output.
One of these 4 inputs will be connected to the output based on the combination of
inputs present at these two selection lines.
When both the select inputs Sel0 = 0, Sel1 = 0, the top of AND logic gate is enable
and other two AND gate is disable, so the data input D0 input line is selected
Hence, the output Y = i0.When both the select inputs Sel0 = 1, Sel1= 1, the bottom
of AND logic gate is enable and other two AND gate is disable, so the data input i3
input line is selected and transmitted as output. Hence, the output Y = i3. And so on.
We can implement this Boolean function using Inverters, AND gates & OR gate.
The circuit diagram of 4x1 multiplexer
VERILOG RTL CODE AND TESTBENCH CODE FOR 4x1 MULTIPLEXERS :
module mux4x1_g(out,i0,i1,i2,i3,s0,s1);
output out;
input i0,i1,i2,i3,s0,s1;
wire s0bar,s1bar,w0,w1,w2,w3;
not g1(s0bar,s0),(s1bar,s1);
and g2(w1,i0,s0bar,s1bar),(w2,i1,s0bar,s1),(w3,i2,s0,sbar),(w4,i3,s0,s1);
or g3(out,w1,w2,w3,w4);
endmodule
//verilog code for mux4x1 using Data flow modeling.
Module mux4x1_data(i0,i1,i2,i3,s0,s1,out);
input i0,i1,i2,i3;
input s0,s1;
output out;
endmodule
module testbench;
reg I0;
reg I1;
reg I2;
reg I3;
reg s0, s1;
wire out;
module mux4x1_b(out,i0,i1,i2,i3,s0,s1);
output reg out;
input s0,s1,i0,i1,i2,i3;
always @(*)
begin
case(sel)
2'b: y = i0;
2'b: y = i1;
2'b: y = i2;
2'b: y = i3;
default:2'bx;
$display("i0=%b,i1=%b,i2=%b,i3=%b,s0=%b,s1=%b,out=%
b" ,i0,i1,i2,i3,s0,s1,out);
endcase
end
endmodule
RTL IMPLEMENTATION :
SIMULATION RESULT :
8x1 MULTIPLEXERS :
In the 8 to 1 multiplexer, there are total eight inputs, i.e., i0, i1, i2, i3, i4, i5, i6, and i7,
3 selection lines,I.e S0, S1and S2 and single output, i.e., Y. On the basis of
the combination of inputs that are present at the selection lines S0, S1, and S2,
of these 8 inputs are connected to the output.
When all select input line are S0=0 S1=0, S2=0, then the topmost AND logic gates
is enable
and all other AND logic gates are disable so the data input D0 is selected and
transmitted
as output. The output is Y = i0.
When all select input line are S0=1 S1=1, S2=, then the topmost AND logic gates is
disable and all other AND logic gates are enable so the data input i7 is selected
and transmitted as output.
The output is Y = i7.
BOOLEAN EXPRESSION 8x1 MULTIPLEXERS :
APPLICATIONS OF MULTIPLEXERS :
Multiplexers are used in various types of applications :
not g1(s0bar,s0);
not g2(s1bar,s1);
not g3(s2bar,s2);
and g4(w1,i0,s2bar,s1bar,s0bar);
and g5(w2,i1,s2bar,s1bar,s0);
and g6(w3,i2,s2bar,s1,s0bar);
and g7(w4,i3,s2bar,s1,s0);
and g8(w5,i4,s2,s1bar,s0bar);
and g9(w6,i5,s2,s1bar,s0);
and g10(w7,i6,s2,s1,s0bar);
and g11(w8,i7,s2,s1,s0);
or g12(out,w1,w2,w3,w4,w5,w6,w7,w8);
endmodule
module testbench;
reg[7:0]i;
reg[2:0]s;
wire out;
//wire[7:0]w;
mux8x1_b mux_b(i,s,out);
initial begin
repeat(8) begin
{i,s} = $random;
#5;
$display("time = %t, i=%b,sel = %b,out = %b",
$time,i,s,out);
end
end
endmodule
RTL IMPLEMENTATION MUX 8x1 :
SIMULATION RESULT :
2.2 DEMULTIPLEXER
The word “Demultiplex” means “One into many”.
DeMultiplexing is the process of taking information from one input
and transmitting
the same over one of several outputs.
Demultiplexer is an also a combinational circuit in which one input and more
than two output.
It has single input, ‘n’ selection lines and maximum of 2n outputs.
The demultiplexer is a reverse process of multiplexer.De-multiplexer is also
treated as De-mux.
Since there are ‘n’ selection lines, there will be 2n possible combinations of
zeros and ones.
So, each combination can select only one output.
1x8 De-Multiplexer :
Y0 = S2 S1 S0 D
Y1 = S2 S1 S0 D
Y2 = S2 S1 S0 D
Y3 = S2 S1 S0 D
Y4 = S2 S1 S0 D
Y5 = S2 S1 S0 D
Y6 = S2 S1 S0 D
Y7 = S2 S1 S0 D
APPLICATION OF DEMULTIPLEXER :
Since the demultiplexers are used to select or enable the one signal out of many,
these are extensively used in microprocessor or computer control systems such as:
MULTIPLEXER
It is a combinational circuit in which have several input and one output.
It is a digital switching circuit.
It is a parallel to serial converter.
The multiplexer used in TDM.
8-1 MUX, 16-1 MUX, and 32-1 MUX are the different type of MUX
In multiplexer, the selection lines are used to control the specific input.
DEMULTIPLEXER
It is a combination circuit.
It has one input and several outputs.
It is a data distributer.
The serial to parallel conversion.
It works on the principle of one-to-many.
The various types of demultiplexers are 1-8 Demux, 1-16 Demux, 1-32 Demux.
VERILOG RTL CODE AND TESTBENCH CODE FOR 1x8
DEMULTIPLEXERS :
module demux_1_8(y,s,in);
output reg [7:0]y;
input [2:0]s;
input in;
always @(*)
begin
y=0;
case(s)
3'd0: y[0]=in;
3'd1: y[1]=in;
3'd2: y[2]=in;
3'd3: y[3]=in;
3'd4: y[4]=in;
3'd5: y[5]=in;
3'd6: y[6]=in;
3'd7: y[7]=in;
endcase
end
endmodule
module testbench;
reg [2:0]S;
reg in;
wire [7:0]Y;
demux_1_8 mydemux(.y(Y), .in(in), .s(S));
initial begin
in=1;
S=3'd0;
#5;
S=3'd1;
#5;
S=3'd2;
#5;
S=3'd3;
#5;
S=3'd4;
#5;
S=3'd5;
#5;
S=3'd6;
#5;
S=3'd7;
#5;
end
endmodule
RTL IMPLEMENTATION :
SIMULATION RESULT :
2.3 DECODER
Decoder has ‘n’ select lines or input lines and ‘m’ output lines and used to generate
either active high or active low output. The relation between select lines and output
lines is given by m = 2^n Depending on the logic status on ‘n’ input lines at a time
one of the output line
goes high or low.the Decoder performs the reverse operation of the Encoder.
at a time, only one input line is activated for simplicity. The produced 2N-bit output
code is equivalent to the binary information.
2 to 4 Decoder :
In the 2 to 4 line decoder, there is a total of three inputs, i.e., A0, and A1 and E and
four outputs,
ie., Y0, Y1, Y2, and Y3. For each combination of inputs, when the enable 'E' is set to
1, one of
these four outputs will be 1.
Y3=E.A1.A0
Y2=E.A1.A0'
Y1=E.A1'.A0
Y0=E.A1'.A0'
always@(en,a,b)
begin
if(en==0)
begin
if(a==1'b0 & b==1'b0) D=4'b1110;
else if(a==1'b0 & b==1'b1) D=4'b1101;
else if(a==1'b1 & b==1'b0) D=4'b1011;
else if(a==1'b1 & b==1'b1) D=4'b0111;
else D =4'b0000;
end
else
D=4'b1111;
end
endmodule
module decoder_2x4(a,b,D);
input a,b;
output[3:0]D;
decoder_2x4 dut(en,a,b,D);
initial begin
$monitor("en=%b,a=%b,b=%b,D=%b", en,a,b,D);
en = 1'b0;
a=1'b0; b=1'b0;
en = 1'b1;
end
begin
always #50 a=~a;
always #100 b=~b;
end
endmodule
RTL IMPLEMENTATION :
SIMULATION LOGIC :
3 to 8 line decoder:
The logical expression of the term Y0, Y1, Y2, Y3, Y4, Y5, Y6, and Y7 is as follows:
Y0=A0'.A1'.A2'
Y1=A0.A1'.A2'
Y2=A0'.A1.A2'
Y3=A0.A1.A2'
Y4=A0'.A1'.A2
Y5=A0.A1'.A2
Y6=A0'.A1.A2
Y7=A0.A1.A2
//
module decoder_3x8(in,En,D);
module testbench;
input[2:0]in;
reg[2:0]in;
input En;
reg En;
output reg[7:0]y;
wire[7:0]y;
//functionality of design..
decoder_3x8
always@(in or En)
dut(.in(in), .En(En), .y(y));
begin
initial begin
En=1'b1;
if(En)
#100
case(in)
in=3'b000;
3'b000: y=8'b0000_0001;
#100
3'b001: y=8'b0000_0010;
in=3'b001;
3'b010: y=8'b0000_0100;
#100
3'b011: y=8'b0000_1000;
in=3'b010;
3'b100: y=8'b0001_0000;
#100
3'b101: y=8'b0010_0000;
in=3'b011;
3'b110: y=8'b0100_0000;
#100
3'b111: y=8'b1000_0000;
in=3'b100;
endcase
#100
else
in=3'b101;
y=8'b0000_0000;
#100
in=3'b110;
end
#100
endmodule
in=3'b111;
#100;
$monitor("in=%b,
En=%b,y=%b" ,in,En,y);
end
endmodule
RTL IMPLEMENTATION :
SIMULATION RESULT :
2.4 ENCODER
The combinational circuits that change the binary information into N output lines are
known as Encoders. The binary information is passed in the form of 2N input lines. The
output lines define the N-bit code for the binary information. In simple words,
the Encoder performs the reverse operation of the Decoder. At a time, only one input
line is activated for simplicity.
8 x3 LINE ENCODER :
A2=Y4+Y5+Y6+Y7
A1=Y2+Y3+Y6+Y7
A0=Y7+Y5+Y3+Y1
module encoder_8_3(
input[7:0]in,
output reg[2:0]out );
always@(in)
begin
if(in[7]==1)
out=3'b111;
else if(in[6]==1)
out=3'b110;
else if(in[5]==1)
out=3'b101;
else if(in[4]==1)
out=3'b100;
else if(in[3]==1)
out=3'b011;
else if(in[2]==1)
out=3'b010;
else if(in[1]==1)
out=3'b001;
else
out=3'b000;
end module testbench;
endmodule reg [7:0] in;
wire [2:0] out;
encoder_8_3
dut(.in(in), .out(out));
Initial begin
#0 in=8'b10000000;
#10 in=8'b01000000;
#10 in=8'b00100000;
#10 in=8'b00010000;
#10 in=8'b00001000;
#10 in=8'b00000100;
#10 in=8'b00000010;
#10 in=8'b00000001;
#10 in=8'b00000000;
end
initial
begin $monitor("in: %b out: %b ",in,
out);
#100 $finish;
end
endmodule
RTL IMPLEMENTATION :
SIMULATION WAVEFORM :
2.5 PRIORITY ENCODER
TRUTH TABLE :
BOOLEAN EXPRESSION :
Q0 = D4+D5+D6+D7
Q1 = D2+D3+D6+D7
Q2 = D1+D3+D5+D7
CIRCUIT DIAGRAM USING LOGIC GATES :
APPLICATION :
It is used to reduce the no. of wires and connections required for electronic
circuit designing
that have multiple input lines. Example keypads and keyboards.
Used in controlling the position in the ship’s navigation and robotics arm
position.
Used in the detection of highest priority input in various applications of
microprocessor
interrupt controllers.
Used to encode the analog to digital converter’s output.
Used in synchronization of the speed of motors in industries.
Used robotic vehicles
Used in applications of home automation systems with RF
Used in hospitals for health monitoring systems
Used in secure communication systems with RF technology to enable secret
code.
VERILOG RTL CODE AND TESTBENCH CODE FOR 8x3 PRIORITY
ENCODER :
module Priority_Encoder(out,in);
input [7:0]in;
output reg[2:0]out;
always @(in)
begin
if(in[7]==1) out=3'b111;
else if(in[6]==1) out=3'b110;
else if(in[5]==1) out=3'b101;
else if(in[4]==1) out=3'b100;
else if(in[3]==1) out=3'b011;
else if(in[2]==1) out=3'b010;
else if(in[1]==1) out=3'b001;
else out=3'b000;
end
endmodule
Priority_Encoder dut(out,in);
initial begin
repeat(10) begin
in = $random;
#10;
end
end
initial begin
$monitor("in: %b out: %b",in, out);
#100 $finish;
end
endmodule
RTL IMPLEMENTATION :
SIMULATION WAVEFORM :
3 .SEQUENTIAL LOGIC DESIGN
BLOCK DIAGRAM :
3.1CONSTRUCTION OF FLIP-FLOPS :
D-FLIP FLOP :
D flip-flop always Hold the information, which is available on data input, D of earlier
positive transition of clock signal. From the above state table, we can directly
write the next state equation as,
Qn+1 = D
APPLICATIONS :
module d_flipflop(q,qbar,d,clk);
output reg q;
output reg qbar;
input d,clk;
module testbench_dff();
wire q;
wire qbar;
reg d,clk;
end
always #5 clk=~clk;
always #10 d=~d;
endmodule
RTL IMPLEMENTATION :
SIMULATION RESULT :
3.1.3 JK FLIP-FLOP
CIRCUIT DIAGRAM :
J K Q(n+1) State
0 0 Qn No Change
0 1 0 RESET
1 0 1 SET
1 1 Qn’ TOGGLE
CHARACTERISTICS EQUATION FOR JK FLIP FLOP :
APPLICATIONS :
Counters: The JK flip-flop can be used in conjunction with other digital logic
gates to create a binary counter. This makes it useful in real-time applications such
as timers and clocks.
Data storage: The JK flip-flop can be used to store temporary data in digital
systems.
Synchronization: The JK flip-flop can be used to synchronize data signals
between two digital circuits, ensuring that they are operating on the same clock
cycle. This makes it useful in applications where timing is critical.
module jk_flipflop(Clk,reset,J,K,Q,Q_bar);
input J,K;
input Clk,reset;
output reg Q;
output Q_bar;
always@(posedge Clk)
begin
if({reset})
Q <= 1'b0;
else
begin
case({J,K})
2'b00: Q <= Q;
2'b01: Q <= 1'b0;
2'b10: Q <= 1'b1;
2'b11: Q <= ~Q;
endcase
end
end
assign Q_bar = ~Q;
endmodule
RTL IMPLEMENTATION :
// Module Name: tb_JK_flipflop
module jk_ff_tb();
reg Clk,reset,J,K;
wire Q,Q_bar;
jk_flipflop dut(Clk,reset,J,K,Q,Q_bar);
initial begin
Clk=1'b0;
forever #5 Clk = ~Clk;
end
initial begin
reset = 1;
#10;
J = 1'b0; K = 1'b0; #10; //Initial memory
reset = 0;
J = 1'b0; K = 1'b1; #10; //Reset State
J = 1'b0; K = 1'b0; #10; //Memory State
SIMULATION WAVEFORM :
3.1.2 T FLIP-FLOP
T Q Qt+1
0 0 0
0 1 1
1 0 1
1 1 0
module t_flipflop(t,clk,reset,q,qbar);
input t,clk;
input reset;
output reg q;
output qbar;
reg t,clk,reset;
wire q,qbar;
t_flipflop dut(.t(t),.clk(clk),.reset(reset),.q(q),.qbar(qbar));
initial
begin
t = 0;
clk = 0;
reset = 1;
end
endmodule
RTL IMPLEMENTATION :
SIMULATION RESULT :
3.2 COUNTERS
Counters are basically used to count no. Of clock pulse applied.The counter is one
of the widest applications of the flip flop. Based on the clock pulse, the output
of the counter contains a predefined state. The number of the pulse can be
counted using the output of the counter.
CLASSIFICATION OF COUNTERS :
UP/DOWN COUNTER :
An up/down counter is a digital counter which can be set to count either from
0 to max_value
or max_value to 0. The direction of the count(mode) is selected using a
single bit input.
APPLICATION OF COUNTERS :
Frequency counters
Digital clock
Time measurement
A to D converter
Frequency divider circuits
Digital triangular wave generator.
VERILOG CODE AND TESTBENCH CODE FOR N_BIT UP/DOWN
COUNTER :
When Up mode is selected, counter counts from 0 to 15 and then again from
0 to 15.
When Down mode is selected, counter counts from 15 to 0 and then again from 15 to 0.
Changing mode doesn't reset the Count value to zero.
You have apply high value to reset, to reset the Counter output.
module testbench_nbit_updowncounter;
parameter N = 4;
// Inputs
reg clk;
reg reset;
reg UpOrDown;
wire [N-1:0] count;
// Instantiate the Unit Under Test (UUT)
N_Bit_updowncounter uut (
.clk(clk),
.reset(reset),
.UpOrDown(UpOrDown),
.count(count) );
initial clk = 0;
always #5 clk = ~clk;
initial begin
reset = 1;
#50 reset = 0;
UpOrDown = 0;
#300;
UpOrDown = 1;
#300;
reset = 1;
UpOrDown = 0;
#100;
reset = 0;
end
initial begin
$monitor("\t\t UpOrDown =%b, Count = %b",UpOrDown,count);
#800 $finish;
end
endmodule
SIMULATION WAVEFORM FOR N-BIT UP/DOWNCOUNTER :
3.3 MODULUS COUNTER (MOD-N COUNTER)
The 2-bit ripple counter is called as MOD-4 counter and 3-bit ripple counter
is called
as MOD-8 counter. So in general, an n-bit ripple counter is called as
modulo-N counter.
Where, MOD number = 2n.
MODULUS OF COUNTER :
Counter indicates the number of states through which counter passes during its
operation.
• 2-Bit counter = Mod 4 counter.
• 3-BIT counter = Mod 8 counter.
• MOD-N Counter is also called as Modulo Counter.
TYPE OF MODULUS :
Therefore, a "Mod-N" counter will require the "N" number of flip-flops connected
to count a single data bit while providing 2n different output states (n is the number of
bits). Note that N is always a whole integer value.
Then we can see that MOD counters have a modulus value that is an integral power
of 2, that is, 2, 4, 8, 16 and so on to produce an n-bit counter depending on the number
of flip-flops used, and how they are connected, determining the type and modulus of
the counter.
Application of counters :
Frequency counters
Digital clock
Time measurement
A to D converter
Frequency divider circuits
Digital triangular wave generator.
VERILOG CODE AND TESTBENCH CODE FOR MOD-N COUNTER :
Mod_N_Counter dut(clk,reset,count);
initial begin
clk =0;
forever #10 clk = ~clk;
end
initial begin
reset = 1;
#15;
reset = 0;
end
initial begin
$monitor("\t count= %b",count);
#200 $finish;
end
endmodule
RTL IMPLEMENTATION :
SIMULATION WAVEFORM :
3.4 GRAY CODE COUNTER
Gray code is one kind of binary number system where only one bit will change at
a time.
Today gray code is widely used in digital . This will helpful for error correction
and
signal transmission. Gray counter is also useful in design and verification in VLSI
domain.
In a gray code, only one bit changes at one time. This design code has two inputs,
clock and reset signals and one 4 bit output that will generate gray code.
First, if the reset signal is high, then the output will be zero, and as soon as reset goes
low, on the rising edge of clk, the design will generate a four-bit gray code and
continue to generate at every rising edge of clk signal.
This design code can be upgraded and put binary numbers as input, and this design
will work as a binary to gray code converter.
VERILOG CODE AND TESTBENCH CODE FOR GRAY CODE COUNTER :
module gray_counter(clk,reset,gray_count);
parameter N = 4;
input clk,reset;
output reg [N-1:0] gray_count;
reg [N-1:0] bin_count;
always@(posedge clk)
begin
if(reset)
begin
gray_count=4'b0000;
bin_count=4'b0000;
end
else
begin
bin_count = bin_count + 1;
gray_count =
{bin_count[3],bin_count[3]^bin_count[2],bin_count[2]^bin_count[1],
bin_count[1]^bin_count[0]};
end
end
endmodule
module test_bench;
parameter N = 4;
reg clk,reset;
wire [N-1:0] gray_count;
gray_counter dut(clk, reset, gray_count);
initial begin
clk= 1'b0;
forever #5 clk= ~clk;
end
initial begin
reset= 1'b1;
#10;
reset= 1'b0;
end
initial begin
$monitor("\t\t counter: %d", gray_count);
#175 $finish;
end
endmodule
RTL IMPLEMENTATION:
SIMULATION WAVEFORM :
3.5 RING COUNTER
At the time of reset the value of the counter is initialized to, say, 0001. It then becomes
0010 at the next clock cycle - and this keeps going on. Basically there is one bit that
keeps shifting to left 1 bit at each clock cycle and then it rolls over when it reaches
MSB.
ring_counters dut(clk,reset,count);
initial begin
clk = 0;
reset = 0;
#20 reset = 1;
#20reset = 0;
#300 $finish;
end
endmodule
always@(posedge clk)
begin
if(reset)
count <= 4'b0001;
else
count <= {count[0],count[N-1:1]};
end
endmodule
RTL IMPLEMENTATION RING COUNTER :
The main difference between the 4 bit ring counter and the Johnson counter is that,in
ring counter , we connect the output of last flip flop directly to the input of first flip flop.
But in johnson counter, we connect the inverted output of last stage to the first stage input.
The Johnson counter is also known as Twisted Ring Counter, with a feedback. In
Johnson counter the input of the first flip flop is connected from the inverted output of
the last flip flop.
TRUTH TABLE :
VERILOG CODE AND TESTBENCH CODE FOR TWISTED COUNTER :
module twisted_counter(clk,reset,count);
parameter N = 4;
input clk,reset;
output reg[N-1:0]count;
always@(posedge clk)
begin
if(reset)
count <= 0;
else
count <= {~count[0],count[N-1:1]};
end
endmodule
twisted_counter dut(clk,reset,count);
initial begin
clk =1'b0;
forever #5 clk = ~clk;
end
initial begin
reset =1'b1;
#15;
reset = 1'b0;
end
initial begin
$monitor("\t count = %d",count);
#100 $finish;
end
endmodule
RTL IMPLEMENTATION :
SIMULATION WAVEFORM :
3.6 LINEAR FEEDBACK SHIFT REGISTER (LFSR)
At every step,
Q[3] xor Q[2]
Q = Q << 1
The result of the XOR operation is fed to the LSB (0th bit).
VERILOG CODE AND TESTBENCH CODE FOR LFSR :
module LFSR(clk,reset,seq_out);
input clk,reset;
output reg [3:0] seq_out;
always@(posedge clk)begin
if(reset)
seq_out <= 4'hf;
else
seq_out <= {seq_out[2:0], seq_out[3]^seq_out[2]};
end
endmodule
module tb_LFSR;
reg clk,reset;
wire [3:0] seq_out;
LFSR dut(clk,reset,seq_out);
TCL CONSOLE :