]> git.sur5r.net Git - cc65/blob - libsrc/dbg/dbgsupp.s
Improved memset/memcpy/memmove functions by Christian Krueger.
[cc65] / libsrc / dbg / dbgsupp.s
1 ;
2 ; Ullrich von Bassewitz, 08.08.1998
3 ;
4 ; Support routines for the debugger
5 ;
6
7         .export         _DbgInit
8         .export         _DbgSP, _DbgCS, _DbgHI
9         .import         popax, return0, _DbgEntry, _set_brk, _end_brk
10         .import         _DbgBreaks
11         .import         _brk_pc
12         .import         __ZP_START__            ; Linker generated
13
14         .include        "zeropage.inc"
15
16
17 ; C callable function, will install the debugger
18
19 _DbgInit:
20         lda     #<DbgBreak
21         ldx     #>DbgBreak
22         jmp     _set_brk
23
24
25 ; Entry for the break vector.
26
27 DbgBreak:
28         pla
29         sta     retsav
30         pla
31         sta     retsav+1
32
33         cli
34         tsx                     ; Stack pointer
35         stx     _DbgSP
36
37         jsr     DbgSwapZP       ; Swap stuff
38         lda     #<DbgStack      ; Set new stack
39         sta     sp
40         lda     #>DbgStack
41         sta     sp+1
42         jsr     ResetDbgBreaks  ; Reset temporary breakpoints
43         jsr     _DbgEntry       ; Call C code
44         jsr     SetDbgBreaks    ; Set temporary breakpoints
45         jsr     DbgSwapZP       ; Swap stuff back
46
47         lda     retsav+1
48         pha
49         lda     retsav
50         pha
51         rts
52
53
54
55 ; Stack used when in debugger mode
56
57 .bss
58         .res    256
59 DbgStack:
60
61 ; Swap space for the the C temporaries
62
63 CTemp:
64 _DbgCS: .res    2               ; sp
65 _DbgHI: .res    2               ; sreg
66         .res    (zpspace-4)     ; Other stuff
67 _DbgSP: .res    1
68 retsav: .res    2               ; Save buffer for return address
69
70 .code
71
72 ; Swap the C temporaries
73
74 DbgSwapZP:
75         ldy     #zpspace-1
76 Swap1:  ldx     CTemp,y
77         lda     <__ZP_START__,y
78         sta     CTemp,y
79         txa
80         sta     sp,y
81         dey
82         bpl     Swap1
83         rts
84
85 ; ----------------------------------------------------------------------------
86 ; Utility functions
87
88
89 ; Set/reset the breakpoints. We must do that here since the breakpoints
90 ; may be in the runtime stuff, causing the C part to fail before it has
91 ; reset the breakpoints. See declaration of struct breakpoint in the C
92 ; source
93
94 MaxBreaks       = 48            ; 4*12
95
96 ResetDbgBreaks:
97         ldy     #0
98         ldx     #0
99 L4:     lda     _DbgBreaks+3,x  ; Get bk_use
100         beq     L6              ; Jump if not set
101         bpl     L5              ; Jump if user breakpoint
102         lda     #0
103         sta     _DbgBreaks+3,x  ; Clear if temp breakpoint
104 L5:     lda     _DbgBreaks+1,x  ; PC hi
105         sta     ptr1+1
106         lda     _DbgBreaks,x    ; PC lo
107         sta     ptr1
108         lda     _DbgBreaks+2,x  ; Old OPC
109         sta     (ptr1),y        ; Reset the breakpoint
110 L6:     inx
111         inx
112         inx
113         inx
114         cpx     #MaxBreaks      ; Done?
115         bne     L4
116         rts
117
118 SetDbgBreaks:
119         ldx     #0
120         ldy     #0
121 L7:     lda     _DbgBreaks+3,x  ; Get bk_use
122         beq     L8              ; Jump if not set
123         lda     _DbgBreaks+1,x  ; PC hi
124         sta     ptr1+1
125         lda     _DbgBreaks,x    ; PC lo
126         sta     ptr1
127         lda     (ptr1),y        ; Get the breakpoint OPC...
128         sta     _DbgBreaks+2,x  ; ...and save it
129         lda     #$00            ; Load BRK opcode
130         sta     (ptr1),y
131 L8:     inx
132         inx
133         inx
134         inx
135         cpx     #MaxBreaks      ; Done?
136         bne     L7
137         rts
138
139 ; Get a free breakpoint slot or return 0
140
141         .export         _DbgGetBreakSlot
142
143 _DbgGetBreakSlot:
144         ldx     #0
145 L10:    lda     _DbgBreaks+3,x  ; Get bk_use
146         beq     L11             ; Jump if not set
147         inx
148         inx
149         inx
150         inx
151         cpx     #MaxBreaks      ; Done?
152         bne     L10
153         jmp     return0         ; No free slot
154
155 L11:    stx     tmp1
156         lda     #<_DbgBreaks
157         ldx     #>_DbgBreaks
158         clc
159         adc     tmp1
160         bcc     L12
161         inx
162 L12:    ldy     #1              ; Force != 0
163         rts
164
165
166 ; Check if a given address has a user breakpoint set, if found, return the
167 ; slot, otherwise return 0.
168
169         .export         _DbgIsBreak
170
171 _DbgIsBreak:
172         jsr     popax           ; Get address
173         sta     ptr1
174         stx     ptr1+1
175         ldx     #0
176 L20:    lda     _DbgBreaks+3,x  ; Get bk_use
177         beq     L21             ; Jump if not set
178         bmi     L21             ; Jump if temp breakpoint
179         lda     _DbgBreaks,x    ; Low byte of address
180         cmp     ptr1
181         bne     L21
182         lda     _DbgBreaks+1,x  ; High byte of address
183         cmp     ptr1+1
184         beq     L22
185 L21:    inx
186         inx
187         inx
188         inx
189         cpx     #MaxBreaks      ; Done?
190         bne     L20
191         jmp     return0         ; Not found
192
193 L22:    stx     tmp1
194         lda     #<_DbgBreaks
195         ldx     #>_DbgBreaks
196         clc
197         adc     tmp1
198         bcc     L23
199         inx
200 L23:    ldy     #1              ; Force != 0
201         rts
202
203
204