summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2017-01-08 22:45:04 -0600
committerBobby Bingham <koorogi@koorogi.info>2017-01-08 22:45:04 -0600
commit47cfb7ba2df247310e2160e4e53d1843d4e4d1c8 (patch)
tree0627a39acd1996a00e2cbd2458afcfe037897f3d
parent67b51195967009edbcf26767f9f3e8e1dd2db780 (diff)
Implement subtraction in terms of addition
-rw-r--r--alu.v14
1 files changed, 6 insertions, 8 deletions
diff --git a/alu.v b/alu.v
index 5fbb8ba..63812a7 100644
--- a/alu.v
+++ b/alu.v
@@ -26,16 +26,17 @@ module alu (
// 1111: result = x >> y (barrel shift)
wire [16:0] sum;
- wire [16:0] diff;
wire [16:0] prod;
wire [16:0] quot;
wire [15:0] anded;
wire [15:0] ored;
wire [15:0] xored;
wire [15:0] shifted;
+ wire issum;
- assign sum = x + y;
- assign diff = x - y;
+ assign issum = ~|(op[3:1]);
+
+ assign sum = x + (op[0] ? ~y + 1 : y);
assign prod = x * y;
assign quot = x / y;
assign anded = x & y;
@@ -51,8 +52,7 @@ module alu (
);
assign result =
- op == 4'b0000 ? sum[15:0] :
- op == 4'b0001 ? diff[15:0] :
+ issum ? sum[15:0] :
op == 4'b0010 ? prod[15:0] :
op == 4'b0011 ? quot[15:0] :
op == 4'b0100 ? anded :
@@ -62,8 +62,6 @@ module alu (
shifted;
assign zero = ~(|result);
- assign carry =
- op == 4'b0000 ? sum[16] :
- op == 4'b0001 ? diff[16] : 1'b0;
+ assign carry = issum ? sum[16] : 1'b0;
endmodule