]> git.sur5r.net Git - cc65/blobdiff - libsrc/c128/crt0.s
Added support for building 'extra' object files.
[cc65] / libsrc / c128 / crt0.s
index eaad29ed45a06e3231f9bba9723b6f921d56ec7b..4315947e1a8c37d9ae98e2045c08f8c3a62edf9e 100644 (file)
@@ -1,80 +1,48 @@
 ;
 ; Startup code for cc65 (C128 version)
-;
-; This must be the *first* file on the linker command line
 ;
 
        .export         _exit
-       .import         __hinit, initconio, doneconio, zerobss
-       .import         push0, doatexit, _main
-
+       .export         __STARTUP__ : absolute = 1      ; Mark as startup
+       .import         initlib, donelib
+       .import         zerobss
+       .import         push0, callmain
+       .import         RESTOR, BSOUT, CLRCH
+       .import         __RAM_START__, __RAM_SIZE__, __STACKSIZE__
+       .importzp       ST
+
+       .include        "zeropage.inc"
        .include        "c128.inc"
-       .include        "../cbm/cbm.inc"
-
-; ------------------------------------------------------------------------
-; Define and export the ZP variables for the C64 runtime
-
-       .exportzp       sp, sreg, regsave
-       .exportzp       ptr1, ptr2, ptr3, ptr4
-       .exportzp       tmp1, tmp2, tmp3, tmp4
-       .exportzp       regbank, zpspace
-
-.zeropage
 
-zpstart        = *
-sp:            .res    2       ; Stack pointer
-sreg:          .res    2       ; Secondary register/high 16 bit for longs
-regsave:       .res    2       ; slot to save/restore (E)AX into
-ptr1:          .res    2
-ptr2:          .res    2
-ptr3:          .res    2
-ptr4:          .res    2
-tmp1:          .res    1
-tmp2:          .res    1
-tmp3:          .res    1
-tmp4:          .res    1
-regbank:       .res    6       ; 6 byte register bank
-
-zpspace        = * - zpstart           ; Zero page space allocated
-
-.code
 
 ; ------------------------------------------------------------------------
-; BASIC header with a SYS call
-
-       .org    $1BFF
-        .word   Head            ; Load address
-Head:   .word   @Next
-        .word   1000            ; Line number
-        .byte   $9E,"7181"      ; SYS 7181
-        .byte   $00             ; End of BASIC line
-@Next:  .word   0               ; BASIC end marker
-       .reloc
+; Startup code
 
-; ------------------------------------------------------------------------
-; Actual code
+.segment       "STARTUP"
 
-       ldy     #zpspace-1
-L1:    lda     sp,y
-       sta     zpsave,y        ; save the zero page locations we need
-       dey
-               bpl     L1
+Start:
 
-; Close open files
+; Switch to the second charset
 
-       jsr     CLRCH
+       lda     #14
+       jsr     BSOUT
 
-; Switch to second charset
+; Before doing anything else, we have to setup our banking configuration.
+; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
+; to the underlying RAM works, but it is bad style.
 
-       lda     #14
-       jsr     BSOUT
+       lda     MMU_CR          ; Get current memory configuration...
+       pha                     ; ...and save it for later
+       lda     #MMU_CFG_CC65   ; Bank0 with kernal ROM
+       sta     MMU_CR
 
-; Get the current MMU setting and save it. Set new memory config.
+; Save the zero page locations we need
 
-       lda     MMU_CR          ; Get current memory configuration...
-               pha                     ; ...and save it for later
-       lda     #$0E            ; Bank0 with kernal ROM
-       sta     MMU_CR
+       ldx     #zpspace-1
+L1:    lda     sp,x
+       sta     zpsave,x
+       dex
+       bpl     L1
 
 ; Clear the BSS data
 
@@ -85,63 +53,68 @@ L1: lda     sp,y
        pla                     ; Get MMU setting
        sta     mmusave
 
-               tsx
-               stx     spsave          ; save system stk ptr
+       tsx
+       stx     spsave          ; Save the system stack pointer
 
-       lda     #<$C000
-       sta     sp
-       lda     #>$C000
-       sta     sp+1
+       lda     #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
+       sta     sp
+       lda     #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
+       sta     sp+1            ; Set argument stack ptr
 
-; Initialize the heap
+; Call module constructors
 
-       jsr     __hinit
+       jsr     initlib
 
-; Initialize conio stuff
+; Set the bank for the file name to our execution bank. We must do this,
+; *after* calling constructors, because some of them may depend on the
+; original value of this register.
 
-       jsr     initconio
+       lda     #0
+       sta     FNAM_BANK
 
-; Pass an empty command line
+; Push arguments and call main()
 
-       jsr     push0           ; argc
-       jsr     push0           ; argv
+       jsr     callmain
 
-       ldy     #4              ; Argument size
-               jsr     _main           ; call the users code
+; Back from main (this is also the _exit entry). Run module destructors
 
-; fall thru to exit...
+_exit:  pha                    ; Save the return code on stack
+       jsr     donelib
 
-_exit: jsr     doatexit        ; call exit functions
+; Copy back the zero page stuff
 
-; Reset the conio stuff
+       ldx     #zpspace-1
+L2:    lda     zpsave,x
+       sta     sp,x
+       dex
+       bpl     L2
 
-       jsr     doneconio
+; Place the program return code into ST
 
-; Reset stack and the MMU
+       pla
+       sta     ST
 
-       ldx     spsave          ; Patched at runtime
-       txs
-               lda     mmusave         ; Patched at runtime
-       sta     MMU_CR
+; Reset the stack and the memory configuration
 
-; Copy back the zero page stuff
+       ldx     spsave
+       txs
+       ldx     mmusave
+       stx     MMU_CR
 
-       ldy     #zpspace-1
-L2:    lda     zpsave,y
-       sta     sp,y
-       dey
-       bpl     L2
+; Done, return to BASIC
 
-; Done
+       rts
 
-       jmp     RESTOR
+; ------------------------------------------------------------------------
+; Data
+
+.segment       "ZPSAVE"
 
-.data
 zpsave:        .res    zpspace
 
+; ------------------------------------------------------------------------
+
 .bss
+
 spsave:        .res    1
 mmusave:.res   1
-
-
-