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__, __STACKSIZE__
14 .include "zeropage.inc"
18 ; ------------------------------------------------------------------------
21 IRQInd = $2FD ; JMP $0000 - used as indirect IRQ vector
23 ; ------------------------------------------------------------------------
30 ; Switch to the second charset
35 ; Before doing anything else, we have to setup our banking configuration.
36 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
37 ; to the underlying RAM works, but it is bad style.
39 lda MMU_CR ; Get current memory configuration...
40 pha ; ...and save it for later
41 lda #MMU_CFG_CC65 ; Bank0 with kernal ROM
44 ; Save the zero page locations we need
56 ; Save system stuff and setup the stack
62 stx spsave ; Save the system stack pointer
64 lda #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
66 lda #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
67 sta sp+1 ; Set argument stack ptr
69 ; If we have IRQ functions, chain our stub into the IRQ vector
71 lda #<__INTERRUPTOR_COUNT__
84 ; Call module constructors
88 ; Set the bank for the file name to our execution bank. We must do this,
89 ; *after* calling constructors, because some of them may depend on the
90 ; original value of this register.
95 ; Push arguments and call main()
99 ; Back from main (this is also the _exit entry). Run module destructors
103 ; Reset the IRQ vector if we chained it.
105 pha ; Save the return code on stack
106 lda #<__INTERRUPTOR_COUNT__
115 ; Copy back the zero page stuff
117 NoIRQ2: ldx #zpspace-1
123 ; Place the program return code into ST
128 ; Reset the stack and the memory configuration
135 ; Done, return to BASIC
139 ; ------------------------------------------------------------------------
140 ; The C128 has ROM parallel to the RAM starting from $4000. The startup code
141 ; above will change this setting so that we have RAM from $0000-$BFFF. This
142 ; works quite well with the exception of interrupts: The interrupt handler
143 ; is in ROM, and the ROM switches back to the ROM configuration, which means
144 ; that parts of our program may not be accessible. To solve this, we place
145 ; the following code into a special segment called "LOWCODE" which will be
146 ; placed just above the startup code, so it goes into a RAM area that is
152 cld ; Just to be sure
153 lda MMU_CR ; Get old register value
154 pha ; And save on stack
155 lda #MMU_CFG_CC65 ; Bank 0 with kernal ROM
157 jsr callirq ; Call the functions
158 pla ; Get old register value
160 jmp IRQInd ; Jump to the saved IRQ vector
163 ; ------------------------------------------------------------------------