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 ; BASIC header with a SYS call
28 .word Head ; Load address
30 .word .version ; Line number
32 .byte <(((Start / 1000) .mod 10) + $30)
33 .byte <(((Start / 100) .mod 10) + $30)
34 .byte <(((Start / 10) .mod 10) + $30)
35 .byte <(((Start / 1) .mod 10) + $30)
36 .byte $00 ; End of BASIC line
37 @Next: .word 0 ; BASIC end marker
39 ; ------------------------------------------------------------------------
50 ; Switch to the second charset
55 ; Before doing anything else, we have to setup our banking configuration.
56 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
57 ; to the underlying RAM works, but it is bad style.
59 lda MMU_CR ; Get current memory configuration...
60 pha ; ...and save it for later
61 lda #MMU_CFG_CC65 ; Bank0 with kernal ROM
64 ; Save the zero page locations we need
76 ; Save system stuff and setup the stack
82 stx spsave ; Save the system stack pointer
84 lda #<(__RAM_START__ + __RAM_SIZE__)
86 lda #>(__RAM_START__ + __RAM_SIZE__)
87 sta sp+1 ; Set argument stack ptr
89 ; If we have IRQ functions, chain our stub into the IRQ vector
91 lda #<__INTERRUPTOR_COUNT__
104 ; Call module constructors
108 ; Set the bank for the file name to our execution bank. We must do this,
109 ; *after* calling constructors, because some of them may depend on the
110 ; original value of this register.
115 ; Push arguments and call main()
119 ; Back from main (this is also the _exit entry). Run module destructors
123 ; Reset the IRQ vector if we chained it.
125 pha ; Save the return code on stack
126 lda #<__INTERRUPTOR_COUNT__
135 ; Copy back the zero page stuff
137 NoIRQ2: ldx #zpspace-1
143 ; Place the program return code into ST
148 ; Reset the stack and the memory configuration
155 ; Done, restore kernal vectors in an attempt to cleanup
159 ; ------------------------------------------------------------------------
160 ; The C128 has ROM parallel to the RAM starting from $4000. The startup code
161 ; above will change this setting so that we have RAM from $0000-$BFFF. This
162 ; works quite well with the exception of interrupts: The interrupt handler
163 ; is in ROM, and the ROM switches back to the ROM configuration, which means
164 ; that parts of our program may not be accessible. To solve this, we place
165 ; the following code into a special segment called "LOWCODE" which will be
166 ; placed just above the startup code, so it goes into a RAM area that is
172 cld ; Just to be sure
173 lda MMU_CR ; Get old register value
174 pha ; And save on stack
175 lda #MMU_CFG_CC65 ; Bank 0 with kernal ROM
177 jsr callirq ; Call the functions
178 pla ; Get old register value
180 jmp IRQInd ; Jump to the saved IRQ vector
183 ; ------------------------------------------------------------------------