Skip to content

Commit dbfbff1

Browse files
committed
feat: add alu module impl
1 parent 5276816 commit dbfbff1

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

rtl/tc_l3/src/main/scala/core/ALU.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,40 @@ object ALU {
1919
val ALU_COPY_B = 11.U(ALUOperLen.W)
2020
val ALU_XXX = 15.U(ALUOperLen.W)
2121
}
22+
23+
class ALUIo(implicit p: Parameters) extends Bundle {
24+
val A = Input(UInt(xlen.W))
25+
val B = Input(UInt(xlen.W))
26+
val alu_op = Input(UInt(4.W))
27+
val out = Output(UInt(xlen.W))
28+
val sum = Output(UInt(xlen.W))
29+
}
30+
31+
class ALU(implicit p: Parameters) extends Module {
32+
val sum = io.A + Mux(io.alu_op(0), -io.B, io.B)
33+
val cmp = Mux(io.A(xlen - 1) === io.B(xlen - 1), sum(xlen - 1), Mux(io.alu_op(1), io.B(xlen - 1), io.A(xlen - 1)))
34+
val shamt = io.B(4, 0).asUInt
35+
val shin = Mux(io.alu_op(3), io.A, Reverse(io.A))
36+
val shiftr = (Cat(io.alu_op(0) && shin(xlen - 1), shin).asSInt >> shamt)(xlen - 1, 0)
37+
val shiftl = Reverse(shiftr)
38+
39+
val out = MuxLookup(
40+
io.alu_op,
41+
io.B,
42+
Seq(
43+
ALU.ALU_ADD -> sum,
44+
ALU.ALU_SUB -> sum,
45+
ALU.ALU_SLT -> cmp,
46+
ALU.ALU_SLTU -> cmp,
47+
ALU.ALU_SRA -> shiftr,
48+
ALU.ALU_SRL -> shiftr,
49+
ALU.ALU_SLL -> shiftl,
50+
ALU.ALU_AND -> (io.A & io.B),
51+
ALU.ALU_OR -> (io.A | io.B),
52+
ALU.ALU_XOR -> (io.A ^ io.B),
53+
ALU.ALU_COPY_A -> io.A
54+
)
55+
)
56+
io.out := out
57+
io.sum := sum
58+
}

0 commit comments

Comments
 (0)