2 ; Startup code for cc65 (CBM 600/700 version)
4 ; This must be the *first* file on the linker command line
8 .import initlib, donelib
10 .import __BSS_RUN__, __BSS_SIZE__
12 .import k_irq, k_nmi, k_plot, k_udtim, k_scnkey
14 .include "zeropage.inc"
18 ; ------------------------------------------------------------------------
19 ; BASIC header and a small BASIC program. Since it is not possible to start
20 ; programs in other banks using SYS, the BASIC program will write a small
21 ; machine code program into memory at $100 and start that machine code
22 ; program. The machine code program will then start the machine language
23 ; code in bank 1, which will initialize the system by copying stuff from
24 ; the system bank, and start the application.
26 ; Here's the basic program that's in the following lines:
33 ; 60 data 120,169,1,133,0
35 ; The machine program in the data lines is:
39 ; sta $00 <-- Switch to bank 1 after this command
41 ; Initialization is not only complex because of the jumping from one bank
42 ; into another. but also because we want to save memory, and because of
43 ; this, we will use the system memory ($00-$3FF) for initialization stuff
44 ; that is overwritten later.
49 ; To make things more simple, make the code of this module absolute.
52 Head: .byte $03,$00,$11,$00,$0a,$00,$81,$20,$49,$b2,$30,$20,$a4,$20,$34,$00
53 .byte $19,$00,$14,$00,$87,$20,$4a,$00,$27,$00,$1e,$00,$97,$20,$32,$35
54 .byte $36,$aa,$49,$2c,$4a,$00,$2f,$00,$28,$00,$82,$20,$49,$00,$39,$00
55 .byte $32,$00,$9e,$20,$32,$35,$36,$00,$4f,$00,$3c,$00,$83,$20,$31,$32
56 .byte $30,$2c,$31,$36,$39,$2c,$31,$2c,$31,$33,$33,$2c,$30,$00,$00,$00
58 ; Since we need some vectors to access stuff in the system bank for our own,
59 ; we will include them here, starting from $60:
75 RecvBuf: .word $0100 ; RS232 received buffer
76 SendBuf: .word $0200 ; RS232 send buffer
79 ; The code in the target bank when switching back will be put at the bottom
80 ; of the stack. We will jump here to switch segments. The range $F2..$FF is
81 ; not used by any kernal routine.
89 ; The following code is a copy of the code that is poked in the system bank
90 ; memory by the basic header program, it's only for documentation and not
97 ; This is the actual starting point of our code after switching banks for
98 ; startup. Beware: The following code will get overwritten as soon as we
99 ; use the stack (since it's in page 1)!
102 stx spsave ; Save the system stackpointer
104 txs ; Set up our own stack
106 ; Set the interrupt, NMI and other vectors
114 ; Switch the indirect segment to the system bank
119 ; Copy the kernal zero page ($90-$F2) from the system bank
131 ; Copy the page 3 vectors in place
140 ; Copy the rest of page 3 from the system bank
151 ; Set the indirect segment to bank we're executing in
156 ; Zero the BSS segment. We will do that here instead calling the routine
157 ; in the common library, since we have the memory anyway, and this way,
174 inc ptr1+1 ; Next page
178 ; Clear the remaining page
180 Z2: ldx #<__BSS_SIZE__
195 ; We expect to be in page 2 now
205 .error "Code range invalid"
208 ; This code is in page 2, so we may now start calling subroutines safely,
209 ; since the code we execute is no longer in the stack page.
210 ; Call module constructors
214 ; Create the (empty) command line for the program
219 ; Execute the program code
223 ; ------------------------------------------------------------------------
224 ; Additional data that we need for initialization and that's overwritten
238 jmp k_scnkey ; SCNKEY
247 jmp k_setlfs ; SETLFS
248 jmp k_setnam ; SETNAM
258 jmp k_settim ; SETTIM
264 jmp k_screen ; SCREEN
266 jmp k_iobase ; IOBASE
271 .word 0 ; Reset - not used
273 vectable_size = * - vectable
276 .word k_irq ; IRQ user vector
277 .word k_brk ; BRK user vector
278 .word k_nmi ; NMI user vector
279 p3vectable_size = * - p3vectable
282 ; ------------------------------------------------------------------------
283 ; This is the program code after setup. It starts at $400
295 ldy #4 ; Argument size
296 jsr _main ; call the users code
298 ; Call module destructors. This is also the _exit entry.
300 _exit: jsr donelib ; Run module destructors
302 ; Clear the start of the zero page, since it will be interpreted as a
303 ; (garbage) BASIC program otherwise. This is also the default entry for
313 ; Setup the welcome code at the stack bottom in the system bank. Use
314 ; the F4/F5 vector to access the system bank
329 ; ------------------------------------------------------------------------
330 ; Code that is copied into the system bank at $100 when switching back
333 jmp $8000 ; BASIC cold start
334 reset_size = * - reset
336 ; ------------------------------------------------------------------------
337 ; Code for a few simpler kernal calls goes here
381 ; -------------------------------------------------------------------------
382 ; Data area - switch back to relocatable mode