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