Verilog_Lab_File (2)
Verilog_Lab_File (2)
of
DIGITAL SYSTEM DESIGN USING VERILOG
Name :- Bipin Kumar
Entry no. :- 22BEC038
Faculty Guide:- Dr. Vijay Kumar Sharma
Semester :- 5th
INDEX
EXPERIMENT 1:- AND gate
EXPERIMENT 2:- XNOR gate
EXPERIMENT 3:- Half Adder
EXPERIMENT 4:- Full Adder
EXPERIMENT 5:- 2x1 mux
EXPERIMENT 6:- 4x1 mux
EXPERIMENT 7:- 2x4 decoder
EXPERIMENT 8:- 3x8 decoder
EXPERIMENT 9:- D Flip-Flop
EXPERIMENT 10:- JK Flip-Flop
EXPERIMENT 11:- T Flip-Flop
1. AND gate
module and_gate(
input a,
input b,
output out );
assign out=a&b;
endmodule
module and_tb;
reg a,b;
wire out;
and_gate uut(.a(a),.b(b),.out(out));
ini al begin
$dumpfile("dump.vcd"); $dumpvars;
a=0;
b=0;
#10 a=1;b=0;
#10 a=0;b=1;
#10 a=1;b=1;
#10 $finish;
end
endmodule
1.3. Output Waveforms:
2. XNOR gate
2.1. Verilog Code:
module xnor_gate(
input a,
input b,
output out );
assign out=~(a^b);
endmodule
module xnor_tb;
reg a,b;
wire out;
xnor_gate uut(.a(a),.b(b),.out(out));
ini al begin
$dumpfile("dump.vcd"); $dumpvars;
a=0;
b=0;
#10 a=1;b=0;
#10 a=0;b=1;
#10 a=1;b=1;
#10 $finish;
end
endmodule
2.3. Output Waveforms:
3. Half Adder
3.1. Verilog Code:
module half_adder(
input a,b,
output sum,cout);
assign sum=a^b;
assign cout=a&b;
endmodule
module half_adder_tb;
reg in1,in2;
wire sum1,cout1;
half_adder uut(.a(in1),.b(in2),.sum(sum1),.cout(cout1));
ini al begin
$dumpfile("dump.vcd"); $dumpvars;
in1=0;in2=0;
#10 in1=0;in2=1;
#10 in1=1;in2=0;
#10 in1=1;in2=1;
#10 $finish;
end
endmodule
module full_adder(
input a,b,cin,
output sum,cout);
assign sum=a^b^cin;
assign cout=(a&b)|(b&cin)|(cin&a);
endmodule
module half_adder_tb;
reg in1,in2,cin1;
wire sum1,cout1;
full_adder uut(.a(in1),.b(in2),.cin(cin1),.sum(sum1),.cout(cout1));
ini al begin
$dumpfile("dump.vcd"); $dumpvars;
in1=0;in2=0;cin1=0;
#10 in1=0;in2=0;cin1=1;
#10 in1=0;in2=1;cin1=0;
#10 in1=0;in2=1;cin1=1;
#10 in1=1;in2=0;cin1=0;
#10 in1=1;in2=0;cin1=1;
#10 in1=1;in2=1;cin1=0;
#10 in1=1;in2=1;cin1=1;
#10 $finish;
end
endmodule
module mux_2_1(
input a,b,sel,
output reg out);
always @(*)
begin
if(sel)
out=b;
else
out=a;
end
endmodule
5.2. Test Bench:
module mux_2_1_tb;
reg a1,a2,sel1;
wire out1;
mux_2_1 uut(.a(a1),.b(a2),.sel(sel1),.out(out1));
ini al begin
$dumpfile("dump.vcd"); $dumpvars;
a1=0;a2=0;sel1=0;
#10 a1=0;a2=0;sel1=1;
#10 a1=0;a2=1;sel1=0;
#10 a1=0;a2=1;sel1=1;
#10 a1=1;a2=0;sel1=0;
#10 a1=1;a2=0;sel1=1;
#10 a1=1;a2=1;sel1=0;
#10 a1=1;a2=1;sel1=1;
#10 $finish;
end
endmodule
endmodule
6.2. Test Bench:
module mux_4_1_tb;
reg a1, b1, c1, d1, sel0, sel1;
wire out1;
mux_4_1 uut(
.a(a1),
.b(b1),
.c(c1),
.d(d1),
.sel0(sel0),
.sel1(sel1),
.out(out1)
);
ini al begin
$dumpfile("dump.vcd"); $dumpvars;
a1 = 0; b1 = 0; c1 = 0; d1 = 0; sel0 = 0; sel1 = 0;
#10 a1 = 0; b1 = 1; c1 = 0; d1 = 0; sel0 = 1; sel1 = 0;
#10 a1 = 0; b1 = 0; c1 = 1; d1 = 0; sel0 = 0; sel1 = 1;
#10 a1 = 0; b1 = 0; c1 = 0; d1 = 1; sel0 = 1; sel1 = 1;
#10 a1 = 1; b1 = 0; c1 = 0; d1 = 0; sel0 = 0; sel1 = 0;
#10 a1 = 0; b1 = 1; c1 = 0; d1 = 0; sel0 = 1; sel1 = 0;
#10 a1 = 0; b1 = 0; c1 = 1; d1 = 0; sel0 = 0; sel1 = 1;
#10 a1 = 0; b1 = 0; c1 = 0; d1 = 1; sel0 = 1; sel1 = 1;
#10 $finish;
end
endmodule
6.3. Output Waveforms:
7. 2x4 decoder
7.1. Verilog Code:
module decoder_2x4(
input [1:0] in,
input enable,
output reg [3:0] out
);
endmodule
7.2. Test Bench:
module decoder_2x4_tb;
reg [1:0] in_tb;
reg enable_tb;
wire [3:0] out_tb;
decoder_2x4 uut (
.in(in_tb),
.enable(enable_tb),
.out(out_tb)
);
ini al begin
$dumpfile("decoder.vcd");
$dumpvars(0, decoder_2x4_tb);
$finish;
end
endmodule
7.3. Output Waveforms:
8. 3x8 decoder
8.1. Verilog Code:
module decoder_3x8(
input [2:0] in,
input enable,
output reg [7:0] out
);
endmodule
8.2. Test Bench:
module decoder_3x8_tb;
reg [2:0] in_tb;
reg enable_tb;
wire [7:0] out_tb;
decoder_3x8 uut (
.in(in_tb),
.enable(enable_tb),
.out(out_tb)
);
ini al begin
$dumpfile("decoder.vcd");
$dumpvars(0, decoder_3x8_tb);
$finish;
end
endmodule
8.3. Output Waveforms:
9. D Flip-Flop
9.1. Verilog Code:
module d_flip_flop(
input d,
input clk,
input reset,
output reg q
);
endmodule
9.2. Test Bench:
module d_flip_flop_tb;
reg d_tb;
reg clk_tb;
reg reset_tb;
wire q_tb;
d_flip_flop uut (
.d(d_tb),
.clk(clk_tb),
.reset(reset_tb),
.q(q_tb)
);
ini al begin
$dumpfile("d_flip_flop.vcd");
$dumpvars(0, d_flip_flop_tb);
clk_tb = 0;
reset_tb = 0; d_tb = 0; #10;
reset_tb = 1; #10;
reset_tb = 0; d_tb = 1; #10;
d_tb = 0; #10;
d_tb = 1; #10;
$finish;
end
endmodule
9.3. Output Waveforms:
10. JK Flip-Flop
10.1. Verilog Code:
module jk_flip_flop(
input j,
input k,
input clk,
input reset,
output reg q
);
endmodule
10.2. Test Bench:
module jk_flip_flop_tb;
reg j_tb;
reg k_tb;
reg clk_tb;
reg reset_tb;
wire q_tb;
jk_flip_flop uut (
.j(j_tb),
.k(k_tb),
.clk(clk_tb),
.reset(reset_tb),
.q(q_tb)
);
ini al begin
$dumpfile("jk_flip_flop.vcd");
$dumpvars(0, jk_flip_flop_tb);
clk_tb = 0;
reset_tb = 1; j_tb = 0; k_tb = 0; #10;
reset_tb = 0; j_tb = 0; k_tb = 0; #10;
j_tb = 0; k_tb = 1; #10;
j_tb = 1; k_tb = 0; #10;
j_tb = 1; k_tb = 1; #10;
$finish;
end
endmodule
10.3. Output Waveforms:
11. T Flip-Flop
11.1. Verilog Code:
module t_flip_flop(
input t,
input clk,
input reset,
output reg q
);
endmodule
11.2. Test Bench:
module t_flip_flop_tb;
reg t_tb;
reg clk_tb;
reg reset_tb;
wire q_tb;
t_flip_flop uut (
.t(t_tb),
.clk(clk_tb),
.reset(reset_tb),
.q(q_tb)
);
ini al begin
$dumpfile("t_flip_flop.vcd");
$dumpvars(0, t_flip_flop_tb);
clk_tb = 0;
reset_tb = 1; t_tb = 0; #10;
reset_tb = 0; t_tb = 0; #10;
t_tb = 1; #10;
t_tb = 1; #10;
t_tb = 0; #10;
$finish;
end
endmodule
11.3. Output Waveforms: