xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision cf7d6b7a1a781c73aeb87de112de2e7fe5ea3b7c)
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
1809c6f1ddSLingrui98import chisel3._
1909c6f1ddSLingrui98import chisel3.util._
20*cf7d6b7aSMuziimport freechips.rocketchip.diplomacy.LazyModule
21*cf7d6b7aSMuziimport freechips.rocketchip.diplomacy.LazyModuleImp
22*cf7d6b7aSMuziimport org.chipsalliance.cde.config.Parameters
233c02ee8fSwakafaimport utility._
24*cf7d6b7aSMuziimport utils._
2509c6f1ddSLingrui98import xiangshan._
26*cf7d6b7aSMuziimport xiangshan.backend.fu.PFEvent
27*cf7d6b7aSMuziimport xiangshan.backend.fu.PMP
28*cf7d6b7aSMuziimport xiangshan.backend.fu.PMPChecker
29*cf7d6b7aSMuziimport xiangshan.backend.fu.PMPReqBundle
30ee175d78SJayimport xiangshan.cache.mmu._
311d8f4dcbSJayimport xiangshan.frontend.icache._
3209c6f1ddSLingrui98
3309c6f1ddSLingrui98class Frontend()(implicit p: Parameters) extends LazyModule with HasXSParameter {
3495e60e55STang Haojin  override def shouldBeInlined: Boolean = false
35233f2ad0Szhanglinjuan  val inner       = LazyModule(new FrontendInlined)
36233f2ad0Szhanglinjuan  lazy val module = new FrontendImp(this)
37233f2ad0Szhanglinjuan}
38233f2ad0Szhanglinjuan
39233f2ad0Szhanglinjuanclass FrontendImp(wrapper: Frontend)(implicit p: Parameters) extends LazyModuleImp(wrapper) {
40233f2ad0Szhanglinjuan  val io      = IO(wrapper.inner.module.io.cloneType)
41233f2ad0Szhanglinjuan  val io_perf = IO(wrapper.inner.module.io_perf.cloneType)
42233f2ad0Szhanglinjuan  io <> wrapper.inner.module.io
43233f2ad0Szhanglinjuan  io_perf <> wrapper.inner.module.io_perf
44233f2ad0Szhanglinjuan  if (p(DebugOptionsKey).ResetGen) {
45233f2ad0Szhanglinjuan    ResetGen(ResetGenNode(Seq(ModuleNode(wrapper.inner.module))), reset, sim = false)
46233f2ad0Szhanglinjuan  }
47233f2ad0Szhanglinjuan}
48233f2ad0Szhanglinjuan
49233f2ad0Szhanglinjuanclass FrontendInlined()(implicit p: Parameters) extends LazyModule with HasXSParameter {
50233f2ad0Szhanglinjuan  override def shouldBeInlined: Boolean = true
5109c6f1ddSLingrui98
5209c6f1ddSLingrui98  val instrUncache = LazyModule(new InstrUncache())
5309c6f1ddSLingrui98  val icache       = LazyModule(new ICache())
5409c6f1ddSLingrui98
55233f2ad0Szhanglinjuan  lazy val module = new FrontendInlinedImp(this)
5609c6f1ddSLingrui98}
5709c6f1ddSLingrui98
58233f2ad0Szhanglinjuanclass FrontendInlinedImp(outer: FrontendInlined) extends LazyModuleImp(outer)
5909c6f1ddSLingrui98    with HasXSParameter
60*cf7d6b7aSMuzi    with HasPerfEvents {
6109c6f1ddSLingrui98  val io = IO(new Bundle() {
62f57f7f2aSYangyu Chen    val hartId       = Input(UInt(hartIdLen.W))
63c4b44470SGuokai Chen    val reset_vector = Input(UInt(PAddrBits.W))
6409c6f1ddSLingrui98    val fencei       = Input(Bool())
651a718038SHaoyuan Feng    val ptw          = new TlbPtwIO()
6609c6f1ddSLingrui98    val backend      = new FrontendToCtrlIO
672c9f4a9fSxu_zh    val softPrefetch = Vec(backendParams.LduCnt, Flipped(Valid(new SoftIfetchPrefetchBundle)))
6809c6f1ddSLingrui98    val sfence       = Input(new SfenceBundle)
6909c6f1ddSLingrui98    val tlbCsr       = Input(new TlbCsrBundle)
7009c6f1ddSLingrui98    val csrCtrl      = Input(new CustomCSRCtrlIO)
710184a80eSYanqin Li    val error        = ValidIO(new L1CacheErrorInfo)
7209c6f1ddSLingrui98    val frontendInfo = new Bundle {
7309c6f1ddSLingrui98      val ibufFull = Output(Bool())
7409c6f1ddSLingrui98      val bpuInfo = new Bundle {
7509c6f1ddSLingrui98        val bpRight = Output(UInt(XLEN.W))
7609c6f1ddSLingrui98        val bpWrong = Output(UInt(XLEN.W))
7709c6f1ddSLingrui98      }
7809c6f1ddSLingrui98    }
79233f2ad0Szhanglinjuan    val resetInFrontend = Output(Bool())
8060ebee38STang Haojin    val debugTopDown = new Bundle {
8160ebee38STang Haojin      val robHeadVaddr = Flipped(Valid(UInt(VAddrBits.W)))
8260ebee38STang Haojin    }
8309c6f1ddSLingrui98  })
8409c6f1ddSLingrui98
8509c6f1ddSLingrui98  // decouped-frontend modules
861d8f4dcbSJay  val instrUncache = outer.instrUncache.module
871d8f4dcbSJay  val icache       = outer.icache.module
8809c6f1ddSLingrui98  val bpu          = Module(new Predictor)
8909c6f1ddSLingrui98  val ifu          = Module(new NewIFU)
9044c9c1deSEaston Man  val ibuffer      = Module(new IBuffer)
9109c6f1ddSLingrui98  val ftq          = Module(new Ftq)
9209c6f1ddSLingrui98
93f1fe8698SLemover  val needFlush            = RegNext(io.backend.toFtq.redirect.valid)
94d2b20d1aSTang Haojin  val FlushControlRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsCtrl)
95d2b20d1aSTang Haojin  val FlushMemVioRedirect  = RegNext(io.backend.toFtq.redirect.bits.debugIsMemVio)
96d2b20d1aSTang Haojin  val FlushControlBTBMiss  = Wire(Bool())
97d2b20d1aSTang Haojin  val FlushTAGEMiss        = Wire(Bool())
98d2b20d1aSTang Haojin  val FlushSCMiss          = Wire(Bool())
99d2b20d1aSTang Haojin  val FlushITTAGEMiss      = Wire(Bool())
100d2b20d1aSTang Haojin  val FlushRASMiss         = Wire(Bool())
101f1fe8698SLemover
1026f688dacSYinan Xu  val tlbCsr  = DelayN(io.tlbCsr, 2)
1036f688dacSYinan Xu  val csrCtrl = DelayN(io.csrCtrl, 2)
104fa9f9690SLemover  val sfence  = RegNext(RegNext(io.sfence))
10572951335SLi Qianruo
10672951335SLi Qianruo  // trigger
1076f688dacSYinan Xu  ifu.io.frontendTrigger := csrCtrl.frontend_trigger
10872951335SLi Qianruo
1096ee06c7aSSteve Gou  // bpu ctrl
1106ee06c7aSSteve Gou  bpu.io.ctrl         := csrCtrl.bp_ctrl
1115f119905STang Haojin  bpu.io.reset_vector := io.reset_vector
1126ee06c7aSSteve Gou
113b6982e83SLemover// pmp
114b92f8445Sssszwic  val PortNumber = ICacheParameters().PortNumber
115b6982e83SLemover  val pmp        = Module(new PMP())
11634f9624dSguohongyu  val pmp_check  = VecInit(Seq.fill(coreParams.ipmpPortNum)(Module(new PMPChecker(3, sameCycle = true)).io))
1176f688dacSYinan Xu  pmp.io.distribute_csr := csrCtrl.distribute_csr
11834f9624dSguohongyu  val pmp_req_vec = Wire(Vec(coreParams.ipmpPortNum, Valid(new PMPReqBundle())))
119b92f8445Sssszwic  (0 until 2 * PortNumber).foreach(i => pmp_req_vec(i) <> icache.io.pmp(i).req)
1200c26d810Sguohongyu  pmp_req_vec.last <> ifu.io.pmp.req
121ee175d78SJay
122b6982e83SLemover  for (i <- pmp_check.indices) {
123ee175d78SJay    pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i))
124b6982e83SLemover  }
125b92f8445Sssszwic  (0 until 2 * PortNumber).foreach(i => icache.io.pmp(i).resp <> pmp_check(i).resp)
1260c26d810Sguohongyu  ifu.io.pmp.resp <> pmp_check.last.resp
127ee175d78SJay
128*cf7d6b7aSMuzi  val itlb =
129*cf7d6b7aSMuzi    Module(new TLB(coreParams.itlbPortNum, nRespDups = 1, Seq.fill(PortNumber)(false) ++ Seq(true), itlbParams))
130b92f8445Sssszwic  itlb.io.requestor.take(PortNumber) zip icache.io.itlb foreach { case (a, b) => a <> b }
1310c26d810Sguohongyu  itlb.io.requestor.last <> ifu.io.iTLBInter // mmio may need re-tlb, blocked
132254e4960SHaoyuan Feng  itlb.io.hartId := io.hartId
1331a718038SHaoyuan Feng  itlb.io.base_connect(sfence, tlbCsr)
134f1fe8698SLemover  itlb.io.flushPipe.map(_ := needFlush)
135a4f9c77fSpeixiaokun  itlb.io.redirect := DontCare // itlb has flushpipe, don't need redirect signal
13609c6f1ddSLingrui98
1371a718038SHaoyuan Feng  val itlb_ptw = Wire(new VectorTlbPtwIO(coreParams.itlbPortNum))
1381a718038SHaoyuan Feng  itlb_ptw.connect(itlb.io.ptw)
1391a718038SHaoyuan Feng  val itlbRepeater1 = PTWFilter(itlbParams.fenceDelay, itlb_ptw, sfence, tlbCsr, l2tlbParams.ifilterSize)
140*cf7d6b7aSMuzi  val itlbRepeater2 =
141*cf7d6b7aSMuzi    PTWRepeaterNB(passReady = false, itlbParams.fenceDelay, itlbRepeater1.io.ptw, io.ptw, sfence, tlbCsr)
1421a718038SHaoyuan Feng
1432c9f4a9fSxu_zh  icache.io.ftqPrefetch <> ftq.io.toPrefetch
1442c9f4a9fSxu_zh  icache.io.softPrefetch <> io.softPrefetch
14509c6f1ddSLingrui98
14609c6f1ddSLingrui98  // IFU-Ftq
14709c6f1ddSLingrui98  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
148c5c5edaeSJenius  ftq.io.toIfu.req.ready := ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
149c5c5edaeSJenius
15009c6f1ddSLingrui98  ftq.io.fromIfu <> ifu.io.ftqInter.toFtq
15109c6f1ddSLingrui98  bpu.io.ftq_to_bpu <> ftq.io.toBpu
15209c6f1ddSLingrui98  ftq.io.fromBpu <> bpu.io.bpu_to_ftq
1531d1e6d4dSJenius
1541d1e6d4dSJenius  ftq.io.mmioCommitRead <> ifu.io.mmioCommitRead
15509c6f1ddSLingrui98  // IFU-ICache
156c5c5edaeSJenius
157c5c5edaeSJenius  icache.io.fetch.req <> ftq.io.toICache.req
158c5c5edaeSJenius  ftq.io.toICache.req.ready := ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
159c5c5edaeSJenius
160c5c5edaeSJenius  ifu.io.icacheInter.resp <> icache.io.fetch.resp
16150780602SJenius  ifu.io.icacheInter.icacheReady       := icache.io.toIFU
162d2b20d1aSTang Haojin  ifu.io.icacheInter.topdownIcacheMiss := icache.io.fetch.topdownIcacheMiss
163d2b20d1aSTang Haojin  ifu.io.icacheInter.topdownItlbMiss   := icache.io.fetch.topdownItlbMiss
1641d8f4dcbSJay  icache.io.stop                       := ifu.io.icacheStop
165b92f8445Sssszwic  icache.io.flush                      := ftq.io.icacheFlush
16609c6f1ddSLingrui98
1671d8f4dcbSJay  ifu.io.icachePerfInfo := icache.io.perfInfo
1681d8f4dcbSJay
169ecccf78fSJay  icache.io.csr_pf_enable     := RegNext(csrCtrl.l1I_pf_enable)
170ecccf78fSJay  icache.io.csr_parity_enable := RegNext(csrCtrl.icache_parity_enable)
171ecccf78fSJay
1723dbaa960SEaston Man  icache.io.fencei := RegNext(io.fencei)
1732a6078bfSguohongyu
17409c6f1ddSLingrui98  // IFU-Ibuffer
17509c6f1ddSLingrui98  ifu.io.toIbuffer <> ibuffer.io.in
17609c6f1ddSLingrui98
17709c6f1ddSLingrui98  ftq.io.fromBackend <> io.backend.toFtq
17892c61038SXuan Hu  io.backend.fromFtq := ftq.io.toBackend
17992c61038SXuan Hu  io.backend.fromIfu := ifu.io.toBackend
18009c6f1ddSLingrui98  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
18109c6f1ddSLingrui98
1825359309bSGuokai Chen  val checkPcMem = Reg(Vec(FtqSize, new Ftq_RF_Components))
1835359309bSGuokai Chen  when(ftq.io.toBackend.pc_mem_wen) {
184f533cba7SHuSipeng    checkPcMem(ftq.io.toBackend.pc_mem_waddr) := ftq.io.toBackend.pc_mem_wdata
1855359309bSGuokai Chen  }
1865359309bSGuokai Chen
1875359309bSGuokai Chen  val checkTargetIdx = Wire(Vec(DecodeWidth, UInt(log2Up(FtqSize).W)))
1885359309bSGuokai Chen  val checkTarget    = Wire(Vec(DecodeWidth, UInt(VAddrBits.W)))
1895359309bSGuokai Chen
1905359309bSGuokai Chen  for (i <- 0 until DecodeWidth) {
1915359309bSGuokai Chen    checkTargetIdx(i) := ibuffer.io.out(i).bits.ftqPtr.value
192*cf7d6b7aSMuzi    checkTarget(i) := Mux(
193*cf7d6b7aSMuzi      ftq.io.toBackend.newest_entry_ptr.value === checkTargetIdx(i),
1945359309bSGuokai Chen      ftq.io.toBackend.newest_entry_target,
195*cf7d6b7aSMuzi      checkPcMem(checkTargetIdx(i) + 1.U).startAddr
196*cf7d6b7aSMuzi    )
1975359309bSGuokai Chen  }
1985359309bSGuokai Chen
1995359309bSGuokai Chen  // commented out for this br could be the last instruction in the fetch block
2005359309bSGuokai Chen  def checkNotTakenConsecutive = {
2015359309bSGuokai Chen    val prevNotTakenValid  = RegInit(0.B)
2025359309bSGuokai Chen    val prevNotTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W))
2035359309bSGuokai Chen    for (i <- 0 until DecodeWidth - 1) {
2045359309bSGuokai Chen      // for instrs that is not the last, if a not-taken br, the next instr should have the same ftqPtr
2055359309bSGuokai Chen      // for instrs that is the last, record and check next request
2065359309bSGuokai Chen      when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr) {
2075359309bSGuokai Chen        when(ibuffer.io.out(i + 1).fire) {
2085359309bSGuokai Chen          // not last br, check now
2095359309bSGuokai Chen          XSError(checkTargetIdx(i) =/= checkTargetIdx(i + 1), "not-taken br should have same ftqPtr\n")
2105359309bSGuokai Chen        }.otherwise {
2115359309bSGuokai Chen          // last br, record its info
2125359309bSGuokai Chen          prevNotTakenValid  := true.B
2135359309bSGuokai Chen          prevNotTakenFtqIdx := checkTargetIdx(i)
2145359309bSGuokai Chen        }
2155359309bSGuokai Chen      }
2165359309bSGuokai Chen    }
2175359309bSGuokai Chen    when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr) {
2185359309bSGuokai Chen      // last instr is a br, record its info
2195359309bSGuokai Chen      prevNotTakenValid  := true.B
2205359309bSGuokai Chen      prevNotTakenFtqIdx := checkTargetIdx(DecodeWidth - 1)
2215359309bSGuokai Chen    }
2225359309bSGuokai Chen    when(prevNotTakenValid && ibuffer.io.out(0).fire) {
2235359309bSGuokai Chen      XSError(prevNotTakenFtqIdx =/= checkTargetIdx(0), "not-taken br should have same ftqPtr\n")
2245359309bSGuokai Chen      prevNotTakenValid := false.B
2255359309bSGuokai Chen    }
2265359309bSGuokai Chen    when(needFlush) {
2275359309bSGuokai Chen      prevNotTakenValid := false.B
2285359309bSGuokai Chen    }
2295359309bSGuokai Chen  }
2305359309bSGuokai Chen
2315359309bSGuokai Chen  def checkTakenNotConsecutive = {
2325359309bSGuokai Chen    val prevTakenValid  = RegInit(0.B)
2335359309bSGuokai Chen    val prevTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W))
2345359309bSGuokai Chen    for (i <- 0 until DecodeWidth - 1) {
2355359309bSGuokai Chen      // for instrs that is not the last, if a taken br, the next instr should not have the same ftqPtr
2365359309bSGuokai Chen      // for instrs that is the last, record and check next request
2375359309bSGuokai Chen      when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && ibuffer.io.out(i).bits.pred_taken) {
2385359309bSGuokai Chen        when(ibuffer.io.out(i + 1).fire) {
2395359309bSGuokai Chen          // not last br, check now
2405359309bSGuokai Chen          XSError(checkTargetIdx(i) + 1.U =/= checkTargetIdx(i + 1), "taken br should have consecutive ftqPtr\n")
2415359309bSGuokai Chen        }.otherwise {
2425359309bSGuokai Chen          // last br, record its info
2435359309bSGuokai Chen          prevTakenValid  := true.B
2445359309bSGuokai Chen          prevTakenFtqIdx := checkTargetIdx(i)
2455359309bSGuokai Chen        }
2465359309bSGuokai Chen      }
2475359309bSGuokai Chen    }
248*cf7d6b7aSMuzi    when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && ibuffer.io.out(
249*cf7d6b7aSMuzi      DecodeWidth - 1
250*cf7d6b7aSMuzi    ).bits.pred_taken) {
2515359309bSGuokai Chen      // last instr is a br, record its info
2525359309bSGuokai Chen      prevTakenValid  := true.B
2535359309bSGuokai Chen      prevTakenFtqIdx := checkTargetIdx(DecodeWidth - 1)
2545359309bSGuokai Chen    }
2555359309bSGuokai Chen    when(prevTakenValid && ibuffer.io.out(0).fire) {
2565359309bSGuokai Chen      XSError(prevTakenFtqIdx + 1.U =/= checkTargetIdx(0), "taken br should have consecutive ftqPtr\n")
2575359309bSGuokai Chen      prevTakenValid := false.B
2585359309bSGuokai Chen    }
2595359309bSGuokai Chen    when(needFlush) {
2605359309bSGuokai Chen      prevTakenValid := false.B
2615359309bSGuokai Chen    }
2625359309bSGuokai Chen  }
2635359309bSGuokai Chen
2645359309bSGuokai Chen  def checkNotTakenPC = {
2655359309bSGuokai Chen    val prevNotTakenPC    = Reg(UInt(VAddrBits.W))
2665359309bSGuokai Chen    val prevIsRVC         = Reg(Bool())
2675359309bSGuokai Chen    val prevNotTakenValid = RegInit(0.B)
2685359309bSGuokai Chen
2695359309bSGuokai Chen    for (i <- 0 until DecodeWidth - 1) {
2705359309bSGuokai Chen      when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && !ibuffer.io.out(i).bits.pred_taken) {
2715359309bSGuokai Chen        when(ibuffer.io.out(i + 1).fire) {
272*cf7d6b7aSMuzi          XSError(
273*cf7d6b7aSMuzi            ibuffer.io.out(i).bits.pc + Mux(ibuffer.io.out(i).bits.pd.isRVC, 2.U, 4.U) =/= ibuffer.io.out(
274*cf7d6b7aSMuzi              i + 1
275*cf7d6b7aSMuzi            ).bits.pc,
276*cf7d6b7aSMuzi            "not-taken br should have consecutive pc\n"
277*cf7d6b7aSMuzi          )
2785359309bSGuokai Chen        }.otherwise {
2795359309bSGuokai Chen          prevNotTakenValid := true.B
2805359309bSGuokai Chen          prevIsRVC         := ibuffer.io.out(i).bits.pd.isRVC
2815359309bSGuokai Chen          prevNotTakenPC    := ibuffer.io.out(i).bits.pc
2825359309bSGuokai Chen        }
2835359309bSGuokai Chen      }
2845359309bSGuokai Chen    }
285*cf7d6b7aSMuzi    when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && !ibuffer.io.out(
286*cf7d6b7aSMuzi      DecodeWidth - 1
287*cf7d6b7aSMuzi    ).bits.pred_taken) {
2885359309bSGuokai Chen      prevNotTakenValid := true.B
2895359309bSGuokai Chen      prevIsRVC         := ibuffer.io.out(DecodeWidth - 1).bits.pd.isRVC
2905359309bSGuokai Chen      prevNotTakenPC    := ibuffer.io.out(DecodeWidth - 1).bits.pc
2915359309bSGuokai Chen    }
2925359309bSGuokai Chen    when(prevNotTakenValid && ibuffer.io.out(0).fire) {
293*cf7d6b7aSMuzi      XSError(
294*cf7d6b7aSMuzi        prevNotTakenPC + Mux(prevIsRVC, 2.U, 4.U) =/= ibuffer.io.out(0).bits.pc,
295*cf7d6b7aSMuzi        "not-taken br should have same pc\n"
296*cf7d6b7aSMuzi      )
2975359309bSGuokai Chen      prevNotTakenValid := false.B
2985359309bSGuokai Chen    }
2995359309bSGuokai Chen    when(needFlush) {
3005359309bSGuokai Chen      prevNotTakenValid := false.B
3015359309bSGuokai Chen    }
3025359309bSGuokai Chen  }
3035359309bSGuokai Chen
3045359309bSGuokai Chen  def checkTakenPC = {
3055359309bSGuokai Chen    val prevTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W))
3065359309bSGuokai Chen    val prevTakenValid  = RegInit(0.B)
3075359309bSGuokai Chen    val prevTakenTarget = Wire(UInt(VAddrBits.W))
3085359309bSGuokai Chen    prevTakenTarget := checkPcMem(prevTakenFtqIdx + 1.U).startAddr
3095359309bSGuokai Chen
3105359309bSGuokai Chen    for (i <- 0 until DecodeWidth - 1) {
3115359309bSGuokai Chen      when(ibuffer.io.out(i).fire && !ibuffer.io.out(i).bits.pd.notCFI && ibuffer.io.out(i).bits.pred_taken) {
3125359309bSGuokai Chen        when(ibuffer.io.out(i + 1).fire) {
3135359309bSGuokai Chen          XSError(checkTarget(i) =/= ibuffer.io.out(i + 1).bits.pc, "taken instr should follow target pc\n")
3145359309bSGuokai Chen        }.otherwise {
3155359309bSGuokai Chen          prevTakenValid  := true.B
3165359309bSGuokai Chen          prevTakenFtqIdx := checkTargetIdx(i)
3175359309bSGuokai Chen        }
3185359309bSGuokai Chen      }
3195359309bSGuokai Chen    }
320*cf7d6b7aSMuzi    when(ibuffer.io.out(DecodeWidth - 1).fire && !ibuffer.io.out(DecodeWidth - 1).bits.pd.notCFI && ibuffer.io.out(
321*cf7d6b7aSMuzi      DecodeWidth - 1
322*cf7d6b7aSMuzi    ).bits.pred_taken) {
3235359309bSGuokai Chen      prevTakenValid  := true.B
3245359309bSGuokai Chen      prevTakenFtqIdx := checkTargetIdx(DecodeWidth - 1)
3255359309bSGuokai Chen    }
3265359309bSGuokai Chen    when(prevTakenValid && ibuffer.io.out(0).fire) {
3275359309bSGuokai Chen      XSError(prevTakenTarget =/= ibuffer.io.out(0).bits.pc, "taken instr should follow target pc\n")
3285359309bSGuokai Chen      prevTakenValid := false.B
3295359309bSGuokai Chen    }
3305359309bSGuokai Chen    when(needFlush) {
3315359309bSGuokai Chen      prevTakenValid := false.B
3325359309bSGuokai Chen    }
3335359309bSGuokai Chen  }
3345359309bSGuokai Chen
3355359309bSGuokai Chen  // checkNotTakenConsecutive
3365359309bSGuokai Chen  checkTakenNotConsecutive
3375359309bSGuokai Chen  checkTakenPC
3385359309bSGuokai Chen  checkNotTakenPC
3395359309bSGuokai Chen
340a37fbf10SJay  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
341a37fbf10SJay
34209c6f1ddSLingrui98  ibuffer.io.flush                := needFlush
343d2b20d1aSTang Haojin  ibuffer.io.ControlRedirect      := FlushControlRedirect
344d2b20d1aSTang Haojin  ibuffer.io.MemVioRedirect       := FlushMemVioRedirect
345d2b20d1aSTang Haojin  ibuffer.io.ControlBTBMissBubble := FlushControlBTBMiss
346d2b20d1aSTang Haojin  ibuffer.io.TAGEMissBubble       := FlushTAGEMiss
347d2b20d1aSTang Haojin  ibuffer.io.SCMissBubble         := FlushSCMiss
348d2b20d1aSTang Haojin  ibuffer.io.ITTAGEMissBubble     := FlushITTAGEMiss
349d2b20d1aSTang Haojin  ibuffer.io.RASMissBubble        := FlushRASMiss
35005cc2a4eSXuan Hu  ibuffer.io.decodeCanAccept      := io.backend.canAccept
351d2b20d1aSTang Haojin
352d2b20d1aSTang Haojin  FlushControlBTBMiss := ftq.io.ControlBTBMissBubble
353d2b20d1aSTang Haojin  FlushTAGEMiss       := ftq.io.TAGEMissBubble
354d2b20d1aSTang Haojin  FlushSCMiss         := ftq.io.SCMissBubble
355d2b20d1aSTang Haojin  FlushITTAGEMiss     := ftq.io.ITTAGEMissBubble
356d2b20d1aSTang Haojin  FlushRASMiss        := ftq.io.RASMissBubble
357d2b20d1aSTang Haojin
35809c6f1ddSLingrui98  io.backend.cfVec <> ibuffer.io.out
359d2b20d1aSTang Haojin  io.backend.stallReason <> ibuffer.io.stallReason
36009c6f1ddSLingrui98
3610be662e4SJay  instrUncache.io.req <> ifu.io.uncacheInter.toUncache
3620be662e4SJay  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
36358dbdfc2SJay  instrUncache.io.flush := false.B
36458dbdfc2SJay  io.error <> RegNext(RegNext(icache.io.error))
36509c6f1ddSLingrui98
36641cb8b61SJenius  icache.io.hartId := io.hartId
36741cb8b61SJenius
36860ebee38STang Haojin  itlbRepeater1.io.debugTopDown.robHeadVaddr := io.debugTopDown.robHeadVaddr
36960ebee38STang Haojin
3709c55e669SEaston Man  val frontendBubble = Mux(io.backend.canAccept, DecodeWidth.U - PopCount(ibuffer.io.out.map(_.valid)), 0.U)
37109c6f1ddSLingrui98  XSPerfAccumulate("FrontendBubble", frontendBubble)
37209c6f1ddSLingrui98  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
373233f2ad0Szhanglinjuan  io.resetInFrontend       := reset.asBool
374cd365d4cSrvcoresjw
3751ca0e4f3SYinan Xu  // PFEvent
3761ca0e4f3SYinan Xu  val pfevent = Module(new PFEvent)
3771ca0e4f3SYinan Xu  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
3781ca0e4f3SYinan Xu  val csrevents = pfevent.io.hpmevent.take(8)
379cd365d4cSrvcoresjw
3809a128342SHaoyuan Feng  val perfFromUnits = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerfEvents)
3819a128342SHaoyuan Feng  val perfFromIO    = Seq()
3829a128342SHaoyuan Feng  val perfBlock     = Seq()
3839a128342SHaoyuan Feng  // let index = 0 be no event
3849a128342SHaoyuan Feng  val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock
3859a128342SHaoyuan Feng
3869a128342SHaoyuan Feng  if (printEventCoding) {
3879a128342SHaoyuan Feng    for (((name, inc), i) <- allPerfEvents.zipWithIndex) {
3889a128342SHaoyuan Feng      println("Frontend perfEvents Set", name, inc, i)
3899a128342SHaoyuan Feng    }
3909a128342SHaoyuan Feng  }
3919a128342SHaoyuan Feng
3929a128342SHaoyuan Feng  val allPerfInc          = allPerfEvents.map(_._2.asTypeOf(new PerfEvent))
3939a128342SHaoyuan Feng  override val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents
3941ca0e4f3SYinan Xu  generatePerfEvent()
39509c6f1ddSLingrui98}
396