2 ; Startup code for cc65 (C64 version)
4 ; This must be the *first* file on the linker command line
8 .import initlib, donelib, callirq
11 .import RESTOR, BSOUT, CLRCH
12 .import __IRQFUNC_COUNT__
13 .import __RAM_START__, __RAM_SIZE__ ; Linker generated
15 .include "zeropage.inc"
19 ; ------------------------------------------------------------------------
20 ; Place the startup code in a special segment.
24 ; BASIC header with a SYS call
26 .word Head ; Load address
28 .word .version ; Line number
29 .byte $9E,"2061" ; SYS 2061
30 .byte $00 ; End of BASIC line
31 @Next: .word 0 ; BASIC end marker
33 ; ------------------------------------------------------------------------
38 sta zpsave,x ; Save the zero page locations we need
46 ; Switch to second charset
51 ; Switch off the BASIC ROM
54 pha ; Remember the value
56 ora #$06 ; Enable kernal+I/O, disable basic
63 ; Save system settings and setup the stack
66 sta mmusave ; Save the memory configuration
69 stx spsave ; Save the system stack ptr
71 lda #<(__RAM_START__ + __RAM_SIZE__)
73 lda #>(__RAM_START__ + __RAM_SIZE__)
74 sta sp+1 ; Set argument stack ptr
76 ; Call module constructors
80 ; If we have IRQ functions, chain our stub into the IRQ vector
82 lda #<__IRQFUNC_COUNT__
95 ; Push arguments and call main
99 ; Back from main (This is also the _exit entry). Reset the IRQ vector if we
102 _exit: pha ; Save the return code on stack
103 lda #<__IRQFUNC_COUNT__
112 ; Run module destructors
116 ; Copy back the zero page stuff
124 ; Place the program return code into ST
129 ; Restore system stuff
132 txs ; Restore stack pointer
134 stx $01 ; Restore memory configuration
136 ; Reset changed vectors, back to basic
140 ; ------------------------------------------------------------------------
141 ; The IRQ vector jumps here, if condes routines are defined with type 2.
144 cld ; Just to be sure
145 jsr callirq ; Call the functions
146 jmp IRQInd ; Jump to the saved IRQ vector
148 ; ------------------------------------------------------------------------