Skip to content

Commit 0fb283b

Browse files
committed
riscv: define scounteren CSR with macro helpers
Uses CSR macro helpers to define the `scounteren` CSR register.
1 parent 40cb40d commit 0fb283b

File tree

2 files changed

+34
-44
lines changed

2 files changed

+34
-44
lines changed

riscv/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3535
- Use CSR helper macros to define `pmpcfgx` field types
3636
- Use CSR helper macros to define `scause` field types
3737
- Use CSR helper macros to define `sie` register
38+
- Use CSR helper macros to define `scounteren` field types
3839

3940
## [v0.12.1] - 2024-10-20
4041

riscv/src/register/scounteren.rs

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,36 @@
22
33
use crate::result::{Error, Result};
44

5-
/// scounteren register
6-
#[derive(Clone, Copy, Debug)]
7-
pub struct Scounteren {
8-
bits: usize,
5+
read_write_csr! {
6+
/// scounteren register
7+
Scounteren: 0x106,
8+
mask: 0xffff_ffff,
99
}
1010

11-
impl Scounteren {
11+
read_write_csr_field! {
12+
Scounteren,
1213
/// User "cycle\[h\]" Enable
13-
#[inline]
14-
pub fn cy(&self) -> bool {
15-
self.bits & (1 << 0) != 0
16-
}
14+
cy: 0,
15+
}
1716

17+
read_write_csr_field! {
18+
Scounteren,
1819
/// User "time\[h\]" Enable
19-
#[inline]
20-
pub fn tm(&self) -> bool {
21-
self.bits & (1 << 1) != 0
22-
}
20+
tm: 1,
21+
}
2322

23+
read_write_csr_field! {
24+
Scounteren,
2425
/// User "instret\[h]\" Enable
25-
#[inline]
26-
pub fn ir(&self) -> bool {
27-
self.bits & (1 << 2) != 0
28-
}
29-
30-
/// User "hpm\[x\]" Enable (bits 3-31)
31-
#[inline]
32-
pub fn hpm(&self, index: usize) -> bool {
33-
assert!((3..32).contains(&index));
34-
self.bits & (1 << index) != 0
35-
}
26+
ir: 2,
27+
}
3628

29+
read_write_csr_field! {
30+
Scounteren,
3731
/// 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-
}
32+
hpm: 3..=31,
5233
}
5334

54-
read_csr_as!(Scounteren, 0x106);
55-
write_csr!(0x106);
5635
set!(0x106);
5736
clear!(0x106);
5837

@@ -68,12 +47,17 @@ set_clear_csr!(
6847
/// User instret Enable
6948
, set_ir, clear_ir, 1 << 2);
7049

50+
/// Sets the "hpm\[x\]" enable (bits 3-31).
51+
///
52+
/// # Note
53+
///
54+
/// Panics if `index` is out-of-bounds.
7155
#[inline]
7256
pub unsafe fn set_hpm(index: usize) {
73-
assert!((3..32).contains(&index));
74-
_set(1 << index);
57+
try_set_hpm(index).unwrap();
7558
}
7659

60+
/// Attempts to set the "hpm\[x\]" enable (bits 3-31).
7761
#[inline]
7862
pub unsafe fn try_set_hpm(index: usize) -> Result<()> {
7963
if (3..32).contains(&index) {
@@ -87,12 +71,17 @@ pub unsafe fn try_set_hpm(index: usize) -> Result<()> {
8771
}
8872
}
8973

74+
/// Clears the "hpm\[x\]" enable (bits 3-31).
75+
///
76+
/// # Note
77+
///
78+
/// Panics if `index` is out-of-bounds.
9079
#[inline]
9180
pub unsafe fn clear_hpm(index: usize) {
92-
assert!((3..32).contains(&index));
93-
_clear(1 << index);
81+
try_clear_hpm(index).unwrap()
9482
}
9583

84+
/// Attempts to clear the "hpm\[x\]" enable (bits 3-31).
9685
#[inline]
9786
pub unsafe fn try_clear_hpm(index: usize) -> Result<()> {
9887
if (3..32).contains(&index) {

0 commit comments

Comments
 (0)