Skip to content

Commit ca4850a

Browse files
Merge pull request #242 from rmsyn/riscv/mip-csr-macro
riscv: define mip using CSR macros
2 parents 517d52b + 803a68c commit ca4850a

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

riscv/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2222
- Use CSR helper macros to define `mie` register
2323
- Use CSR helper macros to define `mimpid` register
2424
- Use CSR helper macros to define `misa` register
25+
- Use CSR helper macros to define `mip` register
2526

2627
## [v0.12.1] - 2024-10-20
2728

riscv/src/register/mip.rs

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,47 @@
11
//! mip register
22
3-
/// mip register
4-
#[derive(Clone, Copy, Debug)]
5-
pub struct Mip {
6-
bits: usize,
3+
read_write_csr! {
4+
/// `mip` register
5+
Mip: 0x344,
6+
mask: 0xaaa,
77
}
88

9-
impl Mip {
10-
/// Returns the contents of the register as raw bits
11-
#[inline]
12-
pub fn bits(&self) -> usize {
13-
self.bits
14-
}
15-
9+
read_write_csr_field! {
10+
Mip,
1611
/// Supervisor Software Interrupt Pending
17-
#[inline]
18-
pub fn ssoft(&self) -> bool {
19-
self.bits & (1 << 1) != 0
20-
}
12+
ssoft: 1,
13+
}
2114

15+
read_only_csr_field! {
16+
Mip,
2217
/// Machine Software Interrupt Pending
23-
#[inline]
24-
pub fn msoft(&self) -> bool {
25-
self.bits & (1 << 3) != 0
26-
}
18+
msoft: 3,
19+
}
2720

21+
read_write_csr_field! {
22+
Mip,
2823
/// Supervisor Timer Interrupt Pending
29-
#[inline]
30-
pub fn stimer(&self) -> bool {
31-
self.bits & (1 << 5) != 0
32-
}
24+
stimer: 5,
25+
}
3326

27+
read_only_csr_field! {
28+
Mip,
3429
/// Machine Timer Interrupt Pending
35-
#[inline]
36-
pub fn mtimer(&self) -> bool {
37-
self.bits & (1 << 7) != 0
38-
}
30+
mtimer: 7,
31+
}
3932

33+
read_write_csr_field! {
34+
Mip,
4035
/// Supervisor External Interrupt Pending
41-
#[inline]
42-
pub fn sext(&self) -> bool {
43-
self.bits & (1 << 9) != 0
44-
}
36+
sext: 9,
37+
}
4538

39+
read_only_csr_field! {
40+
Mip,
4641
/// Machine External Interrupt Pending
47-
#[inline]
48-
pub fn mext(&self) -> bool {
49-
self.bits & (1 << 11) != 0
50-
}
42+
mext: 11,
5143
}
5244

53-
read_csr_as!(Mip, 0x344);
5445
set!(0x344);
5546
clear!(0x344);
5647

@@ -63,3 +54,25 @@ set_clear_csr!(
6354
set_clear_csr!(
6455
/// Supervisor External Interrupt Pending
6556
, set_sext, clear_sext, 1 << 9);
57+
58+
#[cfg(test)]
59+
mod tests {
60+
use super::*;
61+
62+
#[test]
63+
fn test_mip() {
64+
let mut m = Mip::from_bits(0);
65+
66+
test_csr_field!(m, ssoft);
67+
test_csr_field!(m, stimer);
68+
test_csr_field!(m, sext);
69+
70+
assert!(!m.msoft());
71+
assert!(!m.mtimer());
72+
assert!(!m.mext());
73+
74+
assert!(Mip::from_bits(1 << 3).msoft());
75+
assert!(Mip::from_bits(1 << 7).mtimer());
76+
assert!(Mip::from_bits(1 << 11).mext());
77+
}
78+
}

0 commit comments

Comments
 (0)