;
-; Startup code for cc65 (Apple2 version)
+; Oliver Schmidt, 2009-09-15
;
-; This must be the *first* file on the linker command line
+; Startup code for cc65 (Apple2 version)
;
- .export _exit, __Exit
- .import zerobss
- .import initlib, donelib
- .import callmain, callirq
- .import __STARTUP_LOAD__, __BSS_LOAD__ ; Linker generated
- .import __INTERRUPTOR_COUNT__ ; Linker generated
-
- .include "zeropage.inc"
- .include "apple2.inc"
- .include "mli.inc"
-
-; ------------------------------------------------------------------------
+ .export _exit, done, return
+ .export __STARTUP__ : absolute = 1 ; Mark as startup
- .segment "EXEHDR"
+ .import initlib, donelib
+ .import zerobss, callmain
+ .import __ONCE_LOAD__, __ONCE_SIZE__ ; Linker generated
+ .import __LC_START__, __LC_LAST__ ; Linker generated
- .addr __STARTUP_LOAD__ ; Start address
- .word __BSS_LOAD__ - __STARTUP_LOAD__ ; Size
+ .include "zeropage.inc"
+ .include "apple2.inc"
; ------------------------------------------------------------------------
- .segment "STARTUP"
+ .segment "STARTUP"
; ProDOS TechRefMan, chapter 5.2.1:
; "For maximum interrupt efficiency, a system program should not
; use more than the upper 3/4 of the stack."
- ldx #$FF
- txs ; Init stack pointer
-
- ; Delegate all further processing to keep STARTUP small
- jsr init
-
- ; Avoid re-entrance of donelib. This is also the _exit entry
-_exit: ldx #<__Exit
- lda #>__Exit
- jsr reset ; Setup RESET vector
-
- ; Check for valid interrrupt vector table entry number
- lda intnum
- beq :+
-
- ; Deallocate interrupt vector table entry
- dec params ; Adjust parameter count
- jsr ENTRY
- .byte $41 ; Dealloc interrupt
- .addr params
-
- ; Call module destructors
-: jsr donelib
-
- ; Restore the original RESET vector. This is also the __Exit entry
-__Exit: ldx #$02
-: lda rvsave,x
- sta SOFTEV,x
+ ldx #$FF
+ txs ; Init stack pointer
+
+ ; Save space by putting some of the start-up code in the ONCE segment,
+ ; which can be re-used by the BSS segment, the heap and the C stack.
+ jsr init
+
+ ; Clear the BSS data.
+ jsr zerobss
+
+ ; Push the command-line arguments; and, call main().
+ jsr callmain
+
+ ; Avoid a re-entrance of donelib. This is also the exit() entry.
+_exit: ldx #<exit
+ lda #>exit
+ jsr reset ; Setup RESET vector
+
+ ; Switch in ROM, in case it wasn't already switched in by a RESET.
+ bit $C082
+
+ ; Call the module destructors.
+ jsr donelib
+
+ ; Restore the original RESET vector.
+exit: ldx #$02
+: lda rvsave,x
+ sta SOFTEV,x
dex
- bpl :-
+ bpl :-
- ; Copy back the zero page stuff
- ldx #zpspace-1
-: lda zpsave,x
- sta sp,x
+ ; Copy back the zero-page stuff.
+ ldx #zpspace-1
+: lda zpsave,x
+ sta sp,x
dex
- bpl :-
+ bpl :-
; ProDOS TechRefMan, chapter 5.2.1:
; "System programs should set the stack pointer to $FF at the
; warm-start entry point."
- ldx #$FF
- txs ; Re-init stack pointer
+ ldx #$FF
+ txs ; Re-init stack pointer
- ; Back to DOS
- jmp DOSWARM
+ ; We're done
+ jmp done
; ------------------------------------------------------------------------
- .segment "INIT"
+ .segment "ONCE"
- ; Save the zero page locations we need
-init: ldx #zpspace-1
-: lda sp,x
- sta zpsave,x
+ ; Save the zero-page locations that we need.
+init: ldx #zpspace-1
+: lda sp,x
+ sta zpsave,x
dex
- bpl :-
+ bpl :-
- ; Save the original RESET vector
- ldx #$02
-: lda SOFTEV,x
- sta rvsave,x
+ ; Save the original RESET vector.
+ ldx #$02
+: lda SOFTEV,x
+ sta rvsave,x
dex
- bpl :-
+ bpl :-
- ; ProDOS TechRefMan, chapter 5.3.5:
- ; "Your system program should place in the RESET vector the
- ; address of a routine that ... closes the files."
- ldx #<_exit
- lda #>_exit
- jsr reset ; Setup RESET vector
+ ; Check for ProDOS.
+ ldy $BF00 ; MLI call entry point
+ cpy #$4C ; Is MLI present? (JMP opcode)
+ bne basic
- ; Clear the BSS data
- jsr zerobss
+ ; Check the ProDOS system bit map.
+ lda $BF6F ; Protection for pages $B8 - $BF
+ cmp #%00000001 ; Exactly system global page is protected
+ bne basic
- ; Setup the stack
- lda HIMEM
- sta sp
- lda HIMEM+1
- sta sp+1 ; Set argument stack ptr
+ ; No BASIC.SYSTEM; so, quit to the ProDOS dispatcher instead.
+ lda #<quit
+ ldx #>quit
+ sta done+1
+ stx done+2
- ; Call module constructors
- jsr initlib
+ ; No BASIC.SYSTEM; so, use the addr of the ProDOS system global page.
+ lda #<$BF00
+ ldx #>$BF00
+ bne :+ ; Branch always
- ; Check for interruptors
- lda #<__INTERRUPTOR_COUNT__
- beq :+
+ ; Get the highest available mem addr from the BASIC interpreter.
+basic: lda HIMEM
+ ldx HIMEM+1
- ; Check for ProDOS
- lda ENTRY
- cmp #$4C ; Is MLI present? (JMP opcode)
- bne :+
+ ; Set up the C stack.
+: sta sp
+ stx sp+1
- ; Allocate interrupt vector table entry
- jsr ENTRY
- .byte $40 ; Alloc interrupt
- .addr params
-
- ; Push arguments and call main()
-: jmp callmain
+ ; ProDOS TechRefMan, chapter 5.3.5:
+ ; "Your system program should place in the RESET vector the
+ ; address of a routine that ... closes the files."
+ ldx #<_exit
+ lda #>_exit
+ jsr reset ; Setup RESET vector
+
+ ; Call the module constructors.
+ jsr initlib
+
+ ; Switch in LC bank 2 for W/O.
+ bit $C081
+ bit $C081
+
+ ; Set the source start address.
+ ; Aka __LC_LOAD__ iff segment LC exists.
+ lda #<(__ONCE_LOAD__ + __ONCE_SIZE__)
+ ldy #>(__ONCE_LOAD__ + __ONCE_SIZE__)
+ sta $9B
+ sty $9C
+
+ ; Set the source last address.
+ ; Aka __LC_LOAD__ + __LC_SIZE__ iff segment LC exists.
+ lda #<((__ONCE_LOAD__ + __ONCE_SIZE__) + (__LC_LAST__ - __LC_START__))
+ ldy #>((__ONCE_LOAD__ + __ONCE_SIZE__) + (__LC_LAST__ - __LC_START__))
+ sta $96
+ sty $97
+
+ ; Set the destination last address.
+ ; Aka __LC_RUN__ + __LC_SIZE__ iff segment LC exists.
+ lda #<__LC_LAST__
+ ldy #>__LC_LAST__
+ sta $94
+ sty $95
+
+ ; Call into Applesoft Block Transfer Up -- which handles zero-
+ ; sized blocks well -- to move the content of the LC memory area.
+ jsr $D39A ; BLTU2
+
+ ; Switch in LC bank 2 for R/O and return.
+ bit $C080
+ rts
; ------------------------------------------------------------------------
- .segment "LOWCODE"
-
- ; ProDOS TechRefMan, chapter 6.2:
- ; "Each installed routine must begin with a CLD instruction"
-intrpt: cld
+ .code
- ; Call interruptors
- jsr callirq
+ ; Set up the RESET vector.
+reset: stx SOFTEV
+ sta SOFTEV+1
+ eor #$A5
+ sta PWREDUP
+return: rts
- ; ProDOS TechRefMan, chapter 6.2:
- ; "When the routine that can process the interrupt is called, it
- ; should ... return (via an RTS) with the carry flag clear."
- clc
- rts
+ ; Quit to the ProDOS dispatcher.
+quit: jsr $BF00 ; MLI call entry point
+ .byte $65 ; Quit
+ .word q_param
; ------------------------------------------------------------------------
- .code
+ .rodata
- ; Setup RESET vector
-reset: stx SOFTEV
- sta SOFTEV+1
- eor #$A5
- sta PWREDUP
- rts
+ ; MLI parameter list for quit
+q_param:.byte $04 ; param_count
+ .byte $00 ; quit_type
+ .word $0000 ; reserved
+ .byte $00 ; reserved
+ .word $0000 ; reserved
; ------------------------------------------------------------------------
.data
-zpsave: .res zpspace
+ ; Final jump when we're done
+done: jmp DOSWARM ; Potentially patched at runtime
+
+; ------------------------------------------------------------------------
-rvsave: .res 3
+ .segment "INIT"
-params: .byte $02 ; Parameter count
-intnum: .byte $00 ; Interrupt number
- .addr intrpt ; Interrupt handler
+zpsave: .res zpspace
+rvsave: .res 3