1package xiangshan.frontend 2 3import chisel3._ 4import chisel3.util._ 5import utils._ 6import xiangshan._ 7import xiangshan.backend.ALUOpType 8import xiangshan.backend.JumpOpType 9import chisel3.experimental.chiselName 10 11trait HasBPUParameter extends HasXSParameter { 12 val BPUDebug = true 13 val EnableCFICommitLog = true 14 val EnbaleCFIPredLog = true 15 val EnableBPUTimeRecord = EnableCFICommitLog || EnbaleCFIPredLog 16} 17 18class TableAddr(val idxBits: Int, val banks: Int) extends XSBundle with HasIFUConst { 19 def tagBits = VAddrBits - idxBits - instOffsetBits 20 21 val tag = UInt(tagBits.W) 22 val idx = UInt(idxBits.W) 23 val offset = UInt(instOffsetBits.W) 24 25 def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this) 26 def getTag(x: UInt) = fromUInt(x).tag 27 def getIdx(x: UInt) = fromUInt(x).idx 28 def getBank(x: UInt) = getIdx(x)(log2Up(banks) - 1, 0) 29 def getBankIdx(x: UInt) = getIdx(x)(idxBits - 1, log2Up(banks)) 30} 31 32class PredictorResponse extends XSBundle { 33 class UbtbResp extends XSBundle { 34 // the valid bits indicates whether a target is hit 35 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 36 val hits = Vec(PredictWidth, Bool()) 37 val takens = Vec(PredictWidth, Bool()) 38 val brMask = Vec(PredictWidth, Bool()) 39 val is_RVC = Vec(PredictWidth, Bool()) 40 } 41 class BtbResp extends XSBundle { 42 // the valid bits indicates whether a target is hit 43 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 44 val hits = Vec(PredictWidth, Bool()) 45 val types = Vec(PredictWidth, UInt(2.W)) 46 val isRVC = Vec(PredictWidth, Bool()) 47 } 48 class BimResp extends XSBundle { 49 val ctrs = Vec(PredictWidth, UInt(2.W)) 50 } 51 class TageResp extends XSBundle { 52 // the valid bits indicates whether a prediction is hit 53 val takens = Vec(PredictWidth, Bool()) 54 val hits = Vec(PredictWidth, Bool()) 55 } 56 class LoopResp extends XSBundle { 57 val exit = Vec(PredictWidth, Bool()) 58 } 59 60 val ubtb = new UbtbResp 61 val btb = new BtbResp 62 val bim = new BimResp 63 val tage = new TageResp 64 val loop = new LoopResp 65} 66 67trait PredictorUtils { 68 // circular shifting 69 def circularShiftLeft(source: UInt, len: Int, shamt: UInt): UInt = { 70 val res = Wire(UInt(len.W)) 71 val higher = source << shamt 72 val lower = source >> (len.U - shamt) 73 res := higher | lower 74 res 75 } 76 77 def circularShiftRight(source: UInt, len: Int, shamt: UInt): UInt = { 78 val res = Wire(UInt(len.W)) 79 val higher = source << (len.U - shamt) 80 val lower = source >> shamt 81 res := higher | lower 82 res 83 } 84 85 // To be verified 86 def satUpdate(old: UInt, len: Int, taken: Bool): UInt = { 87 val oldSatTaken = old === ((1 << len)-1).U 88 val oldSatNotTaken = old === 0.U 89 Mux(oldSatTaken && taken, ((1 << len)-1).U, 90 Mux(oldSatNotTaken && !taken, 0.U, 91 Mux(taken, old + 1.U, old - 1.U))) 92 } 93 94 def signedSatUpdate(old: SInt, len: Int, taken: Bool): SInt = { 95 val oldSatTaken = old === ((1 << (len-1))-1).S 96 val oldSatNotTaken = old === (-(1 << (len-1))).S 97 Mux(oldSatTaken && taken, ((1 << (len-1))-1).S, 98 Mux(oldSatNotTaken && !taken, (-(1 << (len-1))).S, 99 Mux(taken, old + 1.S, old - 1.S))) 100 } 101} 102 103trait HasIFUFire { this: MultiIOModule => 104 val fires = IO(Input(Vec(4, Bool()))) 105 val s1_fire = fires(0) 106 val s2_fire = fires(1) 107 val s3_fire = fires(2) 108 val out_fire = fires(3) 109} 110abstract class BasePredictor extends XSModule 111 with HasBPUParameter with HasIFUConst with PredictorUtils 112 with HasIFUFire { 113 val metaLen = 0 114 115 // An implementation MUST extend the IO bundle with a response 116 // and the special input from other predictors, as well as 117 // the metas to store in BRQ 118 abstract class Resp extends XSBundle {} 119 abstract class FromOthers extends XSBundle {} 120 abstract class Meta extends XSBundle {} 121 122 class DefaultBasePredictorIO extends XSBundle { 123 val flush = Input(Bool()) 124 val pc = Flipped(ValidIO(UInt(VAddrBits.W))) 125 val hist = Input(UInt(HistoryLength.W)) 126 val inMask = Input(UInt(PredictWidth.W)) 127 val update = Flipped(ValidIO(new CfiUpdateInfo)) 128 } 129 130 val io = new DefaultBasePredictorIO 131 val debug = true 132} 133 134class BPUStageIO extends XSBundle { 135 val pc = UInt(VAddrBits.W) 136 val mask = UInt(PredictWidth.W) 137 val resp = new PredictorResponse 138 // val target = UInt(VAddrBits.W) 139 val brInfo = Vec(PredictWidth, new BpuMeta) 140 // val saveHalfRVI = Bool() 141} 142 143 144abstract class BPUStage extends XSModule with HasBPUParameter 145 with HasIFUConst with HasIFUFire { 146 class DefaultIO extends XSBundle { 147 val flush = Input(Bool()) 148 val in = Input(new BPUStageIO) 149 val inFire = Input(Bool()) 150 val pred = Output(new BranchPrediction) // to ifu 151 val out = Output(new BPUStageIO) // to the next stage 152 val outFire = Input(Bool()) 153 154 val debug_hist = Input(UInt((if (BPUDebug) (HistoryLength) else 0).W)) 155 // val debug_histPtr = Input(UInt((if (BPUDebug) (ExtHistoryLength) else 0).W)) 156 } 157 val io = IO(new DefaultIO) 158 159 def npc(pc: UInt, instCount: UInt) = pc + (instCount << instOffsetBits.U) 160 161 val inLatch = RegEnable(io.in, io.inFire) 162 163 // Each stage has its own logic to decide 164 // takens, notTakens and target 165 166 val takens = Wire(Vec(PredictWidth, Bool())) 167 // val notTakens = Wire(Vec(PredictWidth, Bool())) 168 val brMask = Wire(Vec(PredictWidth, Bool())) 169 val jalMask = Wire(Vec(PredictWidth, Bool())) 170 171 val targets = Wire(Vec(PredictWidth, UInt(VAddrBits.W))) 172 173 val firstBankHasHalfRVI = Wire(Bool()) 174 val lastBankHasHalfRVI = Wire(Bool()) 175 val lastBankHasInst = WireInit(inLatch.mask(PredictWidth-1, bankWidth).orR) 176 177 io.pred <> DontCare 178 io.pred.takens := takens.asUInt 179 io.pred.brMask := brMask.asUInt 180 io.pred.jalMask := jalMask.asUInt 181 io.pred.targets := targets 182 io.pred.firstBankHasHalfRVI := firstBankHasHalfRVI 183 io.pred.lastBankHasHalfRVI := lastBankHasHalfRVI 184 185 io.out <> DontCare 186 io.out.pc := inLatch.pc 187 io.out.mask := inLatch.mask 188 io.out.resp <> inLatch.resp 189 io.out.brInfo := inLatch.brInfo 190 (0 until PredictWidth).map(i => io.out.brInfo(i).sawNotTakenBranch := io.pred.sawNotTakenBr(i)) 191 192 if (BPUDebug) { 193 val jmpIdx = io.pred.jmpIdx 194 val taken = io.pred.taken 195 val target = Mux(taken, io.pred.targets(jmpIdx), snpc(inLatch.pc)) 196 XSDebug("in(%d): pc=%x, mask=%b\n", io.inFire, io.in.pc, io.in.mask) 197 XSDebug("inLatch: pc=%x, mask=%b\n", inLatch.pc, inLatch.mask) 198 XSDebug("out(%d): pc=%x, mask=%b, taken=%d, jmpIdx=%d, target=%x, firstHasHalfRVI=%d, lastHasHalfRVI=%d\n", 199 io.outFire, io.out.pc, io.out.mask, taken, jmpIdx, target, firstBankHasHalfRVI, lastBankHasHalfRVI) 200 XSDebug("flush=%d\n", io.flush) 201 val p = io.pred 202 } 203} 204 205@chiselName 206class BPUStage1 extends BPUStage { 207 208 // ubtb is accessed with inLatch pc in s1, 209 // so we use io.in instead of inLatch 210 val ubtbResp = io.in.resp.ubtb 211 // the read operation is already masked, so we do not need to mask here 212 takens := VecInit((0 until PredictWidth).map(i => ubtbResp.takens(i))) 213 // notTakens := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && !ubtbResp.takens(i) && ubtbResp.brMask(i))) 214 brMask := ubtbResp.brMask 215 jalMask := DontCare 216 targets := ubtbResp.targets 217 218 firstBankHasHalfRVI := Mux(lastBankHasInst, false.B, ubtbResp.hits(bankWidth-1) && !ubtbResp.is_RVC(bankWidth-1)) && HasCExtension.B 219 lastBankHasHalfRVI := ubtbResp.hits(PredictWidth-1) && !ubtbResp.is_RVC(PredictWidth-1) && HasCExtension.B 220 221 // resp and brInfo are from the components, 222 // so it does not need to be latched 223 io.out.resp <> io.in.resp 224 io.out.brInfo := io.in.brInfo 225 226 if (BPUDebug) { 227 XSDebug(io.outFire, "outPred using ubtb resp: hits:%b, takens:%b, notTakens:%b, isRVC:%b\n", 228 ubtbResp.hits.asUInt, ubtbResp.takens.asUInt, ~ubtbResp.takens.asUInt & brMask.asUInt, ubtbResp.is_RVC.asUInt) 229 } 230 if (EnableBPUTimeRecord) { 231 io.out.brInfo.map(_.debug_ubtb_cycle := GTimer()) 232 } 233} 234@chiselName 235class BPUStage2 extends BPUStage { 236 // Use latched response from s1 237 val btbResp = inLatch.resp.btb 238 val bimResp = inLatch.resp.bim 239 takens := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && (btbResp.types(i) === BTBtype.B && bimResp.ctrs(i)(1) || btbResp.types(i) =/= BTBtype.B))) 240 targets := btbResp.targets 241 brMask := VecInit((0 until PredictWidth).map(i => btbResp.types(i) === BTBtype.B && btbResp.hits(i))) 242 jalMask := DontCare 243 244 firstBankHasHalfRVI := Mux(lastBankHasInst, false.B, btbResp.hits(bankWidth-1) && !btbResp.isRVC(bankWidth-1) && inLatch.mask(bankWidth-1)) && HasCExtension.B 245 lastBankHasHalfRVI := btbResp.hits(PredictWidth-1) && !btbResp.isRVC(PredictWidth-1) && inLatch.mask(PredictWidth-1) && HasCExtension.B 246 247 if (BPUDebug) { 248 XSDebug(io.outFire, "outPred using btb&bim resp: hits:%b, ctrTakens:%b\n", 249 btbResp.hits.asUInt, VecInit(bimResp.ctrs.map(_(1))).asUInt) 250 } 251 if (EnableBPUTimeRecord) { 252 io.out.brInfo.map(_.debug_btb_cycle := GTimer()) 253 } 254} 255@chiselName 256class BPUStage3 extends BPUStage { 257 class S3IO extends XSBundle { 258 259 val predecode = Input(new Predecode) 260 val realMask = Input(UInt(PredictWidth.W)) 261 val prevHalf = Flipped(ValidIO(new PrevHalfInstr)) 262 val recover = Flipped(ValidIO(new CfiUpdateInfo)) 263 } 264 val s3IO = IO(new S3IO) 265 // TAGE has its own pipelines and the 266 // response comes directly from s3, 267 // so we do not use those from inLatch 268 val tageResp = io.in.resp.tage 269 val tageTakens = tageResp.takens 270 271 val loopResp = io.in.resp.loop.exit 272 273 // realMask is in it 274 val pdMask = s3IO.predecode.mask 275 val pdLastHalf = s3IO.predecode.lastHalf 276 val pds = s3IO.predecode.pd 277 278 val btbResp = WireInit(inLatch.resp.btb) 279 val btbHits = WireInit(btbResp.hits.asUInt) 280 val bimTakens = VecInit(inLatch.resp.bim.ctrs.map(_(1))) 281 282 val brs = pdMask & Reverse(Cat(pds.map(_.isBr))) 283 val jals = pdMask & Reverse(Cat(pds.map(_.isJal))) 284 val jalrs = pdMask & Reverse(Cat(pds.map(_.isJalr))) 285 val calls = pdMask & Reverse(Cat(pds.map(_.isCall))) 286 val rets = pdMask & Reverse(Cat(pds.map(_.isRet))) 287 val RVCs = pdMask & Reverse(Cat(pds.map(_.isRVC))) 288 289 val callIdx = PriorityEncoder(calls) 290 val retIdx = PriorityEncoder(rets) 291 292 val brPred = (if(EnableBPD) tageTakens else bimTakens).asUInt 293 val loopRes = (if (EnableLoop) loopResp else VecInit(Fill(PredictWidth, 0.U(1.W)))).asUInt 294 val prevHalfTaken = s3IO.prevHalf.valid && s3IO.prevHalf.bits.taken && HasCExtension.B 295 val prevHalfTakenMask = prevHalfTaken.asUInt 296 val brTakens = ((brs & brPred | prevHalfTakenMask) & ~loopRes) 297 // VecInit((0 until PredictWidth).map(i => brs(i) && (brPred(i) || (if (i == 0) prevHalfTaken else false.B)) && !loopRes(i))) 298 // we should provide btb resp as well 299 btbHits := btbResp.hits.asUInt | prevHalfTakenMask 300 301 // predict taken only if btb has a target, jal targets will be provided by IFU 302 takens := VecInit((0 until PredictWidth).map(i => (brTakens(i) || jalrs(i)) && btbHits(i) || jals(i))) 303 304 305 targets := inLatch.resp.btb.targets 306 307 brMask := WireInit(brs.asTypeOf(Vec(PredictWidth, Bool()))) 308 jalMask := WireInit(jals.asTypeOf(Vec(PredictWidth, Bool()))) 309 310 lastBankHasInst := s3IO.realMask(PredictWidth-1, bankWidth).orR 311 firstBankHasHalfRVI := Mux(lastBankHasInst, false.B, pdLastHalf(0)) && HasCExtension.B 312 lastBankHasHalfRVI := pdLastHalf(1) && HasCExtension.B 313 314 //RAS 315 if(EnableRAS){ 316 val ras = Module(new RAS) 317 ras.io <> DontCare 318 ras.io.pc.bits := bankAligned(inLatch.pc) 319 ras.io.pc.valid := io.outFire//predValid 320 ras.io.is_ret := rets.orR && (retIdx === io.pred.jmpIdx) 321 ras.io.callIdx.valid := calls.orR && (callIdx === io.pred.jmpIdx) 322 ras.io.callIdx.bits := callIdx 323 ras.io.isRVC := (calls & RVCs).orR //TODO: this is ugly 324 ras.io.isLastHalfRVI := s3IO.predecode.hasLastHalfRVI 325 ras.io.recover := s3IO.recover 326 ras.fires <> fires 327 328 for(i <- 0 until PredictWidth){ 329 io.out.brInfo(i).rasSp := ras.io.meta.rasSp 330 io.out.brInfo(i).rasTopCtr := ras.io.meta.rasTopCtr 331 io.out.brInfo(i).rasToqAddr := ras.io.meta.rasToqAddr 332 } 333 takens := VecInit((0 until PredictWidth).map(i => { 334 ((brTakens(i) || jalrs(i)) && btbHits(i)) || 335 jals(i) || 336 (ras.io.out.valid && rets(i)) || 337 (!ras.io.out.valid && rets(i) && btbHits(i)) 338 } 339 )) 340 341 for (i <- 0 until PredictWidth) { 342 when(rets(i) && ras.io.out.valid){ 343 targets(i) := ras.io.out.bits.target 344 } 345 } 346 } 347 348 349 // we should provide the prediction for the first half RVI of the end of a fetch packet 350 // branch taken information would be lost in the prediction of the next packet, 351 // so we preserve this information here 352 when (firstBankHasHalfRVI && btbResp.types(bankWidth-1) === BTBtype.B && btbHits(bankWidth-1) && HasCExtension.B) { 353 takens(bankWidth-1) := brPred(bankWidth-1) && !loopRes(bankWidth-1) 354 } 355 when (lastBankHasHalfRVI && btbResp.types(PredictWidth-1) === BTBtype.B && btbHits(PredictWidth-1) && HasCExtension.B) { 356 takens(PredictWidth-1) := brPred(PredictWidth-1) && !loopRes(PredictWidth-1) 357 } 358 359 // targets would be lost as well, since it is from btb 360 // unless it is a ret, which target is from ras 361 when (prevHalfTaken && !rets(0) && HasCExtension.B) { 362 targets(0) := s3IO.prevHalf.bits.target 363 } 364 365 // Wrap tage resp and tage meta in 366 // This is ugly 367 io.out.resp.tage <> io.in.resp.tage 368 io.out.resp.loop <> io.in.resp.loop 369 for (i <- 0 until PredictWidth) { 370 io.out.brInfo(i).tageMeta := io.in.brInfo(i).tageMeta 371 io.out.brInfo(i).specCnt := io.in.brInfo(i).specCnt 372 } 373 374 if (BPUDebug) { 375 XSDebug(io.inFire, "predecode: pc:%x, mask:%b\n", inLatch.pc, s3IO.predecode.mask) 376 for (i <- 0 until PredictWidth) { 377 val p = s3IO.predecode.pd(i) 378 XSDebug(io.inFire && s3IO.predecode.mask(i), "predecode(%d): brType:%d, br:%d, jal:%d, jalr:%d, call:%d, ret:%d, RVC:%d, excType:%d\n", 379 i.U, p.brType, p.isBr, p.isJal, p.isJalr, p.isCall, p.isRet, p.isRVC, p.excType) 380 } 381 XSDebug(p"brs:${Binary(brs)} jals:${Binary(jals)} jalrs:${Binary(jalrs)} calls:${Binary(calls)} rets:${Binary(rets)} rvcs:${Binary(RVCs)}\n") 382 XSDebug(p"callIdx:${callIdx} retIdx:${retIdx}\n") 383 XSDebug(p"brPred:${Binary(brPred)} loopRes:${Binary(loopRes)} prevHalfTaken:${prevHalfTaken} brTakens:${Binary(brTakens)}\n") 384 } 385 386 if (EnbaleCFIPredLog) { 387 val out = io.out 388 XSDebug(io.outFire, p"cfi_pred: fetchpc(${Hexadecimal(out.pc)}) mask(${out.mask}) brmask(${brMask.asUInt}) hist(${Hexadecimal(io.debug_hist)})\n") 389 } 390 391 if (EnableBPUTimeRecord) { 392 io.out.brInfo.map(_.debug_tage_cycle := GTimer()) 393 } 394} 395 396trait BranchPredictorComponents extends HasXSParameter { 397 val ubtb = Module(new MicroBTB) 398 val btb = Module(new BTB) 399 val bim = Module(new BIM) 400 val tage = (if(EnableBPD) { Module(new Tage) } 401 else { Module(new FakeTage) }) 402 val loop = Module(new LoopPredictor) 403 val preds = Seq(ubtb, btb, bim, tage, loop) 404 preds.map(_.io := DontCare) 405} 406 407class BPUReq extends XSBundle { 408 val pc = UInt(VAddrBits.W) 409 val hist = UInt(HistoryLength.W) 410 val inMask = UInt(PredictWidth.W) 411 // val histPtr = UInt(log2Up(ExtHistoryLength).W) // only for debug 412} 413 414// class CfiUpdateInfoWithHist extends XSBundle { 415// val ui = new CfiUpdateInfo 416// val hist = UInt(HistoryLength.W) 417// } 418 419// object CfiUpdateInfoWithHist { 420// def apply (brInfo: CfiUpdateInfo, hist: UInt) = { 421// val b = Wire(new CfiUpdateInfoWithHist) 422// b.ui <> brInfo 423// b.hist := hist 424// b 425// } 426// } 427 428abstract class BaseBPU extends XSModule with BranchPredictorComponents with HasBPUParameter{ 429 val io = IO(new Bundle() { 430 // from backend 431 val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo)) 432 // val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfoWithHist)) 433 // from ifu, frontend redirect 434 val flush = Input(Vec(3, Bool())) 435 // from if1 436 val in = Input(new BPUReq) 437 val inFire = Input(Vec(4, Bool())) 438 // to if2/if3/if4 439 val out = Vec(3, Output(new BranchPrediction)) 440 // from if4 441 val predecode = Input(new Predecode) 442 val realMask = Input(UInt(PredictWidth.W)) 443 val prevHalf = Flipped(ValidIO(new PrevHalfInstr)) 444 // to if4, some bpu info used for updating 445 val bpuMeta = Output(Vec(PredictWidth, new BpuMeta)) 446 }) 447 448 def npc(pc: UInt, instCount: UInt) = pc + (instCount << 1.U) 449 450 preds.map(p => { 451 p.io.update <> io.cfiUpdateInfo 452 p.fires <> io.inFire 453 }) 454 455 // tage.io.update <> io.cfiUpdateInfo 456 457 val s1 = Module(new BPUStage1) 458 val s2 = Module(new BPUStage2) 459 val s3 = Module(new BPUStage3) 460 461 Seq(s1, s2, s3).foreach(s => s.fires <> io.inFire) 462 463 val s1_fire = io.inFire(0) 464 val s2_fire = io.inFire(1) 465 val s3_fire = io.inFire(2) 466 val s4_fire = io.inFire(3) 467 468 s1.io.flush := io.flush(0) 469 s2.io.flush := io.flush(1) 470 s3.io.flush := io.flush(2) 471 472 s1.io.in <> DontCare 473 s2.io.in <> s1.io.out 474 s3.io.in <> s2.io.out 475 476 s1.io.inFire := s1_fire 477 s2.io.inFire := s2_fire 478 s3.io.inFire := s3_fire 479 480 s1.io.outFire := s2_fire 481 s2.io.outFire := s3_fire 482 s3.io.outFire := s4_fire 483 484 io.out(0) <> s1.io.pred 485 io.out(1) <> s2.io.pred 486 io.out(2) <> s3.io.pred 487 488 io.bpuMeta := s3.io.out.brInfo 489 490 if (BPUDebug) { 491 XSDebug(io.inFire(3), "bpuMeta sent!\n") 492 for (i <- 0 until PredictWidth) { 493 val b = io.bpuMeta(i) 494 XSDebug(io.inFire(3), "brInfo(%d): ubtbWrWay:%d, ubtbHit:%d, btbWrWay:%d, btbHitJal:%d, bimCtr:%d, fetchIdx:%d\n", 495 i.U, b.ubtbWriteWay, b.ubtbHits, b.btbWriteWay, b.btbHitJal, b.bimCtr, b.fetchIdx) 496 val t = b.tageMeta 497 XSDebug(io.inFire(3), " tageMeta: pvder(%d):%d, altDiffers:%d, pvderU:%d, pvderCtr:%d, allocate(%d):%d\n", 498 t.provider.valid, t.provider.bits, t.altDiffers, t.providerU, t.providerCtr, t.allocate.valid, t.allocate.bits) 499 } 500 } 501 val debug_verbose = false 502} 503 504 505class FakeBPU extends BaseBPU { 506 io.out.foreach(i => { 507 // Provide not takens 508 i <> DontCare 509 i.takens := 0.U 510 }) 511 io.bpuMeta <> DontCare 512} 513@chiselName 514class BPU extends BaseBPU { 515 516 //**********************Stage 1****************************// 517 518 val s1_resp_in = Wire(new PredictorResponse) 519 val s1_brInfo_in = Wire(Vec(PredictWidth, new BpuMeta)) 520 521 s1_resp_in.tage := DontCare 522 s1_resp_in.loop := DontCare 523 s1_brInfo_in := DontCare 524 (0 until PredictWidth).foreach(i => s1_brInfo_in(i).fetchIdx := i.U) 525 526 val s1_inLatch = RegEnable(io.in, s1_fire) 527 ubtb.io.flush := io.flush(0) // TODO: fix this 528 ubtb.io.pc.valid := s2_fire 529 ubtb.io.pc.bits := s1_inLatch.pc 530 ubtb.io.inMask := s1_inLatch.inMask 531 532 533 534 // Wrap ubtb response into resp_in and brInfo_in 535 s1_resp_in.ubtb <> ubtb.io.out 536 for (i <- 0 until PredictWidth) { 537 s1_brInfo_in(i).ubtbWriteWay := ubtb.io.uBTBMeta.writeWay(i) 538 s1_brInfo_in(i).ubtbHits := ubtb.io.uBTBMeta.hits(i) 539 } 540 541 btb.io.flush := io.flush(0) // TODO: fix this 542 btb.io.pc.valid := s1_fire 543 btb.io.pc.bits := io.in.pc 544 btb.io.inMask := io.in.inMask 545 546 547 548 // Wrap btb response into resp_in and brInfo_in 549 s1_resp_in.btb <> btb.io.resp 550 for (i <- 0 until PredictWidth) { 551 s1_brInfo_in(i).btbWriteWay := btb.io.meta.writeWay(i) 552 s1_brInfo_in(i).btbHitJal := btb.io.meta.hitJal(i) 553 } 554 555 bim.io.flush := io.flush(0) // TODO: fix this 556 bim.io.pc.valid := s1_fire 557 bim.io.pc.bits := io.in.pc 558 bim.io.inMask := io.in.inMask 559 560 561 // Wrap bim response into resp_in and brInfo_in 562 s1_resp_in.bim <> bim.io.resp 563 for (i <- 0 until PredictWidth) { 564 s1_brInfo_in(i).bimCtr := bim.io.meta.ctrs(i) 565 } 566 567 568 s1.io.inFire := s1_fire 569 s1.io.in.pc := io.in.pc 570 s1.io.in.mask := io.in.inMask 571 s1.io.in.resp <> s1_resp_in 572 s1.io.in.brInfo <> s1_brInfo_in 573 574 val s1_hist = RegEnable(io.in.hist, enable=s1_fire) 575 val s2_hist = RegEnable(s1_hist, enable=s2_fire) 576 val s3_hist = RegEnable(s2_hist, enable=s3_fire) 577 578 s1.io.debug_hist := s1_hist 579 s2.io.debug_hist := s2_hist 580 s3.io.debug_hist := s3_hist 581 582 //**********************Stage 2****************************// 583 tage.io.flush := io.flush(1) // TODO: fix this 584 tage.io.pc.valid := s2_fire 585 tage.io.pc.bits := s2.io.in.pc // PC from s1 586 tage.io.hist := s1_hist // The inst is from s1 587 tage.io.inMask := s2.io.in.mask 588 // tage.io.s3Fire := s3_fire // Tell tage to march 1 stage 589 tage.io.bim <> s1.io.out.resp.bim // Use bim results from s1 590 591 //**********************Stage 3****************************// 592 // Wrap tage response and meta into s3.io.in.bits 593 // This is ugly 594 595 loop.io.flush := io.flush(2) 596 loop.io.pc.valid := s2_fire 597 loop.io.if3_fire := s3_fire 598 loop.io.pc.bits := s2.io.in.pc 599 loop.io.inMask := io.predecode.mask 600 // loop.io.outFire := s4_fire 601 loop.io.respIn.taken := s3.io.pred.taken 602 loop.io.respIn.jmpIdx := s3.io.pred.jmpIdx 603 604 605 s3.io.in.resp.tage <> tage.io.resp 606 s3.io.in.resp.loop <> loop.io.resp 607 for (i <- 0 until PredictWidth) { 608 s3.io.in.brInfo(i).tageMeta := tage.io.meta(i) 609 s3.io.in.brInfo(i).specCnt := loop.io.meta.specCnts(i) 610 } 611 612 s3.s3IO.predecode <> io.predecode 613 614 s3.s3IO.realMask := io.realMask 615 616 s3.s3IO.prevHalf := io.prevHalf 617 618 s3.s3IO.recover.valid <> io.cfiUpdateInfo.valid 619 s3.s3IO.recover.bits <> io.cfiUpdateInfo.bits 620 621 if (BPUDebug) { 622 if (debug_verbose) { 623 val uo = ubtb.io.out 624 XSDebug("debug: ubtb hits:%b, takens:%b, notTakens:%b\n", uo.hits.asUInt, uo.takens.asUInt, ~uo.takens.asUInt & uo.brMask.asUInt) 625 val bio = bim.io.resp 626 XSDebug("debug: bim takens:%b\n", VecInit(bio.ctrs.map(_(1))).asUInt) 627 val bo = btb.io.resp 628 XSDebug("debug: btb hits:%b\n", bo.hits.asUInt) 629 } 630 } 631 632 633 634 if (EnableCFICommitLog) { 635 val buValid = io.cfiUpdateInfo.valid && !io.cfiUpdateInfo.bits.isReplay 636 val buinfo = io.cfiUpdateInfo.bits 637 val pd = buinfo.pd 638 val tage_cycle = buinfo.bpuMeta.debug_tage_cycle 639 XSDebug(buValid, p"cfi_update: isBr(${pd.isBr}) pc(${Hexadecimal(buinfo.pc)}) taken(${buinfo.taken}) mispred(${buinfo.isMisPred}) cycle($tage_cycle) hist(${Hexadecimal(buinfo.bpuMeta.predHist.asUInt)})\n") 640 } 641 642} 643 644object BPU{ 645 def apply(enableBPU: Boolean = true) = { 646 if(enableBPU) { 647 val BPU = Module(new BPU) 648 BPU 649 } 650 else { 651 val FakeBPU = Module(new FakeBPU) 652 FakeBPU 653 } 654 } 655} 656