Skip to content

Commit 66fa6a4

Browse files
committed
riscv: add fallible functions to scounteren
Adds fallible access functions for `Scounteren` HPM fields.
1 parent 5165db3 commit 66fa6a4

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

riscv/src/register/scounteren.rs

+44
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! scounteren register
22
3+
use crate::result::{Error, Result};
4+
35
/// scounteren register
46
#[derive(Clone, Copy, Debug)]
57
pub struct Scounteren {
@@ -31,6 +33,22 @@ impl Scounteren {
3133
assert!((3..32).contains(&index));
3234
self.bits & (1 << index) != 0
3335
}
36+
37+
/// User "hpm\[x\]" Enable (bits 3-31)
38+
///
39+
/// Attempts to read the "hpm\[x\]" value, and returns an error if the index is invalid.
40+
#[inline]
41+
pub fn try_hpm(&self, index: usize) -> Result<bool> {
42+
if (3..32).contains(&index) {
43+
Ok(self.bits & (1 << index) != 0)
44+
} else {
45+
Err(Error::IndexOutOfBounds {
46+
index,
47+
min: 3,
48+
max: 31,
49+
})
50+
}
51+
}
3452
}
3553

3654
read_csr_as!(Scounteren, 0x106);
@@ -56,8 +74,34 @@ pub unsafe fn set_hpm(index: usize) {
5674
_set(1 << index);
5775
}
5876

77+
#[inline]
78+
pub unsafe fn try_set_hpm(index: usize) -> Result<()> {
79+
if (3..32).contains(&index) {
80+
_try_set(1 << index)
81+
} else {
82+
Err(Error::IndexOutOfBounds {
83+
index,
84+
min: 3,
85+
max: 31,
86+
})
87+
}
88+
}
89+
5990
#[inline]
6091
pub unsafe fn clear_hpm(index: usize) {
6192
assert!((3..32).contains(&index));
6293
_clear(1 << index);
6394
}
95+
96+
#[inline]
97+
pub unsafe fn try_clear_hpm(index: usize) -> Result<()> {
98+
if (3..32).contains(&index) {
99+
_try_clear(1 << index)
100+
} else {
101+
Err(Error::IndexOutOfBounds {
102+
index,
103+
min: 3,
104+
max: 31,
105+
})
106+
}
107+
}

0 commit comments

Comments
 (0)