]> git.sur5r.net Git - cc65/blob - libsrc/runtime/stkchk.s
Added an IRQ vector
[cc65] / libsrc / runtime / stkchk.s
1 ;
2 ; Ullrich von Bassewitz, 19.03.2001
3 ;
4 ; Stack checking code. These are actually two routines, one to check the C
5 ; stack, and the other one to check the 6502 hardware stack.
6 ; For performance reasons (to avoid having to pass a parameter), the compiler
7 ; calls the cstkchk routine *after* allocating space on the stack. So the
8 ; stackpointer may already be invalid if this routine is called. In addition
9 ; to that, pushs and pops that are needed for expression evaluation are not
10 ; checked (this would be way too much overhead). As a consequence we will
11 ; operate using a safety area at the stack bottom. Once the stack reaches this
12 ; safety area, we consider it an overflow, even if the stack is still inside
13 ; its' bounds.
14 ;
15
16         .export         stkchk, cstkchk
17         .constructor    initstkchk, 25
18         .import         __STACKSIZE__                   ; Linker defined
19         .import         pusha0, _exit
20         .importzp       sp
21
22         ; Use macros for better readability
23         .macpack        generic
24
25 .code
26
27 ; ----------------------------------------------------------------------------
28 ; Initialization code. This is a constructor, so it is called on startup if
29 ; the linker has detected references to this module.
30
31 .proc   initstkchk
32
33         lda     sp
34         sta     initialsp
35         sub     #<__STACKSIZE__
36         sta     lowwater
37         lda     sp+1
38         sta     initialsp+1
39         sbc     #>__STACKSIZE__
40         add     #1                      ; Add 256 bytes safety area
41         sta     lowwater+1
42         rts
43
44 .endproc
45
46 ; ----------------------------------------------------------------------------
47 ; 6502 stack checking routine. Does not need to save any registers.
48 ; Safety zone for the hardware stack is 12 bytes.
49
50 stkchk: tsx
51         cpx     #12
52         bcc     Fail            ; Jump on stack overflow
53         rts                     ; Return if ok
54
55 ; ----------------------------------------------------------------------------
56 ; C stack checking routine. Does not need to save any registers.
57
58 cstkchk:
59
60 ; Check the high byte of the software stack
61
62 @L0:    lda     lowwater+1
63         cmp     sp+1
64         bcs     @L1
65         rts
66
67 ; Check low byte
68
69 @L1:    bne     CStackOverflow
70         lda     lowwater
71         cmp     sp
72         bcs     CStackOverflow
73 Done:   rts
74
75 ; We have a C stack overflow. Set the stack pointer to the initial value, so
76 ; we can continue without worrying about stack issues.
77
78 CStackOverflow:
79         lda     initialsp
80         sta     sp
81         lda     initialsp+1
82         sta     sp+1
83
84 ; Generic abort entry. We should output a diagnostic here, but this is
85 ; difficult, since we're operating at a lower level here.
86
87 Fail:   lda     #4
88         ldx     #0
89         jmp     _exit
90
91 ; ----------------------------------------------------------------------------
92 ; Data
93
94 .bss
95
96 ; Initial stack pointer value. Stack is reset to this in case of overflows to
97 ; allow program exit processing.
98 initialsp:      .word   0
99
100 ; Stack low water mark.
101 lowwater:       .word   0
102
103
104