Mercurial > hg > forks > dxa
annotate dump.c @ 7:81b25f8cde12
Cosmetics.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 25 Feb 2015 04:27:18 +0200 |
parents | 0990d9322fc8 |
children | 122134900c0e |
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 */ | |
7 | 104 fprintf(stdout, "\n\n"); |
0 | 105 |
106 if(Options & B_SA_WORD) | |
107 fprintf(stdout, "%s\t.word $%04x", lineprefix, StartAddress); | |
7 | 108 |
0 | 109 fprintf (stdout, "\n%s\t* = $%04x\n\n", lineprefix, StartAddress); |
110 | |
7 | 111 |
0 | 112 for (address = StartAddress; (ADDR_T)(address - StartAddress) < |
113 (ADDR_T)(EndAddress - StartAddress); address += size) | |
114 if (GetMemType (address) == MEM_INSTRUCTION) { | |
115 if (IsLabeled (address)) { | |
116 if (Options & M_ADDRESSES) | |
117 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
|
118 lineinfix, Label (address, abso, 0)); |
0 | 119 else { |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
120 fprintf (stdout, "%s", Label (address, abso, 0)); |
0 | 121 if (Options & B_LABCOL) |
122 fprintf(stdout, ":\n"); | |
123 } | |
124 } | |
125 | |
126 instr = &opset[Memory[address]]; | |
127 size = sizes[instr->admode]; | |
128 | |
129 for (counter = 1; counter < size; counter++) { | |
130 if (IsLabeled (address + counter)) { | |
131 if (Options & M_ADDRESSES) | |
132 fprintf (stdout, "\t%04x %s%s = * + %u\n", | |
133 (ADDR_T)(address + counter), | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
134 lineinfix, Label (address + counter, abso, 0), counter); |
0 | 135 else |
136 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
|
137 Label (address + counter, abso, 0), counter); |
0 | 138 } |
139 | |
140 if (FindNextEntry (NULL, address, ~0, WRN_INSTR_WRITTEN_TO)) | |
141 fprintf (stdout, "%s; Instruction opcode accessed.\n", lineprefix); | |
142 | |
143 entry = NULL; | |
144 | |
145 while ((entry = FindNextEntry (entry, address + counter, 0, 0))) | |
146 switch (entry->type) { | |
147 case WRN_PARAM_WRITTEN_TO: | |
148 fprintf (stdout, "%s; Instruction parameter accessed.\n", | |
149 lineprefix); | |
150 break; | |
151 | |
152 case WRN_PARAM_JUMPED_TO: | |
153 fprintf (stdout, "%s; Instruction parameter jumped to.\n", | |
154 lineprefix); | |
155 break; | |
156 } | |
157 } | |
158 | |
159 switch (Options & M_ADDRESSES) { | |
160 case O_ADR_ADRPFIX: | |
161 fprintf (stdout, "%04x ", address); | |
162 break; | |
163 | |
164 case O_ADR_ADR_DMP: | |
165 fprintf (stdout, "%04x ", address); | |
166 | |
167 for (counter = 0; counter < size; counter++) | |
168 fprintf (stdout, "%02x ", Memory[(ADDR_T)(address + counter)]); | |
169 | |
170 fputs (lineinfix + 3 * counter, stdout); | |
171 } | |
172 | |
173 fputs ("\t", stdout); | |
174 | |
175 switch (instr->admode) { | |
176 case accu: | |
177 case impl: | |
178 fprintf (stdout, "%s%s\n", mne[instr->mnemonic], | |
179 postfix[instr->admode]); | |
180 break; | |
181 case imm: | |
182 addr = Memory[(ADDR_T)(address + 1)]; | |
183 fprintf (stdout, "%s #$%02x\n", mne[instr->mnemonic], addr); | |
184 break; | |
185 case abso: | |
186 case absx: | |
187 case absy: | |
188 case iabs: | |
189 case iabsx: | |
190 addr = Memory[(ADDR_T)(address + 1)] | | |
191 (Memory[(ADDR_T)(address + 2)] << 8); | |
192 /* Fix to ensure 16-bit addresses to zero-page are maintained as 16-bit */ | |
193 fprintf (stdout, "%s %s%s%s%s\n", mne[instr->mnemonic], | |
194 prefix[instr->admode], | |
195 ((addr < 256 && instr->mnemonic != S_JMP && | |
196 instr->mnemonic != S_JSR) ? "!" : ""), | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
197 Label (addr, abso, 1), postfix[instr->admode]); |
0 | 198 break; |
199 case zp: | |
200 case zpx: | |
201 case zpy: | |
202 case ind: | |
203 case indx: | |
204 case indy: | |
205 addr = Memory[(ADDR_T)(address + 1)]; | |
206 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
|
207 prefix[instr->admode], Label (addr, zp, 1), |
0 | 208 postfix[instr->admode]); |
209 break; | |
210 case rel: | |
211 addr = (int)(char)Memory[(ADDR_T)(address + 1)]; | |
212 /* addr -= (addr > 127) ? 256 : 0; BUGFIX: sign extend already done */ | |
213 /*fprintf(stderr, "%d %d %d\n", address, size, addr);*/ | |
214 addr += address + size; | |
215 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
|
216 prefix[instr->admode], Label (addr, abso, 1), |
0 | 217 postfix[instr->admode]); |
218 break; | |
219 case zrel: /* BBR0, etc. 65C02 instructions */ | |
220 addr = (int)(char)Memory[(ADDR_T)(address + 2)]; | |
221 /* addr -= (addr > 127) ? 256 : 0; BUGFIX: sign extend already done */ | |
222 addr += address + size; | |
223 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
|
224 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
|
225 Label (addr, abso, 1)); |
0 | 226 break; |
227 } | |
228 } | |
229 else if (address != (addr = WordTableEnd (address))) { /* word table */ | |
230 for (size = (ADDR_T)(addr - address); size; | |
231 address += (counter = size > (maxwidth & ~1) ? | |
232 (maxwidth & ~1) : size), size -= counter) { | |
233 if (IsLabeled (address)) { | |
234 if (Options & M_ADDRESSES) | |
235 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
|
236 Label (address, abso, 0)); |
0 | 237 else |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
238 fprintf (stdout, "%s ", Label (address, abso, 0)); |
0 | 239 } |
240 for (counter = size > (maxwidth & ~1) ? (maxwidth & ~1) : size, | |
241 addr = address + 1; --counter; addr++) | |
242 if (IsLabeled (addr)) { | |
243 if (Options & M_ADDRESSES) | |
244 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
|
245 Label (addr, abso, 0), (ADDR_T)(addr - address)); |
0 | 246 else |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
247 fprintf (stdout, "\t%s = * + %u\n", Label (addr, abso, 0), |
0 | 248 (ADDR_T)(addr - address)); |
249 } | |
250 | |
251 if (Options & M_ADDRESSES) | |
252 fprintf (stdout, "%04x ", address); | |
253 | |
254 if ((Options & M_ADDRESSES) == O_ADR_ADR_DMP) { | |
255 for (counter = size > (maxwidth & ~1) ? (maxwidth & ~1) : size, | |
256 addr = address; counter--; addr++) { | |
257 fprintf (stdout, "%02x ", Memory[addr]); | |
258 } | |
259 fputs (lineinfix + 3 * (size > (maxwidth & ~1) ? | |
260 (maxwidth & ~1) : size), stdout); | |
261 } | |
262 | |
263 fprintf (stdout, " .word %s", | |
264 Label (Memory[address] | | |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
265 (Memory[(ADDR_T)(address + 1)] << 8), abso, 0)); |
0 | 266 |
267 for (counter = size > (maxwidth & ~1) ? (maxwidth & ~1) : size, | |
268 addr = address + 2; counter -= 2; addr += 2) | |
269 fprintf (stdout, ",%s", | |
270 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
|
271 abso, 0)); |
0 | 272 |
273 | |
274 fputc ('\n', stdout); | |
275 } | |
276 } | |
277 else { /* data block */ | |
278 for (size = 1; size < maxwidth; size++) { /* determine the size */ | |
279 addr = address + size; | |
280 | |
281 if (GetMemType (addr) == MEM_INSTRUCTION || | |
282 addr != WordTableEnd (addr)) | |
283 break; | |
284 } | |
285 | |
286 if (IsLabeled (address)) { | |
287 if (Options & M_ADDRESSES) | |
288 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
|
289 Label (address, abso, 0)); |
0 | 290 else |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
291 fprintf (stdout, "%s ", Label (address, abso, 0)); |
0 | 292 } |
293 | |
294 for (counter = size, addr = address + 1; --counter; addr++) | |
295 if (IsLabeled (addr)) { | |
296 if (Options & M_ADDRESSES) | |
297 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
|
298 Label (addr, abso, 0), (ADDR_T)(addr - address)); |
0 | 299 else |
4
0990d9322fc8
Implement address-ranged labels. Breaks compatibility of label files.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
300 fprintf (stdout, "\t%s = * + %u\n", Label (addr, abso, 0), |
0 | 301 (ADDR_T)(addr - address)); |
302 } | |
303 | |
304 if (Options & M_ADDRESSES) | |
305 fprintf (stdout, "%04x ", address); | |
306 | |
307 if ((Options & M_ADDRESSES) == O_ADR_ADR_DMP) { | |
308 for (counter = size, addr = address; counter--; addr++) | |
309 fprintf (stdout, "%02x ", Memory[addr]); | |
310 | |
311 fputs (lineinfix + 3 * size, stdout); | |
312 } | |
313 | |
314 fprintf (stdout, "\t.byt $%02x", Memory[address]); | |
315 | |
316 for (counter = size, addr = address + 1; --counter; addr++) | |
317 if (addr < EndAddress) /* problems with this overflowing */ | |
318 fprintf (stdout, ",$%02x", Memory[addr]); | |
319 | |
320 fputc ('\n', stdout); | |
321 } | |
322 } |