-
Notifications
You must be signed in to change notification settings - Fork 185
riscv: define mvendorid CSR with macro helpers #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
romancardenas
merged 3 commits into
rust-embedded:master
from
rmsyn:riscv/mvendorid-csr-macro
Jan 15, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,100 @@ | ||
| //! mvendorid register | ||
|
|
||
| use core::num::NonZeroUsize; | ||
| read_only_csr! { | ||
| /// `mvendorid` register | ||
| Mvendorid: 0xF11, | ||
| mask: 0xffff_ffff, | ||
| sentinel: 0, | ||
| } | ||
|
|
||
| read_only_csr_field! { | ||
| Mvendorid, | ||
| /// Represents the number of continuation bytes (`0x7f`) in the JEDEC manufacturer ID. | ||
| bank: [7:31], | ||
| } | ||
|
|
||
| /// mvendorid register | ||
| #[derive(Clone, Copy, Debug)] | ||
| pub struct Mvendorid { | ||
| bits: NonZeroUsize, | ||
| read_only_csr_field! { | ||
| Mvendorid, | ||
| /// Represents the final offset field in the JEDEC manufacturer ID. | ||
| /// | ||
| /// # Note | ||
| /// | ||
| /// The encoded value returned by `offset` does not include the odd parity bit (`0x80`). | ||
| offset: [0:6], | ||
| } | ||
|
|
||
| impl Mvendorid { | ||
| /// Returns the contents of the register as raw bits | ||
| #[inline] | ||
| pub fn bits(&self) -> usize { | ||
| self.bits.get() | ||
| } | ||
| /// Represents the JEDEC manufacture continuation byte. | ||
| pub const CONTINUATION: u8 = 0x7f; | ||
|
|
||
| /// Returns the JEDEC manufacturer ID | ||
| #[inline] | ||
| pub fn jedec_manufacturer(&self) -> usize { | ||
| self.bits() >> 7 | ||
| /// Gets the decoded JEDEC manufacturer ID from the `mvendorid` value. | ||
| /// | ||
| /// # Note | ||
| /// | ||
| /// This function returns an iterator over the decoded bytes. | ||
| /// | ||
| /// An iterator is needed because the encoding can theoretically return a max count (`0x1ff_ffff`) of continuation bytes (`0x7f`). | ||
| /// | ||
| /// 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 DONE: usize = usize::MAX; | ||
|
|
||
| let mut bank = self.bank(); | ||
| let offset = self.offset(); | ||
|
|
||
| core::iter::from_fn(move || match bank { | ||
| DONE => None, | ||
| 0 => { | ||
| bank = DONE; | ||
| let parity = ((1 - (offset.count_ones() % 2)) << 7) as usize; | ||
| Some((parity | offset) as u8) | ||
| } | ||
| _ => { | ||
| bank -= 1; | ||
| Some(Self::CONTINUATION) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| read_csr!(0xF11); | ||
| #[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 & (Mvendorid::CONTINUATION as usize); | ||
| let exp_parity = ((1 - (exp_offset.count_ones() % 2)) << 7) as u8; | ||
| let exp_mvendorid = Mvendorid::from_bits(raw); | ||
|
|
||
| /// Reads the CSR | ||
| #[inline] | ||
| pub fn read() -> Option<Mvendorid> { | ||
| let r = unsafe { _read() }; | ||
| // When mvendorid is hardwired to zero it means that the mvendorid | ||
| // csr isn't implemented. | ||
| NonZeroUsize::new(r).map(|bits| Mvendorid { bits }) | ||
| 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); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.