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