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 ; Setup the IRQ vector in the banked RAM and switch off the ROM
60 sei ; No ints, handler not yet in place
63 sta $FFFE ; Install interrupt handler
66 cli ; Allow interrupts
72 ; Save system stuff and setup the stack. The stack starts at the top of the
76 stx spsave ; save system stk ptr
83 ; Call module constructors
87 ; If we have IRQ functions, chain our stub into the IRQ vector
89 lda #<__IRQFUNC_COUNT__
102 ; Push arguments and call main()
106 ; Back from main (this is also the _exit entry). Reset the IRQ vector if
109 _exit: lda #<__IRQFUNC_COUNT__
118 ; Run module destructors.
120 NoIRQ2: jsr donelib ; Run module destructors
122 ; Restore system stuff
127 ; Copy back the zero page stuff
135 ; Enable the ROM, reset changed vectors and return to BASIC
141 ; ------------------------------------------------------------------------
149 tsx ; Get the stack pointer
150 lda $0103,x ; Get the saved status register
152 and #$10 ; Test for BRK bit
154 lda #>irq_ret ; Push new return address
160 sta ENABLE_ROM ; Switch to ROM
161 jmp ($FFFE) ; Jump to kernal irq handler
164 sta ENABLE_RAM ; Switch back to RAM
171 lda brk_jmp+2 ; Check high byte of address
173 jmp brk_jmp ; Jump to the handler
175 ; No break handler installed, jump to ROM
179 pha ; ROM handler expects Y on stack
181 jmp (BRKVec) ; Jump indirect to the break vector
183 ; ------------------------------------------------------------------------
184 ; Stub for the IRQ chain. Is used only if there are IRQs defined. Needed in
185 ; low memory because of the banking.
190 cld ; Just to be sure
191 sta ENABLE_RAM ; Switch to RAM
192 ldy #<(__IRQFUNC_COUNT__*2)
193 lda #<__IRQFUNC_TABLE__
194 ldx #>__IRQFUNC_TABLE__
195 jsr condes ; Call the IRQ functions
197 jmp IRQInd ; Jump to the saved IRQ vector
199 ; ------------------------------------------------------------------------