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._ 243c02ee8fSwakafaimport utility._ 2509c6f1ddSLingrui98import chisel3.experimental.chiselName 2609c6f1ddSLingrui98 2709c6f1ddSLingrui98import scala.math.min 28*adc0b8dfSGuokai Chenimport scala.{Tuple2 => &} 2909c6f1ddSLingrui98 3009c6f1ddSLingrui98trait HasSCParameter extends TageParams { 3109c6f1ddSLingrui98} 3209c6f1ddSLingrui98 3309c6f1ddSLingrui98class SCReq(implicit p: Parameters) extends TageReq 3409c6f1ddSLingrui98 3509c6f1ddSLingrui98abstract class SCBundle(implicit p: Parameters) extends TageBundle with HasSCParameter {} 3609c6f1ddSLingrui98abstract class SCModule(implicit p: Parameters) extends TageModule with HasSCParameter {} 3709c6f1ddSLingrui98 3809c6f1ddSLingrui98 3934ed6fbcSLingrui98class SCMeta(val ntables: Int)(implicit p: Parameters) extends XSBundle with HasSCParameter { 4034ed6fbcSLingrui98 val tageTakens = Vec(numBr, Bool()) 41744dc920SLingrui98 val scUsed = Vec(numBr, Bool()) 4234ed6fbcSLingrui98 val scPreds = Vec(numBr, Bool()) 4309c6f1ddSLingrui98 // Suppose ctrbits of all tables are identical 4434ed6fbcSLingrui98 val ctrs = Vec(numBr, Vec(ntables, SInt(SCCtrBits.W))) 4509c6f1ddSLingrui98} 4609c6f1ddSLingrui98 4709c6f1ddSLingrui98 4809c6f1ddSLingrui98class SCResp(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle { 4934ed6fbcSLingrui98 val ctrs = Vec(numBr, Vec(2, SInt(ctrBits.W))) 5009c6f1ddSLingrui98} 5109c6f1ddSLingrui98 5209c6f1ddSLingrui98class SCUpdate(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle { 5309c6f1ddSLingrui98 val pc = UInt(VAddrBits.W) 54dd6c0695SLingrui98 val folded_hist = new AllFoldedHistories(foldedGHistInfos) 5534ed6fbcSLingrui98 val mask = Vec(numBr, Bool()) 5634ed6fbcSLingrui98 val oldCtrs = Vec(numBr, SInt(ctrBits.W)) 5734ed6fbcSLingrui98 val tagePreds = Vec(numBr, Bool()) 5834ed6fbcSLingrui98 val takens = Vec(numBr, Bool()) 5909c6f1ddSLingrui98} 6009c6f1ddSLingrui98 6109c6f1ddSLingrui98class SCTableIO(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle { 6209c6f1ddSLingrui98 val req = Input(Valid(new SCReq)) 6309c6f1ddSLingrui98 val resp = Output(new SCResp(ctrBits)) 6409c6f1ddSLingrui98 val update = Input(new SCUpdate(ctrBits)) 6509c6f1ddSLingrui98} 6609c6f1ddSLingrui98 6709c6f1ddSLingrui98@chiselName 6809c6f1ddSLingrui98class SCTable(val nRows: Int, val ctrBits: Int, val histLen: Int)(implicit p: Parameters) 6909c6f1ddSLingrui98 extends SCModule with HasFoldedHistory { 7009c6f1ddSLingrui98 val io = IO(new SCTableIO(ctrBits)) 7109c6f1ddSLingrui98 7209c6f1ddSLingrui98 // val table = Module(new SRAMTemplate(SInt(ctrBits.W), set=nRows, way=2*TageBanks, shouldReset=true, holdRead=true, singlePort=false)) 736fe623afSLingrui98 val table = Module(new SRAMTemplate(SInt(ctrBits.W), set=nRows, way=2*TageBanks, shouldReset=true, holdRead=true, singlePort=false, bypassWrite=true)) 7409c6f1ddSLingrui98 75dd6c0695SLingrui98 // def getIdx(hist: UInt, pc: UInt) = { 76dd6c0695SLingrui98 // (compute_folded_ghist(hist, log2Ceil(nRows)) ^ (pc >> instOffsetBits))(log2Ceil(nRows)-1,0) 77dd6c0695SLingrui98 // } 78dd6c0695SLingrui98 79dd6c0695SLingrui98 80dd6c0695SLingrui98 val idxFhInfo = (histLen, min(log2Ceil(nRows), histLen)) 81dd6c0695SLingrui98 82dd6c0695SLingrui98 def getFoldedHistoryInfo = Set(idxFhInfo).filter(_._1 > 0) 83dd6c0695SLingrui98 84dd6c0695SLingrui98 def getIdx(pc: UInt, allFh: AllFoldedHistories) = { 85dd6c0695SLingrui98 if (histLen > 0) { 86dd6c0695SLingrui98 val idx_fh = allFh.getHistWithInfo(idxFhInfo).folded_hist 87dd6c0695SLingrui98 // require(idx_fh.getWidth == log2Ceil(nRows)) 88dd6c0695SLingrui98 ((pc >> instOffsetBits) ^ idx_fh)(log2Ceil(nRows)-1,0) 89dd6c0695SLingrui98 } 90dd6c0695SLingrui98 else { 9134ed6fbcSLingrui98 (pc >> instOffsetBits)(log2Ceil(nRows)-1,0) 92dd6c0695SLingrui98 } 9309c6f1ddSLingrui98 } 9409c6f1ddSLingrui98 9581d86739SLingrui98 9609c6f1ddSLingrui98 def ctrUpdate(ctr: SInt, cond: Bool): SInt = signedSatUpdate(ctr, ctrBits, cond) 9709c6f1ddSLingrui98 98dd6c0695SLingrui98 val s0_idx = getIdx(io.req.bits.pc, io.req.bits.folded_hist) 99005e809bSJiuyang Liu val s1_idx = RegEnable(s0_idx, io.req.valid) 10009c6f1ddSLingrui98 10181d86739SLingrui98 val s1_pc = RegEnable(io.req.bits.pc, io.req.fire()) 10281d86739SLingrui98 val s1_unhashed_idx = s1_pc >> instOffsetBits 10381d86739SLingrui98 10409c6f1ddSLingrui98 table.io.r.req.valid := io.req.valid 10509c6f1ddSLingrui98 table.io.r.req.bits.setIdx := s0_idx 10609c6f1ddSLingrui98 10781d86739SLingrui98 val per_br_ctrs_unshuffled = table.io.r.resp.data.sliding(2,2).toSeq.map(VecInit(_)) 10881d86739SLingrui98 val per_br_ctrs = VecInit((0 until numBr).map(i => Mux1H( 10981d86739SLingrui98 UIntToOH(get_phy_br_idx(s1_unhashed_idx, i), numBr), 11081d86739SLingrui98 per_br_ctrs_unshuffled 11181d86739SLingrui98 ))) 11209c6f1ddSLingrui98 11381d86739SLingrui98 io.resp.ctrs := per_br_ctrs 11481d86739SLingrui98 11581d86739SLingrui98 val update_wdata = Wire(Vec(numBr, SInt(ctrBits.W))) // correspond to physical bridx 11634ed6fbcSLingrui98 val update_wdata_packed = VecInit(update_wdata.map(Seq.fill(2)(_)).reduce(_++_)) 11781d86739SLingrui98 val updateWayMask = Wire(Vec(2*numBr, Bool())) // correspond to physical bridx 11834ed6fbcSLingrui98 11981d86739SLingrui98 val update_unhashed_idx = io.update.pc >> instOffsetBits 12081d86739SLingrui98 for (pi <- 0 until numBr) { 12181d86739SLingrui98 updateWayMask(2*pi) := Seq.tabulate(numBr)(li => 12281d86739SLingrui98 io.update.mask(li) && get_phy_br_idx(update_unhashed_idx, li) === pi.U && !io.update.tagePreds(li) 12381d86739SLingrui98 ).reduce(_||_) 12481d86739SLingrui98 updateWayMask(2*pi+1) := Seq.tabulate(numBr)(li => 12581d86739SLingrui98 io.update.mask(li) && get_phy_br_idx(update_unhashed_idx, li) === pi.U && io.update.tagePreds(li) 12681d86739SLingrui98 ).reduce(_||_) 12734ed6fbcSLingrui98 } 12809c6f1ddSLingrui98 129dd6c0695SLingrui98 val update_idx = getIdx(io.update.pc, io.update.folded_hist) 13009c6f1ddSLingrui98 13109c6f1ddSLingrui98 table.io.w.apply( 13234ed6fbcSLingrui98 valid = io.update.mask.reduce(_||_), 13334ed6fbcSLingrui98 data = update_wdata_packed, 13409c6f1ddSLingrui98 setIdx = update_idx, 13534ed6fbcSLingrui98 waymask = updateWayMask.asUInt 13609c6f1ddSLingrui98 ) 13709c6f1ddSLingrui98 13812cedb6fSLingrui98 val wrBypassEntries = 16 13909c6f1ddSLingrui98 14081d86739SLingrui98 // let it corresponds to logical brIdx 14112cedb6fSLingrui98 val wrbypasses = Seq.fill(numBr)(Module(new WrBypass(SInt(ctrBits.W), wrBypassEntries, log2Ceil(nRows), numWays=2))) 14209c6f1ddSLingrui98 14381d86739SLingrui98 for (pi <- 0 until numBr) { 14481d86739SLingrui98 val br_lidx = get_lgc_br_idx(update_unhashed_idx, pi.U(log2Ceil(numBr).W)) 14512cedb6fSLingrui98 14681d86739SLingrui98 val wrbypass_io = Mux1H(UIntToOH(br_lidx, numBr), wrbypasses.map(_.io)) 14781d86739SLingrui98 14881d86739SLingrui98 val ctrPos = Mux1H(UIntToOH(br_lidx, numBr), io.update.tagePreds) 14981d86739SLingrui98 val bypass_ctr = wrbypass_io.hit_data(ctrPos) 15081d86739SLingrui98 val previous_ctr = Mux1H(UIntToOH(br_lidx, numBr), io.update.oldCtrs) 15181d86739SLingrui98 val hit_and_valid = wrbypass_io.hit && bypass_ctr.valid 15281d86739SLingrui98 val oldCtr = Mux(hit_and_valid, bypass_ctr.bits, previous_ctr) 15381d86739SLingrui98 val taken = Mux1H(UIntToOH(br_lidx, numBr), io.update.takens) 15481d86739SLingrui98 update_wdata(pi) := ctrUpdate(oldCtr, taken) 15581d86739SLingrui98 } 15681d86739SLingrui98 15781d86739SLingrui98 val per_br_update_wdata_packed = update_wdata_packed.sliding(2,2).map(VecInit(_)).toSeq 15881d86739SLingrui98 val per_br_update_way_mask = updateWayMask.sliding(2,2).map(VecInit(_)).toSeq 15981d86739SLingrui98 for (li <- 0 until numBr) { 16081d86739SLingrui98 val wrbypass = wrbypasses(li) 16181d86739SLingrui98 val br_pidx = get_phy_br_idx(update_unhashed_idx, li) 16281d86739SLingrui98 wrbypass.io.wen := io.update.mask(li) 16312cedb6fSLingrui98 wrbypass.io.write_idx := update_idx 16481d86739SLingrui98 wrbypass.io.write_data := Mux1H(UIntToOH(br_pidx, numBr), per_br_update_wdata_packed) 16581d86739SLingrui98 wrbypass.io.write_way_mask.map(_ := Mux1H(UIntToOH(br_pidx, numBr), per_br_update_way_mask)) 16634ed6fbcSLingrui98 } 16709c6f1ddSLingrui98 16809c6f1ddSLingrui98 16909c6f1ddSLingrui98 val u = io.update 17009c6f1ddSLingrui98 XSDebug(io.req.valid, 17109c6f1ddSLingrui98 p"scTableReq: pc=0x${Hexadecimal(io.req.bits.pc)}, " + 172e69b7315SLingrui98 p"s0_idx=${s0_idx}\n") 17309c6f1ddSLingrui98 XSDebug(RegNext(io.req.valid), 17409c6f1ddSLingrui98 p"scTableResp: s1_idx=${s1_idx}," + 17534ed6fbcSLingrui98 p"ctr:${io.resp.ctrs}\n") 17634ed6fbcSLingrui98 XSDebug(io.update.mask.reduce(_||_), 177e69b7315SLingrui98 p"update Table: pc:${Hexadecimal(u.pc)}, " + 17834ed6fbcSLingrui98 p"tageTakens:${u.tagePreds}, taken:${u.takens}, oldCtr:${u.oldCtrs}\n") 17909c6f1ddSLingrui98} 18009c6f1ddSLingrui98 18109c6f1ddSLingrui98class SCThreshold(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle { 18209c6f1ddSLingrui98 val ctr = UInt(ctrBits.W) 18309c6f1ddSLingrui98 def satPos(ctr: UInt = this.ctr) = ctr === ((1.U << ctrBits) - 1.U) 18409c6f1ddSLingrui98 def satNeg(ctr: UInt = this.ctr) = ctr === 0.U 18567ba96b4SYinan Xu def neutralVal = (1 << (ctrBits - 1)).U 18609c6f1ddSLingrui98 val thres = UInt(8.W) 18709c6f1ddSLingrui98 def initVal = 6.U 18809c6f1ddSLingrui98 def minThres = 6.U 18909c6f1ddSLingrui98 def maxThres = 31.U 19009c6f1ddSLingrui98 def update(cause: Bool): SCThreshold = { 19109c6f1ddSLingrui98 val res = Wire(new SCThreshold(this.ctrBits)) 19209c6f1ddSLingrui98 val newCtr = satUpdate(this.ctr, this.ctrBits, cause) 19309c6f1ddSLingrui98 val newThres = Mux(res.satPos(newCtr) && this.thres <= maxThres, this.thres + 2.U, 19409c6f1ddSLingrui98 Mux(res.satNeg(newCtr) && this.thres >= minThres, this.thres - 2.U, 19509c6f1ddSLingrui98 this.thres)) 19609c6f1ddSLingrui98 res.thres := newThres 19709c6f1ddSLingrui98 res.ctr := Mux(res.satPos(newCtr) || res.satNeg(newCtr), res.neutralVal, newCtr) 19809c6f1ddSLingrui98 // XSDebug(true.B, p"scThres Update: cause${cause} newCtr ${newCtr} newThres ${newThres}\n") 19909c6f1ddSLingrui98 res 20009c6f1ddSLingrui98 } 20109c6f1ddSLingrui98} 20209c6f1ddSLingrui98 20309c6f1ddSLingrui98object SCThreshold { 20409c6f1ddSLingrui98 def apply(bits: Int)(implicit p: Parameters) = { 20509c6f1ddSLingrui98 val t = Wire(new SCThreshold(ctrBits=bits)) 20609c6f1ddSLingrui98 t.ctr := t.neutralVal 20709c6f1ddSLingrui98 t.thres := t.initVal 20809c6f1ddSLingrui98 t 20909c6f1ddSLingrui98 } 21009c6f1ddSLingrui98} 21109c6f1ddSLingrui98 21209c6f1ddSLingrui98 2131ca0e4f3SYinan Xutrait HasSC extends HasSCParameter with HasPerfEvents { this: Tage => 214efe3f3bbSSteve Gou val update_on_mispred, update_on_unconf = WireInit(0.U.asTypeOf(Vec(TageBanks, Bool()))) 215dd6c0695SLingrui98 var sc_fh_info = Set[FoldedHistoryInfo]() 216bf358e08SLingrui98 if (EnableSC) { 21734ed6fbcSLingrui98 val scTables = SCTableInfos.map { 21809c6f1ddSLingrui98 case (nRows, ctrBits, histLen) => { 21909c6f1ddSLingrui98 val t = Module(new SCTable(nRows/TageBanks, ctrBits, histLen)) 22009c6f1ddSLingrui98 val req = t.io.req 221*adc0b8dfSGuokai Chen req.valid := io.s0_fire(3) 222*adc0b8dfSGuokai Chen req.bits.pc := s0_pc_dup(3) 223*adc0b8dfSGuokai Chen req.bits.folded_hist := io.in.bits.folded_hist(3) 22486d9c530SLingrui98 req.bits.ghist := DontCare 22509c6f1ddSLingrui98 if (!EnableSC) {t.io.update := DontCare} 22609c6f1ddSLingrui98 t 22709c6f1ddSLingrui98 } 22809c6f1ddSLingrui98 } 22934ed6fbcSLingrui98 sc_fh_info = scTables.map(_.getFoldedHistoryInfo).reduce(_++_).toSet 23009c6f1ddSLingrui98 23109c6f1ddSLingrui98 val scThresholds = List.fill(TageBanks)(RegInit(SCThreshold(5))) 23209c6f1ddSLingrui98 val useThresholds = VecInit(scThresholds map (_.thres)) 2337e8b966aSLingrui98 234d71e9942SLingrui98 def sign(x: SInt) = x(x.getWidth-1) 235d71e9942SLingrui98 def pos(x: SInt) = !sign(x) 236d71e9942SLingrui98 def neg(x: SInt) = sign(x) 2377e8b966aSLingrui98 2387e8b966aSLingrui98 def aboveThreshold(scSum: SInt, tagePvdr: SInt, threshold: UInt): Bool = { 239d71e9942SLingrui98 val signedThres = threshold.zext 2407e8b966aSLingrui98 val totalSum = scSum +& tagePvdr 2417e8b966aSLingrui98 (scSum > signedThres - tagePvdr) && pos(totalSum) || 2427e8b966aSLingrui98 (scSum < -signedThres - tagePvdr) && neg(totalSum) 243d71e9942SLingrui98 } 24409c6f1ddSLingrui98 val updateThresholds = VecInit(useThresholds map (t => (t << 3) +& 21.U)) 24509c6f1ddSLingrui98 24634ed6fbcSLingrui98 val s1_scResps = VecInit(scTables.map(t => t.io.resp)) 24709c6f1ddSLingrui98 24834ed6fbcSLingrui98 val scUpdateMask = WireInit(0.U.asTypeOf(Vec(numBr, Vec(SCNTables, Bool())))) 24909c6f1ddSLingrui98 val scUpdateTagePreds = Wire(Vec(TageBanks, Bool())) 25009c6f1ddSLingrui98 val scUpdateTakens = Wire(Vec(TageBanks, Bool())) 25134ed6fbcSLingrui98 val scUpdateOldCtrs = Wire(Vec(numBr, Vec(SCNTables, SInt(SCCtrBits.W)))) 25209c6f1ddSLingrui98 scUpdateTagePreds := DontCare 25309c6f1ddSLingrui98 scUpdateTakens := DontCare 25409c6f1ddSLingrui98 scUpdateOldCtrs := DontCare 25509c6f1ddSLingrui98 25634ed6fbcSLingrui98 val updateSCMeta = updateMeta.scMeta.get 25709c6f1ddSLingrui98 25809c6f1ddSLingrui98 val s2_sc_used, s2_conf, s2_unconf, s2_agree, s2_disagree = 259ff1cd593SLingrui98 WireInit(0.U.asTypeOf(Vec(TageBanks, Bool()))) 26009c6f1ddSLingrui98 val update_sc_used, update_conf, update_unconf, update_agree, update_disagree = 261ff1cd593SLingrui98 WireInit(0.U.asTypeOf(Vec(TageBanks, Bool()))) 262efe3f3bbSSteve Gou val sc_misp_tage_corr, sc_corr_tage_misp = 263ff1cd593SLingrui98 WireInit(0.U.asTypeOf(Vec(TageBanks, Bool()))) 26409c6f1ddSLingrui98 26509c6f1ddSLingrui98 // for sc ctrs 266238c84b9SLingrui98 def getCentered(ctr: SInt): SInt = Cat(ctr, 1.U(1.W)).asSInt 267238c84b9SLingrui98 // for tage ctrs, (2*(ctr-4)+1)*8 268238c84b9SLingrui98 def getPvdrCentered(ctr: UInt): SInt = Cat(ctr ^ (1 << (TageCtrBits-1)).U, 1.U(1.W), 0.U(3.W)).asSInt 26909c6f1ddSLingrui98 27034ed6fbcSLingrui98 val scMeta = resp_meta.scMeta.get 27109c6f1ddSLingrui98 scMeta := DontCare 27234ed6fbcSLingrui98 for (w <- 0 until TageBanks) { 27309c6f1ddSLingrui98 // do summation in s2 27409c6f1ddSLingrui98 val s1_scTableSums = VecInit( 27509c6f1ddSLingrui98 (0 to 1) map { i => 27634ed6fbcSLingrui98 ParallelSingedExpandingAdd(s1_scResps map (r => getCentered(r.ctrs(w)(i)))) // TODO: rewrite with wallace tree 27709c6f1ddSLingrui98 } 27809c6f1ddSLingrui98 ) 279*adc0b8dfSGuokai Chen val s2_scTableSums = RegEnable(s1_scTableSums, io.s1_fire(3)) 280*adc0b8dfSGuokai Chen val s2_tagePrvdCtrCentered = getPvdrCentered(RegEnable(s1_providerResps(w).ctr, io.s1_fire(3))) 281cb4f77ceSLingrui98 val s2_totalSums = s2_scTableSums.map(_ +& s2_tagePrvdCtrCentered) 282e82f7653SSteve Gou val s2_sumAboveThresholds = VecInit((0 to 1).map(i => aboveThreshold(s2_scTableSums(i), s2_tagePrvdCtrCentered, useThresholds(w)))) 283cb4f77ceSLingrui98 val s2_scPreds = VecInit(s2_totalSums.map(_ >= 0.S)) 28409c6f1ddSLingrui98 285*adc0b8dfSGuokai Chen val s2_scResps = VecInit(RegEnable(s1_scResps, io.s1_fire(3)).map(_.ctrs(w))) 286*adc0b8dfSGuokai Chen val s2_scCtrs = VecInit(s2_scResps.map(_(s2_tageTakens_dup(3)(w).asUInt))) 287*adc0b8dfSGuokai Chen val s2_chooseBit = s2_tageTakens_dup(3)(w) 28809c6f1ddSLingrui98 289cb4f77ceSLingrui98 val s2_pred = 2904813e060SLingrui98 Mux(s2_provideds(w) && s2_sumAboveThresholds(s2_chooseBit), 291cb4f77ceSLingrui98 s2_scPreds(s2_chooseBit), 292*adc0b8dfSGuokai Chen s2_tageTakens_dup(3)(w) 293cb4f77ceSLingrui98 ) 294cb4f77ceSLingrui98 295*adc0b8dfSGuokai Chen val s3_disagree = RegEnable(s2_disagree, io.s2_fire(3)) 296d2b20d1aSTang Haojin // FIXME: not portable 297*adc0b8dfSGuokai Chen io.out.last_stage_ftb_entry.brSlots(0).sc := RegEnable(s2_disagree(0), io.s2_fire(3)) 298*adc0b8dfSGuokai Chen io.out.last_stage_ftb_entry.tailSlot.sc := RegEnable(s2_disagree(1), io.s2_fire(3)) 299d2b20d1aSTang Haojin 300*adc0b8dfSGuokai Chen scMeta.tageTakens(w) := RegEnable(s2_tageTakens_dup(3)(w), io.s2_fire(3)) 301*adc0b8dfSGuokai Chen scMeta.scUsed(w) := RegEnable(s2_provideds(w), io.s2_fire(3)) 302*adc0b8dfSGuokai Chen scMeta.scPreds(w) := RegEnable(s2_scPreds(s2_chooseBit), io.s2_fire(3)) 303*adc0b8dfSGuokai Chen scMeta.ctrs(w) := RegEnable(s2_scCtrs, io.s2_fire(3)) 30434ed6fbcSLingrui98 3054813e060SLingrui98 when (s2_provideds(w)) { 30609c6f1ddSLingrui98 s2_sc_used(w) := true.B 307b30c10d6SLingrui98 s2_unconf(w) := !s2_sumAboveThresholds(s2_chooseBit) 308b30c10d6SLingrui98 s2_conf(w) := s2_sumAboveThresholds(s2_chooseBit) 30909c6f1ddSLingrui98 // Use prediction from Statistical Corrector 31009c6f1ddSLingrui98 XSDebug(p"---------tage_bank_${w} provided so that sc used---------\n") 311b30c10d6SLingrui98 when (s2_sumAboveThresholds(s2_chooseBit)) { 31209c6f1ddSLingrui98 val pred = s2_scPreds(s2_chooseBit) 31309c6f1ddSLingrui98 val debug_pc = Cat(debug_pc_s2, w.U, 0.U(instOffsetBits.W)) 314*adc0b8dfSGuokai Chen s2_agree(w) := s2_tageTakens_dup(3)(w) === pred 315*adc0b8dfSGuokai Chen s2_disagree(w) := s2_tageTakens_dup(3)(w) =/= pred 31609c6f1ddSLingrui98 // fit to always-taken condition 317c2d1ec7dSLingrui98 // io.out.s2.full_pred.br_taken_mask(w) := pred 31809c6f1ddSLingrui98 XSDebug(p"pc(${Hexadecimal(debug_pc)}) SC(${w.U}) overriden pred to ${pred}\n") 31909c6f1ddSLingrui98 } 32009c6f1ddSLingrui98 } 32109c6f1ddSLingrui98 322*adc0b8dfSGuokai Chen val s3_pred_dup = io.s2_fire.map(f => RegEnable(s2_pred, f)) 323*adc0b8dfSGuokai Chen val sc_enable_dup = dup(RegNext(io.ctrl.sc_enable)) 324*adc0b8dfSGuokai Chen for (sc_enable & fp & s3_pred <- 325*adc0b8dfSGuokai Chen sc_enable_dup zip io.out.s3.full_pred zip s3_pred_dup) { 326*adc0b8dfSGuokai Chen when (sc_enable) { 327*adc0b8dfSGuokai Chen fp.br_taken_mask(w) := s3_pred 328*adc0b8dfSGuokai Chen } 3296ee06c7aSSteve Gou } 330b30c10d6SLingrui98 33134ed6fbcSLingrui98 val updateTageMeta = updateMeta 332744dc920SLingrui98 when (updateValids(w) && updateSCMeta.scUsed(w)) { 33334ed6fbcSLingrui98 val scPred = updateSCMeta.scPreds(w) 33434ed6fbcSLingrui98 val tagePred = updateSCMeta.tageTakens(w) 335803124a6SLingrui98 val taken = update.br_taken_mask(w) 33634ed6fbcSLingrui98 val scOldCtrs = updateSCMeta.ctrs(w) 3374813e060SLingrui98 val pvdrCtr = updateTageMeta.providerResps(w).ctr 33809c6f1ddSLingrui98 val sum = ParallelSingedExpandingAdd(scOldCtrs.map(getCentered)) +& getPvdrCentered(pvdrCtr) 33909c6f1ddSLingrui98 val sumAbs = sum.abs.asUInt 340ff1cd593SLingrui98 val updateThres = updateThresholds(w) 341ff1cd593SLingrui98 val sumAboveThreshold = aboveThreshold(sum, getPvdrCentered(pvdrCtr), updateThres) 34209c6f1ddSLingrui98 scUpdateTagePreds(w) := tagePred 34309c6f1ddSLingrui98 scUpdateTakens(w) := taken 34409c6f1ddSLingrui98 (scUpdateOldCtrs(w) zip scOldCtrs).foreach{case (t, c) => t := c} 34509c6f1ddSLingrui98 34609c6f1ddSLingrui98 update_sc_used(w) := true.B 347b30c10d6SLingrui98 update_unconf(w) := !sumAboveThreshold 348b30c10d6SLingrui98 update_conf(w) := sumAboveThreshold 34909c6f1ddSLingrui98 update_agree(w) := scPred === tagePred 35009c6f1ddSLingrui98 update_disagree(w) := scPred =/= tagePred 35109c6f1ddSLingrui98 sc_corr_tage_misp(w) := scPred === taken && tagePred =/= taken && update_conf(w) 35209c6f1ddSLingrui98 sc_misp_tage_corr(w) := scPred =/= taken && tagePred === taken && update_conf(w) 35309c6f1ddSLingrui98 35409c6f1ddSLingrui98 val thres = useThresholds(w) 35509c6f1ddSLingrui98 when (scPred =/= tagePred && sumAbs >= thres - 4.U && sumAbs <= thres - 2.U) { 35609c6f1ddSLingrui98 val newThres = scThresholds(w).update(scPred =/= taken) 35709c6f1ddSLingrui98 scThresholds(w) := newThres 35809c6f1ddSLingrui98 XSDebug(p"scThres $w update: old ${useThresholds(w)} --> new ${newThres.thres}\n") 35909c6f1ddSLingrui98 } 36009c6f1ddSLingrui98 361b30c10d6SLingrui98 when (scPred =/= taken || !sumAboveThreshold) { 36209c6f1ddSLingrui98 scUpdateMask(w).foreach(_ := true.B) 36309c6f1ddSLingrui98 XSDebug(sum < 0.S, 36409c6f1ddSLingrui98 p"scUpdate: bank(${w}), scPred(${scPred}), tagePred(${tagePred}), " + 36509c6f1ddSLingrui98 p"scSum(-$sumAbs), mispred: sc(${scPred =/= taken}), tage(${updateMisPreds(w)})\n" 36609c6f1ddSLingrui98 ) 36709c6f1ddSLingrui98 XSDebug(sum >= 0.S, 36809c6f1ddSLingrui98 p"scUpdate: bank(${w}), scPred(${scPred}), tagePred(${tagePred}), " + 36909c6f1ddSLingrui98 p"scSum(+$sumAbs), mispred: sc(${scPred =/= taken}), tage(${updateMisPreds(w)})\n" 37009c6f1ddSLingrui98 ) 37109c6f1ddSLingrui98 XSDebug(p"bank(${w}), update: sc: ${updateSCMeta}\n") 37209c6f1ddSLingrui98 update_on_mispred(w) := scPred =/= taken 37309c6f1ddSLingrui98 update_on_unconf(w) := scPred === taken 37409c6f1ddSLingrui98 } 37509c6f1ddSLingrui98 } 37609c6f1ddSLingrui98 } 37709c6f1ddSLingrui98 37809c6f1ddSLingrui98 37909c6f1ddSLingrui98 for (b <- 0 until TageBanks) { 38034ed6fbcSLingrui98 for (i <- 0 until SCNTables) { 38134ed6fbcSLingrui98 scTables(i).io.update.mask(b) := RegNext(scUpdateMask(b)(i)) 38234ed6fbcSLingrui98 scTables(i).io.update.tagePreds(b) := RegNext(scUpdateTagePreds(b)) 38334ed6fbcSLingrui98 scTables(i).io.update.takens(b) := RegNext(scUpdateTakens(b)) 38434ed6fbcSLingrui98 scTables(i).io.update.oldCtrs(b) := RegNext(scUpdateOldCtrs(b)(i)) 38534ed6fbcSLingrui98 scTables(i).io.update.pc := RegNext(update.pc) 38634ed6fbcSLingrui98 scTables(i).io.update.folded_hist := RegNext(updateFHist) 38709c6f1ddSLingrui98 } 38809c6f1ddSLingrui98 } 38909c6f1ddSLingrui98 39009c6f1ddSLingrui98 tage_perf("sc_conf", PopCount(s2_conf), PopCount(update_conf)) 39109c6f1ddSLingrui98 tage_perf("sc_unconf", PopCount(s2_unconf), PopCount(update_unconf)) 39209c6f1ddSLingrui98 tage_perf("sc_agree", PopCount(s2_agree), PopCount(update_agree)) 39309c6f1ddSLingrui98 tage_perf("sc_disagree", PopCount(s2_disagree), PopCount(update_disagree)) 39409c6f1ddSLingrui98 tage_perf("sc_used", PopCount(s2_sc_used), PopCount(update_sc_used)) 39509c6f1ddSLingrui98 XSPerfAccumulate("sc_update_on_mispred", PopCount(update_on_mispred)) 39609c6f1ddSLingrui98 XSPerfAccumulate("sc_update_on_unconf", PopCount(update_on_unconf)) 39709c6f1ddSLingrui98 XSPerfAccumulate("sc_mispred_but_tage_correct", PopCount(sc_misp_tage_corr)) 39809c6f1ddSLingrui98 XSPerfAccumulate("sc_correct_and_tage_wrong", PopCount(sc_corr_tage_misp)) 399cd365d4cSrvcoresjw 400efe3f3bbSSteve Gou } 401efe3f3bbSSteve Gou 402dd6c0695SLingrui98 override def getFoldedHistoryInfo = Some(tage_fh_info ++ sc_fh_info) 403dd6c0695SLingrui98 4044813e060SLingrui98 override val perfEvents = Seq( 4054813e060SLingrui98 ("tage_tht_hit ", PopCount(updateMeta.providers.map(_.valid))), 406cd365d4cSrvcoresjw ("sc_update_on_mispred ", PopCount(update_on_mispred) ), 407cd365d4cSrvcoresjw ("sc_update_on_unconf ", PopCount(update_on_unconf) ), 408cd365d4cSrvcoresjw ) 4091ca0e4f3SYinan Xu generatePerfEvent() 410bf358e08SLingrui98} 411