Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 2 additions & 0 deletions riscv/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Add `Mstatus::from(usize)` for use in unit tests
- Add `Mstatus.bits()`
- Add `Eq` and `PartialEq` for `pmpcfgx::{Range, Permission}`
- Add `Mstatus::update_*` helpers to manipulate Mstatus values without touching
the CSR
- Export `riscv::register::macros` module macros for external use

### Fixed
Expand Down
17 changes: 17 additions & 0 deletions riscv/src/bits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// Insert a new value into a bitfield
///
/// `value` is masked to `width` bits and inserted into `orig`.`
#[inline]
pub fn bf_insert(orig: usize, bit: usize, width: usize, value: usize) -> usize {
let mask = (1 << width) - 1;
orig & !(mask << bit) | ((value & mask) << bit)
}

/// Extract a value from a bitfield
///
/// Extracts `width` bits from bit offset `bit` and returns it shifted to bit 0.s
#[inline]
pub fn bf_extract(orig: usize, bit: usize, width: usize) -> usize {
let mask = (1 << width) - 1;
(orig >> bit) & mask
}
1 change: 1 addition & 0 deletions riscv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#![allow(clippy::missing_safety_doc)]

pub mod asm;
pub(crate) mod bits;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can leave it as this, but now I don't find any reason why we should hide it from dependents. Maybe PACs find them handy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd hold off on that until we're confident it's a stable public api.

pub mod delay;
pub mod interrupt;
pub mod register;
Expand Down
28 changes: 28 additions & 0 deletions riscv/src/register/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,34 @@ macro_rules! write_csr_rv32 {
};
}

/// Convenience macro to write a value with `bits` to a CSR
#[macro_export]
macro_rules! write_csr_as {
($csr_type:ty, $csr_number:literal) => {
$crate::write_csr!($csr_number);

/// Writes the CSR
#[inline]
pub fn write(value: $csr_type) {
unsafe { _write(value.bits) }
}
};
}

/// Convenience macro to write a value to a CSR register.
#[macro_export]
macro_rules! write_csr_as_rv32 {
($csr_type:ty, $csr_number:literal) => {
$crate::write_csr_rv32!($csr_number);

/// Writes the CSR
#[inline]
pub fn write(value: $csr_type) {
unsafe { _write(value.bits) }
}
};
}

/// Convenience macro to write a [`usize`] value to a CSR register.
#[macro_export]
macro_rules! write_csr_as_usize {
Expand Down
6 changes: 3 additions & 3 deletions riscv/src/register/misa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub struct Misa {
/// Base integer ISA width
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum XLEN {
XLEN32,
XLEN64,
XLEN128,
XLEN32 = 1,
XLEN64 = 2,
XLEN128 = 3,
}

impl XLEN {
Expand Down
Loading