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 callmain, callirq
12 .import __LC_START__, __LC_LAST__ ; Linker generated
13 .import __INIT_RUN__, __INIT_SIZE__ ; Linker generated
14 .import __ZPSAVE_RUN__ ; Linker generated
15 .import __INTERRUPTOR_COUNT__ ; Linker generated
17 .include "zeropage.inc"
20 ; ------------------------------------------------------------------------
24 ; ProDOS TechRefMan, chapter 5.2.1:
25 ; "For maximum interrupt efficiency, a system program should not
26 ; use more than the upper 3/4 of the stack."
28 txs ; Init stack pointer
30 ; Switch in LC bank 2 for W/O
34 ; Set source start address
35 lda #<(__ZPSAVE_RUN__ + __INIT_SIZE__)
36 ldy #>(__ZPSAVE_RUN__ + __INIT_SIZE__)
40 ; Set source last address
41 lda #<(__ZPSAVE_RUN__ + __INIT_SIZE__ + __LC_LAST__ - __LC_START__)
42 ldy #>(__ZPSAVE_RUN__ + __INIT_SIZE__ + __LC_LAST__ - __LC_START__)
46 ; Set destination last address
52 ; Call into Applesoft Block Transfer Utility - which handles zero
53 ; sized blocks well - to move content of the LC memory area
56 ; Set source start address
62 ; Set source last address
63 lda #<(__ZPSAVE_RUN__ + __INIT_SIZE__)
64 ldy #>(__ZPSAVE_RUN__ + __INIT_SIZE__)
68 ; Set destination last address
69 lda #<(__INIT_RUN__ + __INIT_SIZE__)
70 ldy #>(__INIT_RUN__ + __INIT_SIZE__)
74 ; Call into Applesoft Block Transfer Utility - which handles moving
75 ; overlapping blocks upwards well - to move the INIT segment
78 ; Delegate all further processing to keep the STARTUP segment small
81 ; Avoid re-entrance of donelib. This is also the _exit entry
84 jsr reset ; Setup RESET vector
86 ; Switch in ROM in case it wasn't already switched in by a RESET
89 ; Call module destructors
92 ; Check for valid interrupt vector table entry number
96 ; Deallocate interrupt vector table entry
97 dec i_param ; Adjust parameter count
98 jsr $BF00 ; MLI call entry point
99 .byte $41 ; Dealloc interrupt
102 ; Restore the original RESET vector
109 ; Copy back the zero page stuff
116 ; ProDOS TechRefMan, chapter 5.2.1:
117 ; "System programs should set the stack pointer to $FF at the
118 ; warm-start entry point."
120 txs ; Re-init stack pointer
125 ; ------------------------------------------------------------------------
129 ; Save the zero page locations we need
139 ; Save the original RESET vector
146 ; ProDOS TechRefMan, chapter 5.3.5:
147 ; "Your system program should place in the RESET vector the
148 ; address of a routine that ... closes the files."
151 jsr reset ; Setup RESET vector
154 ldy $BF00 ; MLI call entry point
155 cpy #$4C ; Is MLI present? (JMP opcode)
158 ; Check ProDOS system bit map
159 lda $BF6F ; Protection for pages $B8 - $BF
160 cmp #%00000001 ; Exactly system global page is protected
163 ; No BASIC.SYSTEM so quit to ProDOS dispatcher instead
169 ; No BASIC.SYSTEM so use addr of ProDOS system global page
172 bne :+ ; Branch always
174 ; Get highest available mem addr from BASIC interpreter
182 ; Check for interruptors
183 lda #<__INTERRUPTOR_COUNT__
187 cpy #$4C ; Is MLI present? (JMP opcode)
190 ; Allocate interrupt vector table entry
191 jsr $BF00 ; MLI call entry point
192 .byte $40 ; Alloc interrupt
196 ; Enable interrupts as old ProDOS versions (i.e. 1.1.1)
197 ; jump to SYS and BIN programs with interrupts disabled
200 ; Call module constructors
203 ; Switch in LC bank 2 for R/O
206 ; Push arguments and call main()
209 ; Print error message and return
210 prterr: ldx #msglen-1
217 errmsg: .ifdef __APPLE2ENH__
218 .byte $8D, 't'|$80, 'p'|$80, 'u'|$80, 'r'|$80, 'r'|$80
219 .byte 'e'|$80, 't'|$80, 'n'|$80, 'i'|$80, ' '|$80, 'c'|$80
220 .byte 'o'|$80, 'l'|$80, 'l'|$80, 'a'|$80, ' '|$80, 'o'|$80
221 .byte 't'|$80, ' '|$80, 'd'|$80, 'e'|$80, 'l'|$80, 'i'|$80
222 .byte 'a'|$80, 'F'|$80, $8D
224 .byte $8D, 'T'|$80, 'P'|$80, 'U'|$80, 'R'|$80, 'R'|$80
225 .byte 'E'|$80, 'T'|$80, 'N'|$80, 'I'|$80, ' '|$80, 'C'|$80
226 .byte 'O'|$80, 'L'|$80, 'L'|$80, 'A'|$80, ' '|$80, 'O'|$80
227 .byte 'T'|$80, ' '|$80, 'D'|$80, 'E'|$80, 'L'|$80, 'I'|$80
228 .byte 'A'|$80, 'F'|$80, $8D
233 ; ------------------------------------------------------------------------
237 ; ProDOS TechRefMan, chapter 6.2:
238 ; "Each installed routine must begin with a CLD instruction."
241 ; Call interruptors and check for success
245 ; ProDOS TechRefMan, chapter 6.2:
246 ; "When the routine that can process the interrupt is called, it
247 ; should ... return (via an RTS) with the carry flag clear."
251 ; ProDOS TechRefMan, chapter 6.2:
252 ; "When a routine that cannot process the interrupt is called,
253 ; it should return (via an RTS) with the cary flag set ..."
264 ; Quit to ProDOS dispatcher
265 quit: jsr $BF00 ; MLI call entry point
269 ; ------------------------------------------------------------------------
273 ; MLI parameter list for quit
274 q_param:.byte $04 ; param_count
275 .byte $00 ; quit_type
276 .word $0000 ; reserved
278 .word $0000 ; reserved
280 ; ------------------------------------------------------------------------
284 ; MLI parameter list for (de)alloc interrupt
285 i_param:.byte $02 ; param_count
286 int_num:.byte $00 ; int_num
287 .addr intptr ; int_code
289 ; Location to jump to when we're done
292 ; ------------------------------------------------------------------------
298 ; ------------------------------------------------------------------------