adder-subtractor
An adder-subtractor can be built from an adder by optionally negating one of the inputs, which is equivalent to inverting the input then adding 1. The net result is a circuit that can do two operations: (a + b + 0) and (a + ~b + 1). See Wikipedia if you want a more detailed explanation of how this circuit works.
代码如下:
input [31:0] a,
input [31:0] b,
input sub,
output [31:0] sum
);
wire cin;
wire cout1,cout2;
wire [15:0]sum1,sum2;
wire [31:0] b_n;
assign b_n = b ^ {32{sub}};
add16 add1 (.a(a[15:0]),.b(b_n[15:0]),.cin(sub),.sum(sum1),.cout(cout1));
add16 add2 (.a(a[31:16]),.b(b_n[31:16]),.cin(cout1),.sum(sum2),.cout(cout2));
assign sum = {sum2,sum1};
endmodule

浙公网安备 33010602011771号