]> git.sur5r.net Git - cc65/blob - libsrc/common/atexit.s
Added a C header that translates from the source file's encoding to PetSCII.
[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, 17
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         jsr     __seterrno
42         ldx     #$FF            ; Return -1
43         txa
44         rts
45
46 .endproc
47
48
49
50 ; ---------------------------------------------------------------------------
51
52 .code
53
54 .proc   doatexit
55
56         ldy     exitfunc_index          ; Get index
57         beq     @L9                     ; Jump if done
58         dey
59         lda     exitfunc_table,y
60         tax
61         dey
62         lda     exitfunc_table,y
63         sty     exitfunc_index
64         jsr     callax                  ; Call the function
65 .if (.cpu .bitand ::CPU_ISET_65SC02)
66         bra     doatexit
67 .else
68         jmp     doatexit                ; Next one
69 .endif
70
71 @L9:    rts
72
73 .endproc
74
75
76
77 ; ---------------------------------------------------------------------------
78
79 .bss
80 exitfunc_index: .res    1       ; Index into table, inc'ed by 2
81 exitfunc_table: .res    10      ; 5 exit functions
82 exitfunc_max    = <(* - exitfunc_table)
83
84