Skip to content

Commit 08886c1

Browse files
committed
riscv: define scause CSR with macro helpers
Uses CSR macro helpers to define the `scause` CSR register.
1 parent f92d787 commit 08886c1

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

riscv/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3232
- Use CSR helper macros to define `mtvendorid` register
3333
- Use CSR helper macros to define `satp` register
3434
- Use CSR helper macros to define `pmpcfgx` field types
35+
- Use CSR helper macros to define `scause` field types
3536

3637
## [v0.12.1] - 2024-10-20
3738

riscv/src/register/scause.rs

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,47 @@
33
pub use crate::interrupt::Trap;
44
pub use riscv_pac::{CoreInterruptNumber, ExceptionNumber, InterruptNumber}; // re-export useful riscv-pac traits
55

6-
/// scause register
7-
#[derive(Clone, Copy)]
8-
pub struct Scause {
9-
bits: usize,
6+
read_write_csr! {
7+
/// scause register
8+
Scause: 0x142,
9+
mask: usize::MAX,
1010
}
1111

12-
impl Scause {
13-
/// Returns the contents of the register as raw bits
14-
#[inline]
15-
pub fn bits(&self) -> usize {
16-
self.bits
17-
}
12+
#[cfg(target_arch = "riscv32")]
13+
read_write_csr_field! {
14+
Scause,
15+
/// Returns the type of the trap:
16+
///
17+
/// - `true`: an interrupt caused the trap
18+
/// - `false`: an exception caused the trap
19+
interrupt: 31,
20+
}
1821

22+
#[cfg(not(target_arch = "riscv32"))]
23+
read_write_csr_field! {
24+
Scause,
25+
/// Returns the type of the trap:
26+
///
27+
/// - `true`: an interrupt caused the trap
28+
/// - `false`: an exception caused the trap
29+
interrupt: 63,
30+
}
31+
32+
#[cfg(target_arch = "riscv32")]
33+
read_write_csr_field! {
34+
Scause,
1935
/// Returns the code field
20-
#[inline]
21-
pub fn code(&self) -> usize {
22-
self.bits & !(1 << (usize::BITS as usize - 1))
23-
}
36+
code: [0:30],
37+
}
2438

39+
#[cfg(not(target_arch = "riscv32"))]
40+
read_write_csr_field! {
41+
Scause,
42+
/// Returns the code field
43+
code: [0:62],
44+
}
45+
46+
impl Scause {
2547
/// Returns the trap cause represented by this register.
2648
///
2749
/// # Note
@@ -40,25 +62,16 @@ impl Scause {
4062
/// Is trap cause an interrupt.
4163
#[inline]
4264
pub fn is_interrupt(&self) -> bool {
43-
self.bits & (1 << (usize::BITS as usize - 1)) != 0
65+
self.interrupt()
4466
}
4567

4668
/// Is trap cause an exception.
4769
#[inline]
4870
pub fn is_exception(&self) -> bool {
49-
!self.is_interrupt()
71+
!self.interrupt()
5072
}
5173
}
5274

53-
read_csr_as!(Scause, 0x142);
54-
write_csr!(0x142);
55-
56-
/// Writes the CSR
57-
#[inline]
58-
pub unsafe fn write(bits: usize) {
59-
_write(bits)
60-
}
61-
6275
/// Set supervisor cause register to corresponding cause.
6376
#[inline]
6477
pub unsafe fn set<I: CoreInterruptNumber, E: ExceptionNumber>(cause: Trap<I, E>) {

0 commit comments

Comments
 (0)