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