Professional Documents
Culture Documents
LOGIC DIAGRAM
VERILOG SOURCE CODE
module logicgates(a,b,c);
input a;
input b;
output [6:0]c;
endmodule
Test Bench:
module tb_logicgates;
reg a,b;
wire [6:0]c;
logicgates U1(a,b,c);
initial begin
a=1’b0;b=1’b0;
#5
a=1’b1;b=1’b1;
#10
$stop();
end
endmodule
SIMULATION RESULT
RESULT
Thus the outputs of all logic gates are verified by synthesizing and simulating the verilog
code.
VIVA QUESTIONS
1. Which gate output will be a LOW for any case when one or more inputs are zero.
2. If a signal passing through a gate is inhibited by sending a low into one of the inputs, and
the output is HIGH the gate is
5. How many truth table entries are necessary for a four-input circuit?
LOGIC DIAGRAM
A1 A0 EN C3 C2 C1 C0
X X 0 0 0 0 0
0 0 1 0 0 0 1
0 1 1 0 0 1 0
1 0 1 0 1 0 0
1 1 1 1 0 0 0
input [1:0]a;
input en;
output [3:0]c;
reg [3:0]c;
always @(a,en)
begin
if(en= =1'b0)
c=4'b0000;
else
case(a)
2'b00: c=4'b0001;
2'b01: c=4'b0010;
2'b10: c=4'b0100;
2'b11: c=4'b1000;
endcase
end
endmodule
Test Bench:
module tb_decoder;
reg [1:0]a;
reg en;
wire [3:0]c;
decoder U1(a,c,en);
initial begin
a=2’b00;
en=1’b0;
#5
a=2’b00;
en=1’b1;
#5
a=2’b01;
#10
$stop();
end
endmodule
SIMULATION RESULT
RESULT
Thus the output of decoder is verified by synthesizing and simulating the verilog code.
VIVA QUESTIONS
1. What is decoder?
LOGIC DIAGRAM
ENCODER WITHOUT PRIORITY
input[7:0]a;
input en;
output reg[2:0]b;
always@( en,a )
begin
if(en= = 1’b0)
b=3’b000;
else
case(a)
default:b=3'bXXX;
endcase
end
endmodule
Test Bench:
module tb_encoder;
reg [7:0]a;
reg en;
wire [2:0]b;
encoder U1(a,b,en);
initial begin
en=1’b0;
a=8’b00000001;
#5
en=1’b1;
a=8’b00000010;
#5
a=8’b00001000;
#10
$stop();
end
endmodule
SIMULATION RESULT
ENCODER WITH PRIORITY
input [7:0]a;
input en;
output [2:0]b;
reg [2:0]b;
always@(a,en)
begin
if(en= =1'b0)
b<=3'b000;
else
case(1)
a[7]:b=3’b111;
a[6]:b=3’b110;
a[5]:b=3’b101;
a[4]:b=3’b100;
a[3]:b=3’b011;
a[2]:b=3’b010;
a[1]:b=3’b001;
a[0]:b=3’b000;
endcase
end
endmodule
Test Bench:
module tb_encoderpp();
reg [7:0]a;
reg en;
wire [2:0]b;
encoderpp u1 (en,a,b);
initial begin
en=1’b0;
a=8’b00000010;
#5
en=1’b1;
a=8,b00001000;
#10
a=8’b00101101;
#10
a=8’b01001011;
#20
$stop();
end
endmodule
SIMULATION RESULT
RESULT
Thus the output of encoder is verified by synthesizing and simulating the verilog code.
VIVA QUESTIONS
1. What is encoder?
4. Applications of encoder?
EXPERIMENT NO : 4
AIM
To develop the source code for multiplexer and demultiplexer by using verilog and obtain
the simulation, synthesis, place and route and implement into FPGA.
LOGIC DIAGRAM
EN S2 S1 S0 Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 Z
0 X X X X X X X X X X X 0
1 0 0 0 0 0 0 0 0 0 0 1 1
1 0 0 1 0 0 0 0 0 0 1 0 1
1 0 1 0 0 0 0 0 0 1 0 0 1
1 0 1 1 0 0 0 0 1 0 0 0 1
1 1 0 0 0 0 0 1 0 0 0 0 1
1 1 0 1 0 0 1 0 0 0 0 0 1
1 1 1 0 0 1 0 0 0 0 0 0 1
1 1 1 1 1 0 0 0 0 0 0 0 1
VERILOG SOURCE CODE
module mux (y,s,en,z);
input [7:0] y;
input [2:0] s;
input en;
output z;
reg z;
begin
if(en= =1'b0)
z=1'b0;
else
case(s)
3'b000 : z=y[0];
3'b001 : z=y[1];
3'b010 : z=y[2];
3'b011 : z=y[3];
3'b100 : z=y[4];
3'b101 : z=y[5];
3'b110 : z=y[6];
3'b111 : z=y[7];
endcase
end
endmodule
Test Bench:
module tb_mux;
reg [7:0]y;
reg [2:0]s;
reg en;
wire [2:0]z;
mux u1(y,s,en,z);
initial begin
en=1’b0;
y=8’b10101000;
s=3’b000;
#10
en=1’b1;
y=8’b11010100;
s=3’b001;
#5
s=3’b010;
#5
$stop();
end
endmodule
SIMULATION RESULT
DEMULTIPLEXER
TRUTH TABLE
I S2 S1 S0 Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
0 X X X 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 1
1 0 0 1 0 0 0 0 0 0 1 0
1 0 1 0 0 0 0 0 0 1 0 0
1 0 1 1 0 0 0 0 1 0 0 0
1 1 0 0 0 0 0 1 0 0 0 0
1 1 0 1 0 0 1 0 0 0 0 0
1 1 1 0 0 1 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0
VERILOG SOURCE CODE
module demux(a,y,s,en);
input [2:0]s;
input en,a;
always@(en,s,a)
begin
y=8’b00000000;
if(en = = 1'b0)
y = 8'b00000000;
else
case(s)
endcase
end
endmodule
Test Bench:
module tb_demux;
reg en,a;
reg [2:0]s;
wire [7:0]y;
demux u1(a,y,s,en);
initial begin
en=1’b0;
a=1’b1;
s=3’b000;
#5
en=1’b1;
a=1’b0;
s=3’b001;
#5
a=1’b1;
s=1’b010;
#10
$stop();
end
endmodule
SIMULATION RESULT
RESULT
Thus the output of Multiplexer and Demultiplexer is verified by synthesizing and
simulating in verilog code.
VIVA QUESTIONS
1. What is a multiplexer?
2. What is a de-multiplexer?
input [3:0] b;
output [3:0]g;
assign g[3]=b[3];
assign g[2]=b[2]^b[3];
assign g[1]=b[2]^b[1];
assign g[0]=b[1]^b[0];
endmodule
Test Bench:
module tb_btog;
reg[3:0]b;
wire[3:0]g;
btog gg(b,g);
initial begin
b=4’b0000;
#5 b=4’b0001;
-----
-----
#5 b=4’b1111;
#20
$stop;
end
endmodule
SIMULATION RESULT
RESULT
Thus the output of binary to gray converter is verified by synthesizing and simulating
the verilog code.
VIVA QUESTIONS
1. Realize the Boolean expression for binary to gray and gray to binary converter?
LOGIC DIAGRAM
VERILOG SOURCE CODE
module comparator(a,b,equal,greater,lower);
input [3:0]a;
input [3:0]b;
always @(a , b)
begin
if(a<b)
begin
equal=0;
greater=0;
lower=1;
end
else if(a>b)
begin
equal=0;
greater=1;
lower=0;
end
else
begin
equal=1;
greater=0;
lower=0;
end
end
endmodule
Test Bench:
module tb_comparator;
reg [3:0]a,b;
wire equal,greater,lower;
comparator u1(a,b,equal,grater,lower);
initial begin
a=4’b0000;
b=4’b0000;
#5
a=4’b0010;
b=4’b0001;
#5
$stop();
end
endmodule
SIMULATION RESULT
RESULT
Thus the output of 4 bit comparator is verified by synthesizing and simulating the
VERILOG code.
VIVA QUESTIONS
1.What is Magnitude Comparator?
2. To form a 12 – bit comparator how many 4-bit comparators are connected in cascaded form.
LOGIC DIAGRAM
module fuladdder(a,b,c,sum,carry);
input a,b,c;
output sum,carry;
assign sum=a^b^c;
assign carry=((a&b)|(b&c)|(c&a));
endmodule
Behavioral Modeling:
module FuladderBM(a,b,c,sum,carry);
input a,b,c;
always @(a or b or c)
begin
sum=a^b^c;
carry=((a&b)|(b&c)|(c&a));
end
endmodule
Structural Modeling:
module fastruct(a,b,c,sum,carry);
input a,b,c;
output sum,carry;
wire t1,t2,t3;
xor(t1,a,b);
xor(sum,t1,c);
and(t2,a,b);
and(t3,t1,c);
or(carry,t2,t3);
endmodule
Test Bench:
module tb_fulladder;
reg a,b,c;
wire sum,carry;
fulladder u1(a,b,c,sum,carry);
initial begin
a=1’b0;
b=1’b0;
c=1’b0;
#5
---
a=1’b1;
b=1’b1;
c=1’b1;
#5
$stop();
end
endmodule
SIMULATION RESULT
RESULT
Thus the outputs of full adder using three modeling styles are verified by synthesizing
and simulating the verilog code.
EXPERIMENT NO:8
DESIGN OF FLIP FLOPS (SR,JK,D,T)
AIM
To develop the source code for FLIP FLOPS by using VERILOG and obtained the
simulation, synthesis, place and route and implement into FPGA.
LOGIC DIAGRAM
SR FLIPFLOP
CLK S R Q QT+1
0 0 0 0
0 0 1 1
0 1 0 0
0 1 1 0
1 0 0 1
1 0 1 1
1 1 0 x
1 1 1 x
module srff(clk,rst,s,r,q,qb);
input clk,rst,s,r;
begin
if(rst= =1'b0)
begin
q=q;
qb=qb;
end
begin
q=q;
qb=qb;
end
begin
q=q;
qb= ~q;
end
begin
q=1'b1;
qb=~q;
end
begin
q=1'bX;qb=~q;
end
end
endmodule
Test Bench:
module tb_srff;
reg s,r,clk,rst;
wire q,qb;
srff u1(clk,rst,s,r,q,qb);
initial begin
clk=1’b0;
forever
#5
clk=~clk;
end
initial begin
rst=1’b0;s=1’b0;r=1’b0;
#5 rst=1’b1;s=1’b0;r=1’b1;
#5 s=1’b0;r=1’b1;
#5
$stop();
end
endmodule
SIMULATION RESULT
JK FLIPFLOP
CLK J K Q QT+1
0 0 0 0
0 0 1 1
0 1 0 0
0 1 1 0
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 0
module jkff(clk,rst,j,k,q,qb);
input clk,rst,j,k;
begin
if(rst= =1'b0)
begin
q=q;
qb=qb;
end
begin
q=q;
qb=qb;
end
q=q;
qb=~q;
end
begin
q=1'b1;
qb=~q;
end
begin
q=~q;
qb=~qb;
end
end
endmodule
Test Bench:
module tb_jkff;
reg j,k,clk,rst;
wire q,qb;
jkff u1(clk,rst,j,k,q,qb);
initial begin
clk=1’b0;
forever
#5
clk=~clk;
end
initial begin
rst=1’b0;j=1’b0;k=1’b0;
#5 rst=1’b1;j=1’b0;k=1’b1;
#5 j=1’b0;k=1’b1;
#5
$stop();
end
endmodule
SIMULATION RESULT
D FLIPFLOP TRUTH TABLE
CLK D Q QT+1
0 0 0
0 1 0
1 0 1
1 1 1
input d,clk,rst;
begin
if(rst= =1'b0)
begin
q=1'b0;
qb=~q;
end
begin
q=1'b0;
qb=~q;
end
else
begin
q=1'b1;
qb=~q;
end
end
endmodule
Test Bench:
module tb_dff;
reg d,clk,rst;
wire q,qb;
dff u1(d,clk,rst,q,qb);
initial begin
clk=1’b0;
forever
#5
clk=~clk;
end
initial begin
rst=1’b0;d=1’b0;
#5 rst=1’b1;d=1’b0;
#5 d=1’b0;
#5
$stop();
end
endmodule
SIMULATION RESULT
CLK T Q QT+1
0 0 0
0 1 1
1 0 1
1 1 0
input T,clk,rst;
begin
if(rst= =1'b0)
begin
q=1'b0;
qb=~q;
end
else if(T= =1'b0)
begin
q=q;
qb=~q;
end
else
begin
q=~q;
qb=~qb;
end
end
endmodule
Test Bench:
module tb_tff;
reg T,clk,rst;
wire q,qb;
tff u1(T,clk,rst,q,qb);
initial begin
clk=1’b0;
forever
#5
clk=~clk;
end
initial begin
rst=1’b0;T=1’b0;
#5 rst=1’b1;T=1’b0;
#5 T=1’b0;
#5
$stop();
end
endmodule
SIMULATION RESULT
RESULT
Thus the outputs of Flip flops are verified by synthesizing and simulating the verilog
code.
VIVA QUESTIONS
1. what is flip-flop?
LOGIC DIAGRAM
input x,clk,rst;
output z;
reg [1:0]state;
parameter a=2’b00 , b=2’b01 , c=2’b10 , d=2’b11;
always@(posedge clk )
begin
if(rst= =1’b0)
state=a;
else
case(state)
a:begin
if(x= =0)
state=a;
else
state=b;
end
b:begin
if(x= =0)
state=c;
else
state=b;
end
c:begin
if(x= =0)
state=a;
else
state=d;
end
d:begin
if(x= =0)
state=c;
else
state=b;
end
endcase
end
endmodule
Test Bench:
Module tb_fsm;
Reg clk,rst,x;
Wire z;
fsm hh(x,rst,clk,z);
Initial begin
Clk=1’b0;
Forever
#5 clk=~clk;
End
Initial begin
X=0;rst=0;
#10 x=1;rst=1;
#10 x=1;
#10 x=0;
#20;
$stop;
endendmodule
SIMULATION RESULT
RESULT
Thus the output of FSM are verified by synthesizing and simulating the verilog code.
VIVA QUESTIONS
1. What is FSM