2 ; Startup code for cc65 (CBM 500 version)
4 ; This must be the *first* file on the linker command line
8 .import _clrscr, initlib, donelib
10 .import __CHARRAM_START__, __CHARRAM_SIZE__, __VIDRAM_START__
11 .import __BSS_RUN__, __BSS_SIZE__
13 .import k_irq, k_nmi, k_plot, k_udtim, k_scnkey
15 .include "zeropage.inc"
19 ; ------------------------------------------------------------------------
20 ; BASIC header and a small BASIC program. Since it is not possible to start
21 ; programs in other banks using SYS, the BASIC program will write a small
22 ; machine code program into memory at $100 and start that machine code
23 ; program. The machine code program will then start the machine language
24 ; code in bank 0, which will initialize the system by copying stuff from
25 ; the system bank, and start the application.
27 ; Here's the basic program that's in the following lines:
34 ; 60 data 120,169,0,133,0
36 ; The machine program in the data lines is:
40 ; sta $00 <-- Switch to bank 0 after this command
42 ; Initialization is not only complex because of the jumping from one bank
43 ; into another. but also because we want to save memory, and because of
44 ; this, we will use the system memory ($00-$3FF) for initialization stuff
45 ; that is overwritten later.
50 ; To make things more simple, make the code of this module absolute.
53 Head: .byte $03,$00,$11,$00,$0a,$00,$81,$20,$49,$b2,$30,$20,$a4,$20,$34,$00
54 .byte $19,$00,$14,$00,$87,$20,$4a,$00,$27,$00,$1e,$00,$97,$20,$32,$35
55 .byte $36,$aa,$49,$2c,$4a,$00,$2f,$00,$28,$00,$82,$20,$49,$00,$39,$00
56 .byte $32,$00,$9e,$20,$32,$35,$36,$00,$4f,$00,$3c,$00,$83,$20,$31,$32
57 .byte $30,$2c,$31,$36,$39,$2c,$30,$2c,$31,$33,$33,$2c,$30,$00,$00,$00
59 ; Since we need some vectors to access stuff in the system bank for our own,
60 ; we will include them here, starting from $60:
76 RecvBuf: .word $0100 ; RS232 received buffer
77 SendBuf: .word $0200 ; RS232 send buffer
80 ; The code in the target bank when switching back will be put at the bottom
81 ; of the stack. We will jump here to switch segments. The range $F2..$FF is
82 ; not used by any kernal routine.
90 ; The following code is a copy of the code that is poked in the system bank
91 ; memory by the basic header program, it's only for documentation and not
98 ; This is the actual starting point of our code after switching banks for
99 ; startup. Beware: The following code will get overwritten as soon as we
100 ; use the stack (since it's in page 1)!
103 stx spsave ; Save the system stackpointer
105 txs ; Set up our own stack
107 ; Set the interrupt, NMI and other vectors
115 ; Switch the indirect segment to the system bank
120 ; Copy the kernal zero page ($90-$F2) from the system bank
132 ; Copy the page 3 vectors in place
141 ; Copy the rest of page 3 from the system bank
152 ; Set the indirect segment to bank we're executing in
157 ; Zero the BSS segment. We will do that here instead calling the routine
158 ; in the common library, since we have the memory anyway, and this way,
175 inc ptr1+1 ; Next page
179 ; Clear the remaining page
181 Z2: ldx #<__BSS_SIZE__
196 ; We expect to be in page 2 now
206 .error "Code range invalid"
209 ; This code is in page 2, so we may now start calling subroutines safely,
210 ; since the code we execute is no longer in the stack page.
212 ; Copy the character rom from the system bank into the execution bank
218 lda #<__CHARRAM_START__
220 lda #>__CHARRAM_START__
222 lda #>__CHARRAM_SIZE__ ; 16 * 256 bytes to copy
226 sta IndReg ; Access the system bank
228 sta __VIDRAM_START__,y
233 ccopy2: lda __VIDRAM_START__,y
238 inc ptr2+1 ; Bump high pointer bytes
242 ; Clear the video memory. We will do this before switching the video to bank 0
243 ; to avoid garbage when doing so.
247 ; Reprogram the VIC so that the text screen and the character ROM is in the
248 ; execution bank. This is done in three steps:
250 lda #$0F ; We need access to the system bank
253 ; Place the VIC video RAM into bank 0
264 ; Set bit 14/15 of the VIC address range to the high bits of __VIDRAM_START__
265 ; PC6/PC7 (VICBANKSEL 0/1) = 11
271 ora #<((>__VIDRAM_START__) & $C0)
274 ; Set the VIC base address register to the addresses of the video and
281 ora #<(((__VIDRAM_START__ >> 6) & $F0) | ((__CHARRAM_START__ >> 10) & $0E) | $02)
283 ; ora #<(((>__VIDRAM_START__) << 2) & $F0)
286 ; Switch back to the execution bank
291 ; Call module constructors
295 ; Create the (empty) command line for the program
300 ; Execute the program code
304 ; ------------------------------------------------------------------------
305 ; Additional data that we need for initialization and that's overwritten
319 jmp k_scnkey ; SCNKEY
328 jmp k_setlfs ; SETLFS
329 jmp k_setnam ; SETNAM
339 jmp k_settim ; SETTIM
345 jmp k_screen ; SCREEN
347 jmp k_iobase ; IOBASE
352 .word 0 ; Reset - not used
354 vectable_size = * - vectable
357 .word k_irq ; IRQ user vector
358 .word k_brk ; BRK user vector
359 .word k_nmi ; NMI user vector
360 p3vectable_size = * - p3vectable
363 ; ------------------------------------------------------------------------
364 ; This is the program code after setup. It starts at $400
376 ldy #4 ; Argument size
377 jsr _main ; call the users code
379 ; Call module destructors. This is also the _exit entry.
381 _exit: jsr donelib ; Run module destructors
383 ; We need access to the system bank now
388 ; Switch back the video to the system bank
402 ; Clear the start of the zero page, since it will be interpreted as a
403 ; (garbage) BASIC program otherwise. This is also the default entry for
413 ; Setup the welcome code at the stack bottom in the system bank. Use
414 ; the F4/F5 vector to access the system bank
427 ; ------------------------------------------------------------------------
428 ; Code that is copied into the system bank at $100 when switching back
431 jmp $8000 ; BASIC cold start
432 reset_size = * - reset
434 ; ------------------------------------------------------------------------
435 ; Code for a few simpler kernal calls goes here
479 ; -------------------------------------------------------------------------
480 ; Data area - switch back to relocatable mode