CSR.scala 30.9 KB
Newer Older
L
Add CSR  
LinJiawei 已提交
1 2 3
package xiangshan.backend.fu

import chisel3._
4
import chisel3.ExcitingUtils.{ConnectionType, Debug}
L
Add CSR  
LinJiawei 已提交
5 6 7 8 9
import chisel3.util._
import fpu.Fflags
import utils._
import xiangshan._
import xiangshan.backend._
L
LinJiawei 已提交
10
import utils.XSDebug
L
Add CSR  
LinJiawei 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

trait HasCSRConst {
  // User Trap Setup
  val Ustatus       = 0x000
  val Uie           = 0x004
  val Utvec         = 0x005

  // User Trap Handling
  val Uscratch      = 0x040
  val Uepc          = 0x041
  val Ucause        = 0x042
  val Utval         = 0x043
  val Uip           = 0x044

  // User Floating-Point CSRs (not implemented)
  val Fflags        = 0x001
  val Frm           = 0x002
  val Fcsr          = 0x003

  // User Counter/Timers
  val Cycle         = 0xC00
  val Time          = 0xC01
  val Instret       = 0xC02

  // Supervisor Trap Setup
  val Sstatus       = 0x100
  val Sedeleg       = 0x102
  val Sideleg       = 0x103
  val Sie           = 0x104
  val Stvec         = 0x105
  val Scounteren    = 0x106

  // Supervisor Trap Handling
  val Sscratch      = 0x140
  val Sepc          = 0x141
  val Scause        = 0x142
  val Stval         = 0x143
  val Sip           = 0x144

  // Supervisor Protection and Translation
  val Satp          = 0x180

53 54 55 56 57
  // Machine Information Registers
  val Mvendorid     = 0xF11
  val Marchid       = 0xF12
  val Mimpid        = 0xF13
  val Mhartid       = 0xF14
L
Add CSR  
LinJiawei 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

  // Machine Trap Setup
  val Mstatus       = 0x300
  val Misa          = 0x301
  val Medeleg       = 0x302
  val Mideleg       = 0x303
  val Mie           = 0x304
  val Mtvec         = 0x305
  val Mcounteren    = 0x306

  // Machine Trap Handling
  val Mscratch      = 0x340
  val Mepc          = 0x341
  val Mcause        = 0x342
  val Mtval         = 0x343
  val Mip           = 0x344

  // Machine Memory Protection
  // TBD
  val Pmpcfg0       = 0x3A0
  val Pmpcfg1       = 0x3A1
  val Pmpcfg2       = 0x3A2
  val Pmpcfg3       = 0x3A3
  val PmpaddrBase   = 0x3B0

  // Machine Counter/Timers
84
  // Currently, we uses perfcnt csr set instead of standard Machine Counter/Timers
L
Add CSR  
LinJiawei 已提交
85 86 87 88 89 90
  // 0xB80 - 0x89F are also used as perfcnt csr

  // Machine Counter Setup (not implemented)
  // Debug/Trace Registers (shared with Debug Mode) (not implemented)
  // Debug Mode Registers (not implemented)

W
William Wang 已提交
91 92 93 94 95
  def privEcall  = 0x000.U
  def privEbreak = 0x001.U
  def privMret   = 0x302.U
  def privSret   = 0x102.U
  def privUret   = 0x002.U
L
Add CSR  
LinJiawei 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

  def ModeM     = 0x3.U
  def ModeH     = 0x2.U
  def ModeS     = 0x1.U
  def ModeU     = 0x0.U

  def IRQ_UEIP  = 0
  def IRQ_SEIP  = 1
  def IRQ_MEIP  = 3

  def IRQ_UTIP  = 4
  def IRQ_STIP  = 5
  def IRQ_MTIP  = 7

  def IRQ_USIP  = 8
  def IRQ_SSIP  = 9
  def IRQ_MSIP  = 11

  val IntPriority = Seq(
    IRQ_MEIP, IRQ_MSIP, IRQ_MTIP,
    IRQ_SEIP, IRQ_SSIP, IRQ_STIP,
    IRQ_UEIP, IRQ_USIP, IRQ_UTIP
  )
W
William Wang 已提交
119 120 121 122 123 124

  def csrAccessPermissionCheck(addr: UInt, wen: Bool, mode: UInt): Bool = {
    val readOnly = addr(11,10) === "b11".U
    val lowestAccessPrivilegeLevel = addr(9,8)
    mode >= lowestAccessPrivilegeLevel && !(wen && readOnly)
  }
L
Add CSR  
LinJiawei 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
}

trait HasExceptionNO {
  def instrAddrMisaligned = 0
  def instrAccessFault    = 1
  def illegalInstr        = 2
  def breakPoint          = 3
  def loadAddrMisaligned  = 4
  def loadAccessFault     = 5
  def storeAddrMisaligned = 6
  def storeAccessFault    = 7
  def ecallU              = 8
  def ecallS              = 9
  def ecallM              = 11
  def instrPageFault      = 12
  def loadPageFault       = 13
  def storePageFault      = 15

  val ExcPriority = Seq(
W
William Wang 已提交
144 145 146 147 148 149 150 151 152 153 154 155
      breakPoint, // TODO: different BP has different priority
      instrPageFault,
      instrAccessFault,
      illegalInstr,
      instrAddrMisaligned,
      ecallM, ecallS, ecallU,
      storePageFault,
      loadPageFault,
      storeAccessFault,
      loadAccessFault,
      storeAddrMisaligned,
      loadAddrMisaligned
L
Add CSR  
LinJiawei 已提交
156 157 158 159 160 161 162 163 164 165
  )
}

class FpuCsrIO extends XSBundle {
  val fflags = Output(new Fflags)
  val isIllegal = Output(Bool())
  val dirty_fs = Output(Bool())
  val frm = Input(UInt(3.W))
}

166

167 168 169 170
class PerfCounterIO extends XSBundle {
  val value = Input(UInt(XLEN.W))
}

L
LinJiawei 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183
class CSR extends FunctionUnit(FuConfig(
  fuType = FuType.csr,
  numIntSrc = 1,
  numFpSrc = 0,
  writeIntRf = true,
  writeFpRf = false,
  hasRedirect = false
)) with HasCSRConst
{

  val redirectOut = IO(Output(new Redirect))
  val redirectOutValid = IO(Output(Bool()))
  val fpu_csr = IO(Flipped(new FpuCsrIO))
184
  // from rob
L
LinJiawei 已提交
185 186
  val exception = IO(Flipped(ValidIO(new MicroOp)))
  val isInterrupt = IO(Input(Bool()))
L
Add CSR  
LinJiawei 已提交
187 188
  // for exception check
  // for differential testing
L
LinJiawei 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  //  val intrNO = Output(UInt(XLEN.W))
  val wenFix = IO(Output(Bool()))
  val perf = IO(Vec(NumPerfCounters, new PerfCounterIO))
  val memExceptionVAddr = IO(Input(UInt(VAddrBits.W)))
  val trapTarget = IO(Output(UInt(VAddrBits.W)))
  val mtip = IO(Input(Bool()))
  val msip = IO(Input(Bool()))
  val meip = IO(Input(Bool()))
  val interrupt = IO(Output(Bool()))
  val tlbCsrIO = IO(Output(new TlbCsrBundle))

  val cfIn = io.in.bits.uop.cf
  val cfOut = Wire(new CtrlFlow)
  cfOut := cfIn
  val flushPipe = Wire(Bool())

  val (valid, src1, src2, func) = (
    io.in.valid,
    io.in.bits.src(0),
    io.in.bits.uop.ctrl.imm,
    io.in.bits.uop.ctrl.fuOpType
  )
L
Add CSR  
LinJiawei 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224

  // CSR define

  class Priv extends Bundle {
    val m = Output(Bool())
    val h = Output(Bool())
    val s = Output(Bool())
    val u = Output(Bool())
  }

  val csrNotImplemented = RegInit(UInt(XLEN.W), 0.U)

  class MstatusStruct extends Bundle {
    val sd = Output(UInt(1.W))
W
William Wang 已提交
225 226 227 228 229 230

    val pad1 = if (XLEN == 64) Output(UInt(27.W)) else null
    val sxl  = if (XLEN == 64) Output(UInt(2.W))  else null
    val uxl  = if (XLEN == 64) Output(UInt(2.W))  else null
    val pad0 = if (XLEN == 64) Output(UInt(9.W))  else Output(UInt(8.W))

L
Add CSR  
LinJiawei 已提交
231 232
    val tsr = Output(UInt(1.W))
    val tw = Output(UInt(1.W))
W
William Wang 已提交
233
    val tvm = Output(UInt(1.W))
L
Add CSR  
LinJiawei 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246
    val mxr = Output(UInt(1.W))
    val sum = Output(UInt(1.W))
    val mprv = Output(UInt(1.W))
    val xs = Output(UInt(2.W))
    val fs = Output(UInt(2.W))
    val mpp = Output(UInt(2.W))
    val hpp = Output(UInt(2.W))
    val spp = Output(UInt(1.W))
    val pie = new Priv
    val ie = new Priv
    assert(this.getWidth == XLEN)
  }

247 248 249 250 251 252
  class SatpStruct extends Bundle {
    val mode = UInt(4.W)
    val asid = UInt(16.W)
    val ppn  = UInt(44.W)
  }

L
Add CSR  
LinJiawei 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  class Interrupt extends Bundle {
    val e = new Priv
    val t = new Priv
    val s = new Priv
  }

  // Machine-Level CSRs

  val mtvec = RegInit(UInt(XLEN.W), 0.U)
  val mcounteren = RegInit(UInt(XLEN.W), 0.U)
  val mcause = RegInit(UInt(XLEN.W), 0.U)
  val mtval = RegInit(UInt(XLEN.W), 0.U)
  val mepc = Reg(UInt(XLEN.W))

  val mie = RegInit(0.U(XLEN.W))
  val mipWire = WireInit(0.U.asTypeOf(new Interrupt))
  val mipReg  = RegInit(0.U.asTypeOf(new Interrupt).asUInt)
W
William Wang 已提交
270
  val mipFixMask = GenMask(9) | GenMask(5) | GenMask(1)
L
Add CSR  
LinJiawei 已提交
271 272
  val mip = (mipWire.asUInt | mipReg).asTypeOf(new Interrupt)

273 274
  def getMisaMxl(mxl: Int): UInt = {mxl.U << (XLEN-2)}.asUInt()
  def getMisaExt(ext: Char): UInt = {1.U << (ext.toInt - 'a'.toInt)}.asUInt()
L
Add CSR  
LinJiawei 已提交
275 276 277
  var extList = List('a', 's', 'i', 'u')
  if(HasMExtension){ extList = extList :+ 'm'}
  if(HasCExtension){ extList = extList :+ 'c'}
Y
Yinan Xu 已提交
278
  if(HasFPU){ extList = extList ++ List('f', 'd')}
279 280
  val misaInitVal = getMisaMxl(2) | extList.foldLeft(0.U)((sum, i) => sum | getMisaExt(i)) //"h8000000000141105".U
  val misa = RegInit(UInt(XLEN.W), misaInitVal)
L
Add CSR  
LinJiawei 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  // MXL = 2          | 0 | EXT = b 00 0000 0100 0001 0001 0000 0101
  // (XLEN-1, XLEN-2) |   |(25, 0)  ZY XWVU TSRQ PONM LKJI HGFE DCBA

  val mvendorid = RegInit(UInt(XLEN.W), 0.U) // this is a non-commercial implementation
  val marchid = RegInit(UInt(XLEN.W), 0.U) // return 0 to indicate the field is not implemented
  val mimpid = RegInit(UInt(XLEN.W), 0.U) // provides a unique encoding of the version of the processor implementation
  val mhartid = RegInit(UInt(XLEN.W), 0.U) // the hardware thread running the code
  val mstatus = RegInit(UInt(XLEN.W), "h00001800".U)
  // val mstatus = RegInit(UInt(XLEN.W), "h8000c0100".U)
  // mstatus Value Table
  // | sd   |
  // | pad1 |
  // | sxl  | hardlinked to 10, use 00 to pass xv6 test
  // | uxl  | hardlinked to 00
  // | pad0 |
  // | tsr  |
  // | tw   |
W
William Wang 已提交
298
  // | tvm  |
L
Add CSR  
LinJiawei 已提交
299 300 301 302
  // | mxr  |
  // | sum  |
  // | mprv |
  // | xs   | 00 |
W
William Wang 已提交
303
  // | fs   | 00 |
L
Add CSR  
LinJiawei 已提交
304 305 306
  // | mpp  | 00 |
  // | hpp  | 00 |
  // | spp  | 0 |
W
William Wang 已提交
307
  // | pie  | 0000 | pie.h is used as UBE
L
Add CSR  
LinJiawei 已提交
308 309 310 311
  // | ie   | 0000 | uie hardlinked to 0, as N ext is not implemented
  val mstatusStruct = mstatus.asTypeOf(new MstatusStruct)
  def mstatusUpdateSideEffect(mstatus: UInt): UInt = {
    val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct))
W
William Wang 已提交
312
    val mstatusNew = Cat(mstatusOld.xs === "b11".U || mstatusOld.fs === "b11".U, mstatus(XLEN-2, 0))
L
Add CSR  
LinJiawei 已提交
313 314 315
    mstatusNew
  }

316
  val mstatusMask = (~ZeroExt((
W
William Wang 已提交
317 318 319 320
    GenMask(XLEN-2, 38) | GenMask(31, 23) | GenMask(10, 9) | GenMask(2) |
    GenMask(37) | // MBE
    GenMask(36) | // SBE
    GenMask(6)    // UBE
321
  ), 64)).asUInt()
W
William Wang 已提交
322

L
Add CSR  
LinJiawei 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  val medeleg = RegInit(UInt(XLEN.W), 0.U)
  val mideleg = RegInit(UInt(XLEN.W), 0.U)
  val mscratch = RegInit(UInt(XLEN.W), 0.U)

  val pmpcfg0 = RegInit(UInt(XLEN.W), 0.U)
  val pmpcfg1 = RegInit(UInt(XLEN.W), 0.U)
  val pmpcfg2 = RegInit(UInt(XLEN.W), 0.U)
  val pmpcfg3 = RegInit(UInt(XLEN.W), 0.U)
  val pmpaddr0 = RegInit(UInt(XLEN.W), 0.U)
  val pmpaddr1 = RegInit(UInt(XLEN.W), 0.U)
  val pmpaddr2 = RegInit(UInt(XLEN.W), 0.U)
  val pmpaddr3 = RegInit(UInt(XLEN.W), 0.U)

  // Superviser-Level CSRs

  // val sstatus = RegInit(UInt(XLEN.W), "h00000000".U)
  val sstatusWmask = "hc6122".U
  // Sstatus Write Mask
  // -------------------------------------------------------
  //    19           9   5     2
  // 0  1100 0000 0001 0010 0010
  // 0  c    0    1    2    2
  // -------------------------------------------------------
  val sstatusRmask = sstatusWmask | "h8000000300018000".U
  // Sstatus Read Mask = (SSTATUS_WMASK | (0xf << 13) | (1ull << 63) | (3ull << 32))
  val stvec = RegInit(UInt(XLEN.W), 0.U)
  // val sie = RegInit(0.U(XLEN.W))
  val sieMask = "h222".U & mideleg
  val sipMask  = "h222".U & mideleg
352 353
  val satp = RegInit(0.U(XLEN.W))
  // val satp = RegInit(UInt(XLEN.W), "h8000000000087fbe".U) // only use for tlb naive debug
354
  val satpMask = "h80000fffffffffff".U // disable asid, mode can only be 8 / 0
355
  // val satp = RegInit(UInt(XLEN.W), 0.U)
L
Add CSR  
LinJiawei 已提交
356 357 358 359 360
  val sepc = RegInit(UInt(XLEN.W), 0.U)
  val scause = RegInit(UInt(XLEN.W), 0.U)
  val stval = Reg(UInt(XLEN.W))
  val sscratch = RegInit(UInt(XLEN.W), 0.U)
  val scounteren = RegInit(UInt(XLEN.W), 0.U)
361 362

  val tlbBundle = Wire(new TlbCsrBundle)
363
  tlbBundle.satp := satp.asTypeOf(new SatpStruct)
L
LinJiawei 已提交
364
  tlbCsrIO := tlbBundle
L
Add CSR  
LinJiawei 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423

  // User-Level CSRs
  val uepc = Reg(UInt(XLEN.W))

  // fcsr
  class FcsrStruct extends Bundle{
    val reserved = UInt((XLEN-3-5).W)
    val frm = UInt(3.W)
    val fflags = UInt(5.W)
    assert(this.getWidth == XLEN)
  }
  val fcsr = RegInit(0.U(XLEN.W))
  // set mstatus->sd and mstatus->fs when true
  val csrw_dirty_fp_state = WireInit(false.B)

  def frm_wfn(wdata: UInt): UInt = {
    val fcsrOld = WireInit(fcsr.asTypeOf(new FcsrStruct))
    csrw_dirty_fp_state := true.B
    fcsrOld.frm := wdata(2,0)
    fcsrOld.asUInt()
  }
  def frm_rfn(rdata: UInt): UInt = rdata(7,5)

  def fflags_wfn(wdata: UInt): UInt = {
    val fcsrOld = WireInit(fcsr.asTypeOf(new FcsrStruct))
    csrw_dirty_fp_state := true.B
    fcsrOld.fflags := wdata(4,0)
    fcsrOld.asUInt()
  }
  def fflags_rfn(rdata:UInt): UInt = rdata(4,0)

  def fcsr_wfn(wdata: UInt): UInt = {
    val fcsrOld = WireInit(fcsr.asTypeOf(new FcsrStruct))
    csrw_dirty_fp_state := true.B
    Cat(fcsrOld.reserved, wdata.asTypeOf(fcsrOld).frm, wdata.asTypeOf(fcsrOld).fflags)
  }

  val fcsrMapping = Map(
    MaskedRegMap(Fflags, fcsr, wfn = fflags_wfn, rfn = fflags_rfn),
    MaskedRegMap(Frm, fcsr, wfn = frm_wfn, rfn = frm_rfn),
    MaskedRegMap(Fcsr, fcsr, wfn = fcsr_wfn)
  )

  // Atom LR/SC Control Bits
//  val setLr = WireInit(Bool(), false.B)
//  val setLrVal = WireInit(Bool(), false.B)
//  val setLrAddr = WireInit(UInt(AddrBits.W), DontCare) //TODO : need check
//  val lr = RegInit(Bool(), false.B)
//  val lrAddr = RegInit(UInt(AddrBits.W), 0.U)
//
//  when(setLr){
//    lr := setLrVal
//    lrAddr := setLrAddr
//  }

  // Hart Priviledge Mode
  val priviledgeMode = RegInit(UInt(2.W), ModeM)

  // perfcnt
L
LinJiawei 已提交
424
  val hasPerfCnt = !env.FPGAPlatform
L
Add CSR  
LinJiawei 已提交
425 426
  val nrPerfCnts = if (hasPerfCnt) 0x80 else 0x3
  val perfCnts = List.fill(nrPerfCnts)(RegInit(0.U(XLEN.W)))
L
LinJiawei 已提交
427 428
  val perfCntsLoMapping = (0 until nrPerfCnts).map(i => MaskedRegMap(0xb00 + i, perfCnts(i)))
  val perfCntsHiMapping = (0 until nrPerfCnts).map(i => MaskedRegMap(0xb80 + i, perfCnts(i)(63, 32)))
L
Add CSR  
LinJiawei 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

  // CSR reg map
  val mapping = Map(

    // User Trap Setup
    // MaskedRegMap(Ustatus, ustatus),
    // MaskedRegMap(Uie, uie, 0.U, MaskedRegMap.Unwritable),
    // MaskedRegMap(Utvec, utvec),

    // User Trap Handling
    // MaskedRegMap(Uscratch, uscratch),
    // MaskedRegMap(Uepc, uepc),
    // MaskedRegMap(Ucause, ucause),
    // MaskedRegMap(Utval, utval),
    // MaskedRegMap(Uip, uip),

    // User Counter/Timers
    // MaskedRegMap(Cycle, cycle),
    // MaskedRegMap(Time, time),
    // MaskedRegMap(Instret, instret),

    // Supervisor Trap Setup
    MaskedRegMap(Sstatus, mstatus, sstatusWmask, mstatusUpdateSideEffect, sstatusRmask),

    // MaskedRegMap(Sedeleg, Sedeleg),
    // MaskedRegMap(Sideleg, Sideleg),
    MaskedRegMap(Sie, mie, sieMask, MaskedRegMap.NoSideEffect, sieMask),
    MaskedRegMap(Stvec, stvec),
    MaskedRegMap(Scounteren, scounteren),

    // Supervisor Trap Handling
    MaskedRegMap(Sscratch, sscratch),
    MaskedRegMap(Sepc, sepc),
    MaskedRegMap(Scause, scause),
    MaskedRegMap(Stval, stval),
    MaskedRegMap(Sip, mip.asUInt, sipMask, MaskedRegMap.Unwritable, sipMask),

    // Supervisor Protection and Translation
467
    MaskedRegMap(Satp, satp, satpMask, MaskedRegMap.NoSideEffect, satpMask),
L
Add CSR  
LinJiawei 已提交
468 469 470 471 472 473 474 475 476

    // Machine Information Registers
    MaskedRegMap(Mvendorid, mvendorid, 0.U, MaskedRegMap.Unwritable),
    MaskedRegMap(Marchid, marchid, 0.U, MaskedRegMap.Unwritable),
    MaskedRegMap(Mimpid, mimpid, 0.U, MaskedRegMap.Unwritable),
    MaskedRegMap(Mhartid, mhartid, 0.U, MaskedRegMap.Unwritable),

    // Machine Trap Setup
    // MaskedRegMap(Mstatus, mstatus, "hffffffffffffffee".U, (x=>{printf("mstatus write: %x time: %d\n", x, GTimer()); x})),
W
William Wang 已提交
477
    MaskedRegMap(Mstatus, mstatus, mstatusMask, mstatusUpdateSideEffect, mstatusMask),
L
Add CSR  
LinJiawei 已提交
478
    MaskedRegMap(Misa, misa), // now MXL, EXT is not changeable
W
William Wang 已提交
479
    MaskedRegMap(Medeleg, medeleg, "hf3ff".U),
L
Add CSR  
LinJiawei 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    MaskedRegMap(Mideleg, mideleg, "h222".U),
    MaskedRegMap(Mie, mie),
    MaskedRegMap(Mtvec, mtvec),
    MaskedRegMap(Mcounteren, mcounteren),

    // Machine Trap Handling
    MaskedRegMap(Mscratch, mscratch),
    MaskedRegMap(Mepc, mepc),
    MaskedRegMap(Mcause, mcause),
    MaskedRegMap(Mtval, mtval),
    MaskedRegMap(Mip, mip.asUInt, 0.U, MaskedRegMap.Unwritable),

    // Machine Memory Protection
    MaskedRegMap(Pmpcfg0, pmpcfg0),
    MaskedRegMap(Pmpcfg1, pmpcfg1),
    MaskedRegMap(Pmpcfg2, pmpcfg2),
    MaskedRegMap(Pmpcfg3, pmpcfg3),
    MaskedRegMap(PmpaddrBase + 0, pmpaddr0),
    MaskedRegMap(PmpaddrBase + 1, pmpaddr1),
    MaskedRegMap(PmpaddrBase + 2, pmpaddr2),
    MaskedRegMap(PmpaddrBase + 3, pmpaddr3)

  ) ++
    perfCntsLoMapping ++ (if (XLEN == 32) perfCntsHiMapping else Nil) ++
    (if(HasFPU) fcsrMapping else Nil)

  val addr = src2(11, 0)
  val rdata = Wire(UInt(XLEN.W))
L
LinJiawei 已提交
508
  val csri = ZeroExt(cfIn.instr(19,15), XLEN) //unsigned imm for csri. [TODO]
L
Add CSR  
LinJiawei 已提交
509 510 511 512 513 514 515 516 517
  val wdata = LookupTree(func, List(
    CSROpType.wrt  -> src1,
    CSROpType.set  -> (rdata | src1),
    CSROpType.clr  -> (rdata & (~src1).asUInt()),
    CSROpType.wrti -> csri,//TODO: csri --> src2
    CSROpType.seti -> (rdata | csri),
    CSROpType.clri -> (rdata & (~csri).asUInt())
  ))

518
  // satp wen check
519
  val satpLegalMode = (wdata.asTypeOf(new SatpStruct).mode===0.U) || (wdata.asTypeOf(new SatpStruct).mode===8.U)
520 521 522

  // general CSR wen check
  val wen = valid && func =/= CSROpType.jmp && (addr=/=Satp.U || satpLegalMode)
523
  val permitted = csrAccessPermissionCheck(addr, false.B, priviledgeMode)
W
William Wang 已提交
524 525 526
  // Writeable check is ingored.
  // Currently, write to illegal csr addr will be ignored
  MaskedRegMap.generate(mapping, addr, rdata, wen && permitted, wdata)
527 528
  io.out.bits.data := rdata
  io.out.bits.uop := io.in.bits.uop
L
LinJiawei 已提交
529 530
  io.out.bits.uop.cf := cfOut
  io.out.bits.uop.ctrl.flushPipe := flushPipe
L
Add CSR  
LinJiawei 已提交
531 532 533 534 535 536 537 538 539

  // Fix Mip/Sip write
  val fixMapping = Map(
    MaskedRegMap(Mip, mipReg.asUInt, mipFixMask),
    MaskedRegMap(Sip, mipReg.asUInt, sipMask, MaskedRegMap.NoSideEffect, sipMask)
  )
  val rdataDummy = Wire(UInt(XLEN.W))
  MaskedRegMap.generate(fixMapping, addr, rdataDummy, wen, wdata)

L
LinJiawei 已提交
540 541
  when(fpu_csr.fflags.asUInt() =/= 0.U){
    fcsr := fflags_wfn(fpu_csr.fflags.asUInt())
L
Add CSR  
LinJiawei 已提交
542 543
  }
  // set fs and sd in mstatus
L
LinJiawei 已提交
544
  when(csrw_dirty_fp_state || fpu_csr.dirty_fs){
L
Add CSR  
LinJiawei 已提交
545 546 547 548 549
    val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct))
    mstatusNew.fs := "b11".U
    mstatusNew.sd := true.B
    mstatus := mstatusNew.asUInt()
  }
L
LinJiawei 已提交
550
  fpu_csr.frm := fcsr.asTypeOf(new FcsrStruct).frm
L
Add CSR  
LinJiawei 已提交
551 552

  // CSR inst decode
W
William Wang 已提交
553
  val isEbreak = addr === privEbreak && func === CSROpType.jmp
L
Add CSR  
LinJiawei 已提交
554 555 556 557 558
  val isEcall = addr === privEcall && func === CSROpType.jmp
  val isMret = addr === privMret   && func === CSROpType.jmp
  val isSret = addr === privSret   && func === CSROpType.jmp
  val isUret = addr === privUret   && func === CSROpType.jmp

L
LinJiawei 已提交
559 560
  XSDebug(wen, "csr write: pc %x addr %x rdata %x wdata %x func %x\n", cfIn.pc, addr, rdata, wdata, func)
  XSDebug(wen, "pc %x mstatus %x mideleg %x medeleg %x mode %x\n", cfIn.pc, mstatus, mideleg , medeleg, priviledgeMode)
561

W
William Wang 已提交
562 563
  // Illegal priviledged operation list
  val illegalSModeSret = valid && isSret && priviledgeMode === ModeS && mstatusStruct.tsr.asBool
L
Add CSR  
LinJiawei 已提交
564

W
William Wang 已提交
565 566 567 568
  // Illegal priviledged instruction check
  val isIllegalAddr = MaskedRegMap.isIllegalAddr(mapping, addr)
  val isIllegalAccess = !permitted
  val isIllegalPrivOp = illegalSModeSret
L
Add CSR  
LinJiawei 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

  // def MMUPermissionCheck(ptev: Bool, pteu: Bool): Bool = ptev && !(priviledgeMode === ModeU && !pteu) && !(priviledgeMode === ModeS && pteu && mstatusStruct.sum.asBool)
  // def MMUPermissionCheckLoad(ptev: Bool, pteu: Bool): Bool = ptev && !(priviledgeMode === ModeU && !pteu) && !(priviledgeMode === ModeS && pteu && mstatusStruct.sum.asBool) && (pter || (mstatusStruct.mxr && ptex))
  // imem
  // val imemPtev = true.B
  // val imemPteu = true.B
  // val imemPtex = true.B
  // val imemReq = true.B
  // val imemPermissionCheckPassed = MMUPermissionCheck(imemPtev, imemPteu)
  // val hasInstrPageFault = imemReq && !(imemPermissionCheckPassed && imemPtex)
  // assert(!hasInstrPageFault)

  // dmem
  // val dmemPtev = true.B
  // val dmemPteu = true.B
  // val dmemReq = true.B
  // val dmemPermissionCheckPassed = MMUPermissionCheck(dmemPtev, dmemPteu)
  // val dmemIsStore = true.B

  // val hasLoadPageFault  = dmemReq && !dmemIsStore && !(dmemPermissionCheckPassed)
  // val hasStorePageFault = dmemReq &&  dmemIsStore && !(dmemPermissionCheckPassed)
  // assert(!hasLoadPageFault)
  // assert(!hasStorePageFault)

  //TODO: Havn't test if io.dmemMMU.priviledgeMode is correct yet
594 595 596 597
  tlbBundle.priv.mxr   := mstatusStruct.mxr.asBool
  tlbBundle.priv.sum   := mstatusStruct.sum.asBool
  tlbBundle.priv.imode := priviledgeMode
  tlbBundle.priv.dmode := Mux(mstatusStruct.mprv.asBool, mstatusStruct.mpp, priviledgeMode)
L
Add CSR  
LinJiawei 已提交
598

L
LinJiawei 已提交
599 600 601 602 603
  val hasInstrPageFault = exception.bits.cf.exceptionVec(instrPageFault) && exception.valid
  val hasLoadPageFault = exception.bits.cf.exceptionVec(loadPageFault) && exception.valid
  val hasStorePageFault = exception.bits.cf.exceptionVec(storePageFault) && exception.valid
  val hasStoreAddrMisaligned = exception.bits.cf.exceptionVec(storeAddrMisaligned) && exception.valid
  val hasLoadAddrMisaligned = exception.bits.cf.exceptionVec(loadAddrMisaligned) && exception.valid
L
Add CSR  
LinJiawei 已提交
604

W
William Wang 已提交
605
  // mtval write logic
L
LinJiawei 已提交
606
  val memExceptionAddr = SignExt(memExceptionVAddr, XLEN)
L
Add CSR  
LinJiawei 已提交
607 608 609 610
  when(hasInstrPageFault || hasLoadPageFault || hasStorePageFault){
    val tval = Mux(
      hasInstrPageFault,
      Mux(
L
LinJiawei 已提交
611 612 613
        exception.bits.cf.crossPageIPFFix,
        SignExt(exception.bits.cf.pc + 2.U, XLEN),
        SignExt(exception.bits.cf.pc, XLEN)
L
Add CSR  
LinJiawei 已提交
614
      ),
615
      memExceptionAddr
L
Add CSR  
LinJiawei 已提交
616 617 618 619 620 621 622 623 624 625
    )
    when(priviledgeMode === ModeM){
      mtval := tval
    }.otherwise{
      stval := tval
    }
  }

  when(hasLoadAddrMisaligned || hasStoreAddrMisaligned)
  {
626
    mtval := memExceptionAddr
L
Add CSR  
LinJiawei 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639
  }

  // Exception and Intr

  // interrupts

  val ideleg =  (mideleg & mip.asUInt)
  def priviledgedEnableDetect(x: Bool): Bool = Mux(x, ((priviledgeMode === ModeS) && mstatusStruct.ie.s) || (priviledgeMode < ModeS),
    ((priviledgeMode === ModeM) && mstatusStruct.ie.m) || (priviledgeMode < ModeM))

  val intrVecEnable = Wire(Vec(12, Bool()))
  intrVecEnable.zip(ideleg.asBools).map{case(x,y) => x := priviledgedEnableDetect(y)}
  val intrVec = mie(11,0) & mip.asUInt & intrVecEnable.asUInt
Y
Yinan Xu 已提交
640
  val intrBitSet = intrVec.orR()
L
LinJiawei 已提交
641
  interrupt := intrBitSet
642
  val intrNO = IntPriority.foldRight(0.U)((i: Int, sum: UInt) => Mux(intrVec(i), i.U, sum))
L
LinJiawei 已提交
643 644
  val raiseIntr = intrBitSet && exception.valid && isInterrupt
  XSDebug(raiseIntr, "interrupt: pc=0x%x, %d\n", exception.bits.cf.pc, intrNO)
L
Add CSR  
LinJiawei 已提交
645

L
LinJiawei 已提交
646 647 648
  mipWire.t.m := mtip
  mipWire.s.m := msip
  mipWire.e.m := meip
L
Add CSR  
LinJiawei 已提交
649 650 651 652

  // exceptions
  val csrExceptionVec = Wire(Vec(16, Bool()))
  csrExceptionVec.map(_ := false.B)
W
William Wang 已提交
653
  csrExceptionVec(breakPoint) := io.in.valid && isEbreak
L
Add CSR  
LinJiawei 已提交
654 655 656
  csrExceptionVec(ecallM) := priviledgeMode === ModeM && io.in.valid && isEcall
  csrExceptionVec(ecallS) := priviledgeMode === ModeS && io.in.valid && isEcall
  csrExceptionVec(ecallU) := priviledgeMode === ModeU && io.in.valid && isEcall
W
William Wang 已提交
657 658 659 660
  // Trigger an illegal instr exception when:
  // * unimplemented csr is being read/written
  // * csr access is illegal
  csrExceptionVec(illegalInstr) := (isIllegalAddr || isIllegalAccess) && wen
L
Add CSR  
LinJiawei 已提交
661 662
  csrExceptionVec(loadPageFault) := hasLoadPageFault
  csrExceptionVec(storePageFault) := hasStorePageFault
L
LinJiawei 已提交
663
  val iduExceptionVec = cfIn.exceptionVec
664
  val exceptionVec = csrExceptionVec.asUInt() | iduExceptionVec.asUInt()
L
LinJiawei 已提交
665 666
  cfOut.exceptionVec.zipWithIndex.map{case (e, i) => e := exceptionVec(i) }
  wenFix := DontCare
667

L
LinJiawei 已提交
668
  val raiseExceptionVec = exception.bits.cf.exceptionVec.asUInt()
L
Add CSR  
LinJiawei 已提交
669 670
  val exceptionNO = ExcPriority.foldRight(0.U)((i: Int, sum: UInt) => Mux(raiseExceptionVec(i), i.U, sum))
  val causeNO = (raiseIntr << (XLEN-1)).asUInt() | Mux(raiseIntr, intrNO, exceptionNO)
671 672
  val difftestIntrNO = Mux(raiseIntr, causeNO, 0.U)
  ExcitingUtils.addSource(difftestIntrNO, "difftestIntrNOfromCSR")
673
  ExcitingUtils.addSource(causeNO, "difftestCausefromCSR")
L
Add CSR  
LinJiawei 已提交
674

L
LinJiawei 已提交
675
  val raiseExceptionIntr = exception.valid
L
Add CSR  
LinJiawei 已提交
676
  val retTarget = Wire(UInt(VAddrBits.W))
W
William Wang 已提交
677
  val resetSatp = addr === Satp.U && wen // write to satp will cause the pipeline be flushed
L
LinJiawei 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
  redirectOut := DontCare
  redirectOutValid := valid && func === CSROpType.jmp && !isEcall
  redirectOut.target := retTarget
  flushPipe := resetSatp

  XSDebug(redirectOutValid, "redirect to %x, pc=%x\n", redirectOut.target, cfIn.pc)

  XSDebug(raiseExceptionIntr, "int/exc: pc %x int (%d):%x exc: (%d):%x\n",
    exception.bits.cf.pc,
    intrNO,
    exception.bits.cf.intrVec.asUInt,
    exceptionNO,
    raiseExceptionVec.asUInt
  )
  XSDebug(raiseExceptionIntr,
    "pc %x mstatus %x mideleg %x medeleg %x mode %x\n",
    exception.bits.cf.pc,
    mstatus,
    mideleg,
    medeleg,
    priviledgeMode
  )
L
Add CSR  
LinJiawei 已提交
700 701 702 703 704 705

  // Branch control

  val deleg = Mux(raiseIntr, mideleg , medeleg)
  // val delegS = ((deleg & (1 << (causeNO & 0xf))) != 0) && (priviledgeMode < ModeM);
  val delegS = (deleg(causeNO(3,0))) && (priviledgeMode < ModeM)
W
William Wang 已提交
706
  val tvalWen = !(hasInstrPageFault || hasLoadPageFault || hasStorePageFault || hasLoadAddrMisaligned || hasStoreAddrMisaligned) || raiseIntr // TODO: need check
L
Add CSR  
LinJiawei 已提交
707

L
LinJiawei 已提交
708
  trapTarget := Mux(delegS, stvec, mtvec)(VAddrBits-1, 0)
L
Add CSR  
LinJiawei 已提交
709 710 711 712 713 714 715 716 717 718
  retTarget := DontCare
  // val illegalEret = TODO

  when (valid && isMret) {
    val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct))
    val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct))
    mstatusNew.ie.m := mstatusOld.pie.m
    priviledgeMode := mstatusOld.mpp
    mstatusNew.pie.m := true.B
    mstatusNew.mpp := ModeU
W
William Wang 已提交
719
    mstatusNew.mprv := 0.U
L
Add CSR  
LinJiawei 已提交
720 721 722 723 724
    mstatus := mstatusNew.asUInt
//    lr := false.B
    retTarget := mepc(VAddrBits-1, 0)
  }

W
William Wang 已提交
725
  when (valid && isSret && !illegalSModeSret) {
L
Add CSR  
LinJiawei 已提交
726 727 728 729 730 731 732
    val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct))
    val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct))
    mstatusNew.ie.s := mstatusOld.pie.s
    priviledgeMode := Cat(0.U(1.W), mstatusOld.spp)
    mstatusNew.pie.s := true.B
    mstatusNew.spp := ModeU
    mstatus := mstatusNew.asUInt
W
William Wang 已提交
733 734
    mstatusNew.mprv := 0.U
    // lr := false.B
L
Add CSR  
LinJiawei 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
    retTarget := sepc(VAddrBits-1, 0)
  }

  when (valid && isUret) {
    val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct))
    val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct))
    // mstatusNew.mpp.m := ModeU //TODO: add mode U
    mstatusNew.ie.u := mstatusOld.pie.u
    priviledgeMode := ModeU
    mstatusNew.pie.u := true.B
    mstatus := mstatusNew.asUInt
    retTarget := uepc(VAddrBits-1, 0)
  }

  when (raiseExceptionIntr) {
    val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct))
    val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct))

    when (delegS) {
      scause := causeNO
L
LinJiawei 已提交
755
      sepc := SignExt(exception.bits.cf.pc, XLEN)
L
Add CSR  
LinJiawei 已提交
756 757 758 759
      mstatusNew.spp := priviledgeMode
      mstatusNew.pie.s := mstatusOld.ie.s
      mstatusNew.ie.s := false.B
      priviledgeMode := ModeS
W
William Wang 已提交
760
      when(tvalWen){stval := 0.U}
L
Add CSR  
LinJiawei 已提交
761 762 763
      // trapTarget := stvec(VAddrBits-1. 0)
    }.otherwise {
      mcause := causeNO
L
LinJiawei 已提交
764
      mepc := SignExt(exception.bits.cf.pc, XLEN)
L
Add CSR  
LinJiawei 已提交
765 766 767 768
      mstatusNew.mpp := priviledgeMode
      mstatusNew.pie.m := mstatusOld.ie.m
      mstatusNew.ie.m := false.B
      priviledgeMode := ModeM
W
William Wang 已提交
769
      when(tvalWen){mtval := 0.U}
L
Add CSR  
LinJiawei 已提交
770 771 772 773 774 775 776 777 778
      // trapTarget := mtvec(VAddrBits-1. 0)
    }

    mstatus := mstatusNew.asUInt
  }

  io.in.ready := true.B
  io.out.valid := valid

779

L
LinJiawei 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
  XSDebug(redirectOutValid,
    "Rediret %x raiseExcepIntr:%d isSret:%d retTarget:%x sepc:%x delegs:%d deleg:%x cfInpc:%x valid:%d\n",
    redirectOut.target,
    raiseExceptionIntr,
    isSret,
    retTarget,
    sepc,
    delegS,
    deleg,
    cfIn.pc,
    valid
  )
  XSDebug(raiseExceptionIntr && delegS,
    "Red(%d, %x) raiseExcepIntr:%d isSret:%d retTarget:%x sepc:%x delegs:%d deleg:%x cfInpc:%x valid:%d\n",
    redirectOutValid,
    redirectOut.target,
    raiseExceptionIntr,
    isSret,
    retTarget,
    sepc,
    delegS,
    deleg,
    cfIn.pc,
    valid
  )
  XSDebug(raiseExceptionIntr && delegS, "sepc is writen!!! pc:%x\n", cfIn.pc)
806

L
Add CSR  
LinJiawei 已提交
807 808 809 810

  // perfcnt

  val perfCntList = Map(
L
LinJiawei 已提交
811 812 813 814 815 816 817 818 819 820 821 822
//    "Mcycle"      -> (0xb00, "perfCntCondMcycle"     ),
//    "Minstret"    -> (0xb02, "perfCntCondMinstret"   ),
    "MbpInstr"    -> (0xb03, "perfCntCondMbpInstr"   ),
    "MbpRight"    -> (0xb04, "perfCntCondMbpRight"   ),
    "MbpWrong"    -> (0xb05, "perfCntCondMbpWrong"   ),
    "MbpBRight"   -> (0xb06, "perfCntCondMbpBRight"   ),
    "MbpBWrong"   -> (0xb07, "perfCntCondMbpBWrong"   ),
    "MbpJRight"   -> (0xb08, "perfCntCondMbpJRight"   ),
    "MbpJWrong"   -> (0xb09, "perfCntCondMbpJWrong"   ),
    "MbpIRight"   -> (0xb0a, "perfCntCondMbpIRight"   ),
    "MbpIWrong"   -> (0xb0b, "perfCntCondMbpIWrong"   ),
    "MbpRRight"   -> (0xb0c, "perfCntCondMbpRRight"   ),
823 824
    "MbpRWrong"   -> (0xb0d, "perfCntCondMbpRWrong"   ),
    "DpqReplay"   -> (0xb0e, "perfCntCondDpqReplay"   ),
Y
Yinan Xu 已提交
825 826 827 828 829
    "RoqWalk"     -> (0xb0f, "perfCntCondRoqWalk"     ),
    "RoqWaitInt"  -> (0xb10, "perfCntCondRoqWaitInt"  ),
    "RoqWaitFp"   -> (0xb11, "perfCntCondRoqWaitFp"   ),
    "RoqWaitLoad" -> (0xb12, "perfCntCondRoqWaitLoad" ),
    "RoqWaitStore"-> (0xb13, "perfCntCondRoqWaitStore"),
Z
ZhangZifei 已提交
830 831 832 833 834 835 836 837 838
    "Dp1Empty"    -> (0xb14, "perfCntCondDp1Empty"    ),
    "DTlbReqCnt0" -> (0xb15, "perfCntDtlbReqCnt0"     ),
    "DTlbReqCnt1" -> (0xb16, "perfCntDtlbReqCnt1"     ),
    "DTlbReqCnt2" -> (0xb17, "perfCntDtlbReqCnt2"     ),
    "DTlbReqCnt3" -> (0xb18, "perfCntDtlbReqCnt3"     ),
    "DTlbMissCnt0"-> (0xb19, "perfCntDtlbMissCnt0"    ),
    "DTlbMissCnt1"-> (0xb20, "perfCntDtlbMissCnt1"    ),
    "DTlbMissCnt2"-> (0xb21, "perfCntDtlbMissCnt2"    ),
    "DTlbMissCnt3"-> (0xb22, "perfCntDtlbMissCnt3"    ),
Z
zhanglinjuan 已提交
839 840 841 842
    "ITlbReqCnt0" -> (0xb23, "perfCntItlbReqCnt0"     ),
    "ITlbMissCnt0"-> (0xb24, "perfCntItlbMissCnt0"    ),
    "PtwReqCnt"   -> (0xb25, "perfCntPtwReqCnt"       ),
    "PtwCycleCnt" -> (0xb26, "perfCntPtwCycleCnt"     ),
J
jinyue110 已提交
843 844 845
    "PtwL2TlbHit" -> (0xb27, "perfCntPtwL2TlbHit"     ),
    "ICacheReq"   -> (0xb28, "perfCntIcacheReqCnt"     ),
    "ICacheMiss"   -> (0xb29, "perfCntIcacheMissCnt"     )
L
LinJiawei 已提交
846 847 848 849 850 851 852 853 854
//    "Custom1"     -> (0xb1b, "Custom1"             ),
//    "Custom2"     -> (0xb1c, "Custom2"             ),
//    "Custom3"     -> (0xb1d, "Custom3"             ),
//    "Custom4"     -> (0xb1e, "Custom4"             ),
//    "Custom5"     -> (0xb1f, "Custom5"             ),
//    "Custom6"     -> (0xb20, "Custom6"             ),
//    "Custom7"     -> (0xb21, "Custom7"             ),
//    "Custom8"     -> (0xb22, "Custom8"             ),
//    "Ml2cacheHit" -> (0xb23, "perfCntCondMl2cacheHit")
L
Add CSR  
LinJiawei 已提交
855 856
  )
  val perfCntCond = List.fill(0x80)(WireInit(false.B))
L
LinJiawei 已提交
857 858 859 860 861 862 863
  (perfCnts zip perfCntCond).map { case (c, e) => when (e) { c := c + 1.U } }

//  ExcitingUtils.addSource(WireInit(true.B), "perfCntCondMcycle", ConnectionType.Perf)
  perfCntList.foreach {
    case (_, (address, boringId)) =>
      if(hasPerfCnt){
        ExcitingUtils.addSink(perfCntCond(address & 0x7f), boringId, ConnectionType.Perf)
L
Add CSR  
LinJiawei 已提交
864
      }
L
LinJiawei 已提交
865 866 867 868 869 870 871
//      if (!hasPerfCnt) {
//        // do not enable perfcnts except for Mcycle and Minstret
//        if (address != perfCntList("Mcycle")._1 && address != perfCntList("Minstret")._1) {
//          perfCntCond(address & 0x7f) := false.B
//        }
//      }
  }
L
Add CSR  
LinJiawei 已提交
872

L
LinJiawei 已提交
873
  val xstrap = WireInit(false.B)
874 875 876
  if(!env.FPGAPlatform && EnableBPU){
    ExcitingUtils.addSink(xstrap, "XSTRAP", ConnectionType.Debug)
  }
L
Add CSR  
LinJiawei 已提交
877 878
  def readWithScala(addr: Int): UInt = mapping(addr)._1

L
LinJiawei 已提交
879
  if (!env.FPGAPlatform) {
L
Add CSR  
LinJiawei 已提交
880 881

    // display all perfcnt when nooptrap is executed
L
LinJiawei 已提交
882 883 884 885 886 887
    when (xstrap) {
      printf("======== PerfCnt =========\n")
      perfCntList.toSeq.sortBy(_._2._1).foreach { case (str, (address, boringId)) =>
        printf("%d <- " + str + "\n", readWithScala(address))
      }
    }
L
Add CSR  
LinJiawei 已提交
888

889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
    ExcitingUtils.addSource(priviledgeMode, "difftestMode", Debug)
    ExcitingUtils.addSource(mstatus, "difftestMstatus", Debug)
    ExcitingUtils.addSource(mstatus & sstatusRmask, "difftestSstatus", Debug)
    ExcitingUtils.addSource(mepc, "difftestMepc", Debug)
    ExcitingUtils.addSource(sepc, "difftestSepc", Debug)
    ExcitingUtils.addSource(mtval, "difftestMtval", Debug)
    ExcitingUtils.addSource(stval, "difftestStval", Debug)
    ExcitingUtils.addSource(mtvec, "difftestMtvec", Debug)
    ExcitingUtils.addSource(stvec, "difftestStvec", Debug)
    ExcitingUtils.addSource(mcause, "difftestMcause", Debug)
    ExcitingUtils.addSource(scause, "difftestScause", Debug)
    ExcitingUtils.addSource(satp, "difftestSatp", Debug)
    ExcitingUtils.addSource(mipReg, "difftestMip", Debug)
    ExcitingUtils.addSource(mie, "difftestMie", Debug)
    ExcitingUtils.addSource(mscratch, "difftestMscratch", Debug)
    ExcitingUtils.addSource(sscratch, "difftestSscratch", Debug)
    ExcitingUtils.addSource(mideleg, "difftestMideleg", Debug)
    ExcitingUtils.addSource(medeleg, "difftestMedeleg", Debug)
L
Add CSR  
LinJiawei 已提交
907 908 909
  } else {
  }
}