|
1 | 1 | //! mtvec register |
2 | 2 |
|
3 | | -/// mtvec register |
4 | | -#[derive(Clone, Copy, Debug)] |
5 | | -pub struct Mtvec { |
6 | | - bits: usize, |
7 | | -} |
| 3 | +const MASK: usize = usize::MAX; |
| 4 | +const TRAP_MASK: usize = 0b11; |
8 | 5 |
|
9 | | -/// Trap mode |
10 | | -#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
11 | | -pub enum TrapMode { |
12 | | - Direct = 0, |
13 | | - Vectored = 1, |
| 6 | +read_write_csr! { |
| 7 | + /// mtvec register |
| 8 | + Mtvec: 0x305, |
| 9 | + mask: MASK, |
14 | 10 | } |
15 | 11 |
|
16 | | -impl Mtvec { |
17 | | - /// Returns the contents of the register as raw bits |
18 | | - #[inline] |
19 | | - pub fn bits(&self) -> usize { |
20 | | - self.bits |
| 12 | +csr_field_enum! { |
| 13 | + /// Trap mode |
| 14 | + TrapMode { |
| 15 | + default: Direct, |
| 16 | + Direct = 0, |
| 17 | + Vectored = 1, |
21 | 18 | } |
| 19 | +} |
| 20 | + |
| 21 | +read_write_csr_field! { |
| 22 | + Mtvec, |
| 23 | + /// Accesses the trap-vector mode.. |
| 24 | + trap_mode, |
| 25 | + TrapMode: [0:1], |
| 26 | +} |
22 | 27 |
|
| 28 | +impl Mtvec { |
23 | 29 | /// Returns the trap-vector base-address |
24 | 30 | #[inline] |
25 | | - pub fn address(&self) -> usize { |
26 | | - self.bits - (self.bits & 0b11) |
| 31 | + pub const fn address(&self) -> usize { |
| 32 | + self.bits - (self.bits & TRAP_MASK) |
27 | 33 | } |
28 | 34 |
|
29 | | - /// Returns the trap-vector mode |
| 35 | + /// Sets the trap-vector base-address. |
| 36 | + /// |
| 37 | + /// # Note |
| 38 | + /// |
| 39 | + /// The address is aligned to 4-bytes. |
30 | 40 | #[inline] |
31 | | - pub fn trap_mode(&self) -> Option<TrapMode> { |
32 | | - let mode = self.bits & 0b11; |
33 | | - match mode { |
34 | | - 0 => Some(TrapMode::Direct), |
35 | | - 1 => Some(TrapMode::Vectored), |
36 | | - _ => None, |
37 | | - } |
| 41 | + pub fn set_address(&mut self, address: usize) { |
| 42 | + self.bits = (address & !TRAP_MASK) | (self.bits & TRAP_MASK); |
38 | 43 | } |
39 | 44 | } |
40 | | - |
41 | | -read_csr_as!(Mtvec, 0x305); |
42 | | - |
43 | | -write_csr!(0x305); |
44 | | - |
45 | | -/// Writes the CSR |
46 | | -#[inline] |
47 | | -pub unsafe fn write(addr: usize, mode: TrapMode) { |
48 | | - let bits = addr + mode as usize; |
49 | | - _write(bits); |
50 | | -} |
0 commit comments