]> git.sur5r.net Git - cc65/blobdiff - libsrc/common/atexit.s
atari5200: name conio constructor 'initconio'
[cc65] / libsrc / common / atexit.s
index 5d94d36fe7d541eb5a83cd55435036396d9be7e2..6edfd4a5dc220806c9a69191de704d68dceab611 100644 (file)
@@ -4,66 +4,81 @@
 ; int atexit (void (*f) (void));
 ;
 
-; The exit functions
+        .export         _atexit
+        .destructor     doatexit, 17
+        .import         callax
 
-       .export         _atexit, doatexit
-       .import         __errno, jmpvec
+        .include        "errno.inc"
 
-.bss
-ecount:        .byte   0               ; Really an index, inc'ed by 2
-efunc:         .word   0,0,0,0,0       ; 5 exit functions
-maxcount = * - efunc
+        .macpack        cpu
 
+; ---------------------------------------------------------------------------
 
-.code
+.proc   _atexit
 
-_atexit:
-       ldy     ecount
-       cpy     #maxcount       ; slot available?
-       beq     E0              ; jump if no
+        ldy     exitfunc_index
+        cpy     #exitfunc_max           ; Slot available?
+        beq     @Error                  ; Jump if no
 
 ; Enter the function into the table
 
-       sta     efunc,y
-       iny
-       txa
-       sta     efunc,y
-       iny
-       sty     ecount
+        sta     exitfunc_table,y
+        iny
+        txa
+        sta     exitfunc_table,y
+        iny
+        sty     exitfunc_index
 
 ; Done, return zero
 
-       lda     #0
-       tax
-       rts
+        lda     #0
+        tax
+        rts
 
 ; Error, no space left
 
-E0:    lda     #$FF
-               sta     __errno         ; Use -1 until codes are defined ###
-       sta     __errno+1
-       tax
-       rts
+@Error: lda     #ENOSPC         ; No space left
+        jsr     __seterrno
+        ldx     #$FF            ; Return -1
+        txa
+        rts
+
+.endproc
 
-; Function called from exit
 
-doatexit:
-       ldy     ecount          ; get index
-       beq     L9              ; jump if done
-       dey
-       lda     efunc,y
-       sta     jmpvec+2
-       dey
-       lda     efunc,y
-       sta     jmpvec+1
-       sty     ecount
-       ldy     #0              ; number of function parms
-       jsr     jmpvec
-       jmp     doatexit        ; next one
 
-L9:    rts
+; ---------------------------------------------------------------------------
+
+.code
+
+.proc   doatexit
 
+        ldy     exitfunc_index          ; Get index
+        beq     @L9                     ; Jump if done
+        dey
+        lda     exitfunc_table,y
+        tax
+        dey
+        lda     exitfunc_table,y
+        sty     exitfunc_index
+        jsr     callax                  ; Call the function
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        bra     doatexit
+.else
+        jmp     doatexit                ; Next one
+.endif
 
+@L9:    rts
 
+.endproc
+
+
+
+; ---------------------------------------------------------------------------
+
+.bss
+exitfunc_index: .res    1       ; Index into table, inc'ed by 2
+exitfunc_table: .res    10      ; 5 exit functions
+exitfunc_max    = <(* - exitfunc_table)