Skip to content

Commit 3a5c598

Browse files
Add read-only CSR support to existing test_csr_field! macro
Extend test_csr_field! macro with new arm for read-only multi-bit fields. Update mtopi tests to use standard macro instead of custom implementation.
1 parent 311f82b commit 3a5c598

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

riscv/src/register/macros.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,19 @@ macro_rules! test_csr_field {
10411041
}
10421042
}};
10431043

1044+
// test a multi-bit bitfield for read-only CSR (must come before enum pattern)
1045+
($reg:ident, $field:ident: [$start:literal, $end:literal]) => {{
1046+
let bits = $reg.bits();
1047+
1048+
let shift = $end - $start + 1;
1049+
let mask = (1usize << shift) - 1;
1050+
1051+
let exp_val = (bits >> $start) & mask;
1052+
1053+
// Test field extraction matches expected value
1054+
assert_eq!($reg.$field(), exp_val);
1055+
}};
1056+
10441057
// test an enum bit field
10451058
($reg:ident, $field:ident: $var:expr) => {{
10461059
$crate::paste! {

riscv/src/register/mtopi.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,15 @@ impl Mtopi {
7676
mod tests {
7777
use super::*;
7878

79-
macro_rules! test_ro_csr_field {
80-
// test a multi-bit bitfield for read-only CSR - matches test_csr_field! pattern
81-
($reg:ident, $field:ident: [$start:expr, $end:expr]) => {{
82-
let bits = $reg.bits();
83-
let shift = $end - $start + 1;
84-
let mask = (1usize << shift) - 1;
85-
let exp_val = (bits >> $start) & mask;
86-
87-
// Test field extraction matches expected value (same as test_csr_field! macro)
88-
assert_eq!($reg.$field(), exp_val);
89-
}};
90-
}
91-
9279
#[test]
9380
fn test_mtopi_fields() {
9481
// Test using helper macros as requested - follows mcounteren.rs pattern
9582
let mut mtopi = Mtopi::from_bits(0);
9683

9784
// Test iid field [16:27] - using test helper macro
98-
test_ro_csr_field!(mtopi, iid: [16, 27]);
85+
test_csr_field!(mtopi, iid: [16, 27]);
9986
// Test ipid field [0:7] - using test helper macro
100-
test_ro_csr_field!(mtopi, ipid: [0, 7]);
87+
test_csr_field!(mtopi, ipid: [0, 7]);
10188

10289
// Test helper methods
10390
assert!(!mtopi.has_interrupt());
@@ -106,26 +93,26 @@ mod tests {
10693

10794
// Test with some interrupt pending (IID = 11, IPID = 5)
10895
mtopi = Mtopi::from_bits((11 << 16) | 5);
109-
test_ro_csr_field!(mtopi, iid: [16, 27]);
110-
test_ro_csr_field!(mtopi, ipid: [0, 7]);
96+
test_csr_field!(mtopi, iid: [16, 27]);
97+
test_csr_field!(mtopi, ipid: [0, 7]);
11198
assert!(mtopi.has_interrupt());
11299
assert_eq!(mtopi.priority(), 5);
113100
assert_eq!(mtopi.interrupt_id(), 11);
114101

115102
// Test maximum values for each field
116103
mtopi = Mtopi::from_bits((0xFFF << 16) | 0xFF);
117-
test_ro_csr_field!(mtopi, iid: [16, 27]);
118-
test_ro_csr_field!(mtopi, ipid: [0, 7]);
104+
test_csr_field!(mtopi, iid: [16, 27]);
105+
test_csr_field!(mtopi, ipid: [0, 7]);
119106
assert!(mtopi.has_interrupt());
120107

121108
// Test field boundaries
122109
mtopi = Mtopi::from_bits(1 << 16);
123-
test_ro_csr_field!(mtopi, iid: [16, 27]);
124-
test_ro_csr_field!(mtopi, ipid: [0, 7]);
110+
test_csr_field!(mtopi, iid: [16, 27]);
111+
test_csr_field!(mtopi, ipid: [0, 7]);
125112

126113
mtopi = Mtopi::from_bits(1);
127-
test_ro_csr_field!(mtopi, iid: [16, 27]);
128-
test_ro_csr_field!(mtopi, ipid: [0, 7]);
114+
test_csr_field!(mtopi, iid: [16, 27]);
115+
test_csr_field!(mtopi, ipid: [0, 7]);
129116
}
130117

131118
#[test]

0 commit comments

Comments
 (0)