Skip to content

Commit 9c783c1

Browse files
committed
riscv: add misa unit tests
Adds basic unit tests for the `misa` register.
1 parent 6c58cca commit 9c783c1

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,30 @@ impl Misa {
6565
const fn ext_char_to_bit(extension: char) -> u8 {
6666
(extension as u8).saturating_sub('A' as u8)
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).zip([XLEN::XLEN32, XLEN::XLEN64, XLEN::XLEN128]).for_each(|(raw, exp_xlen)| {
77+
assert_eq!(XLEN::try_from(raw), Ok(exp_xlen));
78+
assert_eq!(usize::from(exp_xlen), raw);
79+
80+
let misa = Misa::from_bits(raw << (usize::BITS - 2));
81+
assert_eq!(misa.try_mxl(), Ok(exp_xlen));
82+
assert_eq!(misa.mxl(), exp_xlen);
83+
});
84+
85+
(0..62).map(|b| 1 << b).for_each(|invalid_mxl| {
86+
assert_eq!(Misa::from_bits(invalid_mxl).try_mxl(), Err(Error::InvalidVariant(0)));
87+
});
88+
89+
('A'..='Z').for_each(|ext| {
90+
assert!(!Misa::from_bits(0).has_extension(ext));
91+
assert!(Misa::from_bits(1 << ext_char_to_bit(ext)).has_extension(ext));
92+
});
93+
}
94+
}

0 commit comments

Comments
 (0)