comparison dump.c @ 0:4410c9c7750d

Initial import.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 24 Feb 2015 18:53:52 +0200
parents
children ec2f8f6f1dc9
comparison
equal deleted inserted replaced
-1:000000000000 0:4410c9c7750d
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 #ifndef __STDC__
45 void
46 Dump ()
47 #else
48 void Dump (void)
49 #endif
50 {
51 ADDR_T address, addr;
52 unsigned counter, size, maxwidth;
53 char *lineprefix, *lineinfix;
54 table *entry;
55 opcodes *instr;
56
57 if (fVerbose)
58 fprintf (stderr, "%s: Dumping the source code.\n", prog);
59
60 /* determine the maximum amount of bytes dumped per line */
61
62 maxwidth = listwidth < 2 ? 2 : listwidth;
63
64 for (counter = 0; counter < 256; counter++)
65 if (maxwidth < sizes[opset[counter].admode])
66 maxwidth = sizes[opset[counter].admode];
67
68 /* create prefix string for lines without address information */
69 switch (Options & M_ADDRESSES) {
70 case O_ADR_ADRPFIX:
71 counter = 5;
72 break;
73
74 case O_ADR_ADR_DMP:
75 counter = 5 + 3 * maxwidth;
76 break;
77
78 default:
79 counter = 0;
80 }
81
82 lineprefix = malloc (counter + 1);
83
84 if (counter >= 5) {
85 lineinfix = lineprefix + 5;
86
87 for (lineprefix[counter] = 0; counter--; lineprefix[counter] = ' ');
88 }
89 else {
90 *lineprefix = 0;
91 lineinfix = lineprefix;
92 }
93
94 /* print the label definitions */
95
96 for (address = EndAddress < StartAddress ? EndAddress : 0;
97 address != StartAddress; address++)
98 if (IsLabeled (address))
99 fprintf (stdout, "%s%s = $%x\n", lineprefix, Label (address, abso),
100 address);
101
102 if (EndAddress >= StartAddress)
103 for (address = EndAddress; address; address++)
104 if (IsLabeled (address))
105 fprintf (stdout, "%s%s = $%x\n", lineprefix, Label (address, abso),
106 address);
107
108 /* dump the program */
109
110 if(Options & B_SA_WORD)
111 fprintf(stdout, "%s\t.word $%04x", lineprefix, StartAddress);
112 fprintf (stdout, "\n%s\t* = $%04x\n\n", lineprefix, StartAddress);
113
114 for (address = StartAddress; (ADDR_T)(address - StartAddress) <
115 (ADDR_T)(EndAddress - StartAddress); address += size)
116 if (GetMemType (address) == MEM_INSTRUCTION) {
117 if (IsLabeled (address)) {
118 if (Options & M_ADDRESSES)
119 fprintf (stdout, "%04x %s%s:\n", address,
120 lineinfix, Label (address, abso));
121 else {
122 fprintf (stdout, "%s", Label (address, abso));
123 if (Options & B_LABCOL)
124 fprintf(stdout, ":\n");
125 }
126 }
127
128 instr = &opset[Memory[address]];
129 size = sizes[instr->admode];
130
131 for (counter = 1; counter < size; counter++) {
132 if (IsLabeled (address + counter)) {
133 if (Options & M_ADDRESSES)
134 fprintf (stdout, "\t%04x %s%s = * + %u\n",
135 (ADDR_T)(address + counter),
136 lineinfix, Label (address + counter, abso), counter);
137 else
138 fprintf (stdout, "\t%s = * + %u\n",
139 Label (address + counter, abso), counter);
140 }
141
142 if (FindNextEntry (NULL, address, ~0, WRN_INSTR_WRITTEN_TO))
143 fprintf (stdout, "%s; Instruction opcode accessed.\n", lineprefix);
144
145 entry = NULL;
146
147 while ((entry = FindNextEntry (entry, address + counter, 0, 0)))
148 switch (entry->type) {
149 case WRN_PARAM_WRITTEN_TO:
150 fprintf (stdout, "%s; Instruction parameter accessed.\n",
151 lineprefix);
152 break;
153
154 case WRN_PARAM_JUMPED_TO:
155 fprintf (stdout, "%s; Instruction parameter jumped to.\n",
156 lineprefix);
157 break;
158 }
159 }
160
161 switch (Options & M_ADDRESSES) {
162 case O_ADR_ADRPFIX:
163 fprintf (stdout, "%04x ", address);
164 break;
165
166 case O_ADR_ADR_DMP:
167 fprintf (stdout, "%04x ", address);
168
169 for (counter = 0; counter < size; counter++)
170 fprintf (stdout, "%02x ", Memory[(ADDR_T)(address + counter)]);
171
172 fputs (lineinfix + 3 * counter, stdout);
173 }
174
175 fputs ("\t", stdout);
176
177 switch (instr->admode) {
178 case accu:
179 case impl:
180 fprintf (stdout, "%s%s\n", mne[instr->mnemonic],
181 postfix[instr->admode]);
182 break;
183 case imm:
184 addr = Memory[(ADDR_T)(address + 1)];
185 fprintf (stdout, "%s #$%02x\n", mne[instr->mnemonic], addr);
186 break;
187 case abso:
188 case absx:
189 case absy:
190 case iabs:
191 case iabsx:
192 addr = Memory[(ADDR_T)(address + 1)] |
193 (Memory[(ADDR_T)(address + 2)] << 8);
194 /* Fix to ensure 16-bit addresses to zero-page are maintained as 16-bit */
195 fprintf (stdout, "%s %s%s%s%s\n", mne[instr->mnemonic],
196 prefix[instr->admode],
197 ((addr < 256 && instr->mnemonic != S_JMP &&
198 instr->mnemonic != S_JSR) ? "!" : ""),
199 Label (addr, abso),postfix[instr->admode]);
200 break;
201 case zp:
202 case zpx:
203 case zpy:
204 case ind:
205 case indx:
206 case indy:
207 addr = Memory[(ADDR_T)(address + 1)];
208 fprintf (stdout, "%s %s%s%s\n", mne[instr->mnemonic],
209 prefix[instr->admode], Label (addr, zp),
210 postfix[instr->admode]);
211 break;
212 case rel:
213 addr = (int)(char)Memory[(ADDR_T)(address + 1)];
214 /* addr -= (addr > 127) ? 256 : 0; BUGFIX: sign extend already done */
215 /*fprintf(stderr, "%d %d %d\n", address, size, addr);*/
216 addr += address + size;
217 fprintf (stdout, "%s %s%s%s\n", mne[instr->mnemonic],
218 prefix[instr->admode], Label (addr, abso),
219 postfix[instr->admode]);
220 break;
221 case zrel: /* BBR0, etc. 65C02 instructions */
222 addr = (int)(char)Memory[(ADDR_T)(address + 2)];
223 /* addr -= (addr > 127) ? 256 : 0; BUGFIX: sign extend already done */
224 addr += address + size;
225 fprintf (stdout, "%s %s, %s\n", mne[instr->mnemonic],
226 Label (Memory[(ADDR_T)(address + 1)], abso),
227 Label (addr, abso));
228 break;
229 }
230 }
231 else if (address != (addr = WordTableEnd (address))) { /* word table */
232 for (size = (ADDR_T)(addr - address); size;
233 address += (counter = size > (maxwidth & ~1) ?
234 (maxwidth & ~1) : size), size -= counter) {
235 if (IsLabeled (address)) {
236 if (Options & M_ADDRESSES)
237 fprintf (stdout, "%04x %s%s:\n", address, lineinfix,
238 Label (address, abso));
239 else
240 fprintf (stdout, "%s ", Label (address, abso));
241 }
242 for (counter = size > (maxwidth & ~1) ? (maxwidth & ~1) : size,
243 addr = address + 1; --counter; addr++)
244 if (IsLabeled (addr)) {
245 if (Options & M_ADDRESSES)
246 fprintf (stdout, "%04x %s%s = * + %u\n", addr, lineinfix,
247 Label (addr, abso), (ADDR_T)(addr - address));
248 else
249 fprintf (stdout, "\t%s = * + %u\n", Label (addr, abso),
250 (ADDR_T)(addr - address));
251 }
252
253 if (Options & M_ADDRESSES)
254 fprintf (stdout, "%04x ", address);
255
256 if ((Options & M_ADDRESSES) == O_ADR_ADR_DMP) {
257 for (counter = size > (maxwidth & ~1) ? (maxwidth & ~1) : size,
258 addr = address; counter--; addr++) {
259 fprintf (stdout, "%02x ", Memory[addr]);
260 }
261 fputs (lineinfix + 3 * (size > (maxwidth & ~1) ?
262 (maxwidth & ~1) : size), stdout);
263 }
264
265 fprintf (stdout, " .word %s",
266 Label (Memory[address] |
267 (Memory[(ADDR_T)(address + 1)] << 8), abso));
268
269 for (counter = size > (maxwidth & ~1) ? (maxwidth & ~1) : size,
270 addr = address + 2; counter -= 2; addr += 2)
271 fprintf (stdout, ",%s",
272 Label (Memory[addr] | (Memory[(ADDR_T)(addr + 1)] << 8),
273 abso));
274
275
276 fputc ('\n', stdout);
277 }
278 }
279 else { /* data block */
280 for (size = 1; size < maxwidth; size++) { /* determine the size */
281 addr = address + size;
282
283 if (GetMemType (addr) == MEM_INSTRUCTION ||
284 addr != WordTableEnd (addr))
285 break;
286 }
287
288 if (IsLabeled (address)) {
289 if (Options & M_ADDRESSES)
290 fprintf (stdout, "%04x %s%s:\n", address, lineinfix,
291 Label (address, abso));
292 else
293 fprintf (stdout, "%s ", Label (address, abso));
294 }
295
296 for (counter = size, addr = address + 1; --counter; addr++)
297 if (IsLabeled (addr)) {
298 if (Options & M_ADDRESSES)
299 fprintf (stdout, "%04x %s%s = * + %u\n", addr, lineinfix,
300 Label (addr, abso), (ADDR_T)(addr - address));
301 else
302 fprintf (stdout, "\t%s = * + %u\n", Label (addr, abso),
303 (ADDR_T)(addr - address));
304 }
305
306 if (Options & M_ADDRESSES)
307 fprintf (stdout, "%04x ", address);
308
309 if ((Options & M_ADDRESSES) == O_ADR_ADR_DMP) {
310 for (counter = size, addr = address; counter--; addr++)
311 fprintf (stdout, "%02x ", Memory[addr]);
312
313 fputs (lineinfix + 3 * size, stdout);
314 }
315
316 fprintf (stdout, "\t.byt $%02x", Memory[address]);
317
318 for (counter = size, addr = address + 1; --counter; addr++)
319 if (addr < EndAddress) /* problems with this overflowing */
320 fprintf (stdout, ",$%02x", Memory[addr]);
321
322 fputc ('\n', stdout);
323 }
324 }