1package xiangshan.frontend 2 3import chisel3._ 4import chisel3.util._ 5import utils._ 6import freechips.rocketchip.rocket.{RVCDecoder, ExpandedInstruction} 7import xiangshan._ 8import xiangshan.backend.decode.isa.predecode.PreDecodeInst 9import xiangshan.cache._ 10 11trait HasPdconst{ this: XSModule => 12 def isRVC(inst: UInt) = (inst(1,0) =/= 3.U) 13 def isLink(reg:UInt) = reg === 1.U || reg === 5.U 14 def brInfo(instr: UInt) = { 15 val brType::Nil = ListLookup(instr, List(BrType.notBr), PreDecodeInst.brTable) 16 val rd = Mux(isRVC(instr), instr(12), instr(11,7)) 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 19 val isRet = brType === BrType.jalr && isLink(rs) && !isCall 20 List(brType, isCall, isRet) 21 } 22} 23 24object BrType { 25 def notBr = "b00".U 26 def branch = "b01".U 27 def jal = "b10".U 28 def jalr = "b11".U 29 def apply() = UInt(2.W) 30} 31 32object ExcType { //TODO:add exctype 33 def notExc = "b000".U 34 def apply() = UInt(3.W) 35} 36 37class PreDecodeInfo extends XSBundle { // 8 bit 38 val isRVC = Bool() 39 val brType = UInt(2.W) 40 val isCall = Bool() 41 val isRet = Bool() 42 val excType = UInt(3.W) 43 def isBr = brType === BrType.branch 44 def isJal = brType === BrType.jal 45 def isJalr = brType === BrType.jalr 46 def notCFI = brType === BrType.notBr 47} 48 49class PreDecodeInfoForDebug(val usePerf: Boolean = true) extends XSBundle { 50 val isRVC = if (usePerf) Bool() else UInt(0.W) 51 val brType = if (usePerf) UInt(2.W) else UInt(0.W) 52 val isCall = if (usePerf) Bool() else UInt(0.W) 53 val isRet = if (usePerf) Bool() else UInt(0.W) 54 val excType = if (usePerf) UInt(3.W) else UInt(0.W) 55 def isBr = brType === BrType.branch 56 def isJal = brType === BrType.jal 57 def isJalr = brType === BrType.jalr 58 def notCFI = brType === BrType.notBr 59} 60 61class PreDecodeResp extends XSBundle with HasIFUConst { 62 val instrs = Vec(PredictWidth, UInt(32.W)) 63 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 64 val mask = UInt(PredictWidth.W) 65 // one for the first bank 66 val lastHalf = Bool() 67 val pd = Vec(PredictWidth, (new PreDecodeInfo)) 68} 69 70class PreDecode extends XSModule with HasPdconst with HasIFUConst { 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) 76 }) 77 78 val data = io.in.data 79 val mask = io.in.mask 80 81 val packetAlignedPC = packetAligned(io.in.pc) 82 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") 86 87 val instsMask = Wire(Vec(PredictWidth, Bool())) 88 val instsEndMask = Wire(Vec(PredictWidth, Bool())) 89 90 val rawInsts = if (HasCExtension) { 91 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 } else { 94 VecInit((0 until PredictWidth).map(i => data(i*32+31, i*32))) 95 } 96 97 for (i <- 0 until PredictWidth) { 98 val inst = WireInit(rawInsts(i)) 99 val validStart = Wire(Bool()) // is the beginning of a valid inst 100 val validEnd = Wire(Bool()) // is the end of a valid inst 101 102 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 108 inst := (if (HasCExtension) 109 Mux(io.prev.valid && i.U === 0.U, 110 Cat(rawInsts(i)(15,0), io.prev.bits), 111 rawInsts(i)) 112 else 113 rawInsts(i)) 114 115 // when disable rvc, every 4 bytes should be an inst 116 validStart := lastIsValidEnd && !(isLastInPacket && !currentRVC) || !HasCExtension.B 117 validEnd := validStart && currentRVC || !validStart && !(isLastInPacket && !currentRVC) || !HasCExtension.B 118 119 val currentLastHalf = lastIsValidEnd && (isLastInPacket && !currentRVC) && HasCExtension.B 120 121 instsMask(i) := (if (i == 0) Mux(io.prev.valid, validEnd, validStart) else validStart) 122 instsEndMask(i) := validEnd 123 124 val brType::isCall::isRet::Nil = brInfo(inst) 125 io.out.pd(i).isRVC := currentRVC 126 io.out.pd(i).brType := brType 127 io.out.pd(i).isCall := isCall 128 io.out.pd(i).isRet := isRet 129 io.out.pd(i).excType := ExcType.notExc 130 io.out.instrs(i) := inst 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))) 132 133 if (i == PredictWidth-1) { io.out.lastHalf := currentLastHalf } 134 } 135 io.out.mask := instsMask.asUInt & mask 136 137 for (i <- 0 until PredictWidth) { 138 XSDebug(true.B, 139 p"instr ${Hexadecimal(io.out.instrs(i))}, " + 140 p"mask ${Binary(instsMask(i))}, " + 141 p"endMask ${Binary(instsEndMask(i))}, " + 142 p"pc ${Hexadecimal(io.out.pc(i))}, " + 143 p"isRVC ${Binary(io.out.pd(i).isRVC)}, " + 144 p"brType ${Binary(io.out.pd(i).brType)}, " + 145 p"isRet ${Binary(io.out.pd(i).isRet)}, " + 146 p"isCall ${Binary(io.out.pd(i).isCall)}\n" 147 ) 148 } 149} 150 151class RVCExpander extends XSModule { 152 val io = IO(new Bundle { 153 val in = Input(UInt(32.W)) 154 val out = Output(new ExpandedInstruction) 155 }) 156 157 if (HasCExtension) { 158 io.out := new RVCDecoder(io.in, XLEN).decode 159 } else { 160 io.out := new RVCDecoder(io.in, XLEN).passthrough 161 } 162} 163