Skip to content

Commit

Permalink
riscv: add mtvec unit-tests
Browse files Browse the repository at this point in the history
Adds basic unit-tests for the `mtvec` CSR.
  • Loading branch information
rmsyn committed Dec 26, 2024
1 parent b343fbe commit 9fd2f73
Show file tree
Hide file tree
Showing 2 changed files with 39 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 @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Use CSR helper macros to define `mip` register
- Use CSR helper macros to define `mstatus` register
- Use CSR helper macros to define `mstatush` register
- Use CSR helper macros to define `mtvec` register

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

Expand Down
38 changes: 38 additions & 0 deletions riscv/src/register/mtvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,41 @@ impl Mtvec {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_mtvec() {
let mut m = Mtvec::from_bits(0);

(1..=usize::BITS)
.map(|r| (((1u128 << r) - 1) as usize) & !TRAP_MASK)
.for_each(|address| {
m.set_address(address);
assert_eq!(m.address(), address);

assert_eq!(m.try_set_address(address), Ok(()));
assert_eq!(m.address(), address);
});

(1..=usize::BITS)
.filter_map(|r| match ((1u128 << r) - 1) as usize {
addr if (addr & TRAP_MASK) != 0 => Some(addr),
_ => None,
})
.for_each(|address| {
assert_eq!(
m.try_set_address(address),
Err(Error::InvalidFieldVariant {
field: "mtvec::address",
value: address,
})
);
});

test_csr_field!(m, trap_mode: TrapMode::Direct);
test_csr_field!(m, trap_mode: TrapMode::Vectored);
}
}

0 comments on commit 9fd2f73

Please sign in to comment.