Skip to content

Commit 100b6a1

Browse files
committed
riscv: add misa unit tests
Adds basic unit tests for the `misa` register.
1 parent 8b66b17 commit 100b6a1

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

riscv/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2121
- Use CSR helper macros to define `mcounteren` register
2222
- Use CSR helper macros to define `mie` register
2323
- Use CSR helper macros to define `mimpid` register
24+
- Use CSR helper macros to define `misa` register
2425

2526
## [v0.12.1] - 2024-10-20
2627

riscv/src/register/misa.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,35 @@ impl Misa {
6565
const fn ext_char_to_bit(extension: char) -> u8 {
6666
(extension as u8).saturating_sub(b'A')
6767
}
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use super::*;
72+
use crate::result::Error;
73+
74+
#[test]
75+
fn test_misa() {
76+
(1..=3)
77+
.zip([XLEN::XLEN32, XLEN::XLEN64, XLEN::XLEN128])
78+
.for_each(|(raw, exp_xlen)| {
79+
assert_eq!(XLEN::try_from(raw), Ok(exp_xlen));
80+
assert_eq!(usize::from(exp_xlen), raw);
81+
82+
let misa = Misa::from_bits(raw << (usize::BITS - 2));
83+
assert_eq!(misa.try_mxl(), Ok(exp_xlen));
84+
assert_eq!(misa.mxl(), exp_xlen);
85+
});
86+
87+
(0..62).map(|b| 1 << b).for_each(|invalid_mxl| {
88+
assert_eq!(
89+
Misa::from_bits(invalid_mxl).try_mxl(),
90+
Err(Error::InvalidVariant(0))
91+
);
92+
});
93+
94+
('A'..='Z').for_each(|ext| {
95+
assert!(!Misa::from_bits(0).has_extension(ext));
96+
assert!(Misa::from_bits(1 << ext_char_to_bit(ext)).has_extension(ext));
97+
});
98+
}
99+
}

0 commit comments

Comments
 (0)