xref: /XiangShan/src/main/scala/xiangshan/frontend/Bim.scala (revision 0be64786e3f92090f2feec39645c2052ed97cd82)
1package xiangshan.frontend
2
3import chisel3._
4import chisel3.util._
5import xiangshan._
6import utils._
7import chisel3.experimental.chiselName
8
9trait BimParams extends HasXSParameter {
10  val BimBanks = PredictWidth
11  val BimSize = 4096
12  val nRows = BimSize / BimBanks
13  val bypassEntries = 4
14}
15
16@chiselName
17class BIM extends BasePredictor with BimParams {
18  class BIMResp extends Resp {
19    val ctrs = Vec(PredictWidth, UInt(2.W))
20  }
21  class BIMMeta extends Meta {
22    val ctrs = Vec(PredictWidth, UInt(2.W))
23  }
24  class BIMFromOthers extends FromOthers {}
25
26  class BIMIO extends DefaultBasePredictorIO {
27    val resp = Output(new BIMResp)
28    val meta = Output(new BIMMeta)
29  }
30
31  override val io = IO(new BIMIO)
32  override val debug = true
33
34  val bimAddr = new TableAddr(log2Up(BimSize), BimBanks)
35
36  val bim = Module(new SRAMTemplate(UInt(2.W), set = nRows, way=BimBanks, shouldReset = false, holdRead = true))
37
38  val doing_reset = RegInit(true.B)
39  val resetRow = RegInit(0.U(log2Ceil(nRows).W))
40  resetRow := resetRow + doing_reset
41  when (resetRow === (nRows-1).U) { doing_reset := false.B }
42
43  val if1_packetAlignedPC = packetAligned(io.pc.bits)
44  val if2_pc = RegEnable(if1_packetAlignedPC, io.pc.valid)
45
46  val if1_mask = io.inMask
47  val if1_row  = bimAddr.getBankIdx(if1_packetAlignedPC)
48
49  bim.io.r.req.valid := io.pc.valid
50  bim.io.r.req.bits.setIdx := if1_row
51
52  val if2_bimRead = bim.io.r.resp.data
53  val ctrlMask = Fill(if2_bimRead.getWidth, ctrl.bim_enable.asUInt).asTypeOf(if2_bimRead)
54  io.resp.ctrs  := VecInit(if2_bimRead zip ctrlMask map {case (a, b) => a & b})
55  io.meta.ctrs  := if2_bimRead
56
57  val updateValid = RegNext(io.update.valid)
58  val u = RegNext(io.update.bits)
59
60  val updateRow = bimAddr.getBankIdx(u.ftqPC)
61
62
63  val wrbypass_ctrs       = Reg(Vec(bypassEntries, Vec(BimBanks, UInt(2.W))))
64  val wrbypass_ctr_valids = Reg(Vec(bypassEntries, Vec(BimBanks, Bool())))
65  val wrbypass_rows     = Reg(Vec(bypassEntries, UInt(log2Up(nRows).W)))
66  val wrbypass_enq_idx  = RegInit(0.U(log2Up(bypassEntries).W))
67
68  val wrbypass_hits = VecInit((0 until bypassEntries).map( i =>
69    !doing_reset && wrbypass_rows(i) === updateRow))
70  val wrbypass_hit = wrbypass_hits.reduce(_||_)
71  val wrbypass_hit_idx = PriorityEncoder(wrbypass_hits)
72
73  val oldCtrs = VecInit((0 until BimBanks).map(b =>
74                  Mux(wrbypass_hit && wrbypass_ctr_valids(wrbypass_hit_idx)(b),
75                    wrbypass_ctrs(wrbypass_hit_idx)(b), u.metas(b).bimCtr)))
76
77  val newTakens = VecInit((0 until BimBanks).map(b => u.cfiIndex.valid && u.cfiIndex.bits === b.U))
78  val newCtrs = VecInit((0 until BimBanks).map(b => satUpdate(oldCtrs(b), 2, newTakens(b))))
79  // val oldSaturated = newCtr === oldCtr
80
81  val needToUpdate = VecInit((0 until PredictWidth).map(i => updateValid && u.br_mask(i) && u.valids(i)))
82
83  when (reset.asBool) { wrbypass_ctr_valids.foreach(_.foreach(_ := false.B))}
84
85  for (b <- 0 until BimBanks) {
86    when (needToUpdate(b)) {
87      when (wrbypass_hit) {
88        wrbypass_ctrs(wrbypass_hit_idx)(b) := newCtrs(b)
89        wrbypass_ctr_valids(wrbypass_hit_idx)(b) := true.B
90      } .otherwise {
91        wrbypass_ctrs(wrbypass_enq_idx)(b) := newCtrs(b)
92        (0 until BimBanks).foreach(b => wrbypass_ctr_valids(wrbypass_enq_idx)(b) := false.B) // reset valid bits
93        wrbypass_ctr_valids(wrbypass_enq_idx)(b) := true.B
94        wrbypass_rows(wrbypass_enq_idx) := updateRow
95        wrbypass_enq_idx := (wrbypass_enq_idx + 1.U)(log2Up(bypassEntries)-1,0)
96      }
97    }
98  }
99
100  bim.io.w.apply(
101    valid = needToUpdate.asUInt.orR || doing_reset,
102    data = Mux(doing_reset, VecInit(Seq.fill(BimBanks)(2.U(2.W))), newCtrs),
103    setIdx = Mux(doing_reset, resetRow, updateRow),
104    waymask = Mux(doing_reset, Fill(BimBanks, "b1".U).asUInt, needToUpdate.asUInt)
105  )
106
107  if (BPUDebug && debug) {
108    XSDebug(doing_reset, "Reseting...\n")
109    XSDebug("[update] v=%d pc=%x valids=%b, tgt=%x\n", updateValid, u.ftqPC, u.valids.asUInt, u.target)
110
111    XSDebug("[update] brMask=%b, taken=%b isMisPred=%b\n", u.br_mask.asUInt, newTakens.asUInt, u.mispred.asUInt)
112    for (i <- 0 until BimBanks) {
113      XSDebug(true.B, p"bimCtr(${i.U})=${Binary(u.metas(i).bimCtr)} oldCtr=${Binary(oldCtrs(i))} newCtr=${Binary(newCtrs(i))}\n")
114    }
115    XSDebug("needToUpdate=%b updateRow=%x\n", needToUpdate.asUInt, updateRow)
116    XSDebug("[wrbypass] hit=%d hits=%b\n", wrbypass_hit, wrbypass_hits.asUInt)
117  }
118
119}