xref: /XiangShan/src/main/scala/xiangshan/backend/fu/CSR.scala (revision b720b0cd6c9c77ec4fddf067662248c19fb09348)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17package xiangshan.backend.fu
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import difftest._
23import freechips.rocketchip.util._
24import utility.MaskedRegMap.WritableMask
25import utils._
26import utility._
27import xiangshan.ExceptionNO._
28import xiangshan._
29import xiangshan.backend.fu.util._
30import xiangshan.cache._
31import xiangshan.backend.Bundles.{ExceptionInfo, TrapInstInfo}
32import xiangshan.backend.fu.NewCSR.CSREvents.TargetPCBundle
33import xiangshan.backend.fu.NewCSR.CSRNamedConstant.ContextStatus
34import xiangshan.backend.rob.RobPtr
35import utils.MathUtils.{BigIntGenMask, BigIntNot}
36import xiangshan.backend.trace._
37
38class FpuCsrIO extends Bundle {
39  val fflags = Output(Valid(UInt(5.W)))
40  val isIllegal = Output(Bool())
41  val dirty_fs = Output(Bool())
42  val frm = Input(UInt(3.W))
43}
44
45class VpuCsrIO(implicit p: Parameters) extends XSBundle {
46  val vstart = Input(UInt(XLEN.W))
47  val vxrm = Input(UInt(2.W))
48
49  val vl = Output(UInt(XLEN.W))
50
51  val set_vstart = Output(Valid(UInt(XLEN.W)))
52  val set_vtype = Output(Valid(UInt(XLEN.W)))
53  val set_vxsat = Output(Valid(UInt(1.W)))
54
55  val dirty_vs = Output(Bool())
56}
57
58
59class PerfCounterIO(implicit p: Parameters) extends XSBundle {
60  val perfEventsFrontend  = Vec(numCSRPCntFrontend, new PerfEvent)
61  val perfEventsBackend   = Vec(numCSRPCntCtrl, new PerfEvent)
62  val perfEventsLsu       = Vec(numCSRPCntLsu, new PerfEvent)
63  val perfEventsHc        = Vec(numPCntHc * coreParams.L2NBanks + 1, new PerfEvent)
64  val retiredInstr = UInt(7.W)
65  val frontendInfo = new Bundle {
66    val ibufFull  = Bool()
67    val bpuInfo = new Bundle {
68      val bpRight = UInt(XLEN.W)
69      val bpWrong = UInt(XLEN.W)
70    }
71  }
72  val ctrlInfo = new Bundle {
73    val robFull   = Bool()
74    val intdqFull = Bool()
75    val fpdqFull  = Bool()
76    val lsdqFull  = Bool()
77  }
78  val memInfo = new Bundle {
79    val sqFull = Bool()
80    val lqFull = Bool()
81    val dcacheMSHRFull = Bool()
82  }
83}
84
85class CSRFileIO(implicit p: Parameters) extends XSBundle {
86  val hartId = Input(UInt(hartIdLen.W))
87  // output (for func === CSROpType.jmp)
88  val perf = Input(new PerfCounterIO)
89  val criticalErrorState = Output(Bool())
90  val isPerfCnt = Output(Bool())
91  // to FPU
92  val fpu = Flipped(new FpuCsrIO)
93  // to VPU
94  val vpu = Flipped(new VpuCsrIO)
95  // from rob
96  val exception = Flipped(ValidIO(new ExceptionInfo))
97  val robDeqPtr = Input(new RobPtr)
98  // to ROB
99  val isXRet = Output(Bool())
100  val trapTarget = Output(new TargetPCBundle)
101  val interrupt = Output(Bool())
102  val wfi_event = Output(Bool())
103  val trapTraceInfo = ValidIO(new TraceTrap)
104  // from LSQ
105  val memExceptionVAddr = Input(UInt(XLEN.W))
106  val memExceptionGPAddr = Input(UInt(XLEN.W))
107  val memExceptionIsForVSnonLeafPTE = Input(Bool())
108  // from outside cpu,externalInterrupt
109  val externalInterrupt = Input(new ExternalInterruptIO)
110  // TLB
111  val tlb = Output(new TlbCsrBundle)
112  // Debug Mode
113  // val singleStep = Output(Bool())
114  val debugMode = Output(Bool())
115  // Custom microarchiture ctrl signal
116  val customCtrl = Output(new CustomCSRCtrlIO)
117  // instruction fetch address translation type
118  val instrAddrTransType = Output(new AddrTransType)
119}
120
121class VtypeStruct(implicit p: Parameters) extends XSBundle {
122  val vill = UInt(1.W)
123  val reserved = UInt((XLEN - 9).W)
124  val vma = UInt(1.W)
125  val vta = UInt(1.W)
126  val vsew = UInt(3.W)
127  val vlmul = UInt(3.W)
128}
129/*
130class CSR(cfg: FuConfig)(implicit p: Parameters) extends FuncUnit(cfg)
131  with HasCSRConst
132  with PMPMethod
133  with PMAMethod
134  with HasXSParameter
135  with SdtrigExt
136  with DebugCSR
137{
138  val csrio = io.csrio.get
139
140  val flushPipe = Wire(Bool())
141
142  val (valid, src1, src2, func) = (
143    io.in.valid,
144    io.in.bits.data.src(0),
145    io.in.bits.data.imm,
146    io.in.bits.ctrl.fuOpType
147  )
148
149  // CSR define
150  val virtMode = RegInit(false.B)
151  csrio.customCtrl.virtMode := virtMode
152
153  class Priv extends Bundle {
154    val m = Output(Bool())
155    val h = Output(Bool()) // unused
156    val s = Output(Bool())
157    val u = Output(Bool())
158  }
159
160  class MstatusStruct extends Bundle {
161    val sd = Output(UInt(1.W))
162
163    val pad1 = if (XLEN == 64 && HasHExtension) Output(UInt(23.W)) else if (XLEN == 64) Output(UInt(25.W)) else null
164    val mpv  = if (XLEN == 64 && HasHExtension) Output(UInt(1.W)) else null
165    val gva  = if (XLEN == 64 && HasHExtension) Output(UInt(1.W)) else null
166    val mbe  = if (XLEN == 64) Output(UInt(1.W)) else null
167    val sbe  = if (XLEN == 64) Output(UInt(1.W)) else null
168    val sxl  = if (XLEN == 64) Output(UInt(2.W))  else null
169    val uxl  = if (XLEN == 64) Output(UInt(2.W))  else null
170    val pad0 = if (XLEN == 64) Output(UInt(9.W))  else Output(UInt(8.W))
171
172    val tsr = Output(UInt(1.W))
173    val tw = Output(UInt(1.W))
174    val tvm = Output(UInt(1.W))
175    val mxr = Output(UInt(1.W))
176    val sum = Output(UInt(1.W))
177    val mprv = Output(UInt(1.W))
178    val xs = Output(UInt(2.W))
179    val fs = Output(UInt(2.W))
180    val mpp = Output(UInt(2.W))
181    val vs = Output(UInt(2.W))
182    val spp = Output(UInt(1.W))
183    val pie = new Priv
184    val ie = new Priv
185    assert(this.getWidth == XLEN)
186
187    def ube = pie.h // a little ugly
188    def ube_(r: UInt): Unit = {
189      pie.h := r(0)
190    }
191  }
192
193  class HstatusStruct extends Bundle {
194    val pad4 = if (HSXLEN == 64) Output(UInt(30.W)) else null
195    val vsxl = if (HSXLEN == 64) Output(UInt(2.W)) else null
196    val pad3 = Output(UInt(9.W))
197    val vtsr = Output(UInt(1.W))
198    val vtw = Output(UInt(1.W))
199    val vtvm = Output(UInt(1.W))
200    val pad2 = Output(UInt(2.W))
201    val vgein = Output(UInt(6.W))
202    val pad1 = Output(UInt(2.W))
203    val hu = Output(UInt(1.W))
204    val spvp = Output(UInt(1.W))
205    val spv = Output(UInt(1.W))
206    val gva = Output(UInt(1.W))
207    val vsbe = Output(UInt(1.W))
208    val pad0 = Output(UInt(5.W))
209    assert(this.getWidth == XLEN)
210  }
211
212  class Interrupt extends Bundle {
213//  val d = Output(Bool())    // Debug
214    val e = new Priv
215    val t = new Priv
216    val s = new Priv
217  }
218
219  // Debug CSRs
220  val dcsr = RegInit(UInt(32.W), DcsrStruct.init)
221  val dpc = Reg(UInt(64.W))
222  val dscratch0 = Reg(UInt(64.W))
223  val dscratch1 = Reg(UInt(64.W))
224  val debugMode = RegInit(false.B)
225  val debugIntrEnable = RegInit(true.B) // debug interrupt will be handle only when debugIntrEnable
226  csrio.debugMode := debugMode
227
228  val dpcPrev = RegNext(dpc)
229  XSDebug(dpcPrev =/= dpc, "Debug Mode: dpc is altered! Current is %x, previous is %x\n", dpc, dpcPrev)
230
231  val dcsrData = Wire(new DcsrStruct)
232  dcsrData := dcsr.asTypeOf(new DcsrStruct)
233  val dcsrMask = ZeroExt(GenMask(15) | GenMask(13, 11) | GenMask(4) | GenMask(2, 0), XLEN)// Dcsr write mask
234  def dcsrUpdateSideEffect(dcsr: UInt): UInt = {
235    val dcsrOld = WireInit(dcsr.asTypeOf(new DcsrStruct))
236    val dcsrNew = dcsr | (dcsrOld.prv(0) | dcsrOld.prv(1)).asUInt // turn 10 priv into 11
237    dcsrNew
238  }
239  // csrio.singleStep := dcsrData.step
240  csrio.customCtrl.singlestep := dcsrData.step && !debugMode
241
242  // Trigger CSRs
243  private val tselectPhy = RegInit(0.U(log2Up(TriggerNum).W))
244
245  private val tdata1RegVec = RegInit(VecInit(Seq.fill(TriggerNum)(Tdata1Bundle.default)))
246  private val tdata2RegVec = RegInit(VecInit(Seq.fill(TriggerNum)(0.U(64.W))))
247  private val tdata1WireVec = tdata1RegVec.map(_.asTypeOf(new Tdata1Bundle))
248  private val tdata2WireVec = tdata2RegVec
249  private val tdata1Selected = tdata1RegVec(tselectPhy).asTypeOf(new Tdata1Bundle)
250  private val tdata2Selected = tdata2RegVec(tselectPhy)
251  private val newTriggerChainVec = UIntToOH(tselectPhy, TriggerNum).asBools | tdata1WireVec.map(_.data.asTypeOf(new MControlData).chain)
252  private val newTriggerChainIsLegal = TriggerCheckChainLegal(newTriggerChainVec, TriggerChainMaxLength)
253  val tinfo = RegInit((BigInt(1) << TrigTypeEnum.MCONTROL.litValue.toInt).U(XLEN.W)) // This value should be 4.U
254
255
256  def WriteTselect(wdata: UInt) = {
257    Mux(wdata < TriggerNum.U, wdata(log2Up(TriggerNum) - 1, 0), tselectPhy)
258  }
259
260  def GenTdataDistribute(tdata1: Tdata1Bundle, tdata2: UInt): MatchTriggerIO = {
261    val res = Wire(new MatchTriggerIO)
262    val mcontrol: MControlData = WireInit(tdata1.data.asTypeOf(new MControlData))
263    res.matchType := mcontrol.match_.asUInt
264    res.select    := mcontrol.select
265    res.timing    := mcontrol.timing
266    res.action    := mcontrol.action.asUInt
267    res.chain     := mcontrol.chain
268    res.execute   := mcontrol.execute
269    res.load      := mcontrol.load
270    res.store     := mcontrol.store
271    res.tdata2    := tdata2
272    res
273  }
274
275  csrio.customCtrl.frontend_trigger.tUpdate.bits.addr := tselectPhy
276  csrio.customCtrl.mem_trigger.tUpdate.bits.addr := tselectPhy
277  csrio.customCtrl.frontend_trigger.tUpdate.bits.tdata := GenTdataDistribute(tdata1Selected, tdata2Selected)
278  csrio.customCtrl.mem_trigger.tUpdate.bits.tdata := GenTdataDistribute(tdata1Selected, tdata2Selected)
279
280  // Machine-Level CSRs
281  // mtvec: {BASE (WARL), MODE (WARL)} where mode is 0 or 1
282  val mtvecMask = ~(0x2.U(XLEN.W))
283  val mtvec = RegInit(UInt(XLEN.W), 0.U)
284  val mcounteren = RegInit(UInt(XLEN.W), 0.U)
285  // Currently, XiangShan don't support Unprivileged Counter/Timers CSRs ("Zicntr" and "Zihpm")
286  val mcounterenMask = 0.U(XLEN.W)
287  val mcause = RegInit(UInt(XLEN.W), 0.U)
288  val mtval = RegInit(UInt(XLEN.W), 0.U)
289  val mtval2 = RegInit(UInt(XLEN.W), 0.U)
290  val mtinst = RegInit(UInt(XLEN.W), 0.U)
291  val mepc = RegInit(UInt(XLEN.W), 0.U)
292  // Page 36 in riscv-priv: The low bit of mepc (mepc[0]) is always zero.
293  val mepcMask = ~(0x1.U(XLEN.W))
294
295  val mie = RegInit(0.U(XLEN.W))
296  val mipWire = WireInit(0.U.asTypeOf(new Interrupt))
297  val mipReg  = RegInit(0.U(XLEN.W))
298  val mipMask = ZeroExt(Array(
299    1,  // SSIP
300    2,  // VSSIP
301    3,  // MSIP
302    5,  // STIP
303    6,  // VSTIP
304    7,  // MTIP
305    9,  // SEIP
306    10, // VSEIP
307    11, // MEIP
308    12, // SGEIP
309  ).map(GenMask(_)).reduce(_ | _), XLEN)
310  val mip = (mipWire.asUInt | mipReg).asTypeOf(new Interrupt)
311
312  val mip_mie_WMask_H = if(HasHExtension){((1 << 2) | (1 << 6) | (1 << 10) | (1 << 12)).U(XLEN.W)}else{0.U(XLEN.W)}
313  val vssip_Mask = (1 << 2).U(XLEN.W)
314
315  val mipWMask = vssip_Mask | ((1 << 9) | (1 << 5) | (1 << 1)).U(XLEN.W)
316  val mieWMask = mip_mie_WMask_H | "haaa".U(XLEN.W)
317
318  def getMisaMxl(mxl: BigInt): BigInt = mxl << (XLEN - 2)
319  def getMisaExt(ext: Char): Long = 1 << (ext.toInt - 'a'.toInt)
320  var extList = List('a', 's', 'i', 'u')
321  if (HasMExtension) { extList = extList :+ 'm' }
322  if (HasCExtension) { extList = extList :+ 'c' }
323  if (HasHExtension) { extList = extList :+ 'h' }
324  if (HasFPU) { extList = extList ++ List('f', 'd') }
325  if (HasVPU) { extList = extList :+ 'v' }
326  val misaInitVal = getMisaMxl(2) | extList.foldLeft(0L)((sum, i) => sum | getMisaExt(i)) //"h8000000000141185".U
327  val misa = RegInit(UInt(XLEN.W), misaInitVal.U)
328  println(s"[CSR] supported isa ext: $extList")
329
330  // MXL = 2          | 0 | EXT = b 00 0000 0100 0001 0001 0000 0101
331  // (XLEN-1, XLEN-2) |   |(25, 0)  ZY XWVU TSRQ PONM LKJI HGFE DCBA
332
333  // Machine Configuration
334  val menvcfg = RegInit(UInt(XLEN.W), 0.U)
335
336  val mvendorid = RegInit(UInt(XLEN.W), 0.U) // this is a non-commercial implementation
337  val marchid = RegInit(UInt(XLEN.W), 25.U) // architecture id for XiangShan is 25; see https://github.com/riscv/riscv-isa-manual/blob/master/marchid.md
338  val mimpid = RegInit(UInt(XLEN.W), 0.U) // provides a unique encoding of the version of the processor implementation
339  val mhartid = Reg(UInt(XLEN.W)) // the hardware thread running the code
340  when (RegNext(RegNext(reset.asBool) && !reset.asBool)) {
341    mhartid := csrio.hartId
342  }
343  val mconfigptr = RegInit(UInt(XLEN.W), 0.U) // the read-only pointer pointing to the platform config structure, 0 for not supported.
344  val mstatus = RegInit("ha00002200".U(XLEN.W))
345
346  // mstatus Value Table
347  // | sd   | Read Only
348  // | pad1 | WPRI
349  // | sxl  | hardlinked to 10, use 00 to pass xv6 test
350  // | uxl  | hardlinked to 10
351  // | pad0 |
352  // | tsr  |
353  // | tw   |
354  // | tvm  |
355  // | mxr  |
356  // | sum  |
357  // | mprv |
358  // | xs   | 00 |
359  // | fs   | 01 |
360  // | mpp  | 00 |
361  // | vs   | 01 |
362  // | spp  | 0 |
363  // | pie  | 0000 | pie.h is used as UBE
364  // | ie   | 0000 | uie hardlinked to 0, as N ext is not implemented
365
366  val mstatusStruct = mstatus.asTypeOf(new MstatusStruct)
367  def mstatusUpdateSideEffect(mstatus: UInt): UInt = {
368    val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct))
369    // Cat(sd, other)
370    val mstatusNew = Cat(
371      mstatusOld.xs === ContextStatus.dirty || mstatusOld.fs === ContextStatus.dirty || mstatusOld.vs === ContextStatus.dirty,
372      mstatus(XLEN-2, 0)
373    )
374    mstatusNew
375  }
376  def vsstatusUpdateSideEffect(vsstatus: UInt): UInt = {
377    val vsstatusOld = WireInit(vsstatus.asTypeOf(new MstatusStruct))
378    val vsstatusNew = Cat(vsstatusOld.xs === "b11".U || vsstatusOld.fs === "b11".U, vsstatus(XLEN-2, 0))
379    vsstatusNew
380  }
381  val mstatusWMask = (~ZeroExt((
382    GenMask(63)           | // SD is read-only
383    (if(HasHExtension)
384        GenMask(62, 40)    // WPRI
385      else
386        GenMask(62, 38)  )| // WPRI
387    GenMask(35, 32)       | // SXL and UXL cannot be changed
388    GenMask(31, 23)       | // WPRI
389    GenMask(16, 15)       | // XS is read-only
390    GenMask(6)            | // UBE, always little-endian (0)
391    GenMask(4)            | // WPRI
392    GenMask(2)            | // WPRI
393    GenMask(0)              // WPRI
394  ), 64)).asUInt
395
396  val medeleg = RegInit(UInt(XLEN.W), 0.U)
397  val midelegInit = if(HasHExtension){((1 << 12) | (1 << 10) | (1 << 6) | (1 << 2)).U}else{0.U}
398  val medelegWMask = if(HasHExtension) {
399    "hf0b7ff".U(XLEN.W)
400  }else {
401    "hb3ff".U(XLEN.W)
402  }
403
404
405  val mideleg = RegInit(UInt(XLEN.W), midelegInit)
406  val mscratch = RegInit(UInt(XLEN.W), 0.U)
407
408  val midelegWMask = "h222".U(XLEN.W)
409  // PMP Mapping
410  val pmp = Wire(Vec(NumPMP, new PMPEntry())) // just used for method parameter
411  val pma = Wire(Vec(NumPMA, new PMPEntry())) // just used for method parameter
412  val pmpMapping = pmp_gen_mapping(pmp_init, NumPMP, PmpcfgBase, PmpaddrBase, pmp)
413  val pmaMapping = pmp_gen_mapping(pma_init, NumPMA, PmacfgBase, PmaaddrBase, pma)
414  // !WARNNING: pmp and pma CSRs are not checked in difftest.
415
416  // Supervisor-Level CSRs
417
418  val sstatusWNmask: BigInt = (
419    BigIntGenMask(63)     | // SD is read-only
420    BigIntGenMask(62, 34) | // WPRI
421    BigIntGenMask(33, 32) | // UXL is hard-wired to 64(b10)
422    BigIntGenMask(31, 20) | // WPRI
423    BigIntGenMask(17)     | // WPRI
424    BigIntGenMask(16, 15) | // XS is read-only to zero
425    BigIntGenMask(12, 11) | // WPRI
426    BigIntGenMask(7)      | // WPRI
427    BigIntGenMask(6)      | // UBE is always little-endian (0)
428    BigIntGenMask(4, 2)   | // WPRI
429    BigIntGenMask(0)        // WPRI
430  )
431
432  val sstatusWmask = BigIntNot(sstatusWNmask).U(XLEN.W)
433  val sstatusRmask = (
434    BigIntGenMask(63)     | // SD
435    BigIntGenMask(33, 32) | // UXL
436    BigIntGenMask(19)     | // MXR
437    BigIntGenMask(18)     | // SUM
438    BigIntGenMask(16, 15) | // XS
439    BigIntGenMask(14, 13) | // FS
440    BigIntGenMask(10, 9 ) | // VS
441    BigIntGenMask(8)      | // SPP
442    BigIntGenMask(6)      | // UBE: hard wired to 0
443    BigIntGenMask(5)      | // SPIE
444    BigIntGenMask(1)
445  ).U(XLEN.W)
446
447  println(s"sstatusWNmask: 0x${sstatusWNmask.toString(16)}")
448  println(s"sstatusWmask: 0x${sstatusWmask.litValue.toString(16)}")
449  println(s"sstatusRmask: 0x${sstatusRmask.litValue.toString(16)}")
450
451  // stvec: {BASE (WARL), MODE (WARL)} where mode is 0 or 1
452  val stvecMask = ~(0x2.U(XLEN.W))
453  val stvec = RegInit(UInt(XLEN.W), 0.U)
454  // val sie = RegInit(0.U(XLEN.W))
455  val sieMask = "h222".U & mideleg
456  val sipMask = "h222".U & mideleg
457  val sipWMask = "h2".U(XLEN.W) // ssip is writeable in smode
458  val satp = if(EnbaleTlbDebug) RegInit(UInt(XLEN.W), "h8000000000087fbe".U) else RegInit(0.U(XLEN.W))
459  // val satp = RegInit(UInt(XLEN.W), "h8000000000087fbe".U) // only use for tlb naive debug
460  // val satpMask = "h80000fffffffffff".U(XLEN.W) // disable asid, mode can only be 8 / 0
461  // TODO: use config to control the length of asid
462  // val satpMask = "h8fffffffffffffff".U(XLEN.W) // enable asid, mode can only be 8 / 0
463  val satpMask = Cat("h8".U(Satp_Mode_len.W), satp_part_wmask(Satp_Asid_len, AsidLength), satp_part_wmask(Satp_Addr_len, PAddrBits-12))
464  val sepc = RegInit(UInt(XLEN.W), 0.U)
465  // Page 60 in riscv-priv: The low bit of sepc (sepc[0]) is always zero.
466  val sepcMask = ~(0x1.U(XLEN.W))
467  val scause = RegInit(UInt(XLEN.W), 0.U)
468  val stval = RegInit(UInt(XLEN.W), 0.U)
469  val sscratch = RegInit(UInt(XLEN.W), 0.U)
470  val scounteren = RegInit(UInt(XLEN.W), 0.U)
471  val senvcfg = RegInit(UInt(XLEN.W), 0.U)  // !WARNING: there is no logic about this CSR.
472  // Currently, XiangShan don't support Unprivileged Counter/Timers CSRs ("Zicntr" and "Zihpm")
473  val scounterenMask = 0.U(XLEN.W)
474
475  // sbpctl
476  // Bits 0-7: {LOOP, RAS, SC, TAGE, BIM, BTB, uBTB}
477  val sbpctl = RegInit(UInt(XLEN.W), "h7f".U)
478  csrio.customCtrl.bp_ctrl.ubtb_enable := sbpctl(0)
479  csrio.customCtrl.bp_ctrl.btb_enable  := sbpctl(1)
480  csrio.customCtrl.bp_ctrl.bim_enable  := sbpctl(2)
481  csrio.customCtrl.bp_ctrl.tage_enable := sbpctl(3)
482  csrio.customCtrl.bp_ctrl.sc_enable   := sbpctl(4)
483  csrio.customCtrl.bp_ctrl.ras_enable  := sbpctl(5)
484  csrio.customCtrl.bp_ctrl.loop_enable := sbpctl(6)
485
486  // spfctl Bit 0: L1I Cache Prefetcher Enable
487  // spfctl Bit 1: L2Cache Prefetcher Enable
488  // spfctl Bit 2: L1D Cache Prefetcher Enable
489  // spfctl Bit 3: L1D train prefetch on hit
490  // spfctl Bit 4: L1D prefetch enable agt
491  // spfctl Bit 5: L1D prefetch enable pht
492  // spfctl Bit [9:6]: L1D prefetch active page threshold
493  // spfctl Bit [15:10]: L1D prefetch active page stride
494  // turn off L2 BOP, turn on L1 SMS by default
495  val spfctl = RegInit(UInt(XLEN.W), Seq(
496    0 << 17,    // L2 pf store only [17] init: false
497    1 << 16,    // L1D pf enable stride [16] init: true
498    30 << 10,   // L1D active page stride [15:10] init: 30
499    12 << 6,    // L1D active page threshold [9:6] init: 12
500    1  << 5,    // L1D enable pht [5] init: true
501    1  << 4,    // L1D enable agt [4] init: true
502    0  << 3,    // L1D train on hit [3] init: false
503    1  << 2,    // L1D pf enable [2] init: true
504    1  << 1,    // L2 pf enable [1] init: true
505    1  << 0,    // L1I pf enable [0] init: true
506  ).reduce(_|_).U(XLEN.W))
507  csrio.customCtrl.l1I_pf_enable := spfctl(0)
508  csrio.customCtrl.l2_pf_enable := spfctl(1)
509  csrio.customCtrl.l1D_pf_enable := spfctl(2)
510  csrio.customCtrl.l1D_pf_train_on_hit := spfctl(3)
511  csrio.customCtrl.l1D_pf_enable_agt := spfctl(4)
512  csrio.customCtrl.l1D_pf_enable_pht := spfctl(5)
513  csrio.customCtrl.l1D_pf_active_threshold := spfctl(9, 6)
514  csrio.customCtrl.l1D_pf_active_stride := spfctl(15, 10)
515  csrio.customCtrl.l1D_pf_enable_stride := spfctl(16)
516  csrio.customCtrl.l2_pf_store_only := spfctl(17)
517
518  // sfetchctl Bit 0: L1I Cache Parity check enable
519  val sfetchctl = RegInit(UInt(XLEN.W), "b0".U)
520  csrio.customCtrl.icache_parity_enable := sfetchctl(0)
521
522  // slvpredctl: load violation predict settings
523  // Default reset period: 2^16
524  // Why this number: reset more frequently while keeping the overhead low
525  // Overhead: extra two redirections in every 64K cycles => ~0.1% overhead
526  val slvpredctl = Reg(UInt(XLEN.W))
527  when(reset.asBool) {
528    slvpredctl := Constantin.createRecord("slvpredctl", 0x60)
529  }
530  csrio.customCtrl.lvpred_disable := slvpredctl(0)
531  csrio.customCtrl.no_spec_load := slvpredctl(1)
532  csrio.customCtrl.storeset_wait_store := slvpredctl(2)
533  csrio.customCtrl.storeset_no_fast_wakeup := slvpredctl(3)
534  csrio.customCtrl.lvpred_timeout := slvpredctl(8, 4)
535
536  //  smblockctl: memory block configurations
537  //  +------------------------+---+---+---+----+----+-----+--------+
538  //  |XLEN-1                10| 9 | 8 | 7 | 6  | 5  |  4  |3      0|
539  //  +------------------------+---+---+---+----+----+-----+--------+
540  //  |           Reserved     | L | S | O | CE | SP | LVC |   Th   |
541  //  +------------------------+---+---+---+----+----+-----+--------+
542  //  Description:
543  //  Bit 3-0   : Store buffer flush threshold (Th).
544  //  Bit 4     : Enable load violation check after reset (LVC).
545  //  Bit 5     : Enable soft-prefetch after reset (SP).
546  //  Bit 6     : Enable cache error after reset (CE).
547  //  Bit 7     : Enable uncache write outstanding (O).
548  //  Bit 8     : Enable unaligned store (S).
549  //  Bit 9     : Enable unaligned load (L).
550  //  Others    : Reserved.
551
552  val smblockctl_init_val =
553    (0xf & StoreBufferThreshold) |
554    (EnableLdVioCheckAfterReset.toInt << 4) |
555    (EnableSoftPrefetchAfterReset.toInt << 5) |
556    (EnableCacheErrorAfterReset.toInt << 6) |
557    (EnableUncacheWriteOutstanding.toInt << 7) |
558    (EnableHardwareStoreMisalign.toInt << 8) |
559    (EnableHardwareLoadMisalign.toInt << 9)
560  val smblockctl = RegInit(UInt(XLEN.W), smblockctl_init_val.U)
561  csrio.customCtrl.sbuffer_threshold := smblockctl(3, 0)
562  // bits 4: enable load load violation check
563  csrio.customCtrl.ldld_vio_check_enable := smblockctl(4)
564  csrio.customCtrl.soft_prefetch_enable := smblockctl(5)
565  csrio.customCtrl.cache_error_enable := smblockctl(6)
566  csrio.customCtrl.uncache_write_outstanding_enable := smblockctl(7)
567  csrio.customCtrl.hd_misalign_st_enable := smblockctl(8)
568  csrio.customCtrl.hd_misalign_ld_enable := smblockctl(9)
569
570  println("CSR smblockctl init value:")
571  println("  Store buffer replace threshold: " + StoreBufferThreshold)
572  println("  Enable ld-ld vio check after reset: " + EnableLdVioCheckAfterReset)
573  println("  Enable soft prefetch after reset: " + EnableSoftPrefetchAfterReset)
574  println("  Enable cache error after reset: " + EnableCacheErrorAfterReset)
575  println("  Enable uncache write outstanding: " + EnableUncacheWriteOutstanding)
576  println("  Enable unaligned store: " + EnableHardwareStoreMisalign)
577  println("  Enable unaligned load: " + EnableHardwareLoadMisalign)
578
579  val srnctl = RegInit(UInt(XLEN.W), "h7".U)
580  csrio.customCtrl.fusion_enable := srnctl(0)
581  csrio.customCtrl.wfi_enable := srnctl(2)
582
583  // Hypervisor CSRs
584  val hstatusWMask = "h7003c0".U(XLEN.W)
585  // hstatus: vtsr, vtw, vtvm, hu, spvp, spv, gva,
586  val hstatus = RegInit("h200000000".U(XLEN.W))
587  val hstatusStruct = hstatus.asTypeOf(new HstatusStruct)
588  val hedeleg = RegInit(UInt(XLEN.W), 0.U)
589  val hideleg = RegInit(UInt(XLEN.W), 0.U)
590  val hidelegRMask = mideleg
591  val hidelegWMask = ((1 << 10) | (1 << 6) | (1 << 2)).U(XLEN.W)
592  val hgeie   = RegInit(UInt(XLEN.W), 0.U)
593  val htval = RegInit(UInt(XLEN.W), 0.U)
594  // hvip hip hie is part of mip or mie
595  val hvipMask = ((1 << 10) | (1 << 6) | (1 << 2)).U(XLEN.W)
596  val hipRMask = (((1 << 12).U | hvipMask) & mideleg)
597  val hipWMask = ((1 << 2).U & mideleg)// vssip
598  val hieMask = hipRMask
599  val htinst = RegInit(UInt(XLEN.W), 0.U)
600  val hgeip = RegInit(UInt(XLEN.W), 0.U)
601  val henvcfg = RegInit(UInt(XLEN.W), 0.U)
602  val hgatp = RegInit(UInt(XLEN.W), 0.U)
603  val hgatpMask = Cat("h8".U(Hgatp_Mode_len.W), satp_part_wmask(Hgatp_Vmid_len, VmidLength), satp_part_wmask(Hgatp_Addr_len, PAddrBits-12))
604  // val htimedelta = RegInit(UInt(XLEN.W), 0.U)
605  val hcounteren = RegInit(UInt(XLEN.W), 0.U)
606  // Currently, XiangShan don't support Unprivileged Counter/Timers CSRs ("Zicntr" and "Zihpm")
607  val hcounterenMask = 0.U(XLEN.W)
608
609  val vsstatus = RegInit("h200002000".U(XLEN.W))
610  val vsstatusStruct = vsstatus.asTypeOf(new MstatusStruct)
611  //vsie vsip
612  val vsMask = ((1 << 10) | (1 << 6) | (1 << 2)).U(XLEN.W)
613  val vsip_ie_Mask = ZeroExt((hideleg & mideleg & vsMask), XLEN)
614  val vsip_WMask = ZeroExt((hideleg & mideleg & vssip_Mask), XLEN)
615  val vstvec = RegInit(UInt(XLEN.W), 0.U)
616  val vsscratch = RegInit(UInt(XLEN.W), 0.U)
617  val vsepc = RegInit(UInt(XLEN.W), 0.U)
618  val vscause = RegInit(UInt(XLEN.W), 0.U)
619  val vstval = RegInit(UInt(XLEN.W), 0.U)
620  val vsatp = RegInit(UInt(XLEN.W), 0.U)
621  val tlbBundle = Wire(new TlbCsrBundle)
622  tlbBundle.satp.apply(satp)
623  tlbBundle.vsatp.apply(vsatp)
624  tlbBundle.hgatp.apply(hgatp)
625  csrio.tlb := tlbBundle
626
627  // User-Level CSRs
628  val uepc = Reg(UInt(XLEN.W))
629
630  // fcsr
631  class FcsrStruct extends Bundle {
632    val reserved = UInt((XLEN-3-5).W)
633    val frm = UInt(3.W)
634    val fflags = UInt(5.W)
635    assert(this.getWidth == XLEN)
636  }
637  val fcsr = RegInit(0.U(XLEN.W))
638  // set mstatus->sd and mstatus->fs when true
639  val csrw_dirty_fp_state = WireInit(false.B)
640
641  def frm_wfn(wdata: UInt): UInt = {
642    val fcsrOld = WireInit(fcsr.asTypeOf(new FcsrStruct))
643    csrw_dirty_fp_state := true.B
644    fcsrOld.frm := wdata(2,0)
645    fcsrOld.asUInt
646  }
647  def frm_rfn(rdata: UInt): UInt = rdata(7,5)
648
649  def fflags_wfn(update: Boolean)(wdata: UInt): UInt = {
650    val fcsrOld = fcsr.asTypeOf(new FcsrStruct)
651    val fcsrNew = WireInit(fcsrOld)
652    if (update) {
653      fcsrNew.fflags := wdata(4,0) | fcsrOld.fflags
654    } else {
655      fcsrNew.fflags := wdata(4,0)
656    }
657    fcsrNew.asUInt
658  }
659  def fflags_rfn(rdata:UInt): UInt = rdata(4,0)
660
661  def fcsr_wfn(wdata: UInt): UInt = {
662    val fcsrOld = WireInit(fcsr.asTypeOf(new FcsrStruct))
663    csrw_dirty_fp_state := true.B
664    Cat(fcsrOld.reserved, wdata.asTypeOf(fcsrOld).frm, wdata.asTypeOf(fcsrOld).fflags)
665  }
666
667  val fcsrMapping = Map(
668    MaskedRegMap(Fflags, fcsr, wfn = fflags_wfn(update = false), rfn = fflags_rfn),
669    MaskedRegMap(Frm, fcsr, wfn = frm_wfn, rfn = frm_rfn),
670    MaskedRegMap(Fcsr, fcsr, wfn = fcsr_wfn)
671  )
672
673  // Vector extension CSRs
674  val vstart = RegInit(0.U(XLEN.W))
675  val vcsr = RegInit(0.U(XLEN.W))
676  val vl = Reg(UInt(XLEN.W))
677  val vtype = Reg(UInt(XLEN.W))
678  val vlenb = RegInit(VDataBytes.U(XLEN.W))
679
680  // set mstatus->sd and mstatus->vs when true
681  val csrw_dirty_vs_state = WireInit(false.B)
682
683  // vcsr is mapped to vxrm and vxsat
684  class VcsrStruct extends Bundle {
685    val reserved = UInt((XLEN-3).W)
686    val vxrm = UInt(2.W)
687    val vxsat = UInt(1.W)
688    assert(this.getWidth == XLEN)
689  }
690
691  def vxrm_wfn(wdata: UInt): UInt = {
692    val vcsrOld = WireInit(vcsr.asTypeOf(new VcsrStruct))
693    csrw_dirty_vs_state := true.B
694    vcsrOld.vxrm := wdata(1,0)
695    vcsrOld.asUInt
696  }
697  def vxrm_rfn(rdata: UInt): UInt = rdata(2,1)
698
699  def vxsat_wfn(update: Boolean)(wdata: UInt): UInt = {
700    val vcsrOld = WireInit(vcsr.asTypeOf(new VcsrStruct))
701    val vcsrNew = WireInit(vcsrOld)
702    csrw_dirty_vs_state := true.B
703    if (update) {
704      vcsrNew.vxsat := wdata(0) | vcsrOld.vxsat
705    } else {
706      vcsrNew.vxsat := wdata(0)
707    }
708    vcsrNew.asUInt
709  }
710  def vxsat_rfn(rdata: UInt): UInt = rdata(0)
711
712  def vcsr_wfn(wdata: UInt): UInt = {
713    val vcsrOld = WireInit(vcsr.asTypeOf(new VcsrStruct))
714    csrw_dirty_vs_state := true.B
715    vcsrOld.vxrm := wdata.asTypeOf(vcsrOld).vxrm
716    vcsrOld.vxsat := wdata.asTypeOf(vcsrOld).vxsat
717    vcsrOld.asUInt
718  }
719
720  val vcsrMapping = Map(
721    MaskedRegMap(Vstart, vstart),
722    MaskedRegMap(Vxrm, vcsr, wfn = vxrm_wfn, rfn = vxrm_rfn),
723    MaskedRegMap(Vxsat, vcsr, wfn = vxsat_wfn(false), rfn = vxsat_rfn),
724    MaskedRegMap(Vcsr, vcsr, wfn = vcsr_wfn),
725    MaskedRegMap(Vl, vl),
726    MaskedRegMap(Vtype, vtype),
727    MaskedRegMap(Vlenb, vlenb),
728  )
729
730  // Hart Privilege Mode
731  val privilegeMode = RegInit(UInt(2.W), ModeM)
732
733  //val perfEventscounten = List.fill(nrPerfCnts)(RegInit(false(Bool())))
734  // Perf Counter
735  val nrPerfCnts = 29  // 3...31
736  val privilegeModeOH = UIntToOH(privilegeMode)
737  val perfEventscounten = RegInit(0.U.asTypeOf(Vec(nrPerfCnts, Bool())))
738  val perfCnts   = List.fill(nrPerfCnts)(RegInit(0.U(XLEN.W)))
739  val perfEvents = List.fill(8)(RegInit("h0000000000".U(XLEN.W))) ++
740                   List.fill(8)(RegInit("h4010040100".U(XLEN.W))) ++
741                   List.fill(8)(RegInit("h8020080200".U(XLEN.W))) ++
742                   List.fill(5)(RegInit("hc0300c0300".U(XLEN.W)))
743  for (i <-0 until nrPerfCnts) {
744    perfEventscounten(i) := (perfEvents(i)(63,60) & privilegeModeOH).orR
745  }
746
747  val hpmEvents = Wire(Vec(numPCntHc * coreParams.L2NBanks, new PerfEvent))
748  for (i <- 0 until numPCntHc * coreParams.L2NBanks) {
749    hpmEvents(i) := csrio.perf.perfEventsHc(i)
750  }
751
752  // print perfEvents
753  val allPerfEvents = hpmEvents.map(x => (s"Hc", x.value))
754  if (printEventCoding) {
755    for (((name, inc), i) <- allPerfEvents.zipWithIndex) {
756      println("CSR perfEvents Set", name, inc, i)
757    }
758  }
759
760  val csrevents = perfEvents.slice(24, 29)
761  val hpm_hc = HPerfMonitor(csrevents, hpmEvents)
762  val mcountinhibit = RegInit(0.U(XLEN.W))
763  val mcycle = RegInit(0.U(XLEN.W))
764  mcycle := mcycle + 1.U
765  val minstret = RegInit(0.U(XLEN.W))
766  val perf_events = csrio.perf.perfEventsFrontend ++
767                    csrio.perf.perfEventsBackend ++
768                    csrio.perf.perfEventsLsu ++
769                    hpm_hc.getPerf
770  minstret := minstret + RegNext(csrio.perf.retiredInstr)
771  for(i <- 0 until 29){
772    perfCnts(i) := Mux(mcountinhibit(i+3) | !perfEventscounten(i), perfCnts(i), perfCnts(i) + perf_events(i).value)
773  }
774
775  // CSR reg map
776  val basicPrivMapping = Map(
777
778    // Unprivileged Floating-Point CSRs
779    // Has been mapped above
780
781    // TODO: support Unprivileged Counter/Timers CSRs ("Zicntr" and "Zihpm")
782    // Unprivileged Counter/Timers
783    MaskedRegMap(Cycle, mcycle),
784    // We don't support read time CSR.
785    // MaskedRegMap(Time, mtime),
786    MaskedRegMap(Instret, minstret),
787
788    //--- Supervisor Trap Setup ---
789    MaskedRegMap(Sstatus, mstatus, sstatusWmask, mstatusUpdateSideEffect, sstatusRmask),
790    // MaskedRegMap(Sedeleg, Sedeleg),
791    // MaskedRegMap(Sideleg, Sideleg),
792    MaskedRegMap(Sie, mie, sieMask, MaskedRegMap.NoSideEffect, sieMask),
793    MaskedRegMap(Stvec, stvec, stvecMask, MaskedRegMap.NoSideEffect, stvecMask),
794    MaskedRegMap(Scounteren, scounteren, scounterenMask),
795
796    //--- Supervisor Configuration ---
797    MaskedRegMap(Senvcfg, senvcfg),
798
799    //--- Supervisor Trap Handling ---
800    MaskedRegMap(Sscratch, sscratch),
801    MaskedRegMap(Sepc, sepc, sepcMask, MaskedRegMap.NoSideEffect, sepcMask),
802    MaskedRegMap(Scause, scause),
803    MaskedRegMap(Stval, stval),
804    MaskedRegMap(Sip, mipReg.asUInt, sipWMask, MaskedRegMap.NoSideEffect, sipMask, x => (mipWire.asUInt | x) & sipMask),
805
806    //--- Supervisor Protection and Translation ---
807    MaskedRegMap(Satp, satp, satpMask, MaskedRegMap.NoSideEffect, satpMask),
808
809    //--- Supervisor Custom Read/Write Registers
810    MaskedRegMap(Sbpctl, sbpctl),
811    MaskedRegMap(Spfctl, spfctl),
812    MaskedRegMap(Sfetchctl, sfetchctl),
813    MaskedRegMap(Slvpredctl, slvpredctl),
814    MaskedRegMap(Smblockctl, smblockctl),
815    MaskedRegMap(Srnctl, srnctl),
816
817    //--- Machine Information Registers ---
818    MaskedRegMap(Mvendorid, mvendorid, 0.U(XLEN.W), MaskedRegMap.Unwritable),
819    MaskedRegMap(Marchid, marchid, 0.U(XLEN.W), MaskedRegMap.Unwritable),
820    MaskedRegMap(Mimpid, mimpid, 0.U(XLEN.W), MaskedRegMap.Unwritable),
821    MaskedRegMap(Mhartid, mhartid, 0.U(XLEN.W), MaskedRegMap.Unwritable),
822    MaskedRegMap(Mconfigptr, mconfigptr, 0.U(XLEN.W), MaskedRegMap.Unwritable),
823
824    //--- Machine Configuration Registers ---
825    MaskedRegMap(Menvcfg, menvcfg),
826
827    //--- Machine Trap Setup ---
828    MaskedRegMap(Mstatus, mstatus, mstatusWMask, mstatusUpdateSideEffect),
829    MaskedRegMap(Misa, misa, 0.U, MaskedRegMap.Unwritable), // now whole misa is unchangeable
830    MaskedRegMap(Medeleg, medeleg, medelegWMask),
831    MaskedRegMap(Mideleg, mideleg, midelegWMask),
832    MaskedRegMap(Mie, mie, mieWMask),
833    MaskedRegMap(Mtvec, mtvec, mtvecMask, MaskedRegMap.NoSideEffect, mtvecMask),
834    MaskedRegMap(Mcounteren, mcounteren, mcounterenMask),
835
836    //--- Machine Trap Handling ---
837    MaskedRegMap(Mscratch, mscratch),
838    MaskedRegMap(Mepc, mepc, mepcMask, MaskedRegMap.NoSideEffect, mepcMask),
839    MaskedRegMap(Mcause, mcause),
840    MaskedRegMap(Mtval, mtval),
841    MaskedRegMap(Mip, mipReg.asUInt, mipWMask, MaskedRegMap.NoSideEffect, mipMask, x => (mipWire.asUInt | x) & mipMask),
842
843    //--- Trigger ---
844    MaskedRegMap(Tselect, tselectPhy, WritableMask, WriteTselect),
845    // Todo: support chain length = 2
846    MaskedRegMap(Tdata1, tdata1RegVec(tselectPhy),
847      WritableMask,
848      x => Tdata1Bundle.Write(x, tdata1RegVec(tselectPhy), newTriggerChainIsLegal, debug_mode = debugMode),
849      WritableMask,
850      x => Tdata1Bundle.Read(x)),
851    MaskedRegMap(Tdata2, tdata2RegVec(tselectPhy)),
852    MaskedRegMap(Tinfo, tinfo, 0.U(XLEN.W), MaskedRegMap.Unwritable),
853
854    //--- Debug Mode ---
855    MaskedRegMap(Dcsr, dcsr, dcsrMask, dcsrUpdateSideEffect),
856    MaskedRegMap(Dpc, dpc),
857    MaskedRegMap(Dscratch0, dscratch0),
858    MaskedRegMap(Dscratch1, dscratch1),
859    MaskedRegMap(Mcountinhibit, mcountinhibit),
860    MaskedRegMap(Mcycle, mcycle),
861    MaskedRegMap(Minstret, minstret),
862  )
863
864  // hypervisor csr map
865  val hcsrMapping = Map(
866    //--- Hypervisor Trap Setup ---
867    MaskedRegMap(Hstatus, hstatus, hstatusWMask),
868    MaskedRegMap(Hedeleg, hedeleg),
869    MaskedRegMap(Hideleg, hideleg, hidelegWMask, MaskedRegMap.NoSideEffect, hidelegRMask),
870    MaskedRegMap(Hie, mie, hieMask, MaskedRegMap.NoSideEffect, hieMask),
871    MaskedRegMap(Hcounteren, hcounteren, hcounterenMask),
872    MaskedRegMap(Hgeie, hgeie),
873
874    //--- Hypervisor Trap Handling ---
875    MaskedRegMap(Htval, htval),
876    MaskedRegMap(Hip, mipReg.asUInt, hipWMask, MaskedRegMap.NoSideEffect, hipRMask, x => (mipWire.asUInt | x) & hipRMask),
877    MaskedRegMap(Hvip, mipReg.asUInt, hvipMask, MaskedRegMap.NoSideEffect, hvipMask, x => (mipWire.asUInt | x) & hvipMask),
878    MaskedRegMap(Htinst, htinst),
879    MaskedRegMap(Hgeip, hgeip),
880
881    //--- Hypervisor Configuration ---
882    MaskedRegMap(Henvcfg, henvcfg),
883
884    //--- Hypervisor Protection and Translation ---
885    MaskedRegMap(Hgatp, hgatp, hgatpMask, MaskedRegMap.NoSideEffect, hgatpMask),
886
887    //--- Hypervisor Counter/Timer Virtualization Registers ---
888    // MaskedRegMap(Htimedelta, htimedelta),
889
890    //--- Virtual Supervisor Registers ---
891    MaskedRegMap(Vsstatus, vsstatus, rmask = sstatusRmask, wmask = sstatusWmask, wfn = vsstatusUpdateSideEffect),
892    MaskedRegMap(Vsie, mie, rmask = vsip_ie_Mask, wmask = vsip_ie_Mask),
893    MaskedRegMap(Vstvec, vstvec),
894    MaskedRegMap(Vsscratch, vsscratch),
895    MaskedRegMap(Vsepc, vsepc),
896    MaskedRegMap(Vscause, vscause),
897    MaskedRegMap(Vstval, vstval),
898    MaskedRegMap(Vsip, mipReg.asUInt, vsip_WMask, MaskedRegMap.NoSideEffect, vsip_ie_Mask, x => mipWire.asUInt | x),
899    MaskedRegMap(Vsatp, vsatp, satpMask, MaskedRegMap.NoSideEffect, satpMask),
900
901    //--- Machine Registers ---
902    MaskedRegMap(Mtval2, mtval2),
903    MaskedRegMap(Mtinst, mtinst),
904  )
905
906  val perfCntMapping = (0 until 29).map(i => {Map(
907    MaskedRegMap(addr = Mhpmevent3 +i,
908                 reg  = perfEvents(i),
909                 wmask = "hf87fff3fcff3fcff".U(XLEN.W)),
910    MaskedRegMap(addr = Mhpmcounter3 +i,
911                 reg = perfCnts(i)),
912    MaskedRegMap(addr = Hpmcounter3 + i,
913                 reg  = perfCnts(i))
914  )}).fold(Map())((a,b) => a ++ b)
915  // TODO: mechanism should be implemented later
916  // val MhpmcounterStart = Mhpmcounter3
917  // val MhpmeventStart   = Mhpmevent3
918  // for (i <- 0 until nrPerfCnts) {
919  //   perfCntMapping += MaskedRegMap(MhpmcounterStart + i, perfCnts(i))
920  //   perfCntMapping += MaskedRegMap(MhpmeventStart + i, perfEvents(i))
921  // }
922
923  val cacheopRegs = CacheInstrucion.CacheInsRegisterList.map{case (name, attribute) => {
924    name -> RegInit(0.U(attribute("width").toInt.W))
925  }}
926  val cacheopMapping = CacheInstrucion.CacheInsRegisterList.map{case (name, attribute) => {
927    MaskedRegMap(
928      Scachebase + attribute("offset").toInt,
929      cacheopRegs(name)
930    )
931  }}
932
933  val mapping = basicPrivMapping ++
934                perfCntMapping ++
935                pmpMapping ++
936                pmaMapping ++
937                (if (HasFPU) fcsrMapping else Nil) ++
938                (if (HasVPU) vcsrMapping else Nil) ++
939                (if (HasCustomCSRCacheOp) cacheopMapping else Nil) ++
940                (if (HasHExtension) hcsrMapping else Nil)
941
942
943  println("XiangShan CSR Lists")
944
945  for (addr <- mapping.keys.toSeq.sorted) {
946    println(f"$addr%#03x ${mapping(addr)._1}")
947  }
948
949  val vs_s_csr_map = List(
950    Sstatus.U  -> Vsstatus.U,
951    Sie.U      -> Vsie.U,
952    Stvec.U    -> Vstvec.U,
953    Sscratch.U -> Vsscratch.U,
954    Sepc.U     -> Vsepc.U,
955    Scause.U   -> Vscause.U,
956    Stval.U    -> Vstval.U,
957    Sip.U      -> Vsip.U,
958    Satp.U     -> Vsatp.U
959  )
960  val addr = Wire(UInt(12.W))
961  val vscsr_addr = LookupTreeDefault(src2(11, 0), src2(11, 0), vs_s_csr_map)
962  when(virtMode){
963    addr := vscsr_addr
964  }.otherwise{
965    addr := src2(11, 0)
966  }
967  val csri = ZeroExt(src2(16, 12), XLEN)
968  val rdata = Wire(UInt(XLEN.W))
969  val rdata_tmp = Wire(UInt(XLEN.W))
970  val wdata_tmp = LookupTree(func, List(
971    CSROpType.wrt  -> src1,
972    CSROpType.set  -> (rdata | src1),
973    CSROpType.clr  -> (rdata & (~src1).asUInt),
974    CSROpType.wrti -> csri,
975    CSROpType.seti -> (rdata | csri),
976    CSROpType.clri -> (rdata & (~csri).asUInt)
977  ))
978  val is_vsip_ie = addr === Vsip.U || addr === Vsie.U
979  // for the difftest with NEMU(stay consistent with Spike)
980  val is_satp  = addr === Satp.U
981  val is_vsatp = addr === Vsatp.U
982  val is_hgatp = addr === Hgatp.U
983  val check_apt_mode = wdata_tmp(wdata_tmp.getWidth-1, 64-Satp_Mode_len) === 8.U || wdata_tmp(wdata_tmp.getWidth-1, 64-Satp_Mode_len) === 0.U
984  val wdata = MuxCase(wdata_tmp, Seq(
985    is_vsip_ie -> ZeroExt(wdata_tmp << 1, XLEN),
986    (is_satp && !check_apt_mode) -> satp,
987    (is_vsatp && !check_apt_mode) -> vsatp,
988    (is_hgatp && !check_apt_mode) -> hgatp
989  ))
990  val addrInPerfCnt = (addr >= Mcycle.U) && (addr <= Mhpmcounter31.U) ||
991    (addr >= Mcountinhibit.U) && (addr <= Mhpmevent31.U) ||
992    (addr >= Cycle.U) && (addr <= Hpmcounter31.U) ||
993    addr === Mip.U
994  csrio.isPerfCnt := addrInPerfCnt && valid && func =/= CSROpType.jmp
995
996  // satp wen check
997  val satpLegalMode = (wdata.asTypeOf(new SatpStruct).mode===0.U) || (wdata.asTypeOf(new SatpStruct).mode===8.U)
998
999  // csr access check, special case
1000  val tvmNotPermit = (privilegeMode === ModeS && !virtMode && mstatusStruct.tvm.asBool)
1001  val accessPermitted = !(addr === Satp.U && tvmNotPermit)
1002  val vtvmNotPermit = (privilegeMode === ModeS && virtMode && hstatusStruct.vtvm.asBool)
1003  val vaccessPermitted = !(addr === Vsatp.U && vtvmNotPermit)
1004//  csrio.disableSfence := (tvmNotPermit || !virtMode && privilegeMode < ModeS) || (vtvmNotPermit || virtMode && privilegeMode < ModeS)
1005//  csrio.disableHfenceg := !((!virtMode && privilegeMode === ModeS && !mstatusStruct.tvm.asBool) || (privilegeMode === ModeM)) // only valid in HS and mstatus.tvm == 0 or in M
1006//  csrio.disableHfencev :=  !(privilegeMode === ModeM || (!virtMode && privilegeMode === ModeS))
1007
1008  // general CSR wen check
1009  val wen = valid && CSROpType.isCsrAccess(func) && ((addr=/=Satp.U && addr =/= Vsatp.U) || satpLegalMode)
1010  val dcsrPermitted = dcsrPermissionCheck(addr, false.B, debugMode)
1011  val triggerPermitted = triggerPermissionCheck(addr, true.B, debugMode) // todo dmode
1012  val HasH = (HasHExtension == true).asBool
1013  val csrAccess = csrAccessPermissionCheck(addr, false.B, privilegeMode, virtMode, HasH)
1014  val modePermitted = csrAccess === 0.U && dcsrPermitted && triggerPermitted
1015  val perfcntPermitted = perfcntPermissionCheck(addr, privilegeMode, mcounteren, scounteren)
1016  val permitted = Mux(addrInPerfCnt, perfcntPermitted, modePermitted) && Mux(virtMode, vaccessPermitted, accessPermitted)
1017  MaskedRegMap.generate(mapping, addr, rdata_tmp, wen && permitted, wdata)
1018  rdata := Mux(is_vsip_ie, ZeroExt(rdata_tmp >> 1, XLEN), rdata_tmp)
1019  io.out.bits.res.data := rdata
1020  io.out.bits.ctrl.flushPipe.get := flushPipe
1021  connect0LatencyCtrlSingal
1022
1023  // send distribute csr a w signal
1024  csrio.customCtrl.distribute_csr.w.valid := wen && permitted
1025  csrio.customCtrl.distribute_csr.w.bits.data := wdata
1026  csrio.customCtrl.distribute_csr.w.bits.addr := addr
1027
1028  when (RegNext(csrio.fpu.fflags.valid)) {
1029    fcsr := fflags_wfn(update = true)(RegEnable(csrio.fpu.fflags.bits, csrio.fpu.fflags.valid))
1030  }
1031  when(RegNext(csrio.vpu.set_vxsat.valid)) {
1032    vcsr := vxsat_wfn(update = true)(RegEnable(csrio.vpu.set_vxsat.bits, csrio.vpu.set_vxsat.valid))
1033  }
1034
1035  // set fs and sd in mstatus
1036  when (csrw_dirty_fp_state || RegNext(csrio.fpu.dirty_fs)) {
1037    val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct))
1038    mstatusNew.fs := "b11".U
1039    mstatusNew.sd := true.B
1040    mstatus := mstatusNew.asUInt
1041    when(virtMode){
1042      val vsstatusNew = WireInit(vsstatus.asTypeOf(new MstatusStruct))
1043      vsstatusNew.fs := "b11".U
1044      vsstatusNew.sd := true.B
1045      vsstatus := vsstatusNew.asUInt
1046    }
1047  }
1048  csrio.fpu.frm := fcsr.asTypeOf(new FcsrStruct).frm
1049
1050  when (RegNext(csrio.vpu.set_vstart.valid)) {
1051    vstart := RegEnable(csrio.vpu.set_vstart.bits, csrio.vpu.set_vstart.valid)
1052  }
1053  when (RegNext(csrio.vpu.set_vtype.valid)) {
1054    vtype := RegEnable(csrio.vpu.set_vtype.bits, csrio.vpu.set_vtype.valid)
1055  }
1056  vl := csrio.vpu.vl
1057  // set vs and sd in mstatus
1058  when(csrw_dirty_vs_state || RegNext(csrio.vpu.dirty_vs)) {
1059    val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct))
1060    mstatusNew.vs := ContextStatus.dirty
1061    mstatusNew.sd := true.B
1062    mstatus := mstatusNew.asUInt
1063  }
1064
1065  csrio.vpu.vstart := vstart
1066  csrio.vpu.vxrm := vcsr.asTypeOf(new VcsrStruct).vxrm
1067
1068  // Trigger Ctrl
1069  val triggerEnableVec = tdata1RegVec.map { tdata1 =>
1070    val mcontrolData = tdata1.asTypeOf(new Tdata1Bundle).data.asTypeOf(new MControlData)
1071    tdata1.asTypeOf(new Tdata1Bundle).type_.asUInt === TrigTypeEnum.MCONTROL && (
1072      mcontrolData.m && privilegeMode === ModeM ||
1073        mcontrolData.s && privilegeMode === ModeS ||
1074        mcontrolData.u && privilegeMode === ModeU)
1075  }
1076  val fetchTriggerEnableVec = triggerEnableVec.zip(tdata1WireVec).map {
1077    case (tEnable, tdata1) => tEnable && tdata1.asTypeOf(new Tdata1Bundle).data.asTypeOf(new MControlData).isFetchTrigger
1078  }
1079  val memAccTriggerEnableVec = triggerEnableVec.zip(tdata1WireVec).map {
1080    case (tEnable, tdata1) => tEnable && tdata1.asTypeOf(new Tdata1Bundle).data.asTypeOf(new MControlData).isMemAccTrigger
1081  }
1082  csrio.customCtrl.frontend_trigger.tEnableVec := fetchTriggerEnableVec
1083  csrio.customCtrl.mem_trigger.tEnableVec := memAccTriggerEnableVec
1084
1085  val tdata1Update = wen && (addr === Tdata1.U)
1086  val tdata2Update = wen && (addr === Tdata2.U)
1087  val triggerUpdate = wen && (addr === Tdata1.U || addr === Tdata2.U)
1088  val frontendTriggerUpdate =
1089    tdata1Update && wdata.asTypeOf(new Tdata1Bundle).type_.asUInt === TrigTypeEnum.MCONTROL &&
1090      wdata.asTypeOf(new Tdata1Bundle).data.asTypeOf(new MControlData).isFetchTrigger ||
1091      tdata1Selected.data.asTypeOf(new MControlData).isFetchTrigger && triggerUpdate
1092  val memTriggerUpdate =
1093    tdata1Update && wdata.asTypeOf(new Tdata1Bundle).type_.asUInt === TrigTypeEnum.MCONTROL &&
1094      wdata.asTypeOf(new Tdata1Bundle).data.asTypeOf(new MControlData).isMemAccTrigger ||
1095      tdata1Selected.data.asTypeOf(new MControlData).isMemAccTrigger && triggerUpdate
1096
1097  csrio.customCtrl.frontend_trigger.tUpdate.valid := RegNext(RegNext(frontendTriggerUpdate))
1098  csrio.customCtrl.mem_trigger.tUpdate.valid := RegNext(RegNext(memTriggerUpdate))
1099  XSDebug(triggerEnableVec.reduce(_ || _), p"Debug Mode: At least 1 trigger is enabled," +
1100    p"trigger enable is ${Binary(triggerEnableVec.asUInt)}\n")
1101
1102  // CSR inst decode
1103  val isEbreak = addr === privEbreak && func === CSROpType.jmp
1104  val isEcall  = addr === privEcall  && func === CSROpType.jmp
1105  val isMret   = addr === privMret   && func === CSROpType.jmp
1106  val isSret   = addr === privSret   && func === CSROpType.jmp
1107  val isUret   = addr === privUret   && func === CSROpType.jmp
1108  val isDret   = addr === privDret   && func === CSROpType.jmp
1109  val isWFI    = func === CSROpType.wfi
1110
1111  // Illegal privileged operation list
1112  val illegalMret = valid && isMret && privilegeMode < ModeM
1113  val illegalSret = valid && isSret && privilegeMode < ModeS
1114  val illegalSModeSret = valid && isSret && privilegeMode === ModeS && virtMode === false.B && mstatusStruct.tsr.asBool
1115  // when hstatus.vtsr == 1, if sret is executed in VS-mode, it will cause virtual instruction
1116  val illegalVSModeSret = valid && isSret && privilegeMode === ModeS && virtMode && hstatusStruct.vtsr.asBool
1117  // When TW=1, then if WFI is executed in any less-privileged mode,
1118  // and it does not complete within an implementation-specific, bounded time limit,
1119  // the WFI instruction causes an illegal instruction exception.
1120  // The time limit may always be 0, in which case WFI always causes
1121  // an illegal instruction exception in less-privileged modes when TW=1.
1122  val illegalWFI = valid && isWFI && (privilegeMode < ModeM && mstatusStruct.tw === 1.U ||  privilegeMode === ModeU && !virtMode)
1123  val illegalVWFI = valid && isWFI && ((virtMode && privilegeMode === ModeS && hstatusStruct.vtw === 1.U && mstatusStruct.tw === 0.U)||
1124      (virtMode && privilegeMode === ModeU && mstatusStruct.tw === 0.U))
1125  // Illegal privileged instruction check
1126  val isIllegalAddr = valid && CSROpType.isCsrAccess(func) && MaskedRegMap.isIllegalAddr(mapping, addr)
1127  val isIllegalAccess = !virtMode && wen && !(Mux(addrInPerfCnt, perfcntPermitted, csrAccess === 0.U && dcsrPermitted && triggerPermitted) && accessPermitted)
1128  val isIllegalPrivOp = illegalMret || illegalSret || illegalSModeSret || illegalWFI
1129
1130  val isIllegalVAccess = virtMode && wen && (csrAccess === 2.U || !vaccessPermitted)
1131  val isIllegalVPrivOp = illegalVSModeSret || illegalVWFI
1132  // expose several csr bits for tlb
1133  tlbBundle.priv.mxr   := mstatusStruct.mxr.asBool
1134  tlbBundle.priv.sum   := mstatusStruct.sum.asBool
1135  tlbBundle.priv.vmxr := vsstatusStruct.mxr.asBool
1136  tlbBundle.priv.vsum := vsstatusStruct.sum.asBool
1137  tlbBundle.priv.spvp := hstatusStruct.spvp
1138  tlbBundle.priv.virt  := Mux(mstatusStruct.mprv.asBool, mstatusStruct.mpv & (mstatusStruct.mpp =/= ModeM), virtMode)
1139  tlbBundle.priv.imode := privilegeMode
1140  tlbBundle.priv.dmode := Mux((debugMode && dcsr.asTypeOf(new DcsrStruct).mprven || !debugMode) && mstatusStruct.mprv.asBool, mstatusStruct.mpp, privilegeMode)
1141
1142  // Branch control
1143  val retTarget = WireInit(0.U)
1144  val resetSatp = (addr === Satp.U || addr === Hgatp.U || addr === Vsatp.U) && wen // write to satp will cause the pipeline be flushed
1145  val writeVstart = addr === Vstart.U && wen // write to vstart will cause the pipeline be flushed
1146  dontTouch(writeVstart)
1147
1148  val w_fcsr_change_rm = wen && addr === Fcsr.U && wdata(7, 5) =/= fcsr(7, 5)
1149  val w_frm_change_rm = wen && addr === Frm.U && wdata(2, 0) =/= fcsr(7, 5)
1150  val frm_change = w_fcsr_change_rm || w_frm_change_rm
1151  val isXRet = valid && func === CSROpType.jmp && !isEcall && !isEbreak
1152  flushPipe := resetSatp || frm_change || isXRet || frontendTriggerUpdate || writeVstart
1153
1154  private val illegalRetTarget = WireInit(false.B)
1155  when(valid) {
1156    when(isDret) {
1157      retTarget := dpc(VAddrBits - 1, 0)
1158    }.elsewhen(isMret && !illegalMret) {
1159      retTarget := mepc(VAddrBits - 1, 0)
1160    }.elsewhen(isSret && !illegalSret && !illegalSModeSret && !illegalVSModeSret) {
1161      retTarget := Mux(virtMode, vsepc(VAddrBits - 1, 0), sepc(VAddrBits - 1, 0))
1162    }.elsewhen(isUret) {
1163      retTarget := uepc(VAddrBits - 1, 0)
1164    }.otherwise {
1165      illegalRetTarget := true.B
1166    }
1167  }.otherwise {
1168    illegalRetTarget := true.B // when illegalRetTarget setted, retTarget should never be used
1169  }
1170
1171  // Mux tree for regs
1172  when(valid) {
1173    when(isDret) {
1174      val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct))
1175      val debugModeNew = WireInit(debugMode)
1176      when(dcsr.asTypeOf(new DcsrStruct).prv =/= ModeM) {
1177        mstatusNew.mprv := 0.U
1178      } //If the new privilege mode is less privileged than M-mode, MPRV in mstatus is cleared.
1179      mstatus := mstatusNew.asUInt
1180      privilegeMode := dcsr.asTypeOf(new DcsrStruct).prv
1181      debugModeNew := false.B
1182      debugIntrEnable := true.B
1183      debugMode := debugModeNew
1184      XSDebug("Debug Mode: Dret executed, returning to %x.", retTarget)
1185    }.elsewhen(isMret && !illegalMret) {
1186      val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct))
1187      val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct))
1188      mstatusNew.ie.m := mstatusOld.pie.m
1189      privilegeMode := mstatusOld.mpp
1190      if (HasHExtension) {
1191        virtMode := mstatusOld.mpv
1192        mstatusNew.mpv := 0.U
1193      }
1194      mstatusNew.pie.m := true.B
1195      mstatusNew.mpp := ModeU
1196      when(mstatusOld.mpp =/= ModeM) {
1197        mstatusNew.mprv := 0.U
1198      }
1199      mstatus := mstatusNew.asUInt
1200    }.elsewhen(isSret && !illegalSret && !illegalSModeSret && !illegalVSModeSret) {
1201      val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct))
1202      val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct))
1203      val hstatusOld = WireInit(hstatus.asTypeOf(new HstatusStruct))
1204      val hstatusNew = WireInit(hstatus.asTypeOf(new HstatusStruct))
1205      val vsstatusOld = WireInit(vsstatus.asTypeOf(new MstatusStruct))
1206      val vsstatusNew = WireInit(vsstatus.asTypeOf(new MstatusStruct))
1207      when(virtMode === 0.U) {
1208        virtMode := hstatusOld.spv
1209        hstatusNew.spv := 0.U
1210        mstatusNew.ie.s := mstatusOld.pie.s
1211        privilegeMode := Cat(0.U(1.W), mstatusOld.spp)
1212        mstatusNew.pie.s := true.B
1213        mstatusNew.spp := ModeU
1214        when(mstatusOld.spp =/= ModeM) {
1215          mstatusNew.mprv := 0.U
1216        }
1217        mstatus := mstatusNew.asUInt
1218        hstatus := hstatusNew.asUInt
1219      }.otherwise {
1220        privilegeMode := vsstatusOld.spp
1221        vsstatusNew.spp := ModeU
1222        vsstatusNew.ie.s := vsstatusOld.pie.s
1223        vsstatusNew.pie.s := 1.U
1224        vsstatus := vsstatusNew.asUInt
1225      }
1226    }.elsewhen(isUret) {
1227      val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct))
1228      val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct))
1229      // mstatusNew.mpp.m := ModeU //TODO: add mode U
1230      mstatusNew.ie.u := mstatusOld.pie.u
1231      privilegeMode := ModeU
1232      mstatusNew.pie.u := true.B
1233      mstatus := mstatusNew.asUInt
1234    }
1235  }
1236
1237  io.in.ready := true.B
1238  io.out.valid := valid
1239
1240  // In this situation, hart will enter debug mode instead of handling a breakpoint exception simply.
1241  // Ebreak block instructions backwards, so it's ok to not keep extra info to distinguish between breakpoint
1242  // exception and enter-debug-mode exception.
1243  val ebreakEnterDebugMode =
1244    (privilegeMode === ModeM && dcsrData.ebreakm) ||
1245    (privilegeMode === ModeS && dcsrData.ebreaks) ||
1246    (privilegeMode === ModeU && dcsrData.ebreaku)
1247
1248  // raise a debug exception waiting to enter debug mode, instead of a breakpoint exception
1249  val raiseDebugException = !debugMode && isEbreak && ebreakEnterDebugMode
1250
1251  val csrExceptionVec = WireInit(0.U.asTypeOf(ExceptionVec()))
1252  csrExceptionVec(breakPoint) := io.in.valid && isEbreak
1253  csrExceptionVec(ecallM) := privilegeMode === ModeM && io.in.valid && isEcall
1254  csrExceptionVec(ecallVS) := privilegeMode === ModeS && virtMode && io.in.valid && isEcall
1255  csrExceptionVec(ecallS) := privilegeMode === ModeS && !virtMode && io.in.valid && isEcall
1256  csrExceptionVec(ecallU) := privilegeMode === ModeU && io.in.valid && isEcall
1257  // Trigger an illegal instr exception when:
1258  // * unimplemented csr is being read/written
1259  // * csr access is illegal
1260  csrExceptionVec(illegalInstr) := isIllegalAddr || isIllegalAccess || isIllegalPrivOp
1261  csrExceptionVec(virtualInstr) := isIllegalVAccess || isIllegalVPrivOp
1262  io.out.bits.ctrl.exceptionVec.get := csrExceptionVec
1263
1264  XSDebug(io.in.valid, s"Debug Mode: an Ebreak is executed, ebreak cause enter-debug-mode exception ? ${raiseDebugException}\n")
1265
1266  /**
1267    * Exception and Intr
1268    */
1269  val idelegS =  (mideleg & mip.asUInt)
1270  val idelegVS = (hideleg & mideleg & mip.asUInt)
1271  def privilegedEnableDetect(idelegS: Bool, idelegVS: Bool): Bool = Mux(idelegS,
1272    Mux(idelegVS, (virtMode && privilegeMode === ModeS && vsstatusStruct.ie.s) || (virtMode && privilegeMode < ModeS),
1273      ((privilegeMode === ModeS) && mstatusStruct.ie.s) || (privilegeMode < ModeS) || virtMode),
1274    ((privilegeMode === ModeM) && mstatusStruct.ie.m) || (privilegeMode < ModeM))
1275
1276  val debugIntr = csrio.externalInterrupt.debug & debugIntrEnable
1277  XSDebug(debugIntr, "Debug Mode: debug interrupt is asserted and valid!")
1278  // send interrupt information to ROB
1279  val intrVecEnable = Wire(Vec(13, Bool()))
1280  val disableInterrupt = debugMode || (dcsrData.step && !dcsrData.stepie)
1281  intrVecEnable.zip(idelegS.asBools).zip(idelegVS.asBools).map{case((x,y),z) => x := privilegedEnableDetect(y, z) && !disableInterrupt}
1282  val intrVec = Cat(debugIntr && !debugMode, (mie(11,0) & mip.asUInt & intrVecEnable.asUInt))
1283  val intrBitSet = intrVec.orR
1284  csrio.interrupt := intrBitSet
1285  // Page 45 in RISC-V Privileged Specification
1286  // The WFI instruction can also be executed when interrupts are disabled. The operation of WFI
1287  // must be unaffected by the global interrupt bits in mstatus (MIE and SIE) and the delegation
1288  // register mideleg, but should honor the individual interrupt enables (e.g, MTIE).
1289  csrio.wfi_event := debugIntr || (mie(11, 0) & mip.asUInt).orR
1290  mipWire.t.m := csrio.externalInterrupt.mtip
1291  mipWire.s.m := csrio.externalInterrupt.msip
1292  mipWire.e.m := csrio.externalInterrupt.meip
1293  mipWire.e.s := csrio.externalInterrupt.seip
1294
1295  // interrupts
1296  val intrNO = IntPriority.foldRight(0.U)((i: Int, sum: UInt) => Mux(intrVec(i), i.U, sum))
1297  val hasIntr = csrio.exception.valid && csrio.exception.bits.isInterrupt
1298  val ivmEnable = tlbBundle.priv.imode < ModeM && satp.asTypeOf(new SatpStruct).mode === 8.U
1299  val iexceptionPC = Mux(ivmEnable, SignExt(csrio.exception.bits.pc, XLEN), csrio.exception.bits.pc)
1300  val iexceptionGPAddr = Mux(ivmEnable, SignExt(csrio.exception.bits.gpaddr, XLEN), csrio.exception.bits.gpaddr)
1301  val dvmEnable = tlbBundle.priv.dmode < ModeM && satp.asTypeOf(new SatpStruct).mode === 8.U
1302  val dexceptionPC = Mux(dvmEnable, SignExt(csrio.exception.bits.pc, XLEN), csrio.exception.bits.pc)
1303  XSDebug(hasIntr, "interrupt: pc=0x%x, %d\n", dexceptionPC, intrNO)
1304  val hasDebugIntr = intrNO === IRQ_DEBUG.U && hasIntr
1305
1306  // exceptions from rob need to handle
1307  val exceptionVecFromRob    = csrio.exception.bits.exceptionVec
1308  val hasException           = csrio.exception.valid && !csrio.exception.bits.isInterrupt
1309  val hasInstrPageFault      = hasException && exceptionVecFromRob(instrPageFault)
1310  val hasLoadPageFault       = hasException && exceptionVecFromRob(loadPageFault)
1311  val hasStorePageFault      = hasException && exceptionVecFromRob(storePageFault)
1312  val hasStoreAddrMisalign   = hasException && exceptionVecFromRob(storeAddrMisaligned)
1313  val hasLoadAddrMisalign    = hasException && exceptionVecFromRob(loadAddrMisaligned)
1314  val hasInstrAccessFault    = hasException && exceptionVecFromRob(instrAccessFault)
1315  val hasLoadAccessFault     = hasException && exceptionVecFromRob(loadAccessFault)
1316  val hasStoreAccessFault    = hasException && exceptionVecFromRob(storeAccessFault)
1317  val hasBreakPoint          = hasException && exceptionVecFromRob(breakPoint)
1318  val hasInstGuestPageFault  = hasException && exceptionVecFromRob(instrGuestPageFault)
1319  val hasLoadGuestPageFault  = hasException && exceptionVecFromRob(loadGuestPageFault)
1320  val hasStoreGuestPageFault = hasException && exceptionVecFromRob(storeGuestPageFault)
1321  val hasSingleStep          = hasException && csrio.exception.bits.singleStep
1322  val hasTriggerFire         = hasException && csrio.exception.bits.trigger.canFire
1323  val triggerFrontendHitVec = csrio.exception.bits.trigger.frontendHit
1324  val triggerMemHitVec = csrio.exception.bits.trigger.backendHit
1325  val triggerHitVec = triggerFrontendHitVec | triggerMemHitVec // Todo: update mcontrol.hit
1326  val triggerCanFireVec = csrio.exception.bits.trigger.frontendCanFire | csrio.exception.bits.trigger.backendCanFire
1327  // More than one triggers can hit at the same time, but only fire one
1328  // We select the first hit trigger to fire
1329  val triggerFireOH = PriorityEncoderOH(triggerCanFireVec)
1330  val triggerFireAction = PriorityMux(triggerFireOH, tdata1WireVec.map(_.getTriggerAction)).asUInt
1331
1332
1333  XSDebug(hasSingleStep, "Debug Mode: single step exception\n")
1334  XSDebug(hasTriggerFire, p"Debug Mode: trigger fire, frontend hit vec ${Binary(csrio.exception.bits.trigger.frontendHit.asUInt)} " +
1335    p"backend hit vec ${Binary(csrio.exception.bits.trigger.backendHit.asUInt)}\n")
1336
1337  val hasExceptionVec = csrio.exception.bits.exceptionVec
1338  val regularExceptionNO = ExceptionNO.priorities.foldRight(0.U)((i: Int, sum: UInt) => Mux(hasExceptionVec(i), i.U, sum))
1339  val exceptionNO = Mux(hasSingleStep || hasTriggerFire, 3.U, regularExceptionNO)
1340  val causeNO = (hasIntr << (XLEN - 1)).asUInt | Mux(hasIntr, intrNO, exceptionNO)
1341
1342  val hasExceptionIntr = csrio.exception.valid
1343
1344  val hasDebugEbreakException = hasBreakPoint && ebreakEnterDebugMode
1345  val hasDebugTriggerException = hasTriggerFire && triggerFireAction === TrigActionEnum.DEBUG_MODE
1346  val hasDebugException = hasDebugEbreakException || hasDebugTriggerException || hasSingleStep
1347  val hasDebugTrap = hasDebugException || hasDebugIntr
1348  val ebreakEnterParkLoop = debugMode && hasExceptionIntr
1349
1350  XSDebug(hasExceptionIntr, "int/exc: pc %x int (%d):%x exc: (%d):%x\n",
1351    dexceptionPC, intrNO, intrVec, exceptionNO, hasExceptionVec.asUInt
1352  )
1353  XSDebug(hasExceptionIntr,
1354    "pc %x mstatus %x mideleg %x medeleg %x mode %x\n",
1355    dexceptionPC,
1356    mstatus,
1357    mideleg,
1358    medeleg,
1359    privilegeMode
1360  )
1361
1362  // mtval write logic
1363  // Due to timing reasons of memExceptionVAddr, we delay the write of mtval and stval
1364  val memExceptionAddr = SignExt(csrio.memExceptionVAddr, XLEN)
1365  val memExceptionGPAddr = SignExt(csrio.memExceptionGPAddr, XLEN)
1366  val updateTval = VecInit(Seq(
1367    hasInstrPageFault,
1368    hasLoadPageFault,
1369    hasStorePageFault,
1370    hasInstrAccessFault,
1371    hasLoadAccessFault,
1372    hasStoreAccessFault,
1373    hasLoadAddrMisalign,
1374    hasStoreAddrMisalign,
1375    hasInstGuestPageFault,
1376    hasLoadGuestPageFault,
1377    hasStoreGuestPageFault,
1378    hasBreakPoint,
1379  )).asUInt.orR
1380  val updateTval_h = VecInit(Seq(
1381    hasInstGuestPageFault,
1382    hasLoadGuestPageFault,
1383    hasStoreGuestPageFault
1384  )).asUInt.orR
1385  when (RegNext(RegNext(updateTval))) {
1386      val tval = Mux(
1387        RegNext(RegNext(hasInstrPageFault || hasInstrAccessFault || hasInstGuestPageFault || hasBreakPoint)),
1388        RegNext(RegNext(Mux(
1389          csrio.exception.bits.crossPageIPFFix,
1390          SignExt(csrio.exception.bits.pc + 2.U, XLEN),
1391          iexceptionPC
1392        ))),
1393        memExceptionAddr
1394    )
1395    // because we update tval two beats later, we can choose xtval according to the privilegeMode which has been updated
1396    when (RegNext(privilegeMode === ModeM)) {
1397      mtval := tval
1398    }.otherwise {
1399      when (virtMode){
1400        vstval := tval
1401      }.otherwise{
1402        stval := tval
1403      }
1404    }
1405  }
1406
1407  when(RegNext(RegNext(updateTval_h))) {
1408    val tval_tmp = Mux(
1409      RegNext(RegNext(hasInstGuestPageFault)),
1410      RegNext(RegNext(Mux(
1411        csrio.exception.bits.crossPageIPFFix,
1412        SignExt(csrio.exception.bits.gpaddr + 2.U, XLEN),
1413        iexceptionGPAddr
1414      ))),
1415      memExceptionGPAddr
1416    )
1417    val tval = tval_tmp >> 2
1418    when(RegNext(privilegeMode === ModeM)) {
1419      mtval2 := tval
1420    }.otherwise {
1421      htval := tval
1422    }
1423  }
1424
1425  val debugTrapTarget = Mux(!isEbreak && debugMode, 0x38020808.U, 0x38020800.U) // 0x808 is when an exception occurs in debug mode prog buf exec
1426  val deleg = Mux(hasIntr, mideleg , medeleg)
1427  val hdeleg = Mux(hasIntr, hideleg, hedeleg)
1428  // val delegS = ((deleg & (1 << (causeNO & 0xf))) != 0) && (privilegeMode < ModeM);
1429  val delegS = deleg(causeNO(7,0)) && (privilegeMode < ModeM)
1430  val delegVS = virtMode && delegS && hdeleg(causeNO(7, 0)) && (privilegeMode < ModeM)
1431  val clearTval = !updateTval || hasIntr
1432
1433  val clearTval_h = !updateTval_h || hasIntr
1434  val isHyperInst = csrio.exception.bits.isHls
1435  // ctrl block will use theses later for flush
1436  val isXRetFlag = RegInit(false.B)
1437  when (DelayN(io.flush.valid, 5)) {
1438    isXRetFlag := false.B
1439  }.elsewhen (isXRet) {
1440    isXRetFlag := true.B
1441  }
1442  csrio.isXRet := isXRetFlag
1443  private val retTargetReg = RegEnable(retTarget, isXRet && !illegalRetTarget)
1444  private val illegalXret = RegEnable(illegalMret || illegalSret || illegalSModeSret || illegalVSModeSret, isXRet)
1445
1446  private val xtvec = Mux(delegS, Mux(delegVS, vstvec, stvec), mtvec)
1447  private val xtvecBase = xtvec(VAddrBits - 1, 2)
1448  // When MODE=Vectored, all synchronous exceptions into M/S mode
1449  // cause the pc to be set to the address in the BASE field, whereas
1450  // interrupts cause the pc to be set to the address in the BASE field
1451  // plus four times the interrupt cause number.
1452  private val pcFromXtvec = Cat(xtvecBase + Mux(xtvec(0) && hasIntr, causeNO(3, 0), 0.U), 0.U(2.W))
1453
1454  // XRet sends redirect instead of Flush and isXRetFlag is true.B before redirect.valid.
1455  // ROB sends exception at T0 while CSR receives at T2.
1456  // We add a RegNext here and trapTarget is valid at T3.
1457  csrio.trapTarget := RegEnable(
1458    MuxCase(pcFromXtvec, Seq(
1459      (isXRetFlag && !illegalXret) -> retTargetReg,
1460      ((hasDebugTrap && !debugMode) || ebreakEnterParkLoop) -> debugTrapTarget
1461    )),
1462    isXRetFlag || csrio.exception.valid)
1463
1464  when(hasExceptionIntr) {
1465    val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct))
1466    val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct))
1467    val hstatusOld = WireInit(hstatus.asTypeOf(new HstatusStruct))
1468    val hstatusNew = WireInit(hstatus.asTypeOf(new HstatusStruct))
1469    val vsstatusOld = WireInit(vsstatus.asTypeOf(new MstatusStruct))
1470    val vsstatusNew = WireInit(vsstatus.asTypeOf(new MstatusStruct))
1471    val dcsrNew = WireInit(dcsr.asTypeOf(new DcsrStruct))
1472    val debugModeNew = WireInit(debugMode)
1473    when(hasDebugTrap && !debugMode) {
1474      import DcsrStruct._
1475      debugModeNew := true.B
1476      dcsrNew.prv := privilegeMode
1477      privilegeMode := ModeM
1478      when(hasDebugIntr) {
1479        dpc := iexceptionPC
1480        dcsrNew.cause := CAUSE_HALTREQ
1481        XSDebug(hasDebugIntr, "Debug Mode: Trap to %x at pc %x\n", debugTrapTarget, dpc)
1482      }.otherwise { // hasDebugException
1483        dpc := iexceptionPC // TODO: check it when hasSingleStep
1484        dcsrNew.cause := MuxCase(0.U, Seq(
1485          hasTriggerFire -> CAUSE_TRIGGER,
1486          raiseDebugException -> CAUSE_EBREAK,
1487          hasBreakPoint -> CAUSE_HALTREQ,
1488          hasSingleStep -> CAUSE_STEP
1489        ))
1490      }
1491      dcsr := dcsrNew.asUInt
1492      debugIntrEnable := false.B
1493    }.elsewhen (debugMode) {
1494      //do nothing
1495    }.elsewhen (delegVS) {
1496      vscause := (hasIntr << (XLEN-1)).asUInt | Mux(hasIntr, intrNO - 1.U, exceptionNO)
1497      vsepc := Mux(hasInstrPageFault || hasInstrAccessFault, iexceptionPC, dexceptionPC)
1498      vsstatusNew.spp := privilegeMode
1499      vsstatusNew.pie.s := vsstatusOld.ie.s
1500      vsstatusNew.ie.s := false.B
1501      when (clearTval) {vstval := 0.U}
1502      virtMode := true.B
1503      privilegeMode := ModeS
1504    }.elsewhen (delegS) {
1505      val virt = Mux(mstatusOld.mprv.asBool, mstatusOld.mpv, virtMode)
1506      // to do hld st
1507      hstatusNew.gva := (hasInstGuestPageFault || hasLoadGuestPageFault || hasStoreGuestPageFault ||
1508                      ((virt.asBool || isHyperInst) && ((hasException && 0.U <= exceptionNO && exceptionNO <= 7.U && exceptionNO =/= 2.U)
1509                      || hasInstrPageFault || hasLoadPageFault || hasStorePageFault)))
1510      hstatusNew.spv := virtMode
1511      when(virtMode){
1512        hstatusNew.spvp := privilegeMode
1513      }
1514      virtMode := false.B
1515      scause := causeNO
1516      sepc := Mux(hasInstrPageFault || hasInstrAccessFault, iexceptionPC, dexceptionPC)
1517      mstatusNew.spp := privilegeMode
1518      mstatusNew.pie.s := mstatusOld.ie.s
1519      mstatusNew.ie.s := false.B
1520      privilegeMode := ModeS
1521      when (clearTval) { stval := 0.U }
1522      when (clearTval_h) {htval := 0.U}
1523    }.otherwise {
1524      val virt = Mux(mstatusOld.mprv.asBool, mstatusOld.mpv, virtMode)
1525      // to do hld st
1526      mstatusNew.gva := (hasInstGuestPageFault || hasLoadGuestPageFault || hasStoreGuestPageFault ||
1527      ((virt.asBool || isHyperInst) && ((hasException && 0.U <= exceptionNO && exceptionNO <= 7.U && exceptionNO =/= 2.U)
1528        || hasInstrPageFault || hasLoadPageFault || hasStorePageFault)))
1529      mstatusNew.mpv := virtMode
1530      virtMode := false.B
1531      mcause := causeNO
1532      mepc := Mux(hasInstrPageFault || hasInstrAccessFault, iexceptionPC, dexceptionPC)
1533      mstatusNew.mpp := privilegeMode
1534      mstatusNew.pie.m := mstatusOld.ie.m
1535      mstatusNew.ie.m := false.B
1536      privilegeMode := ModeM
1537      when (clearTval) { mtval := 0.U }
1538      when (clearTval_h) {mtval2 := 0.U}
1539    }
1540    mstatus := mstatusNew.asUInt
1541    vsstatus := vsstatusNew.asUInt
1542    hstatus := hstatusNew.asUInt
1543    debugMode := debugModeNew
1544  }
1545
1546  // Cache error debug support
1547  if(HasCustomCSRCacheOp){
1548    val cache_error_decoder = Module(new CSRCacheErrorDecoder)
1549    cache_error_decoder.io.encoded_cache_error := cacheopRegs("CACHE_ERROR")
1550  }
1551
1552  // Implicit add reset values for mepc[0] and sepc[0]
1553  // TODO: rewrite mepc and sepc using a struct-like style with the LSB always being 0
1554  when (RegNext(RegNext(reset.asBool) && !reset.asBool)) {
1555    mepc := Cat(mepc(XLEN - 1, 1), 0.U(1.W))
1556    sepc := Cat(sepc(XLEN - 1, 1), 0.U(1.W))
1557    vsepc := Cat(vsepc(XLEN - 1, 1), 0.U(1.W))
1558  }
1559
1560  def readWithScala(addr: Int): UInt = mapping(addr)._1
1561
1562  val difftestIntrNO = Mux(hasIntr, causeNO, 0.U)
1563
1564  // Always instantiate basic difftest modules.
1565  if (env.AlwaysBasicDiff || env.EnableDifftest) {
1566    val difftest = DifftestModule(new DiffArchEvent, delay = 3, dontCare = true)
1567    difftest.coreid      := csrio.hartId
1568    difftest.valid       := csrio.exception.valid
1569    difftest.interrupt   := Mux(hasIntr, causeNO, 0.U)
1570    difftest.exception   := Mux(hasException, causeNO, 0.U)
1571    difftest.exceptionPC := dexceptionPC
1572    if (env.EnableDifftest) {
1573      difftest.exceptionInst := csrio.exception.bits.instr
1574    }
1575  }
1576
1577  // Always instantiate basic difftest modules.
1578  if (env.AlwaysBasicDiff || env.EnableDifftest) {
1579    val difftest = DifftestModule(new DiffCSRState)
1580    difftest.coreid := csrio.hartId
1581    difftest.privilegeMode := privilegeMode
1582    difftest.mstatus := mstatus
1583    difftest.sstatus := mstatus & sstatusRmask
1584    difftest.mepc := mepc
1585    difftest.sepc := sepc
1586    difftest.mtval:= mtval
1587    difftest.stval:= stval
1588    difftest.mtvec := mtvec
1589    difftest.stvec := stvec
1590    difftest.mcause := mcause
1591    difftest.scause := scause
1592    difftest.satp := satp
1593    difftest.mip := mipReg
1594    difftest.mie := mie
1595    difftest.mscratch := mscratch
1596    difftest.sscratch := sscratch
1597    difftest.mideleg := mideleg
1598    difftest.medeleg := medeleg
1599  }
1600
1601  if (env.AlwaysBasicDiff || env.EnableDifftest) {
1602    val difftest = DifftestModule(new DiffHCSRState)
1603    difftest.coreid := csrio.hartId
1604    difftest.virtMode := virtMode
1605    difftest.mtval2 := mtval2
1606    difftest.mtinst := mtinst
1607    difftest.hstatus := hstatus
1608    difftest.hideleg := hideleg
1609    difftest.hedeleg := hedeleg
1610    difftest.hcounteren := hcounteren
1611    difftest.htval := htval
1612    difftest.htinst := htinst
1613    difftest.hgatp := hgatp
1614    difftest.vsstatus := vsstatus
1615    difftest.vstvec := vstvec
1616    difftest.vsepc := vsepc
1617    difftest.vscause := vscause
1618    difftest.vstval := vstval
1619    difftest.vsatp := vsatp
1620    difftest.vsscratch := vsscratch
1621  }
1622
1623  if(env.AlwaysBasicDiff || env.EnableDifftest) {
1624    val difftest = DifftestModule(new DiffDebugMode)
1625    difftest.coreid := csrio.hartId
1626    difftest.debugMode := debugMode
1627    difftest.dcsr := dcsr
1628    difftest.dpc := dpc
1629    difftest.dscratch0 := dscratch0
1630    difftest.dscratch1 := dscratch1
1631  }
1632
1633  if (env.AlwaysBasicDiff || env.EnableDifftest) {
1634    val difftest = DifftestModule(new DiffVecCSRState)
1635    difftest.coreid := csrio.hartId
1636    difftest.vstart := vstart
1637    difftest.vxsat := vcsr.asTypeOf(new VcsrStruct).vxsat
1638    difftest.vxrm := vcsr.asTypeOf(new VcsrStruct).vxrm
1639    difftest.vcsr := vcsr
1640    difftest.vl := vl
1641    difftest.vtype := vtype
1642    difftest.vlenb := vlenb
1643  }
1644}
1645*/
1646class PFEvent(implicit p: Parameters) extends XSModule with HasCSRConst  {
1647  val io = IO(new Bundle {
1648    val distribute_csr = Flipped(new DistributedCSRIO())
1649    val hpmevent = Output(Vec(29, UInt(XLEN.W)))
1650  })
1651
1652  val w = io.distribute_csr.w
1653
1654  val perfEvents = List.fill(8)(RegInit("h0000000000".U(XLEN.W))) ++
1655                   List.fill(8)(RegInit("h4010040100".U(XLEN.W))) ++
1656                   List.fill(8)(RegInit("h8020080200".U(XLEN.W))) ++
1657                   List.fill(5)(RegInit("hc0300c0300".U(XLEN.W)))
1658
1659  val perfEventMapping = (0 until 29).map(i => {Map(
1660    MaskedRegMap(addr = Mhpmevent3 +i,
1661                 reg  = perfEvents(i),
1662                 wmask = "hf87fff3fcff3fcff".U(XLEN.W))
1663  )}).fold(Map())((a,b) => a ++ b)
1664
1665  val rdata = Wire(UInt(XLEN.W))
1666  MaskedRegMap.generate(perfEventMapping, w.bits.addr, rdata, w.valid, w.bits.data)
1667  for(i <- 0 until 29){
1668    io.hpmevent(i) := perfEvents(i)
1669  }
1670}
1671