@@ -15,79 +15,100 @@ pub enum Trap {
15
15
16
16
/// Interrupt
17
17
#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
18
+ #[ repr( usize ) ]
18
19
pub enum Interrupt {
19
- UserSoft ,
20
- SupervisorSoft ,
21
- MachineSoft ,
22
- UserTimer ,
23
- SupervisorTimer ,
24
- MachineTimer ,
25
- UserExternal ,
26
- SupervisorExternal ,
27
- MachineExternal ,
20
+ SupervisorSoft = 1 ,
21
+ MachineSoft = 3 ,
22
+ SupervisorTimer = 5 ,
23
+ MachineTimer = 7 ,
24
+ SupervisorExternal = 9 ,
25
+ MachineExternal = 11 ,
28
26
Unknown ,
29
27
}
30
28
31
29
/// Exception
32
30
#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
31
+ #[ repr( usize ) ]
33
32
pub enum Exception {
34
- InstructionMisaligned ,
35
- InstructionFault ,
36
- IllegalInstruction ,
37
- Breakpoint ,
38
- LoadMisaligned ,
39
- LoadFault ,
40
- StoreMisaligned ,
41
- StoreFault ,
42
- UserEnvCall ,
43
- SupervisorEnvCall ,
44
- MachineEnvCall ,
45
- InstructionPageFault ,
46
- LoadPageFault ,
47
- StorePageFault ,
33
+ InstructionMisaligned = 0 ,
34
+ InstructionFault = 1 ,
35
+ IllegalInstruction = 2 ,
36
+ Breakpoint = 3 ,
37
+ LoadMisaligned = 4 ,
38
+ LoadFault = 5 ,
39
+ StoreMisaligned = 6 ,
40
+ StoreFault = 7 ,
41
+ UserEnvCall = 8 ,
42
+ SupervisorEnvCall = 9 ,
43
+ MachineEnvCall = 11 ,
44
+ InstructionPageFault = 12 ,
45
+ LoadPageFault = 13 ,
46
+ StorePageFault = 15 ,
48
47
Unknown ,
49
48
}
50
49
51
- impl Interrupt {
50
+ impl From < usize > for Interrupt {
52
51
#[ inline]
53
- pub fn from ( nr : usize ) -> Self {
52
+ fn from ( nr : usize ) -> Self {
54
53
match nr {
55
- 0 => Interrupt :: UserSoft ,
56
- 1 => Interrupt :: SupervisorSoft ,
57
- 3 => Interrupt :: MachineSoft ,
58
- 4 => Interrupt :: UserTimer ,
59
- 5 => Interrupt :: SupervisorTimer ,
60
- 7 => Interrupt :: MachineTimer ,
61
- 8 => Interrupt :: UserExternal ,
62
- 9 => Interrupt :: SupervisorExternal ,
63
- 11 => Interrupt :: MachineExternal ,
64
- _ => Interrupt :: Unknown ,
54
+ 1 => Self :: SupervisorSoft ,
55
+ 3 => Self :: MachineSoft ,
56
+ 5 => Self :: SupervisorTimer ,
57
+ 7 => Self :: MachineTimer ,
58
+ 9 => Self :: SupervisorExternal ,
59
+ 11 => Self :: MachineExternal ,
60
+ _ => Self :: Unknown ,
65
61
}
66
62
}
67
63
}
68
64
69
- impl Exception {
65
+ impl TryFrom < Interrupt > for usize {
66
+ type Error = Interrupt ;
67
+
68
+ #[ inline]
69
+ fn try_from ( value : Interrupt ) -> Result < Self , Self :: Error > {
70
+ match value {
71
+ Interrupt :: Unknown => Err ( Self :: Error :: Unknown ) ,
72
+ _ => Ok ( value as Self ) ,
73
+ }
74
+ }
75
+ }
76
+
77
+ impl From < usize > for Exception {
70
78
#[ inline]
71
- pub fn from ( nr : usize ) -> Self {
79
+ fn from ( nr : usize ) -> Self {
72
80
match nr {
73
- 0 => Exception :: InstructionMisaligned ,
74
- 1 => Exception :: InstructionFault ,
75
- 2 => Exception :: IllegalInstruction ,
76
- 3 => Exception :: Breakpoint ,
77
- 4 => Exception :: LoadMisaligned ,
78
- 5 => Exception :: LoadFault ,
79
- 6 => Exception :: StoreMisaligned ,
80
- 7 => Exception :: StoreFault ,
81
- 8 => Exception :: UserEnvCall ,
82
- 9 => Exception :: SupervisorEnvCall ,
83
- 11 => Exception :: MachineEnvCall ,
84
- 12 => Exception :: InstructionPageFault ,
85
- 13 => Exception :: LoadPageFault ,
86
- 15 => Exception :: StorePageFault ,
87
- _ => Exception :: Unknown ,
81
+ 0 => Self :: InstructionMisaligned ,
82
+ 1 => Self :: InstructionFault ,
83
+ 2 => Self :: IllegalInstruction ,
84
+ 3 => Self :: Breakpoint ,
85
+ 4 => Self :: LoadMisaligned ,
86
+ 5 => Self :: LoadFault ,
87
+ 6 => Self :: StoreMisaligned ,
88
+ 7 => Self :: StoreFault ,
89
+ 8 => Self :: UserEnvCall ,
90
+ 9 => Self :: SupervisorEnvCall ,
91
+ 11 => Self :: MachineEnvCall ,
92
+ 12 => Self :: InstructionPageFault ,
93
+ 13 => Self :: LoadPageFault ,
94
+ 15 => Self :: StorePageFault ,
95
+ _ => Self :: Unknown ,
88
96
}
89
97
}
90
98
}
99
+
100
+ impl TryFrom < Exception > for usize {
101
+ type Error = Exception ;
102
+
103
+ #[ inline]
104
+ fn try_from ( value : Exception ) -> Result < Self , Self :: Error > {
105
+ match value {
106
+ Exception :: Unknown => Err ( Self :: Error :: Unknown ) ,
107
+ _ => Ok ( value as Self ) ,
108
+ }
109
+ }
110
+ }
111
+
91
112
impl Mcause {
92
113
/// Returns the contents of the register as raw bits
93
114
#[ inline]
@@ -98,14 +119,7 @@ impl Mcause {
98
119
/// Returns the code field
99
120
#[ inline]
100
121
pub fn code ( & self ) -> usize {
101
- match ( ) {
102
- #[ cfg( target_pointer_width = "32" ) ]
103
- ( ) => self . bits & !( 1 << 31 ) ,
104
- #[ cfg( target_pointer_width = "64" ) ]
105
- ( ) => self . bits & !( 1 << 63 ) ,
106
- #[ cfg( target_pointer_width = "128" ) ]
107
- ( ) => self . bits & !( 1 << 127 ) ,
108
- }
122
+ self . bits & !( 1 << ( usize:: BITS as usize - 1 ) )
109
123
}
110
124
111
125
/// Trap Cause
@@ -121,14 +135,7 @@ impl Mcause {
121
135
/// Is trap cause an interrupt.
122
136
#[ inline]
123
137
pub fn is_interrupt ( & self ) -> bool {
124
- match ( ) {
125
- #[ cfg( target_pointer_width = "32" ) ]
126
- ( ) => self . bits & ( 1 << 31 ) == 1 << 31 ,
127
- #[ cfg( target_pointer_width = "64" ) ]
128
- ( ) => self . bits & ( 1 << 63 ) == 1 << 63 ,
129
- #[ cfg( target_pointer_width = "128" ) ]
130
- ( ) => self . bits & ( 1 << 127 ) == 1 << 127 ,
131
- }
138
+ self . bits & ( 1 << ( usize:: BITS as usize - 1 ) ) != 0
132
139
}
133
140
134
141
/// Is trap cause an exception.
0 commit comments