Skip to content
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

riscv: define mvendorid CSR with macro helpers #255

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions riscv/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Use CSR helper macros to define `mstatus` register
- Use CSR helper macros to define `mstatush` register
- Use CSR helper macros to define `mtvec` register
- Use CSR helper macros to define `mtvendorid` register

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

Expand Down
48 changes: 22 additions & 26 deletions riscv/src/register/mvendorid.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
//! mvendorid register

use core::num::NonZeroUsize;

/// mvendorid register
#[derive(Clone, Copy, Debug)]
pub struct Mvendorid {
bits: NonZeroUsize,
read_only_csr! {
/// `mvendorid` register
Mvendorid: 0xF11,
mask: 0xffff_ffff,
sentinel: 0,
}

impl Mvendorid {
/// Returns the contents of the register as raw bits
#[inline]
pub fn bits(&self) -> usize {
self.bits.get()
}

/// Returns the JEDEC manufacturer ID
#[inline]
pub fn jedec_manufacturer(&self) -> usize {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add the jedec_manufacturer field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, I missed that function when converting to the CSR macro.

After reading the ISA again, it looks like the jedec_manufacturer implementation was incorrect.

mvendorid encodes the number of one-byte continuation codes in the Bank field, and
encodes the final byte in the Offset field, discarding the parity bit. For example, the JEDEC
manufacturer ID 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x7f 0x8a (twelve
continuation codes followed by 0x8a) would be encoded in the mvendorid CSR as 0x60a.

So, we would probably want to add bank and offset fields, along with a correct jedec_manufacturer function that decodes the ID:

pub const fn jedec_manufacturer(&self) -> (usize, usize) {
    (self.bank(), 0x80 | self.offset())
}

self.bits() >> 7
}
read_only_csr_field! {
Mvendorid,
/// Represents the number of continuation bytes (`0x7f`) in the JEDEC manufacturer ID.
bank: [7:31],
}

read_csr!(0xF11);
read_only_csr_field! {
Mvendorid,
/// Represents the final offset field in the JEDEC manufacturer ID.
offset: [0:6],
}

/// 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 })
impl Mvendorid {
/// Gets the JEDEC manufacturer ID decoded into parts:
///
/// - `bank`: the number of continuation bytes (`0x7f`)
/// - `offset`: specific offset in the JEDEC bank number
pub fn jedec_manufacturer(&self) -> (usize, usize) {
(self.bank(), (0x80 | self.offset()))
}
}
Loading