xref: /XiangShan/src/main/scala/xiangshan/frontend/PreDecode.scala (revision cfcf47eec23c492bdaadb4f6d49238279c06f3c6)
1package xiangshan.frontend
2
3import chisel3._
4import chisel3.util._
5import utils._
6import xiangshan._
7import xiangshan.backend.decode.isa.predecode.PreDecodeInst
8import xiangshan.cache._
9
10trait HasPdconst{ this: XSModule =>
11  def isRVC(inst: UInt) = (inst(1,0) =/= 3.U)
12  def isLink(reg:UInt) = reg === 1.U || reg === 5.U
13  def brInfo(instr: UInt) = {
14    val brType::Nil = ListLookup(instr, List(BrType.notBr), PreDecodeInst.brTable)
15    val rd = Mux(isRVC(instr), instr(12), instr(11,7))
16    val rs = Mux(isRVC(instr), Mux(brType === BrType.jal, 0.U, instr(11, 7)), instr(19, 15))
17    val isCall = (brType === BrType.jal && !isRVC(instr) || brType === BrType.jalr) && isLink(rd) // Only for RV64
18    val isRet = brType === BrType.jalr && isLink(rs) && !isCall
19    List(brType, isCall, isRet)
20  }
21}
22
23object BrType {
24  def notBr   = "b00".U
25  def branch  = "b01".U
26  def jal     = "b10".U
27  def jalr    = "b11".U
28  def apply() = UInt(2.W)
29}
30
31object ExcType {  //TODO:add exctype
32  def notExc = "b000".U
33  def apply() = UInt(3.W)
34}
35
36class PreDecodeInfo extends XSBundle {  // 8 bit
37  val isRVC   = Bool()
38  val brType  = UInt(2.W)
39  val isCall  = Bool()
40  val isRet   = Bool()
41  val excType = UInt(3.W)
42  def isBr = brType === BrType.branch
43  def isJal = brType === BrType.jal
44  def isJalr = brType === BrType.jalr
45  def notCFI = brType === BrType.notBr
46}
47
48class PreDecodeResp extends XSBundle with HasIFUConst {
49  val instrs = Vec(PredictWidth, UInt(32.W))
50  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
51  val mask = UInt(PredictWidth.W)
52  // one for the first bank
53  val lastHalf = UInt(nBanksInPacket.W)
54  val pd = Vec(PredictWidth, (new PreDecodeInfo))
55}
56
57class PreDecode extends XSModule with HasPdconst with HasIFUConst {
58  val io = IO(new Bundle() {
59    val in = Input(new ICacheResp)
60    val prev = Flipped(ValidIO(UInt(16.W)))
61    val out = Output(new PreDecodeResp)
62  })
63
64  val data = io.in.data
65  val mask = io.in.mask
66
67  val validCount = PopCount(mask)
68  val bankAlignedPC = bankAligned(io.in.pc)
69  val bankOffset = offsetInBank(io.in.pc)
70  val isAligned = bankOffset === 0.U
71
72  val firstValidIdx = bankOffset // io.prev.valid should only occur with firstValidIdx = 0
73  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")
74  // val lastHalfInstrIdx = Mux(isInLastBank(pc), (bankWidth-1).U, (bankWidth*2-1).U)
75  // in case loop buffer gives a packet ending at an unaligned position
76  val lastHalfInstrIdx = PriorityMux(Reverse(mask), (PredictWidth-1 to 0 by -1).map(i => i.U))
77
78  val insts = Wire(Vec(PredictWidth, UInt(32.W)))
79  val instsMask = Wire(Vec(PredictWidth, Bool()))
80  val instsEndMask = Wire(Vec(PredictWidth, Bool()))
81  val instsRVC = Wire(Vec(PredictWidth,Bool()))
82  val instsPC = Wire(Vec(PredictWidth, UInt(VAddrBits.W)))
83
84  val rawInsts = if (HasCExtension) {
85                   VecInit((0 until PredictWidth).map(i => if (i == PredictWidth-1) Cat(0.U(16.W), data(i*16+15, i*16))
86                                                         else data(i*16+31, i*16)))
87                 } else {
88                   VecInit((0 until PredictWidth).map(i => data(i*32+31, i*32)))
89                 }
90  // val nextHalf = Wire(UInt(16.W))
91
92  val lastHalf = Wire(Vec(nBanksInPacket, UInt(1.W)))
93
94  for (i <- 0 until PredictWidth) {
95    val inst = WireInit(rawInsts(i))
96    val validStart = Wire(Bool()) // is the beginning of a valid inst
97    val validEnd = Wire(Bool())  // is the end of a valid inst
98    val pc = bankAlignedPC + (i << instOffsetBits).U - Mux(io.prev.valid && (i.U === firstValidIdx) && HasCExtension.B, 2.U, 0.U)
99
100    val isFirstInPacket = i.U === firstValidIdx
101    val isLastInPacket = i.U === lastHalfInstrIdx
102    val currentRVC = isRVC(insts(i)) && HasCExtension.B
103
104    val lastIsValidEnd = (if (i == 0) { !io.prev.valid } else { instsEndMask(i-1) || isFirstInPacket }) || HasCExtension.B
105
106    inst := (if (HasCExtension) Mux(io.prev.valid && i.U === 0.U, Cat(rawInsts(i)(15,0), io.prev.bits), rawInsts(i))
107            else rawInsts(i))
108
109    validStart := lastIsValidEnd && !(isLastInPacket && !currentRVC) || !HasCExtension.B
110    validEnd := validStart && currentRVC || !validStart && !(isLastInPacket && !currentRVC) || !HasCExtension.B
111
112    val currentLastHalf = lastIsValidEnd && (isLastInPacket && !currentRVC) && HasCExtension.B
113
114    insts(i) := inst
115    instsRVC(i) := isRVC(inst) && HasCExtension.B
116    instsMask(i) := (if (i == 0) Mux(io.prev.valid, validEnd, validStart) else validStart)
117    instsEndMask(i) := validEnd
118    instsPC(i) := pc
119
120    val brType::isCall::isRet::Nil = brInfo(inst)
121    io.out.pd(i).isRVC := instsRVC(i)
122    io.out.pd(i).brType := brType
123    io.out.pd(i).isCall := isCall
124    io.out.pd(i).isRet := isRet
125    io.out.pd(i).excType := ExcType.notExc
126    io.out.instrs(i) := insts(i)
127    io.out.pc(i) := instsPC(i)
128
129    if (i == bankWidth-1)    { lastHalf(0) := currentLastHalf }
130    if (i == PredictWidth-1) { lastHalf(1) := currentLastHalf }
131  }
132  io.out.mask := instsMask.asUInt & mask
133  io.out.lastHalf := (if (HasCExtension) lastHalf.asUInt else 0.U(2.W))
134
135  for (i <- 0 until PredictWidth) {
136    XSDebug(true.B,
137      p"instr ${Hexadecimal(io.out.instrs(i))}, " +
138      p"mask ${Binary(instsMask(i))}, " +
139      p"endMask ${Binary(instsEndMask(i))}, " +
140      p"pc ${Hexadecimal(io.out.pc(i))}, " +
141      p"isRVC ${Binary(io.out.pd(i).isRVC)}, " +
142      p"brType ${Binary(io.out.pd(i).brType)}, " +
143      p"isRet ${Binary(io.out.pd(i).isRet)}, " +
144      p"isCall ${Binary(io.out.pd(i).isCall)}\n"
145    )
146  }
147}
148