]> git.sur5r.net Git - cc65/blobdiff - libsrc/runtime/condes.s
Fixed a bug
[cc65] / libsrc / runtime / condes.s
index f0a1fabb72fdcc92a5eb36b7cfedb91965dc2065..36940530c37fbaf154ed9756a0d38144d2b6e020 100644 (file)
@@ -4,17 +4,63 @@
 ; CC65 runtime: Support for calling module constructors/destructors
 ;
 ; The condes routine must be called with the table address in a/x and the
-; number of vectors in the table in y. The current implementation limits
-; the table size to 128 bytes (64 vectors) but this shouldn't be problem
-; for now and may be changed later.
+; size of the table in y. The current implementation limits the table size
+; to 254 bytes (127 vectors) but this shouldn't be problem for now and may
+; be changed later.
 ;
+; libinit and libdone call condes with the predefined module constructor and
+; destructor tables, they must be called from the platform specific startup
+; code.
+
+;
+; The function does also export jmpvec as general purpose jump vector that
+; lies in the data segment so it's address may be patched at runtime.
+;
+
+               .export initlib, donelib, condes
+               .export jmpvec
+
+               .import __CONSTRUCTOR_TABLE__, __CONSTRUCTOR_COUNT__
+       .import __DESTRUCTOR_TABLE__, __DESTRUCTOR_COUNT__
+
 
-               .export condes
-               .import jmpvec
 
 .code
 
-condes:        sta     getbyt+1
+; --------------------------------------------------------------------------
+; Initialize library modules
+
+.proc  initlib
+
+       lda     #<__CONSTRUCTOR_TABLE__
+       ldx     #>__CONSTRUCTOR_TABLE__
+       ldy     #<(__CONSTRUCTOR_COUNT__*2)
+       bne     condes
+       rts
+
+.endproc
+
+
+; --------------------------------------------------------------------------
+; Cleanup library modules
+
+.proc  donelib
+
+       lda     #<__DESTRUCTOR_TABLE__
+       ldx     #>__DESTRUCTOR_TABLE__
+       ldy     #<(__DESTRUCTOR_COUNT__*2)
+       bne     condes
+       rts
+
+.endproc
+
+
+; --------------------------------------------------------------------------
+; Generic table call handler
+
+.proc  condes
+
+       sta     getbyt+1
        stx     getbyt+2
        sty     index
 
@@ -26,11 +72,19 @@ loop:       ldy     index
        dey
        jsr     getbyt
        sta     jmpvec+1
+       sty     index
        jsr     jmpvec
-       jmp     loop
+.ifpc02
+       bra     loop
+.else
+       jmp     loop
+.endif
 
 done:  rts
 
+.endproc
+
+
 ; --------------------------------------------------------------------------
 ; Data. The getbyte routine is placed in the data segment cause it's patched
 ; at runtime.
@@ -44,3 +98,6 @@ index:        .byte   0
 getbyt:        lda     $FFFF,y
        rts
 
+jmpvec:        jmp     $FFFF
+
+