Verilog Code For Basic Gates and Test Bench
Verilog Code For Basic Gates and Test Bench
Verilog Code For Basic Gates and Test Bench
input a;
input b;
output c;
end module
Test Bench
module nandg_tst_v;
reg a;
reg b;
wire c;
nandg uut (
.a(a),
.b(b),
.c(c)
);
initial
begin
a = 0;b = 0;
#100 a = 0;
b = 1;
#100 a = 1;
b = 0;
#100 a = 1;
b = 1;
end module
NOT GATE
module NOT_Gate(
input A,
output Y);
endmodule
TEST BENCH
module NOT_Gate_tb;
reg A;
wire Y;
initial
begin
$dumpfile("dump.vcd");
$dumpvars;
#1000 $finish;
end
initial begin
A='b0;
#50 $finish;
end
always #5 A=~A;
Endmodule
OR GATE
module orgate (a, b, y);
input a, b;
output y;
assign y = a | b;
endmodule
TEST BENCH
module orgate_tb;
wire y;
reg a, b;
initial
begin
$dumpfile("dump.vcd");
$dumpvars(1);
$monitor(a, b, y);
#1 a = 1'b0; b = 0'b0;
#2 a = 1'b0; b = 1'b1;
#3 a = 1'b1; b = 1'b0;
#4 a = 1'b1; b = 1'b1;
end
endmodule
AND GATE
module gate(a,b,y);
input a,b;
output y;
assign y=a&b;
endmodule
TEST BENCH
module testbench_gate;
reg a,b;
wire y;
gate DUT(a,b,y);
initial
begin
$dumpfile("gate.vcd);
$dumpvars(1,testbench_gate);
$mointor(a,b,y);
a=0;b=0;c=0;
#5 a=1;b=0;c=0;
#5 a=0;b=1;c=1;
#5 a=1;b=1;c=1;
#5 $finish;
end
endmodule
NAND GATE
module gate(a,b,y);
input a,b;
output y;
assign y=~(a|b);
endmodule
TEST BENCH
module gate_testbench;
reg a,b;
wire y;
gate DUT(a,b,y);
initial
begin
$dumpfile("gate.vcd");
$dumpvars(1,gate_testbench);
$monitor(a,b,y);
a=0;b=0;
#5 a=1; b=0;
#5 a=0; b=0;
#5 a=1; b=1;
#5 a=0; b=0;
#5 $finish;
end
endmodule
ALL GATES STRUCTURAL DESCRIPTION USING PRIMITIVE
GATES
module example(a,b,c,d,e,f,y);
input a,b,c,d,e,f;
output y;
wire t1,t2,t3,y;
nand #1 G1 (t1,a,b);
and #2 G2 (t2,c,~b,d);
nor #1 G3 (t3,e,f);
nand #1 G4 (y,t1,t2,t3);
endmodule
TEST BENCH
module example_testbench;
reg a,b,c,d,e,f;
wire y;
example DUT(a,b,c,d,e,f,y);
initial
begin
$dumpfile ("example.vcd");
$dumpvars (1,example_testbench);
$monitor($time,"a=%b,b=%b,c=%b,d=%b,e=%b,f=%b,y=%b",a,b,c,d,e,f,y);
a=0;b=0;c=0;d=0;e=0;f=0;
#5 a=1;b=1;c=0;d=1;e=0;f=0;
#5 a=1;b=0;c=1;d=1;e=0;f=0;
#5 a=1;b=1;c=0;d=1;e=0;f=0;
#5 a=1;b=0;c=0;d=1;e=0;f=0;
#5 $finish;
end
endmodule
EX-OR GATE DATA FLOW DESCRIPTION
module example1(a,b,y);
input a,b;
output y;
assign y=(~a&b)|(a&~b);
endmodule
TEST BENCH
module example1_testbench;
reg a,b;
wire y;
initial
begin
$dumpfile("example1.vcd");
$dumpvars(1,example1_testbench);
$monitor(a,b,y);
a=0;b=0;
#5 a=0; b=1;
#5 a=1; b=0;
#5 a=0; b=0;
#5 a=1; b=1;
#5 a=0; b=1;
$finish;
end
endmodule
STRUCTURAL DESCSRIPTION OF AND GATE
module and_gate(a,b,y);
input a,b;
output y;
wire f;
nand u1(f,a,b);
inv u2(f,y);
endmodule
module inv(a,y);
input a;
output y;
assign y=~a;
endmodule
TEST BENCH
module example1_testbench;
reg a,b;
wire y;
and_gate DUT(a,b,y);
initial
begin
$dumpfile("and_gate.vcd");
$dumpvars(1,example1_testbench);
$monitor(a,b,y);
a=0;b=0;
#5 a=1;b=0;
#5 a=1;b=1;
#5 a=0;b=1;
$finish;
end
endmodule
module fulladder(a,b,c,s,c);
input a,b,c;
output s,c;
assign s=a^b^c;
c=(a&b)|(b&c)|(c&a);
endmodule
TEST BENCH
module fulladder_testbench;
reg a,b,c;
wire s,c;
fulladder DUT(a,b,c,s,c);
initial
begin
$dumpfile("fulladder.vcd");
$dumpvars(1,fulladder_testbench);
$mointor(a,b,c,s,c);
a=0;b=0;c=0;
#5 a=0;b=0;c=1;
#5 a=0;b=1;c=0;
#5 a=1;b=0;c=0;
#5a=1;b=0;c=1;
#5a=1;b=1;c=1;
$finish;
end
endmodule
wire w1,w2,w3;
xor u1(sum,a,b,cin);
and u2(w1,a,b);
and u3(w2,b,cin);
and u4(w3,a,cin);
or u5(carryout,w1,w2,w3);
endmodule
TEST BENCH
module test_fulladderstruct;
reg a,b,cin;
wire sum,carryout;
initial
begin
$dumpfile("fulladder_stru.vcd");
$dumpvars(1,test_fulladderstruct);
$monitor(a,b,cin,sum,carryout);
a=0;b=0;cin=0;
#5 a=0;b=1;cin=0;
#5 a=0;b=1;cin=1;
#5 a=1;b=0;cin=0;
#5 a=1;b=0;cin=1;
#5 a=1;b=1;cin=0;
#5 a=1;b=1;cin=1;
end
endmodule
assign sum=(a^b);
assign carryout=(a&b);
endmodule
Test bench
module test_halfadder;
reg a,b;
wire sum,carryout;
initial
begin
$dumpfile("halfadder.vcd");
$dumpvars(1,test_halfadder);
$monitor(a,b,sum,carryout);
a=0;b=0;
#5 a=0;b=1;
#5 a=1;b=0;
#5 a=1;b=1;
end
endmodule
module fulladder_beh(a,b,cin,sum,carryout);
input a,b,cin;
output sum,carryout;
assign#20 sum=(a^b^cin);
endmodule
TEST BENCH
module fulladder_testbench;
reg a,b,cin;
wire sum,carryout;
fulladder_beh DUT(a,b,cin,sum,carryout);
initial
begin
$dumpfile("fulladder_beh.vcd");
$dumpvars(1,fulladder_testbench);
$monitor(a,b,cin,sum,carryout);
a=0;b=0;cin=0;
#5 a=0;b=0;cin=1;
#5 a=0;b=1;cin=0;
#5 a=0;b=1;cin=1;
#5 a=1;b=0;cin=0;
#5 a=1;b=0;cin=1;
#5 a=1;b=1;cin=0;
#5 a=1;b=1;cin=1;
end
endmodule
endmodule
endmodule
TEST BENCH
module Test_Full_Adder; // No need for Ports
reg a, b, c; // variables
$dumpfile("Full_Adder.vcd");
$dumpvars(1,Test_Full_Adder);
$monitor(a,b,c,cout,sum);
endmodule
endmodule
TEST BENCH
module Test_Full_Adder; // No need for Ports
fulladder DUT(a,b,c,cout,sum);
initial
$dumpfile("fulladder.vcd");
$dumpvars(1, Test_Full_Adder);
endmodule
wire w;
nand u1(w,a,b);
not u2(y,w);
endmodule
TEST BENCH
module testbench_structgate;
reg a,b;
wire y;
struct_gate DUT(a,b,y);
initial
begin
$dumpfile("struct_gate.vcd");
$dumpvars(1,testbench_structure);
$monitor(a,b,y);
end
endmodule
endmodule
reg sum,cout;
always@(a,b)
begin
sum=(a^b);
cout=(a&b);
end
endmodule
TEST BENCH
reg a, b, c; // variables
$dumpfile("Full_Adder.vcd");
$dumpvars(1,Test_Full_Adder);
endmodule
assign sum=(a^b);
assign carry=(a&b);
endmodule
and g1(w1,a,b);
and g2(w2,a,b);
or g3(sum,w1,w2);
not g4(a,~a);
not g5(b,~b);
endmodule
input a,b;
output sum,carry;
reg sum,carry;
always @(a,b,sum,carry)
begin
sum=(a^b);
carry=(a&b);
end
endmodule
TEST BENCH
module testbench_binaryadder;
reg a,b;
wire sum,carry;
initial
begin
$dumpfile("binary_adder.vcd");
$dumpvars(1,testbench_binaryadder);
$monitor(a,b,sum,carry);
#5 a=1; b=0;
#5 a=0; b=1;
#5 a=1; b=1;
end
endmodule
input [3:0] A, B;
input cin;
output [3:0] S;
output cout;
endmodule
reg[3:0] a,b;
reg cin;
wire sum,carryout;
adder4 DUT(Sum,carryout,a,b,cin);
initial
begin
$dumpfile("adder4.vcd");
$dumpvars(0,testbench_4bitadder);
$monitor(a,b,cin,sum,carryout);
a=0000; b=0000;cin=0;
#5 a=0000; b=0000;
#5 a=0001; b=0001;
#5 $finish;
end
endmodule
input [3:0] a, b;
input cin;
output [3:0]sum;
output carryout;
endmodule
input [3:0]a,b;
input cin;
output carryout;
output [3:0]c;
wire c1,c2,c3;
fulladder FA1(a[0],b[0],cin,sum[0],c1);
fulladder FA2(a[1],b[1],c1,sum[1],c2);
fulladder FA3(a[2],b[2],c2,sum[2],c3);
fulladder FA4(a[3],b[3],c3,sum[3],carryout);
endmodule
input a,b,cin;
output sum,carryout;
assign sum=(a^b^cin);
assign carryout=(a&b)|(b&cin)|(a&cin);
endmodule
reg carryout,sum;
always @ (a,b,cin)
begin
{sum,carryout}=a+b+cin;
Sum=(a^b^cin);
Carryout=(a&b)|(b&cin)|(a&cin);
end
endmodule
Test BENCH
module testbench_4bitadder;
reg[3:0]a,b;
reg cin;
wire [3:0]sum;
wire carryout;
initial
begin
$dumpfile("fulladder.vcd");
$dumpvars(0,testbench_4bitadder);
$monitor(a,b,cin,sum,carryout);
a=4'b0000; b=4'b0000;cin=0;
#5 a=4'b0000; b=4'b0001;
#5 a=4'b0010; b=4'b0001;
#5 a=4'b0101; b=4'b0101;
#5 a=4'b1001; b=4'b0011;
#5 $finish;
end
endmodule
input sel;
output out;
endmodule
module muxtest;
reg A,B;
reg S;
wire F;
mux2to1 M(A,B,S,F);
initial
begin
$dumpfile ("mux2to1.vcd");
$dumpvars (0,muxtest);
A=0;B=0; S=0;
#5 A=1;S=0; B=0;
#5 A=0;S=1; B=1;
#5 $finish;
end
endmodule
input i0,i1,i2,i3;
output out;
input s0,s1;
wire W1,W2;
mux2to1 M0(i0,i1,s0,W1);
mux2to1 M1(i2,i3,s0,W2);
mux2to1 M2(W1,W2,s1,out);
endmodule
input A,B;
input sel;
output out;
endmodule
input s0,s1;
output out;
endmodule
TEST BENCH
module muxtest;
reg i0,i1,i2,i3;
reg s0,s1;
wire out;
mux4to1 M(i0,i1,i2,i3,s0,s1,out);
initial
begin
$dumpfile ("mux4to1.vcd");
$dumpvars (0,muxtest);
$monitor (i0,i1,i2,i3,s0,s1,out);
#5 i3=0;i2=0;i1=1; i0=0;s0=0;s1=1;
wire [1:0] t;
mux2to1 M0 (in[1:0],sel[0],t[0]);
mux2to1 M1 (in[3:2],sel[0],t[1]);
mux2to1 M2 (t,sel[1],out);
endmodule
Test bench
module mux4to1_testbench:
reg [3:0]in;
reg[1:0] sel;
wire out;
mux4to1 DUT(A,S,F);
initial
begin
$dumpfile ("mux4to1.vcd");
$dumpvars (0,muxtest);
#5 A=4'b0001; S=2'b00;
#5 S=2'b01;A=4'b0010;
#5 S=2'b10;A=4'b0100;
#5 S=2'b11;A=4'b1000;
#5 $finish;
end
endmodule