2 ; Startup code for cc65 (CBM 600/700 version)
6 .export __STARTUP__ : absolute = 1 ; Mark as startup
8 .import callirq_y, initlib, donelib
9 .import push0, callmain
10 .import __BSS_RUN__, __BSS_SIZE__, __EXTZP_RUN__
11 .import __INTERRUPTOR_COUNT__
14 .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 1, 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,1,133,0
36 ; The machine program in the data lines is:
40 ; sta $00 <-- Switch to bank 1 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 .byte $03,$00,$11,$00,$0a,$00,$81,$20,$49,$b2,$30,$20,$a4,$20,$34,$00
51 .byte $19,$00,$14,$00,$87,$20,$4a,$00,$27,$00,$1e,$00,$97,$20,$32,$35
52 .byte $36,$aa,$49,$2c,$4a,$00,$2f,$00,$28,$00,$82,$20,$49,$00,$39,$00
53 .byte $32,$00,$9e,$20,$32,$35,$36,$00,$4f,$00,$3c,$00,$83,$20,$31,$32
54 .byte $30,$2c,$31,$36,$39,$2c,$31,$2c,$31,$33,$33,$2c,$30,$00,$00,$00
56 ;------------------------------------------------------------------------------
57 ; A table that contains values that must be transfered from the system zero
58 ; page into out zero page. Contains pairs of bytes, first one is the address
59 ; in the system ZP, second one is our ZP address. The table goes into page 2,
60 ; but is declared here, because it is needed earlier.
74 ;------------------------------------------------------------------------------
75 ; Page 3 data. This page contains the break vector and the bankswitch
76 ; subroutine that is copied into high memory on startup. The space occupied by
77 ; this routine will later be used for a copy of the bank 15 stack. It must be
78 ; saved, since we're going to destroy it when calling bank 15.
82 BRKVec: .addr _exit ; BRK indirect vector
106 lda #.hibyte(excrts-1)
109 lda #.lobyte(excrts-1)
115 sta $1FF ; Save new sp
143 ldy $1FF ; Restore sp in bank 15
145 lda #.hibyte(expull-1)
148 lda #.lobyte(expull-1)
169 .if (expull <> $FF2E)
170 .error "Symbol expull must be aligned with kernal in bank 15"
177 ;------------------------------------------------------------------------------
178 ; The code in the target bank when switching back will be put at the bottom
179 ; of the stack. We will jump here to switch segments. The range $F2..$FF is
180 ; not used by any kernal routine.
186 ; We are at $100 now. The following snippet is a copy of the code that is poked
187 ; in the system bank memory by the basic header program, it's only for
188 ; documentation and not actually used here:
194 ; This is the actual starting point of our code after switching banks for
195 ; startup. Beware: The following code will get overwritten as soon as we
196 ; use the stack (since it's in page 1)! We jump to another location, since
197 ; we need some space for subroutines that aren't used later.
201 ; Hardware vectors, copied to $FFF6
207 .word nmi ; NMI vector
208 .word 0 ; Reset - not used
209 .word irq ; IRQ vector
212 ; Initializers for the extended zeropage. See extzp.s
230 ; Switch the indirect segment to the system bank
235 ; Initialize the extended zeropage
237 ldx #.sizeof(extzp)-1
243 ; Save the old stack pointer from the system bank and setup our hw sp
248 sta (sysp1),y ; Save system stack point into $F:$1FF
249 ldx #$FE ; Leave $1FF untouched for cross bank calls
250 txs ; Set up our own stack
252 ; Copy stuff from the system zeropage to ours
254 lda #.sizeof(transfer_table)
257 ldy transfer_table-2,x
258 lda transfer_table-1,x
266 ; Set the interrupt, NMI and other vectors
268 ldx #.sizeof(vectors)-1
270 sta $10000 - .sizeof(vectors),x
276 lda #.lobyte(callbank15::entry)
278 lda #.hibyte(callbank15::entry)
281 ; Setup the subroutine and jump vector table that redirects kernal calls to
284 ldy #.sizeof(callbank15)
285 @L1: lda callbank15-1,y
286 sta callbank15::entry-1,y
290 ; Setup the jump vector table. Y is zero on entry.
292 ldx #45-1 ; Number of vectors
293 @L2: lda #$20 ; JSR opcode
296 lda #.lobyte(callbank15::entry)
299 lda #.hibyte(callbank15::entry)
305 ; Set the indirect segment to bank we're executing in
310 ; Zero the BSS segment. We will do that here instead calling the routine
311 ; in the common library, since we have the memory anyway, and this way,
328 inc ptr1+1 ; Next page
332 ; Clear the remaining page
334 Z2: ldx #<__BSS_SIZE__
342 ; ------------------------------------------------------------------------
343 ; We are at $200 now. We may now start calling subroutines safely, since
344 ; the code we execute is no longer in the stack page.
348 ; Activate chained interrupt handlers, then enable interrupts.
350 Init: lda #.lobyte(__INTERRUPTOR_COUNT__*2)
354 ; Call module constructors.
358 ; Push arguments and call main()
362 ; Call module destructors. This is also the _exit entry and the default entry
363 ; point for the break vector.
365 _exit: pha ; Save the return code
366 jsr donelib ; Run module destructors
368 sta irqcount ; Disable custom irq handlers
370 ; Address the system bank
375 ; Copy stuff back from our zeropage to the systems
378 lda #.sizeof(transfer_table)
381 ldy transfer_table-2,x
382 lda transfer_table-1,x
391 ; Place the program return code into ST
397 ; Setup the welcome code at the stack bottom in the system bank.
400 lda (sysp1),y ; Load system bank sp
403 lda #$58 ; CLI opcode
406 lda #$60 ; RTS opcode
413 ; -------------------------------------------------------------------------
414 ; The IRQ handler goes into PAGE2. For performance reasons, and to allow
415 ; easier chaining, we do handle the IRQs in the execution bank (instead of
416 ; passing them to the system bank).
418 ; This is the mapping of the active irq register of the 6525 (tpi1):
420 ; Bit 7 6 5 4 3 2 1 0
422 ; | | | ^ SRQ IEEE 488
435 sta IndReg ; Be sure to address our segment
437 lda $105,x ; Get the flags from the stack
438 and #$10 ; Test break flag
445 ; Call chained IRQ handlers
449 jsr callirq_y ; Call the functions
451 ; Done with chained IRQ handlers, check the TPI for IRQs and handle them
456 lda (tpi1),y ; Interrupt Register 6525
461 cmp #%00000001 ; ticker irq?
463 jsr scnkey ; Poll the keyboard
464 jsr UDTIM ; Bump the time
468 irqend: ldy #TPI::AIR
469 sta (tpi1),y ; Clear interrupt
482 ; -------------------------------------------------------------------------