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