@@ -24,6 +24,9 @@ read_only_csr_field! {
24
24
}
25
25
26
26
impl Mvendorid {
27
+ /// Represents the JEDEC manufacture continuation byte.
28
+ pub const CONTINUATION : u8 = 0x7f ;
29
+
27
30
/// Gets the decoded JEDEC manufacturer ID from the `mvendorid` value.
28
31
///
29
32
/// # Note
@@ -34,8 +37,6 @@ impl Mvendorid {
34
37
///
35
38
/// The final byte in the iterator is the `offset`, including the odd parity bit (set only if even).
36
39
pub fn jedec_manufacturer ( & self ) -> impl Iterator < Item = u8 > {
37
- const CONTINUATION : u8 = 0x7f ;
38
-
39
40
let mut done = false ;
40
41
let mut bank = self . bank ( ) ;
41
42
let offset = self . offset ( ) ;
@@ -49,8 +50,50 @@ impl Mvendorid {
49
50
}
50
51
_ => {
51
52
bank -= 1 ;
52
- Some ( CONTINUATION )
53
+ Some ( Self :: CONTINUATION )
53
54
}
54
55
} )
55
56
}
56
57
}
58
+
59
+ #[ cfg( test) ]
60
+ mod tests {
61
+ use super :: * ;
62
+
63
+ #[ test]
64
+ fn test_mvendorid ( ) {
65
+ ( 0 ..u32:: BITS )
66
+ . map ( |r| ( ( 1u64 << r) - 1 ) as usize )
67
+ . for_each ( |raw| {
68
+ let exp_bank = raw >> 7 ;
69
+ let exp_offset = raw & 0x7f ;
70
+ let exp_parity = ( 1 - ( exp_offset % 2 ) ) << 7 ;
71
+ let exp_mvendorid = Mvendorid :: from_bits ( raw) ;
72
+
73
+ assert_eq ! ( exp_mvendorid. bank( ) , exp_bank) ;
74
+ assert_eq ! ( exp_mvendorid. offset( ) , exp_offset) ;
75
+
76
+ let mut jedec_iter = exp_mvendorid. jedec_manufacturer ( ) ;
77
+ ( 0 ..exp_bank)
78
+ . for_each ( |_| assert_eq ! ( jedec_iter. next( ) , Some ( Mvendorid :: CONTINUATION ) ) ) ;
79
+ assert_eq ! ( jedec_iter. next( ) , Some ( ( exp_parity | exp_offset) as u8 ) ) ;
80
+ assert_eq ! ( jedec_iter. next( ) , None ) ;
81
+ } ) ;
82
+
83
+ // ISA example used as a concrete test vector.
84
+
85
+ let exp_bank = 0xc ;
86
+ let exp_offset = 0x0a ;
87
+ let exp_decoded_offset = 0x8a ;
88
+ let raw_mvendorid = 0x60a ;
89
+ let exp_mvendorid = Mvendorid :: from_bits ( raw_mvendorid) ;
90
+
91
+ assert_eq ! ( exp_mvendorid. bank( ) , exp_bank) ;
92
+ assert_eq ! ( exp_mvendorid. offset( ) , exp_offset) ;
93
+
94
+ let mut jedec_iter = exp_mvendorid. jedec_manufacturer ( ) ;
95
+ ( 0 ..exp_bank) . for_each ( |_| assert_eq ! ( jedec_iter. next( ) , Some ( Mvendorid :: CONTINUATION ) ) ) ;
96
+ assert_eq ! ( jedec_iter. next( ) , Some ( exp_decoded_offset) ) ;
97
+ assert_eq ! ( jedec_iter. next( ) , None ) ;
98
+ }
99
+ }
0 commit comments