--- z88dk-1.8.ds1/lib/cpc_crt0.asm.orig	2008-10-16 15:39:21.000000000 +0200
+++ z88dk-1.8.ds1/lib/cpc_crt0.asm	2008-10-16 13:46:26.000000000 +0200
@@ -2,37 +2,48 @@
 ;
 ;       Stefano Bodrato 8/6/2000
 ;
-;       $Id: cpc_crt0.asm,v 1.9 2007/07/21 21:27:23 dom Exp $
+;       $Id: cpc_crt0.asm,v 1.13 2008/05/26 06:38:07 stefano Exp $
 ;
 
         MODULE  cpc_crt0
 
 
+;--------
+; Include zcc_opt.def to find out some info
+;--------
         INCLUDE "zcc_opt.def"
 
+;--------
+; Some scope definitions
+;--------
 
-        XREF    _main
+        XREF    _main           ;main() is always external to crt0 code
 
+        XDEF    cleanup         ;jp'd to by exit()
+        XDEF    l_dcal          ;jp(hl)
 
+        XDEF    _vfprintf       ;jp to the printf() core
 
-        XDEF    cleanup
-        XDEF    l_dcal
-        XDEF    firmware
-        XDEF	_vfprintf
-        
-        XDEF    _std_seed
-        XDEF    exitsp
+        XDEF    exitsp          ;atexit() variables
         XDEF    exitcount
 
-        ;For stdin, stdout, stder
+        XDEF    heaplast        ;Near malloc heap variables
+        XDEF    heapblocks
+
+        XDEF    __sgoioblk      ;stdio info block
+
+        XDEF    base_graphics   ;Graphical variables
+        XDEF    coords          ;Current xy position
 
-        XDEF    __sgoioblk
+        XDEF    snd_tick        ;Sound variable
 
-;       Graphics stuff
-        XDEF	base_graphics
-        XDEF	coords
+        XDEF    call_rom3       ;Interposer
 
-        ; Now, getting to the real stuff now!
+        XDEF    firmware_bc     ;Needed by the firmware interposer
+
+;--------
+; Set an origin for the application (-zorg=) default to $6000
+;--------
 
         IF      !myzorg
                 defc    myzorg  = $6000
@@ -43,28 +54,33 @@
         org     myzorg
 
 
+;--------
+; REAL CODE
+;--------
+
 .start
-        di
+        ld      hl,($39)        ; Original Interrupt code
+        ld      (oldint),hl
+        ld      hl,newint       ; Point to a null handler (increase stability)
+        ld      ($39),hl
+        
         ld      (start1+1),sp
         ld      hl,-6530
         add     hl,sp
         ld      sp,hl
         ld      (exitsp),sp
         exx
-        ld      (firmware_bc),bc
-        push    af
-        pop     hl
-        ld      (firmware_af),hl
+        ld      (firmware_bc),bc        ; keep BC', otherwise crash
         exx
 IF !DEFINED_nostreams
 IF DEFINED_ANSIstdio
 ; Set up the std* stuff so we can be called again
         ld      hl,__sgoioblk+2
-        ld      (hl),19	;stdin
+        ld      (hl),19 ;stdin
         ld      hl,__sgoioblk+6
-        ld      (hl),21	;stdout
+        ld      (hl),21 ;stdout
         ld      hl,__sgoioblk+10
-        ld      (hl),21	;stderr
+        ld      (hl),21 ;stderr
 ENDIF
 ENDIF
 
@@ -76,6 +92,7 @@
 ENDIF
 
         call    _main
+
 .cleanup
 ;
 ;       Deallocate memory which has been allocated here!
@@ -84,12 +101,17 @@
 IF !DEFINED_nostreams
 IF DEFINED_ANSIstdio
         LIB     closeall
-        call	closeall
+        call    closeall
 ENDIF
 ENDIF
+        exx
+        ld      bc,(firmware_bc)        ; restore BC'
+        exx
 
+        ld      hl,(oldint)
+        ld      ($39),hl
+        
 .start1 ld      sp,0
-        ei
         ret
 
 .l_dcal jp      (hl)
@@ -98,7 +120,7 @@
 
 .__sgoioblk
 IF DEFINED_ANSIstdio
-        INCLUDE	"#stdio_fp.asm"
+        INCLUDE "#stdio_fp.asm"
 ELSE
         defw    -11,-12,-10
 ENDIF
@@ -109,54 +131,43 @@
 
 ._vfprintf
 IF DEFINED_floatstdio
-	LIB	vfprintf_fp
-	jp	vfprintf_fp
+        LIB     vfprintf_fp
+        jp      vfprintf_fp
 ELSE
-	IF DEFINED_complexstdio
-		LIB	vfprintf_comp
-		jp	vfprintf_comp
-	ELSE
-		IF DEFINED_ministdio
-			LIB	vfprintf_mini
-			jp	vfprintf_mini
-		ENDIF
-	ENDIF
-ENDIF
-
-; Function to call firmware function
-; ix = firmware routine to call
-; 
-.firmware
-        exx                      ; Use alternate registers
-        ex      (sp),hl          ; get return address
-        ld      c,(hl)
-        inc     hl
-        ld      b,(hl)           ; BC=BASIC address
-        inc     hl
-        ex      (sp),hl          ; restore return address
-        push    bc
-        ld      bc,(firmware_bc)
-        ld      hl,(firmware_af)
-        push    hl
-        pop     af
-        exx                      ; Back to the regular set
-        ret                      ; And call the firmware routine 
+        IF DEFINED_complexstdio
+                LIB     vfprintf_comp
+                jp      vfprintf_comp
+        ELSE
+                IF DEFINED_ministdio
+                        LIB     vfprintf_mini
+                        jp      vfprintf_mini
+                ENDIF
+        ENDIF
+ENDIF
 
         
-._std_seed      defw    0       ;  Default seed
+IF !DEFINED_HAVESEED
+                XDEF    _std_seed        ;Integer rand() seed
+._std_seed       defw    0       ; Seed for integer rand() routines
+ENDIF
+
 .exitsp         defw    0       ;  atexit       
 .exitcount      defb    0
 .base_graphics
-                defw	$C000
-.coords         defw	0
+                defw    $C000
+.coords         defw    0
 .firmware_bc    defw    0
-.firmware_af    defw    0
+
+.heaplast       defw    0       ; Address of last block on heap
+.heapblocks     defw    0       ; Number of blocks
 
                 defm    "Small C+ CPC"
                 defb    0
 
 IF NEED_floatpack
         INCLUDE         "#float.asm"
+;.init_floatpack
+;       ret
 
 ;seed for random number generator - not used yet..
 .fp_seed        defb    $80,$80,0,0,0,0
@@ -167,3 +178,10 @@
 
 ENDIF
 
+
+.newint
+        ei
+        reti
+
+.oldint
+        defw 0
