2000-05-28 13:40:48 +00:00
|
|
|
;
|
|
|
|
; Startup code for cc65 (Apple2 version)
|
|
|
|
;
|
|
|
|
; This must be the *first* file on the linker command line
|
|
|
|
;
|
|
|
|
|
2004-12-21 15:56:48 +00:00
|
|
|
.export _exit, __Exit
|
2000-11-22 22:19:09 +00:00
|
|
|
.import initlib, donelib
|
2004-03-11 21:54:22 +00:00
|
|
|
.import zerobss
|
2002-11-23 11:10:50 +00:00
|
|
|
.import __STARTUP_LOAD__, __BSS_LOAD__ ; Linker generated
|
2003-03-10 21:21:46 +00:00
|
|
|
.import callmain
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-05-26 09:09:10 +00:00
|
|
|
.include "zeropage.inc"
|
|
|
|
.include "apple2.inc"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-07-05 21:11:16 +00:00
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; The executable header
|
|
|
|
|
|
|
|
.segment "EXEHDR"
|
|
|
|
|
2002-11-23 11:10:50 +00:00
|
|
|
.word __STARTUP_LOAD__ ; Start address
|
|
|
|
.word __BSS_LOAD__ - __STARTUP_LOAD__ ; Size
|
2002-07-05 21:11:16 +00:00
|
|
|
|
2002-11-23 11:10:50 +00:00
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Place the startup code in a special segment.
|
|
|
|
|
|
|
|
.segment "STARTUP"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2004-03-11 21:54:22 +00:00
|
|
|
; 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
|
|
|
|
|
2000-10-30 21:46:45 +00:00
|
|
|
ldx #zpspace-1
|
2004-12-21 15:56:48 +00:00
|
|
|
: lda sp,x
|
2004-03-11 21:54:22 +00:00
|
|
|
sta zpsave,x
|
2000-10-30 21:46:45 +00:00
|
|
|
dex
|
2004-12-21 15:56:48 +00:00
|
|
|
bpl :-
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2004-12-21 15:56:48 +00:00
|
|
|
; 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
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
; Clear the BSS data
|
|
|
|
|
|
|
|
jsr zerobss
|
|
|
|
|
2004-12-21 15:56:48 +00:00
|
|
|
; Setup the stack
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2004-03-11 21:54:22 +00:00
|
|
|
lda HIMEM
|
2000-05-28 13:40:48 +00:00
|
|
|
sta sp
|
2004-03-11 21:54:22 +00:00
|
|
|
lda HIMEM+1
|
2003-03-10 21:21:46 +00:00
|
|
|
sta sp+1 ; Set argument stack ptr
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-11-22 22:19:09 +00:00
|
|
|
; Call module constructors
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-11-22 22:19:09 +00:00
|
|
|
jsr initlib
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-03-10 21:21:46 +00:00
|
|
|
; Push arguments and call main()
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-03-10 21:21:46 +00:00
|
|
|
jsr callmain
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2004-12-21 15:56:48 +00:00
|
|
|
; 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
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2004-12-21 15:56:48 +00:00
|
|
|
; Restore the original RESET vector. This is also the __Exit entry
|
|
|
|
|
|
|
|
__Exit: ldx #$02
|
|
|
|
: lda rvsave,x
|
|
|
|
sta SOFTEV,x
|
|
|
|
dex
|
|
|
|
bpl :-
|
2002-05-26 09:09:10 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
; Copy back the zero page stuff
|
|
|
|
|
2000-10-30 21:46:45 +00:00
|
|
|
ldx #zpspace-1
|
2004-12-21 15:56:48 +00:00
|
|
|
: lda zpsave,x
|
2000-10-30 21:46:45 +00:00
|
|
|
sta sp,x
|
|
|
|
dex
|
2004-12-21 15:56:48 +00:00
|
|
|
bpl :-
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2004-03-11 21:54:22 +00:00
|
|
|
; 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
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2004-03-11 21:54:22 +00:00
|
|
|
; Back to DOS
|
|
|
|
|
|
|
|
jmp DOSWARM
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2004-12-21 15:56:48 +00:00
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Setup RESET vector
|
|
|
|
|
|
|
|
reset: stx SOFTEV
|
|
|
|
sta SOFTEV+1
|
|
|
|
eor #$A5
|
|
|
|
sta PWREDUP
|
|
|
|
rts
|
|
|
|
|
2002-11-23 11:10:50 +00:00
|
|
|
; ------------------------------------------------------------------------
|
|
|
|
; Data
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
.data
|
|
|
|
|
|
|
|
zpsave: .res zpspace
|
2004-12-21 15:56:48 +00:00
|
|
|
|
|
|
|
rvsave: .res 3
|