|
| 1 | +// Copyright (c) 2022 by Rivos Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
1 | 5 | //! mtvec register
|
2 | 6 |
|
3 |
| -/// mtvec register |
4 |
| -#[derive(Clone, Copy, Debug)] |
5 |
| -pub struct Mtvec { |
6 |
| - bits: usize, |
| 7 | +rw_csr!(mtvec, usize); |
| 8 | + |
| 9 | +register_bitfields![usize, |
| 10 | + #[cfg(target_pointer_width = "32")] |
| 11 | + pub mtvec [ |
| 12 | + mode OFFSET(0) NUMBITS(2) [ |
| 13 | + Direct = 0, |
| 14 | + Vectored = 1, |
| 15 | + ], |
| 16 | + base OFFSET(2) NUMBITS(30) [], |
| 17 | + ], |
| 18 | + #[cfg(target_pointer_width = "64")] |
| 19 | + pub mtvec [ |
| 20 | + mode OFFSET(0) NUMBITS(2) [ |
| 21 | + Direct = 0, |
| 22 | + Vectored = 1, |
| 23 | + ], |
| 24 | + base OFFSET(2) NUMBITS(62) [], |
| 25 | + ], |
| 26 | +]; |
| 27 | + |
| 28 | +/// Returns true if the trap vector mode is set to `Direct`. |
| 29 | +#[inline] |
| 30 | +pub fn is_direct() -> bool { |
| 31 | + read_field(mode) == 0x0 |
7 | 32 | }
|
8 | 33 |
|
9 |
| -/// Trap mode |
10 |
| -#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
11 |
| -pub enum TrapMode { |
12 |
| - Direct = 0, |
13 |
| - Vectored = 1, |
| 34 | +/// Returns true if the trap vector mode is set to `Vectored`. |
| 35 | +#[inline] |
| 36 | +pub fn is_vectored() -> bool { |
| 37 | + read_field(mode) == 0x1 |
14 | 38 | }
|
15 | 39 |
|
16 |
| -impl Mtvec { |
17 |
| - /// Returns the contents of the register as raw bits |
18 |
| - #[inline] |
19 |
| - pub fn bits(&self) -> usize { |
20 |
| - self.bits |
21 |
| - } |
22 |
| - |
23 |
| - /// Returns the trap-vector base-address |
24 |
| - #[inline] |
25 |
| - pub fn address(&self) -> usize { |
26 |
| - self.bits - (self.bits & 0b11) |
27 |
| - } |
28 |
| - |
29 |
| - /// Returns the trap-vector mode |
30 |
| - #[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 |
| - } |
38 |
| - } |
| 40 | +/// Sets the trap vector mode to `Direct`. |
| 41 | +#[inline] |
| 42 | +pub fn set_direct() { |
| 43 | + let mut local = read_local(); |
| 44 | + local.write(mode::Direct); |
| 45 | + write_local(local); |
39 | 46 | }
|
40 | 47 |
|
41 |
| -read_csr_as!(Mtvec, 0x305); |
42 |
| - |
43 |
| -write_csr!(0x305); |
| 48 | +/// Sets the trap vector mode to `Vectored`. |
| 49 | +#[inline] |
| 50 | +pub fn set_vectored() { |
| 51 | + let mut local = read_local(); |
| 52 | + local.write(mode::Vectored); |
| 53 | + write_local(local); |
| 54 | +} |
44 | 55 |
|
45 |
| -/// Writes the CSR |
| 56 | +/// Sets the trap vector base. |
46 | 57 | #[inline]
|
47 |
| -pub unsafe fn write(addr: usize, mode: TrapMode) { |
48 |
| - let bits = addr + mode as usize; |
49 |
| - _write(bits); |
| 58 | +pub fn set_base(base_val: usize) { |
| 59 | + let mut local = read_local(); |
| 60 | + local.write(base.val(base_val)); |
| 61 | + write_local(local); |
50 | 62 | }
|
0 commit comments