]> git.sur5r.net Git - cc65/blobdiff - libsrc/atari/crt0.s
Added the telldir() function.
[cc65] / libsrc / atari / crt0.s
index 39235e013c7ad9e0f4927b73ff14e16997b2959f..4542d8945ecd89d891711a64fb225628fd2c7b45 100644 (file)
@@ -5,54 +5,34 @@
 ;      Mark Keates
 ;      Freddy Offenga
 ;      Christian Groessler
-;
-; This must be the *first* file on the linker command line
+;      Stefan Haubenthal
 ;
 
        .export         _exit
-       .import         getargs, argc, argv
-       .import         __hinit, initconio, zerobss, pushax, doatexit
-       .import         _main,__filetab
-       .import         __CODE_LOAD__, __BSS_LOAD__
+       .export         __STARTUP__ : absolute = 1      ; Mark as startup
 
-       .include        "atari.inc"
+       .import         initlib, donelib
+       .import         callmain, zerobss, callirq
+       .import         __INTERRUPTOR_COUNT__
+       .import         __STARTUP_LOAD__, __ZPSAVE_LOAD__
+       .import         __RESERVED_MEMORY__
 
-; ------------------------------------------------------------------------
-; 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        "zeropage.inc"
+       .include        "atari.inc"
 
 ; ------------------------------------------------------------------------
 ; EXE header
 
        .segment "EXEHDR"
        .word   $FFFF
-       .word   __CODE_LOAD__
-       .word   __BSS_LOAD__ - 1
-       .code
-       .reloc
+       .word   __STARTUP_LOAD__
+       .word   __ZPSAVE_LOAD__ - 1
 
 ; ------------------------------------------------------------------------
 ; Actual code
 
+       .segment        "STARTUP"
+
        rts     ; fix for SpartaDOS / OS/A+
                ; they first call the entry point from AUTOSTRT and
                ; then the load addess (this rts here).
@@ -62,101 +42,108 @@ 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
 
        jsr     zerobss
 
-; setup the stack
+; Setup the stack
 
        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
+
+       sec
+       lda     MEMTOP
+       sbc     #<__RESERVED_MEMORY__
+       sta     APPMHI                  ; initialize our APPMHI value
+       sta     sp                      ; setup runtime stack part 1
+       lda     MEMTOP+1
+       sbc     #>__RESERVED_MEMORY__
        sta     APPMHI+1
-       
-; set left margin to 0
+       sta     sp+1                    ; setup runtime stack part 2
+
+; If we have IRQ functions, chain our stub into the IRQ vector
+
+       lda     #<__INTERRUPTOR_COUNT__
+       beq     NoIRQ1
+       lda     VVBLKI
+       ldx     VVBLKI+1
+       sta     IRQInd+1
+       stx     IRQInd+2
+       lda     #6
+       ldy     #<IRQStub
+       ldx     #>IRQStub
+       jsr     SETVBV
+
+; Call module constructors
+
+NoIRQ1:        jsr     initlib
+
+; Set left margin to 0
 
        lda     LMARGN
        sta     old_lmargin
-       lda     #0
-       sta     LMARGN
+       ldy     #0
+       sty     LMARGN
 
-; set keyb to upper/lowercase mode
+; Set keyb to upper/lowercase mode
 
        ldx     SHFLOK
        stx     old_shflok
-       sta     SHFLOK
-       
-; Initialize the heap
-
-       jsr     __hinit
+       sty     SHFLOK
 
 ; Initialize conio stuff
 
-       jsr     initconio
+       dey                             ; Set X to $FF
+       sty     CH
 
-       lda     #$FF
-       sta     CH
+; Push arguments and call main
 
-; ugly hack for now: set stdio stream handles
-; all to iocb #0
-; until we know where to go with fd<->iocb relation
-; this won't stay here!
+       jsr     callmain
 
-       lda     #0
-       sta     __filetab + 2
-       sta     __filetab + 4
+; Call module destructors. This is also the _exit entry.
 
-; Pass command line if present
+_exit: jsr     donelib         ; Run module destructors
 
-       jsr     getargs
+; Reset the IRQ vector if we chained it.
 
-       lda     argc
-       ldx     argc+1
-       jsr     pushax          ; argc
-       lda     #<argv
-       ldx     #>argv
-       jsr     pushax          ; argv
+       pha                     ; Save the return code on stack
+       lda     #<__INTERRUPTOR_COUNT__
+       beq     NoIRQ2
+       lda     #6
+       ldy     IRQInd+1
+       ldx     IRQInd+2
+       jsr     SETVBV
 
-       ldy     #4              ; Argument size
-       jsr     _main           ; call the users code
+; Restore system stuff
 
-; fall thru to exit...
-
-_exit: jsr     doatexit        ; call exit functions
-
-       ldx     spsave
+NoIRQ2:        ldx     spsave
        txs                     ; Restore stack pointer
 
-; restore left margin
+; Restore left margin
 
        lda     old_lmargin
        sta     LMARGN
 
-; restore kb mode
+; Restore kb mode
 
        lda     old_shflok
        sta     SHFLOK
 
-; restore APPMHI
-       
+; Restore APPMHI
+
        lda     appmsav
        sta     APPMHI
        lda     appmsav+1
@@ -164,21 +151,43 @@ _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
 
+; Turn on cursor
+
+       inx
+       stx     CRSINH
+
 ; Back to DOS
 
        rts
 
+; ------------------------------------------------------------------------
+; The IRQ vector jumps here, if condes routines are defined with type 2.
+
+IRQStub:
+       cld                             ; Just to be sure
+       jsr     callirq                 ; Call the functions
+       jmp     IRQInd                  ; Jump to the saved IRQ vector
+
+; ------------------------------------------------------------------------
+; Data
+
 .data
 
-zpsave:        .res    zpspace
-       
-.bss
+IRQInd: jmp    $0000
+
+; *** end of main startup code
+
+.segment       "ZPSAVE"
+
+zpsave: .res   zpspace
+
+       .bss
 
 spsave:                .res    1
 appmsav:       .res    1
@@ -186,6 +195,6 @@ old_shflok: .res    1
 old_lmargin:   .res    1
 
        .segment "AUTOSTRT"
-       .word   $02E0
-       .word   $02E1
-       .word   __CODE_LOAD__ + 1
+       .word   RUNAD                   ; defined in atari.h
+       .word   RUNAD+1
+       .word   __STARTUP_LOAD__ + 1