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 ; Place the startup code in a special segment to cope with the quirks of
28 .word Head ; Load address
30 .word .version ; Line number
31 .byte $9E,"4109" ; SYS 4109
32 .byte $00 ; End of BASIC line
33 @Next: .word 0 ; BASIC end marker
35 ; ------------------------------------------------------------------------
38 sei ; No interrupts since we're banking out the ROM
42 sta zpsave,x ; save the zero page locations we need
52 ; Switch to second charset
57 ; Save system stuff and setup the stack. The stack starts at the top of the
61 stx spsave ; save system stk ptr
68 ; Setup the IRQ vector in the banked RAM and switch off the ROM
72 sei ; No ints, handler not yet in place
74 stx $FFFE ; Install interrupt handler
76 cli ; Allow interrupts
82 ; Initialize irqcount, which means that from now own custom linked in IRQ
83 ; handlers (via condes) will be called.
85 lda #.lobyte(__INTERRUPTOR_COUNT__*2)
88 ; Call module constructors
92 ; Push arguments and call main()
96 ; Back from main (this is also the _exit entry). Run module destructors.
98 _exit: pha ; Save the return code
99 jsr donelib ; Run module destructors
101 ; Disable chained IRQ handlers
104 sta irqcount ; Disable custom IRQ handlers
106 ; Copy back the zero page stuff
114 ; Place the program return code into ST
119 ; Restore the stack pointer
124 ; Enable the ROM, reset changed vectors and return to BASIC
130 ; ------------------------------------------------------------------------
131 ; IRQ handler. The handler in the ROM enables the kernal and jumps to
132 ; $CE00, where the ROM code checks for a BRK or IRQ and branches via the
133 ; indirect vectors at $314/$316.
134 ; To make our stub as fast as possible, we skip the whole part of the ROM
135 ; handler and jump to the indirect vectors directly. We do also call our
136 ; own interrupt handlers if we have any, so they need not use $314.
140 IRQ: cld ; Just to be sure
146 tsx ; Get the stack pointer
147 lda $0104,x ; Get the saved status register
148 and #$10 ; Test for BRK bit
151 ; It's an IRQ and RAM is enabled. If we have handlers, call them. We will use
152 ; a flag here instead of loading __INTERRUPTOR_COUNT__ directly, since the
153 ; condes function is not reentrant. The irqcount flag will be set/reset from
154 ; the main code, to avoid races.
158 jsr callirq_y ; Call the IRQ functions
160 ; Since the ROM handler will end with an RTI, we have to fake an IRQ return
161 ; on stack, so we get control of the CPU after the ROM handler and can switch
164 @L1: lda #>irq_ret ; Push new return address
168 php ; Push faked IRQ frame on stack
169 pha ; Push faked A register
170 pha ; Push faked X register
171 pha ; Push faked Y register
172 sta ENABLE_ROM ; Switch to ROM
173 jmp (IRQVec) ; Jump indirect to kernal irq handler
176 sta ENABLE_RAM ; Switch back to RAM
185 lda brk_jmp+2 ; Check high byte of address
187 jmp brk_jmp ; Jump to the handler
189 ; No break handler installed, jump to ROM
193 jmp (BRKVec) ; Jump indirect to the break vector
195 ; ------------------------------------------------------------------------