Skip to content

Commit e4d24ba

Browse files
committed
riscv: use CSR macros for mcounteren
Uses CSR helper macros to define the `mcounteren` register.
1 parent 64957b3 commit e4d24ba

File tree

2 files changed

+20
-95
lines changed

2 files changed

+20
-95
lines changed

riscv/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515

1616
- Use CSR helper macros to define `marchid` register
1717
- Re-use `try_*` functions in `mcountinhibit`
18+
- Use CSR helper macros to define `mcounteren` register
1819

1920
## [v0.12.1] - 2024-10-20
2021

riscv/src/register/mcounteren.rs

Lines changed: 19 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,37 @@
11
//! mcounteren register
22
3-
use crate::bits::{bf_extract, bf_insert};
43
use crate::result::{Error, Result};
54

6-
/// mcounteren register
7-
#[derive(Clone, Copy, Debug)]
8-
pub struct Mcounteren {
9-
bits: usize,
5+
read_write_csr! {
6+
/// `mcounteren` register
7+
Mcounteren: 0x306,
8+
mask: 0xffff_ffff,
109
}
1110

12-
impl Mcounteren {
11+
read_write_csr_field! {
12+
Mcounteren,
1313
/// Supervisor "cycle\[h\]" Enable
14-
#[inline]
15-
pub fn cy(&self) -> bool {
16-
bf_extract(self.bits, 0, 1) != 0
17-
}
18-
19-
/// Sets whether to enable the "cycle\[h\]" counter.
20-
///
21-
/// Only updates the in-memory value, does not modify the `mcounteren` register.
22-
#[inline]
23-
pub fn set_cy(&mut self, cy: bool) {
24-
self.bits = bf_insert(self.bits, 0, 1, cy as usize);
25-
}
14+
cy: 0,
15+
}
2616

17+
read_write_csr_field! {
18+
Mcounteren,
2719
/// Supervisor "time\[h\]" Enable
28-
#[inline]
29-
pub fn tm(&self) -> bool {
30-
bf_extract(self.bits, 1, 1) != 0
31-
}
32-
33-
/// Sets whether to enable "time\[h\]".
34-
///
35-
/// Only updates the in-memory value, does not modify the `mcounteren` register.
36-
#[inline]
37-
pub fn set_tm(&mut self, tm: bool) {
38-
self.bits = bf_insert(self.bits, 1, 1, tm as usize);
39-
}
20+
tm: 1,
21+
}
4022

23+
read_write_csr_field! {
24+
Mcounteren,
4125
/// Supervisor "instret\[h\]" Enable
42-
#[inline]
43-
pub fn ir(&self) -> bool {
44-
bf_extract(self.bits, 2, 1) != 0
45-
}
46-
47-
/// Sets whether to enable the "instret\[h\]" counter.
48-
///
49-
/// Only updates the in-memory value, does not modify the `mcounteren` register.
50-
#[inline]
51-
pub fn set_ir(&mut self, ir: bool) {
52-
self.bits = bf_insert(self.bits, 2, 1, ir as usize);
53-
}
26+
ir: 2,
27+
}
5428

29+
read_write_csr_field! {
30+
Mcounteren,
5531
/// Supervisor "hpm\[x\]" Enable (bits 3-31)
56-
///
57-
/// **WARNING**: panics on `index` out-of-bounds
58-
#[inline]
59-
pub fn hpm(&self, index: usize) -> bool {
60-
self.try_hpm(index).unwrap()
61-
}
62-
63-
/// Fallible Supervisor "hpm\[x\]" Enable (bits 3-31).
64-
///
65-
/// Attempts to read the "hpm\[x\]" value, and returns an error if the `index` is invalid.
66-
#[inline]
67-
pub fn try_hpm(&self, index: usize) -> Result<bool> {
68-
if (3..32).contains(&index) {
69-
Ok(bf_extract(self.bits, index, 1) != 0)
70-
} else {
71-
Err(Error::IndexOutOfBounds {
72-
index,
73-
min: 3,
74-
max: 31,
75-
})
76-
}
77-
}
78-
79-
/// Sets whether to enable the "hpm\[X\]" counter.
80-
///
81-
/// Only updates the in-memory value, does not modify the `mcounteren` register.
82-
///
83-
/// **WARNING**: panics on `index` out-of-bounds
84-
#[inline]
85-
pub fn set_hpm(&mut self, index: usize, hpm: bool) {
86-
self.try_set_hpm(index, hpm).unwrap()
87-
}
88-
89-
/// Sets whether to enable the "hpm\[X\]" counter.
90-
///
91-
/// Only updates the in-memory value, does not modify the `mcounteren` register.
92-
///
93-
/// Attempts to update the "hpm\[x\]" value, and returns an error if the `index` is invalid.
94-
#[inline]
95-
pub fn try_set_hpm(&mut self, index: usize, hpm: bool) -> Result<()> {
96-
if (3..32).contains(&index) {
97-
self.bits = bf_insert(self.bits, index, 1, hpm as usize);
98-
Ok(())
99-
} else {
100-
Err(Error::IndexOutOfBounds {
101-
index,
102-
min: 3,
103-
max: 31,
104-
})
105-
}
106-
}
32+
hpm: 3..=31,
10733
}
10834

109-
read_csr_as!(Mcounteren, 0x306);
110-
write_csr_as!(Mcounteren, 0x306);
11135
set!(0x306);
11236
clear!(0x306);
11337

0 commit comments

Comments
 (0)