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 xiangshan.frontend.icache._ 24import utils._ 25import scala.math._ 26 27@chiselName 28class FetchRequestBundle(implicit p: Parameters) extends XSBundle with HasICacheParameters { 29 30 //fast path: Timing critical 31 val startAddr = UInt(VAddrBits.W) 32 val nextlineStart = UInt(VAddrBits.W) 33 val nextStartAddr = UInt(VAddrBits.W) 34 //slow path 35 val ftqIdx = new FtqPtr 36 val ftqOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 37 38 def crossCacheline = startAddr(blockOffBits - 1) === 1.U 39 40 def fromFtqPcBundle(b: Ftq_RF_Components) = { 41 this.startAddr := b.startAddr 42 this.nextlineStart := b.nextLineAddr 43 when (b.fallThruError) { 44 val nextBlockHigherTemp = Mux(startAddr(log2Ceil(PredictWidth)+instOffsetBits), b.startAddr, b.nextLineAddr) 45 val nextBlockHigher = nextBlockHigherTemp(VAddrBits-1, log2Ceil(PredictWidth)+instOffsetBits+1) 46 this.nextStartAddr := 47 Cat(nextBlockHigher, 48 startAddr(log2Ceil(PredictWidth)+instOffsetBits) ^ 1.U(1.W), 49 startAddr(log2Ceil(PredictWidth)+instOffsetBits-1, instOffsetBits), 50 0.U(instOffsetBits.W) 51 ) 52 } 53 this 54 } 55 override def toPrintable: Printable = { 56 p"[start] ${Hexadecimal(startAddr)} [next] ${Hexadecimal(nextlineStart)}" + 57 p"[tgt] ${Hexadecimal(nextStartAddr)} [ftqIdx] $ftqIdx [jmp] v:${ftqOffset.valid}" + 58 p" offset: ${ftqOffset.bits}\n" 59 } 60} 61 62class FtqICacheInfo(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 63 val startAddr = UInt(VAddrBits.W) 64 val nextlineStart = UInt(VAddrBits.W) 65 def crossCacheline = startAddr(blockOffBits - 1) === 1.U 66 def fromFtqPcBundle(b: Ftq_RF_Components) = { 67 this.startAddr := b.startAddr 68 this.nextlineStart := b.nextLineAddr 69 this 70 } 71} 72 73class IFUICacheIO(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 74 val icacheReady = Output(Bool()) 75 val resp = Vec(PortNumber, ValidIO(new ICacheMainPipeResp)) 76} 77 78class FtqToICacheRequestBundle(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 79 val pcMemRead = Vec(5, new FtqICacheInfo) 80} 81 82 83class PredecodeWritebackBundle(implicit p:Parameters) extends XSBundle { 84 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 85 val pd = Vec(PredictWidth, new PreDecodeInfo) // TODO: redefine Predecode 86 val ftqIdx = new FtqPtr 87 val ftqOffset = UInt(log2Ceil(PredictWidth).W) 88 val misOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 89 val cfiOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 90 val target = UInt(VAddrBits.W) 91 val jalTarget = UInt(VAddrBits.W) 92 val instrRange = Vec(PredictWidth, Bool()) 93} 94 95// Ftq send req to Prefetch 96class PrefetchRequest(implicit p:Parameters) extends XSBundle { 97 val target = UInt(VAddrBits.W) 98} 99 100class FtqPrefechBundle(implicit p:Parameters) extends XSBundle { 101 val req = DecoupledIO(new PrefetchRequest) 102} 103 104class FetchToIBuffer(implicit p: Parameters) extends XSBundle { 105 val instrs = Vec(PredictWidth, UInt(32.W)) 106 val valid = UInt(PredictWidth.W) 107 val enqEnable = UInt(PredictWidth.W) 108 val pd = Vec(PredictWidth, new PreDecodeInfo) 109 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 110 val foldpc = Vec(PredictWidth, UInt(MemPredPCWidth.W)) 111 val ftqPtr = new FtqPtr 112 val ftqOffset = Vec(PredictWidth, ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 113 val ipf = Vec(PredictWidth, Bool()) 114 val acf = Vec(PredictWidth, Bool()) 115 val crossPageIPFFix = Vec(PredictWidth, Bool()) 116 val triggered = Vec(PredictWidth, new TriggerCf) 117} 118 119// class BitWiseUInt(val width: Int, val init: UInt) extends Module { 120// val io = IO(new Bundle { 121// val set 122// }) 123// } 124// Move from BPU 125abstract class GlobalHistory(implicit p: Parameters) extends XSBundle with HasBPUConst { 126 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): GlobalHistory 127} 128 129class ShiftingGlobalHistory(implicit p: Parameters) extends GlobalHistory { 130 val predHist = UInt(HistoryLength.W) 131 132 def update(shift: UInt, taken: Bool, hist: UInt = this.predHist): ShiftingGlobalHistory = { 133 val g = Wire(new ShiftingGlobalHistory) 134 g.predHist := (hist << shift) | taken 135 g 136 } 137 138 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): ShiftingGlobalHistory = { 139 require(br_valids.length == numBr) 140 require(real_taken_mask.length == numBr) 141 val last_valid_idx = PriorityMux( 142 br_valids.reverse :+ true.B, 143 (numBr to 0 by -1).map(_.U(log2Ceil(numBr+1).W)) 144 ) 145 val first_taken_idx = PriorityEncoder(false.B +: real_taken_mask) 146 val smaller = Mux(last_valid_idx < first_taken_idx, 147 last_valid_idx, 148 first_taken_idx 149 ) 150 val shift = smaller 151 val taken = real_taken_mask.reduce(_||_) 152 update(shift, taken, this.predHist) 153 } 154 155 // static read 156 def read(n: Int): Bool = predHist.asBools()(n) 157 158 final def === (that: ShiftingGlobalHistory): Bool = { 159 predHist === that.predHist 160 } 161 162 final def =/= (that: ShiftingGlobalHistory): Bool = !(this === that) 163} 164 165// circular global history pointer 166class CGHPtr(implicit p: Parameters) extends CircularQueuePtr[CGHPtr]( 167 p => p(XSCoreParamsKey).HistoryLength 168){ 169} 170 171object CGHPtr { 172 def apply(f: Bool, v: UInt)(implicit p: Parameters): CGHPtr = { 173 val ptr = Wire(new CGHPtr) 174 ptr.flag := f 175 ptr.value := v 176 ptr 177 } 178 def inverse(ptr: CGHPtr)(implicit p: Parameters): CGHPtr = { 179 apply(!ptr.flag, ptr.value) 180 } 181} 182 183class CircularGlobalHistory(implicit p: Parameters) extends GlobalHistory { 184 val buffer = Vec(HistoryLength, Bool()) 185 type HistPtr = UInt 186 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): CircularGlobalHistory = { 187 this 188 } 189} 190 191class FoldedHistory(val len: Int, val compLen: Int, val max_update_num: Int)(implicit p: Parameters) 192 extends XSBundle with HasBPUConst { 193 require(compLen >= 1) 194 require(len > 0) 195 // require(folded_len <= len) 196 require(compLen >= max_update_num) 197 val folded_hist = UInt(compLen.W) 198 199 def need_oldest_bits = len > compLen 200 def info = (len, compLen) 201 def oldest_bit_to_get_from_ghr = (0 until max_update_num).map(len - _ - 1) 202 def oldest_bit_pos_in_folded = oldest_bit_to_get_from_ghr map (_ % compLen) 203 def oldest_bit_wrap_around = oldest_bit_to_get_from_ghr map (_ / compLen > 0) 204 def oldest_bit_start = oldest_bit_pos_in_folded.head 205 206 def get_oldest_bits_from_ghr(ghr: Vec[Bool], histPtr: CGHPtr) = { 207 // TODO: wrap inc for histPtr value 208 oldest_bit_to_get_from_ghr.map(i => ghr((histPtr + (i+1).U).value)) 209 } 210 211 def circular_shift_left(src: UInt, shamt: Int) = { 212 val srcLen = src.getWidth 213 val src_doubled = Cat(src, src) 214 val shifted = src_doubled(srcLen*2-1-shamt, srcLen-shamt) 215 shifted 216 } 217 218 // slow path, read bits from ghr 219 def update(ghr: Vec[Bool], histPtr: CGHPtr, num: Int, taken: Bool): FoldedHistory = { 220 val oldest_bits = VecInit(get_oldest_bits_from_ghr(ghr, histPtr)) 221 update(oldest_bits, num, taken) 222 } 223 224 225 // fast path, use pre-read oldest bits 226 def update(ob: Vec[Bool], num: Int, taken: Bool): FoldedHistory = { 227 // do xors for several bitsets at specified bits 228 def bitsets_xor(len: Int, bitsets: Seq[Seq[Tuple2[Int, Bool]]]) = { 229 val res = Wire(Vec(len, Bool())) 230 // println(f"num bitsets: ${bitsets.length}") 231 // println(f"bitsets $bitsets") 232 val resArr = Array.fill(len)(List[Bool]()) 233 for (bs <- bitsets) { 234 for ((n, b) <- bs) { 235 resArr(n) = b :: resArr(n) 236 } 237 } 238 // println(f"${resArr.mkString}") 239 // println(f"histLen: ${this.len}, foldedLen: $folded_len") 240 for (i <- 0 until len) { 241 // println(f"bit[$i], ${resArr(i).mkString}") 242 if (resArr(i).length > 2) { 243 println(f"[warning] update logic of foldest history has two or more levels of xor gates! " + 244 f"histlen:${this.len}, compLen:$compLen, at bit $i") 245 } 246 if (resArr(i).length == 0) { 247 println(f"[error] bits $i is not assigned in folded hist update logic! histlen:${this.len}, compLen:$compLen") 248 } 249 res(i) := resArr(i).foldLeft(false.B)(_^_) 250 } 251 res.asUInt 252 } 253 254 val new_folded_hist = if (need_oldest_bits) { 255 val oldest_bits = ob 256 require(oldest_bits.length == max_update_num) 257 // mask off bits that do not update 258 val oldest_bits_masked = oldest_bits.zipWithIndex.map{ 259 case (ob, i) => ob && (i < num).B 260 } 261 // if a bit does not wrap around, it should not be xored when it exits 262 val oldest_bits_set = (0 until max_update_num).filter(oldest_bit_wrap_around).map(i => (oldest_bit_pos_in_folded(i), oldest_bits_masked(i))) 263 264 // println(f"old bits pos ${oldest_bits_set.map(_._1)}") 265 266 // only the last bit could be 1, as we have at most one taken branch at a time 267 val newest_bits_masked = VecInit((0 until max_update_num).map(i => taken && ((i+1) == num).B)).asUInt 268 // if a bit does not wrap around, newest bits should not be xored onto it either 269 val newest_bits_set = (0 until max_update_num).map(i => (compLen-1-i, newest_bits_masked(i))) 270 271 // println(f"new bits set ${newest_bits_set.map(_._1)}") 272 // 273 val original_bits_masked = VecInit(folded_hist.asBools.zipWithIndex.map{ 274 case (fb, i) => fb && !(num >= (len-i)).B 275 }) 276 val original_bits_set = (0 until compLen).map(i => (i, original_bits_masked(i))) 277 278 // do xor then shift 279 val xored = bitsets_xor(compLen, Seq(original_bits_set, oldest_bits_set, newest_bits_set)) 280 circular_shift_left(xored, num) 281 } else { 282 // histLen too short to wrap around 283 ((folded_hist << num) | taken)(compLen-1,0) 284 } 285 286 val fh = WireInit(this) 287 fh.folded_hist := new_folded_hist 288 fh 289 } 290} 291 292class AheadFoldedHistoryOldestBits(val len: Int, val max_update_num: Int)(implicit p: Parameters) extends XSBundle { 293 val bits = Vec(max_update_num*2, Bool()) 294 // def info = (len, compLen) 295 def getRealOb(brNumOH: UInt): Vec[Bool] = { 296 val ob = Wire(Vec(max_update_num, Bool())) 297 for (i <- 0 until max_update_num) { 298 ob(i) := Mux1H(brNumOH, bits.drop(i).take(numBr+1)) 299 } 300 ob 301 } 302} 303 304class AllAheadFoldedHistoryOldestBits(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 305 val afhob = MixedVec(gen.filter(t => t._1 > t._2).map{_._1} 306 .toSet.toList.map(l => new AheadFoldedHistoryOldestBits(l, numBr))) // remove duplicates 307 require(gen.toSet.toList.equals(gen)) 308 def getObWithInfo(info: Tuple2[Int, Int]) = { 309 val selected = afhob.filter(_.len == info._1) 310 require(selected.length == 1) 311 selected(0) 312 } 313 def read(ghv: Vec[Bool], ptr: CGHPtr) = { 314 val hisLens = afhob.map(_.len) 315 val bitsToRead = hisLens.flatMap(l => (0 until numBr*2).map(i => l-i-1)).toSet // remove duplicates 316 val bitsWithInfo = bitsToRead.map(pos => (pos, ghv((ptr+(pos+1).U).value))) 317 for (ob <- afhob) { 318 for (i <- 0 until numBr*2) { 319 val pos = ob.len - i - 1 320 val bit_found = bitsWithInfo.filter(_._1 == pos).toList 321 require(bit_found.length == 1) 322 ob.bits(i) := bit_found(0)._2 323 } 324 } 325 } 326} 327 328class AllFoldedHistories(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 329 val hist = MixedVec(gen.map{case (l, cl) => new FoldedHistory(l, cl, numBr)}) 330 // println(gen.mkString) 331 require(gen.toSet.toList.equals(gen)) 332 def getHistWithInfo(info: Tuple2[Int, Int]) = { 333 val selected = hist.filter(_.info.equals(info)) 334 require(selected.length == 1) 335 selected(0) 336 } 337 def autoConnectFrom(that: AllFoldedHistories) = { 338 require(this.hist.length <= that.hist.length) 339 for (h <- this.hist) { 340 h := that.getHistWithInfo(h.info) 341 } 342 } 343 def update(ghv: Vec[Bool], ptr: CGHPtr, shift: Int, taken: Bool): AllFoldedHistories = { 344 val res = WireInit(this) 345 for (i <- 0 until this.hist.length) { 346 res.hist(i) := this.hist(i).update(ghv, ptr, shift, taken) 347 } 348 res 349 } 350 def update(afhob: AllAheadFoldedHistoryOldestBits, lastBrNumOH: UInt, shift: Int, taken: Bool): AllFoldedHistories = { 351 val res = WireInit(this) 352 for (i <- 0 until this.hist.length) { 353 val fh = this.hist(i) 354 if (fh.need_oldest_bits) { 355 val info = fh.info 356 val selectedAfhob = afhob.getObWithInfo(info) 357 val ob = selectedAfhob.getRealOb(lastBrNumOH) 358 res.hist(i) := this.hist(i).update(ob, shift, taken) 359 } else { 360 val dumb = Wire(Vec(numBr, Bool())) // not needed 361 dumb := DontCare 362 res.hist(i) := this.hist(i).update(dumb, shift, taken) 363 } 364 } 365 res 366 } 367 368 def display(cond: Bool) = { 369 for (h <- hist) { 370 XSDebug(cond, p"hist len ${h.len}, folded len ${h.compLen}, value ${Binary(h.folded_hist)}\n") 371 } 372 } 373} 374 375class TableAddr(val idxBits: Int, val banks: Int)(implicit p: Parameters) extends XSBundle{ 376 def tagBits = VAddrBits - idxBits - instOffsetBits 377 378 val tag = UInt(tagBits.W) 379 val idx = UInt(idxBits.W) 380 val offset = UInt(instOffsetBits.W) 381 382 def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this) 383 def getTag(x: UInt) = fromUInt(x).tag 384 def getIdx(x: UInt) = fromUInt(x).idx 385 def getBank(x: UInt) = if (banks > 1) getIdx(x)(log2Up(banks) - 1, 0) else 0.U 386 def getBankIdx(x: UInt) = if (banks > 1) getIdx(x)(idxBits - 1, log2Up(banks)) else getIdx(x) 387} 388 389trait BasicPrediction extends HasXSParameter { 390 def cfiIndex: ValidUndirectioned[UInt] 391 def target(pc: UInt): UInt 392 def lastBrPosOH: Vec[Bool] 393 def brTaken: Bool 394 def shouldShiftVec: Vec[Bool] 395 def fallThruError: Bool 396} 397class MinimalBranchPrediction(implicit p: Parameters) extends NewMicroBTBEntry with BasicPrediction { 398 val valid = Bool() 399 def cfiIndex = { 400 val res = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 401 res.valid := taken && valid 402 res.bits := cfiOffset | Fill(res.bits.getWidth, !valid) 403 res 404 } 405 def target(pc: UInt) = nextAddr 406 def lastBrPosOH: Vec[Bool] = VecInit(brNumOH.asBools()) 407 def brTaken = takenOnBr 408 def shouldShiftVec: Vec[Bool] = VecInit((0 until numBr).map(i => lastBrPosOH.drop(i+1).reduce(_||_))) 409 def fallThruError: Bool = false.B // we do this check on the following stages 410 411 def fromMicroBTBEntry(valid: Bool, entry: NewMicroBTBEntry, pc: UInt) = { 412 this.valid := valid 413 this.nextAddr := Mux(valid, entry.nextAddr, pc + (FetchWidth*4).U) 414 this.cfiOffset := entry.cfiOffset | Fill(cfiOffset.getWidth, !valid) 415 this.taken := entry.taken && valid 416 this.takenOnBr := entry.takenOnBr && valid 417 this.brNumOH := Mux(valid, entry.brNumOH, 1.U((numBr+1).W)) 418 } 419} 420@chiselName 421class FullBranchPrediction(implicit p: Parameters) extends XSBundle with HasBPUConst with BasicPrediction { 422 val br_taken_mask = Vec(numBr, Bool()) 423 424 val slot_valids = Vec(totalSlot, Bool()) 425 426 val targets = Vec(totalSlot, UInt(VAddrBits.W)) 427 val jalr_target = UInt(VAddrBits.W) // special path for indirect predictors 428 val offsets = Vec(totalSlot, UInt(log2Ceil(PredictWidth).W)) 429 val fallThroughAddr = UInt(VAddrBits.W) 430 val fallThroughErr = Bool() 431 432 val is_jal = Bool() 433 val is_jalr = Bool() 434 val is_call = Bool() 435 val is_ret = Bool() 436 val last_may_be_rvi_call = Bool() 437 val is_br_sharing = Bool() 438 439 // val call_is_rvc = Bool() 440 val hit = Bool() 441 442 def br_slot_valids = slot_valids.init 443 def tail_slot_valid = slot_valids.last 444 445 def br_valids = { 446 VecInit(br_slot_valids :+ (tail_slot_valid && is_br_sharing)) 447 } 448 449 def taken_mask_on_slot = { 450 VecInit( 451 (br_slot_valids zip br_taken_mask.init).map{ case (t, v) => t && v } :+ ( 452 tail_slot_valid && ( 453 is_br_sharing && br_taken_mask.last || !is_br_sharing 454 ) 455 ) 456 ) 457 } 458 459 def real_slot_taken_mask(): Vec[Bool] = { 460 VecInit(taken_mask_on_slot.map(_ && hit)) 461 } 462 463 // len numBr 464 def real_br_taken_mask(): Vec[Bool] = { 465 VecInit( 466 taken_mask_on_slot.map(_ && hit).init :+ 467 (br_taken_mask.last && tail_slot_valid && is_br_sharing && hit) 468 ) 469 } 470 471 // the vec indicating if ghr should shift on each branch 472 def shouldShiftVec = 473 VecInit(br_valids.zipWithIndex.map{ case (v, i) => 474 v && !real_br_taken_mask.take(i).reduceOption(_||_).getOrElse(false.B)}) 475 476 def lastBrPosOH = 477 VecInit((!hit || !br_valids.reduce(_||_)) +: // not hit or no brs in entry 478 (0 until numBr).map(i => 479 br_valids(i) && 480 !real_br_taken_mask.take(i).reduceOption(_||_).getOrElse(false.B) && // no brs taken in front it 481 (real_br_taken_mask()(i) || !br_valids.drop(i+1).reduceOption(_||_).getOrElse(false.B)) && // no brs behind it 482 hit 483 ) 484 ) 485 486 def brTaken = (br_valids zip br_taken_mask).map{ case (a, b) => a && b && hit}.reduce(_||_) 487 488 def target(pc: UInt): UInt = { 489 val targetVec = targets :+ fallThroughAddr :+ (pc + (FetchWidth * 4).U) 490 val tm = taken_mask_on_slot 491 val selVecOH = 492 tm.zipWithIndex.map{ case (t, i) => !tm.take(i).fold(false.B)(_||_) && t && hit} :+ 493 (!tm.asUInt.orR && hit) :+ !hit 494 Mux1H(selVecOH, targetVec) 495 } 496 497 def fallThruError: Bool = hit && fallThroughErr 498 499 def hit_taken_on_jmp = 500 !real_slot_taken_mask().init.reduce(_||_) && 501 real_slot_taken_mask().last && !is_br_sharing 502 def hit_taken_on_call = hit_taken_on_jmp && is_call 503 def hit_taken_on_ret = hit_taken_on_jmp && is_ret 504 def hit_taken_on_jalr = hit_taken_on_jmp && is_jalr 505 506 def cfiIndex = { 507 val cfiIndex = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 508 cfiIndex.valid := real_slot_taken_mask().asUInt.orR 509 // when no takens, set cfiIndex to PredictWidth-1 510 cfiIndex.bits := 511 ParallelPriorityMux(real_slot_taken_mask(), offsets) | 512 Fill(log2Ceil(PredictWidth), (!real_slot_taken_mask().asUInt.orR).asUInt) 513 cfiIndex 514 } 515 516 def taken = br_taken_mask.reduce(_||_) || slot_valids.last // || (is_jal || is_jalr) 517 518 def fromFtbEntry(entry: FTBEntry, pc: UInt, last_stage: Option[Tuple2[UInt, Bool]] = None) = { 519 slot_valids := entry.brSlots.map(_.valid) :+ entry.tailSlot.valid 520 targets := entry.getTargetVec(pc) 521 jalr_target := targets.last 522 offsets := entry.getOffsetVec 523 is_jal := entry.tailSlot.valid && entry.isJal 524 is_jalr := entry.tailSlot.valid && entry.isJalr 525 is_call := entry.tailSlot.valid && entry.isCall 526 is_ret := entry.tailSlot.valid && entry.isRet 527 last_may_be_rvi_call := entry.last_may_be_rvi_call 528 is_br_sharing := entry.tailSlot.valid && entry.tailSlot.sharing 529 530 val startLower = Cat(0.U(1.W), pc(instOffsetBits+log2Ceil(PredictWidth)-1, instOffsetBits)) 531 val endLowerwithCarry = Cat(entry.carry, entry.pftAddr) 532 fallThroughErr := startLower >= endLowerwithCarry 533 fallThroughAddr := Mux(fallThroughErr, pc + (FetchWidth * 4).U, entry.getFallThrough(pc)) 534 } 535 536 def display(cond: Bool): Unit = { 537 XSDebug(cond, p"[taken_mask] ${Binary(br_taken_mask.asUInt)} [hit] $hit\n") 538 } 539} 540 541@chiselName 542class BranchPredictionBundle(implicit p: Parameters) extends XSBundle 543 with HasBPUConst with BPUUtils { 544 // def full_pred_info[T <: Data](x: T) = if (is_minimal) None else Some(x) 545 val pc = UInt(VAddrBits.W) 546 547 val valid = Bool() 548 549 val hasRedirect = Bool() 550 val ftq_idx = new FtqPtr 551 // val hit = Bool() 552 val is_minimal = Bool() 553 val minimal_pred = new MinimalBranchPrediction 554 val full_pred = new FullBranchPrediction 555 556 557 val folded_hist = new AllFoldedHistories(foldedGHistInfos) 558 val afhob = new AllAheadFoldedHistoryOldestBits(foldedGHistInfos) 559 val lastBrNumOH = UInt((numBr+1).W) 560 val histPtr = new CGHPtr 561 val rasSp = UInt(log2Ceil(RasSize).W) 562 val rasTop = new RASEntry 563 // val specCnt = Vec(numBr, UInt(10.W)) 564 // val meta = UInt(MaxMetaLength.W) 565 566 val ftb_entry = new FTBEntry() 567 568 def target(pc: UInt) = Mux(is_minimal, minimal_pred.target(pc), full_pred.target(pc)) 569 def cfiIndex = Mux(is_minimal, minimal_pred.cfiIndex, full_pred.cfiIndex) 570 def lastBrPosOH = Mux(is_minimal, minimal_pred.lastBrPosOH, full_pred.lastBrPosOH) 571 def brTaken = Mux(is_minimal, minimal_pred.brTaken, full_pred.brTaken) 572 def shouldShiftVec = Mux(is_minimal, minimal_pred.shouldShiftVec, full_pred.shouldShiftVec) 573 def fallThruError = Mux(is_minimal, minimal_pred.fallThruError, full_pred.fallThruError) 574 575 def getTarget = target(pc) 576 def taken = cfiIndex.valid 577 578 def display(cond: Bool): Unit = { 579 XSDebug(cond, p"[pc] ${Hexadecimal(pc)}\n") 580 folded_hist.display(cond) 581 full_pred.display(cond) 582 ftb_entry.display(cond) 583 } 584} 585 586@chiselName 587class BranchPredictionResp(implicit p: Parameters) extends XSBundle with HasBPUConst { 588 // val valids = Vec(3, Bool()) 589 val s1 = new BranchPredictionBundle 590 val s2 = new BranchPredictionBundle 591 val s3 = new BranchPredictionBundle 592 593 def selectedResp ={ 594 val res = 595 PriorityMux(Seq( 596 ((s3.valid && s3.hasRedirect) -> s3), 597 ((s2.valid && s2.hasRedirect) -> s2), 598 (s1.valid -> s1) 599 )) 600 // println("is minimal: ", res.is_minimal) 601 res 602 } 603 def selectedRespIdx = 604 PriorityMux(Seq( 605 ((s3.valid && s3.hasRedirect) -> BP_S3), 606 ((s2.valid && s2.hasRedirect) -> BP_S2), 607 (s1.valid -> BP_S1) 608 )) 609 def lastStage = s3 610} 611 612class BpuToFtqBundle(implicit p: Parameters) extends BranchPredictionResp with HasBPUConst { 613 val meta = UInt(MaxMetaLength.W) 614} 615 616object BpuToFtqBundle { 617 def apply(resp: BranchPredictionResp)(implicit p: Parameters): BpuToFtqBundle = { 618 val e = Wire(new BpuToFtqBundle()) 619 e.s1 := resp.s1 620 e.s2 := resp.s2 621 e.s3 := resp.s3 622 623 e.meta := DontCare 624 e 625 } 626} 627 628class BranchPredictionUpdate(implicit p: Parameters) extends BranchPredictionBundle with HasBPUConst { 629 val mispred_mask = Vec(numBr+1, Bool()) 630 val pred_hit = Bool() 631 val false_hit = Bool() 632 val new_br_insert_pos = Vec(numBr, Bool()) 633 val old_entry = Bool() 634 val meta = UInt(MaxMetaLength.W) 635 val full_target = UInt(VAddrBits.W) 636 val from_stage = UInt(2.W) 637 val ghist = UInt(HistoryLength.W) 638 639 def fromFtqRedirectSram(entry: Ftq_Redirect_SRAMEntry) = { 640 folded_hist := entry.folded_hist 641 afhob := entry.afhob 642 lastBrNumOH := entry.lastBrNumOH 643 histPtr := entry.histPtr 644 rasSp := entry.rasSp 645 rasTop := entry.rasEntry 646 this 647 } 648 649 override def display(cond: Bool) = { 650 XSDebug(cond, p"-----------BranchPredictionUpdate-----------\n") 651 XSDebug(cond, p"[mispred_mask] ${Binary(mispred_mask.asUInt)} [false_hit] $false_hit\n") 652 XSDebug(cond, p"[new_br_insert_pos] ${Binary(new_br_insert_pos.asUInt)}\n") 653 super.display(cond) 654 XSDebug(cond, p"--------------------------------------------\n") 655 } 656} 657 658class BranchPredictionRedirect(implicit p: Parameters) extends Redirect with HasBPUConst { 659 // override def toPrintable: Printable = { 660 // p"-----------BranchPredictionRedirect----------- " + 661 // p"-----------cfiUpdate----------- " + 662 // p"[pc] ${Hexadecimal(cfiUpdate.pc)} " + 663 // p"[predTaken] ${cfiUpdate.predTaken}, [taken] ${cfiUpdate.taken}, [isMisPred] ${cfiUpdate.isMisPred} " + 664 // p"[target] ${Hexadecimal(cfiUpdate.target)} " + 665 // p"------------------------------- " + 666 // p"[robPtr] f=${robIdx.flag} v=${robIdx.value} " + 667 // p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} " + 668 // p"[ftqOffset] ${ftqOffset} " + 669 // p"[level] ${level}, [interrupt] ${interrupt} " + 670 // p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value} " + 671 // p"[stFtqOffset] ${stFtqOffset} " + 672 // p"\n" 673 674 // } 675 676 def display(cond: Bool): Unit = { 677 XSDebug(cond, p"-----------BranchPredictionRedirect----------- \n") 678 XSDebug(cond, p"-----------cfiUpdate----------- \n") 679 XSDebug(cond, p"[pc] ${Hexadecimal(cfiUpdate.pc)}\n") 680 // XSDebug(cond, p"[hist] ${Binary(cfiUpdate.hist.predHist)}\n") 681 XSDebug(cond, p"[br_hit] ${cfiUpdate.br_hit} [isMisPred] ${cfiUpdate.isMisPred}\n") 682 XSDebug(cond, p"[pred_taken] ${cfiUpdate.predTaken} [taken] ${cfiUpdate.taken} [isMisPred] ${cfiUpdate.isMisPred}\n") 683 XSDebug(cond, p"[target] ${Hexadecimal(cfiUpdate.target)} \n") 684 XSDebug(cond, p"[shift] ${cfiUpdate.shift}\n") 685 XSDebug(cond, p"------------------------------- \n") 686 XSDebug(cond, p"[robPtr] f=${robIdx.flag} v=${robIdx.value}\n") 687 XSDebug(cond, p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} \n") 688 XSDebug(cond, p"[ftqOffset] ${ftqOffset} \n") 689 XSDebug(cond, p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value}\n") 690 XSDebug(cond, p"[stFtqOffset] ${stFtqOffset}\n") 691 XSDebug(cond, p"---------------------------------------------- \n") 692 } 693} 694