Bim.scala 5.0 KB
Newer Older
L
Lingrui98 已提交
1 2 3 4 5 6
package xiangshan.frontend

import chisel3._
import chisel3.util._
import xiangshan._
import utils._
L
Lingrui98 已提交
7
import chisel3.experimental.chiselName
L
Lingrui98 已提交
8 9 10 11 12 13 14 15

trait BimParams extends HasXSParameter {
  val BimBanks = PredictWidth
  val BimSize = 4096
  val nRows = BimSize / BimBanks
  val bypassEntries = 4
}

L
Lingrui98 已提交
16
@chiselName
17
class BIM extends BasePredictor with BimParams {
L
Lingrui98 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31
  class BIMResp extends Resp {
    val ctrs = Vec(PredictWidth, UInt(2.W))
  }
  class BIMMeta extends Meta {
    val ctrs = Vec(PredictWidth, UInt(2.W))
  }
  class BIMFromOthers extends FromOthers {}

  class BIMIO extends DefaultBasePredictorIO {
    val resp = Output(new BIMResp)
    val meta = Output(new BIMMeta)
  }

  override val io = IO(new BIMIO)
32
  override val debug = true
L
Lingrui98 已提交
33 34 35

  val bimAddr = new TableAddr(log2Up(BimSize), BimBanks)

L
Lingrui98 已提交
36
  val bim = Module(new SRAMTemplate(UInt(2.W), set = nRows, way=BimBanks, shouldReset = false, holdRead = true))
L
Lingrui98 已提交
37 38 39 40 41 42

  val doing_reset = RegInit(true.B)
  val resetRow = RegInit(0.U(log2Ceil(nRows).W))
  resetRow := resetRow + doing_reset
  when (resetRow === (nRows-1).U) { doing_reset := false.B }

43 44
  val if1_packetAlignedPC = packetAligned(io.pc.bits)
  val if2_pc = RegEnable(if1_packetAlignedPC, io.pc.valid)
L
Lingrui98 已提交
45

46 47
  val if1_mask = io.inMask
  val if1_row  = bimAddr.getBankIdx(if1_packetAlignedPC)
L
Lingrui98 已提交
48

L
Lingrui98 已提交
49 50
  bim.io.r.req.valid := io.pc.valid
  bim.io.r.req.bits.setIdx := if1_row
L
Lingrui98 已提交
51

L
Lingrui98 已提交
52
  val if2_bimRead = bim.io.r.resp.data
53 54
  val ctrlMask = Fill(if2_bimRead.getWidth, ctrl.bim_enable.asUInt).asTypeOf(if2_bimRead)
  io.resp.ctrs  := VecInit(if2_bimRead zip ctrlMask map {case (a, b) => a & b})
L
Lingrui98 已提交
55
  io.meta.ctrs  := if2_bimRead
L
Lingrui98 已提交
56

57 58
  val updateValid = RegNext(io.update.valid)
  val u = RegNext(io.update.bits)
L
Lingrui98 已提交
59

L
Lingrui98 已提交
60
  val updateRow = bimAddr.getBankIdx(u.ftqPC)
L
Lingrui98 已提交
61 62


J
Jay 已提交
63 64 65
  val wrbypass_ctrs       = RegInit(0.U.asTypeOf(Vec(bypassEntries, Vec(BimBanks, UInt(2.W)))))
  val wrbypass_ctr_valids = RegInit(0.U.asTypeOf(Vec(bypassEntries, Vec(BimBanks, Bool()))))
  val wrbypass_rows     = RegInit(0.U.asTypeOf(Vec(bypassEntries, UInt(log2Up(nRows).W))))
L
Lingrui98 已提交
66 67 68 69 70 71 72
  val wrbypass_enq_idx  = RegInit(0.U(log2Up(bypassEntries).W))

  val wrbypass_hits = VecInit((0 until bypassEntries).map( i => 
    !doing_reset && wrbypass_rows(i) === updateRow))
  val wrbypass_hit = wrbypass_hits.reduce(_||_)
  val wrbypass_hit_idx = PriorityEncoder(wrbypass_hits)

L
Lingrui98 已提交
73 74 75 76 77 78
  val oldCtrs = VecInit((0 until BimBanks).map(b => 
                  Mux(wrbypass_hit && wrbypass_ctr_valids(wrbypass_hit_idx)(b),
                    wrbypass_ctrs(wrbypass_hit_idx)(b), u.metas(b).bimCtr)))
  
  val newTakens = VecInit((0 until BimBanks).map(b => u.cfiIndex.valid && u.cfiIndex.bits === b.U))
  val newCtrs = VecInit((0 until BimBanks).map(b => satUpdate(oldCtrs(b), 2, newTakens(b))))
L
Lingrui98 已提交
79 80
  // val oldSaturated = newCtr === oldCtr
  
81
  val needToUpdate = VecInit((0 until PredictWidth).map(i => updateValid && u.br_mask(i) && u.valids(i)))
L
Lingrui98 已提交
82 83 84

  when (reset.asBool) { wrbypass_ctr_valids.foreach(_.foreach(_ := false.B))}
  
L
Lingrui98 已提交
85
  for (b <- 0 until BimBanks) {
86
    when (needToUpdate.reduce(_||_)) {
L
Lingrui98 已提交
87
      when (wrbypass_hit) {
88 89 90 91 92 93 94 95 96 97
        when (needToUpdate(b)) {
          wrbypass_ctrs(wrbypass_hit_idx)(b) := newCtrs(b)
          wrbypass_ctr_valids(wrbypass_hit_idx)(b) := true.B
        }
      }.otherwise {
        wrbypass_ctr_valids(wrbypass_enq_idx)(b) := false.B
        when (needToUpdate(b)) {
          wrbypass_ctr_valids(wrbypass_enq_idx)(b) := true.B
          wrbypass_ctrs(wrbypass_enq_idx)(b) := newCtrs(b)
        }
L
Lingrui98 已提交
98
      }
L
Lingrui98 已提交
99 100
    }
  }
101 102
  
  when (needToUpdate.reduce(_||_) && !wrbypass_hit) {
S
Steve Gou 已提交
103
    wrbypass_rows(wrbypass_enq_idx) := updateRow
104 105
    wrbypass_enq_idx := (wrbypass_enq_idx + 1.U)(log2Up(bypassEntries)-1,0)
  }
L
Lingrui98 已提交
106

L
Lingrui98 已提交
107 108 109 110 111 112
  bim.io.w.apply(
    valid = needToUpdate.asUInt.orR || doing_reset,
    data = Mux(doing_reset, VecInit(Seq.fill(BimBanks)(2.U(2.W))), newCtrs),
    setIdx = Mux(doing_reset, resetRow, updateRow),
    waymask = Mux(doing_reset, Fill(BimBanks, "b1".U).asUInt, needToUpdate.asUInt)
  )
L
Lingrui98 已提交
113

S
Steve Gou 已提交
114 115 116
  XSPerf("bim_wrbypass_hit", needToUpdate.reduce(_||_) && wrbypass_hit)
  XSPerf("bim_wrbypass_enq", needToUpdate.reduce(_||_) && !wrbypass_hit)

L
Lingrui98 已提交
117
  if (BPUDebug && debug) {
118
    val u = io.update.bits
L
Lingrui98 已提交
119
    XSDebug(doing_reset, "Reseting...\n")
120
    XSDebug("[update] v=%d pc=%x valids=%b, tgt=%x\n", updateValid, u.ftqPC, u.valids.asUInt, u.target)
L
Lingrui98 已提交
121 122 123
    
    XSDebug("[update] brMask=%b, taken=%b isMisPred=%b\n", u.br_mask.asUInt, newTakens.asUInt, u.mispred.asUInt)
    for (i <- 0 until BimBanks) {
124 125 126 127 128 129 130
      XSDebug(RegNext(io.pc.valid && io.inMask(i)), p"BimResp[$i]: ctr = ${io.resp.ctrs(i)}\n")
      XSDebug(needToUpdate(i),
        p"update bim bank $i: pc:${Hexadecimal(u.ftqPC)}, taken:${u.takens(i)}, " +
        p"oldCtr:${oldCtrs(i)}, newCtr:${newCtrs(i)}\n")
      XSDebug(wrbypass_hit && wrbypass_ctr_valids(wrbypass_hit_idx)(i) && needToUpdate(i),
        p"bank $i wrbypass hit wridx $wrbypass_hit_idx: row:$updateRow, " +
        p"ctr:${oldCtrs(i)}, newCtr:${newCtrs(i)}\n")
L
Lingrui98 已提交
131 132
      XSDebug(true.B, p"bimCtr(${i.U})=${Binary(u.metas(i).bimCtr)} oldCtr=${Binary(oldCtrs(i))} newCtr=${Binary(newCtrs(i))}\n")
    }
L
Lingrui98 已提交
133 134
  }
  
L
Lingrui98 已提交
135
}