Mercurial > hg > forks > dxa
annotate dump.c @ 4:0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 25 Feb 2015 04:15:07 +0200 |
parents | ec2f8f6f1dc9 |
children | 81b25f8cde12 |
rev | line source |
---|---|
0 | 1 /*\ |
2 * dxa v0.1.3 -- symbolic 65xx disassembler | |
3 * | |
4 * Copyright (C) 1993, 1994 Marko M\"akel\"a | |
5 * Changes for dxa (C) 2004, 2006 Cameron Kaiser | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 * | |
21 * Marko does not maintain dxa, so questions specific to dxa should be | |
22 * sent to me at ckaiser@floodgap.com . Otherwise, | |
23 * | |
24 * Contacting the author: | |
25 * | |
26 * Via Internet E-mail: | |
27 * <Marko.Makela@FTP.FUNET.FI> | |
28 * | |
29 * Via Snail Mail: | |
30 * Marko M\"akel\"a | |
31 * Sillitie 10 A | |
32 * FIN-01480 VANTAA | |
33 * Finland | |
34 \*/ | |
35 | |
36 #define _DUMP_C_ | |
37 #include <stdio.h> | |
38 #include <stdlib.h> | |
39 | |
40 #include "proto.h" | |
41 #include "options.h" | |
42 #include "opcodes.h" | |
43 | |
44 void Dump (void) | |
45 { | |
46 ADDR_T address, addr; | |
47 unsigned counter, size, maxwidth; | |
48 char *lineprefix, *lineinfix; | |
49 table *entry; | |
50 opcodes *instr; | |
51 | |
52 if (fVerbose) | |
53 fprintf (stderr, "%s: Dumping the source code.\n", prog); | |
54 | |
55 /* determine the maximum amount of bytes dumped per line */ | |
56 | |
57 maxwidth = listwidth < 2 ? 2 : listwidth; | |
58 | |
59 for (counter = 0; counter < 256; counter++) | |
60 if (maxwidth < sizes[opset[counter].admode]) | |
61 maxwidth = sizes[opset[counter].admode]; | |
62 | |
63 /* create prefix string for lines without address information */ | |
64 switch (Options & M_ADDRESSES) { | |
65 case O_ADR_ADRPFIX: | |
66 counter = 5; | |
67 break; | |
68 | |
69 case O_ADR_ADR_DMP: | |
70 counter = 5 + 3 * maxwidth; | |
71 break; | |
72 | |
73 default: | |
74 counter = 0; | |
75 } | |
76 | |
77 lineprefix = malloc (counter + 1); | |
78 | |
79 if (counter >= 5) { | |
80 lineinfix = lineprefix + 5; | |
81 | |
82 for (lineprefix[counter] = 0; counter--; lineprefix[counter] = ' '); | |
83 } | |
84 else { | |
85 *lineprefix = 0; | |
86 lineinfix = lineprefix; | |
87 } | |
88 | |
89 /* print the label definitions */ | |
90 | |
91 for (address = EndAddress < StartAddress ? EndAddress : 0; | |
92 address != StartAddress; address++) | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
93 if (IsLabeled (address) && !IsInsideRegion(address)) |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
94 fprintf (stdout, "%s%s = $%x\n", lineprefix, Label (address, abso, 0), |
0 | 95 address); |
96 | |
97 if (EndAddress >= StartAddress) | |
98 for (address = EndAddress; address; address++) | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
99 if (IsLabeled (address) && !IsInsideRegion(address)) |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
100 fprintf (stdout, "%s%s = $%x\n", lineprefix, Label (address, abso, 0), |
0 | 101 address); |
102 | |
103 /* dump the program */ | |
104 | |
105 if(Options & B_SA_WORD) | |
106 fprintf(stdout, "%s\t.word $%04x", lineprefix, StartAddress); | |
107 fprintf (stdout, "\n%s\t* = $%04x\n\n", lineprefix, StartAddress); | |
108 | |
109 for (address = StartAddress; (ADDR_T)(address - StartAddress) < | |
110 (ADDR_T)(EndAddress - StartAddress); address += size) | |
111 if (GetMemType (address) == MEM_INSTRUCTION) { | |
112 if (IsLabeled (address)) { | |
113 if (Options & M_ADDRESSES) | |
114 fprintf (stdout, "%04x %s%s:\n", address, | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
115 lineinfix, Label (address, abso, 0)); |
0 | 116 else { |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
117 fprintf (stdout, "%s", Label (address, abso, 0)); |
0 | 118 if (Options & B_LABCOL) |
119 fprintf(stdout, ":\n"); | |
120 } | |
121 } | |
122 | |
123 instr = &opset[Memory[address]]; | |
124 size = sizes[instr->admode]; | |
125 | |
126 for (counter = 1; counter < size; counter++) { | |
127 if (IsLabeled (address + counter)) { | |
128 if (Options & M_ADDRESSES) | |
129 fprintf (stdout, "\t%04x %s%s = * + %u\n", | |
130 (ADDR_T)(address + counter), | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
131 lineinfix, Label (address + counter, abso, 0), counter); |
0 | 132 else |
133 fprintf (stdout, "\t%s = * + %u\n", | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
134 Label (address + counter, abso, 0), counter); |
0 | 135 } |
136 | |
137 if (FindNextEntry (NULL, address, ~0, WRN_INSTR_WRITTEN_TO)) | |
138 fprintf (stdout, "%s; Instruction opcode accessed.\n", lineprefix); | |
139 | |
140 entry = NULL; | |
141 | |
142 while ((entry = FindNextEntry (entry, address + counter, 0, 0))) | |
143 switch (entry->type) { | |
144 case WRN_PARAM_WRITTEN_TO: | |
145 fprintf (stdout, "%s; Instruction parameter accessed.\n", | |
146 lineprefix); | |
147 break; | |
148 | |
149 case WRN_PARAM_JUMPED_TO: | |
150 fprintf (stdout, "%s; Instruction parameter jumped to.\n", | |
151 lineprefix); | |
152 break; | |
153 } | |
154 } | |
155 | |
156 switch (Options & M_ADDRESSES) { | |
157 case O_ADR_ADRPFIX: | |
158 fprintf (stdout, "%04x ", address); | |
159 break; | |
160 | |
161 case O_ADR_ADR_DMP: | |
162 fprintf (stdout, "%04x ", address); | |
163 | |
164 for (counter = 0; counter < size; counter++) | |
165 fprintf (stdout, "%02x ", Memory[(ADDR_T)(address + counter)]); | |
166 | |
167 fputs (lineinfix + 3 * counter, stdout); | |
168 } | |
169 | |
170 fputs ("\t", stdout); | |
171 | |
172 switch (instr->admode) { | |
173 case accu: | |
174 case impl: | |
175 fprintf (stdout, "%s%s\n", mne[instr->mnemonic], | |
176 postfix[instr->admode]); | |
177 break; | |
178 case imm: | |
179 addr = Memory[(ADDR_T)(address + 1)]; | |
180 fprintf (stdout, "%s #$%02x\n", mne[instr->mnemonic], addr); | |
181 break; | |
182 case abso: | |
183 case absx: | |
184 case absy: | |
185 case iabs: | |
186 case iabsx: | |
187 addr = Memory[(ADDR_T)(address + 1)] | | |
188 (Memory[(ADDR_T)(address + 2)] << 8); | |
189 /* Fix to ensure 16-bit addresses to zero-page are maintained as 16-bit */ | |
190 fprintf (stdout, "%s %s%s%s%s\n", mne[instr->mnemonic], | |
191 prefix[instr->admode], | |
192 ((addr < 256 && instr->mnemonic != S_JMP && | |
193 instr->mnemonic != S_JSR) ? "!" : ""), | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
194 Label (addr, abso, 1), postfix[instr->admode]); |
0 | 195 break; |
196 case zp: | |
197 case zpx: | |
198 case zpy: | |
199 case ind: | |
200 case indx: | |
201 case indy: | |
202 addr = Memory[(ADDR_T)(address + 1)]; | |
203 fprintf (stdout, "%s %s%s%s\n", mne[instr->mnemonic], | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
204 prefix[instr->admode], Label (addr, zp, 1), |
0 | 205 postfix[instr->admode]); |
206 break; | |
207 case rel: | |
208 addr = (int)(char)Memory[(ADDR_T)(address + 1)]; | |
209 /* addr -= (addr > 127) ? 256 : 0; BUGFIX: sign extend already done */ | |
210 /*fprintf(stderr, "%d %d %d\n", address, size, addr);*/ | |
211 addr += address + size; | |
212 fprintf (stdout, "%s %s%s%s\n", mne[instr->mnemonic], | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
213 prefix[instr->admode], Label (addr, abso, 1), |
0 | 214 postfix[instr->admode]); |
215 break; | |
216 case zrel: /* BBR0, etc. 65C02 instructions */ | |
217 addr = (int)(char)Memory[(ADDR_T)(address + 2)]; | |
218 /* addr -= (addr > 127) ? 256 : 0; BUGFIX: sign extend already done */ | |
219 addr += address + size; | |
220 fprintf (stdout, "%s %s, %s\n", mne[instr->mnemonic], | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
221 Label (Memory[(ADDR_T)(address + 1)], abso, 1), |
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
222 Label (addr, abso, 1)); |
0 | 223 break; |
224 } | |
225 } | |
226 else if (address != (addr = WordTableEnd (address))) { /* word table */ | |
227 for (size = (ADDR_T)(addr - address); size; | |
228 address += (counter = size > (maxwidth & ~1) ? | |
229 (maxwidth & ~1) : size), size -= counter) { | |
230 if (IsLabeled (address)) { | |
231 if (Options & M_ADDRESSES) | |
232 fprintf (stdout, "%04x %s%s:\n", address, lineinfix, | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
233 Label (address, abso, 0)); |
0 | 234 else |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
235 fprintf (stdout, "%s ", Label (address, abso, 0)); |
0 | 236 } |
237 for (counter = size > (maxwidth & ~1) ? (maxwidth & ~1) : size, | |
238 addr = address + 1; --counter; addr++) | |
239 if (IsLabeled (addr)) { | |
240 if (Options & M_ADDRESSES) | |
241 fprintf (stdout, "%04x %s%s = * + %u\n", addr, lineinfix, | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
242 Label (addr, abso, 0), (ADDR_T)(addr - address)); |
0 | 243 else |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
244 fprintf (stdout, "\t%s = * + %u\n", Label (addr, abso, 0), |
0 | 245 (ADDR_T)(addr - address)); |
246 } | |
247 | |
248 if (Options & M_ADDRESSES) | |
249 fprintf (stdout, "%04x ", address); | |
250 | |
251 if ((Options & M_ADDRESSES) == O_ADR_ADR_DMP) { | |
252 for (counter = size > (maxwidth & ~1) ? (maxwidth & ~1) : size, | |
253 addr = address; counter--; addr++) { | |
254 fprintf (stdout, "%02x ", Memory[addr]); | |
255 } | |
256 fputs (lineinfix + 3 * (size > (maxwidth & ~1) ? | |
257 (maxwidth & ~1) : size), stdout); | |
258 } | |
259 | |
260 fprintf (stdout, " .word %s", | |
261 Label (Memory[address] | | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
262 (Memory[(ADDR_T)(address + 1)] << 8), abso, 0)); |
0 | 263 |
264 for (counter = size > (maxwidth & ~1) ? (maxwidth & ~1) : size, | |
265 addr = address + 2; counter -= 2; addr += 2) | |
266 fprintf (stdout, ",%s", | |
267 Label (Memory[addr] | (Memory[(ADDR_T)(addr + 1)] << 8), | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
268 abso, 0)); |
0 | 269 |
270 | |
271 fputc ('\n', stdout); | |
272 } | |
273 } | |
274 else { /* data block */ | |
275 for (size = 1; size < maxwidth; size++) { /* determine the size */ | |
276 addr = address + size; | |
277 | |
278 if (GetMemType (addr) == MEM_INSTRUCTION || | |
279 addr != WordTableEnd (addr)) | |
280 break; | |
281 } | |
282 | |
283 if (IsLabeled (address)) { | |
284 if (Options & M_ADDRESSES) | |
285 fprintf (stdout, "%04x %s%s:\n", address, lineinfix, | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
286 Label (address, abso, 0)); |
0 | 287 else |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
288 fprintf (stdout, "%s ", Label (address, abso, 0)); |
0 | 289 } |
290 | |
291 for (counter = size, addr = address + 1; --counter; addr++) | |
292 if (IsLabeled (addr)) { | |
293 if (Options & M_ADDRESSES) | |
294 fprintf (stdout, "%04x %s%s = * + %u\n", addr, lineinfix, | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
295 Label (addr, abso, 0), (ADDR_T)(addr - address)); |
0 | 296 else |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
297 fprintf (stdout, "\t%s = * + %u\n", Label (addr, abso, 0), |
0 | 298 (ADDR_T)(addr - address)); |
299 } | |
300 | |
301 if (Options & M_ADDRESSES) | |
302 fprintf (stdout, "%04x ", address); | |
303 | |
304 if ((Options & M_ADDRESSES) == O_ADR_ADR_DMP) { | |
305 for (counter = size, addr = address; counter--; addr++) | |
306 fprintf (stdout, "%02x ", Memory[addr]); | |
307 | |
308 fputs (lineinfix + 3 * size, stdout); | |
309 } | |
310 | |
311 fprintf (stdout, "\t.byt $%02x", Memory[address]); | |
312 | |
313 for (counter = size, addr = address + 1; --counter; addr++) | |
314 if (addr < EndAddress) /* problems with this overflowing */ | |
315 fprintf (stdout, ",$%02x", Memory[addr]); | |
316 | |
317 fputc ('\n', stdout); | |
318 } | |
319 } |