]> git.sur5r.net Git - cc65/blobdiff - libsrc/atari/crt0.s
Remove the ",r" spec
[cc65] / libsrc / atari / crt0.s
index 66222d2489a55e72bbb51b8b885a32003da44936..95e5cac0e82d5dfb09341e5b3527a1605e8dfb14 100644 (file)
 ;
 
        .export         _exit
+       .constructor    initsp,26
+
        .import         getargs, argc, argv
-       .import         __hinit, initconio, zerobss, pushax, doatexit
+       .import         initlib, donelib
+               .import         zerobss, pushax
        .import         _main,__filetab,getfd
        .import         __CODE_LOAD__, __BSS_LOAD__
 
+        .include        "zeropage.inc"
        .include        "atari.inc"
-
-; ------------------------------------------------------------------------
-; Define and export the ZP variables for the runtime
-
-       .exportzp       sp, sreg, regsave
-       .exportzp       ptr1, ptr2, ptr3, ptr4
-       .exportzp       tmp1, tmp2, tmp3, tmp4
-       .exportzp       fntemp, regbank, zpspace
-
-sp     = $D2           ; (2bytes) stack pointer
-sreg   = $D4           ; (2bytes) secondary register/high 16 bit for longs
-regsave = $D6          ; (4bytes) slot to save/restore (E)AX into
-ptr1   = $DA           ; (2bytes)
-ptr2   = $DC           ; (2bytes)
-ptr3   = $DE           ; (2bytes)
-ptr4   = $E0           ; (2bytes)
-tmp1   = $E2           ; (1byte)
-tmp2   = $E3           ; (1byte)
-tmp3   = $E4           ; (1byte)
-tmp4   = $E5           ; (1byte)
-fntemp = $E6           ; (2bytes) pointer to file name
-regbank = $E8          ; (6bytes) 6 byte register bank
-zpspace = $EE - sp     ; Zero page space allocated
+        .include        "_file.inc"
 
 ; ------------------------------------------------------------------------
 ; EXE header
@@ -47,12 +29,12 @@ zpspace = $EE - sp  ; Zero page space allocated
        .word   $FFFF
        .word   __CODE_LOAD__
        .word   __BSS_LOAD__ - 1
-       .code
-       .reloc
 
 ; ------------------------------------------------------------------------
 ; Actual code
 
+.code
+
        rts     ; fix for SpartaDOS / OS/A+
                ; they first call the entry point from AUTOSTRT and
                ; then the load addess (this rts here).
@@ -62,10 +44,10 @@ zpspace = $EE - sp  ; Zero page space allocated
 
 ; Save the zero page locations we need
 
-       ldy     #zpspace-1
-L1:    lda     sp,y
-       sta     zpsave,y
-       dey
+               ldx     #zpspace-1
+L1:    lda     sp,x
+       sta     zpsave,x
+       dex
        bpl     L1
 
 ; Clear the BSS data
@@ -77,20 +59,22 @@ L1: lda     sp,y
        tsx
        stx     spsave
 
-; report memory usage and initialize stack pointer
+; report memory usage
 
        lda     APPMHI
-       sta     appmsav
+       sta     appmsav                 ; remember old APPMHI value
        lda     APPMHI+1
        sta     appmsav+1
-       
-       lda     #<$8000
-       sta     sp
-       sta     APPMHI
-       lda     #>$8000
-       sta     sp+1            ; Set argument stack ptr
-       sta     APPMHI+1
-       
+
+       lda     MEMTOP
+       sta     APPMHI                  ; initialize our APPMHI value
+       ldx     MEMTOP+1
+       stx     APPMHI+1
+
+; Call module constructors
+
+       jsr     initlib
+
 ; set left margin to 0
 
        lda     LMARGN
@@ -103,15 +87,9 @@ L1: lda     sp,y
        ldx     SHFLOK
        stx     old_shflok
        sta     SHFLOK
-       
-; Initialize the heap
-
-       jsr     __hinit
 
 ; Initialize conio stuff
 
-       jsr     initconio
-
        lda     #$FF
        sta     CH
 
@@ -119,13 +97,13 @@ L1:        lda     sp,y
 
        lda     #0
        jsr     getfd
-       sta     __filetab               ; setup stdin
+               sta     __filetab + (0 * _FILE_size)    ; setup stdin
        lda     #0
        jsr     getfd
-       sta     __filetab + 2           ; setup stdout
+       sta     __filetab + (1 * _FILE_size)    ; setup stdout
        lda     #0
        jsr     getfd
-       sta     __filetab + 4           ; setup stderr
+       sta     __filetab + (2 * _FILE_size)    ; setup stderr
 
 ; Pass command line if present
 
@@ -133,20 +111,22 @@ L1:       lda     sp,y
 
        lda     argc
        ldx     argc+1
-       jsr     pushax          ; argc
+       jsr     pushax          ; argc
        lda     #<argv
        ldx     #>argv
-       jsr     pushax          ; argv
+       jsr     pushax          ; argv
+
+       ldy     #4              ; Argument size
+       jsr     _main           ; call the users code
 
-       ldy     #4              ; Argument size
-       jsr     _main           ; call the users code
+; Call module destructors. This is also the _exit entry.
 
-; fall thru to exit...
+_exit: jsr     donelib         ; Run module destructors
 
-_exit: jsr     doatexit        ; call exit functions
+; Restore system stuff
 
        ldx     spsave
-       txs                     ; Restore stack pointer
+       txs                     ; Restore stack pointer
 
 ; restore left margin
 
@@ -159,7 +139,7 @@ _exit:      jsr     doatexit        ; call exit functions
        sta     SHFLOK
 
 ; restore APPMHI
-       
+
        lda     appmsav
        sta     APPMHI
        lda     appmsav+1
@@ -167,21 +147,32 @@ _exit:    jsr     doatexit        ; call exit functions
 
 ; Copy back the zero page stuff
 
-       ldy     #zpspace-1
-L2:    lda     zpsave,y
-       sta     sp,y
-       dey
+       ldx     #zpspace-1
+L2:    lda     zpsave,x
+       sta     sp,x
+       dex
        bpl     L2
 
 ; Back to DOS
 
        rts
 
-.data
+; *** end of main startup code
+
+; setup sp
+
+initsp:
+       lda     APPMHI
+       sta     sp
+       lda     APPMHI+1
+       sta     sp+1
+       rts
+
+       .data
 
 zpsave:        .res    zpspace
-       
-.bss
+
+       .bss
 
 spsave:                .res    1
 appmsav:       .res    1