Skip to content

Commit 77747ef

Browse files
committed
Rebase to make pmp<x>cfg methods reusable
1 parent 9c3a57f commit 77747ef

File tree

3 files changed

+73
-51
lines changed

3 files changed

+73
-51
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ extern crate bit_field;
2121

2222
pub mod asm;
2323
pub mod interrupt;
24-
pub mod register;
24+
pub mod register;

src/register/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
//! - cycleh
66
//! - timeh
77
//! - instreth
8-
//! - hpmcounter[3-31]h
8+
//! - hpmcounter<3-31>h
99
//! - mcycleh
1010
//! - minstreth
11-
//! - mhpmcounter[3-31]h
11+
//! - mhpmcounter<3-31>h
1212
1313
#[macro_use]
1414
mod macros;

src/register/pmpcfgx.rs

Lines changed: 70 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/// Physical memory protection configuration
22
use bit_field::BitField;
33

4-
/// Permission enum contains all possible pmp register permission configurations
4+
/*************************************************************************************************/
5+
/// Permission enum contains all possible permission modes for pmp registers
56
#[derive(Clone, Copy, Debug)]
67
pub enum Permission {
78
NONE = 0,
@@ -22,36 +23,34 @@ pub enum Range {
2223
NA4 = 2,
2324
NAPOT = 3,
2425
}
25-
26-
/// Pmpconfig struct to hold pmp register's current settings
27-
#[derive(Clone, Copy, Debug)]
28-
pub struct Pmpconfig {
29-
pub permission: Permission,
30-
pub range_type: Range,
31-
pub locked: bool,
32-
}
33-
26+
/*************************************************************************************************/
27+
/// PmpByte holds the a single pmp configuration
3428
#[derive(Clone, Copy, Debug)]
3529
pub struct PmpByte {
36-
byte: u8,
30+
pub byte: u8,
31+
//permission: Option<Permission>,
32+
//range: Option<Range>,
33+
//locked: bool
3734
}
35+
/// PmpByte methods to get a pmp configuration attributes
36+
impl PmpByte{
37+
pub fn is_locked(&self) -> bool {
38+
self.byte.get_bit(7)
39+
}
3840

39-
impl PmpByte {
40-
#[inline]
41-
fn range(&self) -> Range {
41+
pub fn get_range(&self) -> Option<Range> {
4242
match self.byte.get_bits(4..=5) {
43-
0 => Range::OFF,
44-
1 => Range::TOR,
45-
2 => Range::NA4,
46-
3 => Range::NAPOT,
47-
_ => unreachable!(),
43+
0 => Some(Range::OFF),
44+
1 => Some(Range::TOR),
45+
2 => Some(Range::NA4),
46+
3 => Some(Range::NAPOT),
47+
_ => None,
4848
}
4949
}
5050

51-
#[inline]
52-
fn permission(&self) -> Option<Permission> {
51+
pub fn get_permission(&self) -> Option<Permission> {
5352
match self.byte.get_bits(0..=2) {
54-
0 => None,
53+
0 => Some(Permission::NONE),
5554
1 => Some(Permission::R),
5655
2 => Some(Permission::W),
5756
3 => Some(Permission::RW),
@@ -62,47 +61,29 @@ impl PmpByte {
6261
_ => None,
6362
}
6463
}
65-
66-
#[inline]
67-
fn locked(&self) -> bool {
68-
self.byte.get_bit(7)
69-
}
7064
}
65+
/*************************************************************************************************/
66+
67+
/// Physical memory protection configuration
7168
pub mod pmpcfg0 {
72-
use super::{PmpByte,Pmpconfig,Permission,Range,BitField};
7369

70+
/// Pmpcf0 struct contains pmp0cfg - pmp3cfg for 32-bit arch, or pmp0cfg - pmp7cfg for 64-bit arch
71+
/// get_byte() method retrieves a single pmp<x>cfg held in a PmpByte struct
7472
#[derive(Clone, Copy, Debug)]
7573
pub struct Pmpcfg0 {
76-
bits: usize,
74+
pub bits: u32,
7775
}
78-
7976
impl Pmpcfg0 {
80-
///Returns the pmp byte associated with the index
8177
#[inline]
82-
fn pmp_byte(&self, index: usize) -> PmpByte {
78+
pub fn get_byte(&self,index:usize) -> PmpByte {
8379
#[cfg(riscv32)]
8480
assert!(index < 4);
8581

8682
#[cfg(riscv64)]
8783
assert!(index < 8);
8884

8985
PmpByte {
90-
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8,
91-
}
92-
}
93-
94-
///Returns pmpxcfg configuration structure
95-
#[inline]
96-
pub fn pmp_cfg(&self, index: usize) -> Pmpconfig {
97-
let byte = self.pmp_byte(index);
98-
let p = byte.permission().unwrap();
99-
let r = byte.range();
100-
let l = byte.locked();
101-
102-
Pmpconfig {
103-
permission: p,
104-
range_type: r,
105-
locked: l,
86+
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8
10687
}
10788
}
10889
}
@@ -159,18 +140,59 @@ pub mod pmpcfg0 {
159140

160141
/// Physical memory protection configuration, RV32 only
161142
pub mod pmpcfg1 {
143+
144+
#[derive(Clone, Copy, Debug)]
145+
pub struct Pmpcfg1 {
146+
pub bits: u32,
147+
}
148+
impl Pmpcfg1 {
149+
#[inline]
150+
pub fn get_byte(&self,index:usize) -> PmpByte {
151+
PmpByte {
152+
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8
153+
}
154+
}
155+
}
156+
162157
read_csr_as_usize_rv32!(0x3A1, __read_pmpcfg1);
163158
write_csr_as_usize_rv32!(0x3A1, __write_pmpcfg1);
164159
}
165160

166161
/// Physical memory protection configuration
167162
pub mod pmpcfg2 {
163+
164+
#[derive(Clone, Copy, Debug)]
165+
pub struct Pmpcfg2 {
166+
pub bits: u32,
167+
}
168+
impl Pmpcfg2 {
169+
#[inline]
170+
pub fn get_byte(&self,index:usize) -> PmpByte {
171+
PmpByte {
172+
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8
173+
}
174+
}
175+
}
176+
168177
read_csr_as_usize!(0x3A2, __read_pmpcfg2);
169178
write_csr_as_usize!(0x3A2, __write_pmpcfg2);
170179
}
171180

172181
/// Physical memory protection configuration, RV32 only
173182
pub mod pmpcfg3 {
183+
#[derive(Clone, Copy, Debug)]
184+
pub struct Pmpcfg3 {
185+
pub bits: u32,
186+
}
187+
impl Pmpcfg3 {
188+
#[inline]
189+
pub fn get_byte(&self,index:usize) -> PmpByte {
190+
PmpByte {
191+
byte: self.bits.get_bits(8 * index..8 * (index + 1)) as u8
192+
}
193+
}
194+
}
195+
174196
read_csr_as_usize_rv32!(0x3A3, __read_pmpcfg3);
175197
write_csr_as_usize_rv32!(0x3A3, __write_pmpcfg3);
176198
}

0 commit comments

Comments
 (0)