2 ; Startup code for cc65 (Plus/4 version)
4 ; This must be the *first* file on the linker command line
10 .import condes, initlib, donelib
11 .import push0, callmain, zerobss
12 .import __IRQFUNC_TABLE__, __IRQFUNC_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(__IRQFUNC_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 ; Restore system stuff
109 ; Copy back the zero page stuff
117 ; Place the program return code into ST
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 __IRQFUNC_COUNT__ directly, since the condes
151 ; function is not reentrant. The irqcount flag will be set/reset from the main
152 ; code, to avoid races.
156 lda #<__IRQFUNC_TABLE__
157 ldx #>__IRQFUNC_TABLE__
158 jsr condes ; 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 ; ------------------------------------------------------------------------