Skip to content

Commit 920d41b

Browse files
committed
Reimplement register::mtvec
1 parent 4cc9fe6 commit 920d41b

File tree

4 files changed

+53
-41
lines changed

4 files changed

+53
-41
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
- Replaced CSR macros with new ones using `tock-registers`
1111
- Reimplemeted CSR modules using new base functions:
1212
- `mcause`
13+
- `mtvec`
1314

1415
## [v0.9.0] - 2022-10-06
1516

src/register/addresses.rs

-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ pub const CSR_MEDELEG: u16 = 0x302;
204204
pub const CSR_MIDELEG: u16 = 0x303;
205205
#[allow(unused)]
206206
pub const CSR_MIE: u16 = 0x304;
207-
#[allow(unused)]
208207
pub const CSR_MTVEC: u16 = 0x305;
209208
#[allow(unused)]
210209
pub const CSR_MCOUNTEREN: u16 = 0x306;

src/register/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mod addresses;
7070
//pub mod mie;
7171
//pub mod misa;
7272
//pub mod mstatus;
73-
//pub mod mtvec;
73+
pub mod mtvec;
7474

7575
// Machine Trap Handling
7676
pub mod mcause;

src/register/mtvec.rs

+51-39
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,62 @@
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+
15
//! mtvec register
26
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
732
}
833

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
1438
}
1539

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);
3946
}
4047

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+
}
4455

45-
/// Writes the CSR
56+
/// Sets the trap vector base.
4657
#[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);
5062
}

0 commit comments

Comments
 (0)