]> git.sur5r.net Git - cc65/blobdiff - libsrc/runtime/stkchk.s
no TGI_ERR_NO_MEM or TGI_ERR_NO_IOCB anymore: replaced by TGI_ERR_NO_RES
[cc65] / libsrc / runtime / stkchk.s
index 53f9e1487a3906223bf9a222206034dd4d9837f7..ee0b7144c80fc7316fc5d611f970bb562394454d 100644 (file)
@@ -1,9 +1,10 @@
 ;
 ; Ullrich von Bassewitz, 19.03.2001
 ;
-; Stack checking code.
+; Stack checking code. These are actually two routines, one to check the C
+; stack, and the other one to check the 6502 hardware stack.
 ; For performance reasons (to avoid having to pass a parameter), the compiler
-; calls the stkchk routine *after* allocating space on the stack. So the
+; calls the cstkchk routine *after* allocating space on the stack. So the
 ; stackpointer may already be invalid if this routine is called. In addition
 ; to that, pushs and pops that are needed for expression evaluation are not
 ; checked (this would be way too much overhead). As a consequence we will
@@ -12,7 +13,7 @@
 ; its' bounds.
 ;
 
-       .export         stkchk
+       .export         stkchk, cstkchk
        .constructor    initstkchk, 25
        .import         __STACKSIZE__                   ; Linker defined
                .import         pusha0, _exit
 
        ; Use macros for better readability
        .macpack        generic
+        .macpack        cpu
 
-.code
 
 ; ----------------------------------------------------------------------------
 ; Initialization code. This is a constructor, so it is called on startup if
 ; the linker has detected references to this module.
 
+.segment        "INIT"
+
 .proc  initstkchk
 
-       lda     sp
-       sta     initialsp
-       sub     #<__STACKSIZE__
-       sta     lowwater
-       lda     sp+1
-       sta     initialsp+1
-       sbc     #>__STACKSIZE__
-       add     #1                      ; Add 256 bytes safety area
-       sta     lowwater+1
-       rts
+       lda     sp
+       sta     initialsp
+       sub     #<__STACKSIZE__
+       sta     lowwater
+       lda     sp+1
+       sta     initialsp+1
+       sbc     #>__STACKSIZE__
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+               ina                     ; Add 256 bytes safety area
+.else
+               add     #1              ; Add 256 bytes safety area
+.endif
+       sta     lowwater+1
+       rts
 
 .endproc
 
 ; ----------------------------------------------------------------------------
-; Stack checking routine. Does not need to save any registers.
+; 6502 stack checking routine. Does not need to save any registers.
+; Safety zone for the hardware stack is 12 bytes.
+
+.code
+
+stkchk:        tsx
+               cpx     #12
+               bcc     Fail            ; Jump on stack overflow
+       rts                     ; Return if ok
+
+; ----------------------------------------------------------------------------
+; C stack checking routine. Does not need to save any registers.
 
-.proc  stkchk
+.code
 
-       lda     lowwater+1
-       cmp     sp+1
-       bcs     @L1
-       rts
+cstkchk:
+
+; Check the high byte of the software stack
+
+@L0:           lda     lowwater+1
+               cmp     sp+1
+               bcs     @L1
+               rts
 
 ; Check low byte
 
-@L1:   bne     @Overflow
-       lda     lowwater
-       cmp     sp
-       bcs     @Overflow
-       rts
+@L1:   bne     CStackOverflow
+       lda     lowwater
+       cmp     sp
+       bcs     CStackOverflow
+Done:  rts
 
-; We have a stack overflow. Set the stack pointer to the initial value, so
+; We have a stack overflow. Set the stack pointer to the initial value, so
 ; we can continue without worrying about stack issues.
 
-@Overflow:
+CStackOverflow:
        lda     initialsp
        sta     sp
        lda     initialsp+1
        sta     sp+1
-       lda     #4
-       jsr     pusha0
-       jmp     _exit
 
-.endproc
+; Generic abort entry. We should output a diagnostic here, but this is
+; difficult, since we're operating at a lower level here.
+
+Fail:  lda     #4
+        ldx     #0
+       jmp     _exit
 
 ; ----------------------------------------------------------------------------
 ; Data
@@ -87,3 +111,4 @@ initialsp:   .word   0
 lowwater:      .word   0
 
 
+