@@ -15,60 +15,68 @@ pub enum Trap {
1515
1616/// Interrupt
1717#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
18+ #[ repr( usize ) ]
1819pub 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 ,
2826 Unknown ,
2927}
3028
3129/// Exception
3230#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
31+ #[ repr( usize ) ]
3332pub 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 ,
4847 Unknown ,
4948}
5049
51- impl Interrupt {
50+ impl From < usize > for Interrupt {
5251 #[ inline]
53- pub fn from ( nr : usize ) -> Self {
52+ fn from ( nr : usize ) -> Self {
5453 match nr {
55- 0 => Interrupt :: UserSoft ,
5654 1 => Interrupt :: SupervisorSoft ,
5755 3 => Interrupt :: MachineSoft ,
58- 4 => Interrupt :: UserTimer ,
5956 5 => Interrupt :: SupervisorTimer ,
6057 7 => Interrupt :: MachineTimer ,
61- 8 => Interrupt :: UserExternal ,
6258 9 => Interrupt :: SupervisorExternal ,
6359 11 => Interrupt :: MachineExternal ,
6460 _ => Interrupt :: Unknown ,
6561 }
6662 }
6763}
6864
69- impl Exception {
65+ impl TryFrom < Interrupt > for usize {
66+ type Error = Interrupt ;
67+
7068 #[ inline]
71- pub fn from ( nr : usize ) -> Self {
69+ fn try_from ( value : Interrupt ) -> Result < Self , Self :: Error > {
70+ match value {
71+ Interrupt :: Unknown => Err ( Interrupt :: Unknown ) ,
72+ _ => Ok ( value as Self ) ,
73+ }
74+ }
75+ }
76+
77+ impl From < usize > for Exception {
78+ #[ inline]
79+ fn from ( nr : usize ) -> Self {
7280 match nr {
7381 0 => Exception :: InstructionMisaligned ,
7482 1 => Exception :: InstructionFault ,
@@ -88,6 +96,19 @@ impl Exception {
8896 }
8997 }
9098}
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 ( Exception :: Unknown ) ,
107+ _ => Ok ( value as Self ) ,
108+ }
109+ }
110+ }
111+
91112impl Mcause {
92113 /// Returns the contents of the register as raw bits
93114 #[ inline]
0 commit comments