1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16package xiangshan.frontend 17 18import chipsalliance.rocketchip.config.Parameters 19import chisel3._ 20import chisel3.util._ 21import chisel3.experimental.chiselName 22import xiangshan._ 23import utils._ 24 25@chiselName 26class FetchRequestBundle(implicit p: Parameters) extends XSBundle { 27 val startAddr = UInt(VAddrBits.W) 28 val fallThruAddr = UInt(VAddrBits.W) 29 val fallThruError = Bool() 30 val ftqIdx = new FtqPtr 31 val ftqOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 32 val target = UInt(VAddrBits.W) 33 val oversize = Bool() 34 35 def fallThroughError() = { 36 def carryPos = instOffsetBits+log2Ceil(PredictWidth)+1 37 def getLower(pc: UInt) = pc(instOffsetBits+log2Ceil(PredictWidth), instOffsetBits) 38 val carry = (startAddr(carryPos) =/= fallThruAddr(carryPos)).asUInt 39 val startLower = Cat(0.U(1.W), getLower(startAddr)) 40 val endLowerwithCarry = Cat(carry, getLower(fallThruAddr)) 41 require(startLower.getWidth == log2Ceil(PredictWidth)+2) 42 require(endLowerwithCarry.getWidth == log2Ceil(PredictWidth)+2) 43 startLower >= endLowerwithCarry || (endLowerwithCarry - startLower) > (PredictWidth+1).U 44 } 45 def fromFtqPcBundle(b: Ftq_RF_Components) = { 46 this.startAddr := b.startAddr 47 this.fallThruAddr := b.getFallThrough() 48 this.oversize := b.oversize 49 this 50 } 51 def fromBpuResp(resp: BranchPredictionBundle) = { 52 // only used to bypass, so some fields remains unchanged 53 this.startAddr := resp.pc 54 this.target := resp.target 55 this.ftqOffset := resp.genCfiIndex 56 this.fallThruAddr := resp.fallThroughAddr 57 this.oversize := resp.ftb_entry.oversize 58 this 59 } 60 override def toPrintable: Printable = { 61 p"[start] ${Hexadecimal(startAddr)} [pft] ${Hexadecimal(fallThruAddr)}" + 62 p"[tgt] ${Hexadecimal(target)} [ftqIdx] $ftqIdx [jmp] v:${ftqOffset.valid}" + 63 p" offset: ${ftqOffset.bits}\n" 64 } 65} 66 67class PredecodeWritebackBundle(implicit p:Parameters) extends XSBundle { 68 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 69 val pd = Vec(PredictWidth, new PreDecodeInfo) // TODO: redefine Predecode 70 val ftqIdx = new FtqPtr 71 val ftqOffset = UInt(log2Ceil(PredictWidth).W) 72 val misOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 73 val cfiOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 74 val target = UInt(VAddrBits.W) 75 val jalTarget = UInt(VAddrBits.W) 76 val instrRange = Vec(PredictWidth, Bool()) 77} 78 79class Exception(implicit p: Parameters) extends XSBundle { 80 81} 82 83class FetchToIBuffer(implicit p: Parameters) extends XSBundle { 84 val instrs = Vec(PredictWidth, UInt(32.W)) 85 val valid = UInt(PredictWidth.W) 86 val pd = Vec(PredictWidth, new PreDecodeInfo) 87 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 88 val foldpc = Vec(PredictWidth, UInt(MemPredPCWidth.W)) 89 //val exception = new Exception 90 val ftqPtr = new FtqPtr 91 val ftqOffset = Vec(PredictWidth, ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 92 val ipf = Vec(PredictWidth, Bool()) 93 val acf = Vec(PredictWidth, Bool()) 94 val crossPageIPFFix = Vec(PredictWidth, Bool()) 95} 96 97// Move from BPU 98class GlobalHistory(implicit p: Parameters) extends XSBundle with HasBPUConst { 99 val predHist = UInt(HistoryLength.W) 100 // def update(sawNTBr: Bool, takenOnBr: Bool, hist: UInt = predHist): GlobalHistory = { 101 // val g = Wire(new GlobalHistory) 102 // val shifted = takenOnBr || sawNTBr 103 // g.predHist := Mux(shifted, (hist << 1) | takenOnBr.asUInt, hist) 104 // g 105 // } 106 107 // def update(brValids: UInt, taken_mask: UInt, hist: UInt = predHist): GlobalHistory = { 108 // val shift = PopCount(brValids & Mux(taken_mask =/= 0.U, LowerMask(taken_mask), ((1.U<<numBr) - 1.U))) 109 // val g = Wire(new GlobalHistory) 110 // g.predHist := (hist << shift) | (taken_mask =/= 0.U) 111 // g 112 // } 113 114 def update(shift: UInt, taken: Bool, hist: UInt = predHist): GlobalHistory = { 115 val g = Wire(new GlobalHistory) 116 g.predHist := (hist << shift) | taken 117 g 118 } 119 120 final def === (that: GlobalHistory): Bool = { 121 predHist === that.predHist 122 } 123 124 final def =/= (that: GlobalHistory): Bool = !(this === that) 125 126 implicit val name = "IFU" 127 def debug(where: String) = XSDebug(p"[${where}_GlobalHistory] hist=${Binary(predHist)}\n") 128 // override def toString(): String = "histPtr=%d, sawNTBr=%d, takenOnBr=%d, saveHalfRVI=%d".format(histPtr, sawNTBr, takenOnBr, saveHalfRVI) 129} 130 131class TableAddr(val idxBits: Int, val banks: Int)(implicit p: Parameters) extends XSBundle{ 132 def tagBits = VAddrBits - idxBits - instOffsetBits 133 134 val tag = UInt(tagBits.W) 135 val idx = UInt(idxBits.W) 136 val offset = UInt(instOffsetBits.W) 137 138 def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this) 139 def getTag(x: UInt) = fromUInt(x).tag 140 def getIdx(x: UInt) = fromUInt(x).idx 141 def getBank(x: UInt) = if (banks > 1) getIdx(x)(log2Up(banks) - 1, 0) else 0.U 142 def getBankIdx(x: UInt) = if (banks > 1) getIdx(x)(idxBits - 1, log2Up(banks)) else getIdx(x) 143} 144class BranchPrediction(implicit p: Parameters) extends XSBundle with HasBPUConst { 145 val taken_mask = Vec(numBr, Bool()) 146 147 val br_valids = Vec(numBr, Bool()) 148 val br_targets = Vec(numBr, UInt(VAddrBits.W)) 149 150 val jmp_valid = Bool() 151 val jmp_target = UInt(VAddrBits.W) 152 153 val is_jal = Bool() 154 val is_jalr = Bool() 155 val is_call = Bool() 156 val is_ret = Bool() 157 158 // val call_is_rvc = Bool() 159 val hit = Bool() 160 161 def taken = taken_mask.reduce(_||_) // || (is_jal || is_jalr) 162 163 def fromFtbEntry(entry: FTBEntry, pc: UInt) = { 164 br_valids := entry.brValids 165 br_targets := entry.getBrTargets(pc) 166 jmp_valid := entry.jmpValid 167 jmp_target := entry.getJmpTarget(pc) 168 is_jal := entry.jmpValid && entry.isJal 169 is_jalr := entry.jmpValid && entry.isJalr 170 is_call := entry.jmpValid && entry.isCall 171 is_ret := entry.jmpValid && entry.isRet 172 } 173 // override def toPrintable: Printable = { 174 // p"-----------BranchPrediction----------- " + 175 // p"[taken_mask] ${Binary(taken_mask.asUInt)} " + 176 // p"[is_br] ${Binary(is_br.asUInt)}, [is_jal] ${Binary(is_jal.asUInt)} " + 177 // p"[is_jalr] ${Binary(is_jalr.asUInt)}, [is_call] ${Binary(is_call.asUInt)}, [is_ret] ${Binary(is_ret.asUInt)} " + 178 // p"[target] ${Hexadecimal(target)}}, [hit] $hit " 179 // } 180 181 def display(cond: Bool): Unit = { 182 XSDebug(cond, p"[taken_mask] ${Binary(taken_mask.asUInt)} [hit] $hit\n") 183 } 184} 185 186@chiselName 187class BranchPredictionBundle(implicit p: Parameters) extends XSBundle with HasBPUConst with BPUUtils{ 188 val pc = UInt(VAddrBits.W) 189 190 val valid = Bool() 191 192 val hasRedirect = Bool() 193 val ftq_idx = new FtqPtr 194 // val hit = Bool() 195 val preds = new BranchPrediction 196 197 val ghist = new GlobalHistory() 198 val phist = UInt(PathHistoryLength.W) 199 val rasSp = UInt(log2Ceil(RasSize).W) 200 val rasTop = new RASEntry 201 val specCnt = Vec(numBr, UInt(10.W)) 202 // val meta = UInt(MaxMetaLength.W) 203 204 val ftb_entry = new FTBEntry() // TODO: Send this entry to ftq 205 206 def real_br_taken_mask(): Vec[Bool] = { 207 VecInit(preds.taken_mask.zip(preds.br_valids).map{ case(m, b) => m && b && preds.hit}) 208 } 209 210 def real_taken_mask(): Vec[Bool] = { 211 VecInit(real_br_taken_mask() :+ (preds.jmp_valid && preds.hit)) 212 } 213 214 def hit_taken_on_jmp = !real_br_taken_mask().asUInt.orR && preds.hit && preds.jmp_valid 215 def hit_taken_on_call = hit_taken_on_jmp && preds.is_call 216 def hit_taken_on_ret = hit_taken_on_jmp && preds.is_ret 217 def hit_taken_on_jalr = hit_taken_on_jmp && preds.is_jalr 218 219 def fallThroughAddr = getFallThroughAddr(pc, ftb_entry.carry, ftb_entry.pftAddr) 220 221 def target(): UInt = { 222 val targetVec = ftb_entry.getTargetVec(pc) :+ fallThroughAddr :+ (pc + (FetchWidth*4).U) 223 val selVec = real_taken_mask() :+ (preds.hit && !real_taken_mask().asUInt.orR) :+ true.B 224 PriorityMux(selVec zip targetVec) 225 } 226 def genCfiIndex = { 227 val cfiIndex = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 228 cfiIndex.valid := real_taken_mask.asUInt.orR 229 // when no takens, set cfiIndex to PredictWidth-1 230 cfiIndex.bits := 231 ParallelPriorityMux(real_taken_mask, ftb_entry.getOffsetVec) | 232 Fill(log2Ceil(PredictWidth), (!real_taken_mask.asUInt.orR).asUInt) 233 cfiIndex 234 } 235 236 237 // override def toPrintable: Printable = { 238 // p"-----------BranchPredictionBundle----------- " + 239 // p"[pc] ${Hexadecimal(pc)} " + 240 // p"[ghist] ${Binary(ghist.predHist)} " + 241 // preds.toPrintable + 242 // ftb_entry.toPrintable 243 // } 244 245 def display(cond: Bool): Unit = { 246 XSDebug(cond, p"[pc] ${Hexadecimal(pc)}\n") 247 XSDebug(cond, p"[ghist] ${Binary(ghist.predHist)}\n") 248 preds.display(cond) 249 ftb_entry.display(cond) 250 } 251} 252 253@chiselName 254class BranchPredictionResp(implicit p: Parameters) extends XSBundle with HasBPUConst { 255 // val valids = Vec(3, Bool()) 256 val s1 = new BranchPredictionBundle() 257 val s2 = new BranchPredictionBundle() 258 val s3 = new BranchPredictionBundle() 259 260 def selectedResp = 261 PriorityMux(Seq( 262 ((s3.valid && s3.hasRedirect) -> s3), 263 ((s2.valid && s2.hasRedirect) -> s2), 264 (s1.valid -> s1) 265 )) 266 def selectedRespIdx = 267 PriorityMux(Seq( 268 ((s3.valid && s3.hasRedirect) -> BP_S3), 269 ((s2.valid && s2.hasRedirect) -> BP_S2), 270 (s1.valid -> BP_S1) 271 )) 272 def lastStage = s3 273} 274 275class BpuToFtqBundle(implicit p: Parameters) extends BranchPredictionResp with HasBPUConst { 276 val meta = UInt(MaxMetaLength.W) 277} 278 279object BpuToFtqBundle { 280 def apply(resp: BranchPredictionResp)(implicit p: Parameters): BpuToFtqBundle = { 281 val e = Wire(new BpuToFtqBundle()) 282 e.s1 := resp.s1 283 e.s2 := resp.s2 284 e.s3 := resp.s3 285 286 e.meta := DontCare 287 e 288 } 289} 290 291class BranchPredictionUpdate(implicit p: Parameters) extends BranchPredictionBundle with HasBPUConst { 292 val mispred_mask = Vec(numBr+1, Bool()) 293 val false_hit = Bool() 294 val new_br_insert_pos = Vec(numBr, Bool()) 295 val old_entry = Bool() 296 val meta = UInt(MaxMetaLength.W) 297 val full_target = UInt(VAddrBits.W) 298 // val ghist = new GlobalHistory() This in spec_meta 299 300 def fromFtqRedirectSram(entry: Ftq_Redirect_SRAMEntry) = { 301 ghist := entry.ghist 302 phist := entry.phist 303 rasSp := entry.rasSp 304 rasTop := entry.rasEntry 305 specCnt := entry.specCnt 306 this 307 } 308 // override def toPrintable: Printable = { 309 // p"-----------BranchPredictionUpdate----------- " + 310 // p"[mispred_mask] ${Binary(mispred_mask.asUInt)} [false_hit] ${Binary(false_hit)} " + 311 // p"[new_br_insert_pos] ${Binary(new_br_insert_pos.asUInt)} " + 312 // super.toPrintable + 313 // p"\n" 314 // } 315 316 override def display(cond: Bool) { 317 XSDebug(cond, p"-----------BranchPredictionUpdate-----------\n") 318 XSDebug(cond, p"[mispred_mask] ${Binary(mispred_mask.asUInt)} [false_hit] $false_hit\n") 319 XSDebug(cond, p"[new_br_insert_pos] ${Binary(new_br_insert_pos.asUInt)}\n") 320 super.display(cond) 321 XSDebug(cond, p"--------------------------------------------\n") 322 } 323} 324 325class BranchPredictionRedirect(implicit p: Parameters) extends Redirect with HasBPUConst { 326 // override def toPrintable: Printable = { 327 // p"-----------BranchPredictionRedirect----------- " + 328 // p"-----------cfiUpdate----------- " + 329 // p"[pc] ${Hexadecimal(cfiUpdate.pc)} " + 330 // p"[predTaken] ${cfiUpdate.predTaken}, [taken] ${cfiUpdate.taken}, [isMisPred] ${cfiUpdate.isMisPred} " + 331 // p"[target] ${Hexadecimal(cfiUpdate.target)} " + 332 // p"------------------------------- " + 333 // p"[roqPtr] f=${roqIdx.flag} v=${roqIdx.value} " + 334 // p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} " + 335 // p"[ftqOffset] ${ftqOffset} " + 336 // p"[level] ${level}, [interrupt] ${interrupt} " + 337 // p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value} " + 338 // p"[stFtqOffset] ${stFtqOffset} " + 339 // p"\n" 340 341 // } 342 343 def display(cond: Bool): Unit = { 344 XSDebug(cond, p"-----------BranchPredictionRedirect----------- \n") 345 XSDebug(cond, p"-----------cfiUpdate----------- \n") 346 XSDebug(cond, p"[pc] ${Hexadecimal(cfiUpdate.pc)}\n") 347 XSDebug(cond, p"[hist] ${Binary(cfiUpdate.hist.predHist)}\n") 348 XSDebug(cond, p"[br_hit] ${cfiUpdate.br_hit} [isMisPred] ${cfiUpdate.isMisPred}\n") 349 XSDebug(cond, p"[pred_taken] ${cfiUpdate.predTaken} [taken] ${cfiUpdate.taken} [isMisPred] ${cfiUpdate.isMisPred}\n") 350 XSDebug(cond, p"[target] ${Hexadecimal(cfiUpdate.target)} \n") 351 XSDebug(cond, p"[shift] ${cfiUpdate.shift}\n") 352 XSDebug(cond, p"------------------------------- \n") 353 XSDebug(cond, p"[roqPtr] f=${roqIdx.flag} v=${roqIdx.value}\n") 354 XSDebug(cond, p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} \n") 355 XSDebug(cond, p"[ftqOffset] ${ftqOffset} \n") 356 XSDebug(cond, p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value}\n") 357 XSDebug(cond, p"[stFtqOffset] ${stFtqOffset}\n") 358 XSDebug(cond, p"---------------------------------------------- \n") 359 } 360} 361