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 RESTOR, BSOUT, CLRCH
13 .import __IRQFUNC_TABLE__, __IRQFUNC_COUNT__
14 .import __RAM_START__, __RAM_SIZE__
16 .include "zeropage.inc"
20 ; ------------------------------------------------------------------------
23 IRQInd = $2FD ; JMP $0000 - used as indirect IRQ vector
25 ; ------------------------------------------------------------------------
26 ; Place the startup code in a special segment to cope with the quirks of
27 ; c128 banking. Do also create an empty segment named "NMI" to avoid
28 ; warnings if the rs232 routines are not used.
35 ; ------------------------------------------------------------------------
36 ; BASIC header with a SYS call
39 .word Head ; Load address
41 .word 1000 ; Line number
42 .byte $9E,"7181" ; SYS 7181
43 .byte $00 ; End of BASIC line
44 @Next: .word 0 ; BASIC end marker
47 ; ------------------------------------------------------------------------
54 ; Switch to the second charset
59 ; Set the bank for the file name our execution bank
64 ; Before doing anything else, we have to setup our banking configuration.
65 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
66 ; to the underlying RAM works, but it is bad style.
68 lda MMU_CR ; Get current memory configuration...
69 pha ; ...and save it for later
70 lda #CC65_MMU_CFG ; Bank0 with kernal ROM
73 ; Save the zero page locations we need
85 ; Save system stuff and setup the stack
91 stx spsave ; Save the system stack pointer
93 lda #<(__RAM_START__ + __RAM_SIZE__)
95 lda #>(__RAM_START__ + __RAM_SIZE__)
96 sta sp+1 ; Set argument stack ptr
98 ; Call module constructors
102 ; Initialize conio stuff
106 ; If we have IRQ functions, chain our stub into the IRQ vector
108 lda #<__IRQFUNC_COUNT__
121 ; Pass an empty command line
123 NoIRQ1: jsr push0 ; argc
126 ldy #4 ; Argument size
127 jsr _main ; call the users code
129 ; This is also the _exit entry. Reset the IRQ vector if we chained it.
131 _exit: lda #<__IRQFUNC_COUNT__
140 ; Run module destructors
144 ; Reset the conio stuff
153 ; Copy back the zero page stuff
161 ; Reset the memory configuration
166 ; Done, restore kernal vectors in an attempt to cleanup
170 ; ------------------------------------------------------------------------
171 ; The C128 has ROM parallel to the RAM starting from $4000. The startup code
172 ; above will change this setting so that we have RAM from $0000-$BFFF. This
173 ; works quite well with the exception of interrupts: The interrupt handler
174 ; is in ROM, and the ROM switches back to the ROM configuration, which means
175 ; that parts of our program may not be accessible. Since the crt0 module is
176 ; the first module in the program, it will always be below $4000 and always
177 ; in RAM. So we place several short stubs here that switch back our ROM
178 ; config before calling our user defined handlers. These stubs are only
179 ; used if any other code uses the interrupt or break vectors. They are dead
180 ; code otherwise, but since there is no other way to keep them in low memory,
181 ; they have to go here.
184 cld ; Just to be sure
185 lda MMU_CR ; Get old register value
186 pha ; And save on stack
187 lda #CC65_MMU_CFG ; Bank 0 with kernal ROM
189 ldy #<(__IRQFUNC_COUNT__*2)
190 lda #<__IRQFUNC_TABLE__
191 ldx #>__IRQFUNC_TABLE__
192 jsr condes ; Call the functions
193 pla ; Get old register value
195 jmp IRQInd ; Jump to the save IRQ vector
199 pla ; Get original MMU_CR value
200 sta MMU_CR ; And set it
201 jmp BRKInd ; Jump indirect to break
204 ; ------------------------------------------------------------------------
210 ; Old break vector preceeded by a jump opcode
213 ; Indirect vectors preceeded by a jump opcode