]> git.sur5r.net Git - cc65/blob - libsrc/runtime/condes.s
Optimized the condes routine, because it is used to schedule interrupt
[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 .code
24
25 ; --------------------------------------------------------------------------
26 ; Initialize library modules
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 .proc   donelib
44
45         ldy     #<(__DESTRUCTOR_COUNT__*2)
46         beq     initlib::exit
47         lda     #<__DESTRUCTOR_TABLE__
48         ldx     #>__DESTRUCTOR_TABLE__
49         jmp     condes
50
51 .endproc
52
53
54 ; --------------------------------------------------------------------------
55 ; Generic table call handler. Since the routine is also used to call a table
56 ; of interrupt handlers, it uses heavily self modifying code for performance
57 ; reasons. It will go into the data segment for this reason ...
58 ; NOTE: The routine must not be called if the table is empty!
59
60 .data
61
62 .proc   condes
63
64         sta     fetch1+1
65         stx     fetch1+2
66         sta     fetch2+1
67         stx     fetch2+2
68 loop:   dey
69 fetch1: lda     $FFFF,y                 ; Patched at runtime
70         sta     jmpvec+2
71         dey
72 fetch2: lda     $FFFF,y                 ; Patched at runtime
73         sta     jmpvec+1
74         sty     index+1
75 jmpvec: jsr     $FFFF                   ; Patched at runtime
76 index:  ldy     #$FF                    ; Patched at runtime
77         bne     loop
78         rts
79
80 .endproc
81
82