xref: /XiangShan/src/main/scala/xiangshan/frontend/Bim.scala (revision 049559e7211981c4c6940c9b7db1e976190d70df)
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  io.resp.ctrs  := if2_bimRead
54  io.meta.ctrs  := if2_bimRead
55
56  val u = io.update.bits
57
58  val updateRow = bimAddr.getBankIdx(u.ftqPC)
59
60
61  val wrbypass_ctrs       = Reg(Vec(bypassEntries, Vec(BimBanks, UInt(2.W))))
62  val wrbypass_ctr_valids = Reg(Vec(bypassEntries, Vec(BimBanks, Bool())))
63  val wrbypass_rows     = Reg(Vec(bypassEntries, UInt(log2Up(nRows).W)))
64  val wrbypass_enq_idx  = RegInit(0.U(log2Up(bypassEntries).W))
65
66  val wrbypass_hits = VecInit((0 until bypassEntries).map( i =>
67    !doing_reset && wrbypass_rows(i) === updateRow))
68  val wrbypass_hit = wrbypass_hits.reduce(_||_)
69  val wrbypass_hit_idx = PriorityEncoder(wrbypass_hits)
70
71  val oldCtrs = VecInit((0 until BimBanks).map(b =>
72                  Mux(wrbypass_hit && wrbypass_ctr_valids(wrbypass_hit_idx)(b),
73                    wrbypass_ctrs(wrbypass_hit_idx)(b), u.metas(b).bimCtr)))
74
75  val newTakens = VecInit((0 until BimBanks).map(b => u.cfiIndex.valid && u.cfiIndex.bits === b.U))
76  val newCtrs = VecInit((0 until BimBanks).map(b => satUpdate(oldCtrs(b), 2, newTakens(b))))
77  // val oldSaturated = newCtr === oldCtr
78
79  val needToUpdate = VecInit((0 until PredictWidth).map(i => io.update.valid && u.br_mask(i) && u.valids(i)))
80
81  when (reset.asBool) { wrbypass_ctr_valids.foreach(_.foreach(_ := false.B))}
82
83  for (b <- 0 until BimBanks) {
84    when (needToUpdate(b)) {
85      when (wrbypass_hit) {
86        wrbypass_ctrs(wrbypass_hit_idx)(b) := newCtrs(b)
87        wrbypass_ctr_valids(wrbypass_hit_idx)(b) := true.B
88      } .otherwise {
89        wrbypass_ctrs(wrbypass_enq_idx)(b) := newCtrs(b)
90        (0 until BimBanks).foreach(b => wrbypass_ctr_valids(wrbypass_enq_idx)(b) := false.B) // reset valid bits
91        wrbypass_ctr_valids(wrbypass_enq_idx)(b) := true.B
92        wrbypass_rows(wrbypass_enq_idx) := updateRow
93        wrbypass_enq_idx := (wrbypass_enq_idx + 1.U)(log2Up(bypassEntries)-1,0)
94      }
95    }
96  }
97
98  bim.io.w.apply(
99    valid = needToUpdate.asUInt.orR || doing_reset,
100    data = Mux(doing_reset, VecInit(Seq.fill(BimBanks)(2.U(2.W))), newCtrs),
101    setIdx = Mux(doing_reset, resetRow, updateRow),
102    waymask = Mux(doing_reset, Fill(BimBanks, "b1".U).asUInt, needToUpdate.asUInt)
103  )
104
105  if (BPUDebug && debug) {
106    XSDebug(doing_reset, "Reseting...\n")
107    XSDebug("[update] v=%d pc=%x valids=%b, tgt=%x\n", io.update.valid, u.ftqPC, u.valids.asUInt, u.target)
108
109    XSDebug("[update] brMask=%b, taken=%b isMisPred=%b\n", u.br_mask.asUInt, newTakens.asUInt, u.mispred.asUInt)
110    for (i <- 0 until BimBanks) {
111      XSDebug(true.B, p"bimCtr(${i.U})=${Binary(u.metas(i).bimCtr)} oldCtr=${Binary(oldCtrs(i))} newCtr=${Binary(newCtrs(i))}\n")
112    }
113    XSDebug("needToUpdate=%b updateRow=%x\n", needToUpdate.asUInt, updateRow)
114    XSDebug("[wrbypass] hit=%d hits=%b\n", wrbypass_hit, wrbypass_hits.asUInt)
115  }
116
117}