2001-03-19 22:26:47 +00:00
|
|
|
;
|
|
|
|
; Ullrich von Bassewitz, 19.03.2001
|
|
|
|
;
|
2001-03-20 22:34:08 +00:00
|
|
|
; Stack checking code. These are actually two routines, one to check the C
|
|
|
|
; stack, and the other one to check the 6502 hardware stack.
|
2001-03-19 22:26:47 +00:00
|
|
|
; For performance reasons (to avoid having to pass a parameter), the compiler
|
2001-03-20 22:34:08 +00:00
|
|
|
; calls the cstkchk routine *after* allocating space on the stack. So the
|
2001-03-19 22:26:47 +00:00
|
|
|
; 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
|
|
|
|
; operate using a safety area at the stack bottom. Once the stack reaches this
|
|
|
|
; safety area, we consider it an overflow, even if the stack is still inside
|
|
|
|
; its' bounds.
|
|
|
|
;
|
|
|
|
|
2001-03-20 22:34:08 +00:00
|
|
|
.export stkchk, cstkchk
|
2001-03-19 22:26:47 +00:00
|
|
|
.constructor initstkchk, 25
|
2001-03-19 23:00:19 +00:00
|
|
|
.import __STACKSIZE__ ; Linker defined
|
|
|
|
.import pusha0, _exit
|
2001-03-19 22:26:47 +00:00
|
|
|
.importzp sp
|
|
|
|
|
|
|
|
; Use macros for better readability
|
|
|
|
.macpack generic
|
2009-02-22 15:39:19 +00:00
|
|
|
.macpack cpu
|
2001-03-19 22:26:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Initialization code. This is a constructor, so it is called on startup if
|
|
|
|
; the linker has detected references to this module.
|
|
|
|
|
2005-02-26 09:06:46 +00:00
|
|
|
.segment "INIT"
|
|
|
|
|
2001-03-19 22:26:47 +00:00
|
|
|
.proc initstkchk
|
|
|
|
|
2005-02-26 09:06:46 +00:00
|
|
|
lda sp
|
|
|
|
sta initialsp
|
|
|
|
sub #<__STACKSIZE__
|
|
|
|
sta lowwater
|
|
|
|
lda sp+1
|
|
|
|
sta initialsp+1
|
|
|
|
sbc #>__STACKSIZE__
|
2009-02-22 15:39:19 +00:00
|
|
|
.if (.cpu .bitand ::CPU_ISET_65SC02)
|
|
|
|
ina ; Add 256 bytes safety area
|
|
|
|
.else
|
|
|
|
add #1 ; Add 256 bytes safety area
|
|
|
|
.endif
|
2005-02-26 09:06:46 +00:00
|
|
|
sta lowwater+1
|
|
|
|
rts
|
2001-03-19 22:26:47 +00:00
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
2001-03-20 22:34:08 +00:00
|
|
|
; 6502 stack checking routine. Does not need to save any registers.
|
2001-03-23 19:08:07 +00:00
|
|
|
; Safety zone for the hardware stack is 12 bytes.
|
2001-03-19 22:26:47 +00:00
|
|
|
|
2005-02-26 09:06:46 +00:00
|
|
|
.code
|
|
|
|
|
2001-03-20 22:34:08 +00:00
|
|
|
stkchk: tsx
|
2001-03-23 19:08:07 +00:00
|
|
|
cpx #12
|
2001-03-20 22:34:08 +00:00
|
|
|
bcc Fail ; Jump on stack overflow
|
2005-02-26 09:06:46 +00:00
|
|
|
rts ; Return if ok
|
2001-03-19 22:26:47 +00:00
|
|
|
|
2001-03-20 22:34:08 +00:00
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; C stack checking routine. Does not need to save any registers.
|
|
|
|
|
2005-02-26 09:06:46 +00:00
|
|
|
.code
|
|
|
|
|
2001-03-20 22:34:08 +00:00
|
|
|
cstkchk:
|
|
|
|
|
|
|
|
; Check the high byte of the software stack
|
|
|
|
|
|
|
|
@L0: lda lowwater+1
|
|
|
|
cmp sp+1
|
|
|
|
bcs @L1
|
|
|
|
rts
|
2001-03-19 22:26:47 +00:00
|
|
|
|
|
|
|
; Check low byte
|
|
|
|
|
2001-03-20 22:34:08 +00:00
|
|
|
@L1: bne CStackOverflow
|
|
|
|
lda lowwater
|
|
|
|
cmp sp
|
|
|
|
bcs CStackOverflow
|
|
|
|
Done: rts
|
2001-03-19 22:26:47 +00:00
|
|
|
|
2001-03-20 22:34:08 +00:00
|
|
|
; We have a C stack overflow. Set the stack pointer to the initial value, so
|
2001-03-19 23:00:19 +00:00
|
|
|
; we can continue without worrying about stack issues.
|
2001-03-19 22:26:47 +00:00
|
|
|
|
2001-03-20 22:34:08 +00:00
|
|
|
CStackOverflow:
|
2001-03-19 23:00:19 +00:00
|
|
|
lda initialsp
|
|
|
|
sta sp
|
|
|
|
lda initialsp+1
|
|
|
|
sta sp+1
|
2001-03-20 22:34:08 +00:00
|
|
|
|
|
|
|
; 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
|
2002-11-22 00:38:52 +00:00
|
|
|
ldx #0
|
2001-03-19 23:00:19 +00:00
|
|
|
jmp _exit
|
2001-03-19 22:26:47 +00:00
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Data
|
|
|
|
|
|
|
|
.bss
|
|
|
|
|
|
|
|
; Initial stack pointer value. Stack is reset to this in case of overflows to
|
|
|
|
; allow program exit processing.
|
|
|
|
initialsp: .word 0
|
|
|
|
|
|
|
|
; Stack low water mark.
|
|
|
|
lowwater: .word 0
|
|
|
|
|
2001-03-19 23:00:19 +00:00
|
|
|
|
2005-02-26 09:06:46 +00:00
|
|
|
|