1package xiangshan.frontend 2 3import chisel3._ 4import chisel3.util._ 5import xiangshan._ 6import xiangshan.backend.decode.isa.predecode.PreDecodeInst 7 8trait HasPdconst{ this: XSModule => 9 val halfWidth = FetchWidth * 2 10 val groupAlign = log2Up(FetchWidth * 4) 11 def isRVC(inst: UInt) = (inst(1,0) =/= 3.U) 12 def groupPC(pc: UInt): UInt = Cat(pc(VAddrBits-1, groupAlign), 0.U(groupAlign.W)) 13 def isLink(reg:UInt) = reg === 1.U || reg === 5.U 14 def brInfo(instr: UInt) = { 15 val rd = instr(11,7) 16 val rs = instr(19,15) 17 val brType::Nil = ListLookup(instr, List(BrType.notBr), PreDecodeInst.brTable) 18 val isCall = (brType === BrType.jal || brType === BrType.jalr) && isLink(rd) && !isRVC(instr) 19 val isRet = brType === BrType.jalr && isLink(rs) && !isLink(rd) && !isRVC(instr) 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} 32 33object ExcType { //TODO:add exctype 34 def notExc = "b000".U 35 def apply() = UInt(3.W) 36} 37 38class PDInfo extends XSBundle{ // 8 bit 39 val isRVC = Bool() 40 val brType = UInt(2.W) 41 val isCall = Bool() 42 val isRet = Bool() 43 val excType = UInt(3.W) 44 def isBr = brType === BrType.branch 45 def isJal = brType === BrType.jal 46 def isJalr = brType === BrType.jalr 47} 48 49class PDPacket extends PDInfo{ 50 val pc = UInt(VAddrBits.W) 51 val inst = UInt(32.W) 52 val mask = Bool() 53} 54 55class ICacheResp extends XSBundle { 56 val fetchPc = UInt(VAddrBits.W) 57 val data = UInt((FetchWidth * 32).W) 58 val mask = UInt((FetchWidth * 2).W) 59} 60 61class PreDecode extends XSModule with HasPdconst{ 62 val io = IO(new Bundle() { 63 val in = Input(new ICacheResp) 64 val prevHalf = Input(UInt(16.W)) 65 val prevValid = Input(false.B) 66 67 val out = Output(Vec(halfWidth, new PDPacket)) 68 }) 69 70 val gpc = groupPC(io.in.fetchPc) 71 val data = io.in.data 72 val mask = io.in.mask 73 74 val insts = Wire(Vec(halfWidth, UInt(32.W))) 75 val instsMask = Wire(Vec(halfWidth, Bool())) 76 val instsRVC = Wire(Vec(halfWidth,Bool())) 77 val instsPC = Wire(Vec(halfWidth, UInt(VAddrBits.W))) 78 val nextHalf = Wire(UInt(16.W)) 79 80 for (i <- 0 until halfWidth) { 81 val inst = Wire(UInt(32.W)) 82 val valid = Wire(Bool()) 83 val pc = gpc + (i << 1).U - Mux(io.prevValid && (i.U === 0.U), 2.U, 0.U) 84 85 if (i==0) { 86 inst := Mux(io.prevValid, Cat(data(15,0), io.prevHalf), data(31,0)) 87 valid := true.B 88 } else if (i==1) { 89 inst := data(47,16) 90 valid := io.prevValid || !(instsMask(0) && !isRVC(insts(0))) 91 } else if (i==halfWidth-1) { 92 inst := Cat(0.U(16.W), data(i*16+15, i*16)) 93 valid := !(instsMask(i-1) && !isRVC(insts(i-1)) || !isRVC(inst)) 94 } else { 95 inst := data(i*16+31, i*16) 96 valid := !(instsMask(i-1) && !isRVC(insts(i-1))) 97 } 98 99 insts(i) := inst 100 instsRVC(i) := isRVC(inst) 101 instsMask(i) := mask(i) && valid 102 instsPC(i) := pc 103 104 val brType::isCall::isRet::Nil = brInfo(inst) 105 io.out(i).isRVC := instsRVC(i) 106 io.out(i).brType := brType 107 io.out(i).isCall := isCall 108 io.out(i).isRet := isRet 109 io.out(i).excType := ExcType.notExc 110 io.out(i).inst := insts(i) 111 io.out(i).mask := instsMask(i) 112 io.out(i).pc := instsPC(i) 113 } 114 115 116// for (i <- 0 until halfWidth) { 117// XSDebug(true.B, 118// p"instr ${Binary(io.out(i).inst)}, " + 119// p"mask ${Binary(io.out(i).mask)}, " + 120// //p"pc ${Binary(io.out(i).pc)}, " + 121// p"isRVC ${Binary(io.out(i).isRVC)}, " + 122// p"brType ${Binary(io.out(i).brType)}, " + 123// p"isRet ${Binary(io.out(i).isRet)}, " + 124// p"isCall ${Binary(io.out(i).isCall)}\n" 125// ) 126// } 127}