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