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