Debian Codegraph @

contrib/toacme/src/obj.c

raw symbols SCIP
1 // ToACME - converts other source codes to ACME format.
2 // Copyright (C) 1999-2006 Marco Baye
3 // Have a look at "main.c" for further info
4 //
5 // disassembly stuff
6
7 #include <stdio.h>
8 #include "config.h"
9 #include "acme.h"
10 #include "mnemo.h"
11 #include "io.h"
12
13
14 // constants
15
16 // 6502 code table (mnemonics only) *illegals*
17 const char *mnemo_of_code[] = {
18 MnemonicBRK, MnemonicORA, " JAM;0x02", MnemonicSLO, // $00-$03
19 " DOP;0x04", MnemonicORA, MnemonicASL, MnemonicSLO, // $04-$07
20 MnemonicPHP, MnemonicORA, MnemonicASL, "!by$0b;ANC#", // $08-$0b
21 " TOP;0x0c", MnemonicORA, MnemonicASL, MnemonicSLO, // $0c-$0f
22 MnemonicBPL, MnemonicORA, " JAM;0x12", MnemonicSLO, // $10-$13
23 " DOP;0x14", MnemonicORA, MnemonicASL, MnemonicSLO, // $14-$17
24 MnemonicCLC, MnemonicORA, " NOP;0x1a", MnemonicSLO, // $18-$1b
25 " TOP;0x1c", MnemonicORA, MnemonicASL, MnemonicSLO, // $1c-$1f
26 MnemonicJSR, MnemonicAND, " JAM;0x22", MnemonicRLA, // $20-$23
27 MnemonicBIT, MnemonicAND, MnemonicROL, MnemonicRLA, // $24-$27
28 MnemonicPLP, MnemonicAND, MnemonicROL, "!by$2b;ANC#", // $28-$2b
29 MnemonicBIT, MnemonicAND, MnemonicROL, MnemonicRLA, // $2c-$2f
30 MnemonicBMI, MnemonicAND, " JAM;0x32", MnemonicRLA, // $30-$33
31 " DOP;0x34", MnemonicAND, MnemonicROL, MnemonicRLA, // $34-$37
32 MnemonicSEC, MnemonicAND, " NOP;0x3a", MnemonicRLA, // $38-$3b
33 " TOP;0x3c", MnemonicAND, MnemonicROL, MnemonicRLA, // $3c-$3f
34 MnemonicRTI, MnemonicEOR, " JAM;0x42", MnemonicSRE, // $40-$43
35 " DOP;0x44", MnemonicEOR, MnemonicLSR, MnemonicSRE, // $44-$47
36 MnemonicPHA, MnemonicEOR, MnemonicLSR, MnemonicASR, // $48-$4b
37 MnemonicJMP, MnemonicEOR, MnemonicLSR, MnemonicSRE, // $4c-$4f
38 MnemonicBVC, MnemonicEOR, " JAM;0x52", MnemonicSRE, // $50-$53
39 " DOP;0x54", MnemonicEOR, MnemonicLSR, MnemonicSRE, // $54-$57
40 MnemonicCLI, MnemonicEOR, " NOP;0x5a", MnemonicSRE, // $58-$5b
41 " TOP;0x5c", MnemonicEOR, MnemonicLSR, MnemonicSRE, // $5c-$5f
42 MnemonicRTS, MnemonicADC, " JAM;0x62", MnemonicRRA, // $60-$63
43 " DOP;0x64", MnemonicADC, MnemonicROR, MnemonicRRA, // $64-$67
44 MnemonicPLA, MnemonicADC, MnemonicROR, MnemonicARR, // $68-$6b
45 MnemonicJMP, MnemonicADC, MnemonicROR, MnemonicRRA, // $6c-$6f
46 MnemonicBVS, MnemonicADC, " JAM;0x72", MnemonicRRA, // $70-$73
47 " DOP;0x74", MnemonicADC, MnemonicROR, MnemonicRRA, // $74-$77
48 MnemonicSEI, MnemonicADC, " NOP;0x7a", MnemonicRRA, // $78-$7b
49 " TOP;0x7c", MnemonicADC, MnemonicROR, MnemonicRRA, // $7c-$7f
50 " DOP;0x80", MnemonicSTA, " DOP;0x82", MnemonicSAX, // $80-$83
51 MnemonicSTY, MnemonicSTA, MnemonicSTX, MnemonicSAX, // $84-$87
52 MnemonicDEY, " DOP;0x89", MnemonicTXA, NULL, // $88-$8b
53 MnemonicSTY, MnemonicSTA, MnemonicSTX, MnemonicSAX, // $8c-$8f
54 MnemonicBCC, MnemonicSTA, " JAM;0x92", NULL, // $90-$93
55 MnemonicSTY, MnemonicSTA, MnemonicSTX, MnemonicSAX, // $94-$97
56 MnemonicTYA, MnemonicSTA, MnemonicTXS, NULL, // $98-$9b
57 NULL, MnemonicSTA, MnemonicSHX, NULL, // $9c-$9f
58 MnemonicLDY, MnemonicLDA, MnemonicLDX, MnemonicLAX, // $a0-$a3
59 MnemonicLDY, MnemonicLDA, MnemonicLDX, MnemonicLAX, // $a4-$a7
60 MnemonicTAY, MnemonicLDA, MnemonicTAX, NULL, // $a8-$ab
61 MnemonicLDY, MnemonicLDA, MnemonicLDX, MnemonicLAX, // $ac-$af
62 MnemonicBCS, MnemonicLDA, " JAM;0xb2", MnemonicLAX, // $b0-$b3
63 MnemonicLDY, MnemonicLDA, MnemonicLDX, MnemonicLAX, // $b4-$b7
64 MnemonicCLV, MnemonicLDA, MnemonicTSX, NULL, // $b8-$bb
65 MnemonicLDY, MnemonicLDA, MnemonicLDX, MnemonicLAX, // $bc-$bf
66 MnemonicCPY, MnemonicCMP, " DOP;0xc2", MnemonicDCP, // $c0-$c3
67 MnemonicCPY, MnemonicCMP, MnemonicDEC, MnemonicDCP, // $c4-$c7
68 MnemonicINY, MnemonicCMP, MnemonicDEX, MnemonicSBX, // $c8-$cb
69 MnemonicCPY, MnemonicCMP, MnemonicDEC, MnemonicDCP, // $cc-$cf
70 MnemonicBNE, MnemonicCMP, " JAM;0xd2", MnemonicDCP, // $d0-$d3
71 " DOP;0xd4", MnemonicCMP, MnemonicDEC, MnemonicDCP, // $d4-$d7
72 MnemonicCLD, MnemonicCMP, " NOP;0xda", MnemonicDCP, // $d8-$db
73 " TOP;0xdc", MnemonicCMP, MnemonicDEC, MnemonicDCP, // $dc-$df
74 MnemonicCPX, MnemonicSBC, " DOP;0xe2", MnemonicISC, // $e0-$e3
75 MnemonicCPX, MnemonicSBC, MnemonicINC, MnemonicISC, // $e4-$e7
76 MnemonicINX, MnemonicSBC, MnemonicNOP, "!by$eb;SBC#", // $e8-$eb
77 MnemonicCPX, MnemonicSBC, MnemonicINC, MnemonicISC, // $ec-$ef
78 MnemonicBEQ, MnemonicSBC, " JAM;0xf2", MnemonicISC, // $f0-$f3
79 " DOP;0xf4", MnemonicSBC, MnemonicINC, MnemonicISC, // $f4-$f7
80 MnemonicSED, MnemonicSBC, " NOP;0xfa", MnemonicISC, // $f8-$fb
81 " TOP;0xfc", MnemonicSBC, MnemonicINC, MnemonicISC, // $fc-$ff
82 };
83
84
85 // output 2-digit hex argument with correct addressing mode
86 static void put_argument2(const char pre[], int byte, const char post[])
87 {
88 IO_put_string(pre);
89 IO_put_low_byte_hex(byte);
90 IO_put_string(post);
91 }
92
93
94 // output 4-digit hex argument with correct addressing mode
95 static void put_argument4(const char pre[], int word, const char post[])
96 {
97 IO_put_string(pre);
98 IO_put_low_16b_hex(word);
99 IO_put_string(post);
100 }
101
102
103 static int pc; // needed by "relative" addressing mode handler
104
105 // addressing mode handler functions
106 // all of these output the opcode's argument and return the number to add
107 // to the program counter
108
109 // addressing mode handler function for 1-byte-instructions
110 static int am_implied(void)
111 {
112 return 1;
113 }
114 // addressing mode handler functions for 2-byte-instructions
115 static int am_immediate(void)
116 {
117 put_argument2(" #$", IO_get_byte(), "");
118 if (GotByte > 15) {
119 fprintf(global_output_stream, " ; (= %d", GotByte);
120 if ((GotByte > 31) && (GotByte != 127))
121 fprintf(global_output_stream, " = '%c'", GotByte);
122 IO_put_byte(')');
123 }
124 return 2;
125 }
126 static int am_absolute8(void)
127 {
128 put_argument2(" $", IO_get_byte(), "");
129 return 2;
130 }
131 static int am_abs_x8(void)
132 {
133 put_argument2(" $", IO_get_byte(), ", x");
134 return 2;
135 }
136 static int am_abs_y8(void)
137 {
138 put_argument2(" $", IO_get_byte(), ", y");
139 return 2;
140 }
141 static int am_indirect_x(void)
142 {
143 put_argument2(" ($", IO_get_byte(), ", x)");
144 return 2;
145 }
146 static int am_indirect_y(void)
147 {
148 put_argument2(" ($", IO_get_byte(), "), y");
149 return 2;
150 }
151 static int am_relative(void)
152 {
153 put_argument4(" L", pc + 2 + (signed char) IO_get_byte(), "");
154 return 2;
155 }
156 // addressing mode handler functions for 3-byte-instructions
157 static int am_absolute16(void)
158 {
159 put_argument4(" L", IO_get_le16(), "");
160 return 3;
161 }
162 static int am_abs_x16(void)
163 {
164 put_argument4(" L", IO_get_le16(), ", x");
165 return 3;
166 }
167 static int am_abs_y16(void)
168 {
169 put_argument4(" L", IO_get_le16(), ", y");
170 return 3;
171 }
172 static int am_indirect16(void)
173 {
174 put_argument4(" (L", IO_get_le16(), ")");
175 return 3;
176 }
177
178 // 6502 code table (addressing mode handler functions)
179 // all ANC/DOP/TOP are given as "implied", so the argument is not processed
180 int (*addressing_mode_of_code[])(void) = {
181 am_implied, am_indirect_x, am_implied, am_indirect_x, // $00-$03
182 am_implied, am_absolute8, am_absolute8, am_absolute8, // $04-$07
183 am_implied, am_immediate, am_implied, am_implied, // $08-$0b
184 am_implied, am_absolute16, am_absolute16, am_absolute16, // $0c-$0f
185 am_relative, am_indirect_y, am_implied, am_indirect_y, // $10-$13
186 am_implied, am_abs_x8, am_abs_x8, am_abs_x8, // $14-$17
187 am_implied, am_abs_y16, am_implied, am_abs_y16, // $18-$1b
188 am_implied, am_abs_x16, am_abs_x16, am_abs_x16, // $1c-$1f
189 am_absolute16, am_indirect_x, am_implied, am_indirect_x, // $20-$23
190 am_absolute8, am_absolute8, am_absolute8, am_absolute8, // $24-$27
191 am_implied, am_immediate, am_implied, am_implied, // $28-$2b
192 am_absolute16, am_absolute16, am_absolute16, am_absolute16, // $2c-$2f
193 am_relative, am_indirect_y, am_implied, am_indirect_y, // $30-$33
194 am_implied, am_abs_x8, am_abs_x8, am_abs_x8, // $34-$37
195 am_implied, am_abs_y16, am_implied, am_abs_y16, // $38-$3b
196 am_implied, am_abs_x16, am_abs_x16, am_abs_x16, // $3c-$3f
197 am_implied, am_indirect_x, am_implied, am_indirect_x, // $40-$43
198 am_implied, am_absolute8, am_absolute8, am_absolute8, // $44-$47
199 am_implied, am_immediate, am_implied, am_immediate, // $48-$4b
200 am_absolute16, am_absolute16, am_absolute16, am_absolute16, // $4c-$4f
201 am_relative, am_indirect_y, am_implied, am_indirect_y, // $50-$53
202 am_implied, am_abs_x8, am_abs_x8, am_abs_x8, // $54-$57
203 am_implied, am_abs_y16, am_implied, am_abs_y16, // $58-$5b
204 am_implied, am_abs_x16, am_abs_x16, am_abs_x16, // $5c-$5f
205 am_implied, am_indirect_x, am_implied, am_indirect_x, // $60-$63
206 am_implied, am_absolute8, am_absolute8, am_absolute8, // $64-$67
207 am_implied, am_immediate, am_implied, am_immediate, // $68-$6b
208 am_indirect16, am_absolute16, am_absolute16, am_absolute16, // $6c-$6f
209 am_relative, am_indirect_y, am_implied, am_indirect_y, // $70-$73
210 am_implied, am_abs_x8, am_abs_x8, am_abs_x8, // $74-$77
211 am_implied, am_abs_y16, am_implied, am_abs_y16, // $78-$7b
212 am_implied, am_abs_x16, am_abs_x16, am_abs_x16, // $7c-$7f
213 am_implied, am_indirect_x, am_implied, am_indirect_x, // $80-$83
214 am_absolute8, am_absolute8, am_absolute8, am_absolute8, // $84-$87
215 am_implied, am_implied, am_implied, am_implied, // $88-$8b
216 am_absolute16, am_absolute16, am_absolute16, am_absolute16, // $8c-$8f
217 am_relative, am_indirect_y, am_implied, am_implied, // $90-$93
218 am_abs_x8, am_abs_x8, am_abs_y8, am_abs_y8, // $94-$97
219 am_implied, am_abs_y16, am_implied, am_implied, // $98-$9b
220 am_implied, am_abs_x16, am_abs_y16, am_implied, // $9c-$9f
221 am_immediate, am_indirect_x, am_immediate, am_indirect_x, // $a0-$a3
222 am_absolute8, am_absolute8, am_absolute8, am_absolute8, // $a4-$a7
223 am_implied, am_immediate, am_implied, am_implied, // $a8-$ab
224 am_absolute16, am_absolute16, am_absolute16, am_absolute16, // $ac-$af
225 am_relative, am_indirect_y, am_implied, am_indirect_y, // $b0-$b3
226 am_abs_x8, am_abs_x8, am_abs_y8, am_abs_y8, // $b4-$b7
227 am_implied, am_abs_y16, am_implied, am_implied, // $b8-$bb
228 am_abs_x16, am_abs_x16, am_abs_y16, am_abs_y16, // $bc-$bf
229 am_immediate, am_indirect_x, am_implied, am_indirect_x, // $c0-$c3
230 am_absolute8, am_absolute8, am_absolute8, am_absolute8, // $c4-$c7
231 am_implied, am_immediate, am_implied, am_immediate, // $c8-$cb
232 am_absolute16, am_absolute16, am_absolute16, am_absolute16, // $cc-$cf
233 am_relative, am_indirect_y, am_implied, am_indirect_y, // $d0-$d3
234 am_implied, am_abs_x8, am_abs_x8, am_abs_x8, // $d4-$d7
235 am_implied, am_abs_y16, am_implied, am_abs_y16, // $d8-$db
236 am_implied, am_abs_x16, am_abs_x16, am_abs_x16, // $dc-$df
237 am_immediate, am_indirect_x, am_implied, am_indirect_x, // $e0-$e3
238 am_absolute8, am_absolute8, am_absolute8, am_absolute8, // $e4-$e7
239 am_implied, am_immediate, am_implied, am_implied, // $e8-$eb
240 am_absolute16, am_absolute16, am_absolute16, am_absolute16, // $ec-$ef
241 am_relative, am_indirect_y, am_implied, am_indirect_y, // $f0-$f3
242 am_implied, am_abs_x8, am_abs_x8, am_abs_x8, // $f4-$f7
243 am_implied, am_abs_y16, am_implied, am_abs_y16, // $f8-$fb
244 am_implied, am_abs_x16, am_abs_x16, am_abs_x16, // $fc-$ff
245 };
246
247
248 // output mnemonic of given byte
249 static void output_mnemonic(int byte)
250 {
251 const char *mnemo = mnemo_of_code[byte];
252
253 if (mnemo)
254 IO_put_string(mnemo);
255 else
256 put_argument2("$", byte, "");
257 }
258
259
260 // main routine for disassembly
261 void obj_main(void)
262 {
263 IO_set_input_padding(0);
264 // process load address
265 pc = IO_get_le16();
266 put_argument4("\t\t*=$", pc, "\n");
267 IO_get_byte();
268 while (!IO_reached_eof) {
269 put_argument4("L", pc, "\t\t");
270 output_mnemonic(GotByte);
271 pc += addressing_mode_of_code[GotByte]();
272 IO_put_byte('\n');
273 IO_get_byte();
274 }
275 // report end-of-file
276 IO_put_string("; ToACME: Reached end-of-file.\n");
277 }