|
| 1 | +//! `mcountinhibit` register |
| 2 | +
|
| 3 | +use crate::bits::{bf_extract, bf_insert}; |
| 4 | + |
| 5 | +/// `mcountinhibit` register |
| 6 | +#[derive(Clone, Copy, Debug)] |
| 7 | +pub struct Mcountinhibit { |
| 8 | + bits: usize, |
| 9 | +} |
| 10 | + |
| 11 | +impl Mcountinhibit { |
| 12 | + /// Machine "cycle\[h\]" Disable |
| 13 | + #[inline] |
| 14 | + pub fn cy(&self) -> bool { |
| 15 | + bf_extract(self.bits, 0, 1) != 0 |
| 16 | + } |
| 17 | + |
| 18 | + /// Sets whether to inhibit the "cycle\[h\]" counter. |
| 19 | + /// |
| 20 | + /// Only updates the in-memory value, does not modify the `mcountinhibit` register. |
| 21 | + #[inline] |
| 22 | + pub fn set_cy(&mut self, cy: bool) { |
| 23 | + self.bits = bf_insert(self.bits, 0, 1, cy as usize); |
| 24 | + } |
| 25 | + |
| 26 | + /// Machine "instret\[h\]" Disable |
| 27 | + #[inline] |
| 28 | + pub fn ir(&self) -> bool { |
| 29 | + bf_extract(self.bits, 2, 1) != 0 |
| 30 | + } |
| 31 | + |
| 32 | + /// Sets whether to inhibit the "instret\[h\]" counter. |
| 33 | + /// |
| 34 | + /// Only updates the in-memory value, does not modify the `mcountinhibit` register. |
| 35 | + #[inline] |
| 36 | + pub fn set_ir(&mut self, ir: bool) { |
| 37 | + self.bits = bf_insert(self.bits, 2, 1, ir as usize); |
| 38 | + } |
| 39 | + |
| 40 | + /// Machine "hpm\[x\]" Disable (bits 3-31) |
| 41 | + #[inline] |
| 42 | + pub fn hpm(&self, index: usize) -> bool { |
| 43 | + assert!((3..32).contains(&index)); |
| 44 | + bf_extract(self.bits, index, 1) != 0 |
| 45 | + } |
| 46 | + |
| 47 | + /// Sets whether to inhibit the "hpm\[X\]" counter. |
| 48 | + /// |
| 49 | + /// Only updates the in-memory value, does not modify the `mcountinhibit` register. |
| 50 | + #[inline] |
| 51 | + pub fn set_hpm(&mut self, index: usize, hpm: bool) { |
| 52 | + assert!((3..32).contains(&index)); |
| 53 | + self.bits = bf_insert(self.bits, index, 1, hpm as usize); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +read_csr_as!(Mcountinhibit, 0x320); |
| 58 | +write_csr_as!(Mcountinhibit, 0x320); |
| 59 | +set!(0x320); |
| 60 | +clear!(0x320); |
| 61 | + |
| 62 | +set_clear_csr!( |
| 63 | +/// Machine cycle Disable |
| 64 | + , set_cy, clear_cy, 1 << 0); |
| 65 | + |
| 66 | +set_clear_csr!( |
| 67 | +/// Machine instret Disable |
| 68 | + , set_ir, clear_ir, 1 << 2); |
| 69 | + |
| 70 | +#[inline] |
| 71 | +pub unsafe fn set_hpm(index: usize) { |
| 72 | + assert!((3..32).contains(&index)); |
| 73 | + _set(1 << index); |
| 74 | +} |
| 75 | + |
| 76 | +#[inline] |
| 77 | +pub unsafe fn clear_hpm(index: usize) { |
| 78 | + assert!((3..32).contains(&index)); |
| 79 | + _clear(1 << index); |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use super::*; |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_mcountinhibit() { |
| 88 | + let mut m = Mcountinhibit { bits: 0 }; |
| 89 | + |
| 90 | + assert!(!m.cy()); |
| 91 | + |
| 92 | + m.set_cy(true); |
| 93 | + assert!(m.cy()); |
| 94 | + |
| 95 | + m.set_cy(false); |
| 96 | + assert!(!m.cy()); |
| 97 | + |
| 98 | + assert!(!m.ir()); |
| 99 | + |
| 100 | + m.set_ir(true); |
| 101 | + assert!(m.ir()); |
| 102 | + |
| 103 | + m.set_ir(false); |
| 104 | + assert!(!m.ir()); |
| 105 | + |
| 106 | + (3..32).for_each(|i| { |
| 107 | + assert!(!m.hpm(i)); |
| 108 | + |
| 109 | + m.set_hpm(i, true); |
| 110 | + assert!(m.hpm(i)); |
| 111 | + |
| 112 | + m.set_hpm(i, false); |
| 113 | + assert!(!m.hpm(i)); |
| 114 | + }); |
| 115 | + } |
| 116 | +} |
0 commit comments