Half Adder Verilog
HALF ADDER
Design Code (Behavioral Style Using Boolean Expression)
Sum = a^b
Carry = ab
_________________________________________________________________________________
module Half_Adder(output Sum, Cout, input a, b);
wire Sum = a^b;
wire Cout = a&b;
endmodule
_________________________________________________________________________________
Test Bench
_________________________________________________________________________________
module HA_test;
reg a,b;
wire Sum, Cout;
Half_Adder HA1 (Sum, Cout, a, b);
initial
begin
$display("a b \t Sum, Carry"); // It display immediately at t=0 plus insert implicit new line at the end.
$monitor("%b %b \t %b %b", a,b,Sum,Cout); // It display value of a, b, Sum, Cout whenever there is change in their values.
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;
#5 $finish;
end
endmodule
_________________________________________________________________________________
Output
____________________________________
a b Sum, Carry
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
____________________________________
Test Bench 2
_________________________________________________________________________________
module HA_test;
reg a,b;
wire Sum, Cout;
Half_Adder HA1 (Sum, Cout, a, b);
initial
begin
$display("a b \t Sum, Carry");
$monitor("%b %b \t %b %b", a,b,Sum,Cout);
#5 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;
#5 $finish;
end
endmodule
_________________________________
a b Sum, Carry
x x x x
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
_________________________________
Since initial valuse a=0, b=0 is assigned after a delay of #5, defaults values of a, b, and resultant Sum and Cout is displayed at simulation time t=0. Default value of net data type is x.
Design Code (Behavioral Style Using Boolean Expression)
Sum = a^b
Carry = ab
_________________________________________________________________________________
module Half_Adder(output Sum, Cout, input a, b);
wire Sum = a^b;
wire Cout = a&b;
endmodule
_________________________________________________________________________________
Test Bench
_________________________________________________________________________________
module HA_test;
reg a,b;
wire Sum, Cout;
Half_Adder HA1 (Sum, Cout, a, b);
initial
begin
$display("a b \t Sum, Carry"); // It display immediately at t=0 plus insert implicit new line at the end.
$monitor("%b %b \t %b %b", a,b,Sum,Cout); // It display value of a, b, Sum, Cout whenever there is change in their values.
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;
#5 $finish;
end
endmodule
_________________________________________________________________________________
Output
____________________________________
a b Sum, Carry
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
____________________________________
Test Bench 2
_________________________________________________________________________________
module HA_test;
reg a,b;
wire Sum, Cout;
Half_Adder HA1 (Sum, Cout, a, b);
initial
begin
$display("a b \t Sum, Carry");
$monitor("%b %b \t %b %b", a,b,Sum,Cout);
#5 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;
#5 $finish;
end
endmodule
_________________________________
a b Sum, Carry
x x x x
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
_________________________________
Since initial valuse a=0, b=0 is assigned after a delay of #5, defaults values of a, b, and resultant Sum and Cout is displayed at simulation time t=0. Default value of net data type is x.
Comments
Post a Comment