]> git.sur5r.net Git - cc65/blob - libsrc/dbg/dbgdasm.s
Remove va_fix
[cc65] / libsrc / dbg / dbgdasm.s
1 ;
2 ; Ullrich von Bassewitz, 07.08.1998
3 ;
4 ; unsigned DbgDisAsm (char* buf, unsigned addr);
5 ; unsigned DbgDisAsm (unsigned addr);
6 ;
7 ;
8 ; Part of this code is taken from the Plus/4 machine language monitor
9 ; (TEDMon).
10 ;
11
12         .import         utsta0, popax
13         .import         __hextab, OffsetTab, AdrFlagTab
14         .import         SymbolTab1, SymbolTab2, MnemoTab1, MnemoTab2
15
16
17
18 ; -------------------------------------------------------------------------
19 ; Equates for better readability
20
21         .importzp       sreg, tmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3
22
23 BufIndex        = tmp1          ; Index into output buffer
24 OperandLen      = tmp2          ; Length of operand
25 BufLen          = tmp3          ; Length of output buffer
26 AdrFlagBuf      = tmp4          ; Flag for addressing mode
27 YSave           = sreg          ; Temp storage
28 XSave           = sreg+1        ; Dito
29 BufPtr          = ptr1          ; Pointer to output buffer
30 MemPtr          = ptr2          ; Pointer to memory to disassemble
31 MnemoBuf        = ptr3          ; Buffer for decoding mnemonic
32
33
34 ; -------------------------------------------------------------------------
35 ; Main entries
36
37         .export         _DbgDisAsm, _DbgDisAsmLen
38
39 .proc   _DbgDisAsm
40         sta     BufLen          ; Save the buffer length
41         jsr     popax           ; Get the buffer pointer
42         sta     BufPtr
43         stx     BufPtr+1
44         jsr     popax           ; Get the address
45         sta     MemPtr
46         stx     MemPtr+1
47         lda     #0
48         sta     BufIndex        ; Initialize index into buffer
49         jsr     DisAssLine      ; Disassemble one line into the buffer
50
51         lda     BufLen          ; Get requested length
52         sec
53         sbc     BufIndex
54         beq     L2
55         tax                     ; Count into X
56         ldy     BufIndex
57         lda     #$20            ; Get a space
58 L1:     sta     (BufPtr),y
59         iny
60         dex
61         bne     L1
62 L2:     lda     #0              ; Add C string terminator
63         sta     (BufPtr),y
64         beq     disassret
65
66 .endproc
67
68
69 _DbgDisAsmLen:
70         sta     MemPtr          ; Save address
71         stx     MemPtr+1
72         ldy     #$00
73         lda     (MemPtr),y      ; Get the opcode from memory...
74         jsr     AnalyzeOPCode   ; ...and analyze it
75 disassret:
76         ldx     OperandLen      ; Get length of operand
77         inx                     ; Adjust for opcode byte
78         txa
79         jmp     utsta0          ; Set condition codes
80
81 ; -------------------------------------------------------------------------
82 ; Helper functions
83
84
85 Put3Spaces:
86         jsr     PutSpace
87 Put2Spaces:
88         jsr     PutSpace
89 PutSpace:
90         lda     #$20
91 PutChar:
92         sty     YSave           ; Save Y
93         ldy     BufIndex        ; Get current line pointer
94         cpy     BufLen          ; Be sure not to overflow the buffer
95         bcs     PC9
96         sta     (BufPtr),y      ; store character
97         iny                     ; bump index
98         sty     BufIndex
99 PC9:    ldy     YSave           ; get old value
100         rts
101
102 ; Print the 16 bit hex value in X/Y
103
104 PutHex16:
105         txa
106         jsr     PutHex8
107         tya
108
109 ; Print 8 bit value in A, save X and Y
110
111 PutHex8:
112         stx     XSave
113         sty     YSave
114         ldy     BufIndex
115         pha
116         lsr     a
117         lsr     a
118         lsr     a
119         lsr     a
120         tax
121         lda     __hextab,x
122         sta     (BufPtr),y
123         iny
124         pla
125         and     #$0F
126         tax
127         lda     __hextab,x
128         sta     (BufPtr),y
129         iny
130         sty     BufIndex
131         ldy     YSave
132         ldx     XSave
133         rts
134
135 ; -------------------------------------------------------------------------
136 ; Eine Zeile disassemblieren
137
138 DisAssLine:
139         ldy     MemPtr
140         ldx     MemPtr+1
141         jsr     PutHex16                ; Print the address
142         jsr     Put2Spaces              ; Add some space
143         ldy     #$00
144         lda     (MemPtr),y              ; Get the opcode from memory...
145         jsr     AnalyzeOPCode           ; ...and analyze it
146         pha                             ; Save mnemonic
147         ldx     OperandLen              ; Number of bytes
148
149 ; Print the bytes that make up the instruction
150
151         inx
152 L2083:  dex
153         bpl     L208C                   ; Print the instruction bytes
154         jsr     Put3Spaces              ; If none left, print spaces instead
155         jmp     L2094
156 L208C:  lda     (MemPtr),y              ; Get a byte from memory
157         jsr     PutHex8                 ; ...and print it
158         jsr     PutSpace                ; Add some space
159
160 L2094:  iny                             ; Next one...
161         cpy     #$03                    ; Maximum is three
162         bcc     L2083                   ;
163
164         jsr     Put2Spaces              ; Add some space after bytes
165
166 ; Print the assembler mnemonic
167
168         pla                             ; Get mnemonic code
169         ldx     #$03
170         jsr     PutMnemo                ; Print the mnemonic
171         ldx     #$06
172
173 ; Print the operand
174
175 L20A4:  cpx     #$03
176         bne     L20BA
177         ldy     OperandLen
178         beq     L20BA
179
180 L20AC:  lda     AdrFlagBuf
181         cmp     #$E8                    ; Branch?
182         lda     (MemPtr),y              ; Get branch offset
183         bcs     GetBranchAdr            ; If branch: Calculate address
184         jsr     PutHex8                 ; Otherwise print 8bit value
185         dey
186         bne     L20AC
187
188 L20BA:  asl     AdrFlagBuf
189         bcc     L20CC
190         lda     SymbolTab1-1,x
191         jsr     PutChar
192         lda     SymbolTab2-1,x
193         beq     L20CC
194         jsr     PutChar
195
196 L20CC:  dex
197         bne     L20A4
198         rts
199
200 ; If the instruction is a branch, calculate the absolute address of the
201 ; branch target and print it.
202
203 GetBranchAdr:
204         jsr     L20DD
205         clc
206         adc     #$01
207         bne     L20D9
208         inx                             ; Bump high byte
209 L20D9:  tay
210         jmp     PutHex16                ; Output address
211
212 L20DD:  ldx     MemPtr+1
213         tay
214         bpl     L20E3
215         dex
216 L20E3:  adc     MemPtr
217         bcc     L20E8
218         inx                             ; Bump high byte
219 L20E8:  rts
220
221 ; -------------------------------------------------------------------------
222 ; Subroutine to analyze an opcode byte in A. Will return a byte that
223 ; encodes the mnemonic, and will set the number of bytes needed for this
224 ; instruction in OperandLen
225
226 AnalyzeOPCode:
227         tay
228         lsr     a
229         bcc     L20F8
230         lsr     a
231         bcs     L2107
232         cmp     #$22
233         beq     L2107
234         and     #$07
235         ora     #$80
236 L20F8:  lsr     a
237         tax
238         lda     OffsetTab,x
239         bcs     L2103
240         lsr     a
241         lsr     a
242         lsr     a
243         lsr     a
244 L2103:  and     #$0F
245         bne     L210B
246 L2107:  ldy     #$80
247         lda     #$00
248 L210B:  tax
249         lda     AdrFlagTab,x
250         sta     AdrFlagBuf
251         and     #$03
252         sta     OperandLen
253         tya
254         and     #$8F
255         tax
256         tya
257         ldy     #$03
258         cpx     #$8A
259         beq     L212B
260
261 L2120:  lsr     a
262         bcc     L212B
263         lsr     a
264 L2124:  lsr     a
265         ora     #$20
266         dey
267         bne     L2124
268         iny
269 L212B:  dey
270         bne     L2120
271         rts
272
273 ; -------------------------------------------------------------------------
274 ; Print the mnemonic with code in A (that code was returned by
275 ; AnalyzeOpcode).
276
277 PutMnemo:
278         tay
279         lda     MnemoTab1,y
280         sta     MnemoBuf
281         lda     MnemoTab2,y
282         sta     MnemoBuf+1
283 L213A:  lda     #$00
284         ldy     #$05            ; 3*5 bits in two bytes
285 L213E:  asl     MnemoBuf+1
286         rol     MnemoBuf
287         rol     a
288         dey
289         bne     L213E
290         adc     #$3F
291         jsr     PutChar
292         dex
293         bne     L213A
294         jmp     PutSpace
295