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) + '0')
33 .byte <(((Start / 100) .mod 10) + '0')
34 .byte <(((Start / 10) .mod 10) + '0')
35 .byte <(((Start / 1) .mod 10) + '0')
36 .byte $00 ; End of BASIC line
37 @Next: .word 0 ; BASIC end marker
39 ; ------------------------------------------------------------------------
46 ; Switch to the second charset
51 ; Before doing anything else, we have to setup our banking configuration.
52 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
53 ; to the underlying RAM works, but it is bad style.
55 lda MMU_CR ; Get current memory configuration...
56 pha ; ...and save it for later
57 lda #MMU_CFG_CC65 ; Bank0 with kernal ROM
60 ; Save the zero page locations we need
72 ; Save system stuff and setup the stack
78 stx spsave ; Save the system stack pointer
80 lda #<(__RAM_START__ + __RAM_SIZE__)
82 lda #>(__RAM_START__ + __RAM_SIZE__)
83 sta sp+1 ; Set argument stack ptr
85 ; If we have IRQ functions, chain our stub into the IRQ vector
87 lda #<__INTERRUPTOR_COUNT__
100 ; Call module constructors
104 ; Set the bank for the file name to our execution bank. We must do this,
105 ; *after* calling constructors, because some of them may depend on the
106 ; original value of this register.
111 ; Push arguments and call main()
115 ; Back from main (this is also the _exit entry). Run module destructors
119 ; Reset the IRQ vector if we chained it.
121 pha ; Save the return code on stack
122 lda #<__INTERRUPTOR_COUNT__
131 ; Copy back the zero page stuff
133 NoIRQ2: ldx #zpspace-1
139 ; Place the program return code into ST
144 ; Reset the stack and the memory configuration
151 ; Done, restore kernal vectors in an attempt to cleanup
155 ; ------------------------------------------------------------------------
156 ; The C128 has ROM parallel to the RAM starting from $4000. The startup code
157 ; above will change this setting so that we have RAM from $0000-$BFFF. This
158 ; works quite well with the exception of interrupts: The interrupt handler
159 ; is in ROM, and the ROM switches back to the ROM configuration, which means
160 ; that parts of our program may not be accessible. To solve this, we place
161 ; the following code into a special segment called "LOWCODE" which will be
162 ; placed just above the startup code, so it goes into a RAM area that is
168 cld ; Just to be sure
169 lda MMU_CR ; Get old register value
170 pha ; And save on stack
171 lda #MMU_CFG_CC65 ; Bank 0 with kernal ROM
173 jsr callirq ; Call the functions
174 pla ; Get old register value
176 jmp IRQInd ; Jump to the saved IRQ vector
179 ; ------------------------------------------------------------------------