Half and Full Adder Implementation in Verilog (DSD)
Half and Full Adder Implementation in Verilog (DSD)
Truth table
A B SUM CARRY
0
0
1
1
0
1
0
1
0
1
1
0
module ha(sum,cout,a,b);
input a,b;
output sum,cout;
xor (sum,a,b);
and (cout,a,b);
endmodule
module testha;
reg a,b;
wire sum,cout;
ha h0(sum,cout,a,b);
initial
begin
a=1'b0;b=1'b0;
#5 a=1'b0;b=1'b1;
#5 a=1'b1;b=1'b0;
#5 a=1'b1;b=1'b1;
end
endmodule
WAVE FORM
0
0
0
1
Description
Description
and#3 (cout,a,b);
endmodule
WAVE FORM
Description
--------------------------------------------------------------------------------------------
0
0
0
0
1
0
0
1
1
0
0
1
1
0
1
0
0
0
1
0
1
1
1
0
1
1
1
0
1
Testbench
module fl(sum,cout,a,b,cin);
input a,b,cin;
output sum,cout;
wire w1,w2,w3;
ha a1(w1,w2,a,b);
ha a2(sum,w3,w1,cin);
or (cout,w2,w3);
endmodule
module testfl;
reg a,b,cin;
wire sum,cout;
fl f0(sum,cout,a,b,cin);
initial
begin
a=1'b0;b=1'b0;cin=1'b0;
#5 a=1'b0;b=1'b0;cin=1'b1;
#5 a=1'b0;b=1'b1;cin=1'b0;
#5 a=1'b0;b=1'b1;cin=1'b1;
#5 a=1'b1;b=1'b0;cin=1'b0;
#5 a=1'b1;b=1'b0;cin=1'b1;
#5 a=1'b1;b=1'b1;cin=1'b0;
#5 a=1'b1;b=1'b1;cin=1'b1;
end
endmodule
Description
0
0
1
1
1
1
--------------------------------------------------------------------------------------------
Testbench
module testfulladder;
reg a,b,cin;
wire sum,carry;
fulladder f0(sum,cout,a,b,cin);
initial
begin
a=1'b0;b=1'b0;cin=1'b0;
#5 a=1'b0;b=1'b0;cin=1'b1;
#5 a=1'b0;b=1'b1;cin=1'b0;
#5 a=1'b0;b=1'b1;cin=1'b1;
#5 a=1'b1;b=1'b0;cin=1'b0;
#5 a=1'b1;b=1'b0;cin=1'b1;
#5 a=1'b1;b=1'b1;cin=1'b0;
#5 a=1'b1;b=1'b1;cin=1'b1;
end
endmodule
output sum,carry;
wire w1,w2,w3;
xor#3(w1,a,b);
xor#3(sum,w1,cin);
and#3(w2,w1,cin);
and#3(w3,a,b);
or#3(carry,w2,w3);
endmodule