2 ; Startup code for cc65 (CBM 500 version)
4 ; This must be the *first* file on the linker command line
8 .exportzp vic, sid, cia1, cia2, acia, tpi1, tpi2, ktab1
9 .exportzp ktab2, ktab3, ktab4, time, RecvBuf, SendBuf
11 .import _clrscr, initlib, donelib
13 .import __CHARRAM_START__, __CHARRAM_SIZE__, __VIDRAM_START__
14 .import __BSS_RUN__, __BSS_SIZE__
16 .import k_irq, k_nmi, k_plot, k_udtim, k_scnkey
18 .include "zeropage.inc"
22 ; ------------------------------------------------------------------------
23 ; BASIC header and a small BASIC program. Since it is not possible to start
24 ; programs in other banks using SYS, the BASIC program will write a small
25 ; machine code program into memory at $100 and start that machine code
26 ; program. The machine code program will then start the machine language
27 ; code in bank 0, which will initialize the system by copying stuff from
28 ; the system bank, and start the application.
30 ; Here's the basic program that's in the following lines:
37 ; 60 data 120,169,0,133,0
39 ; The machine program in the data lines is:
43 ; sta $00 <-- Switch to bank 0 after this command
45 ; Initialization is not only complex because of the jumping from one bank
46 ; into another. but also because we want to save memory, and because of
47 ; this, we will use the system memory ($00-$3FF) for initialization stuff
48 ; that is overwritten later.
53 ; To make things more simple, make the code of this module absolute.
56 Head: .byte $03,$00,$11,$00,$0a,$00,$81,$20,$49,$b2,$30,$20,$a4,$20,$34,$00
57 .byte $19,$00,$14,$00,$87,$20,$4a,$00,$27,$00,$1e,$00,$97,$20,$32,$35
58 .byte $36,$aa,$49,$2c,$4a,$00,$2f,$00,$28,$00,$82,$20,$49,$00,$39,$00
59 .byte $32,$00,$9e,$20,$32,$35,$36,$00,$4f,$00,$3c,$00,$83,$20,$31,$32
60 .byte $30,$2c,$31,$36,$39,$2c,$30,$2c,$31,$33,$33,$2c,$30,$00,$00,$00
62 ; Since we need some vectors to access stuff in the system bank for our own,
63 ; we will include them here, starting from $60:
79 RecvBuf: .word $0100 ; RS232 received buffer
80 SendBuf: .word $0200 ; RS232 send buffer
83 ; The code in the target bank when switching back will be put at the bottom
84 ; of the stack. We will jump here to switch segments. The range $F2..$FF is
85 ; not used by any kernal routine.
93 ; The following code is a copy of the code that is poked in the system bank
94 ; memory by the basic header program, it's only for documentation and not
101 ; This is the actual starting point of our code after switching banks for
102 ; startup. Beware: The following code will get overwritten as soon as we
103 ; use the stack (since it's in page 1)!
106 stx spsave ; Save the system stackpointer
108 txs ; Set up our own stack
110 ; Set the interrupt, NMI and other vectors
118 ; Switch the indirect segment to the system bank
123 ; Copy the kernal zero page ($90-$F2) from the system bank
135 ; Copy the page 3 vectors in place
144 ; Copy the rest of page 3 from the system bank
155 ; Set the indirect segment to bank we're executing in
160 ; Zero the BSS segment. We will do that here instead calling the routine
161 ; in the common library, since we have the memory anyway, and this way,
178 inc ptr1+1 ; Next page
182 ; Clear the remaining page
184 Z2: ldx #<__BSS_SIZE__
199 ; We expect to be in page 2 now
209 .error "Code range invalid"
212 ; This code is in page 2, so we may now start calling subroutines safely,
213 ; since the code we execute is no longer in the stack page.
215 ; Copy the character rom from the system bank into the execution bank
221 lda #<__CHARRAM_START__
223 lda #>__CHARRAM_START__
225 lda #>__CHARRAM_SIZE__ ; 16 * 256 bytes to copy
229 sta IndReg ; Access the system bank
231 sta __VIDRAM_START__,y
236 ccopy2: lda __VIDRAM_START__,y
241 inc ptr2+1 ; Bump high pointer bytes
245 ; Clear the video memory. We will do this before switching the video to bank 0
246 ; to avoid garbage when doing so.
250 ; Reprogram the VIC so that the text screen and the character ROM is in the
251 ; execution bank. This is done in three steps:
253 lda #$0F ; We need access to the system bank
256 ; Place the VIC video RAM into bank 0
267 ; Set bit 14/15 of the VIC address range to the high bits of __VIDRAM_START__
268 ; PC6/PC7 (VICBANKSEL 0/1) = 11
274 ora #<((>__VIDRAM_START__) & $C0)
277 ; Set the VIC base address register to the addresses of the video and
284 ora #<(((__VIDRAM_START__ >> 6) & $F0) | ((__CHARRAM_START__ >> 10) & $0E) | $02)
286 ; ora #<(((>__VIDRAM_START__) << 2) & $F0)
289 ; Switch back to the execution bank
294 ; Call module constructors
298 ; Create the (empty) command line for the program
303 ; Execute the program code
307 ; ------------------------------------------------------------------------
308 ; Additional data that we need for initialization and that's overwritten
322 jmp k_scnkey ; SCNKEY
331 jmp k_setlfs ; SETLFS
332 jmp k_setnam ; SETNAM
342 jmp k_settim ; SETTIM
348 jmp k_screen ; SCREEN
350 jmp k_iobase ; IOBASE
355 .word 0 ; Reset - not used
357 vectable_size = * - vectable
360 .word k_irq ; IRQ user vector
361 .word k_brk ; BRK user vector
362 .word k_nmi ; NMI user vector
363 p3vectable_size = * - p3vectable
366 ; ------------------------------------------------------------------------
367 ; This is the program code after setup. It starts at $400
379 ldy #4 ; Argument size
380 jsr _main ; call the users code
382 ; Call module destructors. This is also the _exit entry.
384 _exit: jsr donelib ; Run module destructors
386 ; We need access to the system bank now
391 ; Switch back the video to the system bank
405 ; Clear the start of the zero page, since it will be interpreted as a
406 ; (garbage) BASIC program otherwise. This is also the default entry for
416 ; Setup the welcome code at the stack bottom in the system bank. Use
417 ; the F4/F5 vector to access the system bank
430 ; ------------------------------------------------------------------------
431 ; Code that is copied into the system bank at $100 when switching back
434 jmp $8000 ; BASIC cold start
435 reset_size = * - reset
437 ; ------------------------------------------------------------------------
438 ; Code for a few simpler kernal calls goes here
482 ; -------------------------------------------------------------------------
483 ; Data area - switch back to relocatable mode