@@ -138,6 +138,9 @@ export class StateMachine {
138
138
139
139
clockDivInt : number = 1 ;
140
140
clockDivFrac : number = 0 ;
141
+ curClockInt : number = 0 ;
142
+ curClockFrac : number = 0 ;
143
+ remainingDelay : number = 0 ;
141
144
execCtrl = 0x1f << 12 ;
142
145
shiftCtrl = 0b11 << 18 ;
143
146
pinCtrl = 0x5 << 26 ;
@@ -436,7 +439,7 @@ export class StateMachine {
436
439
}
437
440
}
438
441
439
- executeInstruction ( opcode : number ) {
442
+ executeInstruction ( opcode : number ) : number {
440
443
const arg = opcode & 0xff ;
441
444
switch ( opcode >>> 13 ) {
442
445
/* JMP */
@@ -654,14 +657,16 @@ export class StateMachine {
654
657
655
658
if ( this . execValid ) {
656
659
this . execValid = false ;
657
- this . executeInstruction ( this . execOpcode ) ;
660
+ return this . executeInstruction ( this . execOpcode ) ;
658
661
} else if ( this . waiting ) {
659
662
if ( this . waitDelay < 0 ) {
660
663
this . waitDelay = delay ;
661
664
}
662
665
this . checkWait ( ) ;
666
+ return delay ;
663
667
} else {
664
668
this . cycles += delay ;
669
+ return delay ;
665
670
}
666
671
}
667
672
@@ -683,6 +688,27 @@ export class StateMachine {
683
688
}
684
689
685
690
step ( ) {
691
+ if ( ! this . enabled ) {
692
+ return ;
693
+ }
694
+
695
+ this . curClockFrac += this . clockDivFrac ;
696
+ if ( this . curClockFrac > 0xff ) {
697
+ this . curClockInt ++ ;
698
+ this . curClockFrac -= 0x100 ;
699
+ }
700
+ this . curClockInt ++ ;
701
+ if ( this . curClockInt < this . clockDivInt ) {
702
+ return ;
703
+ } else {
704
+ this . curClockInt -= this . clockDivInt ;
705
+ }
706
+
707
+ if ( this . remainingDelay > 0 ) {
708
+ this . remainingDelay -- ;
709
+ return ;
710
+ }
711
+
686
712
if ( this . waiting ) {
687
713
this . checkWait ( ) ;
688
714
if ( this . waiting ) {
@@ -691,7 +717,7 @@ export class StateMachine {
691
717
}
692
718
693
719
this . updatePC = true ;
694
- this . executeInstruction ( this . pio . instructions [ this . pc ] ) ;
720
+ this . remainingDelay += this . executeInstruction ( this . pio . instructions [ this . pc ] ) ;
695
721
if ( this . updatePC ) {
696
722
this . nextPC ( ) ;
697
723
}
@@ -833,6 +859,9 @@ export class StateMachine {
833
859
834
860
restart ( ) {
835
861
this . cycles = 0 ;
862
+ this . curClockInt = 0 ;
863
+ this . curClockFrac = 0 ;
864
+ this . remainingDelay = 0 ;
836
865
this . inputShiftCount = 0 ;
837
866
this . outputShiftCount = 32 ;
838
867
this . inputShiftReg = 0 ;
@@ -1080,7 +1109,6 @@ export class RPPIO extends BasePeripheral implements Peripheral {
1080
1109
const shouldRun = value & 0xf ;
1081
1110
if ( this . stopped && shouldRun ) {
1082
1111
this . stopped = false ;
1083
- this . run ( ) ;
1084
1112
}
1085
1113
if ( ! shouldRun ) {
1086
1114
this . stopped = true ;
@@ -1179,21 +1207,15 @@ export class RPPIO extends BasePeripheral implements Peripheral {
1179
1207
}
1180
1208
1181
1209
step ( ) {
1210
+ if ( this . stopped ) {
1211
+ return ;
1212
+ }
1182
1213
for ( const machine of this . machines ) {
1183
1214
machine . step ( ) ;
1184
1215
}
1185
1216
this . checkChangedPins ( ) ;
1186
1217
}
1187
1218
1188
- run ( ) {
1189
- for ( let i = 0 ; i < 1000 && ! this . stopped ; i ++ ) {
1190
- this . step ( ) ;
1191
- }
1192
- if ( ! this . stopped ) {
1193
- this . runTimer = setTimeout ( ( ) => this . run ( ) , 0 ) ;
1194
- }
1195
- }
1196
-
1197
1219
stop ( ) {
1198
1220
for ( const machine of this . machines ) {
1199
1221
machine . enabled = false ;
0 commit comments