Skip to content

Commit 63b748a

Browse files
authored
Merge pull request #232 from rmsyn/riscv/marchid-csr-macro
riscv: define marchid using CSR macros
2 parents bc1ef18 + c473fdb commit 63b748a

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

Diff for: riscv/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- CSR helper macro to check for platform implementation
13+
14+
### Changed
15+
16+
- Use CSR helper macros to define `marchid` register
17+
1018
## [v0.12.1] - 2024-10-20
1119

1220
### Changed

Diff for: riscv/src/register/macros.rs

+31
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ macro_rules! read_csr_as {
9292
})
9393
}
9494
};
95+
96+
($register:ident, $csr_number:literal, $sentinel:tt) => {
97+
$crate::read_csr!($csr_number);
98+
99+
/// Reads the CSR.
100+
///
101+
/// **WARNING**: panics on targets not implementing this CSR.
102+
#[inline]
103+
pub fn read() -> $register {
104+
try_read().unwrap()
105+
}
106+
107+
/// Attempts to reads the CSR.
108+
#[inline]
109+
pub fn try_read() -> $crate::result::Result<$register> {
110+
match unsafe { _try_read()? } {
111+
$sentinel => Err($crate::result::Error::Unimplemented),
112+
bits => Ok($register { bits }),
113+
}
114+
}
115+
};
95116
}
96117

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

733754
$crate::read_csr_as!($ty, $csr);
734755
};
756+
757+
($(#[$doc:meta])+
758+
$ty:ident: $csr:tt,
759+
mask: $mask:tt,
760+
sentinel: $sentinel:tt$(,)?,
761+
) => {
762+
$crate::csr! { $(#[$doc])+ $ty, $mask }
763+
764+
$crate::read_csr_as!($ty, $csr, $sentinel);
765+
};
735766
}
736767

737768
/// Helper macro to create a read-only CSR type.

Diff for: riscv/src/register/marchid.rs

+5-25
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,8 @@
11
//! marchid register
22
3-
use core::num::NonZeroUsize;
4-
5-
/// marchid register
6-
#[derive(Clone, Copy, Debug)]
7-
pub struct Marchid {
8-
bits: NonZeroUsize,
9-
}
10-
11-
impl Marchid {
12-
/// Returns the contents of the register as raw bits
13-
#[inline]
14-
pub fn bits(&self) -> usize {
15-
self.bits.get()
16-
}
17-
}
18-
19-
read_csr!(0xF12);
20-
21-
/// Reads the CSR
22-
#[inline]
23-
pub fn read() -> Option<Marchid> {
24-
let r = unsafe { _read() };
25-
// When marchid is hardwired to zero it means that the marchid
26-
// csr isn't implemented.
27-
NonZeroUsize::new(r).map(|bits| Marchid { bits })
3+
read_only_csr! {
4+
/// `marchid` register
5+
Marchid: 0xF12,
6+
mask: 0xffff_ffff,
7+
sentinel: 0,
288
}

0 commit comments

Comments
 (0)