2 ; Startup code for cc65 (C16 version)
4 ; Note: The C16 is actually the Plus/4 with just 16KB of memory. So many
5 ; things are similar here, and we even use the plus4.inc include file.
9 .export __STARTUP__ : absolute = 1 ; Mark as startup
10 .import initlib, donelib, callirq
11 .import callmain, zerobss
12 .import MEMTOP, RESTOR, BSOUT, CLRCH
13 .import __INTERRUPTOR_COUNT__
15 .include "zeropage.inc"
16 .include "../plus4/plus4.inc"
19 ; ------------------------------------------------------------------------
20 ; Place the startup code in a special segment.
24 ; BASIC header with a SYS call
26 .word Head ; Load address
28 .word .version ; Line number
29 .byte $9E,"4109" ; SYS 4109
30 .byte $00 ; End of BASIC line
31 @Next: .word 0 ; BASIC end marker
33 ; ------------------------------------------------------------------------
38 sta zpsave,x ; save the zero page locations we need
46 ; Switch to second charset
55 ; Save system stuff and setup the stack
58 stx spsave ; save system stk ptr
61 jsr MEMTOP ; Get top memory
62 cpy #$80 ; We can only use the low 32K :-(
67 sty sp+1 ; set argument stack ptr
69 ; If we have IRQ functions, chain our stub into the IRQ vector
71 lda #<__INTERRUPTOR_COUNT__
84 ; Call module constructors
88 ; Push arguments and call main()
92 ; Call module destructors. This is also the _exit entry.
94 _exit: pha ; Save the return code on stack
95 jsr donelib ; Run module destructors
97 ; Reset the IRQ vector if we chained it.
99 pha ; Save the return code on stack
100 lda #<__INTERRUPTOR_COUNT__
109 ; Copy back the zero page stuff
111 NoIRQ2: ldx #zpspace-1
117 ; Store the return code into ST
122 ; Restore the stack pointer
127 ; Reset changed vectors
131 ; ------------------------------------------------------------------------
132 ; The IRQ vector jumps here, if condes routines are defined with type 2.
135 cld ; Just to be sure
136 jsr callirq ; Call the functions
137 jmp IRQInd ; Jump to the saved IRQ vector
139 ; ------------------------------------------------------------------------