Skip to content

Commit

Permalink
Merge pull request #232 from rmsyn/riscv/marchid-csr-macro
Browse files Browse the repository at this point in the history
riscv: define marchid using CSR macros
  • Loading branch information
jessebraham authored Oct 23, 2024
2 parents bc1ef18 + c473fdb commit 63b748a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
8 changes: 8 additions & 0 deletions riscv/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- CSR helper macro to check for platform implementation

### Changed

- Use CSR helper macros to define `marchid` register

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

### Changed
Expand Down
31 changes: 31 additions & 0 deletions riscv/src/register/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ macro_rules! read_csr_as {
})
}
};

($register:ident, $csr_number:literal, $sentinel:tt) => {
$crate::read_csr!($csr_number);

/// Reads the CSR.
///
/// **WARNING**: panics on targets not implementing this CSR.
#[inline]
pub fn read() -> $register {
try_read().unwrap()
}

/// Attempts to reads the CSR.
#[inline]
pub fn try_read() -> $crate::result::Result<$register> {
match unsafe { _try_read()? } {
$sentinel => Err($crate::result::Error::Unimplemented),
bits => Ok($register { bits }),
}
}
};
}

/// `RV32`: Convenience macro to read a CSR register value as a `register` type.
Expand Down Expand Up @@ -732,6 +753,16 @@ macro_rules! read_only_csr {

$crate::read_csr_as!($ty, $csr);
};

($(#[$doc:meta])+
$ty:ident: $csr:tt,
mask: $mask:tt,
sentinel: $sentinel:tt$(,)?,
) => {
$crate::csr! { $(#[$doc])+ $ty, $mask }

$crate::read_csr_as!($ty, $csr, $sentinel);
};
}

/// Helper macro to create a read-only CSR type.
Expand Down
30 changes: 5 additions & 25 deletions riscv/src/register/marchid.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
//! marchid register
use core::num::NonZeroUsize;

/// marchid register
#[derive(Clone, Copy, Debug)]
pub struct Marchid {
bits: NonZeroUsize,
}

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

read_csr!(0xF12);

/// Reads the CSR
#[inline]
pub fn read() -> Option<Marchid> {
let r = unsafe { _read() };
// When marchid is hardwired to zero it means that the marchid
// csr isn't implemented.
NonZeroUsize::new(r).map(|bits| Marchid { bits })
read_only_csr! {
/// `marchid` register
Marchid: 0xF12,
mask: 0xffff_ffff,
sentinel: 0,
}

0 comments on commit 63b748a

Please sign in to comment.