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