]> git.sur5r.net Git - cc65/blob - libsrc/runtime/condes.s
remove TABs
[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 (which must not be zero!) in y. The current implementation
8 ; limits the table size to 254 bytes (127 vectors) but this shouldn't be
9 ; problem for now and may 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 ; --------------------------------------------------------------------------
24 ; Initialize library modules
25
26 .segment        "ONCE"
27
28 .proc   initlib
29
30         ldy     #<(__CONSTRUCTOR_COUNT__*2)
31         beq     exit
32         lda     #<__CONSTRUCTOR_TABLE__
33         ldx     #>__CONSTRUCTOR_TABLE__
34         jmp     condes
35 exit:   rts
36
37 .endproc
38
39
40 ; --------------------------------------------------------------------------
41 ; Cleanup library modules
42
43 .code
44
45 .proc   donelib
46
47         ldy     #<(__DESTRUCTOR_COUNT__*2)
48         beq     exit
49         lda     #<__DESTRUCTOR_TABLE__
50         ldx     #>__DESTRUCTOR_TABLE__
51         jmp     condes
52 exit:   rts
53
54 .endproc
55
56
57 ; --------------------------------------------------------------------------
58 ; Generic table call handler. The code uses self modifying code and goes
59 ; into the data segment for this reason.
60 ; NOTE: The routine must not be called if the table is empty!
61
62 .data
63
64 .proc   condes
65
66         sta     fetch1+1
67         stx     fetch1+2
68         sta     fetch2+1
69         stx     fetch2+2
70 loop:   dey
71 fetch1: lda     $FFFF,y                 ; Patched at runtime
72         sta     jmpvec+2
73         dey
74 fetch2: lda     $FFFF,y                 ; Patched at runtime
75         sta     jmpvec+1
76         sty     index+1
77 jmpvec: jsr     $FFFF                   ; Patched at runtime
78 index:  ldy     #$FF                    ; Patched at runtime
79         bne     loop
80         rts
81
82 .endproc
83
84