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
35 .byte $9E,"7181" ; SYS 7181
36 .byte $00 ; End of BASIC line
37 @Next: .word 0 ; BASIC end marker
40 ; ------------------------------------------------------------------------
47 ; Switch to the second charset
52 ; Before doing anything else, we have to setup our banking configuration.
53 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
54 ; to the underlying RAM works, but it is bad style.
56 lda MMU_CR ; Get current memory configuration...
57 pha ; ...and save it for later
58 lda #MMU_CFG_CC65 ; Bank0 with kernal ROM
61 ; Save the zero page locations we need
73 ; Save system stuff and setup the stack
79 stx spsave ; Save the system stack pointer
81 lda #<(__RAM_START__ + __RAM_SIZE__)
83 lda #>(__RAM_START__ + __RAM_SIZE__)
84 sta sp+1 ; Set argument stack ptr
86 ; If we have IRQ functions, chain our stub into the IRQ vector
88 lda #<__INTERRUPTOR_COUNT__
101 ; Call module constructors
105 ; Set the bank for the file name to our execution bank. We must do this,
106 ; *after* calling constructors, because some of them may depend on the
107 ; original value of this register.
112 ; Push arguments and call main()
116 ; Back from main (this is also the _exit entry). Run module destructors
120 ; Reset the IRQ vector if we chained it.
122 pha ; Save the return code on stack
123 lda #<__INTERRUPTOR_COUNT__
132 ; Copy back the zero page stuff
134 NoIRQ2: ldx #zpspace-1
140 ; Place the program return code into ST
145 ; Reset the stack and the memory configuration
152 ; Done, restore kernal vectors in an attempt to cleanup
156 ; ------------------------------------------------------------------------
157 ; The C128 has ROM parallel to the RAM starting from $4000. The startup code
158 ; above will change this setting so that we have RAM from $0000-$BFFF. This
159 ; works quite well with the exception of interrupts: The interrupt handler
160 ; is in ROM, and the ROM switches back to the ROM configuration, which means
161 ; that parts of our program may not be accessible. To solve this, we place
162 ; the following code into a special segment called "LOWCODE" which will be
163 ; placed just above the startup code, so it goes into a RAM area that is
169 cld ; Just to be sure
170 lda MMU_CR ; Get old register value
171 pha ; And save on stack
172 lda #MMU_CFG_CC65 ; Bank 0 with kernal ROM
174 jsr callirq ; Call the functions
175 pla ; Get old register value
177 jmp IRQInd ; Jump to the saved IRQ vector
180 ; ------------------------------------------------------------------------