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 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 := true.B 80 } else if (i==1) { 81 inst := data(47,16) 82 valid := io.prev.valid || !(instsMask(0) && !isRVC(insts(0))) 83 } else if (i==PredictWidth-1) { 84 inst := Cat(0.U(16.W), data(i*16+15, i*16)) 85 valid := !(instsMask(i-1) && !isRVC(insts(i-1)) || !isRVC(inst)) 86 } else { 87 inst := data(i*16+31, i*16) 88 valid := !(instsMask(i-1) && !isRVC(insts(i-1))) && Mux(i.U === lastHalfInstrIdx, isRVC(inst), true.B) 89 } 90 91 insts(i) := inst 92 instsRVC(i) := isRVC(inst) 93 instsMask(i) := mask(i) && valid 94 instsPC(i) := pc 95 96 val brType::isCall::isRet::Nil = brInfo(inst) 97 io.out.pd(i).isRVC := instsRVC(i) 98 io.out.pd(i).brType := brType 99 io.out.pd(i).isCall := isCall 100 io.out.pd(i).isRet := isRet 101 io.out.pd(i).excType := ExcType.notExc 102 io.out.instrs(i) := insts(i) 103 io.out.pc(i) := instsPC(i) 104 105 } 106 io.out.mask := instsMask.asUInt 107 108 for (i <- 0 until PredictWidth) { 109 XSDebug(true.B, 110 p"instr ${Hexadecimal(io.out.instrs(i))}, " + 111 p"mask ${Binary(instsMask(i))}, " + 112 p"pc ${Hexadecimal(io.out.pc(i))}, " + 113 p"isRVC ${Binary(io.out.pd(i).isRVC)}, " + 114 p"brType ${Binary(io.out.pd(i).brType)}, " + 115 p"isRet ${Binary(io.out.pd(i).isRet)}, " + 116 p"isCall ${Binary(io.out.pd(i).isCall)}\n" 117 ) 118 } 119} 120