File tree Expand file tree Collapse file tree 1 file changed +14
-37
lines changed Expand file tree Collapse file tree 1 file changed +14
-37
lines changed Original file line number Diff line number Diff line change @@ -84,50 +84,27 @@ pub struct Pmpcsr {
84
84
85
85
impl Pmpcsr {
86
86
/// 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
87
93
#[ inline]
88
94
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 ( )
120
96
}
121
97
122
98
/// Attempts to take the register contents, and translate into a Pmp configuration struct.
123
99
#[ inline]
124
100
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 ) ,
131
108
} ?;
132
109
133
110
if index < max {
You can’t perform that action at this time.
0 commit comments