Skip to content

Commit 86531a7

Browse files
Merge pull request #370 from KushalMeghani1644/dscratch0_and_dscratch1
Implement dscratch0 and dscratch1 CSRs for RISC-V
2 parents 8eed873 + f8565d9 commit 86531a7

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

riscv/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- Add `dcsratch0` and `dscratch1` CSRs
13+
- Add new `read-write_csr_as_usize` macro for registers
1214
- Add `dpc` CSR support for RISC-V
1315
- Add Mtopi
1416
- Added DCSR (Debug Control and Status Register) CSR support for the RISC-V

riscv/src/register.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,5 @@ mod tests;
132132
// TODO: Debug Mode Registers
133133
pub mod dcsr;
134134
pub mod dpc;
135+
pub mod dscratch0;
136+
pub mod dscratch1;

riscv/src/register/dscratch0.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! dscratch0
2+
3+
read_write_csr_as_usize!(0x7b2);
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use super::*;
8+
use crate::result::Error;
9+
10+
#[test]
11+
fn test_dscratch0_read_write() {
12+
for i in 0..usize::BITS {
13+
let val = 1usize << i;
14+
assert_eq!(unsafe { try_write(val) }, Err(Error::Unimplemented));
15+
assert_eq!(try_read(), Err(Error::Unimplemented));
16+
}
17+
}
18+
}

riscv/src/register/dscratch1.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! dscratch1
2+
3+
read_write_csr_as_usize!(0x7b3);
4+
5+
#[cfg(test)]
6+
mod tests {
7+
use super::*;
8+
use crate::result::Error;
9+
10+
#[test]
11+
fn test_dscratch1_read_write() {
12+
for i in 0..usize::BITS {
13+
let val = 1usize << i;
14+
assert_eq!(unsafe { try_write(val) }, Err(Error::Unimplemented));
15+
assert_eq!(try_read(), Err(Error::Unimplemented));
16+
}
17+
}
18+
}

riscv/src/register/macros.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,30 @@ macro_rules! write_csr_as_usize_rv32 {
301301
};
302302
}
303303

304+
/// Convenience macro to provide combined read/write of a CSR as a `usize`.
305+
///
306+
/// This composes [`read_csr_as_usize`] and [`write_csr_as_usize`]. Use the
307+
/// `safe` form to get safe wrappers instead of unsafe.
308+
#[macro_export]
309+
macro_rules! read_write_csr_as_usize {
310+
($csr_number:literal) => {
311+
$crate::read_csr_as_usize!($csr_number);
312+
$crate::write_csr_as_usize!($csr_number);
313+
};
314+
(safe $csr_number:literal) => {
315+
$crate::read_csr_as_usize!($csr_number);
316+
$crate::write_csr_as_usize!(safe $csr_number);
317+
};
318+
($csr_number:literal, $($cfg:meta),*) => {
319+
$crate::read_csr_as_usize!($csr_number, $($cfg),*);
320+
$crate::write_csr_as_usize!($csr_number, $($cfg),*);
321+
};
322+
(safe $csr_number:literal, $($cfg:meta),*) => {
323+
$crate::read_csr_as_usize!($csr_number, $($cfg),*);
324+
$crate::write_csr_as_usize!(safe $csr_number, $($cfg),*);
325+
};
326+
}
327+
304328
/// Convenience macro around the `csrrs` assembly instruction to set the CSR register.
305329
///
306330
/// This macro is intended for use with the [set_csr](crate::set_csr) or [set_clear_csr](crate::set_clear_csr) macros.

0 commit comments

Comments
 (0)