]> git.sur5r.net Git - cc65/blob - libsrc/common/atexit.s
New loadable mouse drivers
[cc65] / libsrc / common / atexit.s
1 ;
2 ; Ullrich von Bassewitz, 06.06.1998
3 ;
4 ; int atexit (void (*f) (void));
5 ;
6
7         .export         _atexit
8         .destructor     doatexit, 5
9         .import         callax
10
11         .include        "errno.inc"
12
13         .macpack        cpu
14
15 ; ---------------------------------------------------------------------------
16
17 .proc   _atexit
18
19         ldy     exitfunc_index
20         cpy     #exitfunc_max           ; Slot available?
21         beq     @Error                  ; Jump if no
22
23 ; Enter the function into the table
24
25         sta     exitfunc_table,y
26         iny
27         txa
28         sta     exitfunc_table,y
29         iny
30         sty     exitfunc_index
31
32 ; Done, return zero
33
34         lda     #0
35         tax
36         rts
37
38 ; Error, no space left
39
40 @Error: lda     #ENOSPC         ; No space left
41         sta     __errno
42         ldx     #$00
43         stx     __errno+1
44         dex                     ; Make return value -1
45         txa
46         rts
47
48 .endproc
49
50
51
52 ; ---------------------------------------------------------------------------
53
54 .code
55
56 .proc   doatexit
57
58         ldy     exitfunc_index          ; Get index
59         beq     @L9                     ; Jump if done
60         dey
61         lda     exitfunc_table,y
62         tax
63         dey
64         lda     exitfunc_table,y
65         sty     exitfunc_index
66         jsr     callax                  ; Call the function
67 .if (.cpu .bitand ::CPU_ISET_65SC02)
68         bra     doatexit
69 .else
70         jmp     doatexit                ; Next one
71 .endif
72
73 @L9:    rts
74
75 .endproc
76
77
78
79 ; ---------------------------------------------------------------------------
80
81 .bss
82 exitfunc_index: .res    1       ; Index into table, inc'ed by 2
83 exitfunc_table: .res    10      ; 5 exit functions
84 exitfunc_max    = <(* - exitfunc_table)
85
86