You are on page 1of 3

module fourbitadder(a,b,cin,sum,cout);

input [3:0]a;

input [3:0]b;

output [3:0]sum;

input cin;

output cout;

wire c0,c1,c2;

fulladder1 fa0(a[0],b[0],cin,sum[0],c0);

fulladder1 fa1(a[1],b[1],c0,sum[1],c1);

fulladder1 fa2(a[2],b[2],c1,sum[2],c2);

fulladder1 fa3(a[3],b[3],c2,sum[3],cout);

endmodule

module fulladder1(input a,input b,input cin,output sum,output cy);

wire s1,c1,c2;

half_adder ha1(a,b,s1,c1);

half_adder ha2(s1,cin,sum,c2);

or or1(cy,c1,c2);

endmodule

module half_adder(input a,b,output sum,carry);

xor(sum,a,b);

and(carry,a,b);

endmodule
Test beanch:

module tb_fulladder_4();

reg[3:0]a;

reg[3:0]b;

reg cin;

wire[3:0] sum;

wire cout;

fourbitadder tb_fulladder(a,b,cin,sum,cout);

initial

begin

#10 a=4'b0000;b=4'b0000;cin=0;

#10 a=4'b0010;b=4'b0110;cin=0;

#20 $finish;

end

endmodule