Skip to content

Commit

Permalink
riscv: add basic mvendorid unit tests
Browse files Browse the repository at this point in the history
Adds basic unit tests for the `mvendorid` CSR.
  • Loading branch information
rmsyn committed Jan 13, 2025
1 parent 27e6eb4 commit 1a7fe3c
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions riscv/src/register/mvendorid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Item = u8> {
const CONTINUATION: u8 = 0x7f;

let mut done = false;
let mut bank = self.bank();
let offset = self.offset();
Expand All @@ -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);
}
}

0 comments on commit 1a7fe3c

Please sign in to comment.