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 is_br_sharing = Bool() 401 402 // val call_is_rvc = Bool() 403 val hit = Bool() 404 405 def br_slot_valids = slot_valids.init 406 def tail_slot_valid = slot_valids.last 407 408 def br_valids = { 409 VecInit(br_slot_valids :+ (tail_slot_valid && is_br_sharing)) 410 } 411 412 def taken_mask_on_slot = { 413 VecInit( 414 (br_slot_valids zip br_taken_mask.init).map{ case (t, v) => t && v } :+ ( 415 tail_slot_valid && ( 416 is_br_sharing && br_taken_mask.last || !is_br_sharing 417 ) 418 ) 419 ) 420 } 421 422 def real_slot_taken_mask(): Vec[Bool] = { 423 VecInit(taken_mask_on_slot.map(_ && hit)) 424 } 425 426 // len numBr 427 def real_br_taken_mask(): Vec[Bool] = { 428 VecInit( 429 taken_mask_on_slot.map(_ && hit).init :+ 430 (br_taken_mask.last && tail_slot_valid && is_br_sharing && hit) 431 ) 432 } 433 434 // the vec indicating if ghr should shift on each branch 435 def shouldShiftVec = 436 VecInit(br_valids.zipWithIndex.map{ case (v, i) => 437 v && !real_br_taken_mask.take(i).reduceOption(_||_).getOrElse(false.B)}) 438 439 def lastBrPosOH = 440 VecInit((!hit || !br_valids.reduce(_||_)) +: // not hit or no brs in entry 441 (0 until numBr).map(i => 442 br_valids(i) && 443 !real_br_taken_mask.take(i).reduceOption(_||_).getOrElse(false.B) && // no brs taken in front it 444 (real_br_taken_mask()(i) || !br_valids.drop(i+1).reduceOption(_||_).getOrElse(false.B)) && // no brs behind it 445 hit 446 ) 447 ) 448 449 def brTaken = (br_valids zip br_taken_mask).map{ case (a, b) => a && b && hit}.reduce(_||_) 450 451 def target(pc: UInt): UInt = { 452 val targetVec = targets :+ fallThroughAddr :+ (pc + (FetchWidth * 4).U) 453 val tm = taken_mask_on_slot 454 val selVecOH = 455 tm.zipWithIndex.map{ case (t, i) => !tm.take(i).fold(false.B)(_||_) && t && hit} :+ 456 (!tm.asUInt.orR && hit) :+ !hit 457 Mux1H(selVecOH, targetVec) 458 } 459 460 def fallThruError: Bool = hit && fallThroughErr 461 462 def hit_taken_on_jmp = 463 !real_slot_taken_mask().init.reduce(_||_) && 464 real_slot_taken_mask().last && !is_br_sharing 465 def hit_taken_on_call = hit_taken_on_jmp && is_call 466 def hit_taken_on_ret = hit_taken_on_jmp && is_ret 467 def hit_taken_on_jalr = hit_taken_on_jmp && is_jalr 468 469 def cfiIndex = { 470 val cfiIndex = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 471 cfiIndex.valid := real_slot_taken_mask().asUInt.orR 472 // when no takens, set cfiIndex to PredictWidth-1 473 cfiIndex.bits := 474 ParallelPriorityMux(real_slot_taken_mask(), offsets) | 475 Fill(log2Ceil(PredictWidth), (!real_slot_taken_mask().asUInt.orR).asUInt) 476 cfiIndex 477 } 478 479 def taken = br_taken_mask.reduce(_||_) || slot_valids.last // || (is_jal || is_jalr) 480 481 def fromFtbEntry(entry: FTBEntry, pc: UInt, last_stage: Option[Tuple2[UInt, Bool]] = None) = { 482 slot_valids := entry.brSlots.map(_.valid) :+ entry.tailSlot.valid 483 targets := entry.getTargetVec(pc) 484 jalr_target := targets.last 485 offsets := entry.getOffsetVec 486 is_jal := entry.tailSlot.valid && entry.isJal 487 is_jalr := entry.tailSlot.valid && entry.isJalr 488 is_call := entry.tailSlot.valid && entry.isCall 489 is_ret := entry.tailSlot.valid && entry.isRet 490 is_br_sharing := entry.tailSlot.valid && entry.tailSlot.sharing 491 492 val startLower = Cat(0.U(1.W), pc(instOffsetBits+log2Ceil(PredictWidth)-1, instOffsetBits)) 493 val endLowerwithCarry = Cat(entry.carry, entry.pftAddr) 494 fallThroughErr := startLower >= endLowerwithCarry 495 fallThroughAddr := Mux(fallThroughErr, pc + (FetchWidth * 4).U, entry.getFallThrough(pc)) 496 } 497 498 def display(cond: Bool): Unit = { 499 XSDebug(cond, p"[taken_mask] ${Binary(br_taken_mask.asUInt)} [hit] $hit\n") 500 } 501} 502 503@chiselName 504class BranchPredictionBundle(implicit p: Parameters) extends XSBundle 505 with HasBPUConst with BPUUtils { 506 // def full_pred_info[T <: Data](x: T) = if (is_minimal) None else Some(x) 507 val pc = UInt(VAddrBits.W) 508 509 val valid = Bool() 510 511 val hasRedirect = Bool() 512 val ftq_idx = new FtqPtr 513 // val hit = Bool() 514 val is_minimal = Bool() 515 val minimal_pred = new MinimalBranchPrediction 516 val full_pred = new FullBranchPrediction 517 518 519 val folded_hist = new AllFoldedHistories(foldedGHistInfos) 520 val afhob = new AllAheadFoldedHistoryOldestBits(foldedGHistInfos) 521 val lastBrNumOH = UInt((numBr+1).W) 522 val histPtr = new CGHPtr 523 val rasSp = UInt(log2Ceil(RasSize).W) 524 val rasTop = new RASEntry 525 // val specCnt = Vec(numBr, UInt(10.W)) 526 // val meta = UInt(MaxMetaLength.W) 527 528 val ftb_entry = new FTBEntry() 529 530 def target(pc: UInt) = Mux(is_minimal, minimal_pred.target(pc), full_pred.target(pc)) 531 def cfiIndex = Mux(is_minimal, minimal_pred.cfiIndex, full_pred.cfiIndex) 532 def lastBrPosOH = Mux(is_minimal, minimal_pred.lastBrPosOH, full_pred.lastBrPosOH) 533 def brTaken = Mux(is_minimal, minimal_pred.brTaken, full_pred.brTaken) 534 def shouldShiftVec = Mux(is_minimal, minimal_pred.shouldShiftVec, full_pred.shouldShiftVec) 535 def fallThruError = Mux(is_minimal, minimal_pred.fallThruError, full_pred.fallThruError) 536 537 def getTarget = target(pc) 538 def taken = cfiIndex.valid 539 540 def display(cond: Bool): Unit = { 541 XSDebug(cond, p"[pc] ${Hexadecimal(pc)}\n") 542 folded_hist.display(cond) 543 full_pred.display(cond) 544 ftb_entry.display(cond) 545 } 546} 547 548@chiselName 549class BranchPredictionResp(implicit p: Parameters) extends XSBundle with HasBPUConst { 550 // val valids = Vec(3, Bool()) 551 val s1 = new BranchPredictionBundle 552 val s2 = new BranchPredictionBundle 553 val s3 = new BranchPredictionBundle 554 555 def selectedResp ={ 556 val res = 557 PriorityMux(Seq( 558 ((s3.valid && s3.hasRedirect) -> s3), 559 ((s2.valid && s2.hasRedirect) -> s2), 560 (s1.valid -> s1) 561 )) 562 // println("is minimal: ", res.is_minimal) 563 res 564 } 565 def selectedRespIdx = 566 PriorityMux(Seq( 567 ((s3.valid && s3.hasRedirect) -> BP_S3), 568 ((s2.valid && s2.hasRedirect) -> BP_S2), 569 (s1.valid -> BP_S1) 570 )) 571 def lastStage = s3 572} 573 574class BpuToFtqBundle(implicit p: Parameters) extends BranchPredictionResp with HasBPUConst { 575 val meta = UInt(MaxMetaLength.W) 576} 577 578object BpuToFtqBundle { 579 def apply(resp: BranchPredictionResp)(implicit p: Parameters): BpuToFtqBundle = { 580 val e = Wire(new BpuToFtqBundle()) 581 e.s1 := resp.s1 582 e.s2 := resp.s2 583 e.s3 := resp.s3 584 585 e.meta := DontCare 586 e 587 } 588} 589 590class BranchPredictionUpdate(implicit p: Parameters) extends BranchPredictionBundle with HasBPUConst { 591 val mispred_mask = Vec(numBr+1, Bool()) 592 val pred_hit = Bool() 593 val false_hit = Bool() 594 val new_br_insert_pos = Vec(numBr, Bool()) 595 val old_entry = Bool() 596 val meta = UInt(MaxMetaLength.W) 597 val full_target = UInt(VAddrBits.W) 598 val from_stage = UInt(2.W) 599 val ghist = UInt(HistoryLength.W) 600 601 def fromFtqRedirectSram(entry: Ftq_Redirect_SRAMEntry) = { 602 folded_hist := entry.folded_hist 603 afhob := entry.afhob 604 lastBrNumOH := entry.lastBrNumOH 605 histPtr := entry.histPtr 606 rasSp := entry.rasSp 607 rasTop := entry.rasEntry 608 this 609 } 610 611 override def display(cond: Bool) = { 612 XSDebug(cond, p"-----------BranchPredictionUpdate-----------\n") 613 XSDebug(cond, p"[mispred_mask] ${Binary(mispred_mask.asUInt)} [false_hit] $false_hit\n") 614 XSDebug(cond, p"[new_br_insert_pos] ${Binary(new_br_insert_pos.asUInt)}\n") 615 super.display(cond) 616 XSDebug(cond, p"--------------------------------------------\n") 617 } 618} 619 620class BranchPredictionRedirect(implicit p: Parameters) extends Redirect with HasBPUConst { 621 // override def toPrintable: Printable = { 622 // p"-----------BranchPredictionRedirect----------- " + 623 // p"-----------cfiUpdate----------- " + 624 // p"[pc] ${Hexadecimal(cfiUpdate.pc)} " + 625 // p"[predTaken] ${cfiUpdate.predTaken}, [taken] ${cfiUpdate.taken}, [isMisPred] ${cfiUpdate.isMisPred} " + 626 // p"[target] ${Hexadecimal(cfiUpdate.target)} " + 627 // p"------------------------------- " + 628 // p"[robPtr] f=${robIdx.flag} v=${robIdx.value} " + 629 // p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} " + 630 // p"[ftqOffset] ${ftqOffset} " + 631 // p"[level] ${level}, [interrupt] ${interrupt} " + 632 // p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value} " + 633 // p"[stFtqOffset] ${stFtqOffset} " + 634 // p"\n" 635 636 // } 637 638 def display(cond: Bool): Unit = { 639 XSDebug(cond, p"-----------BranchPredictionRedirect----------- \n") 640 XSDebug(cond, p"-----------cfiUpdate----------- \n") 641 XSDebug(cond, p"[pc] ${Hexadecimal(cfiUpdate.pc)}\n") 642 // XSDebug(cond, p"[hist] ${Binary(cfiUpdate.hist.predHist)}\n") 643 XSDebug(cond, p"[br_hit] ${cfiUpdate.br_hit} [isMisPred] ${cfiUpdate.isMisPred}\n") 644 XSDebug(cond, p"[pred_taken] ${cfiUpdate.predTaken} [taken] ${cfiUpdate.taken} [isMisPred] ${cfiUpdate.isMisPred}\n") 645 XSDebug(cond, p"[target] ${Hexadecimal(cfiUpdate.target)} \n") 646 XSDebug(cond, p"[shift] ${cfiUpdate.shift}\n") 647 XSDebug(cond, p"------------------------------- \n") 648 XSDebug(cond, p"[robPtr] f=${robIdx.flag} v=${robIdx.value}\n") 649 XSDebug(cond, p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} \n") 650 XSDebug(cond, p"[ftqOffset] ${ftqOffset} \n") 651 XSDebug(cond, p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value}\n") 652 XSDebug(cond, p"[stFtqOffset] ${stFtqOffset}\n") 653 XSDebug(cond, p"---------------------------------------------- \n") 654 } 655} 656