@@ -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 ;
@@ -930,7 +959,6 @@ export class RPPIO extends BasePeripheral implements Peripheral {
930
959
pinDirections = 0 ;
931
960
oldPinValues = 0 ;
932
961
oldPinDirections = 0 ;
933
- private runTimer : NodeJS . Timeout | null = null ;
934
962
935
963
irq0IntEnable = 0 ;
936
964
irq0IntForce = 0 ;
@@ -1077,14 +1105,7 @@ export class RPPIO extends BasePeripheral implements Peripheral {
1077
1105
this . machines [ index ] . clkDivRestart ( ) ;
1078
1106
}
1079
1107
}
1080
- const shouldRun = value & 0xf ;
1081
- if ( this . stopped && shouldRun ) {
1082
- this . stopped = false ;
1083
- this . run ( ) ;
1084
- }
1085
- if ( ! shouldRun ) {
1086
- this . stopped = true ;
1087
- }
1108
+ this . stopped = ! ( value & 0xf ) ;
1088
1109
break ;
1089
1110
}
1090
1111
case FDEBUG :
@@ -1179,29 +1200,19 @@ export class RPPIO extends BasePeripheral implements Peripheral {
1179
1200
}
1180
1201
1181
1202
step ( ) {
1203
+ if ( this . stopped ) {
1204
+ return ;
1205
+ }
1182
1206
for ( const machine of this . machines ) {
1183
1207
machine . step ( ) ;
1184
1208
}
1185
1209
this . checkChangedPins ( ) ;
1186
1210
}
1187
1211
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
1212
stop ( ) {
1198
1213
for ( const machine of this . machines ) {
1199
1214
machine . enabled = false ;
1200
1215
}
1201
1216
this . stopped = true ;
1202
- if ( this . runTimer ) {
1203
- clearTimeout ( this . runTimer ) ;
1204
- this . runTimer = null ;
1205
- }
1206
1217
}
1207
1218
}
0 commit comments