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__
12 .import __MAIN_START__, __MAIN_SIZE__ ; Linker generated
13 .import __STACKSIZE__ ; Linker generated
16 .include "zeropage.inc"
20 ; ------------------------------------------------------------------------
23 IRQInd = $500 ; JMP $0000 - used as indirect IRQ vector
25 ; ------------------------------------------------------------------------
32 ; Save the zero-page locations that we need.
34 sei ; No interrupts since we're banking out the ROM
44 ; Switch to the second charset.
49 ; Save some system stuff; and, set up the stack. The stack starts at the top
53 stx spsave ; Save system stk ptr
55 lda #<(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
56 ldx #>(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
60 ; Set up the IRQ vector in the banked RAM; and, switch off the ROM.
64 sei ; No ints, handler not yet in place
66 stx $FFFE ; Install interrupt handler
68 cli ; Allow interrupts
74 ; Initialize irqcount, which means that, from now on, custom linked-in IRQ
75 ; handlers will be called (via condes).
77 lda #.lobyte(__INTERRUPTOR_COUNT__*2)
80 ; Call the module constructors.
84 ; Push the command-line arguments; and, call main().
88 ; Back from main() [this is also the exit() entry]. Run the module destructors.
90 _exit: pha ; Save the return code
91 jsr donelib ; Run module destructors
93 ; Disable the chained IRQ handlers.
96 sta irqcount ; Disable custom IRQ handlers
98 ; Copy back the zero-page stuff.
106 ; Place the program return code into BASIC's status variable.
111 ; Restore the stack pointer.
116 ; Enable the ROM; and, return to BASIC.
121 ; ------------------------------------------------------------------------
122 ; IRQ handler. The handler in the ROM enables the Kernal, and jumps to
123 ; $CE00, where the ROM code checks for a BRK or IRQ, and branches via the
124 ; indirect vectors at $314/$316.
125 ; To make our stub as fast as possible, we skip the whole part of the ROM
126 ; handler, and jump to the indirect vectors directly. We do also call our
127 ; own interrupt handlers if we have any; so, they need not use $314.
131 IRQ: cld ; Just to be sure
137 tsx ; Get the stack pointer
138 lda $0104,x ; Get the saved status register
139 and #$10 ; Test for BRK bit
142 ; It's an IRQ; and, RAM is enabled. If we have handlers, call them. We will use
143 ; a flag here instead of loading __INTERRUPTOR_COUNT__ directly, since the
144 ; condes function is not reentrant. The irqcount flag will be set/reset from
145 ; the main code, to avoid races.
149 jsr callirq_y ; Call the IRQ functions
151 ; Since the ROM handler will end with an RTI, we have to fake an IRQ return
152 ; on the stack, so that we get control of the CPU after the ROM handler,
153 ; and can switch back to RAM.
155 @L1: lda #>irq_ret ; Push new return address
159 php ; Push faked IRQ frame on stack
160 pha ; Push faked A register
161 pha ; Push faked X register
162 pha ; Push faked Y register
163 sta ENABLE_ROM ; Switch to ROM
164 jmp (IRQVec) ; Jump indirect to Kernal IRQ handler
167 sta ENABLE_RAM ; Switch back to RAM
176 lda brk_jmp+2 ; Check high byte of address
178 jmp brk_jmp ; Jump to the handler
180 ; No break handler installed, jump to ROM.
184 jmp (BRKVec) ; Jump indirect to the break vector
186 ; ------------------------------------------------------------------------