2 ; Startup code for cc65 (C128 version)
4 ; This must be the *first* file on the linker command line
8 .export BRKStub, BRKOld, BRKInd
9 .import condes, initlib, donelib
10 .import initconio, doneconio, zerobss
12 .import __IRQFUNC_TABLE__, __IRQFUNC_COUNT__
13 .import __RAM_START__, __RAM_SIZE__
15 .include "zeropage.inc"
17 .include "../cbm/cbm.inc"
19 ; ------------------------------------------------------------------------
22 IRQInd = $2FD ; JMP $0000 - used as indirect IRQ vector
24 ; ------------------------------------------------------------------------
25 ; Place the startup code in a special segment to cope with the quirks of
26 ; c128 banking. Do also create an empty segment named "NMI" to avoid
27 ; warnings if the rs232 routines are not used.
34 ; ------------------------------------------------------------------------
35 ; BASIC header with a SYS call
38 .word Head ; Load address
40 .word 1000 ; Line number
41 .byte $9E,"7181" ; SYS 7181
42 .byte $00 ; End of BASIC line
43 @Next: .word 0 ; BASIC end marker
46 ; ------------------------------------------------------------------------
53 ; Switch to the second charset
58 ; Set the bank for the file name our execution bank
63 ; Before doing anything else, we have to setup our banking configuration.
64 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
65 ; to the underlying RAM works, but it is bad style.
67 lda MMU_CR ; Get current memory configuration...
68 pha ; ...and save it for later
69 lda #CC65_MMU_CFG ; Bank0 with kernal ROM
72 ; Save the zero page locations we need
84 ; Save system stuff and setup the stack
90 stx spsave ; Save the system stack pointer
92 lda #<(__RAM_START__ + __RAM_SIZE__)
94 lda #>(__RAM_START__ + __RAM_SIZE__)
95 sta sp+1 ; Set argument stack ptr
97 ; Call module constructors
101 ; Initialize conio stuff
105 ; If we have IRQ functions, chain our stub into the IRQ vector
107 lda #<__IRQFUNC_COUNT__
120 ; Pass an empty command line
122 NoIRQ1: jsr push0 ; argc
125 ldy #4 ; Argument size
126 jsr _main ; call the users code
128 ; This is also the _exit entry. Reset the IRQ vector if we chained it.
130 _exit: lda #<__IRQFUNC_COUNT__
139 ; Run module destructors
143 ; Reset the conio stuff
152 ; Copy back the zero page stuff
160 ; Reset the memory configuration
165 ; Done, restore kernal vectors in an attempt to cleanup
169 ; ------------------------------------------------------------------------
170 ; The C128 has ROM parallel to the RAM starting from $4000. The startup code
171 ; above will change this setting so that we have RAM from $0000-$BFFF. This
172 ; works quite well with the exception of interrupts: The interrupt handler
173 ; is in ROM, and the ROM switches back to the ROM configuration, which means
174 ; that parts of our program may not be accessible. Since the crt0 module is
175 ; the first module in the program, it will always be below $4000 and always
176 ; in RAM. So we place several short stubs here that switch back our ROM
177 ; config before calling our user defined handlers. These stubs are only
178 ; used if any other code uses the interrupt or break vectors. They are dead
179 ; code otherwise, but since there is no other way to keep them in low memory,
180 ; they have to go here.
183 cld ; Just to be sure
184 lda MMU_CR ; Get old register value
185 pha ; And save on stack
186 lda #CC65_MMU_CFG ; Bank 0 with kernal ROM
188 ldy #<(__IRQFUNC_COUNT__*2)
189 lda #<__IRQFUNC_TABLE__
190 ldx #>__IRQFUNC_TABLE__
191 jsr condes ; Call the functions
192 pla ; Get old register value
194 jmp IRQInd ; Jump to the save IRQ vector
198 pla ; Get original MMU_CR value
199 sta MMU_CR ; And set it
200 jmp BRKInd ; Jump indirect to break
203 ; ------------------------------------------------------------------------
209 ; Old break vector preceeded by a jump opcode
212 ; Indirect vectors preceeded by a jump opcode