verilog实现四位全加器(基于一位全加器)

代码:
module add_4(input [3:0] a,input[3:0]b,input ci,output[3:0]s,output co
);
wire[3:0] count;
add i0(a[0],b[0],ci,s[0],count[0]);
add i1(a[1],b[1],count[0],s[1],count[1]);
add i2(a[2],b[2],count[1],s[2],count[2]);
add i3(a[3],b[3],count[2],s[3],count[3]);
assign co=count[3];

endmodule

module add(input a ,input b, input ci,output s,output co);
assign s=abci;
assign co=(a&b)|(b&ci)|(a&ci);
endmodule

测试代码:
module tb_add_4;

// Inputs
reg [3:0] a;
reg [3:0] b;
reg ci;

// Outputs
wire [3:0] s;
wire co;

// Instantiate the Unit Under Test (UUT)
add_4 uut (
	.a(a), 
	.b(b), 
	.ci(ci), 
	.s(s), 
	.co(co)
);

always #5 ci=~ci;
initial begin
a=4'b0000;b=4'b0000;ci=0;
repeat(16)
	#10 b=b+1;
	end
initial begin
a=4'b0000;b=4'b0000;ci=0;
repeat(16)
    #10 a=a+1;
	end
	endmodule

你可能感兴趣的:(verilog实现四位全加器(基于一位全加器))