Skip to content

Commit 622793e

Browse files
colinleroyoliverschmidt
authored andcommitted
Apple II: Move _exit out of STARTUP segment
1 parent b688cfa commit 622793e

File tree

2 files changed

+79
-40
lines changed

2 files changed

+79
-40
lines changed

libsrc/apple2/callmain.s

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
;
2+
; Ullrich von Bassewitz, 2003-03-07
3+
;
4+
; Push arguments and call main()
5+
;
6+
7+
8+
.export callmain, _exit
9+
.export __argc, __argv
10+
11+
.import _main, pushax, done, donelib
12+
.import zpsave, rvsave, reset
13+
14+
.include "zeropage.inc"
15+
.include "apple2.inc"
16+
17+
18+
;---------------------------------------------------------------------------
19+
; Setup the stack for main(), then jump to it
20+
21+
callmain:
22+
lda __argc
23+
ldx __argc+1
24+
jsr pushax ; Push argc
25+
26+
lda __argv
27+
ldx __argv+1
28+
jsr pushax ; Push argv
29+
30+
ldy #4 ; Argument size
31+
jsr _main
32+
33+
; Avoid a re-entrance of donelib. This is also the exit() entry.
34+
_exit: ldx #<exit
35+
lda #>exit
36+
jsr reset ; Setup RESET vector
37+
38+
; Switch in LC bank 2 for R/O in case it was switched out by a RESET.
39+
bit $C080
40+
41+
; Call the module destructors.
42+
jsr donelib
43+
44+
; Switch in ROM.
45+
bit $C082
46+
47+
; Restore the original RESET vector.
48+
exit: ldx #$02
49+
: lda rvsave,x
50+
sta SOFTEV,x
51+
dex
52+
bpl :-
53+
54+
; Copy back the zero-page stuff.
55+
ldx #zpspace-1
56+
: lda zpsave,x
57+
sta sp,x
58+
dex
59+
bpl :-
60+
61+
; ProDOS TechRefMan, chapter 5.2.1:
62+
; "System programs should set the stack pointer to $FF at the
63+
; warm-start entry point."
64+
ldx #$FF
65+
txs ; Re-init stack pointer
66+
67+
; We're done
68+
jmp done
69+
70+
;---------------------------------------------------------------------------
71+
; Data
72+
73+
.data
74+
__argc: .word 0
75+
__argv: .addr 0

libsrc/apple2/crt0.s

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
; Startup code for cc65 (Apple2 version)
55
;
66

7-
.export _exit, done, return
7+
.export done, return
8+
.export zpsave, rvsave, reset
89
.export __STARTUP__ : absolute = 1 ; Mark as startup
910

10-
.import initlib, donelib
11+
.import initlib, _exit
1112
.import zerobss, callmain
1213
.import __ONCE_LOAD__, __ONCE_SIZE__ ; Linker generated
1314
.import __LC_START__, __LC_LAST__ ; Linker generated
@@ -33,44 +34,7 @@
3334
jsr zerobss
3435

3536
; Push the command-line arguments; and, call main().
36-
jsr callmain
37-
38-
; Avoid a re-entrance of donelib. This is also the exit() entry.
39-
_exit: ldx #<exit
40-
lda #>exit
41-
jsr reset ; Setup RESET vector
42-
43-
; Switch in LC bank 2 for R/O in case it was switched out by a RESET.
44-
bit $C080
45-
46-
; Call the module destructors.
47-
jsr donelib
48-
49-
; Switch in ROM.
50-
bit $C082
51-
52-
; Restore the original RESET vector.
53-
exit: ldx #$02
54-
: lda rvsave,x
55-
sta SOFTEV,x
56-
dex
57-
bpl :-
58-
59-
; Copy back the zero-page stuff.
60-
ldx #zpspace-1
61-
: lda zpsave,x
62-
sta sp,x
63-
dex
64-
bpl :-
65-
66-
; ProDOS TechRefMan, chapter 5.2.1:
67-
; "System programs should set the stack pointer to $FF at the
68-
; warm-start entry point."
69-
ldx #$FF
70-
txs ; Re-init stack pointer
71-
72-
; We're done
73-
jmp done
37+
jmp callmain
7438

7539
; ------------------------------------------------------------------------
7640

0 commit comments

Comments
 (0)