2 ; Startup code for cc65 (C64 version)
6 .export __STARTUP__ : absolute = 1 ; Mark as startup
7 .import initlib, donelib, callirq
10 .import RESTOR, BSOUT, CLRCH
11 .import __INTERRUPTOR_COUNT__
12 .import __RAM_START__, __RAM_SIZE__ ; Linker generated
14 .include "zeropage.inc"
18 ; ------------------------------------------------------------------------
19 ; Place the startup code in a special segment.
23 ; BASIC header with a SYS call
25 .word Head ; Load address
27 .word .version ; Line number
28 .byte $9E,"2061" ; SYS 2061
29 .byte $00 ; End of BASIC line
30 @Next: .word 0 ; BASIC end marker
32 ; ------------------------------------------------------------------------
37 sta zpsave,x ; Save the zero page locations we need
45 ; Switch to second charset
50 ; Switch off the BASIC ROM
53 pha ; Remember the value
55 ora #$06 ; Enable kernal+I/O, disable basic
62 ; Save system settings and setup the stack
65 sta mmusave ; Save the memory configuration
68 stx spsave ; Save the system stack ptr
70 lda #<(__RAM_START__ + __RAM_SIZE__)
72 lda #>(__RAM_START__ + __RAM_SIZE__)
73 sta sp+1 ; Set argument stack ptr
75 ; If we have IRQ functions, chain our stub into the IRQ vector
77 lda #<__INTERRUPTOR_COUNT__
90 ; Call module constructors
94 ; Push arguments and call main
98 ; Back from main (This is also the _exit entry). Run module destructors
103 ; Reset the IRQ vector if we chained it.
105 pha ; Save the return code on stack
106 lda #<__INTERRUPTOR_COUNT__
115 ; Copy back the zero page stuff
117 NoIRQ2: ldx #zpspace-1
123 ; Place the program return code into ST
128 ; Restore system stuff
131 txs ; Restore stack pointer
133 stx $01 ; Restore memory configuration
135 ; Reset changed vectors, back to basic
139 ; ------------------------------------------------------------------------
140 ; The IRQ vector jumps here, if condes routines are defined with type 2.
143 cld ; Just to be sure
144 jsr callirq ; Call the functions
145 jmp IRQInd ; Jump to the saved IRQ vector
147 ; ------------------------------------------------------------------------