Skip to content

Commit f173422

Browse files
committed
Make pmp_byte() more consise, replace panic!(), and option<Permission>
1 parent bffa133 commit f173422

File tree

1 file changed

+59
-26
lines changed

1 file changed

+59
-26
lines changed

src/register/pmpcfgx.rs

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,14 @@ pub mod pmpcfg0 {
3131
}
3232

3333
#[derive(Clone, Copy, Debug)]
34-
pub struct Pmpcfg0 {
35-
bits: usize,
34+
pub struct PmpByte {
35+
byte: u8,
3636
}
3737

38-
impl Pmpcfg0 {
39-
///Returns the pmp byte associated with the index
40-
#[inline]
41-
pub fn pmp_byte(&self, index: usize) -> usize {
42-
#[cfg(riscv32)]
43-
assert!(index < 4);
44-
45-
#[cfg(riscv64)]
46-
assert!(index < 8);
47-
48-
self.bits.get_bits(8 * index..8 * (index + 1))
49-
}
50-
38+
impl PmpByte {
5139
#[inline]
52-
fn range(&self, byte: usize) -> Range {
53-
match byte.get_bits(4..6) {
40+
fn range(&self) -> Range {
41+
match self.byte.get_bits(4..=5) {
5442
0 => Range::OFF,
5543
1 => Range::TOR,
5644
2 => Range::NA4,
@@ -60,9 +48,9 @@ pub mod pmpcfg0 {
6048
}
6149

6250
#[inline]
63-
fn permission(&self, byte: usize) -> Option<Permission> {
64-
match byte.get_bits(0..3) {
65-
0 => Some(Permission::NONE),
51+
fn permission(&self) -> Option<Permission> {
52+
match self.byte.get_bits(0..=2) {
53+
0 => None,
6654
1 => Some(Permission::R),
6755
2 => Some(Permission::W),
6856
3 => Some(Permission::RW),
@@ -74,14 +62,39 @@ pub mod pmpcfg0 {
7462
}
7563
}
7664

65+
#[inline]
66+
fn locked(&self) -> bool {
67+
self.byte.get_bit(7)
68+
}
69+
}
70+
71+
#[derive(Clone, Copy, Debug)]
72+
pub struct Pmpcfg0 {
73+
bits: usize,
74+
}
75+
76+
impl Pmpcfg0 {
77+
///Returns the pmp byte associated with the index
78+
#[inline]
79+
fn pmp_byte(&self, index: usize) -> PmpByte {
80+
#[cfg(riscv32)]
81+
assert!(index < 4);
82+
83+
#[cfg(riscv64)]
84+
assert!(index < 8);
85+
86+
PmpByte { byte:self.bits.get_bits(8 * index..8 * (index + 1)) as u8 }
87+
}
88+
89+
90+
7791
///Returns pmp[x]cfg configuration structure
7892
#[inline]
7993
pub fn pmp_cfg(&self, index: usize) -> Pmpconfig {
80-
assert!(index < 8);
8194
let byte = self.pmp_byte(index);
82-
let p = self.permission(byte).unwrap();
83-
let r = self.range(byte);
84-
let l = byte.get_bit(7);
95+
let p = byte.permission().unwrap();
96+
let r = byte.range();
97+
let l = byte.locked();
8598

8699
Pmpconfig {
87100
permission: p,
@@ -98,25 +111,45 @@ pub mod pmpcfg0 {
98111

99112
#[inline]
100113
pub unsafe fn set_permissions(permission: Permission, index: usize) {
114+
#[cfg(riscv32)]
115+
assert!(index < 4);
116+
117+
#[cfg(riscv64)]
101118
assert!(index < 8);
102-
_set((permission as usize) << (index * 8));
119+
120+
_write((permission as usize) << (index * 8));
103121
}
104122

105123
#[inline]
106124
pub unsafe fn set_range(range: Range, index: usize) {
125+
#[cfg(riscv32)]
126+
assert!(index < 4);
127+
128+
#[cfg(riscv64)]
107129
assert!(index < 8);
108-
_set((range as usize) << (3 + (index * 8)));
130+
131+
_write((range as usize) << (3 + (index * 8)));
109132
}
110133

111134
#[inline]
112135
pub unsafe fn set_lock(index: usize) {
136+
#[cfg(riscv32)]
137+
assert!(index < 4);
138+
139+
#[cfg(riscv64)]
113140
assert!(index < 8);
141+
114142
_set(1 << (7 + (index * 8)));
115143
}
116144

117145
#[inline]
118146
pub unsafe fn clear_lock(index: usize) {
147+
#[cfg(riscv32)]
148+
assert!(index < 4);
149+
150+
#[cfg(riscv64)]
119151
assert!(index < 8);
152+
120153
_clear(1 << (7 + (index * 8)));
121154
}
122155
}

0 commit comments

Comments
 (0)