File tree 3 files changed +44
-25
lines changed
3 files changed +44
-25
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
7
7
8
8
## [ Unreleased]
9
9
10
+ ### Added
11
+
12
+ - CSR helper macro to check for platform implementation
13
+
14
+ ### Changed
15
+
16
+ - Use CSR helper macros to define ` marchid ` register
17
+
10
18
## [ v0.12.1] - 2024-10-20
11
19
12
20
### Changed
Original file line number Diff line number Diff line change @@ -92,6 +92,27 @@ macro_rules! read_csr_as {
92
92
} )
93
93
}
94
94
} ;
95
+
96
+ ( $register: ident, $csr_number: literal, $sentinel: tt) => {
97
+ $crate:: read_csr!( $csr_number) ;
98
+
99
+ /// Reads the CSR.
100
+ ///
101
+ /// **WARNING**: panics on targets not implementing this CSR.
102
+ #[ inline]
103
+ pub fn read( ) -> $register {
104
+ try_read( ) . unwrap( )
105
+ }
106
+
107
+ /// Attempts to reads the CSR.
108
+ #[ inline]
109
+ pub fn try_read( ) -> $crate:: result:: Result <$register> {
110
+ match unsafe { _try_read( ) ? } {
111
+ $sentinel => Err ( $crate:: result:: Error :: Unimplemented ) ,
112
+ bits => Ok ( $register { bits } ) ,
113
+ }
114
+ }
115
+ } ;
95
116
}
96
117
97
118
/// `RV32`: Convenience macro to read a CSR register value as a `register` type.
@@ -732,6 +753,16 @@ macro_rules! read_only_csr {
732
753
733
754
$crate:: read_csr_as!( $ty, $csr) ;
734
755
} ;
756
+
757
+ ( $( #[ $doc: meta] ) +
758
+ $ty: ident: $csr: tt,
759
+ mask: $mask: tt,
760
+ sentinel: $sentinel: tt$( , ) ?,
761
+ ) => {
762
+ $crate:: csr! { $( #[ $doc] ) + $ty, $mask }
763
+
764
+ $crate:: read_csr_as!( $ty, $csr, $sentinel) ;
765
+ } ;
735
766
}
736
767
737
768
/// Helper macro to create a read-only CSR type.
Original file line number Diff line number Diff line change 1
1
//! marchid register
2
2
3
- use core:: num:: NonZeroUsize ;
4
-
5
- /// marchid register
6
- #[ derive( Clone , Copy , Debug ) ]
7
- pub struct Marchid {
8
- bits : NonZeroUsize ,
9
- }
10
-
11
- impl Marchid {
12
- /// Returns the contents of the register as raw bits
13
- #[ inline]
14
- pub fn bits ( & self ) -> usize {
15
- self . bits . get ( )
16
- }
17
- }
18
-
19
- read_csr ! ( 0xF12 ) ;
20
-
21
- /// Reads the CSR
22
- #[ inline]
23
- pub fn read ( ) -> Option < Marchid > {
24
- let r = unsafe { _read ( ) } ;
25
- // When marchid is hardwired to zero it means that the marchid
26
- // csr isn't implemented.
27
- NonZeroUsize :: new ( r) . map ( |bits| Marchid { bits } )
3
+ read_only_csr ! {
4
+ /// `marchid` register
5
+ Marchid : 0xF12 ,
6
+ mask: 0xffff_ffff ,
7
+ sentinel: 0 ,
28
8
}
You can’t perform that action at this time.
0 commit comments