Skip to content

Commit c2fc408

Browse files
committed
riscv: add CSR unimplemented helper variant
Adds a variant arm to the read-only CSR helper macro to create a CSR register type which may not be implemented on the platform. Takes a sentinel value to test whether the CSR is implemented.
1 parent bc1ef18 commit c2fc408

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

riscv/src/register/macros.rs

Lines changed: 31 additions & 0 deletions
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.

0 commit comments

Comments
 (0)