Skip to content

Commit c3d84f4

Browse files
committed
cycle ordering was broken the JSR instruction
bug introduced in db2d84c, which would have first appeared in release v0.27.0 this subtle bug broke the alignment of the ball and missile in the worm whomper demo (by Manuel Rotschkar) this fix adds the cycle boundary after the lo-byte read of the destination address
1 parent 0051300 commit c3d84f4

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

hardware/cpu/cpu.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,11 @@ func (mc *CPU) ExecuteInstruction(cycleCallback func() error) error {
13261326
if err != nil {
13271327
return err
13281328
}
1329+
mc.LastResult.Cycles++
1330+
err = mc.cycleCallback()
1331+
if err != nil {
1332+
return err
1333+
}
13291334

13301335
// the current value of the PC is now correct, even though we've only read
13311336
// one byte of the address so far. remember, RTS increments the PC when
@@ -1337,24 +1342,7 @@ func (mc *CPU) ExecuteInstruction(cycleCallback func() error) error {
13371342
if err != nil {
13381343
return err
13391344
}
1340-
mc.LastResult.Cycles++
1341-
err = mc.cycleCallback()
1342-
if err != nil {
1343-
return err
1344-
}
1345-
1346-
// adjust stack pointer
13471345
mc.SP.Add(0xff, false)
1348-
1349-
// address of the hibyte being pushed to the stack is on the bus for an
1350-
// additional cycle
1351-
//
1352-
// it's not entirely clear what's happening here. it might be to
1353-
// accomodate the SP adjustment but there is no dead cycle later on when
1354-
// the SP is adjusted again; and nor is there one in RTS when the return
1355-
// address is read from the stack
1356-
//
1357-
// +1 cycle
13581346
mc.LastResult.Cycles++
13591347
err = mc.cycleCallback()
13601348
if err != nil {
@@ -1367,15 +1355,13 @@ func (mc *CPU) ExecuteInstruction(cycleCallback func() error) error {
13671355
if err != nil {
13681356
return err
13691357
}
1358+
mc.SP.Add(0xff, false)
13701359
mc.LastResult.Cycles++
13711360
err = mc.cycleCallback()
13721361
if err != nil {
13731362
return err
13741363
}
13751364

1376-
// adjust stack pointer
1377-
mc.SP.Add(0xff, false)
1378-
13791365
// +1 cycle
13801366
err = mc.read8BitPC(hiByte)
13811367
if err != nil {

0 commit comments

Comments
 (0)