PreDecode.scala 5.7 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._
6
import freechips.rocketchip.rocket.{RVCDecoder, ExpandedInstruction}
Fa_wang's avatar
Fa_wang 已提交
7 8
import xiangshan._
import xiangshan.backend.decode.isa.predecode.PreDecodeInst
9
import xiangshan.cache._
Fa_wang's avatar
Fa_wang 已提交
10 11 12 13 14 15

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 已提交
16
    val rd = Mux(isRVC(instr), instr(12), instr(11,7))
Z
zhanglinjuan 已提交
17
    val rs = Mux(isRVC(instr), Mux(brType === BrType.jal, 0.U, instr(11, 7)), instr(19, 15))
18
    val isCall = (brType === BrType.jal && !isRVC(instr) || brType === BrType.jalr) && isLink(rd) // Only for RV64
Fa_wang's avatar
Fa_wang 已提交
19
    val isRet = brType === BrType.jalr && isLink(rs) && !isCall
Fa_wang's avatar
Fa_wang 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    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)
}

37
class PreDecodeInfo extends XSBundle {  // 8 bit
Fa_wang's avatar
Fa_wang 已提交
38 39 40 41 42 43 44 45
  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 已提交
46
  def notCFI = brType === BrType.notBr
Fa_wang's avatar
Fa_wang 已提交
47 48
}

49 50 51 52 53 54 55 56 57 58 59 60
class PreDecodeInfoForDebug(val usePerf: Boolean = true) extends XSBundle {
  val isRVC   = if (usePerf) Bool() else UInt(0.W)
  val brType  = if (usePerf) UInt(2.W) else UInt(0.W)
  val isCall  = if (usePerf) Bool() else UInt(0.W)
  val isRet   = if (usePerf) Bool() else UInt(0.W)
  val excType = if (usePerf) UInt(3.W) else UInt(0.W)
  def isBr = brType === BrType.branch
  def isJal = brType === BrType.jal
  def isJalr = brType === BrType.jalr
  def notCFI = brType === BrType.notBr
}

61
class PreDecodeResp extends XSBundle with HasIFUConst {
62 63 64
  val instrs = Vec(PredictWidth, UInt(32.W))
  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
  val mask = UInt(PredictWidth.W)
65
  // one for the first bank
66
  val lastHalf = Bool()
67
  val pd = Vec(PredictWidth, (new PreDecodeInfo))
Fa_wang's avatar
Fa_wang 已提交
68 69
}

70
class PreDecode extends XSModule with HasPdconst with HasIFUConst {
Fa_wang's avatar
Fa_wang 已提交
71
  val io = IO(new Bundle() {
72
    val in = Input(new ICacheResp)
73
    val prev = Flipped(ValidIO(UInt(16.W)))
74
    val prev_pc = Input(UInt(VAddrBits.W))
75
    val out = Output(new PreDecodeResp)
Fa_wang's avatar
Fa_wang 已提交
76 77 78 79
  })

  val data = io.in.data
  val mask = io.in.mask
80
  
81 82
  val packetAlignedPC = packetAligned(io.in.pc)
  val packetOffset = offsetInPacket(io.in.pc)
83

84
  val firstValidIdx = packetOffset // io.prev.valid should only occur with firstValidIdx = 0
85
  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")
Fa_wang's avatar
Fa_wang 已提交
86

87
  val instsMask = Wire(Vec(PredictWidth, Bool()))
L
Lingrui98 已提交
88
  val instsEndMask = Wire(Vec(PredictWidth, Bool()))
89

90 91
  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))
92
                                                         else data(i*16+31, i*16)))
93 94 95
                 } else {
                   VecInit((0 until PredictWidth).map(i => data(i*32+31, i*32)))
                 }
Fa_wang's avatar
Fa_wang 已提交
96

97
  for (i <- 0 until PredictWidth) {
98 99 100 101 102
    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

    val isFirstInPacket = i.U === firstValidIdx
103
    val isLastInPacket = (i == PredictWidth-1).B
104
    val currentRVC = isRVC(inst) && HasCExtension.B
105

106
    val lastIsValidEnd = (if (i == 0) { !io.prev.valid } else { instsEndMask(i-1) || isFirstInPacket }) || !HasCExtension.B
107
    
L
Lingrui98 已提交
108 109 110 111 112 113 114 115
    inst := (if (HasCExtension)
               Mux(io.prev.valid && i.U === 0.U,
                 Cat(rawInsts(i)(15,0), io.prev.bits),
                 rawInsts(i))
             else
               rawInsts(i))

    // when disable rvc, every 4 bytes should be an inst
116 117
    validStart := lastIsValidEnd && !(isLastInPacket && !currentRVC) || !HasCExtension.B
    validEnd := validStart && currentRVC || !validStart && !(isLastInPacket && !currentRVC) || !HasCExtension.B
L
Lingrui98 已提交
118

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

121 122
    instsMask(i) := (if (i == 0) Mux(io.prev.valid, validEnd, validStart) else validStart)
    instsEndMask(i) := validEnd
Fa_wang's avatar
Fa_wang 已提交
123 124

    val brType::isCall::isRet::Nil = brInfo(inst)
L
Lingrui98 已提交
125
    io.out.pd(i).isRVC := currentRVC
126 127 128 129
    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
L
Lingrui98 已提交
130
    io.out.instrs(i) := inst
L
Lingrui98 已提交
131
    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)))
L
Lingrui98 已提交
132

L
Lingrui98 已提交
133
    if (i == PredictWidth-1) { io.out.lastHalf := currentLastHalf }
Fa_wang's avatar
Fa_wang 已提交
134
  }
135
  io.out.mask := instsMask.asUInt & mask
Fa_wang's avatar
Fa_wang 已提交
136

137
  for (i <- 0 until PredictWidth) {
Fa_wang's avatar
Fa_wang 已提交
138
    XSDebug(true.B,
Z
zhanglinjuan 已提交
139
      p"instr ${Hexadecimal(io.out.instrs(i))}, " +
140
      p"mask ${Binary(instsMask(i))}, " +
141
      p"endMask ${Binary(instsEndMask(i))}, " +
Z
zhanglinjuan 已提交
142
      p"pc ${Hexadecimal(io.out.pc(i))}, " +
143 144 145 146
      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 已提交
147 148
    )
  }
Fa_wang's avatar
Fa_wang 已提交
149
}
150 151 152 153 154 155 156 157 158 159 160 161 162

class RVCExpander extends XSModule {
  val io = IO(new Bundle {
    val in = Input(UInt(32.W))
    val out = Output(new ExpandedInstruction)
  })

  if (HasCExtension) {
    io.out := new RVCDecoder(io.in, XLEN).decode
  } else {
    io.out := new RVCDecoder(io.in, XLEN).passthrough
  }
}