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