FTB.scala 18.5 KB
Newer Older
Z
zoujr 已提交
1 2
/***************************************************************************************
* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
L
Lingrui98 已提交
3
* Copyright (c) 2020-2021 Peng Cheng Laboratory
Z
zoujr 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
*
* XiangShan is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

package xiangshan.frontend

import chipsalliance.rocketchip.config.Parameters
import chisel3._
import chisel3.stage.{ChiselGeneratorAnnotation, ChiselStage}
import chisel3.util._
import xiangshan._
import utils._
import chisel3.experimental.chiselName

import scala.math.min
28
import os.copy
Z
zoujr 已提交
29 30 31


trait FTBParams extends HasXSParameter with HasBPUConst {
32 33
  val numEntries = FtbSize
  val numWays    = FtbWays
Z
zoujr 已提交
34 35
  val numSets    = numEntries/numWays // 512
  val tagSize    = 20
36

37 38
  

39 40 41 42 43
  val TAR_STAT_SZ = 2
  def TAR_FIT = 0.U(TAR_STAT_SZ.W)
  def TAR_OVF = 1.U(TAR_STAT_SZ.W)
  def TAR_UDF = 2.U(TAR_STAT_SZ.W)

44 45
  def BR_OFFSET_LEN = 12
  def JMP_OFFSET_LEN = 20
Z
zoujr 已提交
46 47
}

L
Lingrui98 已提交
48 49 50 51
class FtbSlot(val offsetLen: Int, val subOffsetLen: Option[Int] = None)(implicit p: Parameters) extends XSBundle with FTBParams {
  if (subOffsetLen.isDefined) {
    require(subOffsetLen.get <= offsetLen)
  }
52 53 54 55 56 57 58 59 60 61 62
  val offset  = UInt(log2Ceil(PredictWidth).W)
  val lower   = UInt(offsetLen.W)
  val tarStat = UInt(TAR_STAT_SZ.W)
  val sharing = Bool()
  val valid   = Bool()

  def setLowerStatByTarget(pc: UInt, target: UInt, isShare: Boolean) = {
    def getTargetStatByHigher(pc_higher: UInt, target_higher: UInt) =
      Mux(target_higher > pc_higher, TAR_OVF,
        Mux(target_higher < pc_higher, TAR_UDF, TAR_FIT))
    def getLowerByTarget(target: UInt, offsetLen: Int) = target(offsetLen, 1)
L
Lingrui98 已提交
63
    val offLen = if (isShare) this.subOffsetLen.get else this.offsetLen
64 65 66 67 68 69 70 71 72
    val pc_higher = pc(VAddrBits-1, offLen+1)
    val target_higher = target(VAddrBits-1, offLen+1)
    val stat = getTargetStatByHigher(pc_higher, target_higher)
    val lower = ZeroExt(getLowerByTarget(target, offLen), this.offsetLen)
    this.lower := lower
    this.tarStat := stat
    this.sharing := isShare.B
  }

L
Lingrui98 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  def getTarget(pc: UInt, last_stage: Option[Tuple2[UInt, Bool]] = None) = {
    def getTarget(offLen: Int)(pc: UInt, lower: UInt, stat: UInt,
      last_stage: Option[Tuple2[UInt, Bool]] = None) = {
      val h = pc(VAddrBits-1, offLen+1)
      val higher = Wire(UInt((VAddrBits-offLen-1).W))
      val higher_plus_one = Wire(UInt((VAddrBits-offLen-1).W))
      val higher_minus_one = Wire(UInt((VAddrBits-offLen-1).W))
      if (last_stage.isDefined) {
        val last_stage_pc = last_stage.get._1
        val last_stage_pc_h = last_stage_pc(VAddrBits-1, offLen+1)
        val stage_en = last_stage.get._2
        higher := RegEnable(last_stage_pc_h, stage_en)
        higher_plus_one := RegEnable(last_stage_pc_h+1.U, stage_en)
        higher_minus_one := RegEnable(last_stage_pc_h-1.U, stage_en)
      } else {
        higher := h
        higher_plus_one := h + 1.U
        higher_minus_one := h - 1.U
      }
92 93
      val target =
        Cat(
L
Lingrui98 已提交
94 95 96 97 98
          Mux1H(Seq(
            (stat === TAR_OVF, higher_plus_one),
            (stat === TAR_UDF, higher_minus_one),
            (stat === TAR_FIT, higher),
          )),
99 100 101 102 103 104
          lower(offLen-1, 0), 0.U(1.W)
        )
      require(target.getWidth == VAddrBits)
      require(offLen != 0)
      target
    }
L
Lingrui98 已提交
105
    if (subOffsetLen.isDefined)
106
      Mux(sharing,
L
Lingrui98 已提交
107 108
        getTarget(subOffsetLen.get)(pc, lower, tarStat, last_stage),
        getTarget(offsetLen)(pc, lower, tarStat, last_stage)
109 110
      )
    else
L
Lingrui98 已提交
111
      getTarget(offsetLen)(pc, lower, tarStat, last_stage)
112 113 114
  }
  def fromAnotherSlot(that: FtbSlot) = {
    require(
L
Lingrui98 已提交
115
      this.offsetLen > that.offsetLen && this.subOffsetLen.map(_ == that.offsetLen).getOrElse(true) ||
116 117 118 119
      this.offsetLen == that.offsetLen
    )
    this.offset := that.offset
    this.tarStat := that.tarStat
L
Lingrui98 已提交
120
    this.sharing := (this.offsetLen > that.offsetLen && that.offsetLen == this.subOffsetLen.get).B
121 122 123 124 125 126
    this.valid := that.valid
    this.lower := ZeroExt(that.lower, this.offsetLen)
  }
  
}

L
Lingrui98 已提交
127
class FTBEntry(implicit p: Parameters) extends XSBundle with FTBParams with BPUUtils {
128 129
  
  
Z
zoujr 已提交
130 131
  val valid       = Bool()

132
  val brSlots = Vec(numBrSlot, new FtbSlot(BR_OFFSET_LEN))
Z
zoujr 已提交
133

L
Lingrui98 已提交
134
  val tailSlot = new FtbSlot(JMP_OFFSET_LEN, Some(BR_OFFSET_LEN))
Z
zoujr 已提交
135 136

  // Partial Fall-Through Address
L
Lingrui98 已提交
137
  val pftAddr     = UInt(log2Up(PredictWidth).W)
Z
zoujr 已提交
138 139
  val carry       = Bool()

Z
zoujr 已提交
140 141 142
  val isCall      = Bool()
  val isRet       = Bool()
  val isJalr      = Bool()
Z
zoujr 已提交
143

144
  val last_may_be_rvi_call = Bool()
Z
zoujr 已提交
145

146
  val always_taken = Vec(numBr, Bool())
147

148
  def getSlotForBr(idx: Int): FtbSlot = {
149 150 151
    require(idx <= numBr-1)
    (idx, numBr) match {
      case (i, n) if i == n-1 => this.tailSlot
152 153
      case _ => this.brSlots(idx)
    }
154
  }
155 156
  def allSlotsForBr = {
    (0 until numBr).map(getSlotForBr(_))
157 158
  }
  def setByBrTarget(brIdx: Int, pc: UInt, target: UInt) = {
159
    val slot = getSlotForBr(brIdx)
160
    slot.setLowerStatByTarget(pc, target, brIdx == numBr-1)
161 162
  }
  def setByJmpTarget(pc: UInt, target: UInt) = {
163
    this.tailSlot.setLowerStatByTarget(pc, target, false)
164 165
  }

L
Lingrui98 已提交
166 167
  def getTargetVec(pc: UInt, last_stage: Option[Tuple2[UInt, Bool]] = None) = {
    VecInit((brSlots :+ tailSlot).map(_.getTarget(pc, last_stage)))
168
  }
169

170
  def getOffsetVec = VecInit(brSlots.map(_.offset) :+ tailSlot.offset)
L
Lingrui98 已提交
171
  def isJal = !isJalr
L
Lingrui98 已提交
172
  def getFallThrough(pc: UInt) = getFallThroughAddr(pc, carry, pftAddr)
173 174
  def hasBr(offset: UInt) =
    brSlots.map{ s => s.valid && s.offset <= offset}.reduce(_||_) ||
175
    (tailSlot.valid && tailSlot.offset <= offset && tailSlot.sharing)
176 177

  def getBrMaskByOffset(offset: UInt) =
178 179
    brSlots.map{ s => s.valid && s.offset <= offset } :+
    (tailSlot.valid && tailSlot.offset <= offset && tailSlot.sharing)
180 181 182
    
  def getBrRecordedVec(offset: UInt) = {
    VecInit(
183 184
      brSlots.map(s => s.valid && s.offset === offset) :+
      (tailSlot.valid && tailSlot.offset === offset && tailSlot.sharing)
185 186 187 188
    )
  }
    
  def brIsSaved(offset: UInt) = getBrRecordedVec(offset).reduce(_||_)
Z
zoujr 已提交
189

190 191
  def brValids = {
    VecInit(
192
      brSlots.map(_.valid) :+ (tailSlot.valid && tailSlot.sharing)
193 194 195 196
    )
  }

  def noEmptySlotForNewBr = {
197
    VecInit(brSlots.map(_.valid) :+ tailSlot.valid).reduce(_&&_)
198 199 200
  }

  def newBrCanNotInsert(offset: UInt) = {
201
    val lastSlotForBr = tailSlot
202 203 204 205
    lastSlotForBr.valid && lastSlotForBr.offset < offset
  }

  def jmpValid = {
206
    tailSlot.valid && !tailSlot.sharing
207 208 209
  }

  def brOffset = {
210
    VecInit(brSlots.map(_.offset) :+ tailSlot.offset)
211 212
  }

Z
zoujr 已提交
213 214
  def display(cond: Bool): Unit = {
    XSDebug(cond, p"-----------FTB entry----------- \n")
L
Lingrui98 已提交
215
    XSDebug(cond, p"v=${valid}\n")
Z
zoujr 已提交
216
    for(i <- 0 until numBr) {
217 218
      XSDebug(cond, p"[br$i]: v=${allSlotsForBr(i).valid}, offset=${allSlotsForBr(i).offset}," +
        p"lower=${Hexadecimal(allSlotsForBr(i).lower)}\n")
Z
zoujr 已提交
219
    }
220 221
    XSDebug(cond, p"[tailSlot]: v=${tailSlot.valid}, offset=${tailSlot.offset}," +
      p"lower=${Hexadecimal(tailSlot.lower)}, sharing=${tailSlot.sharing}}\n")
Z
zoujr 已提交
222 223
    XSDebug(cond, p"pftAddr=${Hexadecimal(pftAddr)}, carry=$carry\n")
    XSDebug(cond, p"isCall=$isCall, isRet=$isRet, isjalr=$isJalr\n")
224
    XSDebug(cond, p"last_may_be_rvi_call=$last_may_be_rvi_call\n")
Z
zoujr 已提交
225
    XSDebug(cond, p"------------------------------- \n")
Z
zoujr 已提交
226
  }
L
Lingrui98 已提交
227 228 229 230 231 232 233

}

class FTBEntryWithTag(implicit p: Parameters) extends XSBundle with FTBParams with BPUUtils {
  val entry = new FTBEntry
  val tag = UInt(tagSize.W)
  def display(cond: Bool): Unit = {
234 235
    entry.display(cond)
    XSDebug(cond, p"tag is ${Hexadecimal(tag)}\n------------------------------- \n")
L
Lingrui98 已提交
236
  }
Z
zoujr 已提交
237 238 239
}

class FTBMeta(implicit p: Parameters) extends XSBundle with FTBParams {
Z
zoujr 已提交
240
  val writeWay = UInt(log2Ceil(numWays).W)
Z
zoujr 已提交
241
  val hit = Bool()
242
  val pred_cycle = if (!env.FPGAPlatform) Some(UInt(64.W)) else None
Z
zoujr 已提交
243 244 245
}

object FTBMeta {
Z
zoujr 已提交
246
  def apply(writeWay: UInt, hit: Bool, pred_cycle: UInt)(implicit p: Parameters): FTBMeta = {
Z
zoujr 已提交
247 248 249
    val e = Wire(new FTBMeta)
    e.writeWay := writeWay
    e.hit := hit
250
    e.pred_cycle.map(_ := pred_cycle)
Z
zoujr 已提交
251 252 253 254
    e
  }
}

Z
zoujr 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
// class UpdateQueueEntry(implicit p: Parameters) extends XSBundle with FTBParams {
//   val pc = UInt(VAddrBits.W)
//   val ftb_entry = new FTBEntry
//   val hit = Bool()
//   val hit_way = UInt(log2Ceil(numWays).W)
// }
//
// object UpdateQueueEntry {
//   def apply(pc: UInt, fe: FTBEntry, hit: Bool, hit_way: UInt)(implicit p: Parameters): UpdateQueueEntry = {
//     val e = Wire(new UpdateQueueEntry)
//     e.pc := pc
//     e.ftb_entry := fe
//     e.hit := hit
//     e.hit_way := hit_way
//     e
//   }
// }

273 274
class FTB(implicit p: Parameters) extends BasePredictor with FTBParams with BPUUtils
  with HasCircularQueuePtrHelper with HasPerfEvents {
Z
zoujr 已提交
275 276
  override val meta_size = WireInit(0.U.asTypeOf(new FTBMeta)).getWidth

Z
zoujr 已提交
277
  val ftbAddr = new TableAddr(log2Up(numSets), 1)
Z
zoujr 已提交
278

Z
zoujr 已提交
279 280
  class FTBBank(val numSets: Int, val nWays: Int) extends XSModule with BPUUtils {
    val io = IO(new Bundle {
Z
zoujr 已提交
281
      val s1_fire = Input(Bool())
Z
zoujr 已提交
282 283 284

      // when ftb hit, read_hits.valid is true, and read_hits.bits is OH of hit way
      // when ftb not hit, read_hits.valid is false, and read_hits is OH of allocWay
Z
zoujr 已提交
285
      // val read_hits = Valid(Vec(numWays, Bool()))
286 287
      val req_pc = Flipped(DecoupledIO(UInt(VAddrBits.W)))
      val read_resp = Output(new FTBEntry)
Z
zoujr 已提交
288
      val read_hits = Valid(UInt(log2Ceil(numWays).W))
Z
zoujr 已提交
289

290 291 292
      val u_req_pc = Flipped(DecoupledIO(UInt(VAddrBits.W)))
      val update_hits = Valid(UInt(log2Ceil(numWays).W))
      val update_access = Input(Bool())
Z
zoujr 已提交
293 294

      val update_pc = Input(UInt(VAddrBits.W))
L
Lingrui98 已提交
295
      val update_write_data = Flipped(Valid(new FTBEntryWithTag))
Z
zoujr 已提交
296 297
      val update_write_way = Input(UInt(log2Ceil(numWays).W))
      val update_write_alloc = Input(Bool())
Z
zoujr 已提交
298 299
    })

300 301
    // Extract holdRead logic to fix bug that update read override predict read result
    val ftb = Module(new SRAMTemplate(new FTBEntryWithTag, set = numSets, way = numWays, shouldReset = true, holdRead = false, singlePort = true))
302
    val ftb_r_entries = ftb.io.r.resp.data.map(_.entry)
303 304 305 306

    val pred_rdata   = HoldUnless(ftb.io.r.resp.data, RegNext(io.req_pc.valid && !io.update_access))
    ftb.io.r.req.valid := io.req_pc.valid || io.u_req_pc.valid // io.s0_fire
    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
Z
zoujr 已提交
307

308
    assert(!(io.req_pc.valid && io.u_req_pc.valid))
Z
zoujr 已提交
309

L
Lingrui98 已提交
310
    io.req_pc.ready := ftb.io.r.req.ready
311
    io.u_req_pc.ready := ftb.io.r.req.ready
Z
zoujr 已提交
312

L
Lingrui98 已提交
313
    val req_tag = RegEnable(ftbAddr.getTag(io.req_pc.bits)(tagSize-1, 0), io.req_pc.valid)
314
    val req_idx = RegEnable(ftbAddr.getIdx(io.req_pc.bits), io.req_pc.valid)
Z
zoujr 已提交
315

316
    val u_req_tag = RegEnable(ftbAddr.getTag(io.u_req_pc.bits)(tagSize-1, 0), io.u_req_pc.valid)
Z
zoujr 已提交
317

318 319
    val read_entries = pred_rdata.map(_.entry)
    val read_tags    = pred_rdata.map(_.tag)
Z
zoujr 已提交
320

321
    val total_hits = VecInit((0 until numWays).map(b => read_tags(b) === req_tag && read_entries(b).valid && io.s1_fire))
Z
zoujr 已提交
322
    val hit = total_hits.reduce(_||_)
Z
zoujr 已提交
323
    // val hit_way_1h = VecInit(PriorityEncoderOH(total_hits))
L
Lingrui98 已提交
324
    val hit_way = OHToUInt(total_hits)
Z
zoujr 已提交
325

326 327 328 329
    val u_total_hits = VecInit((0 until numWays).map(b =>
        ftb.io.r.resp.data(b).tag === u_req_tag && ftb.io.r.resp.data(b).entry.valid && RegNext(io.update_access)))
    val u_hit = u_total_hits.reduce(_||_)
    // val hit_way_1h = VecInit(PriorityEncoderOH(total_hits))
L
Lingrui98 已提交
330
    val u_hit_way = OHToUInt(u_total_hits)
Z
zoujr 已提交
331

332 333 334 335 336 337
    // assert(PopCount(total_hits) === 1.U || PopCount(total_hits) === 0.U)
    // assert(PopCount(u_total_hits) === 1.U || PopCount(u_total_hits) === 0.U)
    for (n <- 1 to numWays) {
      XSPerfAccumulate(f"ftb_pred_${n}_way_hit", PopCount(total_hits) === n.U)
      XSPerfAccumulate(f"ftb_update_${n}_way_hit", PopCount(u_total_hits) === n.U)
    }
Z
zoujr 已提交
338

339
    val replacer = ReplacementPolicy.fromString(Some("setplru"), numWays, numSets)
Z
zoujr 已提交
340
    // val allocWriteWay = replacer.way(req_idx)
341 342 343

    val touch_set = Seq.fill(1)(Wire(UInt(log2Ceil(numSets).W)))
    val touch_way = Seq.fill(1)(Wire(Valid(UInt(log2Ceil(numWays).W))))
Z
zoujr 已提交
344

345 346 347 348 349 350 351 352 353 354 355
    val write_set = Wire(UInt(log2Ceil(numSets).W))
    val write_way = Wire(Valid(UInt(log2Ceil(numWays).W)))

    val read_set = Wire(UInt(log2Ceil(numSets).W))
    val read_way = Wire(Valid(UInt(log2Ceil(numWays).W)))

    read_set := req_idx
    read_way.valid := hit
    read_way.bits  := hit_way

    touch_set(0) := Mux(write_way.valid, write_set, read_set)
Z
zoujr 已提交
356

357 358
    touch_way(0).valid := write_way.valid || read_way.valid
    touch_way(0).bits := Mux(write_way.valid, write_way.bits, read_way.bits)
359

Z
zoujr 已提交
360 361
    replacer.access(touch_set, touch_way)

L
Lingrui98 已提交
362
    def allocWay(valids: UInt, idx: UInt): UInt = {
Z
zoujr 已提交
363 364 365
      if (numWays > 1) {
        val w = Wire(UInt(log2Up(numWays).W))
        val valid = WireInit(valids.andR)
Z
zoujr 已提交
366
        w := Mux(valid, replacer.way(idx), PriorityEncoder(~valids))
Z
zoujr 已提交
367
        w
L
Lingrui98 已提交
368 369
      } else {
        val w = WireInit(0.U(log2Up(numWays).W))
Z
zoujr 已提交
370 371 372
        w
      }
    }
Z
zoujr 已提交
373

L
Lingrui98 已提交
374
    io.read_resp := Mux1H(total_hits, read_entries) // Mux1H
Z
zoujr 已提交
375
    io.read_hits.valid := hit
Z
zoujr 已提交
376
    io.read_hits.bits := hit_way
Z
zoujr 已提交
377

378 379 380
    io.update_hits.valid := u_hit
    io.update_hits.bits := u_hit_way

Z
zoujr 已提交
381 382 383 384
    // Update logic
    val u_valid = io.update_write_data.valid
    val u_data = io.update_write_data.bits
    val u_idx = ftbAddr.getIdx(io.update_pc)
L
Lingrui98 已提交
385 386 387
    val allocWriteWay = allocWay(RegNext(VecInit(ftb_r_entries.map(_.valid))).asUInt, u_idx)
    val u_way = Mux(io.update_write_alloc, allocWriteWay, io.update_write_way)
    val u_mask = UIntToOH(u_way)
Z
zoujr 已提交
388 389

    for (i <- 0 until numWays) {
L
Lingrui98 已提交
390 391
      XSPerfAccumulate(f"ftb_replace_way$i", u_valid && io.update_write_alloc && u_way === i.U)
      XSPerfAccumulate(f"ftb_replace_way${i}_has_empty", u_valid && io.update_write_alloc && !ftb_r_entries.map(_.valid).reduce(_&&_) && u_way === i.U)
Z
zoujr 已提交
392
      XSPerfAccumulate(f"ftb_hit_way$i", hit && !io.update_access && hit_way === i.U)
Z
zoujr 已提交
393
    }
Z
zoujr 已提交
394

Z
zoujr 已提交
395
    ftb.io.w.apply(u_valid, u_data, u_idx, u_mask)
396

397
    // for replacer
L
Lingrui98 已提交
398 399 400
    write_set := u_idx
    write_way.valid := u_valid
    write_way.bits := Mux(io.update_write_alloc, allocWriteWay, io.update_write_way)
401

402
    // print hit entry info
L
Lingrui98 已提交
403
    Mux1H(total_hits, ftb.io.r.resp.data).display(true.B)
Z
zoujr 已提交
404
  } // FTBBank
Z
zoujr 已提交
405

Z
zoujr 已提交
406
  val ftbBank = Module(new FTBBank(numSets, numWays))
Z
zoujr 已提交
407

L
Lingrui98 已提交
408 409
  ftbBank.io.req_pc.valid := io.s0_fire
  ftbBank.io.req_pc.bits := s0_pc
Z
zoujr 已提交
410

411
  val ftb_entry = RegEnable(ftbBank.io.read_resp, io.s1_fire)
L
Lingrui98 已提交
412
  val s3_ftb_entry = RegEnable(ftb_entry, io.s2_fire)
413
  val s1_hit = ftbBank.io.read_hits.valid && io.ctrl.btb_enable
Z
zoujr 已提交
414
  val s2_hit = RegEnable(s1_hit, io.s1_fire)
L
Lingrui98 已提交
415
  val s3_hit = RegEnable(s2_hit, io.s2_fire)
Z
zoujr 已提交
416
  val writeWay = ftbBank.io.read_hits.bits
Z
zoujr 已提交
417

418
  val fallThruAddr = getFallThroughAddr(s2_pc, ftb_entry.carry, ftb_entry.pftAddr)
419

420
  // io.out.bits.resp := RegEnable(io.in.bits.resp_in(0), 0.U.asTypeOf(new BranchPredictionResp), io.s1_fire)
Z
zoujr 已提交
421
  io.out.resp := io.in.bits.resp_in(0)
Z
zoujr 已提交
422

Z
zoujr 已提交
423 424
  val s1_latch_call_is_rvc   = DontCare // TODO: modify when add RAS

L
Lingrui98 已提交
425
  io.out.resp.s2.full_pred.hit       := s2_hit
426 427
  io.out.resp.s2.pc                  := s2_pc
  io.out.resp.s2.ftb_entry           := ftb_entry
428 429
  io.out.resp.s2.full_pred.fromFtbEntry(ftb_entry, s2_pc, Some((s1_pc, io.s1_fire)))
  io.out.resp.s2.is_minimal := false.B
430

L
Lingrui98 已提交
431 432 433 434 435 436 437
  io.out.resp.s3.full_pred.hit := s3_hit
  io.out.resp.s3.pc                  := s3_pc
  io.out.resp.s3.ftb_entry           := s3_ftb_entry
  io.out.resp.s3.full_pred.fromFtbEntry(s3_ftb_entry, s3_pc, Some((s2_pc, io.s2_fire)))
  io.out.resp.s3.is_minimal := false.B

  io.out.last_stage_meta := RegEnable(RegEnable(FTBMeta(writeWay.asUInt(), s1_hit, GTimer()).asUInt(), io.s1_fire), io.s2_fire)
Z
zoujr 已提交
438

L
Lingrui98 已提交
439
  // always taken logic
L
Lingrui98 已提交
440
  for (i <- 0 until numBr) {
441
    io.out.resp.s2.full_pred.br_taken_mask(i) := io.in.bits.resp_in(0).s2.full_pred.br_taken_mask(i) || s2_hit && ftb_entry.always_taken(i)
L
Lingrui98 已提交
442
    io.out.resp.s3.full_pred.br_taken_mask(i) := io.in.bits.resp_in(0).s3.full_pred.br_taken_mask(i) || s3_hit && s3_ftb_entry.always_taken(i)
L
Lingrui98 已提交
443 444
  }

Z
zoujr 已提交
445
  // Update logic
L
Lingrui98 已提交
446
  val update = io.update.bits
Z
zoujr 已提交
447

Z
zoujr 已提交
448
  val u_meta = update.meta.asTypeOf(new FTBMeta)
L
Lingrui98 已提交
449
  val u_valid = io.update.valid && !io.update.bits.old_entry
Z
zoujr 已提交
450

L
Lingrui98 已提交
451 452
  val delay2_pc = DelayN(update.pc, 2)
  val delay2_entry = DelayN(update.ftb_entry, 2)
Z
zoujr 已提交
453

L
Lingrui98 已提交
454
  
Z
zoujr 已提交
455
  val update_now = u_valid && u_meta.hit
L
Lingrui98 已提交
456 457 458
  val update_need_read = u_valid && !u_meta.hit
  // stall one more cycle because we use a whole cycle to do update read tag hit
  io.s1_ready := ftbBank.io.req_pc.ready && !(update_need_read) && !RegNext(update_need_read)
Z
zoujr 已提交
459

L
Lingrui98 已提交
460
  ftbBank.io.u_req_pc.valid := update_need_read
461
  ftbBank.io.u_req_pc.bits := update.pc
Z
zoujr 已提交
462 463


Z
zoujr 已提交
464

L
Lingrui98 已提交
465
  val ftb_write = Wire(new FTBEntryWithTag)
L
Lingrui98 已提交
466 467
  ftb_write.entry := Mux(update_now, update.ftb_entry, delay2_entry)
  ftb_write.tag   := ftbAddr.getTag(Mux(update_now, update.pc, delay2_pc))(tagSize-1, 0)
Z
zoujr 已提交
468

L
Lingrui98 已提交
469
  val write_valid = update_now || DelayN(u_valid && !u_meta.hit, 2)
Z
zoujr 已提交
470 471 472

  ftbBank.io.update_write_data.valid := write_valid
  ftbBank.io.update_write_data.bits := ftb_write
L
Lingrui98 已提交
473 474 475
  ftbBank.io.update_pc          := Mux(update_now, update.pc,       delay2_pc)
  ftbBank.io.update_write_way   := Mux(update_now, u_meta.writeWay, RegNext(ftbBank.io.update_hits.bits)) // use it one cycle later
  ftbBank.io.update_write_alloc := Mux(update_now, false.B,         RegNext(!ftbBank.io.update_hits.valid)) // use it one cycle later
476
  ftbBank.io.update_access := u_valid && !u_meta.hit
Z
zoujr 已提交
477
  ftbBank.io.s1_fire := io.s1_fire
Z
zoujr 已提交
478

L
Lingrui98 已提交
479 480
  XSDebug("req_v=%b, req_pc=%x, ready=%b (resp at next cycle)\n", io.s0_fire, s0_pc, ftbBank.io.req_pc.ready)
  XSDebug("s2_hit=%b, hit_way=%b\n", s2_hit, writeWay.asUInt)
481
  XSDebug("s2_br_taken_mask=%b, s2_real_taken_mask=%b\n",
482 483
    io.in.bits.resp_in(0).s2.full_pred.br_taken_mask.asUInt, io.out.resp.s2.full_pred.real_slot_taken_mask().asUInt)
  XSDebug("s2_target=%x\n", io.out.resp.s2.getTarget)
Z
zoujr 已提交
484

L
Lingrui98 已提交
485
  ftb_entry.display(true.B)
Z
zoujr 已提交
486

L
Lingrui98 已提交
487 488
  XSPerfAccumulate("ftb_read_hits", RegNext(io.s0_fire) && s1_hit)
  XSPerfAccumulate("ftb_read_misses", RegNext(io.s0_fire) && !s1_hit)
Z
zoujr 已提交
489

L
Lingrui98 已提交
490 491
  XSPerfAccumulate("ftb_commit_hits", io.update.valid && u_meta.hit)
  XSPerfAccumulate("ftb_commit_misses", io.update.valid && !u_meta.hit)
492

L
Lingrui98 已提交
493 494 495
  XSPerfAccumulate("ftb_update_req", io.update.valid)
  XSPerfAccumulate("ftb_update_ignored", io.update.valid && io.update.bits.old_entry)
  XSPerfAccumulate("ftb_updated", u_valid)
496

497
  override val perfEvents = Seq(
L
Lingrui98 已提交
498 499
    ("ftb_commit_hits            ", RegNext(io.update.valid)  &&  u_meta.hit),
    ("ftb_commit_misses          ", RegNext(io.update.valid)  && !u_meta.hit),
500
  )
501
  generatePerfEvent()
Z
zoujr 已提交
502
}