comparison dump.c @ 16:a2a81589380d default tip

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