xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision 4d53e0efbe5daf676b4a4bfc064ec7f80daa0232)
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.
15c49ebec8SHaoyuan Feng*
16c49ebec8SHaoyuan Feng*
17c49ebec8SHaoyuan Feng* Acknowledgement
18c49ebec8SHaoyuan Feng*
19c49ebec8SHaoyuan Feng* This implementation is inspired by several key papers:
20c49ebec8SHaoyuan Feng* [1] Alex Ramirez, Oliverio J. Santana, Josep L. Larriba-Pey, and Mateo Valero. "[Fetching instruction streams.]
21c49ebec8SHaoyuan Feng* (https://doi.org/10.1109/MICRO.2002.1176264)" 35th Annual IEEE/ACM International Symposium on Microarchitecture
22c49ebec8SHaoyuan Feng* (MICRO). 2002.
23c49ebec8SHaoyuan Feng* [2] Yasuo Ishii, Jaekyu Lee, Krishnendra Nathella, and Dam Sunwoo. "[Rebasing instruction prefetching: An industry
24c49ebec8SHaoyuan Feng* perspective.](https://doi.org/10.1109/LCA.2020.3035068)" IEEE Computer Architecture Letters 19.2: 147-150. 2020.
25c49ebec8SHaoyuan Feng* [3] Yasuo Ishii, Jaekyu Lee, Krishnendra Nathella, and Dam Sunwoo. "[Re-establishing fetch-directed instruction
26c49ebec8SHaoyuan Feng* prefetching: An industry perspective.](https://doi.org/10.1109/ISPASS51385.2021.00034)" 2021 IEEE International
27c49ebec8SHaoyuan Feng* Symposium on Performance Analysis of Systems and Software (ISPASS). 2021.
2809c6f1ddSLingrui98***************************************************************************************/
2909c6f1ddSLingrui98
3009c6f1ddSLingrui98package xiangshan.frontend
3109c6f1ddSLingrui98import chisel3._
3209c6f1ddSLingrui98import chisel3.util._
33cf7d6b7aSMuziimport freechips.rocketchip.diplomacy.LazyModule
34cf7d6b7aSMuziimport freechips.rocketchip.diplomacy.LazyModuleImp
35cf7d6b7aSMuziimport org.chipsalliance.cde.config.Parameters
363c02ee8fSwakafaimport utility._
3709c6f1ddSLingrui98import xiangshan._
38cf7d6b7aSMuziimport xiangshan.backend.fu.PFEvent
39cf7d6b7aSMuziimport xiangshan.backend.fu.PMP
40cf7d6b7aSMuziimport xiangshan.backend.fu.PMPChecker
41cf7d6b7aSMuziimport xiangshan.backend.fu.PMPReqBundle
42ee175d78SJayimport xiangshan.cache.mmu._
431d8f4dcbSJayimport xiangshan.frontend.icache._
4409c6f1ddSLingrui98
4509c6f1ddSLingrui98class Frontend()(implicit p: Parameters) extends LazyModule with HasXSParameter {
4695e60e55STang Haojin  override def shouldBeInlined: Boolean = false
47233f2ad0Szhanglinjuan  val inner       = LazyModule(new FrontendInlined)
48233f2ad0Szhanglinjuan  lazy val module = new FrontendImp(this)
49233f2ad0Szhanglinjuan}
50233f2ad0Szhanglinjuan
51233f2ad0Szhanglinjuanclass FrontendImp(wrapper: Frontend)(implicit p: Parameters) extends LazyModuleImp(wrapper) {
52233f2ad0Szhanglinjuan  val io      = IO(wrapper.inner.module.io.cloneType)
53233f2ad0Szhanglinjuan  val io_perf = IO(wrapper.inner.module.io_perf.cloneType)
54233f2ad0Szhanglinjuan  io <> wrapper.inner.module.io
55233f2ad0Szhanglinjuan  io_perf <> wrapper.inner.module.io_perf
56233f2ad0Szhanglinjuan  if (p(DebugOptionsKey).ResetGen) {
57233f2ad0Szhanglinjuan    ResetGen(ResetGenNode(Seq(ModuleNode(wrapper.inner.module))), reset, sim = false)
58233f2ad0Szhanglinjuan  }
59233f2ad0Szhanglinjuan}
60233f2ad0Szhanglinjuan
61233f2ad0Szhanglinjuanclass FrontendInlined()(implicit p: Parameters) extends LazyModule with HasXSParameter {
62233f2ad0Szhanglinjuan  override def shouldBeInlined: Boolean = true
6309c6f1ddSLingrui98
6409c6f1ddSLingrui98  val instrUncache = LazyModule(new InstrUncache())
6509c6f1ddSLingrui98  val icache       = LazyModule(new ICache())
6609c6f1ddSLingrui98
67233f2ad0Szhanglinjuan  lazy val module = new FrontendInlinedImp(this)
6809c6f1ddSLingrui98}
6909c6f1ddSLingrui98
70233f2ad0Szhanglinjuanclass FrontendInlinedImp(outer: FrontendInlined) extends LazyModuleImp(outer)
7109c6f1ddSLingrui98    with HasXSParameter
72cf7d6b7aSMuzi    with HasPerfEvents {
7309c6f1ddSLingrui98  val io = IO(new Bundle() {
74f57f7f2aSYangyu Chen    val hartId       = Input(UInt(hartIdLen.W))
75c4b44470SGuokai Chen    val reset_vector = Input(UInt(PAddrBits.W))
7609c6f1ddSLingrui98    val fencei       = Input(Bool())
771a718038SHaoyuan Feng    val ptw          = new TlbPtwIO()
7809c6f1ddSLingrui98    val backend      = new FrontendToCtrlIO
792c9f4a9fSxu_zh    val softPrefetch = Vec(backendParams.LduCnt, Flipped(Valid(new SoftIfetchPrefetchBundle)))
8009c6f1ddSLingrui98    val sfence       = Input(new SfenceBundle)
8109c6f1ddSLingrui98    val tlbCsr       = Input(new TlbCsrBundle)
8209c6f1ddSLingrui98    val csrCtrl      = Input(new CustomCSRCtrlIO)
830184a80eSYanqin Li    val error        = ValidIO(new L1CacheErrorInfo)
8409c6f1ddSLingrui98    val frontendInfo = new Bundle {
8509c6f1ddSLingrui98      val ibufFull = Output(Bool())
8609c6f1ddSLingrui98      val bpuInfo = new Bundle {
8709c6f1ddSLingrui98        val bpRight = Output(UInt(XLEN.W))
8809c6f1ddSLingrui98        val bpWrong = Output(UInt(XLEN.W))
8909c6f1ddSLingrui98      }
9009c6f1ddSLingrui98    }
91233f2ad0Szhanglinjuan    val resetInFrontend = Output(Bool())
9260ebee38STang Haojin    val debugTopDown = new Bundle {
9360ebee38STang Haojin      val robHeadVaddr = Flipped(Valid(UInt(VAddrBits.W)))
9460ebee38STang Haojin    }
9509c6f1ddSLingrui98  })
9609c6f1ddSLingrui98
9709c6f1ddSLingrui98  // decouped-frontend modules
981d8f4dcbSJay  val instrUncache = outer.instrUncache.module
991d8f4dcbSJay  val icache       = outer.icache.module
10009c6f1ddSLingrui98  val bpu          = Module(new Predictor)
10109c6f1ddSLingrui98  val ifu          = Module(new NewIFU)
10244c9c1deSEaston Man  val ibuffer      = Module(new IBuffer)
10309c6f1ddSLingrui98  val ftq          = Module(new Ftq)
10409c6f1ddSLingrui98
105f1fe8698SLemover  val needFlush            = RegNext(io.backend.toFtq.redirect.valid)
106d2b20d1aSTang Haojin  val FlushControlRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsCtrl)
107d2b20d1aSTang Haojin  val FlushMemVioRedirect  = RegNext(io.backend.toFtq.redirect.bits.debugIsMemVio)
108d2b20d1aSTang Haojin  val FlushControlBTBMiss  = Wire(Bool())
109d2b20d1aSTang Haojin  val FlushTAGEMiss        = Wire(Bool())
110d2b20d1aSTang Haojin  val FlushSCMiss          = Wire(Bool())
111d2b20d1aSTang Haojin  val FlushITTAGEMiss      = Wire(Bool())
112d2b20d1aSTang Haojin  val FlushRASMiss         = Wire(Bool())
113f1fe8698SLemover
1146f688dacSYinan Xu  val tlbCsr  = DelayN(io.tlbCsr, 2)
1156f688dacSYinan Xu  val csrCtrl = DelayN(io.csrCtrl, 2)
116fa9f9690SLemover  val sfence  = RegNext(RegNext(io.sfence))
11772951335SLi Qianruo
11872951335SLi Qianruo  // trigger
1196f688dacSYinan Xu  ifu.io.frontendTrigger := csrCtrl.frontend_trigger
12072951335SLi Qianruo
12171b6c42eSxu_zh  // RVCDecoder fsIsOff
12271b6c42eSxu_zh  ifu.io.csr_fsIsOff := csrCtrl.fsIsOff
12371b6c42eSxu_zh
1246ee06c7aSSteve Gou  // bpu ctrl
1256ee06c7aSSteve Gou  bpu.io.ctrl         := csrCtrl.bp_ctrl
1265f119905STang Haojin  bpu.io.reset_vector := io.reset_vector
1276ee06c7aSSteve Gou
128b6982e83SLemover  // pmp
129b92f8445Sssszwic  val PortNumber = ICacheParameters().PortNumber
130b6982e83SLemover  val pmp        = Module(new PMP())
13134f9624dSguohongyu  val pmp_check  = VecInit(Seq.fill(coreParams.ipmpPortNum)(Module(new PMPChecker(3, sameCycle = true)).io))
1326f688dacSYinan Xu  pmp.io.distribute_csr := csrCtrl.distribute_csr
13334f9624dSguohongyu  val pmp_req_vec = Wire(Vec(coreParams.ipmpPortNum, Valid(new PMPReqBundle())))
134b92f8445Sssszwic  (0 until 2 * PortNumber).foreach(i => pmp_req_vec(i) <> icache.io.pmp(i).req)
1350c26d810Sguohongyu  pmp_req_vec.last <> ifu.io.pmp.req
136ee175d78SJay
137b6982e83SLemover  for (i <- pmp_check.indices) {
138ee175d78SJay    pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i))
139b6982e83SLemover  }
140b92f8445Sssszwic  (0 until 2 * PortNumber).foreach(i => icache.io.pmp(i).resp <> pmp_check(i).resp)
1410c26d810Sguohongyu  ifu.io.pmp.resp <> pmp_check.last.resp
142ee175d78SJay
143cf7d6b7aSMuzi  val itlb =
144cf7d6b7aSMuzi    Module(new TLB(coreParams.itlbPortNum, nRespDups = 1, Seq.fill(PortNumber)(false) ++ Seq(true), itlbParams))
145b92f8445Sssszwic  itlb.io.requestor.take(PortNumber) zip icache.io.itlb foreach { case (a, b) => a <> b }
1460c26d810Sguohongyu  itlb.io.requestor.last <> ifu.io.iTLBInter // mmio may need re-tlb, blocked
147254e4960SHaoyuan Feng  itlb.io.hartId := io.hartId
1481a718038SHaoyuan Feng  itlb.io.base_connect(sfence, tlbCsr)
149fad7803dSxu_zh  itlb.io.flushPipe.foreach(_ := icache.io.itlbFlushPipe)
150a4f9c77fSpeixiaokun  itlb.io.redirect := DontCare // itlb has flushpipe, don't need redirect signal
15109c6f1ddSLingrui98
1521a718038SHaoyuan Feng  val itlb_ptw = Wire(new VectorTlbPtwIO(coreParams.itlbPortNum))
1531a718038SHaoyuan Feng  itlb_ptw.connect(itlb.io.ptw)
1541a718038SHaoyuan Feng  val itlbRepeater1 = PTWFilter(itlbParams.fenceDelay, itlb_ptw, sfence, tlbCsr, l2tlbParams.ifilterSize)
155cf7d6b7aSMuzi  val itlbRepeater2 =
156cf7d6b7aSMuzi    PTWRepeaterNB(passReady = false, itlbParams.fenceDelay, itlbRepeater1.io.ptw, io.ptw, sfence, tlbCsr)
1571a718038SHaoyuan Feng
1582c9f4a9fSxu_zh  icache.io.ftqPrefetch <> ftq.io.toPrefetch
1592c9f4a9fSxu_zh  icache.io.softPrefetch <> io.softPrefetch
16009c6f1ddSLingrui98
16109c6f1ddSLingrui98  // IFU-Ftq
16209c6f1ddSLingrui98  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
163c5c5edaeSJenius  ftq.io.toIfu.req.ready := ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
164c5c5edaeSJenius
16509c6f1ddSLingrui98  ftq.io.fromIfu <> ifu.io.ftqInter.toFtq
16609c6f1ddSLingrui98  bpu.io.ftq_to_bpu <> ftq.io.toBpu
16709c6f1ddSLingrui98  ftq.io.fromBpu <> bpu.io.bpu_to_ftq
1681d1e6d4dSJenius
1691d1e6d4dSJenius  ftq.io.mmioCommitRead <> ifu.io.mmioCommitRead
170c5c5edaeSJenius
17171b6c42eSxu_zh  // IFU-ICache
172c5c5edaeSJenius  icache.io.fetch.req <> ftq.io.toICache.req
173c5c5edaeSJenius  ftq.io.toICache.req.ready := ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
174c5c5edaeSJenius
175c5c5edaeSJenius  ifu.io.icacheInter.resp <> icache.io.fetch.resp
17650780602SJenius  ifu.io.icacheInter.icacheReady       := icache.io.toIFU
177d2b20d1aSTang Haojin  ifu.io.icacheInter.topdownIcacheMiss := icache.io.fetch.topdownIcacheMiss
178d2b20d1aSTang Haojin  ifu.io.icacheInter.topdownItlbMiss   := icache.io.fetch.topdownItlbMiss
1791d8f4dcbSJay  icache.io.stop                       := ifu.io.icacheStop
180b92f8445Sssszwic  icache.io.flush                      := ftq.io.icacheFlush
18109c6f1ddSLingrui98
1821d8f4dcbSJay  ifu.io.icachePerfInfo := icache.io.perfInfo
1831d8f4dcbSJay
184ecccf78fSJay  icache.io.csr_pf_enable     := RegNext(csrCtrl.l1I_pf_enable)
185ecccf78fSJay  icache.io.csr_parity_enable := RegNext(csrCtrl.icache_parity_enable)
186ecccf78fSJay
1873dbaa960SEaston Man  icache.io.fencei := RegNext(io.fencei)
1882a6078bfSguohongyu
18909c6f1ddSLingrui98  // IFU-Ibuffer
19009c6f1ddSLingrui98  ifu.io.toIbuffer <> ibuffer.io.in
19109c6f1ddSLingrui98
19209c6f1ddSLingrui98  ftq.io.fromBackend <> io.backend.toFtq
19392c61038SXuan Hu  io.backend.fromFtq := ftq.io.toBackend
19492c61038SXuan Hu  io.backend.fromIfu := ifu.io.toBackend
19509c6f1ddSLingrui98  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
19609c6f1ddSLingrui98
1975359309bSGuokai Chen  val checkPcMem = Reg(Vec(FtqSize, new Ftq_RF_Components))
1985359309bSGuokai Chen  when(ftq.io.toBackend.pc_mem_wen) {
199f533cba7SHuSipeng    checkPcMem(ftq.io.toBackend.pc_mem_waddr) := ftq.io.toBackend.pc_mem_wdata
2005359309bSGuokai Chen  }
2015359309bSGuokai Chen
202*4d53e0efSzhou tao  val checkTargetPtr = Wire(Vec(DecodeWidth, new FtqPtr))
2035359309bSGuokai Chen  val checkTarget    = Wire(Vec(DecodeWidth, UInt(VAddrBits.W)))
2045359309bSGuokai Chen
2055359309bSGuokai Chen  for (i <- 0 until DecodeWidth) {
206*4d53e0efSzhou tao    checkTargetPtr(i) := ibuffer.io.out(i).bits.ftqPtr
207cf7d6b7aSMuzi    checkTarget(i) := Mux(
208*4d53e0efSzhou tao      ftq.io.toBackend.newest_entry_ptr.value === checkTargetPtr(i).value,
2095359309bSGuokai Chen      ftq.io.toBackend.newest_entry_target,
210*4d53e0efSzhou tao      checkPcMem((checkTargetPtr(i) + 1.U).value).startAddr
211cf7d6b7aSMuzi    )
2125359309bSGuokai Chen  }
2135359309bSGuokai Chen
2145359309bSGuokai Chen  // commented out for this br could be the last instruction in the fetch block
2155359309bSGuokai Chen  def checkNotTakenConsecutive = {
2165359309bSGuokai Chen    val prevNotTakenValid  = RegInit(0.B)
217*4d53e0efSzhou tao    val prevNotTakenFtqPtr = Reg(new FtqPtr)
2185359309bSGuokai Chen    for (i <- 0 until DecodeWidth - 1) {
2195359309bSGuokai Chen      // for instrs that is not the last, if a not-taken br, the next instr should have the same ftqPtr
2205359309bSGuokai Chen      // for instrs that is the last, record and check next request
2215359309bSGuokai Chen      when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr) {
2225359309bSGuokai Chen        when(ibuffer.io.out(i + 1).fire) {
2235359309bSGuokai Chen          // not last br, check now
224*4d53e0efSzhou tao          XSError(checkTargetPtr(i).value =/= checkTargetPtr(i + 1).value, "not-taken br should have same ftqPtr\n")
2255359309bSGuokai Chen        }.otherwise {
2265359309bSGuokai Chen          // last br, record its info
2275359309bSGuokai Chen          prevNotTakenValid  := true.B
228*4d53e0efSzhou tao          prevNotTakenFtqPtr := checkTargetPtr(i)
2295359309bSGuokai Chen        }
2305359309bSGuokai Chen      }
2315359309bSGuokai Chen    }
2325359309bSGuokai Chen    when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr) {
2335359309bSGuokai Chen      // last instr is a br, record its info
2345359309bSGuokai Chen      prevNotTakenValid  := true.B
235*4d53e0efSzhou tao      prevNotTakenFtqPtr := checkTargetPtr(DecodeWidth - 1)
2365359309bSGuokai Chen    }
2375359309bSGuokai Chen    when(prevNotTakenValid && ibuffer.io.out(0).fire) {
238*4d53e0efSzhou tao      XSError(prevNotTakenFtqPtr.value =/= checkTargetPtr(0).value, "not-taken br should have same ftqPtr\n")
2395359309bSGuokai Chen      prevNotTakenValid := false.B
2405359309bSGuokai Chen    }
2415359309bSGuokai Chen    when(needFlush) {
2425359309bSGuokai Chen      prevNotTakenValid := false.B
2435359309bSGuokai Chen    }
2445359309bSGuokai Chen  }
2455359309bSGuokai Chen
2465359309bSGuokai Chen  def checkTakenNotConsecutive = {
2475359309bSGuokai Chen    val prevTakenValid  = RegInit(0.B)
248*4d53e0efSzhou tao    val prevTakenFtqPtr = Reg(new FtqPtr)
2495359309bSGuokai Chen    for (i <- 0 until DecodeWidth - 1) {
2505359309bSGuokai Chen      // for instrs that is not the last, if a taken br, the next instr should not have the same ftqPtr
2515359309bSGuokai Chen      // for instrs that is the last, record and check next request
2525359309bSGuokai Chen      when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && ibuffer.io.out(i).bits.pred_taken) {
2535359309bSGuokai Chen        when(ibuffer.io.out(i + 1).fire) {
2545359309bSGuokai Chen          // not last br, check now
255*4d53e0efSzhou tao          XSError(
256*4d53e0efSzhou tao            (checkTargetPtr(i) + 1.U).value =/= checkTargetPtr(i + 1).value,
257*4d53e0efSzhou tao            "taken br should have consecutive ftqPtr\n"
258*4d53e0efSzhou tao          )
2595359309bSGuokai Chen        }.otherwise {
2605359309bSGuokai Chen          // last br, record its info
2615359309bSGuokai Chen          prevTakenValid  := true.B
262*4d53e0efSzhou tao          prevTakenFtqPtr := checkTargetPtr(i)
2635359309bSGuokai Chen        }
2645359309bSGuokai Chen      }
2655359309bSGuokai Chen    }
266cf7d6b7aSMuzi    when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && ibuffer.io.out(
267cf7d6b7aSMuzi      DecodeWidth - 1
268cf7d6b7aSMuzi    ).bits.pred_taken) {
2695359309bSGuokai Chen      // last instr is a br, record its info
2705359309bSGuokai Chen      prevTakenValid  := true.B
271*4d53e0efSzhou tao      prevTakenFtqPtr := checkTargetPtr(DecodeWidth - 1)
2725359309bSGuokai Chen    }
2735359309bSGuokai Chen    when(prevTakenValid && ibuffer.io.out(0).fire) {
274*4d53e0efSzhou tao      XSError((prevTakenFtqPtr + 1.U).value =/= checkTargetPtr(0).value, "taken br should have consecutive ftqPtr\n")
2755359309bSGuokai Chen      prevTakenValid := false.B
2765359309bSGuokai Chen    }
2775359309bSGuokai Chen    when(needFlush) {
2785359309bSGuokai Chen      prevTakenValid := false.B
2795359309bSGuokai Chen    }
2805359309bSGuokai Chen  }
2815359309bSGuokai Chen
2825359309bSGuokai Chen  def checkNotTakenPC = {
2835359309bSGuokai Chen    val prevNotTakenPC    = Reg(UInt(VAddrBits.W))
2845359309bSGuokai Chen    val prevIsRVC         = Reg(Bool())
2855359309bSGuokai Chen    val prevNotTakenValid = RegInit(0.B)
2865359309bSGuokai Chen
2875359309bSGuokai Chen    for (i <- 0 until DecodeWidth - 1) {
2885359309bSGuokai Chen      when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && !ibuffer.io.out(i).bits.pred_taken) {
2895359309bSGuokai Chen        when(ibuffer.io.out(i + 1).fire) {
290cf7d6b7aSMuzi          XSError(
291cf7d6b7aSMuzi            ibuffer.io.out(i).bits.pc + Mux(ibuffer.io.out(i).bits.pd.isRVC, 2.U, 4.U) =/= ibuffer.io.out(
292cf7d6b7aSMuzi              i + 1
293cf7d6b7aSMuzi            ).bits.pc,
294cf7d6b7aSMuzi            "not-taken br should have consecutive pc\n"
295cf7d6b7aSMuzi          )
2965359309bSGuokai Chen        }.otherwise {
2975359309bSGuokai Chen          prevNotTakenValid := true.B
2985359309bSGuokai Chen          prevIsRVC         := ibuffer.io.out(i).bits.pd.isRVC
2995359309bSGuokai Chen          prevNotTakenPC    := ibuffer.io.out(i).bits.pc
3005359309bSGuokai Chen        }
3015359309bSGuokai Chen      }
3025359309bSGuokai Chen    }
303cf7d6b7aSMuzi    when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && !ibuffer.io.out(
304cf7d6b7aSMuzi      DecodeWidth - 1
305cf7d6b7aSMuzi    ).bits.pred_taken) {
3065359309bSGuokai Chen      prevNotTakenValid := true.B
3075359309bSGuokai Chen      prevIsRVC         := ibuffer.io.out(DecodeWidth - 1).bits.pd.isRVC
3085359309bSGuokai Chen      prevNotTakenPC    := ibuffer.io.out(DecodeWidth - 1).bits.pc
3095359309bSGuokai Chen    }
3105359309bSGuokai Chen    when(prevNotTakenValid && ibuffer.io.out(0).fire) {
311cf7d6b7aSMuzi      XSError(
312cf7d6b7aSMuzi        prevNotTakenPC + Mux(prevIsRVC, 2.U, 4.U) =/= ibuffer.io.out(0).bits.pc,
313cf7d6b7aSMuzi        "not-taken br should have same pc\n"
314cf7d6b7aSMuzi      )
3155359309bSGuokai Chen      prevNotTakenValid := false.B
3165359309bSGuokai Chen    }
3175359309bSGuokai Chen    when(needFlush) {
3185359309bSGuokai Chen      prevNotTakenValid := false.B
3195359309bSGuokai Chen    }
3205359309bSGuokai Chen  }
3215359309bSGuokai Chen
3225359309bSGuokai Chen  def checkTakenPC = {
323*4d53e0efSzhou tao    val prevTakenFtqPtr = Reg(new FtqPtr)
3245359309bSGuokai Chen    val prevTakenValid  = RegInit(0.B)
3255359309bSGuokai Chen    val prevTakenTarget = Wire(UInt(VAddrBits.W))
326*4d53e0efSzhou tao    prevTakenTarget := checkPcMem((prevTakenFtqPtr + 1.U).value).startAddr
3275359309bSGuokai Chen
3285359309bSGuokai Chen    for (i <- 0 until DecodeWidth - 1) {
3295359309bSGuokai Chen      when(ibuffer.io.out(i).fire && !ibuffer.io.out(i).bits.pd.notCFI && ibuffer.io.out(i).bits.pred_taken) {
3305359309bSGuokai Chen        when(ibuffer.io.out(i + 1).fire) {
3315359309bSGuokai Chen          XSError(checkTarget(i) =/= ibuffer.io.out(i + 1).bits.pc, "taken instr should follow target pc\n")
3325359309bSGuokai Chen        }.otherwise {
3335359309bSGuokai Chen          prevTakenValid  := true.B
334*4d53e0efSzhou tao          prevTakenFtqPtr := checkTargetPtr(i)
3355359309bSGuokai Chen        }
3365359309bSGuokai Chen      }
3375359309bSGuokai Chen    }
338cf7d6b7aSMuzi    when(ibuffer.io.out(DecodeWidth - 1).fire && !ibuffer.io.out(DecodeWidth - 1).bits.pd.notCFI && ibuffer.io.out(
339cf7d6b7aSMuzi      DecodeWidth - 1
340cf7d6b7aSMuzi    ).bits.pred_taken) {
3415359309bSGuokai Chen      prevTakenValid  := true.B
342*4d53e0efSzhou tao      prevTakenFtqPtr := checkTargetPtr(DecodeWidth - 1)
3435359309bSGuokai Chen    }
3445359309bSGuokai Chen    when(prevTakenValid && ibuffer.io.out(0).fire) {
3455359309bSGuokai Chen      XSError(prevTakenTarget =/= ibuffer.io.out(0).bits.pc, "taken instr should follow target pc\n")
3465359309bSGuokai Chen      prevTakenValid := false.B
3475359309bSGuokai Chen    }
3485359309bSGuokai Chen    when(needFlush) {
3495359309bSGuokai Chen      prevTakenValid := false.B
3505359309bSGuokai Chen    }
3515359309bSGuokai Chen  }
3525359309bSGuokai Chen
3535359309bSGuokai Chen  // checkNotTakenConsecutive
3545359309bSGuokai Chen  checkTakenNotConsecutive
3555359309bSGuokai Chen  checkTakenPC
3565359309bSGuokai Chen  checkNotTakenPC
3575359309bSGuokai Chen
358a37fbf10SJay  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
359a37fbf10SJay
36009c6f1ddSLingrui98  ibuffer.io.flush                := needFlush
361d2b20d1aSTang Haojin  ibuffer.io.ControlRedirect      := FlushControlRedirect
362d2b20d1aSTang Haojin  ibuffer.io.MemVioRedirect       := FlushMemVioRedirect
363d2b20d1aSTang Haojin  ibuffer.io.ControlBTBMissBubble := FlushControlBTBMiss
364d2b20d1aSTang Haojin  ibuffer.io.TAGEMissBubble       := FlushTAGEMiss
365d2b20d1aSTang Haojin  ibuffer.io.SCMissBubble         := FlushSCMiss
366d2b20d1aSTang Haojin  ibuffer.io.ITTAGEMissBubble     := FlushITTAGEMiss
367d2b20d1aSTang Haojin  ibuffer.io.RASMissBubble        := FlushRASMiss
36805cc2a4eSXuan Hu  ibuffer.io.decodeCanAccept      := io.backend.canAccept
369d2b20d1aSTang Haojin
370d2b20d1aSTang Haojin  FlushControlBTBMiss := ftq.io.ControlBTBMissBubble
371d2b20d1aSTang Haojin  FlushTAGEMiss       := ftq.io.TAGEMissBubble
372d2b20d1aSTang Haojin  FlushSCMiss         := ftq.io.SCMissBubble
373d2b20d1aSTang Haojin  FlushITTAGEMiss     := ftq.io.ITTAGEMissBubble
374d2b20d1aSTang Haojin  FlushRASMiss        := ftq.io.RASMissBubble
375d2b20d1aSTang Haojin
37609c6f1ddSLingrui98  io.backend.cfVec <> ibuffer.io.out
377d2b20d1aSTang Haojin  io.backend.stallReason <> ibuffer.io.stallReason
37809c6f1ddSLingrui98
3790be662e4SJay  instrUncache.io.req <> ifu.io.uncacheInter.toUncache
3800be662e4SJay  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
38158dbdfc2SJay  instrUncache.io.flush := false.B
38258dbdfc2SJay  io.error <> RegNext(RegNext(icache.io.error))
38309c6f1ddSLingrui98
38441cb8b61SJenius  icache.io.hartId := io.hartId
38541cb8b61SJenius
38660ebee38STang Haojin  itlbRepeater1.io.debugTopDown.robHeadVaddr := io.debugTopDown.robHeadVaddr
38760ebee38STang Haojin
3889c55e669SEaston Man  val frontendBubble = Mux(io.backend.canAccept, DecodeWidth.U - PopCount(ibuffer.io.out.map(_.valid)), 0.U)
38909c6f1ddSLingrui98  XSPerfAccumulate("FrontendBubble", frontendBubble)
39009c6f1ddSLingrui98  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
391233f2ad0Szhanglinjuan  io.resetInFrontend       := reset.asBool
392cd365d4cSrvcoresjw
3931ca0e4f3SYinan Xu  // PFEvent
3941ca0e4f3SYinan Xu  val pfevent = Module(new PFEvent)
3951ca0e4f3SYinan Xu  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
3961ca0e4f3SYinan Xu  val csrevents = pfevent.io.hpmevent.take(8)
397cd365d4cSrvcoresjw
3989a128342SHaoyuan Feng  val perfFromUnits = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerfEvents)
3999a128342SHaoyuan Feng  val perfFromIO    = Seq()
4009a128342SHaoyuan Feng  val perfBlock     = Seq()
4019a128342SHaoyuan Feng  // let index = 0 be no event
4029a128342SHaoyuan Feng  val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock
4039a128342SHaoyuan Feng
4049a128342SHaoyuan Feng  if (printEventCoding) {
4059a128342SHaoyuan Feng    for (((name, inc), i) <- allPerfEvents.zipWithIndex) {
4069a128342SHaoyuan Feng      println("Frontend perfEvents Set", name, inc, i)
4079a128342SHaoyuan Feng    }
4089a128342SHaoyuan Feng  }
4099a128342SHaoyuan Feng
4109a128342SHaoyuan Feng  val allPerfInc          = allPerfEvents.map(_._2.asTypeOf(new PerfEvent))
4119a128342SHaoyuan Feng  override val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents
4121ca0e4f3SYinan Xu  generatePerfEvent()
41309c6f1ddSLingrui98}
414