Skip to content

Commit

Permalink
Instructions: Separate NMI insts and CSRs
Browse files Browse the repository at this point in the history
Rocket already implemented NMI in chipsalliance#2711,
however, riscv-opcodes currently does not have NMI
as it is a WIP (See riscv/riscv-opcodes#67)

To avoid generating Instructions.scala from a patched riscv-opcodes,
putting NMI related insts/CSRs into CustomInstructions is reasonable.
  • Loading branch information
ZenithalHourlyRate committed Apr 2, 2022
1 parent cfac7a5 commit dd66dfb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/main/scala/rocket/CSR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import freechips.rocketchip.util._
import freechips.rocketchip.util.property
import scala.collection.mutable.LinkedHashMap
import Instructions._
import CustomInstructions._

class MStatus extends Bundle {
// not truly part of mstatus, but convenient
Expand Down Expand Up @@ -634,10 +635,10 @@ class CSRFile(
read_mnstatus.mpv := reg_mnstatus.mpv
read_mnstatus.mie := reg_rnmie
val nmi_csrs = if (!usingNMI) LinkedHashMap() else LinkedHashMap[Int,Bits](
CSRs.mnscratch -> reg_mnscratch,
CSRs.mnepc -> readEPC(reg_mnepc).sextTo(xLen),
CSRs.mncause -> reg_mncause,
CSRs.mnstatus -> read_mnstatus.asUInt)
CustomCSRs.mnscratch -> reg_mnscratch,
CustomCSRs.mnepc -> readEPC(reg_mnepc).sextTo(xLen),
CustomCSRs.mncause -> reg_mncause,
CustomCSRs.mnstatus -> read_mnstatus.asUInt)

val context_csrs = LinkedHashMap[Int,Bits]() ++
reg_mcontext.map(r => CSRs.mcontext -> r) ++
Expand Down Expand Up @@ -1212,10 +1213,10 @@ class CSRFile(

if (usingNMI) {
val new_mnstatus = new MNStatus().fromBits(wdata)
when (decoded_addr(CSRs.mnscratch)) { reg_mnscratch := wdata }
when (decoded_addr(CSRs.mnepc)) { reg_mnepc := formEPC(wdata) }
when (decoded_addr(CSRs.mncause)) { reg_mncause := wdata & UInt((BigInt(1) << (xLen-1)) + BigInt(3)) }
when (decoded_addr(CSRs.mnstatus)) {
when (decoded_addr(CustomCSRs.mnscratch)) { reg_mnscratch := wdata }
when (decoded_addr(CustomCSRs.mnepc)) { reg_mnepc := formEPC(wdata) }
when (decoded_addr(CustomCSRs.mncause)) { reg_mncause := wdata & UInt((BigInt(1) << (xLen-1)) + BigInt(3)) }
when (decoded_addr(CustomCSRs.mnstatus)) {
reg_mnstatus.mpp := legalizePrivilege(new_mnstatus.mpp)
reg_mnstatus.mpv := usingHypervisor && new_mnstatus.mpv
reg_rnmie := reg_rnmie | new_mnstatus.mie // mnie bit settable but not clearable from software
Expand Down
20 changes: 20 additions & 0 deletions src/main/scala/rocket/CustomInstructions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,27 @@ package freechips.rocketchip.rocket
import Chisel._

object CustomInstructions {
def MNRET = BitPat("b01110000001000000000000001110011")
def CEASE = BitPat("b00110000010100000000000001110011")
def CFLUSH_D_L1 = BitPat("b111111000000?????000000001110011")
def CDISCARD_D_L1 = BitPat("b111111000010?????000000001110011")
}

object CustomCSRs {
val mnscratch = 0x350
val mnepc = 0x351
val mncause = 0x352
val mnstatus = 0x353
val all = {
val res = collection.mutable.ArrayBuffer[Int]()
res += mnscratch
res += mnepc
res += mncause
res += mnstatus
res.toArray
}
val all32 = {
val res = collection.mutable.ArrayBuffer(all:_*)
res.toArray
}
}

0 comments on commit dd66dfb

Please sign in to comment.