Skip to content

Commit 6b1c22c

Browse files
Merge pull request #284 from rmsyn/riscv/register/scontext
register: add `scontext` register
2 parents d7fd706 + 541c36f commit 6b1c22c

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

riscv/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212
- CSR helper macro `write_composite_csr` for writing 64-bit CSRs on 32-bit targets.
1313
- Write utilities for `mcycle`, `minstret`
1414
- Add `senvcfg` CSR
15+
- Add `scontext` CSR
1516

1617
### Changed
1718

riscv/src/register.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub mod stvec;
5757

5858
// Supervisor Trap Handling
5959
pub mod scause;
60+
pub mod scontext;
6061
pub mod senvcfg;
6162
pub mod sepc;
6263
pub mod sip;

riscv/src/register/scontext.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//! `scontext` register.
2+
3+
#[cfg(target_arch = "riscv32")]
4+
const MASK: usize = 0xffff;
5+
#[cfg(not(target_arch = "riscv32"))]
6+
const MASK: usize = 0xffff_ffff;
7+
8+
read_write_csr! {
9+
/// `scontext` register.
10+
Scontext: 0x5a8,
11+
mask: MASK,
12+
}
13+
14+
set!(0x5a8);
15+
clear!(0x5a8);
16+
17+
#[cfg(target_arch = "riscv32")]
18+
read_write_csr_field! {
19+
Scontext,
20+
/// Represents the `data` context number of the `scontext` CSR.
21+
data: [0:15],
22+
}
23+
24+
#[cfg(not(target_arch = "riscv32"))]
25+
read_write_csr_field! {
26+
Scontext,
27+
/// Represents the `data` context number of the `scontext` CSR.
28+
data: [0:31],
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use super::*;
34+
35+
#[test]
36+
fn test_scontext() {
37+
#[cfg(target_arch = "riscv32")]
38+
const DATA_BITS: usize = 16;
39+
#[cfg(not(target_arch = "riscv32"))]
40+
const DATA_BITS: usize = 32;
41+
42+
let mut scontext = Scontext::from_bits(0);
43+
44+
(1..=DATA_BITS)
45+
.map(|b| ((1u64 << b) - 1) as usize)
46+
.for_each(|data| {
47+
scontext.set_data(data);
48+
assert_eq!(scontext.data(), data);
49+
assert_eq!(scontext.bits(), data);
50+
});
51+
}
52+
}

0 commit comments

Comments
 (0)