aboutsummaryrefslogtreecommitdiff
path: root/src/arithmetic_unit.v
blob: be087a7e2f4c50cbb285eae6ae88af94967de10a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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,
  output OVERFLOW
);

wire [N-1:0] b, sum;
wire cin, altb;

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