|
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 |
|
|
0 commit comments