Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions riscv/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Write utilities for `mcycle`, `minstret`
- Add `senvcfg` CSR
- Add `scontext` CSR
- Add `mtinst` CSR

### Changed

Expand Down
1 change: 1 addition & 0 deletions riscv/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub mod mcause;
pub mod mepc;
pub mod mip;
pub mod mscratch;
pub mod mtinst;
pub mod mtval;

// Machine Protection and Translation
Expand Down
30 changes: 25 additions & 5 deletions riscv/src/register/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,13 +1007,13 @@ macro_rules! test_csr_field {
// test a single bit field
($reg:ident, $field:ident) => {{
$crate::paste! {
assert!(!$reg.$field());
let val = $reg.$field();

$reg.[<set_ $field>](true);
assert!($reg.$field());
$reg.[<set_ $field>](!val);
assert_eq!($reg.$field(), !val);

$reg.[<set_ $field>](false);
assert!(!$reg.$field());
$reg.[<set_ $field>](val);
assert_eq!($reg.$field(), val);
}
}};

Expand Down Expand Up @@ -1049,4 +1049,24 @@ macro_rules! test_csr_field {
assert_eq!($reg.[<try_ $field>](), Ok($var));
}
}};

// test a multi-bit bitfield
($reg:ident, $field:ident: [$start:expr, $end:expr], $reset:expr) => {{
let bits = $reg.bits();

let shift = $end - $start + 1;
let mask = (1usize << shift) - 1;

let exp_val = (bits >> $start) & mask;

$crate::paste! {
assert_eq!($reg.$field(), exp_val);

$reg.[<set_ $field>]($reset);
assert_eq!($reg.$field(), $reset);

$reg.[<set_ $field>](exp_val);
assert_eq!($reg.$field(), exp_val);
}
}};
}
91 changes: 91 additions & 0 deletions riscv/src/register/mtinst.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//! mtinst register.

const MASK: usize = usize::MAX;

read_write_csr! {
/// mtinst register
Mtinst: 0x34a,
mask: MASK,
}

read_write_csr_field! {
Mtinst,
/// Trapped instruction `opcode` field.
opcode: [0:6],
}

read_write_csr_field! {
Mtinst,
/// Trapped instruction `rd` field for load instructions.
rd: [7:11],
}

read_write_csr_field! {
Mtinst,
/// Trapped instruction `funct3` field.
funct3: [12:14],
}

read_write_csr_field! {
Mtinst,
/// Trapped instruction `address offset` field.
address_offset: [15:19],
}

read_write_csr_field! {
Mtinst,
/// Trapped instruction `rs2` field for store instructions.
rs2: [20:24],
}

read_write_csr_field! {
Mtinst,
/// Trapped instruction `rl` field for atomic instructions.
rl: 25,
}

read_write_csr_field! {
Mtinst,
/// Trapped instruction `aq` field for atomic instructions.
aq: 26,
}

read_write_csr_field! {
Mtinst,
/// Trapped instruction `funct5` field for atomic instructions.
funct5: [27:31],
}

read_write_csr_field! {
Mtinst,
/// Trapped instruction `funct7` field for virtual machine instructions.
funct7: [25:31],
}

set!(0x34a);
clear!(0x34a);

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_mtinst() {
(1..=usize::BITS)
.map(|r| ((1u128 << r) - 1) as usize)
.for_each(|bits| {
let reset = 0;
let mut mtinst = Mtinst::from_bits(bits);

test_csr_field!(mtinst, opcode: [0, 6], reset);
test_csr_field!(mtinst, rd: [7, 11], reset);
test_csr_field!(mtinst, funct3: [12, 14], reset);
test_csr_field!(mtinst, address_offset: [15, 19], reset);
test_csr_field!(mtinst, rs2: [20, 24], reset);
test_csr_field!(mtinst, rl);
test_csr_field!(mtinst, aq);
test_csr_field!(mtinst, funct5: [27, 31], reset);
test_csr_field!(mtinst, funct7: [25, 31], reset);
});
}
}