Skip to content

Commit

Permalink
riscv: add CSR unimplemented helper variant
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rmsyn committed Oct 22, 2024
1 parent bc1ef18 commit c2fc408
Showing 1 changed file with 31 additions and 0 deletions.
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

0 comments on commit c2fc408

Please sign in to comment.