Skip to content

Commit 245872f

Browse files
committed
riscv: define mcause using CSR macros
Uses CSR helper macros to define the `mcause` register.
1 parent 64957b3 commit 245872f

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

riscv/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515

1616
- Use CSR helper macros to define `marchid` register
1717
- Re-use `try_*` functions in `mcountinhibit`
18+
- Use CSR helper macros to define `mcause` register
1819

1920
## [v0.12.1] - 2024-10-20
2021

riscv/src/register/mcause.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,41 @@
22
33
pub use crate::interrupt::Trap;
44

5-
/// mcause register
6-
#[derive(Clone, Copy, Debug)]
7-
pub struct Mcause {
8-
bits: usize,
5+
read_only_csr! {
6+
/// `mcause` register
7+
Mcause: 0x342,
8+
mask: 0xffff_ffff,
99
}
1010

11-
impl From<usize> for Mcause {
12-
#[inline]
13-
fn from(bits: usize) -> Self {
14-
Self { bits }
15-
}
11+
#[cfg(target_arch = "riscv32")]
12+
read_only_csr_field! {
13+
Mcause,
14+
/// Returns the `code` field.
15+
code: [0:30],
1616
}
1717

18-
impl Mcause {
19-
/// Returns the contents of the register as raw bits
20-
#[inline]
21-
pub fn bits(&self) -> usize {
22-
self.bits
23-
}
18+
#[cfg(not(target_arch = "riscv32"))]
19+
read_only_csr_field! {
20+
Mcause,
21+
/// Returns the `code` field.
22+
code: [0:62],
23+
}
2424

25-
/// Returns the code field
26-
#[inline]
27-
pub fn code(&self) -> usize {
28-
self.bits & !(1 << (usize::BITS as usize - 1))
29-
}
25+
#[cfg(target_arch = "riscv32")]
26+
read_only_csr_field! {
27+
Mcause,
28+
/// Is the trap cause an interrupt.
29+
is_interrupt: 31,
30+
}
31+
32+
#[cfg(not(target_arch = "riscv32"))]
33+
read_only_csr_field! {
34+
Mcause,
35+
/// Is the trap cause an interrupt.
36+
is_interrupt: 63,
37+
}
3038

39+
impl Mcause {
3140
/// Returns the trap cause represented by this register.
3241
///
3342
/// # Note
@@ -43,17 +52,9 @@ impl Mcause {
4352
}
4453
}
4554

46-
/// Is trap cause an interrupt.
47-
#[inline]
48-
pub fn is_interrupt(&self) -> bool {
49-
self.bits & (1 << (usize::BITS as usize - 1)) != 0
50-
}
51-
5255
/// Is trap cause an exception.
5356
#[inline]
5457
pub fn is_exception(&self) -> bool {
5558
!self.is_interrupt()
5659
}
5760
}
58-
59-
read_csr_as!(Mcause, 0x342);

0 commit comments

Comments
 (0)