Skip to content

Commit 2a9d778

Browse files
Launcher: Fix crash after 40 launches
A long running test (tests/ramcard.lua) that restarted multiple times including launching DeskTop.system and rebooting via $C700 would cause the stack to "leak", eventually tickling a CFFA2 firmware issue where the return address was calculated improperly and we'd crash to the monitor. I'd assumed (incorrectly) that ProDOS would set the stack pointer on startup. Reset the stack pointer in the launcher, because no-one else does. Thanks to @peterferrie for diagnosing the issue.
1 parent 38c92e2 commit 2a9d778

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Project Page: https://github.com/a2stuff/a2d
1414

1515
### Launcher
1616

17+
* Fix crash after 40 restarts.
1718

1819
### DeskTop
1920

src/desktop.system/desktop.system.s

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ start:
9999
;; Do this for good measure.
100100
cli
101101

102+
;; Clear stack, because ProDOS doesn't.
103+
ldx #$FF
104+
txs
105+
102106
jsr Check128K ; QUITs if check fails
103107
jsr ClearScreenEnable80Cols
104108
jsr CheckRAMEmpty ; QUITs if user cancels

tests/lib/apple2.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,4 +874,16 @@ end
874874

875875
--------------------------------------------------
876876

877+
function apple2.IsCrashedToMonitor()
878+
local cpu = manager.machine.devices[":maincpu"]
879+
local sp = cpu.state.SP.value
880+
if sp == 0x1FE and apple2.ReadMemory(sp) == 0x4E and apple2.ReadMemory(sp+1) == 0xEB then
881+
return true
882+
else
883+
return false
884+
end
885+
end
886+
887+
--------------------------------------------------
888+
877889
return apple2

tests/stack_exhaustion.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--[[ BEGINCONFIG ========================================
2+
3+
MODELARGS="-sl1 ramfactor -sl2 mouse -sl6 '' -sl7 cffa2 -aux ext80"
4+
DISKARGS="-hard1 $HARDIMG"
5+
6+
======================================== ENDCONFIG ]]--
7+
8+
-- A CFFA2 is specified the repro tickles a firmware issue where low
9+
-- stack causes improper address calculation and we crash to the
10+
-- monitor. Low stack being a problem is not specific to the CFFA,
11+
-- however.
12+
13+
test.Step(
14+
"Stack exhaustion (takes about 122 seconds to run)",
15+
function()
16+
local cpu = manager.machine.devices[":maincpu"]
17+
18+
-- Prior to fix, crashes around iteration 40
19+
for i = 1,50 do
20+
--print(string.format("i=%d SP=%04X", i, cpu.state.SP.value))
21+
a2d.CloseAllWindows()
22+
a2d.OpenPath("/A2.DESKTOP/DESKTOP.SYSTEM")
23+
a2d.WaitForRestart()
24+
test.Expect(not apple2.IsCrashedToMonitor(), "should not have crashed to monitor")
25+
end
26+
end)

0 commit comments

Comments
 (0)