2 ; Startup code for cc65 (Plus/4 version)
4 ; This must be the *first* file on the linker command line
10 .import callirq_y, initlib, donelib
11 .import push0, callmain, zerobss
12 .import __INTERRUPTOR_COUNT__
14 .include "zeropage.inc"
18 ; ------------------------------------------------------------------------
21 IRQInd = $500 ; JMP $0000 - used as indirect IRQ vector
23 ; ------------------------------------------------------------------------
24 ; Place the startup code in a special segment to cope with the quirks of
29 .word Head ; Load address
31 .word .version ; Line number
32 .byte $9E,"4109" ; SYS 4109
33 .byte $00 ; End of BASIC line
34 @Next: .word 0 ; BASIC end marker
36 ; ------------------------------------------------------------------------
39 sei ; No interrupts since we're banking out the ROM
43 sta zpsave,x ; save the zero page locations we need
53 ; Switch to second charset
58 ; Save system stuff and setup the stack. The stack starts at the top of the
62 stx spsave ; save system stk ptr
69 ; Setup the IRQ vector in the banked RAM and switch off the ROM
73 sei ; No ints, handler not yet in place
75 stx $FFFE ; Install interrupt handler
77 cli ; Allow interrupts
83 ; Call module constructors
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 ; Push arguments and call main()
97 ; Back from main (this is also the _exit entry). Run module destructors.
99 _exit: pha ; Save the return code
101 sta irqcount ; Disable custom IRQ handlers
102 jsr donelib ; Run module destructors
104 ; Copy back the zero page stuff
112 ; Place the program return code into ST
117 ; Restore the stack pointer
122 ; Enable the ROM, reset changed vectors and return to BASIC
128 ; ------------------------------------------------------------------------
129 ; IRQ handler. The handler in the ROM enables the kernal and jumps to
130 ; $CE00, where the ROM code checks for a BRK or IRQ and branches via the
131 ; indirect vectors at $314/$316.
132 ; To make our stub as fast as possible, we skip the whole part of the ROM
133 ; handler and jump to the indirect vectors directly. We do also call our
134 ; own interrupt handlers if we have any, so they need not use $314.
138 IRQ: cld ; Just to be sure
144 tsx ; Get the stack pointer
145 lda $0104,x ; Get the saved status register
146 and #$10 ; Test for BRK bit
149 ; It's an IRQ and RAM is enabled. If we have handlers, call them. We will use
150 ; a flag here instead of loading __INTERRUPTOR_COUNT__ directly, since the
151 ; condes function is not reentrant. The irqcount flag will be set/reset from
152 ; the main code, to avoid races.
156 jsr callirq_y ; Call the IRQ functions
158 ; Since the ROM handler will end with an RTI, we have to fake an IRQ return
159 ; on stack, so we get control of the CPU after the ROM handler and can switch
162 @L1: lda #>irq_ret ; Push new return address
166 php ; Push faked IRQ frame on stack
167 pha ; Push faked A register
168 pha ; Push faked X register
169 pha ; Push faked Y register
170 sta ENABLE_ROM ; Switch to ROM
171 jmp (IRQVec) ; Jump indirect to kernal irq handler
174 sta ENABLE_RAM ; Switch back to RAM
183 lda brk_jmp+2 ; Check high byte of address
185 jmp brk_jmp ; Jump to the handler
187 ; No break handler installed, jump to ROM
191 jmp (BRKVec) ; Jump indirect to the break vector
193 ; ------------------------------------------------------------------------