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 __RAM_START__, __RAM_SIZE__ ; Linker generated
13 .import __STACKSIZE__ ; Linker generated
15 .include "zeropage.inc"
19 ; ------------------------------------------------------------------------
22 IRQInd = $500 ; JMP $0000 - used as indirect IRQ vector
24 ; ------------------------------------------------------------------------
25 ; BASIC header with a SYS call
29 .word Head ; Load address
31 .word .version ; Line number
33 .byte <(((Start / 1000) .mod 10) + '0')
34 .byte <(((Start / 100) .mod 10) + '0')
35 .byte <(((Start / 10) .mod 10) + '0')
36 .byte <(((Start / 1) .mod 10) + '0')
37 .byte $00 ; End of BASIC line
38 @Next: .word 0 ; BASIC end marker
40 ; ------------------------------------------------------------------------
47 ; Save the zero page locations we need
49 sei ; No interrupts since we're banking out the ROM
59 ; Switch to second charset
64 ; Save system stuff and setup the stack. The stack starts at the top of the
68 stx spsave ; save system stk ptr
70 lda #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
72 lda #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
75 ; Setup the IRQ vector in the banked RAM and switch off the ROM
79 sei ; No ints, handler not yet in place
81 stx $FFFE ; Install interrupt handler
83 cli ; Allow interrupts
89 ; Initialize irqcount, which means that from now own custom linked in IRQ
90 ; handlers (via condes) will be called.
92 lda #.lobyte(__INTERRUPTOR_COUNT__*2)
95 ; Call module constructors
99 ; Push arguments and call main()
103 ; Back from main (this is also the _exit entry). Run module destructors.
105 _exit: pha ; Save the return code
106 jsr donelib ; Run module destructors
108 ; Disable chained IRQ handlers
111 sta irqcount ; Disable custom IRQ handlers
113 ; Copy back the zero page stuff
121 ; Place the program return code into ST
126 ; Restore the stack pointer
131 ; Enable the ROM, reset changed vectors and return to BASIC
137 ; ------------------------------------------------------------------------
138 ; IRQ handler. The handler in the ROM enables the kernal and jumps to
139 ; $CE00, where the ROM code checks for a BRK or IRQ and branches via the
140 ; indirect vectors at $314/$316.
141 ; To make our stub as fast as possible, we skip the whole part of the ROM
142 ; handler and jump to the indirect vectors directly. We do also call our
143 ; own interrupt handlers if we have any, so they need not use $314.
147 IRQ: cld ; Just to be sure
153 tsx ; Get the stack pointer
154 lda $0104,x ; Get the saved status register
155 and #$10 ; Test for BRK bit
158 ; It's an IRQ and RAM is enabled. If we have handlers, call them. We will use
159 ; a flag here instead of loading __INTERRUPTOR_COUNT__ directly, since the
160 ; condes function is not reentrant. The irqcount flag will be set/reset from
161 ; the main code, to avoid races.
165 jsr callirq_y ; Call the IRQ functions
167 ; Since the ROM handler will end with an RTI, we have to fake an IRQ return
168 ; on stack, so we get control of the CPU after the ROM handler and can switch
171 @L1: lda #>irq_ret ; Push new return address
175 php ; Push faked IRQ frame on stack
176 pha ; Push faked A register
177 pha ; Push faked X register
178 pha ; Push faked Y register
179 sta ENABLE_ROM ; Switch to ROM
180 jmp (IRQVec) ; Jump indirect to kernal irq handler
183 sta ENABLE_RAM ; Switch back to RAM
192 lda brk_jmp+2 ; Check high byte of address
194 jmp brk_jmp ; Jump to the handler
196 ; No break handler installed, jump to ROM
200 jmp (BRKVec) ; Jump indirect to the break vector
202 ; ------------------------------------------------------------------------