2 ; Startup code for cc65 (C128 version)
4 ; This must be the *first* file on the linker command line
8 .import condes, initlib, donelib
9 .import initconio, doneconio, zerobss
11 .import RESTOR, BSOUT, CLRCH
12 .import __IRQFUNC_TABLE__, __IRQFUNC_COUNT__
13 .import __RAM_START__, __RAM_SIZE__
15 .include "zeropage.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
30 ; BASIC header with a SYS call
33 .word Head ; Load address
35 .word 1000 ; Line number
36 .byte $9E,"7181" ; SYS 7181
37 .byte $00 ; End of BASIC line
38 @Next: .word 0 ; BASIC end marker
41 ; ------------------------------------------------------------------------
48 ; Switch to the second charset
53 ; Set the bank for the file name our execution bank
58 ; Before doing anything else, we have to setup our banking configuration.
59 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
60 ; to the underlying RAM works, but it is bad style.
62 lda MMU_CR ; Get current memory configuration...
63 pha ; ...and save it for later
64 lda #CC65_MMU_CFG ; Bank0 with kernal ROM
67 ; Save the zero page locations we need
79 ; Save system stuff and setup the stack
85 stx spsave ; Save the system stack pointer
87 lda #<(__RAM_START__ + __RAM_SIZE__)
89 lda #>(__RAM_START__ + __RAM_SIZE__)
90 sta sp+1 ; Set argument stack ptr
92 ; Call module constructors
96 ; Initialize conio stuff
100 ; If we have IRQ functions, chain our stub into the IRQ vector
102 lda #<__IRQFUNC_COUNT__
115 ; Pass an empty command line
117 NoIRQ1: jsr push0 ; argc
120 ldy #4 ; Argument size
121 jsr _main ; call the users code
123 ; This is also the _exit entry. Reset the IRQ vector if we chained it.
125 _exit: lda #<__IRQFUNC_COUNT__
134 ; Run module destructors
138 ; Reset the conio stuff
147 ; Copy back the zero page stuff
155 ; Reset the memory configuration
160 ; Done, restore kernal vectors in an attempt to cleanup
164 ; ------------------------------------------------------------------------
165 ; The C128 has ROM parallel to the RAM starting from $4000. The startup code
166 ; above will change this setting so that we have RAM from $0000-$BFFF. This
167 ; works quite well with the exception of interrupts: The interrupt handler
168 ; is in ROM, and the ROM switches back to the ROM configuration, which means
169 ; that parts of our program may not be accessible. To solve this, we place
170 ; the following code into a special segment called "LOWCODE" which will be
171 ; placed just above the startup code, so it goes into a RAM area that is
177 cld ; Just to be sure
178 lda MMU_CR ; Get old register value
179 pha ; And save on stack
180 lda #CC65_MMU_CFG ; Bank 0 with kernal ROM
182 ldy #<(__IRQFUNC_COUNT__*2)
183 lda #<__IRQFUNC_TABLE__
184 ldx #>__IRQFUNC_TABLE__
185 jsr condes ; Call the functions
186 pla ; Get old register value
188 jmp IRQInd ; Jump to the saved IRQ vector
191 ; ------------------------------------------------------------------------