|
1 | 1 | //! mcounteren register
|
2 | 2 |
|
3 |
| -use crate::bits::{bf_extract, bf_insert}; |
4 | 3 | use crate::result::{Error, Result};
|
5 | 4 |
|
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, |
10 | 9 | }
|
11 | 10 |
|
12 |
| -impl Mcounteren { |
| 11 | +read_write_csr_field! { |
| 12 | + Mcounteren, |
13 | 13 | /// 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 | +} |
26 | 16 |
|
| 17 | +read_write_csr_field! { |
| 18 | + Mcounteren, |
27 | 19 | /// 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 | +} |
40 | 22 |
|
| 23 | +read_write_csr_field! { |
| 24 | + Mcounteren, |
41 | 25 | /// 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 | +} |
54 | 28 |
|
| 29 | +read_write_csr_field! { |
| 30 | + Mcounteren, |
55 | 31 | /// 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, |
107 | 33 | }
|
108 | 34 |
|
109 |
| -read_csr_as!(Mcounteren, 0x306); |
110 |
| -write_csr_as!(Mcounteren, 0x306); |
111 | 35 | set!(0x306);
|
112 | 36 | clear!(0x306);
|
113 | 37 |
|
@@ -189,60 +113,23 @@ mod tests {
|
189 | 113 | fn test_mcounteren() {
|
190 | 114 | let mut m = Mcounteren { bits: 0 };
|
191 | 115 |
|
192 |
| - assert!(!m.cy()); |
193 |
| - |
194 |
| - m.set_cy(true); |
195 |
| - assert!(m.cy()); |
196 |
| - |
197 |
| - m.set_cy(false); |
198 |
| - assert!(!m.cy()); |
199 |
| - |
200 |
| - assert!(!m.tm()); |
201 |
| - |
202 |
| - m.set_tm(true); |
203 |
| - assert!(m.tm()); |
204 |
| - |
205 |
| - m.set_tm(false); |
206 |
| - assert!(!m.tm()); |
207 |
| - |
208 |
| - assert!(!m.ir()); |
209 |
| - |
210 |
| - m.set_ir(true); |
211 |
| - assert!(m.ir()); |
212 |
| - |
213 |
| - m.set_ir(false); |
214 |
| - assert!(!m.ir()); |
215 |
| - |
216 |
| - (3..32).for_each(|i| { |
217 |
| - assert!(!m.hpm(i)); |
218 |
| - assert_eq!(m.try_hpm(i), Ok(false)); |
| 116 | + test_csr_field!(m, cy); |
| 117 | + test_csr_field!(m, tm); |
| 118 | + test_csr_field!(m, ir); |
219 | 119 |
|
220 |
| - m.set_hpm(i, true); |
221 |
| - assert!(m.hpm(i)); |
222 |
| - |
223 |
| - assert_eq!(m.try_set_hpm(i, false), Ok(())); |
224 |
| - assert_eq!(m.try_hpm(i), Ok(false)); |
225 |
| - |
226 |
| - assert!(!m.hpm(i)); |
227 |
| - }); |
| 120 | + (3..32).for_each(|i| test_csr_field!(m, hpm, i)); |
228 | 121 |
|
229 | 122 | (0..3).chain(32..64).for_each(|index| {
|
230 |
| - assert_eq!( |
231 |
| - m.try_hpm(index), |
232 |
| - Err(Error::IndexOutOfBounds { |
233 |
| - index, |
234 |
| - min: 3, |
235 |
| - max: 31 |
236 |
| - }) |
237 |
| - ); |
238 |
| - assert_eq!( |
239 |
| - m.try_set_hpm(index, false), |
240 |
| - Err(Error::IndexOutOfBounds { |
| 123 | + test_csr_field!( |
| 124 | + m, |
| 125 | + hpm, |
| 126 | + index, |
| 127 | + Error::IndexOutOfBounds { |
241 | 128 | index,
|
242 | 129 | min: 3,
|
243 | 130 | max: 31
|
244 |
| - }) |
245 |
| - ); |
246 |
| - }) |
| 131 | + } |
| 132 | + ) |
| 133 | + }); |
247 | 134 | }
|
248 | 135 | }
|
0 commit comments