2 ; Startup code for cc65 (C64 version)
4 ; This must be the *first* file on the linker command line
8 .import initlib, donelib
11 .import __RAM_START__, __RAM_SIZE__ ; Linker generated
14 .include "../cbm/cbm.inc"
16 ; ------------------------------------------------------------------------
17 ; Define and export the ZP variables for the C64 runtime
19 .exportzp sp, sreg, regsave
20 .exportzp ptr1, ptr2, ptr3, ptr4
21 .exportzp tmp1, tmp2, tmp3, tmp4
22 .exportzp regbank, zpspace
27 sp: .res 2 ; Stack pointer
28 sreg: .res 2 ; Secondary register/high 16 bit for longs
29 regsave: .res 2 ; slot to save/restore (E)AX into
38 regbank: .res 6 ; 6 byte register bank
40 zpspace = * - zpstart ; Zero page space allocated
44 ; ------------------------------------------------------------------------
45 ; BASIC header with a SYS call
48 .word Head ; Load address
50 .word 1000 ; Line number
51 .byte $9E,"2061" ; SYS 2061
52 .byte $00 ; End of BASIC line
53 @Next: .word 0 ; BASIC end marker
56 ; ------------------------------------------------------------------------
61 sta zpsave,x ; Save the zero page locations we need
69 ; Switch to second charset
78 ; Save system stuff and setup the stack
81 stx spsave ; Save the system stack ptr
84 sta mmusave ; Save the memory configuration
86 ora #$06 ; Enable kernal+I/O, disable basic
89 lda #<(__RAM_START__ + __RAM_SIZE__)
91 lda #>(__RAM_START__ + __RAM_SIZE__)
92 sta sp+1 ; Set argument stack ptr
94 ; Call module constructors
98 ; Pass an empty command line
103 ldy #4 ; Argument size
104 jsr _main ; call the users code
106 ; Call module destructors. This is also the _exit entry.
108 _exit: jsr donelib ; Run module destructors
110 ; Restore system stuff
113 txs ; Restore stack pointer
115 sta $01 ; Restore memory configuration
117 ; Copy back the zero page stuff
125 ; Reset changed vectors, back to basic