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