;
; Startup code for cc65 (Vic20 version)
-;
-; This must be the *first* file on the linker command line
;
- .export _exit
- .import initlib, donelib
- .import zerobss, push0
- .import _main
+ .export _exit
+ .export __STARTUP__ : absolute = 1 ; Mark as startup
+ .import initlib, donelib
+ .import zerobss, push0
+ .import callmain
.import RESTOR, BSOUT, CLRCH
- .import __RAM_START__, __RAM_SIZE__ ; Linker generated
+ .import __MAIN_START__, __MAIN_SIZE__ ; Linker generated
+ .import __STACKSIZE__ ; Linker generated
+ .importzp ST
.include "zeropage.inc"
- .include "vic20.inc"
- .include "../cbm/cbm.inc"
+ .include "vic20.inc"
; ------------------------------------------------------------------------
-; BASIC header with a SYS call
-
-.segment "EXEHDR"
- .word Head ; Load address
-Head: .word @Next
- .word 1000 ; Line number
- .byte $9E ; SYS token
- .byte <(((@Start / 1000) .mod 10) + $30)
- .byte <(((@Start / 100) .mod 10) + $30)
- .byte <(((@Start / 10) .mod 10) + $30)
- .byte <(((@Start / 1) .mod 10) + $30)
- .byte $00 ; End of BASIC line
-@Next: .word 0 ; BASIC end marker
-@Start:
+; Startup code
-; ------------------------------------------------------------------------
-; Actual code
+.segment "STARTUP"
-.code
+Start:
- ldx #zpspace-1
-L1: lda sp,x
- sta zpsave,x ; Save the zero page locations we need
- dex
- bpl L1
+; Save the zero-page locations that we need.
-; Close open files
+ ldx #zpspace-1
+L1: lda sp,x
+ sta zpsave,x
+ dex
+ bpl L1
- jsr CLRCH
+; Switch to the second charset.
-; Switch to second charset
+ lda #14
+ jsr BSOUT
- lda #14
- jsr BSOUT
+; Clear the BSS data.
-; Clear the BSS data
+ jsr zerobss
- jsr zerobss
+; Save some system stuff; and, set up the stack.
-; Save system stuff and setup the stack
+ tsx
+ stx spsave ; Save the system stack ptr
- tsx
- stx spsave ; Save the system stack ptr
+ lda #<(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
+ ldx #>(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
+ sta sp
+ stx sp+1 ; Set argument stack ptr
- lda #<(__RAM_START__ + __RAM_SIZE__)
- sta sp
- lda #>(__RAM_START__ + __RAM_SIZE__)
- sta sp+1 ; Set argument stack ptr
+; Call the module constructors.
-; Call module constructors
+ jsr initlib
- jsr initlib
+; Push the command-line arguments; and, call main().
-; Pass an empty command line
+ jsr callmain
- jsr push0 ; argc
- jsr push0 ; argv
+; Back from main() [this is also the exit() entry]. Run the module destructors.
- ldy #4 ; Argument size
- jsr _main ; call the users code
+_exit: pha ; Save the return code on stack
+ jsr donelib
-; Call module destructors. This is also the _exit entry.
+; Copy back the zero-page stuff.
-_exit: jsr donelib ; Run module destructors
+ ldx #zpspace-1
+L2: lda zpsave,x
+ sta sp,x
+ dex
+ bpl L2
-; Restore system stuff
+; Place the program return code into BASIC's status variable.
- ldx spsave
- txs ; Restore stack pointer
+ pla
+ sta ST
-; Copy back the zero page stuff
+; Restore the stack pointer.
- ldx #zpspace-1
-L2: lda zpsave,x
- sta sp,x
- dex
- bpl L2
+ ldx spsave
+ txs
-; Reset changed vectors, back to basic
+; Back to BASIC.
- jmp RESTOR
+ rts
+; ------------------------------------------------------------------------
-.data
+.segment "INIT"
-zpsave: .res zpspace
+zpsave: .res zpspace
+
+; ------------------------------------------------------------------------
.bss
-spsave: .res 1
+spsave: .res 1