Skip to content
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 @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Add `read_bits`/`try_read_bits` and `write_bits`/`try_write_bits` to the modules generated by `read_csr_as_usize!` and `write_csr_as_usize!`, as aliases of `read`/`write` so that a generated CSR module offers the same raw accessors no matter which macro defined it.
- Add `mireg` CSR
- Add `stopei` and `vstopei` CSRs (AIA supervisor/virtual-supervisor top external
interrupt), completing the `*topei` set alongside `mtopei`.
Expand Down
51 changes: 51 additions & 0 deletions riscv/src/register/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ macro_rules! read_csr_as_usize {
pub fn try_read() -> $crate::result::Result<usize> {
unsafe { _try_read() }
}

/// Reads the raw bits of the CSR.
///
/// Alias of [read] for modules whose value is already a plain
/// [`usize`].
///
/// **WARNING**: panics on non-`riscv` targets.
#[inline]
pub fn read_bits() -> usize {
unsafe { _read() }
}

/// Attempts to read the raw bits of the CSR.
#[inline]
pub fn try_read_bits() -> $crate::result::Result<usize> {
unsafe { _try_read() }
}
};
}

Expand Down Expand Up @@ -270,6 +287,23 @@ macro_rules! write_csr_as_usize {
pub unsafe fn try_write(bits: usize) -> $crate::result::Result<()> {
_try_write(bits)
}

/// Writes the raw bits of the CSR.
///
/// Alias of [write] for modules whose value is already a plain
/// [`usize`].
///
/// **WARNING**: panics on non-`riscv` targets.
#[inline]
pub unsafe fn write_bits(bits: usize) {
_write(bits);
}

/// Attempts to write the raw bits of the CSR.
#[inline]
pub unsafe fn try_write_bits(bits: usize) -> $crate::result::Result<()> {
_try_write(bits)
}
};
(safe $csr_number:literal, $($cfg:meta),*) => {
$crate::write_csr!($csr_number, $($cfg),*);
Expand All @@ -287,6 +321,23 @@ macro_rules! write_csr_as_usize {
pub fn try_write(bits: usize) -> $crate::result::Result<()> {
unsafe { _try_write(bits) }
}

/// Writes the raw bits of the CSR.
///
/// Alias of [write] for modules whose value is already a plain
/// [`usize`].
///
/// **WARNING**: panics on non-`riscv` targets.
#[inline]
pub fn write_bits(bits: usize) {
unsafe { _write(bits) }
}

/// Attempts to write the raw bits of the CSR.
#[inline]
pub fn try_write_bits(bits: usize) -> $crate::result::Result<()> {
unsafe { _try_write(bits) }
}
};
}

Expand Down
1 change: 1 addition & 0 deletions riscv/src/register/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod read_only_csr;
mod read_write_csr;
mod read_write_csr_as_usize;
mod write_only_csr;
53 changes: 53 additions & 0 deletions riscv/src/register/tests/read_write_csr_as_usize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::result::{Error, Result};

read_write_csr_as_usize!(0x000);

// we don't test the `read` and `write` functions, we are only testing in-memory functions.
#[allow(unused)]
pub fn _read_csr() -> usize {
read()
}

#[allow(unused)]
pub fn _try_read_csr() -> Result<usize> {
try_read()
}

#[allow(unused)]
pub fn _write_csr(bits: usize) {
unsafe { write(bits) };
}

#[allow(unused)]
pub fn _try_write_csr(bits: usize) -> Result<()> {
unsafe { try_write(bits) }
}

#[allow(unused)]
pub fn _read_csr_bits() -> usize {
read_bits()
}

#[allow(unused)]
pub fn _try_read_csr_bits() -> Result<usize> {
try_read_bits()
}

#[allow(unused)]
pub fn _write_csr_bits(bits: usize) {
unsafe { write_bits(bits) };
}

#[allow(unused)]
pub fn _try_write_csr_bits(bits: usize) -> Result<()> {
unsafe { try_write_bits(bits) }
}

#[test]
fn test_usize_raw_bits() {
assert_eq!(try_read_bits(), Err(Error::Unimplemented));
assert_eq!(
unsafe { try_write_bits(usize::MAX) },
Err(Error::Unimplemented)
);
}
Loading