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