xref: /XiangShan/src/main/scala/xiangshan/frontend/PreDecode.scala (revision 280a374dbb4c88d0b5d7506d0236c933497ef46d)
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 rd = instr(11,7)
14    val rs = instr(19,15)
15    val brType::Nil = ListLookup(instr, List(BrType.notBr), PreDecodeInst.brTable)
16    val isCall = (brType === BrType.jal || brType === BrType.jalr) && isLink(rd) && !isRVC(instr)
17    val isRet = brType === BrType.jalr && isLink(rs) && !isLink(rd) && !isRVC(instr)
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}
45
46class PreDecodeResp extends XSBundle {
47  val instrs = Vec(PredictWidth, UInt(32.W))
48  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
49  val mask = UInt(PredictWidth.W)
50  val pd = Vec(PredictWidth, (new PreDecodeInfo))
51}
52
53class PreDecode extends XSModule with HasPdconst{
54  val io = IO(new Bundle() {
55    val in = Input(new FakeIcacheResp)
56    val prev = Flipped(ValidIO(UInt(16.W)))
57    val out = Output(new PreDecodeResp)
58  })
59
60  val data = io.in.data
61  val mask = io.in.mask
62
63  val insts = Wire(Vec(PredictWidth, UInt(32.W)))
64  val instsMask = Wire(Vec(PredictWidth, Bool()))
65  val instsRVC = Wire(Vec(PredictWidth,Bool()))
66  val instsPC = Wire(Vec(PredictWidth, UInt(VAddrBits.W)))
67  val nextHalf = Wire(UInt(16.W))
68
69  for (i <- 0 until PredictWidth) {
70    val inst = Wire(UInt(32.W))
71    val valid = Wire(Bool())
72    val pc = io.in.pc + (i << 1).U - Mux(io.prev.valid && (i.U === 0.U), 2.U, 0.U)
73
74    if (i==0) {
75      inst := Mux(io.prev.valid, Cat(data(15,0), io.prev.bits), data(31,0))
76      valid := true.B
77    } else if (i==1) {
78      inst := data(47,16)
79      valid := io.prev.valid || !(instsMask(0) && !isRVC(insts(0)))
80    } else if (i==PredictWidth-1) {
81      inst := Cat(0.U(16.W), data(i*16+15, i*16))
82      valid := !(instsMask(i-1) && !isRVC(insts(i-1)) || !isRVC(inst))
83    } else {
84      inst := data(i*16+31, i*16)
85      valid := !(instsMask(i-1) && !isRVC(insts(i-1)))
86    }
87
88    insts(i) := inst
89    instsRVC(i) := isRVC(inst)
90    instsMask(i) := mask(i) && valid
91    instsPC(i) := pc
92
93    val brType::isCall::isRet::Nil = brInfo(inst)
94    io.out.pd(i).isRVC := instsRVC(i)
95    io.out.pd(i).brType := brType
96    io.out.pd(i).isCall := isCall
97    io.out.pd(i).isRet := isRet
98    io.out.pd(i).excType := ExcType.notExc
99    io.out.instrs(i) := insts(i)
100    io.out.pc(i) := instsPC(i)
101
102  }
103  io.out.mask := instsMask.asUInt
104
105  for (i <- 0 until PredictWidth) {
106    XSDebug(true.B,
107      p"instr ${Hexdecimal(io.out.instrs(i))}, " +
108      p"mask ${Binary(instsMask(i))}, " +
109      p"pc ${Hexdecimal(io.out.pc(i))}, " +
110      p"isRVC ${Binary(io.out.pd(i).isRVC)}, " +
111      p"brType ${Binary(io.out.pd(i).brType)}, " +
112      p"isRet ${Binary(io.out.pd(i).isRet)}, " +
113      p"isCall ${Binary(io.out.pd(i).isCall)}\n"
114    )
115  }
116}
117