Skip to content

Commit 25e6eab

Browse files
committed
riscv: re-add Mvendorid::jedec_manufacturer implementation
Adds a corrected `Mvendorid::jedec_manufacturer` implementation to return the decoded JEDEC manufacturer ID.
1 parent 22cbf3a commit 25e6eab

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

riscv/src/register/mvendorid.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,37 @@ read_only_csr_field! {
2222
/// The encoded value returned by `offset` does not include the odd parity bit (`0x80`).
2323
offset: [0:6],
2424
}
25+
26+
impl Mvendorid {
27+
/// Represents the JEDEC manufacture continuation byte.
28+
pub const CONTINUATION: u8 = 0x7f;
29+
30+
/// Gets the decoded JEDEC manufacturer ID from the `mvendorid` value.
31+
///
32+
/// # Note
33+
///
34+
/// This function returns an iterator over the decoded bytes.
35+
///
36+
/// An iterator is needed because the encoding can theoretically return a max count (`0x1ff_ffff`) of continuation bytes (`0x7f`).
37+
///
38+
/// The final byte in the iterator is the `offset`, including the odd parity bit (set only if even).
39+
pub fn jedec_manufacturer(&self) -> impl Iterator<Item = u8> {
40+
const DONE: usize = usize::MAX;
41+
42+
let mut bank = self.bank();
43+
let offset = self.offset();
44+
45+
core::iter::from_fn(move || match bank {
46+
DONE => None,
47+
0 => {
48+
bank = DONE;
49+
let parity = ((1 - (offset.count_ones() % 2)) << 7) as usize;
50+
Some((parity | offset) as u8)
51+
}
52+
_ => {
53+
bank -= 1;
54+
Some(Self::CONTINUATION)
55+
}
56+
})
57+
}
58+
}

0 commit comments

Comments
 (0)