Skip to content

Commit

Permalink
Add mstateen0 CSR
Browse files Browse the repository at this point in the history
  • Loading branch information
ved-rivos committed Dec 30, 2023
1 parent d7a3d80 commit faaed49
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions model/riscv_csr_map.sail
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ mapping clause csr_name_map = 0x00F <-> "vcsr"
mapping clause csr_name_map = 0xC20 <-> "vl"
mapping clause csr_name_map = 0xC21 <-> "vtype"
mapping clause csr_name_map = 0xC22 <-> "vlenb"
/* Smstateen csrs */
mapping clause csr_name_map = 0x30C <-> "mstateen0"
mapping clause csr_name_map = 0x31C <-> "mstateen0h"

val csr_name : csreg -> string
overload to_str = {csr_name}
Expand Down
6 changes: 6 additions & 0 deletions model/riscv_insts_zicsr.sail
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ function readCSR csr : csreg -> xlenbits = {
(0x305, _) => get_mtvec(),
(0x306, _) => zero_extend(mcounteren.bits()),
(0x30A, _) => menvcfg.bits()[sizeof(xlen) - 1 .. 0],
(0x30C, _) => mstateen0.bits()[sizeof(xlen) - 1 .. 0],
(0x310, 32) => mstatush.bits(),
(0x31A, 32) => menvcfg.bits()[63 .. 32],
(0x31C, 32) => mstateen0.bits()[63 .. 32],
(0x320, _) => zero_extend(mcountinhibit.bits()),

(0x340, _) => mscratch,
Expand Down Expand Up @@ -200,8 +202,12 @@ function writeCSR (csr : csreg, value : xlenbits) -> unit = {
(0x306, _) => { mcounteren = legalize_mcounteren(mcounteren, value); Some(zero_extend(mcounteren.bits())) },
(0x30A, 32) => { menvcfg = legalize_envcfg(menvcfg, menvcfg.bits()[63 .. 32] @ value); Some(menvcfg.bits()[31 .. 0]) },
(0x30A, 64) => { menvcfg = legalize_envcfg(menvcfg, value); Some(menvcfg.bits()) },
(0x30C, 32) => { mstateen0 = legalize_mstateen0(mstateen0, mstateen0.bits()[63 .. 32] @ value); Some(mstateen0.bits()[31 .. 0]) },
(0x30C, 64) => { mstateen0 = legalize_mstateen0(mstateen0, value); Some(mstateen0.bits()) },

(0x310, 32) => { Some(mstatush.bits()) }, // ignore writes for now
(0x31A, 32) => { menvcfg = legalize_envcfg(menvcfg, value @ menvcfg.bits()[31 .. 0]); Some(menvcfg.bits()[63 .. 32]) },
(0x31C, 32) => { mstateen0 = legalize_mstateen0(mstateen0, value @ mstateen0.bits()[31 .. 0]); Some(mstateen0.bits()[63 .. 32]) },
(0x320, _) => { mcountinhibit = legalize_mcountinhibit(mcountinhibit, value); Some(zero_extend(mcountinhibit.bits())) },
(0x340, _) => { mscratch = value; Some(mscratch) },
(0x341, _) => { Some(set_xret_target(Machine, value)) },
Expand Down
4 changes: 3 additions & 1 deletion model/riscv_sys_control.sail
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ function is_CSR_defined (csr : csreg, p : Privilege) -> bool =
0x305 => p == Machine, // mtvec
0x306 => p == Machine & haveUsrMode(), // mcounteren
0x30A => p == Machine & haveUsrMode(), // menvcfg
0x30C => p == Machine & haveSupMode() & haveSmstateen(), // mstateen0
0x310 => p == Machine & (sizeof(xlen) == 32), // mstatush
0x31A => p == Machine & haveUsrMode() & (sizeof(xlen) == 32), // menvcfgh
0x31C => p == Machine & haveSupMode() & (sizeof(xlen) == 32) & haveSmstateen(), // mstateen0h
0x320 => p == Machine, // mcountinhibit
/* machine mode: trap handling */
0x340 => p == Machine, // mscratch
Expand Down Expand Up @@ -193,7 +195,6 @@ function check_Counteren(csr : csreg, p : Privilege) -> bool =
else true
}


/* Seed may only be accessed if we are doing a write, and access has been
* allowed in the current priv mode
*/
Expand Down Expand Up @@ -603,6 +604,7 @@ function init_sys() -> unit = {

menvcfg->bits() = zero_extend(0b0);
senvcfg->bits() = zero_extend(0b0);
mstateen0->bits() = zero_extend(0b0);
/* initialize vector csrs */
elen = 0b1; /* ELEN=64 as the common case */
vlen = 0b0100; /* VLEN=512 as a default value */
Expand Down
24 changes: 24 additions & 0 deletions model/riscv_sys_regs.sail
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ function haveZmmul() -> bool = true
/* Zicond extension support */
function haveZicond() -> bool = true

/* Smstateen extension support */
function haveSmstateen() -> bool = true

/* Ssqosid extension support */
function haveSsqosid() -> bool = true

bitfield Mstatush : bits(32) = {
MBE : 5,
SBE : 4
Expand Down Expand Up @@ -882,6 +888,24 @@ function is_fiom_active() -> bool = {
User => (menvcfg.FIOM() | senvcfg.FIOM()) == 0b1,
}
}

bitfield Mstateen0 : bits(64) = {
// controls to new state introduced by the following small extensions of Priv-1.14-era:
PRIV114 : 55
}
register mstateen0 : Mstateen0
// If any extension controlled by PRIV114 bit in mstateen0 is present
// then PRIV114 bit is present
function havePriv114Extensions() -> bool = {
haveSsqosid()
}
function legalize_mstateen0(o : Mstateen0, v : bits(64)) -> Mstateen0 = {
let v = Mk_Mstateen0(v);
let o = update_PRIV114(o, if havePriv114Extensions() then v.PRIV114() else 0b0);
// Other extensions are not implemented yet so all other fields are read only zero.
o
}

/* vector csrs */
register vstart : bits(16) /* use the largest possible length of vstart */
register vxsat : bits(1)
Expand Down

0 comments on commit faaed49

Please sign in to comment.