]> git.sur5r.net Git - cc65/blob - libsrc/runtime/condes.s
Use the condes feature
[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 ; The function does also export jmpvec as general purpose jump vector that
16 ; lies in the data segment so it's address may be patched at runtime.
17 ;
18
19         .export initlib, donelib, condes
20         .export jmpvec
21
22         .import __CONSTRUCTOR_TABLE__, __CONSTRUCTOR_COUNT__
23         .import __DESTRUCTOR_TABLE__, __DESTRUCTOR_COUNT__
24
25
26
27 .code
28
29 ; --------------------------------------------------------------------------
30 ; Initialize library modules
31
32 .proc   initlib
33
34         lda     #<__CONSTRUCTOR_TABLE__
35         ldx     #>__CONSTRUCTOR_TABLE__
36         ldy     #<(__CONSTRUCTOR_COUNT__*2)
37         bne     condes
38         rts
39
40 .endproc
41
42
43 ; --------------------------------------------------------------------------
44 ; Cleanup library modules
45
46 .proc   donelib
47
48         lda     #<__DESTRUCTOR_TABLE__
49         ldx     #>__DESTRUCTOR_TABLE__
50         ldy     #<(__DESTRUCTOR_COUNT__*2)
51         bne     condes
52         rts
53
54 .endproc
55
56
57 ; --------------------------------------------------------------------------
58 ; Generic table call handler
59
60 .proc   condes
61
62         sta     getbyt+1
63         stx     getbyt+2
64         sty     index
65
66 loop:   ldy     index
67         beq     done
68         dey
69         jsr     getbyt
70         sta     jmpvec+2
71         dey
72         jsr     getbyt
73         sta     jmpvec+1
74         sty     index
75         jsr     jmpvec
76         jmp     loop
77
78 done:   rts
79
80 .endproc
81
82
83 ; --------------------------------------------------------------------------
84 ; Data. The getbyte routine is placed in the data segment cause it's patched
85 ; at runtime.
86
87 .bss
88
89 index:  .byte   0
90
91 .data
92
93 getbyt: lda     $FFFF,y
94         rts
95
96 jmpvec: jmp     $FFFF
97
98