Skip to content

Commit bffa133

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

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

src/register/pmpcfgx.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,13 @@ pub mod pmpcfg0 {
3939
///Returns the pmp byte associated with the index
4040
#[inline]
4141
pub fn pmp_byte(&self, index: usize) -> usize {
42+
#[cfg(riscv32)]
43+
assert!(index < 4);
44+
45+
#[cfg(riscv64)]
4246
assert!(index < 8);
43-
match index {
44-
0 => self.bits.get_bits(0..8),
45-
1 => self.bits.get_bits(8..16),
46-
2 => self.bits.get_bits(16..24),
47-
3 => self.bits.get_bits(24..32),
48-
4 => self.bits.get_bits(32..40),
49-
5 => self.bits.get_bits(40..48),
50-
6 => self.bits.get_bits(48..56),
51-
7 => self.bits.get_bits(56..64),
52-
_ => panic!(),
53-
}
47+
48+
self.bits.get_bits(8 * index..8 * (index + 1))
5449
}
5550

5651
#[inline]
@@ -60,22 +55,22 @@ pub mod pmpcfg0 {
6055
1 => Range::TOR,
6156
2 => Range::NA4,
6257
3 => Range::NAPOT,
63-
_ => panic!(),
58+
_ => unreachable!(),
6459
}
6560
}
6661

6762
#[inline]
68-
fn permission(&self, byte: usize) -> Permission {
69-
match byte.get_bits(0..4) {
70-
0 => Permission::NONE,
71-
1 => Permission::R,
72-
2 => Permission::W,
73-
3 => Permission::RW,
74-
4 => Permission::X,
75-
5 => Permission::RX,
76-
6 => Permission::WX,
77-
7 => Permission::RWX,
78-
_ => panic!(),
63+
fn permission(&self, byte: usize) -> Option<Permission> {
64+
match byte.get_bits(0..3) {
65+
0 => Some(Permission::NONE),
66+
1 => Some(Permission::R),
67+
2 => Some(Permission::W),
68+
3 => Some(Permission::RW),
69+
4 => Some(Permission::X),
70+
5 => Some(Permission::RX),
71+
6 => Some(Permission::WX),
72+
7 => Some(Permission::RWX),
73+
_ => None,
7974
}
8075
}
8176

@@ -84,7 +79,7 @@ pub mod pmpcfg0 {
8479
pub fn pmp_cfg(&self, index: usize) -> Pmpconfig {
8580
assert!(index < 8);
8681
let byte = self.pmp_byte(index);
87-
let p = self.permission(byte);
82+
let p = self.permission(byte).unwrap();
8883
let r = self.range(byte);
8984
let l = byte.get_bit(7);
9085

0 commit comments

Comments
 (0)