@@ -22,9 +22,9 @@ void interrupts_set_mtvec(void *);
22
22
// structs defined to match layout of hardware registers
23
23
typedef union {
24
24
struct {
25
- uint32_t priority [1024 ]; // only first 256 used (N_SOURCES )
26
- uint32_t pending [1024 ]; // only first 8 used (one pending bit per source)
27
- uint32_t enable [1024 ]; // only first 8 used (one enable bit per source)
25
+ uint32_t priority [1024 ]; // only first 256 indexes are used (priority per source )
26
+ uint32_t pending [1024 ]; // only first 8 indexes used (pending bit per source, 256/32 = 8 )
27
+ uint32_t enable [1024 ]; // only first 8 indexes used (enable bit per source, 256/32 = 8 )
28
28
} regs ;
29
29
} source_t ;
30
30
@@ -85,10 +85,10 @@ __attribute__((interrupt("machine"))) void _trap_handler(void) {
85
85
long mcause = interrupts_get_mcause ();
86
86
#define EXTERNAL_INTERRUPT ((1L << 63) | 0xb)
87
87
if (mcause == EXTERNAL_INTERRUPT ) { // trap is interrupt
88
- // no need to search pending bits to identify source, claim reg has it
88
+ // do not need to search pending bits to identify which source, claim reg has source number
89
89
uint32_t source = module .plic -> regs .claim_complete ; // read claim_complete to "claim" (atomically clears pending bit)
90
90
module .handlers [source ].fn (module .handlers [source ].aux_data ); // dispatch to registered handler
91
- module .plic -> regs .claim_complete = source ; // write claim_complete to "complete"
91
+ module .plic -> regs .claim_complete = source ; // write claim_complete to clear
92
92
} else { // trap is exception
93
93
long mtval = interrupts_get_mtval ();
94
94
void * mepc = (void * )interrupts_get_mepc ();
@@ -110,17 +110,17 @@ __attribute__((interrupt("machine"))) void _trap_handler(void) {
110
110
void interrupts_init (void ) {
111
111
if (module .initialized ) error ("interrupts_init() must be called only once" );
112
112
interrupts_global_disable ();
113
- module .plic -> regs .ctrl = 0 ; // machine mode only
114
- module .plic -> regs .threshhold = 0 ; // accept interrupts of any priority
115
- interrupts_set_mtvec (_trap_handler ); // install trap handler
116
- for (int i = 0 ; i < 8 ; i ++ ) { // all sources start disabled
117
- module .sources -> regs .pending [i ] = 0 ;
113
+ module .plic -> regs .ctrl = 0 ; // machine mode only
114
+ module .plic -> regs .threshhold = 0 ; // accept interrupts of any priority
115
+ interrupts_set_mtvec (_trap_handler ); // install trap handler
116
+ for (int i = 0 ; i < N_SOURCES / 32 ; i ++ ) {
117
+ module .sources -> regs .pending [i ] = 0 ;// all sources initially disabled
118
118
module .sources -> regs .enable [i ] = 0 ;
119
119
}
120
120
for (int i = 0 ; i < N_SOURCES ; i ++ ) {
121
121
module .sources -> regs .priority [i ] = 0 ;
122
- module .handlers [i ].fn = NULL ;
123
- module .plic -> regs .claim_complete = i ; // mark any pending request completed
122
+ module .handlers [i ].fn = NULL ; // all sources initially have NULL handler
123
+ module .plic -> regs .claim_complete = i ; // reset any pending claim
124
124
}
125
125
module .initialized = true;
126
126
}
@@ -144,10 +144,10 @@ static void set_source_enabled(interrupt_source_t source, bool enabled) {
144
144
int shift = source % 32 ;
145
145
if (enabled ) {
146
146
module .sources -> regs .priority [source ] = 1 ; // priority at 1 (0 is disable, 1 is lowest)
147
- module .sources -> regs .enable [bank ] |= (1 << shift );
147
+ module .sources -> regs .enable [bank ] |= (1 << shift ); // set enable bit
148
148
} else {
149
149
module .sources -> regs .priority [source ] = 0 ;
150
- module .sources -> regs .enable [bank ] &= ~(1 << shift );
150
+ module .sources -> regs .enable [bank ] &= ~(1 << shift ); // clear enable bit
151
151
}
152
152
}
153
153
@@ -162,6 +162,6 @@ void interrupts_disable_source(interrupt_source_t source) {
162
162
void interrupts_register_handler (interrupt_source_t source , handlerfn_t fn , void * aux_data ) {
163
163
if (!module .initialized ) error ("interrupts_init() has not been called!\n" );
164
164
if (!is_valid_source (source )) error ("request to register handler for interrupt source that is not valid" );
165
- module .handlers [source ].fn = fn ;
166
- module .handlers [source ].aux_data = aux_data ;
165
+ module .handlers [source ].fn = fn ; // store handler function and
166
+ module .handlers [source ].aux_data = aux_data ; // aux_data pointer into array at index corresponding to source
167
167
}
0 commit comments