]> git.sur5r.net Git - cc65/blob - libsrc/runtime/condes.s
Two fixes from Stefan Haubenthal
[cc65] / libsrc / runtime / condes.s
1 ;
2 ; Ullrich von Bassewitz, 20.11.2000
3 ;
4 ; CC65 runtime: Support for calling module constructors/destructors
5 ;
6 ; The condes routine must be called with the table address in a/x and the
7 ; size of the table in y. The current implementation limits the table size
8 ; to 254 bytes (127 vectors) but this shouldn't be problem for now and may
9 ; be changed later.
10 ;
11 ; libinit and libdone call condes with the predefined module constructor and
12 ; destructor tables, they must be called from the platform specific startup
13 ; code.
14
15
16         .export initlib, donelib, condes
17
18         .import __CONSTRUCTOR_TABLE__, __CONSTRUCTOR_COUNT__
19         .import __DESTRUCTOR_TABLE__, __DESTRUCTOR_COUNT__
20
21         .macpack        cpu
22
23 .code
24
25 ; --------------------------------------------------------------------------
26 ; Initialize library modules
27
28 .proc   initlib
29
30         lda     #<__CONSTRUCTOR_TABLE__
31         ldx     #>__CONSTRUCTOR_TABLE__
32         ldy     #<(__CONSTRUCTOR_COUNT__*2)
33         bne     condes
34         rts
35
36 .endproc
37
38
39 ; --------------------------------------------------------------------------
40 ; Cleanup library modules
41
42 .proc   donelib
43
44         lda     #<__DESTRUCTOR_TABLE__
45         ldx     #>__DESTRUCTOR_TABLE__
46         ldy     #<(__DESTRUCTOR_COUNT__*2)
47         bne     condes
48         rts
49
50 .endproc
51
52
53 ; --------------------------------------------------------------------------
54 ; Generic table call handler. We cannot use callax here, since condes is also
55 ; used for interrupt handlers, and callax clobbers ptr1.
56
57 .proc   condes
58
59         sta     getbyt+1
60         stx     getbyt+2
61         sty     index
62
63 loop:   ldy     index
64         beq     done
65         dey
66         jsr     getbyt
67         sta     jmpvec+2
68         dey
69         jsr     getbyt
70         sta     jmpvec+1
71         sty     index
72         jsr     jmpvec
73 .if (.cpu .bitand ::CPU_ISET_65SC02)
74         bra     loop
75 .else
76         jmp     loop
77 .endif
78
79 done:   rts
80
81 .endproc
82
83
84 ; --------------------------------------------------------------------------
85 ; Data. The getbyte and jmpvec routines are placed in the data segment
86 ; cause they're patched at runtime.
87
88 .bss
89
90 index:  .byte   0
91
92 .data
93
94 getbyt: lda     $FFFF,y
95         rts
96
97 jmpvec: jmp     $0000