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 1000 ; 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.
100 sta irqcount ; Disable custom IRQ handlers
101 jsr donelib ; Run module destructors
103 ; Restore system stuff
108 ; Copy back the zero page stuff
116 ; Enable the ROM, reset changed vectors and return to BASIC
122 ; ------------------------------------------------------------------------
123 ; IRQ handler. The handler in the ROM enables the kernal and jumps to
124 ; $CE00, where the ROM code checks for a BRK or IRQ and branches via the
125 ; indirect vectors at $314/$316.
126 ; To make our stub as fast as possible, we skip the whole part of the ROM
127 ; handler and jump to the indirect vectors directly. We do also call our
128 ; own interrupt handlers if we have any, so they need not use $314.
132 IRQ: cld ; Just to be sure
138 tsx ; Get the stack pointer
139 lda $0104,x ; Get the saved status register
140 and #$10 ; Test for BRK bit
143 ; It's an IRQ and RAM is enabled. If we have handlers, call them. We will use
144 ; a flag here instead of loading __IRQFUNC_COUNT__ directly, since the condes
145 ; function is not reentrant. The irqcount flag will be set/reset from the main
146 ; code, to avoid races.
150 lda #<__IRQFUNC_TABLE__
151 ldx #>__IRQFUNC_TABLE__
152 jsr condes ; Call the IRQ functions
154 ; Since the ROM handler will end with an RTI, we have to fake an IRQ return
155 ; on stack, so we get control of the CPU after the ROM handler and can switch
158 @L1: lda #>irq_ret ; Push new return address
162 php ; Push faked IRQ frame on stack
163 pha ; Push faked A register
164 pha ; Push faked X register
165 pha ; Push faked Y register
166 sta ENABLE_ROM ; Switch to ROM
167 jmp (IRQVec) ; Jump indirect to kernal irq handler
170 sta ENABLE_RAM ; Switch back to RAM
179 lda brk_jmp+2 ; Check high byte of address
181 jmp brk_jmp ; Jump to the handler
183 ; No break handler installed, jump to ROM
187 jmp (BRKVec) ; Jump indirect to the break vector
189 ; ------------------------------------------------------------------------