xref: /XiangShan/src/main/scala/xiangshan/frontend/PreDecode.scala (revision 2225d46ebbe2fd16b9b29963c27a7d0385a42709)
1package xiangshan.frontend
2
3import chipsalliance.rocketchip.config.Parameters
4import chisel3._
5import chisel3.util._
6import utils._
7import freechips.rocketchip.rocket.{RVCDecoder, ExpandedInstruction}
8import xiangshan._
9import xiangshan.backend.decode.isa.predecode.PreDecodeInst
10import xiangshan.cache._
11
12trait HasPdconst{ this: XSModule =>
13  def isRVC(inst: UInt) = (inst(1,0) =/= 3.U)
14  def isLink(reg:UInt) = reg === 1.U || reg === 5.U
15  def brInfo(instr: UInt) = {
16    val brType::Nil = ListLookup(instr, List(BrType.notBr), PreDecodeInst.brTable)
17    val rd = Mux(isRVC(instr), instr(12), instr(11,7))
18    val rs = Mux(isRVC(instr), Mux(brType === BrType.jal, 0.U, instr(11, 7)), instr(19, 15))
19    val isCall = (brType === BrType.jal && !isRVC(instr) || brType === BrType.jalr) && isLink(rd) // Only for RV64
20    val isRet = brType === BrType.jalr && isLink(rs) && !isCall
21    List(brType, isCall, isRet)
22  }
23}
24
25object BrType {
26  def notBr   = "b00".U
27  def branch  = "b01".U
28  def jal     = "b10".U
29  def jalr    = "b11".U
30  def apply() = UInt(2.W)
31}
32
33object ExcType {  //TODO:add exctype
34  def notExc = "b000".U
35  def apply() = UInt(3.W)
36}
37
38class PreDecodeInfo extends Bundle {  // 8 bit
39  val isRVC   = Bool()
40  val brType  = UInt(2.W)
41  val isCall  = Bool()
42  val isRet   = Bool()
43  val excType = UInt(3.W)
44  def isBr = brType === BrType.branch
45  def isJal = brType === BrType.jal
46  def isJalr = brType === BrType.jalr
47  def notCFI = brType === BrType.notBr
48}
49
50class PreDecodeInfoForDebug(val usePerf: Boolean = true) extends Bundle {
51  val isRVC   = if (usePerf) Bool() else UInt(0.W)
52  val brType  = if (usePerf) UInt(2.W) else UInt(0.W)
53  val isCall  = if (usePerf) Bool() else UInt(0.W)
54  val isRet   = if (usePerf) Bool() else UInt(0.W)
55  val excType = if (usePerf) UInt(3.W) else UInt(0.W)
56  def isBr = brType === BrType.branch
57  def isJal = brType === BrType.jal
58  def isJalr = brType === BrType.jalr
59  def notCFI = brType === BrType.notBr
60}
61
62class PreDecodeResp(implicit p: Parameters) extends XSBundle with HasIFUConst {
63  val instrs = Vec(PredictWidth, UInt(32.W))
64  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
65  val mask = UInt(PredictWidth.W)
66  // one for the first bank
67  val lastHalf = Bool()
68  val pd = Vec(PredictWidth, (new PreDecodeInfo))
69}
70
71class PreDecode(implicit p: Parameters) extends XSModule with HasPdconst with HasIFUConst {
72  val io = IO(new Bundle() {
73    val in = Input(new ICacheResp)
74    val prev = Flipped(ValidIO(UInt(16.W)))
75    val prev_pc = Input(UInt(VAddrBits.W))
76    val out = Output(new PreDecodeResp)
77  })
78
79  val data = io.in.data
80  val mask = io.in.mask
81
82  val packetAlignedPC = packetAligned(io.in.pc)
83  val packetOffset = offsetInPacket(io.in.pc)
84
85  val firstValidIdx = packetOffset // io.prev.valid should only occur with firstValidIdx = 0
86  XSError(firstValidIdx =/= 0.U && io.prev.valid && HasCExtension.B, p"pc:${io.in.pc}, mask:${io.in.mask}, prevhalfInst valid occurs on unaligned fetch packet\n")
87
88  val instsMask = Wire(Vec(PredictWidth, Bool()))
89  val instsEndMask = Wire(Vec(PredictWidth, Bool()))
90
91  val rawInsts = if (HasCExtension) {
92                   VecInit((0 until PredictWidth).map(i => if (i == PredictWidth-1) Cat(0.U(16.W), data(i*16+15, i*16))
93                                                         else data(i*16+31, i*16)))
94                 } else {
95                   VecInit((0 until PredictWidth).map(i => data(i*32+31, i*32)))
96                 }
97
98  for (i <- 0 until PredictWidth) {
99    val inst = WireInit(rawInsts(i))
100    val validStart = Wire(Bool()) // is the beginning of a valid inst
101    val validEnd = Wire(Bool())  // is the end of a valid inst
102
103    val isFirstInPacket = i.U === firstValidIdx
104    val isLastInPacket = (i == PredictWidth-1).B
105    val currentRVC = isRVC(inst) && HasCExtension.B
106
107    val lastIsValidEnd = (if (i == 0) { !io.prev.valid } else { instsEndMask(i-1) || isFirstInPacket }) || !HasCExtension.B
108
109    inst := (if (HasCExtension)
110               Mux(io.prev.valid && i.U === 0.U,
111                 Cat(rawInsts(i)(15,0), io.prev.bits),
112                 rawInsts(i))
113             else
114               rawInsts(i))
115
116    // when disable rvc, every 4 bytes should be an inst
117    validStart := lastIsValidEnd && !(isLastInPacket && !currentRVC) || !HasCExtension.B
118    validEnd := validStart && currentRVC || !validStart && !(isLastInPacket && !currentRVC) || !HasCExtension.B
119
120    val currentLastHalf = lastIsValidEnd && (isLastInPacket && !currentRVC) && HasCExtension.B
121
122    instsMask(i) := (if (i == 0) Mux(io.prev.valid, validEnd, validStart) else validStart)
123    instsEndMask(i) := validEnd
124
125    val brType::isCall::isRet::Nil = brInfo(inst)
126    io.out.pd(i).isRVC := currentRVC
127    io.out.pd(i).brType := brType
128    io.out.pd(i).isCall := isCall
129    io.out.pd(i).isRet := isRet
130    io.out.pd(i).excType := ExcType.notExc
131    io.out.instrs(i) := inst
132    io.out.pc(i) := Mux(io.prev.valid && HasCExtension.B && (i==0).B, io.prev_pc, Cat(packetIdx(io.in.pc), (i << instOffsetBits).U(log2Ceil(packetBytes).W)))
133
134    if (i == PredictWidth-1) { io.out.lastHalf := currentLastHalf }
135  }
136  io.out.mask := instsMask.asUInt & mask
137
138  for (i <- 0 until PredictWidth) {
139    XSDebug(true.B,
140      p"instr ${Hexadecimal(io.out.instrs(i))}, " +
141      p"mask ${Binary(instsMask(i))}, " +
142      p"endMask ${Binary(instsEndMask(i))}, " +
143      p"pc ${Hexadecimal(io.out.pc(i))}, " +
144      p"isRVC ${Binary(io.out.pd(i).isRVC)}, " +
145      p"brType ${Binary(io.out.pd(i).brType)}, " +
146      p"isRet ${Binary(io.out.pd(i).isRet)}, " +
147      p"isCall ${Binary(io.out.pd(i).isCall)}\n"
148    )
149  }
150}
151
152class RVCExpander(implicit p: Parameters) extends XSModule {
153  val io = IO(new Bundle {
154    val in = Input(UInt(32.W))
155    val out = Output(new ExpandedInstruction)
156  })
157
158  if (HasCExtension) {
159    io.out := new RVCDecoder(io.in, XLEN).decode
160  } else {
161    io.out := new RVCDecoder(io.in, XLEN).passthrough
162  }
163}
164