Skip to content

Commit 38f45ca

Browse files
committed
switch to decoder, recode InstructionType uop to OneHot, improve dispatch timing by removing useless Mux.
1 parent f73d654 commit 38f45ca

File tree

2 files changed

+102
-99
lines changed

2 files changed

+102
-99
lines changed

src/main/scala/Constants.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package chiselv
22

3+
import chisel3._
34
import chisel3.experimental.ChiselEnum
45

56
object Instruction extends ChiselEnum {
@@ -21,5 +22,12 @@ object Instruction extends ChiselEnum {
2122
}
2223

2324
object InstructionType extends ChiselEnum {
24-
val IN_ERR, INST_R, INST_I, INST_S, INST_B, INST_U, INST_J, INST_Z = Value
25+
val INST_I = Value((1 << 0).U)
26+
val INST_S = Value((1 << 1).U)
27+
val INST_B = Value((1 << 2).U)
28+
val INST_U = Value((1 << 3).U)
29+
val INST_J = Value((1 << 4).U)
30+
val INST_Z = Value((1 << 5).U)
31+
val INST_R = Value((1 << 6).U)
32+
val IN_ERR = Value((1 << 7).U)
2533
}

src/main/scala/Decoder.scala

Lines changed: 93 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package chiselv
22

33
import chisel3._
4-
import chisel3.util.{BitPat, _}
5-
6-
import Instruction._
7-
import InstructionType._
4+
import chisel3.util._
5+
import chiselv.Instruction._
6+
import chiselv.InstructionType._
87

98
class DecoderPort(bitWidth: Int = 32) extends Bundle {
109
val op = Input(UInt(bitWidth.W)) // Op is the 32 bit instruction read received for decoding
@@ -26,116 +25,112 @@ class Decoder(bitWidth: Int = 32) extends Module {
2625
val DecoderPort = new DecoderPort(bitWidth)
2726
})
2827

29-
val signals =
30-
ListLookup(
28+
class DecType extends Bundle {
29+
val inst_type = UInt(InstructionType.getWidth.W)
30+
val inst = UInt(Instruction.getWidth.W)
31+
val to_alu = Bool()
32+
val branch = Bool()
33+
val use_imm = Bool()
34+
val jump = Bool()
35+
val is_load = Bool()
36+
val is_store = Bool()
37+
}
38+
val signals = chisel3.util.experimental.decode
39+
.decoder(
3140
io.DecoderPort.op,
41+
chisel3.util.experimental.decode.TruthTable(
3242
// format: off
33-
List( IN_ERR, ERR_INST, false.B, false.B, false.B, false.B, false.B, false.B), // Default values
3443
Array(
35-
/* inst_type, inst to_alu branch use_imm jump is_load is_store*/
44+
/* inst_type, inst to_alu branch use_imm jump is_load is_store*/
3645
// Arithmetic
37-
BitPat("b0000000??????????000?????0110011") -> List(INST_R, ADD, true.B, false.B, false.B, false.B, false.B, false.B),
38-
BitPat("b?????????????????000?????0010011") -> List(INST_I, ADDI, true.B, false.B, true.B, false.B, false.B, false.B),
39-
BitPat("b0100000??????????000?????0110011") -> List(INST_R, SUB, true.B, false.B, false.B, false.B, false.B, false.B),
40-
BitPat("b?????????????????????????0110111") -> List(INST_U, LUI, false.B, false.B, true.B, false.B, false.B, false.B),
41-
BitPat("b?????????????????????????0010111") -> List(INST_U, AUIPC, false.B, false.B, true.B, false.B, false.B, false.B),
46+
BitPat("b0000000??????????000?????0110011") -> BitPat(Cat(INST_R.asUInt, ADD.asUInt, true.B, false.B, false.B, false.B, false.B, false.B)),
47+
BitPat("b?????????????????000?????0010011") -> BitPat(Cat(INST_I.asUInt, ADDI.asUInt, true.B, false.B, true.B, false.B, false.B, false.B)),
48+
BitPat("b0100000??????????000?????0110011") -> BitPat(Cat(INST_R.asUInt, SUB.asUInt, true.B, false.B, false.B, false.B, false.B, false.B)),
49+
BitPat("b?????????????????????????0110111") -> BitPat(Cat(INST_U.asUInt, LUI.asUInt, false.B, false.B, true.B, false.B, false.B, false.B)),
50+
BitPat("b?????????????????????????0010111") -> BitPat(Cat(INST_U.asUInt, AUIPC.asUInt, false.B, false.B, true.B, false.B, false.B, false.B)),
4251
// Shifts
43-
BitPat("b0000000??????????001?????0110011") -> List(INST_R, SLL, true.B, false.B, false.B, false.B, false.B, false.B),
44-
BitPat("b0000000??????????001?????0010011") -> List(INST_I, SLLI, true.B, false.B, true.B, false.B, false.B, false.B),
45-
BitPat("b0100000??????????101?????0110011") -> List(INST_R, SRL, true.B, false.B, false.B, false.B, false.B, false.B),
46-
BitPat("b0000000??????????101?????0010011") -> List(INST_I, SRLI, true.B, false.B, true.B, false.B, false.B, false.B),
47-
BitPat("b0100000??????????101?????0110011") -> List(INST_R, SRA, true.B, false.B, false.B, false.B, false.B, false.B),
48-
BitPat("b0100000??????????101?????0010011") -> List(INST_I, SRAI, true.B, false.B, true.B, false.B, false.B, false.B),
52+
BitPat("b0000000??????????001?????0110011") -> BitPat(Cat(INST_R.asUInt, SLL.asUInt, true.B, false.B, false.B, false.B, false.B, false.B)),
53+
BitPat("b0000000??????????001?????0010011") -> BitPat(Cat(INST_I.asUInt, SLLI.asUInt, true.B, false.B, true.B, false.B, false.B, false.B)),
54+
BitPat("b0100000??????????101?????0110011") -> BitPat(Cat(INST_R.asUInt, SRL.asUInt, true.B, false.B, false.B, false.B, false.B, false.B)),
55+
BitPat("b0000000??????????101?????0010011") -> BitPat(Cat(INST_I.asUInt, SRLI.asUInt, true.B, false.B, true.B, false.B, false.B, false.B)),
56+
BitPat("b0100000??????????101?????0110011") -> BitPat(Cat(INST_R.asUInt, SRA.asUInt, true.B, false.B, false.B, false.B, false.B, false.B)),
57+
BitPat("b0100000??????????101?????0010011") -> BitPat(Cat(INST_I.asUInt, SRAI.asUInt, true.B, false.B, true.B, false.B, false.B, false.B)),
4958
// Logical
50-
BitPat("b0000000??????????100?????0110011") -> List(INST_R, XOR, true.B, false.B, false.B, false.B, false.B, false.B),
51-
BitPat("b?????????????????100?????0010011") -> List(INST_I, XORI, true.B, false.B, true.B, false.B, false.B, false.B),
52-
BitPat("b0000000??????????110?????0110011") -> List(INST_R, OR, true.B, false.B, false.B, false.B, false.B, false.B),
53-
BitPat("b?????????????????110?????0010011") -> List(INST_I, ORI, true.B, false.B, true.B, false.B, false.B, false.B),
54-
BitPat("b0000000??????????111?????0110011") -> List(INST_R, AND, true.B, false.B, false.B, false.B, false.B, false.B),
55-
BitPat("b?????????????????111?????0010011") -> List(INST_I, ANDI, true.B, false.B, true.B, false.B, false.B, false.B),
59+
BitPat("b0000000??????????100?????0110011") -> BitPat(Cat(INST_R.asUInt, XOR.asUInt, true.B, false.B, false.B, false.B, false.B, false.B)),
60+
BitPat("b?????????????????100?????0010011") -> BitPat(Cat(INST_I.asUInt, XORI.asUInt, true.B, false.B, true.B, false.B, false.B, false.B)),
61+
BitPat("b0000000??????????110?????0110011") -> BitPat(Cat(INST_R.asUInt, OR.asUInt, true.B, false.B, false.B, false.B, false.B, false.B)),
62+
BitPat("b?????????????????110?????0010011") -> BitPat(Cat(INST_I.asUInt, ORI.asUInt, true.B, false.B, true.B, false.B, false.B, false.B)),
63+
BitPat("b0000000??????????111?????0110011") -> BitPat(Cat(INST_R.asUInt, AND.asUInt, true.B, false.B, false.B, false.B, false.B, false.B)),
64+
BitPat("b?????????????????111?????0010011") -> BitPat(Cat(INST_I.asUInt, ANDI.asUInt, true.B, false.B, true.B, false.B, false.B, false.B)),
5665
// Compare
57-
BitPat("b0000000??????????010?????0110011") -> List(INST_R, SLT, true.B, false.B, false.B, false.B, false.B, false.B),
58-
BitPat("b?????????????????010?????0010011") -> List(INST_I, SLTI, true.B, false.B, true.B, false.B, false.B, false.B),
59-
BitPat("b0000000??????????011?????0110011") -> List(INST_R, SLTU, true.B, false.B, false.B, false.B, false.B, false.B),
60-
BitPat("b?????????????????011?????0010011") -> List(INST_I, SLTIU, true.B, false.B, true.B, false.B, false.B, false.B),
66+
BitPat("b0000000??????????010?????0110011") -> BitPat(Cat(INST_R.asUInt, SLT.asUInt, true.B, false.B, false.B, false.B, false.B, false.B)),
67+
BitPat("b?????????????????010?????0010011") -> BitPat(Cat(INST_I.asUInt, SLTI.asUInt, true.B, false.B, true.B, false.B, false.B, false.B)),
68+
BitPat("b0000000??????????011?????0110011") -> BitPat(Cat(INST_R.asUInt, SLTU.asUInt, true.B, false.B, false.B, false.B, false.B, false.B)),
69+
BitPat("b?????????????????011?????0010011") -> BitPat(Cat(INST_I.asUInt, SLTIU.asUInt, true.B, false.B, true.B, false.B, false.B, false.B)),
6170
// Branches
62-
BitPat("b?????????????????000?????1100011") -> List(INST_B, BEQ, false.B, true.B, true.B, false.B, false.B, false.B),
63-
BitPat("b?????????????????001?????1100011") -> List(INST_B, BNE, false.B, true.B, true.B, false.B, false.B, false.B),
64-
BitPat("b?????????????????100?????1100011") -> List(INST_B, BLT, false.B, true.B, true.B, false.B, false.B, false.B),
65-
BitPat("b?????????????????101?????1100011") -> List(INST_B, BGE, false.B, true.B, true.B, false.B, false.B, false.B),
66-
BitPat("b?????????????????110?????1100011") -> List(INST_B, BLTU, false.B, true.B, true.B, false.B, false.B, false.B),
67-
BitPat("b?????????????????111?????1100011") -> List(INST_B, BGEU, false.B, true.B, true.B, false.B, false.B, false.B),
71+
BitPat("b?????????????????000?????1100011") -> BitPat(Cat(INST_B.asUInt, BEQ.asUInt, false.B, true.B, true.B, false.B, false.B, false.B)),
72+
BitPat("b?????????????????001?????1100011") -> BitPat(Cat(INST_B.asUInt, BNE.asUInt, false.B, true.B, true.B, false.B, false.B, false.B)),
73+
BitPat("b?????????????????100?????1100011") -> BitPat(Cat(INST_B.asUInt, BLT.asUInt, false.B, true.B, true.B, false.B, false.B, false.B)),
74+
BitPat("b?????????????????101?????1100011") -> BitPat(Cat(INST_B.asUInt, BGE.asUInt, false.B, true.B, true.B, false.B, false.B, false.B)),
75+
BitPat("b?????????????????110?????1100011") -> BitPat(Cat(INST_B.asUInt, BLTU.asUInt, false.B, true.B, true.B, false.B, false.B, false.B)),
76+
BitPat("b?????????????????111?????1100011") -> BitPat(Cat(INST_B.asUInt, BGEU.asUInt, false.B, true.B, true.B, false.B, false.B, false.B)),
6877
// Jump & link
69-
BitPat("b?????????????????????????1101111") -> List(INST_J, JAL, false.B, false.B, true.B, true.B, false.B, false.B),
70-
BitPat("b?????????????????000?????1100111") -> List(INST_I, JALR, false.B, false.B, true.B, true.B, false.B, false.B),
78+
BitPat("b?????????????????????????1101111") -> BitPat(Cat(INST_J.asUInt, JAL.asUInt, false.B, false.B, true.B, true.B, false.B, false.B)),
79+
BitPat("b?????????????????000?????1100111") -> BitPat(Cat(INST_I.asUInt, JALR.asUInt, false.B, false.B, true.B, true.B, false.B, false.B)),
7180
// Sync
72-
BitPat("b0000????????00000000000000001111") -> List(INST_I, FENCE, false.B, false.B, false.B, false.B, false.B, false.B),
73-
BitPat("b00000000000000000000001000001111") -> List(INST_I, FENCEI, false.B, false.B, true.B, false.B, false.B, false.B),
81+
BitPat("b0000????????00000000000000001111") -> BitPat(Cat(INST_I.asUInt, FENCE.asUInt, false.B, false.B, false.B, false.B, false.B, false.B)),
82+
BitPat("b00000000000000000000001000001111") -> BitPat(Cat(INST_I.asUInt, FENCEI.asUInt, false.B, false.B, true.B, false.B, false.B, false.B)),
7483
// Environment
75-
BitPat("b00000000000000000000000001110011") -> List(INST_I, ECALL, false.B, false.B, false.B, false.B, false.B, false.B),
76-
BitPat("b00000000000100000000000001110011") -> List(INST_I, EBREAK, false.B, false.B, false.B, false.B, false.B, false.B),
84+
BitPat("b00000000000000000000000001110011") -> BitPat(Cat(INST_I.asUInt, ECALL.asUInt, false.B, false.B, false.B, false.B, false.B, false.B)),
85+
BitPat("b00000000000100000000000001110011") -> BitPat(Cat(INST_I.asUInt, EBREAK.asUInt, false.B, false.B, false.B, false.B, false.B, false.B)),
7786
// CSR
78-
BitPat("b?????????????????001?????1110011") -> List(INST_I, CSRRW, false.B, false.B, false.B, false.B, false.B, false.B),
79-
BitPat("b?????????????????010?????1110011") -> List(INST_I, CSRRS, false.B, false.B, false.B, false.B, false.B, false.B),
80-
BitPat("b?????????????????011?????1110011") -> List(INST_I, CSRRC, false.B, false.B, false.B, false.B, false.B, false.B),
81-
BitPat("b?????????????????101?????1110011") -> List(INST_I, CSRRWI, false.B, false.B, true.B, false.B, false.B, false.B),
82-
BitPat("b?????????????????110?????1110011") -> List(INST_I, CSRRSI, false.B, false.B, true.B, false.B, false.B, false.B),
83-
BitPat("b?????????????????111?????1110011") -> List(INST_I, CSRRCI, false.B, false.B, true.B, false.B, false.B, false.B),
87+
BitPat("b?????????????????001?????1110011") -> BitPat(Cat(INST_I.asUInt, CSRRW.asUInt, false.B, false.B, false.B, false.B, false.B, false.B)),
88+
BitPat("b?????????????????010?????1110011") -> BitPat(Cat(INST_I.asUInt, CSRRS.asUInt, false.B, false.B, false.B, false.B, false.B, false.B)),
89+
BitPat("b?????????????????011?????1110011") -> BitPat(Cat(INST_I.asUInt, CSRRC.asUInt, false.B, false.B, false.B, false.B, false.B, false.B)),
90+
BitPat("b?????????????????101?????1110011") -> BitPat(Cat(INST_I.asUInt, CSRRWI.asUInt, false.B, false.B, true.B, false.B, false.B, false.B)),
91+
BitPat("b?????????????????110?????1110011") -> BitPat(Cat(INST_I.asUInt, CSRRSI.asUInt, false.B, false.B, true.B, false.B, false.B, false.B)),
92+
BitPat("b?????????????????111?????1110011") -> BitPat(Cat(INST_I.asUInt, CSRRCI.asUInt, false.B, false.B, true.B, false.B, false.B, false.B)),
8493
// Loads
85-
BitPat("b?????????????????000?????0000011") -> List(INST_I, LB, false.B, false.B, true.B, false.B, true.B, false.B),
86-
BitPat("b?????????????????001?????0000011") -> List(INST_I, LH, false.B, false.B, true.B, false.B, true.B, false.B),
87-
BitPat("b?????????????????100?????0000011") -> List(INST_I, LBU, false.B, false.B, true.B, false.B, true.B, false.B),
88-
BitPat("b?????????????????101?????0000011") -> List(INST_I, LHU, false.B, false.B, true.B, false.B, true.B, false.B),
89-
BitPat("b?????????????????010?????0000011") -> List(INST_I, LW, false.B, false.B, true.B, false.B, true.B, false.B),
94+
BitPat("b?????????????????000?????0000011") -> BitPat(Cat(INST_I.asUInt, LB.asUInt, false.B, false.B, true.B, false.B, true.B, false.B)),
95+
BitPat("b?????????????????001?????0000011") -> BitPat(Cat(INST_I.asUInt, LH.asUInt, false.B, false.B, true.B, false.B, true.B, false.B)),
96+
BitPat("b?????????????????100?????0000011") -> BitPat(Cat(INST_I.asUInt, LBU.asUInt, false.B, false.B, true.B, false.B, true.B, false.B)),
97+
BitPat("b?????????????????101?????0000011") -> BitPat(Cat(INST_I.asUInt, LHU.asUInt, false.B, false.B, true.B, false.B, true.B, false.B)),
98+
BitPat("b?????????????????010?????0000011") -> BitPat(Cat(INST_I.asUInt, LW.asUInt, false.B, false.B, true.B, false.B, true.B, false.B)),
9099
// Stores
91-
BitPat("b?????????????????000?????0100011") -> List(INST_S, SB, false.B, false.B, true.B, false.B, false.B, true.B),
92-
BitPat("b?????????????????001?????0100011") -> List(INST_S, SH, false.B, false.B, true.B, false.B, false.B, true.B),
93-
BitPat("b?????????????????010?????0100011") -> List(INST_S, SW, false.B, false.B, true.B, false.B, false.B, true.B),
100+
BitPat("b?????????????????000?????0100011") -> BitPat(Cat(INST_S.asUInt, SB.asUInt, false.B, false.B, true.B, false.B, false.B, true.B)),
101+
BitPat("b?????????????????001?????0100011") -> BitPat(Cat(INST_S.asUInt, SH.asUInt, false.B, false.B, true.B, false.B, false.B, true.B)),
102+
BitPat("b?????????????????010?????0100011") -> BitPat(Cat(INST_S.asUInt, SW.asUInt, false.B, false.B, true.B, false.B, false.B, true.B))
94103
)
95-
) // format: on
104+
,
105+
BitPat(Cat(IN_ERR.asUInt, ERR_INST.asUInt, false.B, false.B, false.B, false.B, false.B, false.B)) // Default values
106+
// format: on
107+
),
108+
)
109+
.asTypeOf(new DecType)
96110

97-
io.DecoderPort.rd := 0.U
98-
io.DecoderPort.rs1 := 0.U
99-
io.DecoderPort.rs2 := 0.U
100-
io.DecoderPort.imm := 0.S
101-
io.DecoderPort.inst := signals(1)
102-
io.DecoderPort.toALU := signals(2)
103-
io.DecoderPort.branch := signals(3)
104-
io.DecoderPort.use_imm := signals(4)
105-
io.DecoderPort.jump := signals(5)
106-
io.DecoderPort.is_load := signals(6)
107-
io.DecoderPort.is_store := signals(7)
108-
109-
switch(signals(0)) {
110-
is(INST_R) {
111-
io.DecoderPort.rd := io.DecoderPort.op(11, 7)
112-
io.DecoderPort.rs1 := io.DecoderPort.op(19, 15)
113-
io.DecoderPort.rs2 := io.DecoderPort.op(24, 20)
114-
}
115-
is(INST_I) {
116-
io.DecoderPort.rd := io.DecoderPort.op(11, 7)
117-
io.DecoderPort.rs1 := io.DecoderPort.op(19, 15)
118-
io.DecoderPort.imm := ImmGenerator(INST_I, io.DecoderPort.op)
119-
}
120-
is(INST_S) {
121-
io.DecoderPort.rs1 := io.DecoderPort.op(19, 15)
122-
io.DecoderPort.rs2 := io.DecoderPort.op(24, 20)
123-
io.DecoderPort.imm := ImmGenerator(INST_S, io.DecoderPort.op)
124-
}
125-
is(INST_B) {
126-
io.DecoderPort.rs1 := io.DecoderPort.op(19, 15)
127-
io.DecoderPort.rs2 := io.DecoderPort.op(24, 20)
128-
io.DecoderPort.imm := ImmGenerator(INST_B, io.DecoderPort.op)
129-
}
130-
is(INST_U) {
131-
io.DecoderPort.rd := io.DecoderPort.op(11, 7)
132-
io.DecoderPort.imm := ImmGenerator(INST_U, io.DecoderPort.op)
133-
}
134-
is(INST_J) {
135-
io.DecoderPort.rd := io.DecoderPort.op(11, 7)
136-
io.DecoderPort.imm := ImmGenerator(INST_J, io.DecoderPort.op)
137-
}
138-
}
111+
io.DecoderPort.rd := io.DecoderPort.op(11, 7)
112+
io.DecoderPort.rs1 := io.DecoderPort.op(19, 15)
113+
io.DecoderPort.rs2 := io.DecoderPort.op(24, 20)
114+
io.DecoderPort.imm := Mux1H(
115+
signals.inst_type,
116+
Seq(
117+
ImmGenerator(INST_I, io.DecoderPort.op),
118+
ImmGenerator(INST_S, io.DecoderPort.op),
119+
ImmGenerator(INST_B, io.DecoderPort.op),
120+
ImmGenerator(INST_U, io.DecoderPort.op),
121+
ImmGenerator(INST_J, io.DecoderPort.op),
122+
ImmGenerator(INST_Z, io.DecoderPort.op),
123+
0.U, // padding for InstructionType
124+
0.U // padding for InstructionType
125+
),
126+
)
127+
io.DecoderPort.inst := signals.inst
128+
io.DecoderPort.toALU := signals.to_alu
129+
io.DecoderPort.branch := signals.branch
130+
io.DecoderPort.use_imm := signals.use_imm
131+
io.DecoderPort.jump := signals.jump
132+
io.DecoderPort.is_load := signals.is_load
133+
io.DecoderPort.is_store := signals.is_store
139134

140135
/** ImmGenerator generates the immadiate value depending on the instruction type
141136
*

0 commit comments

Comments
 (0)