xref: /XiangShan/src/main/scala/xiangshan/frontend/PreDecode.scala (revision 36cbebc48395871a98fa9351cd9631235b70699b)
1package xiangshan.frontend
2
3import chisel3._
4import chisel3.util._
5import utils.XSDebug
6import xiangshan._
7import xiangshan.backend.decode.isa.predecode.PreDecodeInst
8
9trait HasPdconst{ this: XSModule =>
10  def isRVC(inst: UInt) = (inst(1,0) =/= 3.U)
11  def isLink(reg:UInt) = reg === 1.U || reg === 5.U
12  def brInfo(instr: UInt) = {
13    val brType::Nil = ListLookup(instr, List(BrType.notBr), PreDecodeInst.brTable)
14    val rd = Mux(isRVC(instr), 1.U, instr(11,7))
15    val rs = Mux(isRVC(instr), Mux(brType === BrType.jal, 0.U, instr(11, 7)), instr(19, 15))
16    val isCall = (brType === BrType.jal || brType === BrType.jalr) && isLink(rd)
17    val isRet = brType === BrType.jalr && isLink(rs) && !isCall
18    List(brType, isCall, isRet)
19  }
20}
21
22object BrType {
23  def notBr   = "b00".U
24  def branch  = "b01".U
25  def jal     = "b10".U
26  def jalr    = "b11".U
27  def apply() = UInt(2.W)
28}
29
30object ExcType {  //TODO:add exctype
31  def notExc = "b000".U
32  def apply() = UInt(3.W)
33}
34
35class PreDecodeInfo extends XSBundle {  // 8 bit
36  val isRVC   = Bool()
37  val brType  = UInt(2.W)
38  val isCall  = Bool()
39  val isRet   = Bool()
40  val excType = UInt(3.W)
41  def isBr = brType === BrType.branch
42  def isJal = brType === BrType.jal
43  def isJalr = brType === BrType.jalr
44  def notCFI = brType === BrType.notBr
45}
46
47class PreDecodeResp extends XSBundle {
48  val instrs = Vec(PredictWidth, UInt(32.W))
49  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
50  val mask = UInt(PredictWidth.W)
51  val pd = Vec(PredictWidth, (new PreDecodeInfo))
52}
53
54class PreDecode extends XSModule with HasPdconst{
55  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)
59  })
60
61  val data = io.in.data
62  val mask = io.in.mask
63
64  val insts = Wire(Vec(PredictWidth, UInt(32.W)))
65  val instsMask = Wire(Vec(PredictWidth, Bool()))
66  val instsRVC = Wire(Vec(PredictWidth,Bool()))
67  val instsPC = Wire(Vec(PredictWidth, UInt(VAddrBits.W)))
68  // val nextHalf = Wire(UInt(16.W))
69
70  val lastHalfInstrIdx = PopCount(mask) - 1.U
71
72  for (i <- 0 until PredictWidth) {
73    val inst = Wire(UInt(32.W))
74    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)
76
77    if (i==0) {
78      inst := Mux(io.prev.valid, Cat(data(15,0), io.prev.bits), data(31,0))
79      // valid := Mux(lastHalfInstrIdx === 0.U, isRVC(inst), true.B)
80      valid := Mux(lastHalfInstrIdx === 0.U, Mux(!io.prev.valid, isRVC(inst), true.B), true.B)
81    } else if (i==1) {
82      inst := data(47,16)
83      valid := (io.prev.valid || !(instsMask(0) && !isRVC(insts(0)))) && Mux(lastHalfInstrIdx === 1.U, isRVC(inst), true.B)
84    } else if (i==PredictWidth-1) {
85      inst := Cat(0.U(16.W), data(i*16+15, i*16))
86      valid := !(instsMask(i-1) && !isRVC(insts(i-1)) || !isRVC(inst))
87    } else {
88      inst := data(i*16+31, i*16)
89      valid := !(instsMask(i-1) && !isRVC(insts(i-1))) && Mux(i.U === lastHalfInstrIdx, isRVC(inst), true.B)
90    }
91
92    insts(i) := inst
93    instsRVC(i) := isRVC(inst)
94    instsMask(i) := mask(i) && valid
95    instsPC(i) := pc
96
97    val brType::isCall::isRet::Nil = brInfo(inst)
98    io.out.pd(i).isRVC := instsRVC(i)
99    io.out.pd(i).brType := brType
100    io.out.pd(i).isCall := isCall
101    io.out.pd(i).isRet := isRet
102    io.out.pd(i).excType := ExcType.notExc
103    io.out.instrs(i) := insts(i)
104    io.out.pc(i) := instsPC(i)
105
106  }
107  io.out.mask := instsMask.asUInt
108
109  for (i <- 0 until PredictWidth) {
110    XSDebug(true.B,
111      p"instr ${Hexadecimal(io.out.instrs(i))}, " +
112      p"mask ${Binary(instsMask(i))}, " +
113      p"pc ${Hexadecimal(io.out.pc(i))}, " +
114      p"isRVC ${Binary(io.out.pd(i).isRVC)}, " +
115      p"brType ${Binary(io.out.pd(i).brType)}, " +
116      p"isRet ${Binary(io.out.pd(i).isRet)}, " +
117      p"isCall ${Binary(io.out.pd(i).isCall)}\n"
118    )
119  }
120}
121