2 ; Startup code for cc65 (Plus/4 version)
7 .export __STARTUP__ : absolute = 1 ; Mark as startup
9 .import callirq_y, initlib, donelib
10 .import callmain, zerobss
11 .import __INTERRUPTOR_COUNT__
13 .include "zeropage.inc"
17 ; ------------------------------------------------------------------------
20 IRQInd = $500 ; JMP $0000 - used as indirect IRQ vector
22 ; ------------------------------------------------------------------------
23 ; BASIC header with a SYS call
27 .word Head ; Load address
29 .word .version ; Line number
31 .byte <(((Start / 1000) .mod 10) + '0')
32 .byte <(((Start / 100) .mod 10) + '0')
33 .byte <(((Start / 10) .mod 10) + '0')
34 .byte <(((Start / 1) .mod 10) + '0')
35 .byte $00 ; End of BASIC line
36 @Next: .word 0 ; BASIC end marker
38 ; ------------------------------------------------------------------------
45 ; Save the zero page locations we need
47 sei ; No interrupts since we're banking out the ROM
57 ; Switch to second charset
62 ; Save system stuff and setup the stack. The stack starts at the top of the
66 stx spsave ; save system stk ptr
73 ; Setup the IRQ vector in the banked RAM and switch off the ROM
77 sei ; No ints, handler not yet in place
79 stx $FFFE ; Install interrupt handler
81 cli ; Allow interrupts
87 ; Initialize irqcount, which means that from now own custom linked in IRQ
88 ; handlers (via condes) will be called.
90 lda #.lobyte(__INTERRUPTOR_COUNT__*2)
93 ; Call module constructors
97 ; Push arguments and call main()
101 ; Back from main (this is also the _exit entry). Run module destructors.
103 _exit: pha ; Save the return code
104 jsr donelib ; Run module destructors
106 ; Disable chained IRQ handlers
109 sta irqcount ; Disable custom IRQ handlers
111 ; Copy back the zero page stuff
119 ; Place the program return code into ST
124 ; Restore the stack pointer
129 ; Enable the ROM, reset changed vectors and return to BASIC
135 ; ------------------------------------------------------------------------
136 ; IRQ handler. The handler in the ROM enables the kernal and jumps to
137 ; $CE00, where the ROM code checks for a BRK or IRQ and branches via the
138 ; indirect vectors at $314/$316.
139 ; To make our stub as fast as possible, we skip the whole part of the ROM
140 ; handler and jump to the indirect vectors directly. We do also call our
141 ; own interrupt handlers if we have any, so they need not use $314.
145 IRQ: cld ; Just to be sure
151 tsx ; Get the stack pointer
152 lda $0104,x ; Get the saved status register
153 and #$10 ; Test for BRK bit
156 ; It's an IRQ and RAM is enabled. If we have handlers, call them. We will use
157 ; a flag here instead of loading __INTERRUPTOR_COUNT__ directly, since the
158 ; condes function is not reentrant. The irqcount flag will be set/reset from
159 ; the main code, to avoid races.
163 jsr callirq_y ; Call the IRQ functions
165 ; Since the ROM handler will end with an RTI, we have to fake an IRQ return
166 ; on stack, so we get control of the CPU after the ROM handler and can switch
169 @L1: lda #>irq_ret ; Push new return address
173 php ; Push faked IRQ frame on stack
174 pha ; Push faked A register
175 pha ; Push faked X register
176 pha ; Push faked Y register
177 sta ENABLE_ROM ; Switch to ROM
178 jmp (IRQVec) ; Jump indirect to kernal irq handler
181 sta ENABLE_RAM ; Switch back to RAM
190 lda brk_jmp+2 ; Check high byte of address
192 jmp brk_jmp ; Jump to the handler
194 ; No break handler installed, jump to ROM
198 jmp (BRKVec) ; Jump indirect to the break vector
200 ; ------------------------------------------------------------------------