xref: /XiangShan/src/main/scala/xiangshan/frontend/SC.scala (revision dd6c0695f1cf59980dc2882dce3874535d6d6e49)
109c6f1ddSLingrui98/***************************************************************************************
209c6f1ddSLingrui98* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
309c6f1ddSLingrui98* Copyright (c) 2020-2021 Peng Cheng Laboratory
409c6f1ddSLingrui98*
509c6f1ddSLingrui98* XiangShan is licensed under Mulan PSL v2.
609c6f1ddSLingrui98* You can use this software according to the terms and conditions of the Mulan PSL v2.
709c6f1ddSLingrui98* You may obtain a copy of Mulan PSL v2 at:
809c6f1ddSLingrui98*          http://license.coscl.org.cn/MulanPSL2
909c6f1ddSLingrui98*
1009c6f1ddSLingrui98* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
1109c6f1ddSLingrui98* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
1209c6f1ddSLingrui98* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
1309c6f1ddSLingrui98*
1409c6f1ddSLingrui98* See the Mulan PSL v2 for more details.
1509c6f1ddSLingrui98***************************************************************************************/
1609c6f1ddSLingrui98
1709c6f1ddSLingrui98package xiangshan.frontend
1809c6f1ddSLingrui98
1909c6f1ddSLingrui98import chipsalliance.rocketchip.config.Parameters
2009c6f1ddSLingrui98import chisel3._
2109c6f1ddSLingrui98import chisel3.util._
2209c6f1ddSLingrui98import xiangshan._
2309c6f1ddSLingrui98import utils._
2409c6f1ddSLingrui98import chisel3.experimental.chiselName
2509c6f1ddSLingrui98
2609c6f1ddSLingrui98import scala.math.min
2709c6f1ddSLingrui98
2809c6f1ddSLingrui98trait HasSCParameter extends TageParams {
2909c6f1ddSLingrui98}
3009c6f1ddSLingrui98
3109c6f1ddSLingrui98class SCReq(implicit p: Parameters) extends TageReq
3209c6f1ddSLingrui98
3309c6f1ddSLingrui98abstract class SCBundle(implicit p: Parameters) extends TageBundle with HasSCParameter {}
3409c6f1ddSLingrui98abstract class SCModule(implicit p: Parameters) extends TageModule with HasSCParameter {}
3509c6f1ddSLingrui98
3609c6f1ddSLingrui98
3709c6f1ddSLingrui98class SCMeta(val useSC: Boolean, val ntables: Int)(implicit p: Parameters) extends XSBundle with HasSCParameter {
3809c6f1ddSLingrui98  val tageTaken = if (useSC) Bool() else UInt(0.W)
3909c6f1ddSLingrui98  val scUsed = if (useSC) Bool() else UInt(0.W)
4009c6f1ddSLingrui98  val scPred = if (useSC) Bool() else UInt(0.W)
4109c6f1ddSLingrui98  // Suppose ctrbits of all tables are identical
4209c6f1ddSLingrui98  val ctrs = if (useSC) Vec(ntables, SInt(SCCtrBits.W)) else Vec(ntables, SInt(0.W))
4309c6f1ddSLingrui98}
4409c6f1ddSLingrui98
4509c6f1ddSLingrui98
4609c6f1ddSLingrui98class SCResp(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle {
4709c6f1ddSLingrui98  val ctr = Vec(2, SInt(ctrBits.W))
4809c6f1ddSLingrui98}
4909c6f1ddSLingrui98
5009c6f1ddSLingrui98class SCUpdate(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle {
5109c6f1ddSLingrui98  val pc = UInt(VAddrBits.W)
5209c6f1ddSLingrui98  val hist = UInt(HistoryLength.W)
53*dd6c0695SLingrui98  val folded_hist = new AllFoldedHistories(foldedGHistInfos)
5409c6f1ddSLingrui98  val mask = Bool()
5509c6f1ddSLingrui98  val oldCtr = SInt(ctrBits.W)
5609c6f1ddSLingrui98  val tagePred = Bool()
5709c6f1ddSLingrui98  val taken = Bool()
5809c6f1ddSLingrui98}
5909c6f1ddSLingrui98
6009c6f1ddSLingrui98class SCTableIO(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle {
6109c6f1ddSLingrui98  val req = Input(Valid(new SCReq))
6209c6f1ddSLingrui98  val resp = Output(new SCResp(ctrBits))
6309c6f1ddSLingrui98  val update = Input(new SCUpdate(ctrBits))
6409c6f1ddSLingrui98}
6509c6f1ddSLingrui98
6609c6f1ddSLingrui98@chiselName
6709c6f1ddSLingrui98class SCTable(val nRows: Int, val ctrBits: Int, val histLen: Int)(implicit p: Parameters)
6809c6f1ddSLingrui98  extends SCModule with HasFoldedHistory {
6909c6f1ddSLingrui98  val io = IO(new SCTableIO(ctrBits))
7009c6f1ddSLingrui98
7109c6f1ddSLingrui98  // val table = Module(new SRAMTemplate(SInt(ctrBits.W), set=nRows, way=2*TageBanks, shouldReset=true, holdRead=true, singlePort=false))
7209c6f1ddSLingrui98  val table = Module(new SRAMTemplate(SInt(ctrBits.W), set=nRows, way=2, shouldReset=true, holdRead=true, singlePort=false))
7309c6f1ddSLingrui98
7409c6f1ddSLingrui98  val phistLen = PathHistoryLength
75*dd6c0695SLingrui98  // def getIdx(hist: UInt, pc: UInt) = {
76*dd6c0695SLingrui98  //   (compute_folded_ghist(hist, log2Ceil(nRows)) ^ (pc >> instOffsetBits))(log2Ceil(nRows)-1,0)
77*dd6c0695SLingrui98  // }
78*dd6c0695SLingrui98
79*dd6c0695SLingrui98
80*dd6c0695SLingrui98  val idxFhInfo = (histLen, min(log2Ceil(nRows), histLen))
81*dd6c0695SLingrui98
82*dd6c0695SLingrui98  def getFoldedHistoryInfo = Set(idxFhInfo).filter(_._1 > 0)
83*dd6c0695SLingrui98
84*dd6c0695SLingrui98  def getIdx(pc: UInt, allFh: AllFoldedHistories) = {
85*dd6c0695SLingrui98    if (histLen > 0) {
86*dd6c0695SLingrui98      val idx_fh = allFh.getHistWithInfo(idxFhInfo).folded_hist
87*dd6c0695SLingrui98      // require(idx_fh.getWidth == log2Ceil(nRows))
88*dd6c0695SLingrui98      ((pc >> instOffsetBits) ^ idx_fh)(log2Ceil(nRows)-1,0)
89*dd6c0695SLingrui98    }
90*dd6c0695SLingrui98    else {
91*dd6c0695SLingrui98      pc(log2Ceil(nRows)-1,0)
92*dd6c0695SLingrui98    }
9309c6f1ddSLingrui98  }
9409c6f1ddSLingrui98
9509c6f1ddSLingrui98  def ctrUpdate(ctr: SInt, cond: Bool): SInt = signedSatUpdate(ctr, ctrBits, cond)
9609c6f1ddSLingrui98
97*dd6c0695SLingrui98  val s0_idx = getIdx(io.req.bits.pc, io.req.bits.folded_hist)
9809c6f1ddSLingrui98  val s1_idx = RegEnable(s0_idx, enable=io.req.valid)
9909c6f1ddSLingrui98
10009c6f1ddSLingrui98  table.io.r.req.valid := io.req.valid
10109c6f1ddSLingrui98  table.io.r.req.bits.setIdx := s0_idx
10209c6f1ddSLingrui98
10309c6f1ddSLingrui98  io.resp.ctr := table.io.r.resp.data
10409c6f1ddSLingrui98
10509c6f1ddSLingrui98  val update_wdata = Wire(SInt(ctrBits.W))
10609c6f1ddSLingrui98  val updateWayMask =
10709c6f1ddSLingrui98      VecInit((0 to 1).map(io.update.mask && _.U === io.update.tagePred.asUInt)).asUInt
10809c6f1ddSLingrui98
109*dd6c0695SLingrui98  val update_idx = getIdx(io.update.pc, io.update.folded_hist)
11009c6f1ddSLingrui98
11109c6f1ddSLingrui98  table.io.w.apply(
11209c6f1ddSLingrui98    valid = io.update.mask,
11309c6f1ddSLingrui98    data = VecInit(Seq.fill(2)(update_wdata)),
11409c6f1ddSLingrui98    setIdx = update_idx,
11509c6f1ddSLingrui98    waymask = updateWayMask
11609c6f1ddSLingrui98  )
11709c6f1ddSLingrui98
11809c6f1ddSLingrui98  val wrBypassEntries = 4
11909c6f1ddSLingrui98
12009c6f1ddSLingrui98  class SCWrBypass extends XSModule {
12109c6f1ddSLingrui98    val io = IO(new Bundle {
12209c6f1ddSLingrui98      val wen = Input(Bool())
12309c6f1ddSLingrui98      val update_idx  = Input(UInt(log2Ceil(nRows).W))
12409c6f1ddSLingrui98      val update_ctrs  = Flipped(ValidIO(SInt(ctrBits.W)))
12509c6f1ddSLingrui98      val update_ctrPos = Input(UInt(log2Ceil(2).W))
12609c6f1ddSLingrui98      val update_altPos = Input(UInt(log2Ceil(2).W))
12709c6f1ddSLingrui98
12809c6f1ddSLingrui98      val hit   = Output(Bool())
12909c6f1ddSLingrui98      val ctrs  = Vec(2, ValidIO(SInt(ctrBits.W)))
13009c6f1ddSLingrui98    })
13109c6f1ddSLingrui98
13209c6f1ddSLingrui98    val idxes       = RegInit(0.U.asTypeOf(Vec(wrBypassEntries, UInt(log2Ceil(nRows).W))))
13309c6f1ddSLingrui98    val ctrs        = RegInit(0.U.asTypeOf(Vec(wrBypassEntries, Vec(2, SInt(ctrBits.W)))))
13409c6f1ddSLingrui98    val ctr_valids  = RegInit(0.U.asTypeOf(Vec(wrBypassEntries, Vec(2, Bool()))))
13509c6f1ddSLingrui98    val enq_idx     = RegInit(0.U(log2Ceil(wrBypassEntries).W))
13609c6f1ddSLingrui98
13709c6f1ddSLingrui98    val hits = VecInit((0 until wrBypassEntries).map { i => idxes(i) === io.update_idx })
13809c6f1ddSLingrui98
13909c6f1ddSLingrui98    val hit = hits.reduce(_||_)
14009c6f1ddSLingrui98    val hit_idx = ParallelPriorityEncoder(hits)
14109c6f1ddSLingrui98
14209c6f1ddSLingrui98    io.hit := hit
14309c6f1ddSLingrui98
14409c6f1ddSLingrui98    for (i <- 0 until 2) {
14509c6f1ddSLingrui98      io.ctrs(i).valid := ctr_valids(hit_idx)(i)
14609c6f1ddSLingrui98      io.ctrs(i).bits := ctrs(hit_idx)(i)
14709c6f1ddSLingrui98    }
14809c6f1ddSLingrui98
14909c6f1ddSLingrui98    when (io.wen) {
15009c6f1ddSLingrui98      when (hit) {
15109c6f1ddSLingrui98        ctrs(hit_idx)(io.update_ctrPos) := io.update_ctrs.bits
15209c6f1ddSLingrui98        ctr_valids(hit_idx)(io.update_ctrPos) := io.update_ctrs.valid
15309c6f1ddSLingrui98      }.otherwise {
15409c6f1ddSLingrui98        ctr_valids(enq_idx)(io.update_altPos) := false.B
15509c6f1ddSLingrui98        ctr_valids(enq_idx)(io.update_ctrPos) := io.update_ctrs.valid
15609c6f1ddSLingrui98        ctrs(enq_idx)(io.update_ctrPos) := io.update_ctrs.bits
15709c6f1ddSLingrui98      }
15809c6f1ddSLingrui98    }
15909c6f1ddSLingrui98
16009c6f1ddSLingrui98    when(io.wen && !hit) {
16109c6f1ddSLingrui98      idxes(enq_idx) := io.update_idx
16209c6f1ddSLingrui98      enq_idx := (enq_idx + 1.U)(log2Ceil(wrBypassEntries)-1, 0)
16309c6f1ddSLingrui98    }
16409c6f1ddSLingrui98  }
16509c6f1ddSLingrui98
16609c6f1ddSLingrui98  val wrbypass = Module(new SCWrBypass)
16709c6f1ddSLingrui98
16809c6f1ddSLingrui98  val ctrPos = io.update.tagePred
16909c6f1ddSLingrui98  val altPos = !io.update.tagePred
17009c6f1ddSLingrui98  val bypass_ctr = wrbypass.io.ctrs(ctrPos)
17109c6f1ddSLingrui98  val hit_and_valid = wrbypass.io.hit && bypass_ctr.valid
17209c6f1ddSLingrui98  val oldCtr = Mux(hit_and_valid, bypass_ctr.bits, io.update.oldCtr)
17309c6f1ddSLingrui98  update_wdata := ctrUpdate(oldCtr, io.update.taken)
17409c6f1ddSLingrui98
17509c6f1ddSLingrui98  wrbypass.io.wen := io.update.mask
17609c6f1ddSLingrui98  wrbypass.io.update_ctrs.valid := io.update.mask
17709c6f1ddSLingrui98  wrbypass.io.update_ctrs.bits := update_wdata
17809c6f1ddSLingrui98  wrbypass.io.update_idx := update_idx
17909c6f1ddSLingrui98  wrbypass.io.update_ctrPos := ctrPos
18009c6f1ddSLingrui98  wrbypass.io.update_altPos := altPos
18109c6f1ddSLingrui98
18209c6f1ddSLingrui98  val u = io.update
18309c6f1ddSLingrui98  XSDebug(io.req.valid,
18409c6f1ddSLingrui98    p"scTableReq: pc=0x${Hexadecimal(io.req.bits.pc)}, " +
18509c6f1ddSLingrui98    p"s0_idx=${s0_idx}, hist=${Hexadecimal(io.req.bits.hist)}\n")
18609c6f1ddSLingrui98  XSDebug(RegNext(io.req.valid),
18709c6f1ddSLingrui98    p"scTableResp: s1_idx=${s1_idx}," +
18809c6f1ddSLingrui98    p"ctr:${io.resp.ctr}\n")
18909c6f1ddSLingrui98  XSDebug(io.update.mask,
19009c6f1ddSLingrui98    p"update Table: pc:${Hexadecimal(u.pc)}, hist:${Hexadecimal(u.hist)}, " +
19109c6f1ddSLingrui98    p"tageTaken:${u.tagePred}, taken:${u.taken}, oldCtr:${u.oldCtr}\n")
192*dd6c0695SLingrui98  val updateCtrPos = io.update.tagePred
193*dd6c0695SLingrui98  val hitCtr = wrbypass.io.ctrs(updateCtrPos).bits
194*dd6c0695SLingrui98  XSDebug(wrbypass.io.hit && wrbypass.io.ctrs(updateCtrPos).valid && io.update.mask,
19509c6f1ddSLingrui98    p"wrbypass hit idx:$update_idx, ctr:$hitCtr, " +
19609c6f1ddSLingrui98    p"taken:${io.update.taken} newCtr:${update_wdata}\n")
19709c6f1ddSLingrui98
19809c6f1ddSLingrui98}
19909c6f1ddSLingrui98
20009c6f1ddSLingrui98class SCThreshold(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle {
20109c6f1ddSLingrui98  val ctr = UInt(ctrBits.W)
20209c6f1ddSLingrui98  def satPos(ctr: UInt = this.ctr) = ctr === ((1.U << ctrBits) - 1.U)
20309c6f1ddSLingrui98  def satNeg(ctr: UInt = this.ctr) = ctr === 0.U
20409c6f1ddSLingrui98  def neutralVal = (1.U << (ctrBits - 1))
20509c6f1ddSLingrui98  val thres = UInt(8.W)
20609c6f1ddSLingrui98  def initVal = 6.U
20709c6f1ddSLingrui98  def minThres = 6.U
20809c6f1ddSLingrui98  def maxThres = 31.U
20909c6f1ddSLingrui98  def update(cause: Bool): SCThreshold = {
21009c6f1ddSLingrui98    val res = Wire(new SCThreshold(this.ctrBits))
21109c6f1ddSLingrui98    val newCtr = satUpdate(this.ctr, this.ctrBits, cause)
21209c6f1ddSLingrui98    val newThres = Mux(res.satPos(newCtr) && this.thres <= maxThres, this.thres + 2.U,
21309c6f1ddSLingrui98                      Mux(res.satNeg(newCtr) && this.thres >= minThres, this.thres - 2.U,
21409c6f1ddSLingrui98                      this.thres))
21509c6f1ddSLingrui98    res.thres := newThres
21609c6f1ddSLingrui98    res.ctr := Mux(res.satPos(newCtr) || res.satNeg(newCtr), res.neutralVal, newCtr)
21709c6f1ddSLingrui98    // XSDebug(true.B, p"scThres Update: cause${cause} newCtr ${newCtr} newThres ${newThres}\n")
21809c6f1ddSLingrui98    res
21909c6f1ddSLingrui98  }
22009c6f1ddSLingrui98}
22109c6f1ddSLingrui98
22209c6f1ddSLingrui98object SCThreshold {
22309c6f1ddSLingrui98  def apply(bits: Int)(implicit p: Parameters) = {
22409c6f1ddSLingrui98    val t = Wire(new SCThreshold(ctrBits=bits))
22509c6f1ddSLingrui98    t.ctr := t.neutralVal
22609c6f1ddSLingrui98    t.thres := t.initVal
22709c6f1ddSLingrui98    t
22809c6f1ddSLingrui98  }
22909c6f1ddSLingrui98}
23009c6f1ddSLingrui98
23109c6f1ddSLingrui98
23209c6f1ddSLingrui98trait HasSC extends HasSCParameter { this: Tage =>
233efe3f3bbSSteve Gou  val update_on_mispred, update_on_unconf = WireInit(0.U.asTypeOf(Vec(TageBanks, Bool())))
234*dd6c0695SLingrui98  var sc_fh_info = Set[FoldedHistoryInfo]()
235bf358e08SLingrui98  if (EnableSC) {
23609c6f1ddSLingrui98    val bank_scTables = BankSCTableInfos.zipWithIndex.map {
23709c6f1ddSLingrui98      case (info, b) =>
23809c6f1ddSLingrui98        val tables = info.map {
23909c6f1ddSLingrui98          case (nRows, ctrBits, histLen) => {
24009c6f1ddSLingrui98            val t = Module(new SCTable(nRows/TageBanks, ctrBits, histLen))
24109c6f1ddSLingrui98            val req = t.io.req
24209c6f1ddSLingrui98            req.valid := io.s0_fire
24309c6f1ddSLingrui98            req.bits.pc := s0_pc
244*dd6c0695SLingrui98            req.bits.hist := io.in.bits.ghist
245*dd6c0695SLingrui98            req.bits.folded_hist := io.in.bits.folded_hist
24609c6f1ddSLingrui98            req.bits.phist := DontCare
24709c6f1ddSLingrui98            if (!EnableSC) {t.io.update := DontCare}
24809c6f1ddSLingrui98            t
24909c6f1ddSLingrui98          }
25009c6f1ddSLingrui98        }
25109c6f1ddSLingrui98        tables
25209c6f1ddSLingrui98    }
253*dd6c0695SLingrui98    sc_fh_info = bank_scTables.flatMap(_.map(_.getFoldedHistoryInfo).reduce(_++_)).toSet
25409c6f1ddSLingrui98
25509c6f1ddSLingrui98    val scThresholds = List.fill(TageBanks)(RegInit(SCThreshold(5)))
25609c6f1ddSLingrui98    val useThresholds = VecInit(scThresholds map (_.thres))
25709c6f1ddSLingrui98    val updateThresholds = VecInit(useThresholds map (t => (t << 3) +& 21.U))
25809c6f1ddSLingrui98
25909c6f1ddSLingrui98    val s1_scResps = MixedVecInit(bank_scTables.map(b => VecInit(b.map(t => t.io.resp))))
26009c6f1ddSLingrui98
26109c6f1ddSLingrui98    val scUpdateMask = WireInit(0.U.asTypeOf(MixedVec(BankSCNTables.map(Vec(_, Bool())))))
26209c6f1ddSLingrui98    val scUpdateTagePreds = Wire(Vec(TageBanks, Bool()))
26309c6f1ddSLingrui98    val scUpdateTakens = Wire(Vec(TageBanks, Bool()))
26409c6f1ddSLingrui98    val scUpdateOldCtrs = Wire(MixedVec(BankSCNTables.map(Vec(_, SInt(SCCtrBits.W)))))
26509c6f1ddSLingrui98    scUpdateTagePreds := DontCare
26609c6f1ddSLingrui98    scUpdateTakens := DontCare
26709c6f1ddSLingrui98    scUpdateOldCtrs := DontCare
26809c6f1ddSLingrui98
26909c6f1ddSLingrui98    val updateSCMetas = VecInit(updateMetas.map(_.scMeta))
27009c6f1ddSLingrui98
27109c6f1ddSLingrui98    val s2_sc_used, s2_conf, s2_unconf, s2_agree, s2_disagree =
27209c6f1ddSLingrui98      0.U.asTypeOf(Vec(TageBanks, Bool()))
27309c6f1ddSLingrui98    val update_sc_used, update_conf, update_unconf, update_agree, update_disagree =
27409c6f1ddSLingrui98      0.U.asTypeOf(Vec(TageBanks, Bool()))
275efe3f3bbSSteve Gou    val sc_misp_tage_corr, sc_corr_tage_misp =
27609c6f1ddSLingrui98      0.U.asTypeOf(Vec(TageBanks, Bool()))
27709c6f1ddSLingrui98
27809c6f1ddSLingrui98    // for sc ctrs
27909c6f1ddSLingrui98    def getCentered(ctr: SInt): SInt = (ctr << 1).asSInt + 1.S
28009c6f1ddSLingrui98    // for tage ctrs
28109c6f1ddSLingrui98    def getPvdrCentered(ctr: UInt): SInt = ((((ctr.zext -& 4.S) << 1).asSInt + 1.S) << 3).asSInt
28209c6f1ddSLingrui98
28309c6f1ddSLingrui98    for (w <- 0 until TageBanks) {
28409c6f1ddSLingrui98      val scMeta = resp_meta(w).scMeta
28509c6f1ddSLingrui98      scMeta := DontCare
28609c6f1ddSLingrui98      // do summation in s2
28709c6f1ddSLingrui98      val s1_scTableSums = VecInit(
28809c6f1ddSLingrui98        (0 to 1) map { i =>
28909c6f1ddSLingrui98          ParallelSingedExpandingAdd(s1_scResps(w) map (r => getCentered(r.ctr(i)))) // TODO: rewrite with wallace tree
29009c6f1ddSLingrui98        }
29109c6f1ddSLingrui98      )
29209c6f1ddSLingrui98
29309c6f1ddSLingrui98      val providerCtr = s1_providerCtrs(w)
29409c6f1ddSLingrui98      val s1_pvdrCtrCentered = getPvdrCentered(providerCtr)
29509c6f1ddSLingrui98      val s1_totalSums = VecInit(s1_scTableSums.map(_  +& s1_pvdrCtrCentered))
29609c6f1ddSLingrui98      val s1_sumAbs = VecInit(s1_totalSums.map(_.abs.asUInt))
29709c6f1ddSLingrui98      val s1_sumBelowThresholds = VecInit(s1_sumAbs map (_ <= useThresholds(w)))
29809c6f1ddSLingrui98      val s1_scPreds = VecInit(s1_totalSums.map (_ >= 0.S))
29909c6f1ddSLingrui98
30009c6f1ddSLingrui98      val s2_sumBelowThresholds = RegEnable(s1_sumBelowThresholds, io.s1_fire)
30109c6f1ddSLingrui98      val s2_scPreds = RegEnable(s1_scPreds, io.s1_fire)
30209c6f1ddSLingrui98      val s2_sumAbs = RegEnable(s1_sumAbs, io.s1_fire)
30309c6f1ddSLingrui98
30409c6f1ddSLingrui98      val s2_scCtrs = RegEnable(VecInit(s1_scResps(w).map(r => r.ctr(s1_tageTakens(w).asUInt))), io.s1_fire)
30509c6f1ddSLingrui98      val s2_chooseBit = s2_tageTakens(w)
30609c6f1ddSLingrui98      scMeta.tageTaken := s2_tageTakens(w)
30709c6f1ddSLingrui98      scMeta.scUsed := s2_provideds(w)
30809c6f1ddSLingrui98      scMeta.scPred := s2_scPreds(s2_chooseBit)
30909c6f1ddSLingrui98      scMeta.ctrs   := s2_scCtrs
31009c6f1ddSLingrui98
31109c6f1ddSLingrui98      when (s2_provideds(w)) {
31209c6f1ddSLingrui98        s2_sc_used(w) := true.B
31309c6f1ddSLingrui98        s2_unconf(w) := s2_sumBelowThresholds(s2_chooseBit)
31409c6f1ddSLingrui98        s2_conf(w) := !s2_sumBelowThresholds(s2_chooseBit)
31509c6f1ddSLingrui98        // Use prediction from Statistical Corrector
31609c6f1ddSLingrui98        XSDebug(p"---------tage_bank_${w} provided so that sc used---------\n")
31709c6f1ddSLingrui98        XSDebug(p"scCtrs:$s2_scCtrs, prdrCtr:${s2_providerCtrs(w)}, sumAbs:$s2_sumAbs, tageTaken:${s2_chooseBit}\n")
31809c6f1ddSLingrui98        when (!s2_sumBelowThresholds(s2_chooseBit)) {
31909c6f1ddSLingrui98          val pred = s2_scPreds(s2_chooseBit)
32009c6f1ddSLingrui98          val debug_pc = Cat(debug_pc_s2, w.U, 0.U(instOffsetBits.W))
32109c6f1ddSLingrui98          s2_agree(w) := s2_tageTakens(w) === pred
32209c6f1ddSLingrui98          s2_disagree(w) := s2_tageTakens(w) =/= pred
32309c6f1ddSLingrui98          // fit to always-taken condition
324eeb5ff92SLingrui98          io.out.resp.s2.preds.br_taken_mask(w) := pred
32509c6f1ddSLingrui98          XSDebug(p"pc(${Hexadecimal(debug_pc)}) SC(${w.U}) overriden pred to ${pred}\n")
32609c6f1ddSLingrui98        }
32709c6f1ddSLingrui98      }
32809c6f1ddSLingrui98
32909c6f1ddSLingrui98      val updateSCMeta = updateSCMetas(w)
33009c6f1ddSLingrui98      val updateTageMeta = updateMetas(w)
33109c6f1ddSLingrui98      when (updateValids(w) && updateSCMeta.scUsed.asBool) {
33209c6f1ddSLingrui98        val scPred = updateSCMeta.scPred
33309c6f1ddSLingrui98        val tagePred = updateSCMeta.tageTaken
334eeb5ff92SLingrui98        val taken = update.preds.br_taken_mask(w)
33509c6f1ddSLingrui98        val scOldCtrs = updateSCMeta.ctrs
33609c6f1ddSLingrui98        val pvdrCtr = updateTageMeta.providerCtr
33709c6f1ddSLingrui98        val sum = ParallelSingedExpandingAdd(scOldCtrs.map(getCentered)) +& getPvdrCentered(pvdrCtr)
33809c6f1ddSLingrui98        val sumAbs = sum.abs.asUInt
33909c6f1ddSLingrui98        scUpdateTagePreds(w) := tagePred
34009c6f1ddSLingrui98        scUpdateTakens(w) := taken
34109c6f1ddSLingrui98        (scUpdateOldCtrs(w) zip scOldCtrs).foreach{case (t, c) => t := c}
34209c6f1ddSLingrui98
34309c6f1ddSLingrui98        update_sc_used(w) := true.B
34409c6f1ddSLingrui98        update_unconf(w) := sumAbs < useThresholds(w)
34509c6f1ddSLingrui98        update_conf(w) := sumAbs >= useThresholds(w)
34609c6f1ddSLingrui98        update_agree(w) := scPred === tagePred
34709c6f1ddSLingrui98        update_disagree(w) := scPred =/= tagePred
34809c6f1ddSLingrui98        sc_corr_tage_misp(w) := scPred === taken && tagePred =/= taken && update_conf(w)
34909c6f1ddSLingrui98        sc_misp_tage_corr(w) := scPred =/= taken && tagePred === taken && update_conf(w)
35009c6f1ddSLingrui98
35109c6f1ddSLingrui98        val thres = useThresholds(w)
35209c6f1ddSLingrui98        when (scPred =/= tagePred && sumAbs >= thres - 4.U && sumAbs <= thres - 2.U) {
35309c6f1ddSLingrui98          val newThres = scThresholds(w).update(scPred =/= taken)
35409c6f1ddSLingrui98          scThresholds(w) := newThres
35509c6f1ddSLingrui98          XSDebug(p"scThres $w update: old ${useThresholds(w)} --> new ${newThres.thres}\n")
35609c6f1ddSLingrui98        }
35709c6f1ddSLingrui98
35809c6f1ddSLingrui98        val updateThres = updateThresholds(w)
35909c6f1ddSLingrui98        when (scPred =/= taken || sumAbs < updateThres) {
36009c6f1ddSLingrui98          scUpdateMask(w).foreach(_ := true.B)
36109c6f1ddSLingrui98          XSDebug(sum < 0.S,
36209c6f1ddSLingrui98            p"scUpdate: bank(${w}), scPred(${scPred}), tagePred(${tagePred}), " +
36309c6f1ddSLingrui98            p"scSum(-$sumAbs), mispred: sc(${scPred =/= taken}), tage(${updateMisPreds(w)})\n"
36409c6f1ddSLingrui98          )
36509c6f1ddSLingrui98          XSDebug(sum >= 0.S,
36609c6f1ddSLingrui98            p"scUpdate: bank(${w}), scPred(${scPred}), tagePred(${tagePred}), " +
36709c6f1ddSLingrui98            p"scSum(+$sumAbs), mispred: sc(${scPred =/= taken}), tage(${updateMisPreds(w)})\n"
36809c6f1ddSLingrui98          )
36909c6f1ddSLingrui98          XSDebug(p"bank(${w}), update: sc: ${updateSCMeta}\n")
37009c6f1ddSLingrui98          update_on_mispred(w) := scPred =/= taken
37109c6f1ddSLingrui98          update_on_unconf(w) := scPred === taken
37209c6f1ddSLingrui98        }
37309c6f1ddSLingrui98      }
37409c6f1ddSLingrui98    }
37509c6f1ddSLingrui98
37609c6f1ddSLingrui98
37709c6f1ddSLingrui98    for (b <- 0 until TageBanks) {
37809c6f1ddSLingrui98      for (i <- 0 until BankSCNTables(b)) {
37909c6f1ddSLingrui98        bank_scTables(b)(i).io.update.mask := RegNext(scUpdateMask(b)(i))
38009c6f1ddSLingrui98        bank_scTables(b)(i).io.update.tagePred := RegNext(scUpdateTagePreds(b))
38109c6f1ddSLingrui98        bank_scTables(b)(i).io.update.taken    := RegNext(scUpdateTakens(b))
38209c6f1ddSLingrui98        bank_scTables(b)(i).io.update.oldCtr   := RegNext(scUpdateOldCtrs(b)(i))
38309c6f1ddSLingrui98        bank_scTables(b)(i).io.update.pc := RegNext(update.pc)
384*dd6c0695SLingrui98        bank_scTables(b)(i).io.update.hist := RegNext(updateHist.predHist)
385*dd6c0695SLingrui98        bank_scTables(b)(i).io.update.folded_hist := RegNext(updateFHist)
38609c6f1ddSLingrui98      }
38709c6f1ddSLingrui98    }
38809c6f1ddSLingrui98
38909c6f1ddSLingrui98    tage_perf("sc_conf", PopCount(s2_conf), PopCount(update_conf))
39009c6f1ddSLingrui98    tage_perf("sc_unconf", PopCount(s2_unconf), PopCount(update_unconf))
39109c6f1ddSLingrui98    tage_perf("sc_agree", PopCount(s2_agree), PopCount(update_agree))
39209c6f1ddSLingrui98    tage_perf("sc_disagree", PopCount(s2_disagree), PopCount(update_disagree))
39309c6f1ddSLingrui98    tage_perf("sc_used", PopCount(s2_sc_used), PopCount(update_sc_used))
39409c6f1ddSLingrui98    XSPerfAccumulate("sc_update_on_mispred", PopCount(update_on_mispred))
39509c6f1ddSLingrui98    XSPerfAccumulate("sc_update_on_unconf", PopCount(update_on_unconf))
39609c6f1ddSLingrui98    XSPerfAccumulate("sc_mispred_but_tage_correct", PopCount(sc_misp_tage_corr))
39709c6f1ddSLingrui98    XSPerfAccumulate("sc_correct_and_tage_wrong", PopCount(sc_corr_tage_misp))
398cd365d4cSrvcoresjw
399efe3f3bbSSteve Gou  }
400efe3f3bbSSteve Gou
401*dd6c0695SLingrui98  override def getFoldedHistoryInfo = Some(tage_fh_info ++ sc_fh_info)
402*dd6c0695SLingrui98
403*dd6c0695SLingrui98
404cd365d4cSrvcoresjw  val perfinfo = IO(new Bundle(){
405cd365d4cSrvcoresjw    val perfEvents = Output(new PerfEventsBundle(3))
406cd365d4cSrvcoresjw  })
407cd365d4cSrvcoresjw  val perfEvents = Seq(
408cd365d4cSrvcoresjw    ("tage_tht_hit                  ", updateMetas(1).provider.valid + updateMetas(0).provider.valid),
409cd365d4cSrvcoresjw    ("sc_update_on_mispred          ", PopCount(update_on_mispred) ),
410cd365d4cSrvcoresjw    ("sc_update_on_unconf           ", PopCount(update_on_unconf)  ),
411cd365d4cSrvcoresjw  )
412cd365d4cSrvcoresjw  for (((perf_out,(perf_name,perf)),i) <- perfinfo.perfEvents.perf_events.zip(perfEvents).zipWithIndex) {
413cd365d4cSrvcoresjw    perf_out.incr_step := RegNext(perf)
41409c6f1ddSLingrui98  }
415bf358e08SLingrui98}
416