2 ; Startup code for cc65 (C128 version)
6 .export __STARTUP__ : absolute = 1 ; Mark as startup
7 .import callirq, initlib, donelib
9 .import push0, callmain
10 .import RESTOR, BSOUT, CLRCH
11 .import __INTERRUPTOR_COUNT__
12 .import __RAM_START__, __RAM_SIZE__
14 .include "zeropage.inc"
18 ; ------------------------------------------------------------------------
21 IRQInd = $2FD ; JMP $0000 - used as indirect IRQ vector
23 ; ------------------------------------------------------------------------
24 ; Place the startup code in a special segment to cope with the quirks of
29 ; BASIC header with a SYS call
32 .word Head ; Load address
34 .word .version ; Line number
36 .byte <(((Start / 1000) .mod 10) + $30)
37 .byte <(((Start / 100) .mod 10) + $30)
38 .byte <(((Start / 10) .mod 10) + $30)
39 .byte <(((Start / 1) .mod 10) + $30)
40 .byte $00 ; End of BASIC line
41 @Next: .word 0 ; BASIC end marker
44 ; ------------------------------------------------------------------------
51 ; Switch to the second charset
56 ; Before doing anything else, we have to setup our banking configuration.
57 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
58 ; to the underlying RAM works, but it is bad style.
60 lda MMU_CR ; Get current memory configuration...
61 pha ; ...and save it for later
62 lda #MMU_CFG_CC65 ; Bank0 with kernal ROM
65 ; Save the zero page locations we need
77 ; Save system stuff and setup the stack
83 stx spsave ; Save the system stack pointer
85 lda #<(__RAM_START__ + __RAM_SIZE__)
87 lda #>(__RAM_START__ + __RAM_SIZE__)
88 sta sp+1 ; Set argument stack ptr
90 ; If we have IRQ functions, chain our stub into the IRQ vector
92 lda #<__INTERRUPTOR_COUNT__
105 ; Call module constructors
109 ; Set the bank for the file name to our execution bank. We must do this,
110 ; *after* calling constructors, because some of them may depend on the
111 ; original value of this register.
116 ; Push arguments and call main()
120 ; Back from main (this is also the _exit entry). Run module destructors
124 ; Reset the IRQ vector if we chained it.
126 pha ; Save the return code on stack
127 lda #<__INTERRUPTOR_COUNT__
136 ; Copy back the zero page stuff
138 NoIRQ2: ldx #zpspace-1
144 ; Place the program return code into ST
149 ; Reset the stack and the memory configuration
156 ; Done, restore kernal vectors in an attempt to cleanup
160 ; ------------------------------------------------------------------------
161 ; The C128 has ROM parallel to the RAM starting from $4000. The startup code
162 ; above will change this setting so that we have RAM from $0000-$BFFF. This
163 ; works quite well with the exception of interrupts: The interrupt handler
164 ; is in ROM, and the ROM switches back to the ROM configuration, which means
165 ; that parts of our program may not be accessible. To solve this, we place
166 ; the following code into a special segment called "LOWCODE" which will be
167 ; placed just above the startup code, so it goes into a RAM area that is
173 cld ; Just to be sure
174 lda MMU_CR ; Get old register value
175 pha ; And save on stack
176 lda #MMU_CFG_CC65 ; Bank 0 with kernal ROM
178 jsr callirq ; Call the functions
179 pla ; Get old register value
181 jmp IRQInd ; Jump to the saved IRQ vector
184 ; ------------------------------------------------------------------------