From 1a7fe3cbd21243b9ca1324adbbc152200523f3a4 Mon Sep 17 00:00:00 2001 From: rmsyn Date: Mon, 13 Jan 2025 23:39:28 +0000 Subject: [PATCH] riscv: add basic mvendorid unit tests Adds basic unit tests for the `mvendorid` CSR. --- riscv/src/register/mvendorid.rs | 49 +++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/riscv/src/register/mvendorid.rs b/riscv/src/register/mvendorid.rs index 88b03fe3..94b9d135 100644 --- a/riscv/src/register/mvendorid.rs +++ b/riscv/src/register/mvendorid.rs @@ -24,6 +24,9 @@ read_only_csr_field! { } impl Mvendorid { + /// Represents the JEDEC manufacture continuation byte. + pub const CONTINUATION: u8 = 0x7f; + /// Gets the decoded JEDEC manufacturer ID from the `mvendorid` value. /// /// # Note @@ -34,8 +37,6 @@ impl Mvendorid { /// /// The final byte in the iterator is the `offset`, including the odd parity bit (set only if even). pub fn jedec_manufacturer(&self) -> impl Iterator { - const CONTINUATION: u8 = 0x7f; - let mut done = false; let mut bank = self.bank(); let offset = self.offset(); @@ -49,8 +50,50 @@ impl Mvendorid { } _ => { bank -= 1; - Some(CONTINUATION) + Some(Self::CONTINUATION) } }) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_mvendorid() { + (0..u32::BITS) + .map(|r| ((1u64 << r) - 1) as usize) + .for_each(|raw| { + let exp_bank = raw >> 7; + let exp_offset = raw & 0x7f; + let exp_parity = (1 - (exp_offset % 2)) << 7; + let exp_mvendorid = Mvendorid::from_bits(raw); + + assert_eq!(exp_mvendorid.bank(), exp_bank); + assert_eq!(exp_mvendorid.offset(), exp_offset); + + let mut jedec_iter = exp_mvendorid.jedec_manufacturer(); + (0..exp_bank) + .for_each(|_| assert_eq!(jedec_iter.next(), Some(Mvendorid::CONTINUATION))); + assert_eq!(jedec_iter.next(), Some((exp_parity | exp_offset) as u8)); + assert_eq!(jedec_iter.next(), None); + }); + + // ISA example used as a concrete test vector. + + let exp_bank = 0xc; + let exp_offset = 0x0a; + let exp_decoded_offset = 0x8a; + let raw_mvendorid = 0x60a; + let exp_mvendorid = Mvendorid::from_bits(raw_mvendorid); + + assert_eq!(exp_mvendorid.bank(), exp_bank); + assert_eq!(exp_mvendorid.offset(), exp_offset); + + let mut jedec_iter = exp_mvendorid.jedec_manufacturer(); + (0..exp_bank).for_each(|_| assert_eq!(jedec_iter.next(), Some(Mvendorid::CONTINUATION))); + assert_eq!(jedec_iter.next(), Some(exp_decoded_offset)); + assert_eq!(jedec_iter.next(), None); + } +}