半加器由两个一位输入相加,输出一个结果位和进位,没有进位输入的加法器电路。
S = A ^ B
C = A & B
module half_adder(
input a,
input b,
output sum,
output c_out
);
assign sum = a^b;
assign cout = a&b;
endmodule
由两个1位的加数和一个进位作为输入,输出一个结果位和进位,与半加器相比,全加器不只考虑本位计算结果是否有进位,也考虑上一位对本位的进位。
Si =Ai ^ Bi^ Ci-1
Ci = AiBi + AiCi-1 + BiCi-1 = AiBi + (Ai+Bi)Ci-1
module full_adder(
input a,
input b,
input c_in,
output sum,
output c_out
);
wire sum1;
wire c_out1,c_out2;
half_adder half_adder1(.a(a),.b(b),.sum(sum1),.c_out(c_out1));
half_adder half_adder2(.a(co),.b(sum1),.sum(sum),.c_out(c_out2));
assign c_out = c_out1|c_out2;
endmodule
Si =Ai ^ Bi^ Ci-1
Ci = AiBi + AiCi-1 + BiCi-1 = AiBi + (Ai+Bi)Ci-1
module full_adder (
input a,
input b,
input c_in,
output sum,
output c_out
);
wire S1, T1, T2, T3;
xor x1 (S1, a, b);
xor x2 (Sum, S1, c_in);
and A1 (T3, a, b );
and A2 (T2, b, c_in);
and A3 (T1, a, c_in);
or O1 (c_out, T1, T2, T3 );
endmodule
由4个1位全加器串联形成4位加法器,上一全加器的进位输出端作为下一全加器的进位输入端
S0=A0 ^ B0^ Cin
S1 =A1^ B1^ C0
S2 =A2 ^ B2^ C1
S3 =A3 ^ B3^ C2
C0 = A0B0+ A0Cin + B0Cin
C1 = A1B1 + A1C0 + B1C0
C2 = A2B2+ A2C1 + B2C1
C3= A3B3 + A3C2 + B3C2
module add_4 (
input [3:0]a,
input [3:0]b,
input c_in,
output [3:0] sum,
output c_out
);
wire [3:0] c_tmp;
full_adder i0 ( a[0], b[0], c_in, sum[0], c_tmp[0]);
full_adder i1 ( a[1], b[1], c_tmp[0], sum[1], c_tmp[1] );
full_adder i2 ( a[2], b[2], c_tmp[1], sum[2], c_tmp[2] );
full_adder i3 ( a[3], b[3], c_tmp[2], sum[3], c_tmp[3] );
assign c_out = c_tmp[3];
endmodule
module add_4(
input [3:0] a,
input [3:0] b,
input c_in,
output [3:0] sum,
output c_out
);
assign {c_out,sum} = a+b+c_in;
endmodule
使用了拼接运算符——{a,b}
对普通的全加器进行改良设计的并行加法器,主要针对普通全加器串联互相进位产生延迟而进行改良
Si =Ai ^ Bi^ Ci-1
Ci = AiBi + AiCi-1 + BiCi-1 = AiBi + (Ai+Bi)Ci-1
令 Gi=Ai*Bi ; Pi=Ai+Bi 代入Ci =AiBi + (Ai+Bi)Ci-1
得 Ci=Gi+Gi*Ci-1
C0 = C_in
C1=G0 + P0·C0
C2=G1 + P1·C1 = G1 + P1·G0 + P1·P0 ▪C0
C3=G2 + P2·C2 = G2 + P2·G1 + P2·P1·G0 + P2·P1·P0·C0
C4=G3 + P3·C3 = G3 + P3·G2 + P3·P2·G1 + P3·P2·P1·G0 + P3·P2·P1·P0·C0
C_out=C4
module fastadd_4(
input[3:0] a,
input[3:0] b,
input c_in,
output[3:0] sum,
output c_out
);
wire[4:0] g,p,c;
assign c[0]=c_in;
assign p=a^b;
assign g=a&b;
assign c[1]=g[0]|(p[0]&c[0]);
assign c[2]=g[1]|(p[1]&(g[0]|(p[0]&c[0])));
assign c[3]=g[2]|(p[2]&(g[1]|(p[1]&(g[0]|(p[0]&c[0])))));
assign c[4]=g[3]|(p[3]&(g[2]|(p[2]&(g[1]|(p[1]&(g[0]|(p[0]&c[0])))))));
assign sum=p^c[3:0];
assign c_out=c[4];
endmodule
仿真测试代码:
`timescale 1ns/1ps
module add_top;
reg [3:0] a,b;
reg c_in;
wire [3:0] sum;
wire c_out;
fastadd_4 x(.a(a),.b(b),.c_in(c_in),.sum(sum),.c_out(c_out));
initial begin
a=4'b0000;
b=4'b0000;
c_in=0;
end
always #5 c_in=c_in+1;
always #10 a=a+1;
always #160 b=b+1;
endmodule