PreDecode.scala 5.5 KB
Newer Older
Fa_wang's avatar
Fa_wang 已提交
1 2 3 4
package xiangshan.frontend

import chisel3._
import chisel3.util._
L
Lingrui98 已提交
5
import utils._
Fa_wang's avatar
Fa_wang 已提交
6 7
import xiangshan._
import xiangshan.backend.decode.isa.predecode.PreDecodeInst
8
import xiangshan.cache._
Fa_wang's avatar
Fa_wang 已提交
9 10 11 12 13 14

trait HasPdconst{ this: XSModule =>
  def isRVC(inst: UInt) = (inst(1,0) =/= 3.U)
  def isLink(reg:UInt) = reg === 1.U || reg === 5.U
  def brInfo(instr: UInt) = {
    val brType::Nil = ListLookup(instr, List(BrType.notBr), PreDecodeInst.brTable)
Fa_wang's avatar
Fa_wang 已提交
15
    val rd = Mux(isRVC(instr), instr(12), instr(11,7))
Z
zhanglinjuan 已提交
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
Fa_wang's avatar
Fa_wang 已提交
18
    val isRet = brType === BrType.jalr && isLink(rs) && !isCall
Fa_wang's avatar
Fa_wang 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    List(brType, isCall, isRet)
  }
}

object BrType {
  def notBr   = "b00".U
  def branch  = "b01".U
  def jal     = "b10".U
  def jalr    = "b11".U
  def apply() = UInt(2.W)
}

object ExcType {  //TODO:add exctype
  def notExc = "b000".U
  def apply() = UInt(3.W)
}

36
class PreDecodeInfo extends XSBundle {  // 8 bit
Fa_wang's avatar
Fa_wang 已提交
37 38 39 40 41 42 43 44
  val isRVC   = Bool()
  val brType  = UInt(2.W)
  val isCall  = Bool()
  val isRet   = Bool()
  val excType = UInt(3.W)
  def isBr = brType === BrType.branch
  def isJal = brType === BrType.jal
  def isJalr = brType === BrType.jalr
L
Lingrui98 已提交
45
  def notCFI = brType === BrType.notBr
Fa_wang's avatar
Fa_wang 已提交
46 47
}

48
class PreDecodeResp extends XSBundle with HasIFUConst {
49 50 51
  val instrs = Vec(PredictWidth, UInt(32.W))
  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
  val mask = UInt(PredictWidth.W)
52
  // one for the first bank
L
Lingrui98 已提交
53
  val lastHalf = UInt(nBanksInPacket.W)
54
  val pd = Vec(PredictWidth, (new PreDecodeInfo))
Fa_wang's avatar
Fa_wang 已提交
55 56
}

57
class PreDecode extends XSModule with HasPdconst with HasIFUConst {
Fa_wang's avatar
Fa_wang 已提交
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)
Fa_wang's avatar
Fa_wang 已提交
62 63 64 65
  })

  val data = io.in.data
  val mask = io.in.mask
66 67 68 69 70 71 72
  
  val validCount = PopCount(mask)
  val bankAlignedPC = bankAligned(io.in.pc)
  val bankOffset = offsetInBank(io.in.pc)
  val isAligned = bankOffset === 0.U

  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 75 76
  // val lastHalfInstrIdx = Mux(isInLastBank(pc), (bankWidth-1).U, (bankWidth*2-1).U)
  // in case loop buffer gives a packet ending at an unaligned position
  val lastHalfInstrIdx = PriorityMux(Reverse(mask), (PredictWidth-1 to 0 by -1).map(i => i.U))
Fa_wang's avatar
Fa_wang 已提交
77

78 79
  val insts = Wire(Vec(PredictWidth, UInt(32.W)))
  val instsMask = Wire(Vec(PredictWidth, Bool()))
L
Lingrui98 已提交
80
  val instsEndMask = Wire(Vec(PredictWidth, Bool()))
81 82
  val instsRVC = Wire(Vec(PredictWidth,Bool()))
  val instsPC = Wire(Vec(PredictWidth, UInt(VAddrBits.W)))
83

84 85
  val rawInsts = if (HasCExtension) {
                   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 88 89
                 } else {
                   VecInit((0 until PredictWidth).map(i => data(i*32+31, i*32)))
                 }
L
Lingrui98 已提交
90
  // val nextHalf = Wire(UInt(16.W))
Fa_wang's avatar
Fa_wang 已提交
91

L
Lingrui98 已提交
92
  val lastHalf = Wire(Vec(nBanksInPacket, UInt(1.W)))
93

94
  for (i <- 0 until PredictWidth) {
95 96 97
    val inst = WireInit(rawInsts(i))
    val validStart = Wire(Bool()) // is the beginning of a valid inst
    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 101

    val isFirstInPacket = i.U === firstValidIdx
    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 107
    inst := (if (HasCExtension) Mux(io.prev.valid && i.U === 0.U, Cat(rawInsts(i)(15,0), io.prev.bits), rawInsts(i))
            else rawInsts(i))
108

109 110
    validStart := lastIsValidEnd && !(isLastInPacket && !currentRVC) || !HasCExtension.B
    validEnd := validStart && currentRVC || !validStart && !(isLastInPacket && !currentRVC) || !HasCExtension.B
L
Lingrui98 已提交
111

112
    val currentLastHalf = lastIsValidEnd && (isLastInPacket && !currentRVC) && HasCExtension.B
Fa_wang's avatar
Fa_wang 已提交
113 114

    insts(i) := inst
115
    instsRVC(i) := isRVC(inst) && HasCExtension.B
116 117
    instsMask(i) := (if (i == 0) Mux(io.prev.valid, validEnd, validStart) else validStart)
    instsEndMask(i) := validEnd
Fa_wang's avatar
Fa_wang 已提交
118 119 120
    instsPC(i) := pc

    val brType::isCall::isRet::Nil = brInfo(inst)
121 122 123 124 125 126 127
    io.out.pd(i).isRVC := instsRVC(i)
    io.out.pd(i).brType := brType
    io.out.pd(i).isCall := isCall
    io.out.pd(i).isRet := isRet
    io.out.pd(i).excType := ExcType.notExc
    io.out.instrs(i) := insts(i)
    io.out.pc(i) := instsPC(i)
L
Lingrui98 已提交
128 129 130

    if (i == bankWidth-1)    { lastHalf(0) := currentLastHalf }
    if (i == PredictWidth-1) { lastHalf(1) := currentLastHalf }
Fa_wang's avatar
Fa_wang 已提交
131
  }
132
  io.out.mask := instsMask.asUInt & mask
133
  io.out.lastHalf := (if (HasCExtension) lastHalf.asUInt else 0.U(2.W))
Fa_wang's avatar
Fa_wang 已提交
134

135
  for (i <- 0 until PredictWidth) {
Fa_wang's avatar
Fa_wang 已提交
136
    XSDebug(true.B,
Z
zhanglinjuan 已提交
137
      p"instr ${Hexadecimal(io.out.instrs(i))}, " +
138
      p"mask ${Binary(instsMask(i))}, " +
139
      p"endMask ${Binary(instsEndMask(i))}, " +
Z
zhanglinjuan 已提交
140
      p"pc ${Hexadecimal(io.out.pc(i))}, " +
141 142 143 144
      p"isRVC ${Binary(io.out.pd(i).isRVC)}, " +
      p"brType ${Binary(io.out.pd(i).brType)}, " +
      p"isRet ${Binary(io.out.pd(i).isRet)}, " +
      p"isCall ${Binary(io.out.pd(i).isCall)}\n"
Fa_wang's avatar
Fa_wang 已提交
145 146
    )
  }
Fa_wang's avatar
Fa_wang 已提交
147
}