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"
19 ; ------------------------------------------------------------------------
20 ; BASIC header with a SYS call
24 .word Head ; Load address
26 .word .version ; Line number
28 .byte <(((Start / 1000) .mod 10) + '0')
29 .byte <(((Start / 100) .mod 10) + '0')
30 .byte <(((Start / 10) .mod 10) + '0')
31 .byte <(((Start / 1) .mod 10) + '0')
32 .byte $00 ; End of BASIC line
33 @Next: .word 0 ; BASIC end marker
35 ; ------------------------------------------------------------------------
42 ; Save the zero page locations we need
50 ; Switch to second charset
59 ; Save system stuff and setup the stack
62 stx spsave ; save system stk ptr
65 jsr MEMTOP ; Get top memory
66 cpy #$80 ; We can only use the low 32K :-(
71 sty sp+1 ; set argument stack ptr
73 ; If we have IRQ functions, chain our stub into the IRQ vector
75 lda #<__INTERRUPTOR_COUNT__
88 ; Call module constructors
92 ; Push arguments and call main()
96 ; Call module destructors. This is also the _exit entry.
98 _exit: pha ; Save the return code on stack
99 jsr donelib ; Run module destructors
101 ; Reset the IRQ vector if we chained it.
103 pha ; Save the return code on stack
104 lda #<__INTERRUPTOR_COUNT__
113 ; Copy back the zero page stuff
115 NoIRQ2: ldx #zpspace-1
121 ; Store the return code into ST
126 ; Restore the stack pointer
131 ; Reset changed vectors
135 ; ------------------------------------------------------------------------
136 ; The IRQ vector jumps here, if condes routines are defined with type 2.
139 cld ; Just to be sure
140 jsr callirq ; Call the functions
141 jmp IRQInd ; Jump to the saved IRQ vector
143 ; ------------------------------------------------------------------------