Debian Codegraph @

contrib/toacme/src/f8ab.c

raw symbols SCIP
1 // ToACME - converts other source codes to ACME format.
2 // Copyright (C) 1999-2015 Marco Baye
3 // Have a look at "main.c" for further info
4 //
5 // Flash8-AssBlaster stuff
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include "config.h"
10 #include "ab.h"
11 #include "acme.h"
12 #include "mnemo.h"
13 #include "io.h"
14 #include "scr2iso.h"
15
16
17 // constants
18
19 #define F8AB_ADDRESSING_MODES 23 // (FIXME - check back later!)
20
21 // mnemonic table in Flash8-AssBlaster order (without: JML, WDM)
22 static const char *mnemonic_table[] = {
23 MnemonicADC, // $80 6502
24 MnemonicAND, // $81 6502
25 MnemonicASL, // $82 6502
26 MnemonicBCC, // $83 6502
27 MnemonicBCS, // $84 6502
28 MnemonicBEQ, // $85 6502
29 MnemonicBIT, // $86 6502
30 MnemonicBMI, // $87 6502
31 MnemonicBNE, // $88 6502
32 MnemonicBPL, // $89 6502
33 MnemonicBRA, // $8a 65c02
34 MnemonicBRK, // $8b 6502
35 MnemonicBRL, // $8c 65816
36 MnemonicBVC, // $8d 6502
37 MnemonicBVS, // $8e 6502
38 MnemonicCLC, // $8f 6502
39 MnemonicCLD, // $90 6502
40 MnemonicCLI, // $91 6502
41 MnemonicCLV, // $92 6502
42 MnemonicCMP, // $93 6502
43 MnemonicCOP, // $94 65816
44 MnemonicCPX, // $95 6502
45 MnemonicCPY, // $96 6502
46 MnemonicDEC, // $97 F8AB uses DEA as the 65816's "DEC implied"
47 MnemonicDEC, // $98 6502
48 MnemonicDEX, // $99 6502
49 MnemonicDEY, // $9a 6502
50 MnemonicEOR, // $9b 6502
51 MnemonicINC, // $9c F8AB uses INA as the 65816's "INC implied"
52 MnemonicINC, // $9d 6502
53 MnemonicINX, // $9e 6502
54 MnemonicINY, // $9f 6502
55 // MnemonicJML (65816) seems to be unknown to F8AB ...
56 MnemonicJMP, // $a0 6502
57 MnemonicJSL, // $a1 65816 ...but it *does* know JSL? Strange.
58 MnemonicJSR, // $a2 6502
59 MnemonicLDA, // $a3 6502
60 MnemonicLDX, // $a4 6502
61 MnemonicLDY, // $a5 6502
62 MnemonicLSR, // $a6 6502
63 "+F8AB_BROKEN_MVN", // $a7 65816 F8AB uses non-standard argument
64 "+F8AB_BROKEN_MVP", // $a8 65816 ordering with MVP/MVN
65 MnemonicNOP, // $a9 6502
66 MnemonicORA, // $aa 6502
67 MnemonicPEA, // $ab 65816
68 MnemonicPEI, // $ac 65816
69 MnemonicPER, // $ad 65816
70 MnemonicPHA, // $ae 6502
71 MnemonicPHB, // $af 65816
72 MnemonicPHD, // $b0 65816
73 MnemonicPHK, // $b1 65816
74 MnemonicPHP, // $b2 6502
75 MnemonicPHX, // $b3 65c02
76 MnemonicPHY, // $b4 65c02
77 MnemonicPLA, // $b5 6502
78 MnemonicPLB, // $b6 65816
79 MnemonicPLD, // $b7 65816
80 MnemonicPLP, // $b8 6502
81 MnemonicPLX, // $b9 65c02
82 MnemonicPLY, // $ba 65c02
83 MnemonicREP, // $bb 65816
84 MnemonicROL, // $bc 6502
85 MnemonicROR, // $bd 6502
86 MnemonicRTI, // $be 6502
87 MnemonicRTL, // $bf 65816
88 MnemonicRTS, // $c0 6502
89 MnemonicSBC, // $c1 6502
90 MnemonicSED, // $c2 6502 strange order - SED before SEC?
91 MnemonicSEC, // $c3 6502
92 MnemonicSEI, // $c4 6502
93 MnemonicSEP, // $c5 65816
94 MnemonicSTA, // $c6 6502
95 MnemonicSTP, // $c7 65816
96 MnemonicSTX, // $c8 6502
97 MnemonicSTY, // $c9 6502
98 MnemonicSTZ, // $ca 65c02
99 MnemonicTAX, // $cb 6502
100 MnemonicTAY, // $cc 6502
101 MnemonicTCD, // $cd 65816
102 MnemonicTCS, // $ce 65816
103 MnemonicTDC, // $cf 65816
104 MnemonicTRB, // $d0 65c02
105 MnemonicTSB, // $d1 65c02
106 MnemonicTSC, // $d2 65816
107 MnemonicTSX, // $d3 6502
108 MnemonicTXA, // $d4 6502
109 MnemonicTXS, // $d5 6502
110 MnemonicTXY, // $d6 65816
111 MnemonicTYA, // $d7 6502
112 MnemonicTYX, // $d8 65816
113 MnemonicWAI, // $d9 65816
114 // MnemonicWDM (65816) seems to be unknown to F8AB.
115 MnemonicXBA, // $da 65816
116 MnemonicXCE, // $db 65816
117 };
118
119
120 // PseudoOpcode table in Flash8-AssBlaster order
121 static const char *pseudo_opcode_table[] = {
122 NULL, // (la) $dc // NULL because ACME does not need a pseudo opcode for label defs
123 ACME_set_pc, // (ba) $dd
124 ACME_po_byte, // (by) $de
125 ACME_po_fill, // (br) $df
126 ACME_po_pet, // (tx) $e0
127 ACME_po_macro, // (md) $e1 (see AB_PSEUDOOFFSET_MACRODEF)
128 ACME_endmacro, // (de) $e2
129 ACME_macro_call, // (ma) $e3 (see AB_PSEUDOOFFSET_MACROCALL)
130 ACME_po_eof, // (st) $e4
131 // ACME_po_scr is not available in F8AB. Huh?!
132 "; ToACME: Cannot convert \\wa.\n", // (wa) $e5
133 ACME_po_to, // (on) $e6 (see AB_PSEUDOOFFSET_OUTFILE)
134 ACME_po_word, // (wo) $e7
135 "; ToACME: Cannot convert \\kc.\n", // (kc) $e8
136 ACME_po_rl, // (rl) $e9
137 ACME_po_rs, // (rs) $ea
138 ACME_po_al, // (al) $eb
139 ACME_po_as, // (as) $ec
140 // 0xed-0xfe are unused in F8AB
141 // (FIXME - true? I only checked 0xed)
142 };
143
144
145 // parse AssBlaster's packed number format. returns error bits.
146 //#define AB_NUMVAL_FLAGBIT 0x80 // 10000000 indicates packed number
147 #define F8AB_NUMVAL_ADD_65536 0x40 // 01000000
148 #define F8AB_NUMVAL_ADD_256 0x20 // 00100000
149 #define F8AB_NUMVAL_ADD_1 0x10 // 00010000
150 #define F8AB_NUMVAL_FORMATMASK 0x0c // 00001100
151 #define F8AB_NUMVAL__FORMAT_BIN 0x00 // 00000000
152 #define F8AB_NUMVAL__FORMAT_DEC 0x04 // 00000100
153 #define F8AB_NUMVAL__FORMAT_HEX 0x08 // 00001000
154 #define F8AB_NUMVAL__FORMAT_ILL 0x0c // 00001100 never used by F8AB
155 #define F8AB_NUMVAL_SIZEMASK 0x03 // 00000011
156 #define F8AB_NUMVAL__SIZE_0 0x00 // 00000000
157 #define F8AB_NUMVAL__SIZE_1 0x01 // 00000001
158 #define F8AB_NUMVAL__SIZE_2 0x02 // 00000010
159 #define F8AB_NUMVAL__SIZE_3 0x03 // 00000011
160 static int parse_number(void) // now GotByte = first byte of packed number
161 {
162 int flags = GotByte,
163 err_bits = 0;
164 unsigned long int value = 0,
165 add = 0;
166
167 // decode value
168 if (flags & F8AB_NUMVAL_ADD_65536)
169 add += 65536;
170 if (flags & F8AB_NUMVAL_ADD_256)
171 add += 256;
172 if (flags & F8AB_NUMVAL_ADD_1)
173 add += 1;
174 switch (flags & F8AB_NUMVAL_SIZEMASK) {
175 case F8AB_NUMVAL__SIZE_0: // no bytes follow (0, 1, 256, 257)
176 value = add;
177 break;
178 case F8AB_NUMVAL__SIZE_1: // one byte follows (2 to 511)
179 value = add + IO_get_byte();
180 break;
181 case F8AB_NUMVAL__SIZE_2: // two bytes follow (512 to 65535)
182 value = add + IO_get_le16();
183 break;
184 case F8AB_NUMVAL__SIZE_3: // three bytes follow (anything else)
185 value = add + IO_get_le24();
186 }
187 // continue parsing on next byte
188 IO_get_byte();
189
190 // decode output format
191 switch (flags & F8AB_NUMVAL_FORMATMASK) {
192 case F8AB_NUMVAL__FORMAT_BIN:
193 IO_put_byte('%');
194 AB_output_binary(value);
195 break;
196 case F8AB_NUMVAL__FORMAT_DEC:
197 fprintf(global_output_stream, "%lu", value);
198 break;
199 case F8AB_NUMVAL__FORMAT_HEX:
200 hex_fallback: IO_put_byte('$');
201 AB_output_hexadecimal(value);
202 break;
203 default: // unknown output format
204 // remember to warn
205 err_bits |= AB_ERRBIT_UNKNOWN_NUMBER_FORMAT;
206 goto hex_fallback;
207 }
208 return err_bits;
209 }
210
211
212 // config struct for shared ab code
213 struct vab f8ab_conf = {
214 VABFLAG_ADD_DOT,
215 parse_number,
216 pseudo_opcode_table,
217 mnemonic_table,
218 F8AB_ADDRESSING_MODES,
219 // meaning of input bytes (0x80-0xec differ between AB3 and F8AB)
220 0x80, // first mnemonic
221 92, // count
222 0xdc, // first pseudo opcode
223 17, // count
224 0xed, // first unused value
225 18 // count
226 };
227
228
229 // main
230 void f8ab_main(void) {
231 const char *header_message;
232
233 header_message = "Input does not have any known F8AB header.\n";
234 IO_set_input_padding(AB_ENDOFLINE);
235 IO_put_string(
236 "; ToACME: Adding pseudo opcode to enable 65816 opcodes:\n"
237 "\t!cpu 65816\n"
238 "; ToACME: Adding two macros to fix F8AB's non-standard argument order\n"
239 "; ToACME: concerning MVP/MVN. While the commands are assembled with\n"
240 "; ToACME: the destination bank byte first, the WDC docs say that in\n"
241 "; ToACME: source codes, the source bank byte is given first.\n"
242 "; ToACME: In other words: The macros make sure that assembling this\n"
243 "; ToACME: source with ACME will produce the same binary F8AB produced.\n"
244 "\t!macro F8AB_BROKEN_MVP .dest, .source {mvp .source, .dest}\n"
245 "\t!macro F8AB_BROKEN_MVN .dest, .source {mvn .source, .dest}\n"
246 );
247 IO_process_load_address();
248 // most AB files have this format:
249 // load_address_low, load_address_high, AB_ENDOFLINE, actual content
250 // newer versions of F8AB seem to use this:
251 // $ff, $00, $00, $03, AB_ENDOFLINE, actual content
252 if (IO_get_byte() == AB_ENDOFLINE) {
253 IO_get_byte(); // skip it and pre-read first valid byte
254 header_message = "Input has F8AB 1.0 header.\n";
255 } else {
256 if ((GotByte == 0)
257 && (IO_get_byte() == 3)
258 && (IO_get_byte() == AB_ENDOFLINE)) {
259 IO_get_byte();// skip and pre-read first valid byte
260 header_message = "Input has F8AB 1.2 header.\n";
261 }
262 }
263 fputs(header_message, stderr);
264 AB_main(&f8ab_conf);
265 }