Skip to content

Commit a9d3e33

Browse files
Merge pull request #203 from rmsyn/register/export-macros
`riscv`: register: exports macros for custom CSRs
2 parents 33516b9 + d72d7a3 commit a9d3e33

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
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
- Add `Mcause::from(usize)` for use in unit tests
1313
- Add `Mstatus::from(usize)` for use in unit tests
1414
- Add `Mstatus.bits()`
15+
- Export `riscv::register::macros` module macros for external use
1516

1617
### Fixed
1718

riscv/src/register/macros.rs

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/// `RV64`: Convenience macro to wrap the `csrrs` assembly instruction for reading a CSR register.
2+
///
3+
/// This macro should generally not be called directly.
4+
///
5+
/// Instead, use the [read_csr_as](crate::read_csr_as) or [read_csr_as_usize](crate::read_csr_as_usize) macros.
6+
#[macro_export]
17
macro_rules! read_csr {
28
($csr_number:literal) => {
39
/// Reads the CSR
@@ -18,6 +24,12 @@ macro_rules! read_csr {
1824
};
1925
}
2026

27+
/// `RV32`: Convenience macro to wrap the `csrrs` assembly instruction for reading a CSR register.
28+
///
29+
/// This macro should generally not be called directly.
30+
///
31+
/// Instead, use the [read_csr_as_rv32](crate::read_csr_as_rv32) or [read_csr_as_usize_rv32](crate::read_csr_as_usize_rv32) macros.
32+
#[macro_export]
2133
macro_rules! read_csr_rv32 {
2234
($csr_number:literal) => {
2335
/// Reads the CSR
@@ -38,9 +50,13 @@ macro_rules! read_csr_rv32 {
3850
};
3951
}
4052

53+
/// `RV64`: Convenience macro to read a CSR register value as a `register` type.
54+
///
55+
/// The `register` type must be a defined type in scope of the macro call.
56+
#[macro_export]
4157
macro_rules! read_csr_as {
4258
($register:ident, $csr_number:literal) => {
43-
read_csr!($csr_number);
59+
$crate::read_csr!($csr_number);
4460

4561
/// Reads the CSR
4662
#[inline]
@@ -52,9 +68,13 @@ macro_rules! read_csr_as {
5268
};
5369
}
5470

71+
/// `RV32`: Convenience macro to read a CSR register value as a `register` type.
72+
///
73+
/// The `register` type must be a defined type in scope of the macro call.
74+
#[macro_export]
5575
macro_rules! read_csr_as_rv32 {
5676
($register:ident, $csr_number:literal) => {
57-
read_csr_rv32!($csr_number);
77+
$crate::read_csr_rv32!($csr_number);
5878

5979
/// Reads the CSR
6080
#[inline]
@@ -66,9 +86,11 @@ macro_rules! read_csr_as_rv32 {
6686
};
6787
}
6888

89+
/// `RV64`: Convenience macro to read a CSR register value as a [`usize`].
90+
#[macro_export]
6991
macro_rules! read_csr_as_usize {
7092
($csr_number:literal) => {
71-
read_csr!($csr_number);
93+
$crate::read_csr!($csr_number);
7294

7395
/// Reads the CSR
7496
#[inline]
@@ -78,9 +100,11 @@ macro_rules! read_csr_as_usize {
78100
};
79101
}
80102

103+
/// `RV32`: Convenience macro to read a CSR register value as a [`usize`].
104+
#[macro_export]
81105
macro_rules! read_csr_as_usize_rv32 {
82106
($csr_number:literal) => {
83-
read_csr_rv32!($csr_number);
107+
$crate::read_csr_rv32!($csr_number);
84108

85109
/// Reads the CSR
86110
#[inline]
@@ -90,6 +114,12 @@ macro_rules! read_csr_as_usize_rv32 {
90114
};
91115
}
92116

117+
/// `RV64`: Convenience macro to wrap the `csrrw` assembly instruction for writing to CSR registers.
118+
///
119+
/// This macro should generally not be called directly.
120+
///
121+
/// Instead, use the [write_csr_as_usize](crate::write_csr_as_usize) macro.
122+
#[macro_export]
93123
macro_rules! write_csr {
94124
($csr_number:literal) => {
95125
/// Writes the CSR
@@ -107,6 +137,12 @@ macro_rules! write_csr {
107137
};
108138
}
109139

140+
/// `RV32`: Convenience macro to wrap the `csrrw` assembly instruction for writing to CSR registers.
141+
///
142+
/// This macro should generally not be called directly.
143+
///
144+
/// Instead, use the [write_csr_as_usize_rv32](crate::write_csr_as_usize_rv32) macro.
145+
#[macro_export]
110146
macro_rules! write_csr_rv32 {
111147
($csr_number:literal) => {
112148
/// Writes the CSR
@@ -124,9 +160,11 @@ macro_rules! write_csr_rv32 {
124160
};
125161
}
126162

163+
/// `RV64`: Convenience macro to write a [`usize`] value to a CSR register.
164+
#[macro_export]
127165
macro_rules! write_csr_as_usize {
128166
($csr_number:literal) => {
129-
write_csr!($csr_number);
167+
$crate::write_csr!($csr_number);
130168

131169
/// Writes the CSR
132170
#[inline]
@@ -136,9 +174,11 @@ macro_rules! write_csr_as_usize {
136174
};
137175
}
138176

177+
/// `RV32`: Convenience macro to write a [`usize`] value to a CSR register.
178+
#[macro_export]
139179
macro_rules! write_csr_as_usize_rv32 {
140180
($csr_number:literal) => {
141-
write_csr_rv32!($csr_number);
181+
$crate::write_csr_rv32!($csr_number);
142182

143183
/// Writes the CSR
144184
#[inline]
@@ -148,6 +188,10 @@ macro_rules! write_csr_as_usize_rv32 {
148188
};
149189
}
150190

191+
/// `RV64`: Convenience macro around the `csrrs` assembly instruction to set the CSR register.
192+
///
193+
/// This macro is intended for use with the [set_csr](crate::set_csr) or [set_clear_csr](crate::set_clear_csr) macros.
194+
#[macro_export]
151195
macro_rules! set {
152196
($csr_number:literal) => {
153197
/// Set the CSR
@@ -165,6 +209,10 @@ macro_rules! set {
165209
};
166210
}
167211

212+
/// `RV32`: Convenience macro around the `csrrs` assembly instruction to set the CSR register.
213+
///
214+
/// This macro is intended for use with the [set_csr](crate::set_csr) or [set_clear_csr](crate::set_clear_csr) macros.
215+
#[macro_export]
168216
macro_rules! set_rv32 {
169217
($csr_number:literal) => {
170218
/// Set the CSR
@@ -182,6 +230,10 @@ macro_rules! set_rv32 {
182230
};
183231
}
184232

233+
/// `RV64`: Convenience macro around the `csrrc` assembly instruction to clear the CSR register.
234+
///
235+
/// This macro is intended for use with the [clear_csr](crate::clear_csr) or [set_clear_csr](crate::set_clear_csr) macros.
236+
#[macro_export]
185237
macro_rules! clear {
186238
($csr_number:literal) => {
187239
/// Clear the CSR
@@ -199,6 +251,10 @@ macro_rules! clear {
199251
};
200252
}
201253

254+
/// `RV32`: Convenience macro around the `csrrc` assembly instruction to clear the CSR register.
255+
///
256+
/// This macro is intended for use with the [clear_csr](crate::clear_csr) or [set_clear_csr](crate::set_clear_csr) macros.
257+
#[macro_export]
202258
macro_rules! clear_rv32 {
203259
($csr_number:literal) => {
204260
/// Clear the CSR
@@ -216,6 +272,8 @@ macro_rules! clear_rv32 {
216272
};
217273
}
218274

275+
/// Convenience macro to define field setter functions for a CSR type.
276+
#[macro_export]
219277
macro_rules! set_csr {
220278
($(#[$attr:meta])*, $set_field:ident, $e:expr) => {
221279
$(#[$attr])*
@@ -226,6 +284,8 @@ macro_rules! set_csr {
226284
};
227285
}
228286

287+
/// Convenience macro to define field clear functions for a CSR type.
288+
#[macro_export]
229289
macro_rules! clear_csr {
230290
($(#[$attr:meta])*, $clear_field:ident, $e:expr) => {
231291
$(#[$attr])*
@@ -236,13 +296,20 @@ macro_rules! clear_csr {
236296
};
237297
}
238298

299+
/// Convenience macro to define field setter and clear functions for a CSR type.
300+
#[macro_export]
239301
macro_rules! set_clear_csr {
240302
($(#[$attr:meta])*, $set_field:ident, $clear_field:ident, $e:expr) => {
241-
set_csr!($(#[$attr])*, $set_field, $e);
242-
clear_csr!($(#[$attr])*, $clear_field, $e);
303+
$crate::set_csr!($(#[$attr])*, $set_field, $e);
304+
$crate::clear_csr!($(#[$attr])*, $clear_field, $e);
243305
}
244306
}
245307

308+
/// Convenience macro to read a composite value from a CSR register.
309+
///
310+
/// - `RV32`: reads 32-bits from `hi` and 32-bits from `lo` to create a 64-bit value
311+
/// - `RV64`: reads a 64-bit value from `lo`
312+
#[macro_export]
246313
macro_rules! read_composite_csr {
247314
($hi:expr, $lo:expr) => {
248315
/// Reads the CSR as a 64-bit value

0 commit comments

Comments
 (0)