2 ; Startup code for cc65 (C128 version)
4 ; This must be the *first* file on the linker command line
8 .import callirq, initlib, donelib
10 .import push0, callmain
11 .import RESTOR, BSOUT, CLRCH
12 .import __INTERRUPTOR_COUNT__
13 .import __RAM_START__, __RAM_SIZE__
15 .include "zeropage.inc"
19 ; ------------------------------------------------------------------------
22 IRQInd = $2FD ; JMP $0000 - used as indirect IRQ vector
24 ; ------------------------------------------------------------------------
25 ; Place the startup code in a special segment to cope with the quirks of
30 ; BASIC header with a SYS call
33 .word Head ; Load address
35 .word .version ; Line number
36 .byte $9E,"7181" ; SYS 7181
37 .byte $00 ; End of BASIC line
38 @Next: .word 0 ; BASIC end marker
41 ; ------------------------------------------------------------------------
48 ; Switch to the second charset
53 ; Before doing anything else, we have to setup our banking configuration.
54 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
55 ; to the underlying RAM works, but it is bad style.
57 lda MMU_CR ; Get current memory configuration...
58 pha ; ...and save it for later
59 lda #MMU_CFG_CC65 ; Bank0 with kernal ROM
62 ; Save the zero page locations we need
74 ; Save system stuff and setup the stack
80 stx spsave ; Save the system stack pointer
82 lda #<(__RAM_START__ + __RAM_SIZE__)
84 lda #>(__RAM_START__ + __RAM_SIZE__)
85 sta sp+1 ; Set argument stack ptr
87 ; Call module constructors
91 ; Set the bank for the file name to our execution bank. We must do this,
92 ; *after* calling constructors, because some of them may depend on the
93 ; original value of this register.
98 ; If we have IRQ functions, chain our stub into the IRQ vector
100 lda #<__INTERRUPTOR_COUNT__
113 ; Push arguments and call main()
117 ; Back from main (this is also the _exit entry). Reset the IRQ vector if we
120 _exit: pha ; Save the return code on stack
121 lda #<__INTERRUPTOR_COUNT__
130 ; Run module destructors
134 ; Copy back the zero page stuff
142 ; Place the program return code into ST
147 ; Reset the stack and the memory configuration
154 ; Done, restore kernal vectors in an attempt to cleanup
158 ; ------------------------------------------------------------------------
159 ; The C128 has ROM parallel to the RAM starting from $4000. The startup code
160 ; above will change this setting so that we have RAM from $0000-$BFFF. This
161 ; works quite well with the exception of interrupts: The interrupt handler
162 ; is in ROM, and the ROM switches back to the ROM configuration, which means
163 ; that parts of our program may not be accessible. To solve this, we place
164 ; the following code into a special segment called "LOWCODE" which will be
165 ; placed just above the startup code, so it goes into a RAM area that is
171 cld ; Just to be sure
172 lda MMU_CR ; Get old register value
173 pha ; And save on stack
174 lda #MMU_CFG_CC65 ; Bank 0 with kernal ROM
176 jsr callirq ; Call the functions
177 pla ; Get old register value
179 jmp IRQInd ; Jump to the saved IRQ vector
182 ; ------------------------------------------------------------------------