@@ -9,11 +9,18 @@ import { AVRInterruptConfig, CPU } from '../cpu/cpu';
99import { u8 } from '../types' ;
1010
1111export interface AVRExternalInterrupt {
12- EICRA : u8 ;
13- EICRB : u8 ;
12+ /** either EICRA or EICRB, depending on which register holds the ISCx0/ISCx1 bits for this interrupt */
13+ EICR : u8 ;
1414 EIMSK : u8 ;
1515 EIFR : u8 ;
16+
17+ /* Offset of the ISCx0/ISCx1 bits in the EICRx register */
18+ iscOffset : u8 ;
19+
20+ /** Bit index in the EIMSK / EIFR registers */
1621 index : u8 ; // 0..7
22+
23+ /** Interrupt vector index */
1724 interrupt : u8 ;
1825}
1926
@@ -39,20 +46,20 @@ export interface AVRPortConfig {
3946}
4047
4148export const INT0 : AVRExternalInterrupt = {
42- EICRA : 0x69 ,
43- EICRB : 0 ,
49+ EICR : 0x69 ,
4450 EIMSK : 0x3d ,
4551 EIFR : 0x3c ,
4652 index : 0 ,
53+ iscOffset : 0 ,
4754 interrupt : 2 ,
4855} ;
4956
5057export const INT1 : AVRExternalInterrupt = {
51- EICRA : 0x69 ,
52- EICRB : 0 ,
58+ EICR : 0x69 ,
5359 EIMSK : 0x3d ,
5460 EIFR : 0x3c ,
5561 index : 1 ,
62+ iscOffset : 2 ,
5663 interrupt : 4 ,
5764} ;
5865
@@ -254,10 +261,10 @@ export class AVRIOPort {
254261 }
255262 : null
256263 ) ;
257- const EICRA = externalInterrupts . find ( ( item ) => item && item . EICRA ) ?. EICRA ?? 0 ;
258- this . attachInterruptHook ( EICRA ) ;
259- const EICRB = externalInterrupts . find ( ( item ) => item && item . EICRB ) ?. EICRB ?? 0 ;
260- this . attachInterruptHook ( EICRB ) ;
264+ const EICR = new Set ( externalInterrupts . map ( ( item ) => item ?. EICR ) ) ;
265+ for ( const EICRx of EICR ) {
266+ this . attachInterruptHook ( EICRx || 0 ) ;
267+ }
261268 const EIMSK = externalInterrupts . find ( ( item ) => item && item . EIMSK ) ?. EIMSK ?? 0 ;
262269 this . attachInterruptHook ( EIMSK , 'mask' ) ;
263270 const EIFR = externalInterrupts . find ( ( item ) => item && item . EIFR ) ?. EIFR ?? 0 ;
@@ -394,11 +401,9 @@ export class AVRIOPort {
394401 const externalConfig = externalInterrupts [ pin ] ;
395402 const external = externalInts [ pin ] ;
396403 if ( external && externalConfig ) {
397- const { index , EICRA , EICRB , EIMSK } = externalConfig ;
404+ const { EIMSK , index , EICR , iscOffset } = externalConfig ;
398405 if ( cpu . data [ EIMSK ] & ( 1 << index ) ) {
399- const configRegister = index >= 4 ? EICRB : EICRA ;
400- const configShift = ( index % 4 ) * 2 ;
401- const configuration = ( cpu . data [ configRegister ] >> configShift ) & 0x3 ;
406+ const configuration = ( cpu . data [ EICR ] >> iscOffset ) & 0x3 ;
402407 let generateInterrupt = false ;
403408 external . constant = false ;
404409 switch ( configuration ) {
@@ -469,13 +474,11 @@ export class AVRIOPort {
469474 continue ;
470475 }
471476 const pinValue = ! ! ( this . lastPin & ( 1 << pin ) ) ;
472- const { index , EICRA , EICRB , EIMSK , EIFR , interrupt } = external ;
477+ const { EIFR , EIMSK , index , EICR , iscOffset , interrupt } = external ;
473478 if ( ! ( cpu . data [ EIMSK ] & ( 1 << index ) ) || pinValue ) {
474479 continue ;
475480 }
476- const configRegister = index >= 4 ? EICRB : EICRA ;
477- const configShift = ( index % 4 ) * 2 ;
478- const configuration = ( cpu . data [ configRegister ] >> configShift ) & 0x3 ;
481+ const configuration = ( cpu . data [ EICR ] >> iscOffset ) & 0x3 ;
479482 if ( configuration === InterruptMode . LowLevel ) {
480483 cpu . queueInterrupt ( {
481484 address : interrupt ,
0 commit comments