]> git.sur5r.net Git - cc65/blobdiff - libsrc/atari/crt0.s
The spans do now contain the size of a span, no longer the end offset.
[cc65] / libsrc / atari / crt0.s
index ca4a095d30f7f7ffa9714cd81a98e59942a0a28d..4542d8945ecd89d891711a64fb225628fd2c7b45 100644 (file)
@@ -5,62 +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,getfd
-       .import         __CODE_LOAD__, __BSS_LOAD__
-       .import         __graphmode_used
-
-       .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
-
-
-.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
-fntemp:                .res    2       ; Pointer to file name
-regbank:       .res    6       ; 6 byte register bank
+       .export         __STARTUP__ : absolute = 1      ; Mark as startup
 
-zpspace        = * - zpstart           ; Zero page space allocated
+       .import         initlib, donelib
+       .import         callmain, zerobss, callirq
+       .import         __INTERRUPTOR_COUNT__
+       .import         __STARTUP_LOAD__, __ZPSAVE_LOAD__
+       .import         __RESERVED_MEMORY__
 
-.code
+       .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).
@@ -70,103 +42,107 @@ zpspace   = * - zpstart           ; 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
 
-       jsr     getmemtop       ; adjust for graphics mode to use
+       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
+       sta     sp+1                    ; setup runtime stack part 2
 
-       sta     sp
-       sta     APPMHI
-       stx     sp+1            ; Set argument stack ptr
-       stx     APPMHI+1
+; 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
 
-; set left margin to 0
+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
 
-; set stdio stream handles
+       jsr     callmain
 
-       lda     #0
-       jsr     getfd
-       sta     __filetab               ; setup stdin
-       lda     #0
-       jsr     getfd
-       sta     __filetab + 2           ; setup stdout
-       lda     #0
-       jsr     getfd
-       sta     __filetab + 4           ; setup stderr
+; 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
@@ -175,100 +151,41 @@ _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
 
-; *** end of main startup code
+; ------------------------------------------------------------------------
+; 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
 
-; calc. upper memory limit to use
+; ------------------------------------------------------------------------
+; Data
 
-.proc  getmemtop
+.data
 
-       ldy     __graphmode_used
-       beq     ignore          ; mode 0 doesn't need adjustment
-       cpy     #32
-       bcs     ignore          ; invalid value
+IRQInd: jmp    $0000
 
-       tya
-       asl     a
-       tay
-       lda     MEMTOP
-       sec
-       sbc     grmemusage,y
-       pha
-       lda     MEMTOP+1
-       sbc     grmemusage+1,y
-       tax
-       pla
-       rts
+; *** end of main startup code
 
-ignore:        lda     MEMTOP
-       ldx     MEMTOP+1
-       rts
+.segment       "ZPSAVE"
 
-.endproc
-
-       .data
-
-zpsave:        .res    zpspace
-
-; memory usage of the different graphics modes (0-31)
-; values < 0 of "bytes needed" are mappped to 0
-;               bytes needed    ; mode ; val. of MEMTOP
-grmemusage:
-       .word   0               ; 0     ; 39967
-       .word   0 ;-318         ; 1     ; 40285
-       .word   0 ;-568         ; 2     ; 40535
-       .word   0 ;-558         ; 3     ; 40525
-       .word   0 ;-298         ; 4     ; 40265
-       .word   182             ; 5     ; 39785
-       .word   1182            ; 6     ; 38785
-       .word   3198            ; 7     ; 36769
-       .word   7120            ; 8     ; 32847
-       .word   7146            ; 9     ; 32821
-       .word   7146            ; 10    ; 32821
-       .word   7146            ; 11    ; 32821
-       .word   162             ; 12    ; 39805
-       .word   0 ;-328         ; 13    ; 40295
-       .word   3278            ; 14    ; 36689
-       .word   7120            ; 15    ; 32847
-       .word   0               ; 16    ; 39967
-       .word   0 ;-320         ; 17    ; 40287
-       .word   0 ;-572         ; 18    ; 40539
-       .word   0 ;-560         ; 19    ; 40527
-       .word   0 ;-296         ; 20    ; 40263
-       .word   184             ; 21    ; 39783
-       .word   1192            ; 22    ; 38775
-       .word   3208            ; 23    ; 36759
-       .word   7146            ; 24    ; 32821
-       .word   7146            ; 25    ; 32821
-       .word   7146            ; 26    ; 32821
-       .word   7146            ; 27    ; 32821
-       .word   160             ; 28    ; 39807
-       .word   0 ;-332         ; 29    ; 40299
-       .word   3304            ; 30    ; 36663
-       .word   7146            ; 31    ; 32821
-
-; the program used to get these values (Atari BASIC):
-;  100 FILE=0
-;  110 IF FILE=1 THEN OPEN #1,8,0,"D:FREEMEM.OUT"
-;  120 IF FILE<>1 THEN OPEN #1,8,0,"E:"
-;  200 DIM G(32)
-;  210 FOR I=0 TO 32:GRAPHICS I:GOSUB 1000:G(I)=VAL:NEXT I
-;  220 GRAPHICS 0
-;  230 FOR I=0 TO 31:PRINT #1;I;":",G(I);" - ";G(0)-G(I):NEXT I
-;  240 CLOSE #1
-;  999 END
-;  1000 VAL=PEEK(741)+256*PEEK(742)
-;  1010 RETURN
+zpsave: .res   zpspace
 
        .bss
 
@@ -278,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