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