Skip to content

Commit d88a373

Browse files
committed
fixup: recommended changes for register::pmpcfgx
1 parent 008323c commit d88a373

File tree

1 file changed

+14
-37
lines changed

1 file changed

+14
-37
lines changed

riscv/src/register/pmpcfgx.rs

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -84,50 +84,27 @@ pub struct Pmpcsr {
8484

8585
impl Pmpcsr {
8686
/// Take the register contents and translate into a Pmp configuration struct
87+
///
88+
/// **WARNING**: panics on:
89+
///
90+
/// - non-`riscv` targets
91+
/// - `index` is out of bounds
92+
/// - register fields contain invalid values
8793
#[inline]
8894
pub fn into_config(&self, index: usize) -> Pmp {
89-
#[cfg(riscv32)]
90-
assert!(index < 4);
91-
92-
#[cfg(riscv64)]
93-
assert!(index < 8);
94-
95-
let byte = (self.bits >> (8 * index)) as u8; // move config to LSB and drop the rest
96-
let permission = byte & 0x7; // bits 0-2
97-
let range = (byte >> 3) & 0x3; // bits 3-4
98-
Pmp {
99-
byte,
100-
permission: match permission {
101-
0 => Permission::NONE,
102-
1 => Permission::R,
103-
2 => Permission::W,
104-
3 => Permission::RW,
105-
4 => Permission::X,
106-
5 => Permission::RX,
107-
6 => Permission::WX,
108-
7 => Permission::RWX,
109-
_ => unreachable!(),
110-
},
111-
range: match range {
112-
0 => Range::OFF,
113-
1 => Range::TOR,
114-
2 => Range::NA4,
115-
3 => Range::NAPOT,
116-
_ => unreachable!(),
117-
},
118-
locked: (byte & (1 << 7)) != 0,
119-
}
95+
self.try_into_config(index).unwrap()
12096
}
12197

12298
/// Attempts to take the register contents, and translate into a Pmp configuration struct.
12399
#[inline]
124100
pub fn try_into_config(&self, index: usize) -> Result<Pmp> {
125-
let max = if cfg!(riscv32) {
126-
Ok(4usize)
127-
} else if cfg!(riscv64) {
128-
Ok(8usize)
129-
} else {
130-
Err(Error::Unimplemented)
101+
let max = match () {
102+
#[cfg(riscv32)]
103+
() => Ok(4usize),
104+
#[cfg(riscv64)]
105+
() => Ok(8usize),
106+
#[cfg(not(any(riscv32, riscv64)))]
107+
() => Err(Error::Unimplemented),
131108
}?;
132109

133110
if index < max {

0 commit comments

Comments
 (0)