aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlavian Kaufmann <flavian@flaviankaufmann.ch>2024-05-01 12:40:49 +0200
committerFlavian Kaufmann <flavian@flaviankaufmann.ch>2024-05-01 12:40:49 +0200
commit766273a6a50d57777e455d07a015300255becb6d (patch)
treee283ed5c50a9c8af7d0624c0bc7440b0ccda0af9
parent1ee5fc13995ee1383b0b75a19003b08fe33cfa54 (diff)
downloadriscv_cpu-766273a6a50d57777e455d07a015300255becb6d.tar.gz
riscv_cpu-766273a6a50d57777e455d07a015300255becb6d.zip
alu
-rw-r--r--src/alu.v47
-rw-r--r--src/arithmetic_unit.v24
-rw-r--r--src/logic_unit.v17
-rw-r--r--src/shift_unit.v18
4 files changed, 106 insertions, 0 deletions
diff --git a/src/alu.v b/src/alu.v
new file mode 100644
index 0000000..61bb2fb
--- /dev/null
+++ b/src/alu.v
@@ -0,0 +1,47 @@
+module alu #(
+ parameter N = 32
+)(
+ input [N-1:0] A, B,
+ input [3:0] OP, // OP[3:2] 00: ARITHMETIC, 01: LOGIC, 10: SHIFT
+ output reg [N-1:0] RESULT,
+ output ZERO,
+ output OVERFLOW
+);
+
+wire [N-1:0] arithmetic_result, logic_result, shift_result;
+
+arithmetic_unit #(.N(N)) au (
+ .A(A),
+ .B(B),
+ .OP(OP[1:0]),
+ .RESULT(arithmetic_result),
+ .OVERFLOW(overflow)
+);
+
+logic_unit #(.N(N)) lu (
+ .A(A),
+ .B(B),
+ .OP(OP[1:0]),
+ .RESULT(logic_result)
+);
+
+shift_unit #(.N(N)) su (
+ .A(A),
+ .SHAMT(B[clog2(N):0]),
+ .OP(OP[1:0]),
+ .RESULT(shift_result)
+);
+
+always @ (*) begin
+ case (OP[3:2])
+ 2'b00: RESULT <= arithmetic_result;
+ 2'b01: RESULT <= logic_result;
+ 2'b10: RESULT <= shift_result;
+ endcase
+end
+
+assign OVERFLOW = OP[3:2] == 2'b00 ? overflow : 0;
+
+assign ZERO = ~|RESULT;
+
+endmodule
diff --git a/src/arithmetic_unit.v b/src/arithmetic_unit.v
new file mode 100644
index 0000000..be087a7
--- /dev/null
+++ b/src/arithmetic_unit.v
@@ -0,0 +1,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
diff --git a/src/logic_unit.v b/src/logic_unit.v
new file mode 100644
index 0000000..5ada9c9
--- /dev/null
+++ b/src/logic_unit.v
@@ -0,0 +1,17 @@
+module logic_unit #(
+ parameter N = 32
+)(
+ input [N-1:0] A, B,
+ input [1:0] OP, // 00: AND, 01: OR, 10: XOR
+ output reg [N-1:0] RESULT
+);
+
+ always @ (*) begin
+ case (OP)
+ 2'b00: RESULT <= A & B;
+ 2'b01: RESULT <= A | B;
+ 2'b10: RESULT <= A ^ B;
+ endcase
+ end
+
+endmodule
diff --git a/src/shift_unit.v b/src/shift_unit.v
new file mode 100644
index 0000000..55b6add
--- /dev/null
+++ b/src/shift_unit.v
@@ -0,0 +1,18 @@
+module shift_unit #(
+ parameter N = 32
+)(
+ input [N-1:0] A,
+ input [clog2(N):0] SHAMT,
+ input [1:0] OP, // 00: SLL, 01: SRL, 11: SRA
+ output reg [N-1:0] RESULT
+);
+
+always @ (*) begin
+ case (OP)
+ 2'b00: RESULT <= A << SHAMT;
+ 2'b01: RESULT <= A >> SHAMT;
+ 2'b11: RESULT <= A >>> SHAMT;
+ endcase
+end
+
+endmodule