From 47cfb7ba2df247310e2160e4e53d1843d4e4d1c8 Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Sun, 8 Jan 2017 22:45:04 -0600 Subject: Implement subtraction in terms of addition --- alu.v | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'alu.v') 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 -- cgit v1.2.3