@@ -173,23 +173,29 @@ export class CPU implements ICPU {
173
173
}
174
174
}
175
175
176
- private updateClockEvents ( ) {
177
- this . clockEvents . sort ( ( a , b ) => a . cycles - b . cycles ) ;
178
- this . nextClockEvent = this . clockEvents [ 0 ] ?. cycles ?? 0 ;
179
- }
180
-
181
176
addClockEvent ( callback : AVRClockEventCallback , cycles : number ) {
182
177
const entry = { cycles : this . cycles + Math . max ( 1 , cycles ) , callback } ;
183
- this . clockEvents . push ( entry ) ;
184
- this . updateClockEvents ( ) ;
178
+ // Add the new entry while keeping the array sorted
179
+ const { clockEvents } = this ;
180
+ if ( ! clockEvents . length || clockEvents [ clockEvents . length - 1 ] . cycles <= entry . cycles ) {
181
+ clockEvents . push ( entry ) ;
182
+ } else if ( clockEvents [ 0 ] . cycles >= entry . cycles ) {
183
+ clockEvents . unshift ( entry ) ;
184
+ } else {
185
+ for ( let i = 1 ; i < clockEvents . length ; i ++ ) {
186
+ if ( clockEvents [ i ] . cycles >= entry . cycles ) {
187
+ clockEvents . splice ( i , 0 , entry ) ;
188
+ break ;
189
+ }
190
+ }
191
+ }
192
+ this . nextClockEvent = this . clockEvents [ 0 ] . cycles ;
185
193
return callback ;
186
194
}
187
195
188
196
updateClockEvent ( callback : AVRClockEventCallback , cycles : number ) {
189
- const entry = this . clockEvents . find ( ( item ) => item . callback === callback ) ;
190
- if ( entry ) {
191
- entry . cycles = this . cycles + Math . max ( 1 , cycles ) ;
192
- this . updateClockEvents ( ) ;
197
+ if ( this . clearClockEvent ( callback ) ) {
198
+ this . addClockEvent ( callback , cycles ) ;
193
199
return true ;
194
200
}
195
201
return false ;
@@ -199,18 +205,22 @@ export class CPU implements ICPU {
199
205
const index = this . clockEvents . findIndex ( ( item ) => item . callback === callback ) ;
200
206
if ( index >= 0 ) {
201
207
this . clockEvents . splice ( index , 1 ) ;
202
- this . updateClockEvents ( ) ;
208
+ this . nextClockEvent = this . clockEvents [ 0 ] ?. cycles ?? 0 ;
209
+ return true ;
203
210
}
211
+ return false ;
204
212
}
205
213
206
214
tick ( ) {
207
- if ( this . nextClockEvent && this . nextClockEvent < = this . cycles ) {
208
- const clockEvent = this . clockEvents . shift ( ) ;
209
- clockEvent ?. callback ( ) ;
210
- this . nextClockEvent = this . clockEvents [ 0 ] ?. cycles ?? 0 ;
215
+ const { nextClockEvent, clockEvents } = this ;
216
+ if ( nextClockEvent && nextClockEvent < = this . cycles ) {
217
+ clockEvents . shift ( ) ?. callback ( ) ;
218
+ this . nextClockEvent = clockEvents [ 0 ] ?. cycles ?? 0 ;
211
219
}
212
- if ( this . interruptsEnabled && this . nextInterrupt >= 0 ) {
213
- const interrupt = this . pendingInterrupts [ this . nextInterrupt ] ;
220
+
221
+ const { nextInterrupt } = this ;
222
+ if ( this . interruptsEnabled && nextInterrupt >= 0 ) {
223
+ const interrupt = this . pendingInterrupts [ nextInterrupt ] ;
214
224
avrInterrupt ( this , interrupt . address ) ;
215
225
if ( ! interrupt . constant ) {
216
226
this . clearInterrupt ( interrupt ) ;
0 commit comments