2 ; Oliver Schmidt, 15.09.2009
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 __RAM_START__ , __RAM_LAST__ ; Linker generated
13 .import __MOVE_START__, __MOVE_LAST__ ; Linker generated
14 .import __LC_START__ , __LC_LAST__ ; Linker generated
15 .import __ZPSAVE_RUN__, __INIT_SIZE__ ; Linker generated
16 .import __INTERRUPTOR_COUNT__ ; Linker generated
18 .include "zeropage.inc"
23 ; ------------------------------------------------------------------------
27 .addr __RAM_START__ ; Start address
28 .word __ZPSAVE_RUN__ - __RAM_START__ + \
29 __MOVE_LAST__ - __MOVE_START__ ; Size
31 ; ------------------------------------------------------------------------
35 ; ProDOS TechRefMan, chapter 5.2.1:
36 ; "For maximum interrupt efficiency, a system program should not
37 ; use more than the upper 3/4 of the stack."
39 txs ; Init stack pointer
41 ; Switch in LC bank 2 for W/O
45 ; Set source start address
46 lda #<(__ZPSAVE_RUN__ + __INIT_SIZE__)
47 ldy #>(__ZPSAVE_RUN__ + __INIT_SIZE__)
51 ; Set source last address
52 lda #<(__ZPSAVE_RUN__ + __INIT_SIZE__ + __LC_LAST__ - __LC_START__)
53 ldy #>(__ZPSAVE_RUN__ + __INIT_SIZE__ + __LC_LAST__ - __LC_START__)
57 ; Set destination last address
63 ; Call into Applesoft Block Transfer Utility - which handles zero
64 ; sized blocks well - to move content of the LC memory area
67 ; Set source start address
73 ; Set source last address
74 lda #<(__ZPSAVE_RUN__ + __INIT_SIZE__)
75 ldy #>(__ZPSAVE_RUN__ + __INIT_SIZE__)
79 ; Set destination last address
85 ; Call into Applesoft Block Transfer Utility - which handles moving
86 ; overlapping blocks upwards well - to move the INIT segment
89 ; Delegate all further processing to keep the STARTUP segment small
92 ; Avoid re-entrance of donelib. This is also the _exit entry
95 jsr reset ; Setup RESET vector
97 ; Switch in ROM in case it wasn't already switched in by a RESET
100 ; Call module destructors
103 ; Check for valid interrupt vector table entry number
107 ; Deallocate interrupt vector table entry
108 dec i_param ; Adjust parameter count
109 jsr $BF00 ; MLI call entry point
110 .byte $41 ; Dealloc interrupt
113 ; Restore the original RESET vector
120 ; Copy back the zero page stuff
127 ; ProDOS TechRefMan, chapter 5.2.1:
128 ; "System programs should set the stack pointer to $FF at the
129 ; warm-start entry point."
131 txs ; Re-init stack pointer
136 ; ------------------------------------------------------------------------
140 ; Save the zero page locations we need
150 ; Save the original RESET vector
157 ; ProDOS TechRefMan, chapter 5.3.5:
158 ; "Your system program should place in the RESET vector the
159 ; address of a routine that ... closes the files."
162 jsr reset ; Setup RESET vector
165 ldy $BF00 ; MLI call entry point
166 cpy #$4C ; Is MLI present? (JMP opcode)
169 ; Check ProDOS system bit map
170 lda $BF6F ; Protection for pages $B8 - $BF
171 cmp #%00000001 ; Exactly system global page is protected
174 ; No BASIC.SYSTEM so quit to ProDOS dispatcher instead
180 ; No BASIC.SYSTEM so use addr of ProDOS system global page
183 bne :+ ; Branch always
185 ; Get highest available mem addr from BASIC interpreter
193 ; Check for interruptors
194 lda #<__INTERRUPTOR_COUNT__
198 cpy #$4C ; Is MLI present? (JMP opcode)
201 ; Allocate interrupt vector table entry
202 jsr $BF00 ; MLI call entry point
203 .byte $40 ; Alloc interrupt
207 ; Enable interrupts as old ProDOS versions (i.e. 1.1.1)
208 ; jump to SYS and BIN programs with interrupts disabled
211 ; Call module constructors
214 ; Switch in LC bank 2 for R/O
217 ; Push arguments and call main()
220 ; Print error message and return
221 prterr: ldx #msglen-1
228 errmsg: .ifdef __APPLE2ENH__
229 .byte $8D, 't'|$80, 'p'|$80, 'u'|$80, 'r'|$80, 'r'|$80
230 .byte 'e'|$80, 't'|$80, 'n'|$80, 'i'|$80, ' '|$80, 'c'|$80
231 .byte 'o'|$80, 'l'|$80, 'l'|$80, 'a'|$80, ' '|$80, 'o'|$80
232 .byte 't'|$80, ' '|$80, 'd'|$80, 'e'|$80, 'l'|$80, 'i'|$80
233 .byte 'a'|$80, 'F'|$80, $8D
235 .byte $8D, 'T'|$80, 'P'|$80, 'U'|$80, 'R'|$80, 'R'|$80
236 .byte 'E'|$80, 'T'|$80, 'N'|$80, 'I'|$80, ' '|$80, 'C'|$80
237 .byte 'O'|$80, 'L'|$80, 'L'|$80, 'A'|$80, ' '|$80, 'O'|$80
238 .byte 'T'|$80, ' '|$80, 'D'|$80, 'E'|$80, 'L'|$80, 'I'|$80
239 .byte 'A'|$80, 'F'|$80, $8D
244 ; ------------------------------------------------------------------------
248 ; ProDOS TechRefMan, chapter 6.2:
249 ; "Each installed routine must begin with a CLD instruction."
252 ; Call interruptors and check for success
256 ; ProDOS TechRefMan, chapter 6.2:
257 ; "When the routine that can process the interrupt is called, it
258 ; should ... return (via an RTS) with the carry flag clear."
262 ; ProDOS TechRefMan, chapter 6.2:
263 ; "When a routine that cannot process the interrupt is called,
264 ; it should return (via an RTS) with the cary flag set ..."
275 ; Quit to ProDOS dispatcher
276 quit: jsr $BF00 ; MLI call entry point
280 ; ------------------------------------------------------------------------
284 ; MLI parameter list for quit
285 q_param:.byte $04 ; param_count
286 .byte $00 ; quit_type
287 .word $0000 ; reserved
289 .word $0000 ; reserved
291 ; ------------------------------------------------------------------------
295 ; MLI parameter list for (de)alloc interrupt
296 i_param:.byte $02 ; param_count
297 int_num:.byte $00 ; int_num
298 .addr intptr ; int_code
300 ; Location to jump to when we're done
303 ; ------------------------------------------------------------------------
309 ; ------------------------------------------------------------------------