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