blob: 64ec0f9c4110fd0f8a13ad28b95a682362d7ff99 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
module arithmetic_unit #(
parameter N = 32
)(
input [N-1:0] A, B,
input [1:0] OP, // 00: ADD, 01: SUB, 11: SLT
output [N-1:0] RESULT
);
wire [N-1:0] b, sum;
wire cin, altb, overflow;
assign b = OP[0] ? ~B : B;
assign cin = OP[0];
assign sum = A + b + cin;
assign overflow = ~(A[N-1] ^ B[N-1] ^ OP[0]) & (A[N-1] ^ sum[N-1]);
assign altb = overflow ^ sum[N-1];
assign RESULT = OP[1] ? {{(N-1){1'b0}}, altb} : sum;
endmodule
|