2 ; Startup code for cc65 (Vic20 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"
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
29 .byte <(((@Start / 1000) .mod 10) + $30)
30 .byte <(((@Start / 100) .mod 10) + $30)
31 .byte <(((@Start / 10) .mod 10) + $30)
32 .byte <(((@Start / 1) .mod 10) + $30)
33 .byte $00 ; End of BASIC line
34 @Next: .word 0 ; BASIC end marker
37 ; ------------------------------------------------------------------------
42 sta zpsave,x ; Save the zero page locations we need
50 ; Switch to second charset
59 ; Save system stuff and setup the stack
62 stx spsave ; Save the system stack ptr
64 lda #<(__RAM_START__ + __RAM_SIZE__)
66 lda #>(__RAM_START__ + __RAM_SIZE__)
67 sta sp+1 ; Set argument stack ptr
69 ; Call module constructors
73 ; If we have IRQ functions, chain our stub into the IRQ vector
75 lda #<__IRQFUNC_COUNT__
88 ; Push arguments and call main()
92 ; Back from main (This is also the _exit entry). Reset the IRQ vector if we
95 _exit: pha ; Save the return code on stack
96 lda #<__IRQFUNC_COUNT__
105 ; Run module destructors
109 ; Copy back the zero page stuff
117 ; Place the program return code into ST
122 ; Restore the stack pointer
127 ; Reset changed vectors, back to basic
132 ; ------------------------------------------------------------------------
133 ; The IRQ vector jumps here, if condes routines are defined with type 2.
136 cld ; Just to be sure
137 jsr callirq ; Call the functions
138 jmp IRQInd ; Jump to the saved IRQ vector
140 ; ------------------------------------------------------------------------