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

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

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

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

47 48 49 50 51
class PreDecodeResp extends XSBundle {
  val instrs = Vec(PredictWidth, UInt(32.W))
  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
  val mask = UInt(PredictWidth.W)
  val pd = Vec(PredictWidth, (new PreDecodeInfo))
Fa_wang's avatar
Fa_wang 已提交
52 53 54 55
}

class PreDecode extends XSModule with HasPdconst{
  val io = IO(new Bundle() {
56
    val in = Input(new FakeIcacheResp)
57
    val prev = Flipped(ValidIO(UInt(16.W)))
58
    val out = Output(new PreDecodeResp)
Fa_wang's avatar
Fa_wang 已提交
59 60 61 62 63
  })

  val data = io.in.data
  val mask = io.in.mask

64 65 66 67
  val insts = Wire(Vec(PredictWidth, UInt(32.W)))
  val instsMask = Wire(Vec(PredictWidth, Bool()))
  val instsRVC = Wire(Vec(PredictWidth,Bool()))
  val instsPC = Wire(Vec(PredictWidth, UInt(VAddrBits.W)))
L
Lingrui98 已提交
68
  // val nextHalf = Wire(UInt(16.W))
Fa_wang's avatar
Fa_wang 已提交
69

70 71
  val lastHalfInstrIdx = PopCount(mask) - 1.U

72
  for (i <- 0 until PredictWidth) {
Fa_wang's avatar
Fa_wang 已提交
73 74
    val inst = Wire(UInt(32.W))
    val valid = Wire(Bool())
75
    val pc = io.in.pc + (i << 1).U - Mux(io.prev.valid && (i.U === 0.U), 2.U, 0.U)
Fa_wang's avatar
Fa_wang 已提交
76 77

    if (i==0) {
78
      inst := Mux(io.prev.valid, Cat(data(15,0), io.prev.bits), data(31,0))
79 80
      // valid := Mux(lastHalfInstrIdx === 0.U, isRVC(inst), true.B)
      valid := Mux(lastHalfInstrIdx === 0.U, Mux(!io.prev.valid, isRVC(inst), true.B), true.B)
Fa_wang's avatar
Fa_wang 已提交
81 82
    } else if (i==1) {
      inst := data(47,16)
Z
zhanglinjuan 已提交
83
      valid := (io.prev.valid || !(instsMask(0) && !isRVC(insts(0)))) && Mux(lastHalfInstrIdx === 1.U, isRVC(inst), true.B)
84
    } else if (i==PredictWidth-1) {
Fa_wang's avatar
Fa_wang 已提交
85 86 87 88
      inst := Cat(0.U(16.W), data(i*16+15, i*16))
      valid := !(instsMask(i-1) && !isRVC(insts(i-1)) || !isRVC(inst))
    } else {
      inst := data(i*16+31, i*16)
89
      valid := !(instsMask(i-1) && !isRVC(insts(i-1))) && Mux(i.U === lastHalfInstrIdx, isRVC(inst), true.B)
Fa_wang's avatar
Fa_wang 已提交
90 91 92 93
    }

    insts(i) := inst
    instsRVC(i) := isRVC(inst)
Fa_wang's avatar
Fa_wang 已提交
94
    instsMask(i) := mask(i) && valid 
Fa_wang's avatar
Fa_wang 已提交
95 96 97
    instsPC(i) := pc

    val brType::isCall::isRet::Nil = brInfo(inst)
98 99 100 101 102 103 104 105
    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)
    
Fa_wang's avatar
Fa_wang 已提交
106
  }
107
  io.out.mask := instsMask.asUInt
Fa_wang's avatar
Fa_wang 已提交
108

109
  for (i <- 0 until PredictWidth) {
Fa_wang's avatar
Fa_wang 已提交
110
    XSDebug(true.B,
Z
zhanglinjuan 已提交
111
      p"instr ${Hexadecimal(io.out.instrs(i))}, " +
112
      p"mask ${Binary(instsMask(i))}, " +
Z
zhanglinjuan 已提交
113
      p"pc ${Hexadecimal(io.out.pc(i))}, " +
114 115 116 117
      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 已提交
118 119
    )
  }
Fa_wang's avatar
Fa_wang 已提交
120
}