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 198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 2009c6f1ddSLingrui98import chisel3._ 2109c6f1ddSLingrui98import chisel3.util._ 2209c6f1ddSLingrui98import xiangshan._ 2309c6f1ddSLingrui98import utils._ 243c02ee8fSwakafaimport utility._ 2509c6f1ddSLingrui98 2609c6f1ddSLingrui98import scala.math.min 27adc0b8dfSGuokai Chenimport scala.{Tuple2 => &} 28eeb5ff92SLingrui98import os.copy 2909c6f1ddSLingrui98 3009c6f1ddSLingrui98 3109c6f1ddSLingrui98trait FTBParams extends HasXSParameter with HasBPUConst { 32b37e4b45SLingrui98 val numEntries = FtbSize 33b37e4b45SLingrui98 val numWays = FtbWays 3409c6f1ddSLingrui98 val numSets = numEntries/numWays // 512 3509c6f1ddSLingrui98 val tagSize = 20 3609c6f1ddSLingrui98 37eeb5ff92SLingrui98 38eeb5ff92SLingrui98 3909c6f1ddSLingrui98 val TAR_STAT_SZ = 2 4009c6f1ddSLingrui98 def TAR_FIT = 0.U(TAR_STAT_SZ.W) 4109c6f1ddSLingrui98 def TAR_OVF = 1.U(TAR_STAT_SZ.W) 4209c6f1ddSLingrui98 def TAR_UDF = 2.U(TAR_STAT_SZ.W) 4309c6f1ddSLingrui98 44bf358e08SLingrui98 def BR_OFFSET_LEN = 12 45bf358e08SLingrui98 def JMP_OFFSET_LEN = 20 4609c6f1ddSLingrui98} 4709c6f1ddSLingrui98 48*deb3a97eSGao-Zeyuclass FtbSlot_FtqMem(implicit p: Parameters) extends XSBundle with FTBParams { 49*deb3a97eSGao-Zeyu val offset = UInt(log2Ceil(PredictWidth).W) 50*deb3a97eSGao-Zeyu val sharing = Bool() 51*deb3a97eSGao-Zeyu val valid = Bool() 52*deb3a97eSGao-Zeyu} 53*deb3a97eSGao-Zeyu 54*deb3a97eSGao-Zeyuclass FtbSlot(val offsetLen: Int, val subOffsetLen: Option[Int] = None)(implicit p: Parameters) extends FtbSlot_FtqMem with FTBParams { 55b30c10d6SLingrui98 if (subOffsetLen.isDefined) { 56b30c10d6SLingrui98 require(subOffsetLen.get <= offsetLen) 57b30c10d6SLingrui98 } 58eeb5ff92SLingrui98 val lower = UInt(offsetLen.W) 59eeb5ff92SLingrui98 val tarStat = UInt(TAR_STAT_SZ.W) 6009c6f1ddSLingrui98 61eeb5ff92SLingrui98 def setLowerStatByTarget(pc: UInt, target: UInt, isShare: Boolean) = { 62eeb5ff92SLingrui98 def getTargetStatByHigher(pc_higher: UInt, target_higher: UInt) = 63eeb5ff92SLingrui98 Mux(target_higher > pc_higher, TAR_OVF, 64eeb5ff92SLingrui98 Mux(target_higher < pc_higher, TAR_UDF, TAR_FIT)) 65eeb5ff92SLingrui98 def getLowerByTarget(target: UInt, offsetLen: Int) = target(offsetLen, 1) 66b30c10d6SLingrui98 val offLen = if (isShare) this.subOffsetLen.get else this.offsetLen 67eeb5ff92SLingrui98 val pc_higher = pc(VAddrBits-1, offLen+1) 68eeb5ff92SLingrui98 val target_higher = target(VAddrBits-1, offLen+1) 69eeb5ff92SLingrui98 val stat = getTargetStatByHigher(pc_higher, target_higher) 70eeb5ff92SLingrui98 val lower = ZeroExt(getLowerByTarget(target, offLen), this.offsetLen) 71eeb5ff92SLingrui98 this.lower := lower 72eeb5ff92SLingrui98 this.tarStat := stat 73eeb5ff92SLingrui98 this.sharing := isShare.B 74eeb5ff92SLingrui98 } 7509c6f1ddSLingrui98 76b30c10d6SLingrui98 def getTarget(pc: UInt, last_stage: Option[Tuple2[UInt, Bool]] = None) = { 77b30c10d6SLingrui98 def getTarget(offLen: Int)(pc: UInt, lower: UInt, stat: UInt, 78b30c10d6SLingrui98 last_stage: Option[Tuple2[UInt, Bool]] = None) = { 79b30c10d6SLingrui98 val h = pc(VAddrBits - 1, offLen + 1) 80b30c10d6SLingrui98 val higher = Wire(UInt((VAddrBits - offLen - 1).W)) 81b30c10d6SLingrui98 val higher_plus_one = Wire(UInt((VAddrBits - offLen - 1).W)) 82b30c10d6SLingrui98 val higher_minus_one = Wire(UInt((VAddrBits-offLen-1).W)) 8347c003a9SEaston Man 8447c003a9SEaston Man // Switch between previous stage pc and current stage pc 8547c003a9SEaston Man // Give flexibility for timing 86b30c10d6SLingrui98 if (last_stage.isDefined) { 87b30c10d6SLingrui98 val last_stage_pc = last_stage.get._1 88b30c10d6SLingrui98 val last_stage_pc_h = last_stage_pc(VAddrBits-1, offLen+1) 89b30c10d6SLingrui98 val stage_en = last_stage.get._2 90b30c10d6SLingrui98 higher := RegEnable(last_stage_pc_h, stage_en) 91b30c10d6SLingrui98 higher_plus_one := RegEnable(last_stage_pc_h+1.U, stage_en) 92b30c10d6SLingrui98 higher_minus_one := RegEnable(last_stage_pc_h-1.U, stage_en) 93b30c10d6SLingrui98 } else { 94b30c10d6SLingrui98 higher := h 95b30c10d6SLingrui98 higher_plus_one := h + 1.U 96b30c10d6SLingrui98 higher_minus_one := h - 1.U 97b30c10d6SLingrui98 } 98eeb5ff92SLingrui98 val target = 99eeb5ff92SLingrui98 Cat( 100b30c10d6SLingrui98 Mux1H(Seq( 101b30c10d6SLingrui98 (stat === TAR_OVF, higher_plus_one), 102b30c10d6SLingrui98 (stat === TAR_UDF, higher_minus_one), 103b30c10d6SLingrui98 (stat === TAR_FIT, higher), 104b30c10d6SLingrui98 )), 105eeb5ff92SLingrui98 lower(offLen-1, 0), 0.U(1.W) 106eeb5ff92SLingrui98 ) 107eeb5ff92SLingrui98 require(target.getWidth == VAddrBits) 108eeb5ff92SLingrui98 require(offLen != 0) 109eeb5ff92SLingrui98 target 110eeb5ff92SLingrui98 } 111b30c10d6SLingrui98 if (subOffsetLen.isDefined) 112eeb5ff92SLingrui98 Mux(sharing, 113b30c10d6SLingrui98 getTarget(subOffsetLen.get)(pc, lower, tarStat, last_stage), 114b30c10d6SLingrui98 getTarget(offsetLen)(pc, lower, tarStat, last_stage) 115eeb5ff92SLingrui98 ) 116eeb5ff92SLingrui98 else 117b30c10d6SLingrui98 getTarget(offsetLen)(pc, lower, tarStat, last_stage) 118eeb5ff92SLingrui98 } 119eeb5ff92SLingrui98 def fromAnotherSlot(that: FtbSlot) = { 120eeb5ff92SLingrui98 require( 121b30c10d6SLingrui98 this.offsetLen > that.offsetLen && this.subOffsetLen.map(_ == that.offsetLen).getOrElse(true) || 122eeb5ff92SLingrui98 this.offsetLen == that.offsetLen 123eeb5ff92SLingrui98 ) 124eeb5ff92SLingrui98 this.offset := that.offset 125eeb5ff92SLingrui98 this.tarStat := that.tarStat 126b30c10d6SLingrui98 this.sharing := (this.offsetLen > that.offsetLen && that.offsetLen == this.subOffsetLen.get).B 127eeb5ff92SLingrui98 this.valid := that.valid 128eeb5ff92SLingrui98 this.lower := ZeroExt(that.lower, this.offsetLen) 129eeb5ff92SLingrui98 } 130eeb5ff92SLingrui98 131eeb5ff92SLingrui98} 132eeb5ff92SLingrui98 133*deb3a97eSGao-Zeyu 134*deb3a97eSGao-Zeyuclass FTBEntry_part(implicit p: Parameters) extends XSBundle with FTBParams with BPUUtils { 135*deb3a97eSGao-Zeyu val isCall = Bool() 136*deb3a97eSGao-Zeyu val isRet = Bool() 137*deb3a97eSGao-Zeyu val isJalr = Bool() 138*deb3a97eSGao-Zeyu 139*deb3a97eSGao-Zeyu def isJal = !isJalr 140*deb3a97eSGao-Zeyu} 141*deb3a97eSGao-Zeyu 142*deb3a97eSGao-Zeyuclass FTBEntry_FtqMem(implicit p: Parameters) extends FTBEntry_part with FTBParams with BPUUtils { 143*deb3a97eSGao-Zeyu 144*deb3a97eSGao-Zeyu val brSlots = Vec(numBrSlot, new FtbSlot_FtqMem) 145*deb3a97eSGao-Zeyu val tailSlot = new FtbSlot_FtqMem 146*deb3a97eSGao-Zeyu 147*deb3a97eSGao-Zeyu def jmpValid = { 148*deb3a97eSGao-Zeyu tailSlot.valid && !tailSlot.sharing 149*deb3a97eSGao-Zeyu } 150*deb3a97eSGao-Zeyu 151*deb3a97eSGao-Zeyu def getBrRecordedVec(offset: UInt) = { 152*deb3a97eSGao-Zeyu VecInit( 153*deb3a97eSGao-Zeyu brSlots.map(s => s.valid && s.offset === offset) :+ 154*deb3a97eSGao-Zeyu (tailSlot.valid && tailSlot.offset === offset && tailSlot.sharing) 155*deb3a97eSGao-Zeyu ) 156*deb3a97eSGao-Zeyu } 157*deb3a97eSGao-Zeyu 158*deb3a97eSGao-Zeyu def brIsSaved(offset: UInt) = getBrRecordedVec(offset).reduce(_||_) 159*deb3a97eSGao-Zeyu 160*deb3a97eSGao-Zeyu def getBrMaskByOffset(offset: UInt) = 161*deb3a97eSGao-Zeyu brSlots.map{ s => s.valid && s.offset <= offset } :+ 162*deb3a97eSGao-Zeyu (tailSlot.valid && tailSlot.offset <= offset && tailSlot.sharing) 163*deb3a97eSGao-Zeyu 164*deb3a97eSGao-Zeyu def newBrCanNotInsert(offset: UInt) = { 165*deb3a97eSGao-Zeyu val lastSlotForBr = tailSlot 166*deb3a97eSGao-Zeyu lastSlotForBr.valid && lastSlotForBr.offset < offset 167*deb3a97eSGao-Zeyu } 168*deb3a97eSGao-Zeyu 169*deb3a97eSGao-Zeyu} 170*deb3a97eSGao-Zeyu 171*deb3a97eSGao-Zeyuclass FTBEntry(implicit p: Parameters) extends FTBEntry_part with FTBParams with BPUUtils { 172eeb5ff92SLingrui98 173eeb5ff92SLingrui98 174eeb5ff92SLingrui98 val valid = Bool() 175eeb5ff92SLingrui98 176eeb5ff92SLingrui98 val brSlots = Vec(numBrSlot, new FtbSlot(BR_OFFSET_LEN)) 177eeb5ff92SLingrui98 178b30c10d6SLingrui98 val tailSlot = new FtbSlot(JMP_OFFSET_LEN, Some(BR_OFFSET_LEN)) 17909c6f1ddSLingrui98 18009c6f1ddSLingrui98 // Partial Fall-Through Address 181a60a2901SLingrui98 val pftAddr = UInt(log2Up(PredictWidth).W) 18209c6f1ddSLingrui98 val carry = Bool() 18309c6f1ddSLingrui98 184f4ebc4b2SLingrui98 val last_may_be_rvi_call = Bool() 18509c6f1ddSLingrui98 18609c6f1ddSLingrui98 val always_taken = Vec(numBr, Bool()) 18709c6f1ddSLingrui98 188eeb5ff92SLingrui98 def getSlotForBr(idx: Int): FtbSlot = { 189b37e4b45SLingrui98 require(idx <= numBr-1) 190b37e4b45SLingrui98 (idx, numBr) match { 191b37e4b45SLingrui98 case (i, n) if i == n-1 => this.tailSlot 192eeb5ff92SLingrui98 case _ => this.brSlots(idx) 19309c6f1ddSLingrui98 } 19409c6f1ddSLingrui98 } 195eeb5ff92SLingrui98 def allSlotsForBr = { 196eeb5ff92SLingrui98 (0 until numBr).map(getSlotForBr(_)) 19709c6f1ddSLingrui98 } 19809c6f1ddSLingrui98 def setByBrTarget(brIdx: Int, pc: UInt, target: UInt) = { 199eeb5ff92SLingrui98 val slot = getSlotForBr(brIdx) 200b37e4b45SLingrui98 slot.setLowerStatByTarget(pc, target, brIdx == numBr-1) 20109c6f1ddSLingrui98 } 20209c6f1ddSLingrui98 def setByJmpTarget(pc: UInt, target: UInt) = { 203eeb5ff92SLingrui98 this.tailSlot.setLowerStatByTarget(pc, target, false) 20409c6f1ddSLingrui98 } 20509c6f1ddSLingrui98 206b30c10d6SLingrui98 def getTargetVec(pc: UInt, last_stage: Option[Tuple2[UInt, Bool]] = None) = { 207b30c10d6SLingrui98 VecInit((brSlots :+ tailSlot).map(_.getTarget(pc, last_stage))) 208bf358e08SLingrui98 } 20909c6f1ddSLingrui98 210eeb5ff92SLingrui98 def getOffsetVec = VecInit(brSlots.map(_.offset) :+ tailSlot.offset) 21147c003a9SEaston Man def getFallThrough(pc: UInt, last_stage_entry: Option[Tuple2[FTBEntry, Bool]] = None) = { 21247c003a9SEaston Man if (last_stage_entry.isDefined) { 21347c003a9SEaston Man var stashed_carry = RegEnable(last_stage_entry.get._1.carry, last_stage_entry.get._2) 21447c003a9SEaston Man getFallThroughAddr(pc, stashed_carry, pftAddr) 21547c003a9SEaston Man } else { 21647c003a9SEaston Man getFallThroughAddr(pc, carry, pftAddr) 21747c003a9SEaston Man } 21847c003a9SEaston Man } 21947c003a9SEaston Man 220eeb5ff92SLingrui98 def hasBr(offset: UInt) = 221eeb5ff92SLingrui98 brSlots.map{ s => s.valid && s.offset <= offset}.reduce(_||_) || 222b37e4b45SLingrui98 (tailSlot.valid && tailSlot.offset <= offset && tailSlot.sharing) 22309c6f1ddSLingrui98 224eeb5ff92SLingrui98 def getBrMaskByOffset(offset: UInt) = 225b37e4b45SLingrui98 brSlots.map{ s => s.valid && s.offset <= offset } :+ 226b37e4b45SLingrui98 (tailSlot.valid && tailSlot.offset <= offset && tailSlot.sharing) 227eeb5ff92SLingrui98 228eeb5ff92SLingrui98 def getBrRecordedVec(offset: UInt) = { 229eeb5ff92SLingrui98 VecInit( 230b37e4b45SLingrui98 brSlots.map(s => s.valid && s.offset === offset) :+ 231b37e4b45SLingrui98 (tailSlot.valid && tailSlot.offset === offset && tailSlot.sharing) 232eeb5ff92SLingrui98 ) 23309c6f1ddSLingrui98 } 23409c6f1ddSLingrui98 235eeb5ff92SLingrui98 def brIsSaved(offset: UInt) = getBrRecordedVec(offset).reduce(_||_) 236eeb5ff92SLingrui98 237eeb5ff92SLingrui98 def brValids = { 238eeb5ff92SLingrui98 VecInit( 239b37e4b45SLingrui98 brSlots.map(_.valid) :+ (tailSlot.valid && tailSlot.sharing) 240eeb5ff92SLingrui98 ) 241eeb5ff92SLingrui98 } 242eeb5ff92SLingrui98 243eeb5ff92SLingrui98 def noEmptySlotForNewBr = { 244b37e4b45SLingrui98 VecInit(brSlots.map(_.valid) :+ tailSlot.valid).reduce(_&&_) 245eeb5ff92SLingrui98 } 246eeb5ff92SLingrui98 247eeb5ff92SLingrui98 def newBrCanNotInsert(offset: UInt) = { 248b37e4b45SLingrui98 val lastSlotForBr = tailSlot 249eeb5ff92SLingrui98 lastSlotForBr.valid && lastSlotForBr.offset < offset 250eeb5ff92SLingrui98 } 251eeb5ff92SLingrui98 252eeb5ff92SLingrui98 def jmpValid = { 253b37e4b45SLingrui98 tailSlot.valid && !tailSlot.sharing 254eeb5ff92SLingrui98 } 255eeb5ff92SLingrui98 256eeb5ff92SLingrui98 def brOffset = { 257b37e4b45SLingrui98 VecInit(brSlots.map(_.offset) :+ tailSlot.offset) 258eeb5ff92SLingrui98 } 259eeb5ff92SLingrui98 26009c6f1ddSLingrui98 def display(cond: Bool): Unit = { 26109c6f1ddSLingrui98 XSDebug(cond, p"-----------FTB entry----------- \n") 26209c6f1ddSLingrui98 XSDebug(cond, p"v=${valid}\n") 26309c6f1ddSLingrui98 for(i <- 0 until numBr) { 264eeb5ff92SLingrui98 XSDebug(cond, p"[br$i]: v=${allSlotsForBr(i).valid}, offset=${allSlotsForBr(i).offset}," + 265eeb5ff92SLingrui98 p"lower=${Hexadecimal(allSlotsForBr(i).lower)}\n") 26609c6f1ddSLingrui98 } 267eeb5ff92SLingrui98 XSDebug(cond, p"[tailSlot]: v=${tailSlot.valid}, offset=${tailSlot.offset}," + 268eeb5ff92SLingrui98 p"lower=${Hexadecimal(tailSlot.lower)}, sharing=${tailSlot.sharing}}\n") 26909c6f1ddSLingrui98 XSDebug(cond, p"pftAddr=${Hexadecimal(pftAddr)}, carry=$carry\n") 27009c6f1ddSLingrui98 XSDebug(cond, p"isCall=$isCall, isRet=$isRet, isjalr=$isJalr\n") 271f4ebc4b2SLingrui98 XSDebug(cond, p"last_may_be_rvi_call=$last_may_be_rvi_call\n") 27209c6f1ddSLingrui98 XSDebug(cond, p"------------------------------- \n") 27309c6f1ddSLingrui98 } 27409c6f1ddSLingrui98 27509c6f1ddSLingrui98} 27609c6f1ddSLingrui98 27709c6f1ddSLingrui98class FTBEntryWithTag(implicit p: Parameters) extends XSBundle with FTBParams with BPUUtils { 27809c6f1ddSLingrui98 val entry = new FTBEntry 27909c6f1ddSLingrui98 val tag = UInt(tagSize.W) 28009c6f1ddSLingrui98 def display(cond: Bool): Unit = { 281eeb5ff92SLingrui98 entry.display(cond) 282eeb5ff92SLingrui98 XSDebug(cond, p"tag is ${Hexadecimal(tag)}\n------------------------------- \n") 28309c6f1ddSLingrui98 } 28409c6f1ddSLingrui98} 28509c6f1ddSLingrui98 28609c6f1ddSLingrui98class FTBMeta(implicit p: Parameters) extends XSBundle with FTBParams { 287bb09c7feSzoujr val writeWay = UInt(log2Ceil(numWays).W) 28809c6f1ddSLingrui98 val hit = Bool() 2891bc6e9c8SLingrui98 val pred_cycle = if (!env.FPGAPlatform) Some(UInt(64.W)) else None 29009c6f1ddSLingrui98} 29109c6f1ddSLingrui98 29209c6f1ddSLingrui98object FTBMeta { 29309c6f1ddSLingrui98 def apply(writeWay: UInt, hit: Bool, pred_cycle: UInt)(implicit p: Parameters): FTBMeta = { 29409c6f1ddSLingrui98 val e = Wire(new FTBMeta) 29509c6f1ddSLingrui98 e.writeWay := writeWay 29609c6f1ddSLingrui98 e.hit := hit 2971bc6e9c8SLingrui98 e.pred_cycle.map(_ := pred_cycle) 29809c6f1ddSLingrui98 e 29909c6f1ddSLingrui98 } 30009c6f1ddSLingrui98} 30109c6f1ddSLingrui98 302c6bf0bffSzoujr// class UpdateQueueEntry(implicit p: Parameters) extends XSBundle with FTBParams { 303c6bf0bffSzoujr// val pc = UInt(VAddrBits.W) 304c6bf0bffSzoujr// val ftb_entry = new FTBEntry 305c6bf0bffSzoujr// val hit = Bool() 306c6bf0bffSzoujr// val hit_way = UInt(log2Ceil(numWays).W) 307c6bf0bffSzoujr// } 308c6bf0bffSzoujr// 309c6bf0bffSzoujr// object UpdateQueueEntry { 310c6bf0bffSzoujr// def apply(pc: UInt, fe: FTBEntry, hit: Bool, hit_way: UInt)(implicit p: Parameters): UpdateQueueEntry = { 311c6bf0bffSzoujr// val e = Wire(new UpdateQueueEntry) 312c6bf0bffSzoujr// e.pc := pc 313c6bf0bffSzoujr// e.ftb_entry := fe 314c6bf0bffSzoujr// e.hit := hit 315c6bf0bffSzoujr// e.hit_way := hit_way 316c6bf0bffSzoujr// e 317c6bf0bffSzoujr// } 318c6bf0bffSzoujr// } 319c6bf0bffSzoujr 3201ca0e4f3SYinan Xuclass FTB(implicit p: Parameters) extends BasePredictor with FTBParams with BPUUtils 3211ca0e4f3SYinan Xu with HasCircularQueuePtrHelper with HasPerfEvents { 32209c6f1ddSLingrui98 override val meta_size = WireInit(0.U.asTypeOf(new FTBMeta)).getWidth 32309c6f1ddSLingrui98 32409c6f1ddSLingrui98 val ftbAddr = new TableAddr(log2Up(numSets), 1) 32509c6f1ddSLingrui98 32609c6f1ddSLingrui98 class FTBBank(val numSets: Int, val nWays: Int) extends XSModule with BPUUtils { 32709c6f1ddSLingrui98 val io = IO(new Bundle { 3285371700eSzoujr val s1_fire = Input(Bool()) 32909c6f1ddSLingrui98 33009c6f1ddSLingrui98 // when ftb hit, read_hits.valid is true, and read_hits.bits is OH of hit way 33109c6f1ddSLingrui98 // when ftb not hit, read_hits.valid is false, and read_hits is OH of allocWay 332bb09c7feSzoujr // val read_hits = Valid(Vec(numWays, Bool())) 3331c8d9e26Szoujr val req_pc = Flipped(DecoupledIO(UInt(VAddrBits.W))) 3341c8d9e26Szoujr val read_resp = Output(new FTBEntry) 335bb09c7feSzoujr val read_hits = Valid(UInt(log2Ceil(numWays).W)) 33609c6f1ddSLingrui98 3371c8d9e26Szoujr val u_req_pc = Flipped(DecoupledIO(UInt(VAddrBits.W))) 3381c8d9e26Szoujr val update_hits = Valid(UInt(log2Ceil(numWays).W)) 3391c8d9e26Szoujr val update_access = Input(Bool()) 34009c6f1ddSLingrui98 34109c6f1ddSLingrui98 val update_pc = Input(UInt(VAddrBits.W)) 34209c6f1ddSLingrui98 val update_write_data = Flipped(Valid(new FTBEntryWithTag)) 343c6bf0bffSzoujr val update_write_way = Input(UInt(log2Ceil(numWays).W)) 344c6bf0bffSzoujr val update_write_alloc = Input(Bool()) 34509c6f1ddSLingrui98 }) 34609c6f1ddSLingrui98 34736638515SEaston Man // Extract holdRead logic to fix bug that update read override predict read result 34836638515SEaston Man val ftb = Module(new SRAMTemplate(new FTBEntryWithTag, set = numSets, way = numWays, shouldReset = true, holdRead = false, singlePort = true)) 34936638515SEaston Man val ftb_r_entries = ftb.io.r.resp.data.map(_.entry) 35009c6f1ddSLingrui98 35136638515SEaston Man val pred_rdata = HoldUnless(ftb.io.r.resp.data, RegNext(io.req_pc.valid && !io.update_access)) 35236638515SEaston Man ftb.io.r.req.valid := io.req_pc.valid || io.u_req_pc.valid // io.s0_fire 35336638515SEaston Man ftb.io.r.req.bits.setIdx := Mux(io.u_req_pc.valid, ftbAddr.getIdx(io.u_req_pc.bits), ftbAddr.getIdx(io.req_pc.bits)) // s0_idx 3541c8d9e26Szoujr 3551c8d9e26Szoujr assert(!(io.req_pc.valid && io.u_req_pc.valid)) 35609c6f1ddSLingrui98 35736638515SEaston Man io.req_pc.ready := ftb.io.r.req.ready 35836638515SEaston Man io.u_req_pc.ready := ftb.io.r.req.ready 35909c6f1ddSLingrui98 36009c6f1ddSLingrui98 val req_tag = RegEnable(ftbAddr.getTag(io.req_pc.bits)(tagSize-1, 0), io.req_pc.valid) 361ac3f6f25Szoujr val req_idx = RegEnable(ftbAddr.getIdx(io.req_pc.bits), io.req_pc.valid) 36209c6f1ddSLingrui98 3631c8d9e26Szoujr val u_req_tag = RegEnable(ftbAddr.getTag(io.u_req_pc.bits)(tagSize-1, 0), io.u_req_pc.valid) 36409c6f1ddSLingrui98 3651c8d9e26Szoujr val read_entries = pred_rdata.map(_.entry) 3661c8d9e26Szoujr val read_tags = pred_rdata.map(_.tag) 3671c8d9e26Szoujr 3681c8d9e26Szoujr val total_hits = VecInit((0 until numWays).map(b => read_tags(b) === req_tag && read_entries(b).valid && io.s1_fire)) 36909c6f1ddSLingrui98 val hit = total_hits.reduce(_||_) 370bb09c7feSzoujr // val hit_way_1h = VecInit(PriorityEncoderOH(total_hits)) 371ab890bfeSLingrui98 val hit_way = OHToUInt(total_hits) 37209c6f1ddSLingrui98 3731c8d9e26Szoujr val u_total_hits = VecInit((0 until numWays).map(b => 37436638515SEaston Man ftb.io.r.resp.data(b).tag === u_req_tag && ftb.io.r.resp.data(b).entry.valid && RegNext(io.update_access))) 3751c8d9e26Szoujr val u_hit = u_total_hits.reduce(_||_) 3761c8d9e26Szoujr // val hit_way_1h = VecInit(PriorityEncoderOH(total_hits)) 377ab890bfeSLingrui98 val u_hit_way = OHToUInt(u_total_hits) 3781c8d9e26Szoujr 379ccd953deSSteve Gou // assert(PopCount(total_hits) === 1.U || PopCount(total_hits) === 0.U) 380ccd953deSSteve Gou // assert(PopCount(u_total_hits) === 1.U || PopCount(u_total_hits) === 0.U) 381ccd953deSSteve Gou for (n <- 1 to numWays) { 382ccd953deSSteve Gou XSPerfAccumulate(f"ftb_pred_${n}_way_hit", PopCount(total_hits) === n.U) 383ccd953deSSteve Gou XSPerfAccumulate(f"ftb_update_${n}_way_hit", PopCount(u_total_hits) === n.U) 384ccd953deSSteve Gou } 38509c6f1ddSLingrui98 386ac3f6f25Szoujr val replacer = ReplacementPolicy.fromString(Some("setplru"), numWays, numSets) 387c6bf0bffSzoujr // val allocWriteWay = replacer.way(req_idx) 38809c6f1ddSLingrui98 389ac3f6f25Szoujr val touch_set = Seq.fill(1)(Wire(UInt(log2Ceil(numSets).W))) 390ac3f6f25Szoujr val touch_way = Seq.fill(1)(Wire(Valid(UInt(log2Ceil(numWays).W)))) 391ac3f6f25Szoujr 392a788562dSSteve Gou val write_set = Wire(UInt(log2Ceil(numSets).W)) 393a788562dSSteve Gou val write_way = Wire(Valid(UInt(log2Ceil(numWays).W))) 394ac3f6f25Szoujr 395a788562dSSteve Gou val read_set = Wire(UInt(log2Ceil(numSets).W)) 396a788562dSSteve Gou val read_way = Wire(Valid(UInt(log2Ceil(numWays).W))) 397a788562dSSteve Gou 398a788562dSSteve Gou read_set := req_idx 399a788562dSSteve Gou read_way.valid := hit 400a788562dSSteve Gou read_way.bits := hit_way 401a788562dSSteve Gou 40221bd6001SEaston Man // Read replacer access is postponed for 1 cycle 40321bd6001SEaston Man // this helps timing 40421bd6001SEaston Man touch_set(0) := Mux(write_way.valid, write_set, RegNext(read_set)) 40521bd6001SEaston Man touch_way(0).valid := write_way.valid || RegNext(read_way.valid) 40621bd6001SEaston Man touch_way(0).bits := Mux(write_way.valid, write_way.bits, RegNext(read_way.bits)) 407ac3f6f25Szoujr 408c6bf0bffSzoujr replacer.access(touch_set, touch_way) 409c6bf0bffSzoujr 41021bd6001SEaston Man // Select the update allocate way 41121bd6001SEaston Man // Selection logic: 41221bd6001SEaston Man // 1. if any entries within the same index is not valid, select it 41321bd6001SEaston Man // 2. if all entries is valid, use replacer 41402f21c16SLingrui98 def allocWay(valids: UInt, idx: UInt): UInt = { 41509c6f1ddSLingrui98 if (numWays > 1) { 41609c6f1ddSLingrui98 val w = Wire(UInt(log2Up(numWays).W)) 41709c6f1ddSLingrui98 val valid = WireInit(valids.andR) 4185371700eSzoujr w := Mux(valid, replacer.way(idx), PriorityEncoder(~valids)) 41909c6f1ddSLingrui98 w 42009c6f1ddSLingrui98 } else { 42102f21c16SLingrui98 val w = WireInit(0.U(log2Up(numWays).W)) 42209c6f1ddSLingrui98 w 42309c6f1ddSLingrui98 } 42409c6f1ddSLingrui98 } 42509c6f1ddSLingrui98 426ab890bfeSLingrui98 io.read_resp := Mux1H(total_hits, read_entries) // Mux1H 42709c6f1ddSLingrui98 io.read_hits.valid := hit 4285371700eSzoujr io.read_hits.bits := hit_way 42909c6f1ddSLingrui98 4301c8d9e26Szoujr io.update_hits.valid := u_hit 4311c8d9e26Szoujr io.update_hits.bits := u_hit_way 4321c8d9e26Szoujr 43309c6f1ddSLingrui98 // Update logic 43409c6f1ddSLingrui98 val u_valid = io.update_write_data.valid 43509c6f1ddSLingrui98 val u_data = io.update_write_data.bits 43609c6f1ddSLingrui98 val u_idx = ftbAddr.getIdx(io.update_pc) 43702f21c16SLingrui98 val allocWriteWay = allocWay(RegNext(VecInit(ftb_r_entries.map(_.valid))).asUInt, u_idx) 43802f21c16SLingrui98 val u_way = Mux(io.update_write_alloc, allocWriteWay, io.update_write_way) 43902f21c16SLingrui98 val u_mask = UIntToOH(u_way) 440c6bf0bffSzoujr 441c6bf0bffSzoujr for (i <- 0 until numWays) { 44202f21c16SLingrui98 XSPerfAccumulate(f"ftb_replace_way$i", u_valid && io.update_write_alloc && u_way === i.U) 44302f21c16SLingrui98 XSPerfAccumulate(f"ftb_replace_way${i}_has_empty", u_valid && io.update_write_alloc && !ftb_r_entries.map(_.valid).reduce(_&&_) && u_way === i.U) 4445371700eSzoujr XSPerfAccumulate(f"ftb_hit_way$i", hit && !io.update_access && hit_way === i.U) 445c6bf0bffSzoujr } 44609c6f1ddSLingrui98 44736638515SEaston Man ftb.io.w.apply(u_valid, u_data, u_idx, u_mask) 448eeb5ff92SLingrui98 449a788562dSSteve Gou // for replacer 450f4e1af07SLingrui98 write_set := u_idx 451f4e1af07SLingrui98 write_way.valid := u_valid 452f4e1af07SLingrui98 write_way.bits := Mux(io.update_write_alloc, allocWriteWay, io.update_write_way) 453a788562dSSteve Gou 454eeb5ff92SLingrui98 // print hit entry info 45536638515SEaston Man Mux1H(total_hits, ftb.io.r.resp.data).display(true.B) 45609c6f1ddSLingrui98 } // FTBBank 45709c6f1ddSLingrui98 45809c6f1ddSLingrui98 val ftbBank = Module(new FTBBank(numSets, numWays)) 45909c6f1ddSLingrui98 460adc0b8dfSGuokai Chen ftbBank.io.req_pc.valid := io.s0_fire(0) 461adc0b8dfSGuokai Chen ftbBank.io.req_pc.bits := s0_pc_dup(0) 46209c6f1ddSLingrui98 463adc0b8dfSGuokai Chen val btb_enable_dup = dup(RegNext(io.ctrl.btb_enable)) 464adc0b8dfSGuokai Chen val s2_ftb_entry_dup = io.s1_fire.map(f => RegEnable(ftbBank.io.read_resp, f)) 465adc0b8dfSGuokai Chen val s3_ftb_entry_dup = io.s2_fire.zip(s2_ftb_entry_dup).map {case (f, e) => RegEnable(e, f)} 466adc0b8dfSGuokai Chen 4676ee06c7aSSteve Gou val s1_hit = ftbBank.io.read_hits.valid && io.ctrl.btb_enable 468c89b4642SGuokai Chen val s2_hit_dup = io.s1_fire.map(f => RegEnable(s1_hit, 0.B, f)) 469c89b4642SGuokai Chen val s3_hit_dup = io.s2_fire.zip(s2_hit_dup).map {case (f, h) => RegEnable(h, 0.B, f)} 47009c6f1ddSLingrui98 val writeWay = ftbBank.io.read_hits.bits 47109c6f1ddSLingrui98 47209c6f1ddSLingrui98 // io.out.bits.resp := RegEnable(io.in.bits.resp_in(0), 0.U.asTypeOf(new BranchPredictionResp), io.s1_fire) 473c2d1ec7dSLingrui98 io.out := io.in.bits.resp_in(0) 47409c6f1ddSLingrui98 475adc0b8dfSGuokai Chen io.out.s2.full_pred.zip(s2_hit_dup).map {case (fp, h) => fp.hit := h} 476adc0b8dfSGuokai Chen io.out.s2.pc := s2_pc_dup 477adc0b8dfSGuokai Chen for (full_pred & s2_ftb_entry & s2_pc & s1_pc & s1_fire <- 478adc0b8dfSGuokai Chen io.out.s2.full_pred zip s2_ftb_entry_dup zip s2_pc_dup zip s1_pc_dup zip io.s1_fire) { 47947c003a9SEaston Man full_pred.fromFtbEntry(s2_ftb_entry, 48047c003a9SEaston Man s2_pc, 48147c003a9SEaston Man // Previous stage meta for better timing 48247c003a9SEaston Man Some(s1_pc, s1_fire), 48347c003a9SEaston Man Some(ftbBank.io.read_resp, s1_fire) 48447c003a9SEaston Man ) 485adc0b8dfSGuokai Chen } 48609c6f1ddSLingrui98 487adc0b8dfSGuokai Chen io.out.s3.full_pred.zip(s3_hit_dup).map {case (fp, h) => fp.hit := h} 488adc0b8dfSGuokai Chen io.out.s3.pc := s3_pc_dup 489adc0b8dfSGuokai Chen for (full_pred & s3_ftb_entry & s3_pc & s2_pc & s2_fire <- 490adc0b8dfSGuokai Chen io.out.s3.full_pred zip s3_ftb_entry_dup zip s3_pc_dup zip s2_pc_dup zip io.s2_fire) 491adc0b8dfSGuokai Chen full_pred.fromFtbEntry(s3_ftb_entry, s3_pc, Some((s2_pc, s2_fire))) 492cb4f77ceSLingrui98 493adc0b8dfSGuokai Chen io.out.last_stage_ftb_entry := s3_ftb_entry_dup(0) 494935edac4STang Haojin io.out.last_stage_meta := RegEnable(RegEnable(FTBMeta(writeWay.asUInt, s1_hit, GTimer()).asUInt, io.s1_fire(0)), io.s2_fire(0)) 49509c6f1ddSLingrui98 49609c6f1ddSLingrui98 // always taken logic 49709c6f1ddSLingrui98 for (i <- 0 until numBr) { 498adc0b8dfSGuokai Chen for (out_fp & in_fp & s2_hit & s2_ftb_entry <- 499adc0b8dfSGuokai Chen io.out.s2.full_pred zip io.in.bits.resp_in(0).s2.full_pred zip s2_hit_dup zip s2_ftb_entry_dup) 500adc0b8dfSGuokai Chen out_fp.br_taken_mask(i) := in_fp.br_taken_mask(i) || s2_hit && s2_ftb_entry.always_taken(i) 501adc0b8dfSGuokai Chen for (out_fp & in_fp & s3_hit & s3_ftb_entry <- 502adc0b8dfSGuokai Chen io.out.s3.full_pred zip io.in.bits.resp_in(0).s3.full_pred zip s3_hit_dup zip s3_ftb_entry_dup) 503adc0b8dfSGuokai Chen out_fp.br_taken_mask(i) := in_fp.br_taken_mask(i) || s3_hit && s3_ftb_entry.always_taken(i) 50409c6f1ddSLingrui98 } 50509c6f1ddSLingrui98 50609c6f1ddSLingrui98 // Update logic 50702f21c16SLingrui98 val update = io.update.bits 508c6bf0bffSzoujr 50909c6f1ddSLingrui98 val u_meta = update.meta.asTypeOf(new FTBMeta) 51002f21c16SLingrui98 val u_valid = io.update.valid && !io.update.bits.old_entry 511bb09c7feSzoujr 5127af6acb0SEaston Man val (_, delay2_pc) = DelayNWithValid(update.pc, u_valid, 2) 5137af6acb0SEaston Man val (_, delay2_entry) = DelayNWithValid(update.ftb_entry, u_valid, 2) 514bb09c7feSzoujr 51502f21c16SLingrui98 516c6bf0bffSzoujr val update_now = u_valid && u_meta.hit 51702f21c16SLingrui98 val update_need_read = u_valid && !u_meta.hit 51802f21c16SLingrui98 // stall one more cycle because we use a whole cycle to do update read tag hit 51902f21c16SLingrui98 io.s1_ready := ftbBank.io.req_pc.ready && !(update_need_read) && !RegNext(update_need_read) 520c6bf0bffSzoujr 52102f21c16SLingrui98 ftbBank.io.u_req_pc.valid := update_need_read 5221c8d9e26Szoujr ftbBank.io.u_req_pc.bits := update.pc 523bb09c7feSzoujr 524bb09c7feSzoujr 52509c6f1ddSLingrui98 52609c6f1ddSLingrui98 val ftb_write = Wire(new FTBEntryWithTag) 52702f21c16SLingrui98 ftb_write.entry := Mux(update_now, update.ftb_entry, delay2_entry) 52802f21c16SLingrui98 ftb_write.tag := ftbAddr.getTag(Mux(update_now, update.pc, delay2_pc))(tagSize-1, 0) 52909c6f1ddSLingrui98 53002f21c16SLingrui98 val write_valid = update_now || DelayN(u_valid && !u_meta.hit, 2) 531c6bf0bffSzoujr 532c6bf0bffSzoujr ftbBank.io.update_write_data.valid := write_valid 53309c6f1ddSLingrui98 ftbBank.io.update_write_data.bits := ftb_write 53402f21c16SLingrui98 ftbBank.io.update_pc := Mux(update_now, update.pc, delay2_pc) 53502f21c16SLingrui98 ftbBank.io.update_write_way := Mux(update_now, u_meta.writeWay, RegNext(ftbBank.io.update_hits.bits)) // use it one cycle later 53602f21c16SLingrui98 ftbBank.io.update_write_alloc := Mux(update_now, false.B, RegNext(!ftbBank.io.update_hits.valid)) // use it one cycle later 5371c8d9e26Szoujr ftbBank.io.update_access := u_valid && !u_meta.hit 538adc0b8dfSGuokai Chen ftbBank.io.s1_fire := io.s1_fire(0) 53909c6f1ddSLingrui98 540adc0b8dfSGuokai Chen XSDebug("req_v=%b, req_pc=%x, ready=%b (resp at next cycle)\n", io.s0_fire(0), s0_pc_dup(0), ftbBank.io.req_pc.ready) 541adc0b8dfSGuokai Chen XSDebug("s2_hit=%b, hit_way=%b\n", s2_hit_dup(0), writeWay.asUInt) 542eeb5ff92SLingrui98 XSDebug("s2_br_taken_mask=%b, s2_real_taken_mask=%b\n", 543adc0b8dfSGuokai Chen io.in.bits.resp_in(0).s2.full_pred(0).br_taken_mask.asUInt, io.out.s2.full_pred(0).real_slot_taken_mask().asUInt) 544adc0b8dfSGuokai Chen XSDebug("s2_target=%x\n", io.out.s2.getTarget(0)) 54509c6f1ddSLingrui98 546adc0b8dfSGuokai Chen s2_ftb_entry_dup(0).display(true.B) 54709c6f1ddSLingrui98 548adc0b8dfSGuokai Chen XSPerfAccumulate("ftb_read_hits", RegNext(io.s0_fire(0)) && s1_hit) 549adc0b8dfSGuokai Chen XSPerfAccumulate("ftb_read_misses", RegNext(io.s0_fire(0)) && !s1_hit) 55009c6f1ddSLingrui98 55102f21c16SLingrui98 XSPerfAccumulate("ftb_commit_hits", io.update.valid && u_meta.hit) 55202f21c16SLingrui98 XSPerfAccumulate("ftb_commit_misses", io.update.valid && !u_meta.hit) 55309c6f1ddSLingrui98 55409c6f1ddSLingrui98 XSPerfAccumulate("ftb_update_req", io.update.valid) 55509c6f1ddSLingrui98 XSPerfAccumulate("ftb_update_ignored", io.update.valid && io.update.bits.old_entry) 55609c6f1ddSLingrui98 XSPerfAccumulate("ftb_updated", u_valid) 557cd365d4cSrvcoresjw 5584813e060SLingrui98 override val perfEvents = Seq( 559adc0b8dfSGuokai Chen ("ftb_commit_hits ", io.update.valid && u_meta.hit), 560adc0b8dfSGuokai Chen ("ftb_commit_misses ", io.update.valid && !u_meta.hit), 561cd365d4cSrvcoresjw ) 5621ca0e4f3SYinan Xu generatePerfEvent() 56309c6f1ddSLingrui98} 564