2 ; Oliver Schmidt, 2009-09-15
4 ; Startup code for cc65 (Apple2 version)
7 .export _exit, done, return
8 .export __STARTUP__ : absolute = 1 ; Mark as startup
10 .import initlib, donelib
11 .import zerobss, callmain
12 .import __ONCE_LOAD__, __ONCE_SIZE__ ; Linker generated
13 .import __LC_START__, __LC_LAST__ ; Linker generated
15 .include "zeropage.inc"
18 ; ------------------------------------------------------------------------
22 ; ProDOS TechRefMan, chapter 5.2.1:
23 ; "For maximum interrupt efficiency, a system program should not
24 ; use more than the upper 3/4 of the stack."
26 txs ; Init stack pointer
28 ; Save space by putting some of the start-up code in the ONCE segment,
29 ; which can be re-used by the BSS segment, the heap and the C stack.
35 ; Push the command-line arguments; and, call main().
38 ; Avoid a re-entrance of donelib. This is also the exit() entry.
41 jsr reset ; Setup RESET vector
43 ; Switch in ROM, in case it wasn't already switched in by a RESET.
46 ; Call the module destructors.
49 ; Restore the original RESET vector.
56 ; Copy back the zero-page stuff.
63 ; ProDOS TechRefMan, chapter 5.2.1:
64 ; "System programs should set the stack pointer to $FF at the
65 ; warm-start entry point."
67 txs ; Re-init stack pointer
72 ; ------------------------------------------------------------------------
76 ; Save the zero-page locations that we need.
83 ; Save the original RESET vector.
91 ldy $BF00 ; MLI call entry point
92 cpy #$4C ; Is MLI present? (JMP opcode)
95 ; Check the ProDOS system bit map.
96 lda $BF6F ; Protection for pages $B8 - $BF
97 cmp #%00000001 ; Exactly system global page is protected
100 ; No BASIC.SYSTEM; so, quit to the ProDOS dispatcher instead.
106 ; No BASIC.SYSTEM; so, use the addr of the ProDOS system global page.
109 bne :+ ; Branch always
111 ; Get the highest available mem addr from the BASIC interpreter.
115 ; Set up the C stack.
119 ; ProDOS TechRefMan, chapter 5.3.5:
120 ; "Your system program should place in the RESET vector the
121 ; address of a routine that ... closes the files."
124 jsr reset ; Setup RESET vector
126 ; Call the module constructors.
129 ; Switch in LC bank 2 for W/O.
133 ; Set the source start address.
134 ; Aka __LC_LOAD__ iff segment LC exists.
135 lda #<(__ONCE_LOAD__ + __ONCE_SIZE__)
136 ldy #>(__ONCE_LOAD__ + __ONCE_SIZE__)
140 ; Set the source last address.
141 ; Aka __LC_LOAD__ + __LC_SIZE__ iff segment LC exists.
142 lda #<((__ONCE_LOAD__ + __ONCE_SIZE__) + (__LC_LAST__ - __LC_START__))
143 ldy #>((__ONCE_LOAD__ + __ONCE_SIZE__) + (__LC_LAST__ - __LC_START__))
147 ; Set the destination last address.
148 ; Aka __LC_RUN__ + __LC_SIZE__ iff segment LC exists.
154 ; Call into Applesoft Block Transfer Up -- which handles zero-
155 ; sized blocks well -- to move the content of the LC memory area.
158 ; Switch in LC bank 2 for R/O and return.
162 ; ------------------------------------------------------------------------
166 ; Set up the RESET vector.
173 ; Quit to the ProDOS dispatcher.
174 quit: jsr $BF00 ; MLI call entry point
178 ; ------------------------------------------------------------------------
182 ; MLI parameter list for quit
183 q_param:.byte $04 ; param_count
184 .byte $00 ; quit_type
185 .word $0000 ; reserved
187 .word $0000 ; reserved
189 ; ------------------------------------------------------------------------
193 ; Final jump when we're done
194 done: jmp DOSWARM ; Potentially patched at runtime
196 ; ------------------------------------------------------------------------