Skip to content

Commit 8b66b17

Browse files
committed
riscv: define misa using CSR macros
Uses CSR helper macros to define the `misa` register. Corrections for other users of the `XLEN` enum.
1 parent 6da32e7 commit 8b66b17

File tree

3 files changed

+40
-45
lines changed

3 files changed

+40
-45
lines changed

riscv/src/register/misa.rs

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,67 @@
11
//! misa register
22
3-
use core::num::NonZeroUsize;
4-
5-
/// misa register
6-
#[derive(Clone, Copy, Debug)]
7-
pub struct Misa {
8-
bits: NonZeroUsize,
3+
#[cfg(target_arch = "riscv32")]
4+
read_only_csr! {
5+
/// `misa` register
6+
Misa: 0x301,
7+
mask: 0xc3ff_ffff,
8+
sentinel: 0,
99
}
1010

11-
/// Base integer ISA width
12-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
13-
pub enum XLEN {
14-
XLEN32 = 1,
15-
XLEN64 = 2,
16-
XLEN128 = 3,
11+
#[cfg(not(target_arch = "riscv32"))]
12+
read_only_csr! {
13+
/// `misa` register
14+
Misa: 0x301,
15+
mask: 0xc000_0000_03ff_ffff,
16+
sentinel: 0,
1717
}
1818

19-
impl XLEN {
20-
/// Converts a number into an ISA width
21-
pub(crate) fn from(value: u8) -> Self {
22-
match value {
23-
1 => XLEN::XLEN32,
24-
2 => XLEN::XLEN64,
25-
3 => XLEN::XLEN128,
26-
_ => unreachable!(),
27-
}
19+
csr_field_enum! {
20+
/// Base integer ISA width
21+
XLEN {
22+
default: XLEN32,
23+
XLEN32 = 1,
24+
XLEN64 = 2,
25+
XLEN128 = 3,
2826
}
2927
}
3028

31-
impl Misa {
32-
/// Returns the contents of the register as raw bits
33-
#[inline]
34-
pub fn bits(&self) -> usize {
35-
self.bits.get()
36-
}
29+
#[cfg(target_arch = "riscv32")]
30+
read_only_csr_field! {
31+
Misa,
32+
/// Effective xlen in M-mode (i.e., `MXLEN`).
33+
mxl,
34+
XLEN: [30:31],
35+
}
3736

37+
#[cfg(not(target_arch = "riscv32"))]
38+
read_only_csr_field! {
39+
Misa,
3840
/// Effective xlen in M-mode (i.e., `MXLEN`).
39-
#[inline]
40-
pub fn mxl(&self) -> XLEN {
41-
let value = (self.bits() >> (usize::BITS - 2)) as u8;
42-
XLEN::from(value)
43-
}
41+
mxl,
42+
XLEN: [62:63],
43+
}
4444

45+
impl Misa {
4546
/// Returns true when a given extension is implemented.
4647
///
4748
/// # Example
4849
///
4950
/// ```no_run
50-
/// let misa = unsafe { riscv::register::misa::read() }.unwrap();
51+
/// let misa = unsafe { riscv::register::misa::try_read() }.unwrap();
5152
/// assert!(misa.has_extension('A')); // panics if atomic extension is not implemented
5253
/// ```
5354
#[inline]
5455
pub fn has_extension(&self, extension: char) -> bool {
55-
let bit = extension as u8 - 65;
56+
let bit = ext_char_to_bit(extension);
5657
if bit > 25 {
5758
return false;
5859
}
5960
self.bits() & (1 << bit) == (1 << bit)
6061
}
6162
}
6263

63-
read_csr!(0x301);
64-
65-
/// Reads the CSR
6664
#[inline]
67-
pub fn read() -> Option<Misa> {
68-
let r = unsafe { _read() };
69-
// When misa is hardwired to zero it means that the misa csr
70-
// isn't implemented.
71-
NonZeroUsize::new(r).map(|bits| Misa { bits })
65+
const fn ext_char_to_bit(extension: char) -> u8 {
66+
(extension as u8).saturating_sub(b'A')
7267
}

riscv/src/register/mstatus.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ impl Mstatus {
406406
#[cfg(riscv32)]
407407
() => XLEN::XLEN32,
408408
#[cfg(not(riscv32))]
409-
() => XLEN::from(bf_extract(self.bits, 32, 2) as u8),
409+
() => XLEN::try_from(bf_extract(self.bits, 32, 2)).unwrap_or_default(),
410410
}
411411
}
412412

@@ -431,7 +431,7 @@ impl Mstatus {
431431
#[cfg(riscv32)]
432432
() => XLEN::XLEN32,
433433
#[cfg(not(riscv32))]
434-
() => XLEN::from(bf_extract(self.bits, 34, 2) as u8),
434+
() => XLEN::try_from(bf_extract(self.bits, 34, 2)).unwrap_or_default(),
435435
}
436436
}
437437

riscv/src/register/sstatus.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Sstatus {
8686
#[cfg(riscv32)]
8787
() => XLEN::XLEN32,
8888
#[cfg(not(riscv32))]
89-
() => XLEN::from((self.bits >> 32) as u8 & 0x3),
89+
() => XLEN::try_from((self.bits >> 32) & 0x3).unwrap_or_default(),
9090
}
9191
}
9292

0 commit comments

Comments
 (0)