这是正在使用的波纹进位加法器,但是,当 sub = 1 A = 4 B = 3 时,程序没有返回溢出,并且总和是 1100 而不是 0110。我们是否搞砸了进位行为?
module fullAdder(A, B, Cin, sum, Cout);
input A, B, Cin;
output sum, Cout;
assign sum = A^B^Cin;
assign Cout = (Cin&A) | (Cin&B) |(A&B);
endmodule
module RCA4bit(A, B, C0, sum, C1, overflow);
input [3:0] A;
input [3:0] B;
output [3:0] sum;
input C0;
output C1;
output overflow;
wire [2:0] carry;
fullAdder RCA1(A[0], B[0], C0, sum[0], carry[0]);
fullAdder RCA2(A[1], B[1], carry[0], sum[1], carry[1]);
fullAdder RCA3(A[2], B[2], carry[1], sum[2], carry[2]);
fullAdder RCA4(A[3], B[3], carry[2], sum[3], C1);
assign overflow = C1 ^ carry[2];
endmodule
module RCA4bit2cmp(A, B, sub, sum, C1, overflow);
input [3:0] A;
input [3:0] B;
output [3:0] sum;
input sub;
output C1;
output overflow;
wire [3:0]invB;
assign invB = sub?~B:B;
RCL4bit RC4(A, invB, sub, sum, C1, overflow);
endmodule