Skip to content

Commit

Permalink
Merge pull request #220 from jasonwhite/jw/mstatus-vs
Browse files Browse the repository at this point in the history
mstatus: Support vector extension
  • Loading branch information
romancardenas authored Jun 10, 2024
2 parents 7291ac2 + 0b43d4b commit 28e9b02
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions riscv/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Export `riscv::register::macros` module macros for external use
- Add `riscv::register::mcountinhibit` module for `mcountinhibit` CSR
- Add `Mcounteren` in-memory update functions
- Add `Mstatus` vector extension support

### Fixed

Expand Down
41 changes: 41 additions & 0 deletions riscv/src/register/mstatus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ pub enum FS {
Dirty = 3,
}

/// Vector extension state
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum VS {
Off = 0,
Initial = 1,
Clean = 2,
Dirty = 3,
}

/// Machine Previous Privilege Mode
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum MPP {
Expand Down Expand Up @@ -216,6 +225,19 @@ impl Mstatus {
}
}

/// Vector extension state
#[inline]
pub fn vs(&self) -> VS {
let fs = bf_extract(self.bits, 9, 2); // bits 9-10
match fs {
0b00 => VS::Off,
0b01 => VS::Initial,
0b10 => VS::Clean,
0b11 => VS::Dirty,
_ => unreachable!(),
}
}

/// Update Floating-point extension state
///
/// Note this updates a previously read [`Mstatus`] value, but does not
Expand All @@ -226,6 +248,16 @@ impl Mstatus {
self.bits = bf_insert(self.bits, 13, 2, fs as usize);
}

/// Update vector extension state
///
/// Note this updates a previously read [`Mstatus`] value, but does not
/// affect the mstatus CSR itself. See [`set_vs`] to directly update the
/// CSR.
#[inline]
pub fn set_vs(&mut self, vs: VS) {
self.bits = bf_insert(self.bits, 9, 2, vs as usize);
}

/// Additional extension state
///
/// Encodes the status of additional user-mode extensions and associated
Expand Down Expand Up @@ -559,6 +591,15 @@ pub unsafe fn set_fs(fs: FS) {
_write(value);
}

/// Vector extension state
#[inline]
pub unsafe fn set_vs(vs: VS) {
let mut value = _read();
value &= !(0x3 << 9); // clear previous value
value |= (vs as usize) << 9;
_write(value);
}

/// Set S-mode non-instruction-fetch memory endianness
///
/// # Note
Expand Down

0 comments on commit 28e9b02

Please sign in to comment.