1
0
mirror of https://github.com/cc65/cc65.git synced 2024-11-19 21:32:19 +00:00
cc65/libsrc/apple2/crt0.s
cuz 10431b1067 Added an exit handler - code from Oliver Schmidt
git-svn-id: svn://svn.cc65.org/cc65/trunk@3341 b7a2c559-68d2-44c3-8de9-860c34a00d81
2004-12-21 15:56:48 +00:00

133 lines
2.4 KiB
ArmAsm

;
; Startup code for cc65 (Apple2 version)
;
; This must be the *first* file on the linker command line
;
.export _exit, __Exit
.import initlib, donelib
.import zerobss
.import __STARTUP_LOAD__, __BSS_LOAD__ ; Linker generated
.import callmain
.include "zeropage.inc"
.include "apple2.inc"
; ------------------------------------------------------------------------
; The executable header
.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
: lda sp,x
sta zpsave,x
dex
bpl :-
; Save the original RESET vector
ldx #$02
: lda SOFTEV,x
sta rvsave,x
dex
bpl :-
; ProDOS TechRefMan, chapter 5.3.5:
; "Your system program should place in the RESET vector the address of a
; routine that ... closes the files."
ldx #<_exit
lda #>_exit
jsr reset ; Setup RESET vector
; Clear the BSS data
jsr zerobss
; Setup the stack
lda HIMEM
sta sp
lda HIMEM+1
sta sp+1 ; Set argument stack ptr
; Call module constructors
jsr initlib
; Push arguments and call main()
jsr callmain
; Avoid re-entrance of donelib. This is also the _exit entry
_exit: ldx #<__Exit
lda #>__Exit
jsr reset ; Setup RESET vector
; Call module destructors
jsr donelib
; Restore the original RESET vector. This is also the __Exit entry
__Exit: ldx #$02
: lda rvsave,x
sta SOFTEV,x
dex
bpl :-
; Copy back the zero page stuff
ldx #zpspace-1
: lda zpsave,x
sta sp,x
dex
bpl :-
; ProDOS TechRefMan, chapter 5.2.1:
; "System programs should set the stack pointer to $FF at the warm-start
; entry point."
ldx #$FF
txs ; Re-init stack pointer
; Back to DOS
jmp DOSWARM
; ------------------------------------------------------------------------
; Setup RESET vector
reset: stx SOFTEV
sta SOFTEV+1
eor #$A5
sta PWREDUP
rts
; ------------------------------------------------------------------------
; Data
.data
zpsave: .res zpspace
rvsave: .res 3