.export _exit
.import initlib, donelib
- .import zerobss, push0
- .import _main
+ .import zerobss
+ .import __STARTUP_LOAD__, __BSS_LOAD__ ; Linker generated
+ .import callmain
.include "zeropage.inc"
.include "apple2.inc"
; ------------------------------------------------------------------------
-; Actual code
+; The executable header
-.code
+.segment "EXEHDR"
+
+ .word __STARTUP_LOAD__ ; Start address
+ .word __BSS_LOAD__ - __STARTUP_LOAD__ ; Size
+
+; ------------------------------------------------------------------------
+; Place the startup code in a special segment.
+
+.segment "STARTUP"
+
+; ProDOS TechRefMan, chapter 5.2.1:
+; "For maximum interrupt efficiency, a system program should not use more
+; than the upper 3/4 of the stack."
+
+ ldx #$FF
+ txs ; Init stack pointer
+
+; Save the zero page locations we need
ldx #zpspace-1
L1: lda sp,x
- sta zpsave,x ; Save the zero page locations we need
+ sta zpsave,x
dex
bpl L1
; Save system stuff and setup the stack
- tsx
- stx spsave ; Save the system stack ptr
-
- lda #<TOPMEM
+ lda HIMEM
sta sp
- lda #>TOPMEM
- sta sp+1 ; Set argument stack ptr
+ lda HIMEM+1
+ sta sp+1 ; Set argument stack ptr
; Call module constructors
jsr initlib
-; Initialize conio stuff
-
- lda #$ff
- sta TEXTTYP
-
-; Set up to use Apple ROM $C000-$CFFF
-
- ;; sta USEROM
+; Push arguments and call main()
-; Pass an empty command line
-
- jsr push0 ; argc
- jsr push0 ; argv
-
- ldy #4 ; Argument size
- jsr _main ; call the users code
+ jsr callmain
; Call module destructors. This is also the _exit entry.
_exit: jsr donelib
-; Restore system stuff
-
- lda #$ff ; Reset text mode
- sta TEXTTYP
-
- ldx spsave
- txs ; Restore stack pointer
-
; Copy back the zero page stuff
ldx #zpspace-1
dex
bpl L2
-; Reset changed vectors, back to basic
+; ProDOS TechRefMan, chapter 5.2.1:
+; "System programs should set the stack pointer to $FF at the warm-start
+; entry point."
- jmp RESTOR
+ ldx #$FF
+ txs ; Re-init stack pointer
+; Back to DOS
-.data
+ jmp DOSWARM
-zpsave: .res zpspace
+; ------------------------------------------------------------------------
+; Data
-.bss
+.data
-spsave: .res 1
+zpsave: .res zpspace