Debian Codegraph @

contrib/toacme/src/ab.c

raw symbols SCIP
1 // ToACME - converts other source codes to ACME format.
2 // Copyright (C) 1999-2025 Marco Baye
3 // Have a look at "main.c" for further info
4 //
5 // stuff needed for "VisAss", "AssBlaster 3.x" and/or "Flash8-AssBlaster"
6
7 #include <stdio.h>
8 #include "ab.h"
9 #include "acme.h"
10 #include "mnemo.h"
11 #include "io.h"
12 #include "scr2iso.h"
13
14
15 // comparison:
16 // VisAss AssBlaster 3.x F8-AssBlaster
17 // 00- Mnemonics?
18 // 48- PseudoOps
19 // 80..86 Mnemonics 80..db Mnemonics
20 // 87..95 Illegals
21 // 96..c7 Mnemonics
22 // c8..d4 PseudoOps dc..ec PseudoOps
23 // d5..fe unused ed..fe unused
24 // ff line mark ff line mark ff line mark
25
26 // Mnemonic table in VisAss/AssBlaster 3.x order
27 const char *visass_ab3_mnemonic_table[] = {
28 NULL, // unused
29 MnemonicCPX, MnemonicCPY,
30 MnemonicLDX, MnemonicLDY,
31 MnemonicSTX, MnemonicSTY,
32 //============================= start of illegals =============================
33 MnemonicSAX, // broken in VisAss/AB3, see docs (called AAX)
34 MnemonicASR, // broken in VisAss/AB3, see docs
35 MnemonicARR, // broken in VisAss/AB3, see docs
36 MnemonicSBX, // (called AXS)
37 MnemonicDCP,
38 MnemonicDOP, // ACME uses a different opcode
39 MnemonicISC,
40 MnemonicJAM, // ACME uses a different opcode (called KIL)
41 "!error \"See the ToACME docs about the illegal opcode LAR.\";",
42 // broken in VisAss/AB3? see docs
43 MnemonicLAX, // broken in VisAss/AB3, see docs
44 MnemonicRLA, MnemonicRRA,
45 MnemonicSLO, MnemonicSRE,
46 MnemonicTOP, // ACME uses a different opcode
47 //============================== end of illegals ==============================
48 MnemonicADC, MnemonicAND, MnemonicASL,
49 MnemonicBIT,
50 MnemonicBCS, MnemonicBEQ, MnemonicBCC, MnemonicBMI,
51 MnemonicBNE, MnemonicBPL, MnemonicBVS, MnemonicBVC,
52 MnemonicBRK,
53 MnemonicCLC, MnemonicCLD, MnemonicCLI, MnemonicCLV,
54 MnemonicCMP,
55 MnemonicDEC, MnemonicDEX, MnemonicDEY,
56 MnemonicEOR,
57 MnemonicINC, MnemonicINX, MnemonicINY,
58 MnemonicJMP, MnemonicJSR,
59 MnemonicLDA,
60 MnemonicLSR,
61 MnemonicNOP,
62 MnemonicORA,
63 MnemonicPHA, MnemonicPHP, MnemonicPLA, MnemonicPLP,
64 MnemonicROL, MnemonicROR,
65 MnemonicRTI, MnemonicRTS,
66 MnemonicSBC,
67 MnemonicSEC, MnemonicSED, MnemonicSEI,
68 MnemonicSTA,
69 MnemonicTAX, MnemonicTAY, MnemonicTSX,
70 MnemonicTXA, MnemonicTXS, MnemonicTYA,
71 };
72
73
74 // PseudoOpcode table in VisAss/AssBlaster 3.x order
75 const char *visass_ab3_pseudo_opcode_table[] = {
76 NULL, // la NULL because ACME does not need a pseudo opcode for label defs
77 ACME_set_pc, // ba
78 ACME_po_byte, // by
79 ACME_po_fill, // br
80 ACME_po_pet, // tx
81 ACME_po_macro, // md see AB_PSEUDOOFFSET_MACRODEF
82 ACME_endmacro, // me
83 ACME_macro_call, // ma see AB_PSEUDOOFFSET_MACROCALL
84 ACME_po_eof, // st
85 ACME_po_scr, // ts
86 ACME_po_to, // to see AB_PSEUDOOFFSET_OUTFILE
87 ACME_po_word, // wo
88 "; ToACME: Cannot convert \\kc.\n", // kc
89 nothing // "nothing"
90 };
91 // constants
92 const char nothing[] = "doesnotmatter"; // rename to visass_nopseudoopcode
93
94
95 void visass_ab3_illegals(void)
96 {
97 IO_put_string(
98 "; ToACME: Adding pseudo opcode to enable undocumented (\"illegal\") opcodes:\n"
99 "\t!cpu 6510\n"
100 "; ToACME: Support for illegal opcodes is somewhat broken in VisAss/AssBlaster.\n"
101 "; ToACME: Make sure you read the ToACME docs to know what you'll have to\n"
102 "; ToACME: look out for.\n"
103 "; ToACME: Should work: DCP, DOP, ISC, JAM (called KIL in VisAss/AssBlaster),\n"
104 "; ToACME: RLA, RRA, SBX (was called AXS in AssBlaster), SLO, SRE, TOP.\n"
105 "; ToACME: Trouble: ARR, ASR, LAX, SAX (called AAX in VisAss/AssBlaster).\n"
106 );
107 }
108
109
110 // constants
111
112 // generate error/warning messages
113 const char error_unknown_addressing[] = "Conversion failed: AssBlaster file contains unknown addressing mode.\n";
114 const char error_unknown_compression[] = "Conversion failed: AssBlaster file contains unknown number compression.\n";
115 const char warning_unknown_number_format[] = "Warning: AssBlaster file uses unknown number format. Fallback to hexadecimal.\n";
116
117 #define SCREENCODE_UPARROW (0x1e)
118 // replacement characters for problematic label names
119 #define AB_LABELSPECIAL_NUL ('O') // AssBlaster uses only lower case
120 #define AB_LABELSPECIAL_LEFT ('L') // characters for labels, so these
121 #define AB_LABELSPECIAL_BACK ('B') // shouldn't cause any clashes.
122 #define AB_LABELSPECIAL_RIGHT ('R')
123 #define AB_LABELSPECIAL_UP ('A')
124 // meaning of input bytes
125 // 0x01-0x1f lower case screen codes (used for label names and comments)
126 #define AB_SPACE 0x20
127 // 0x20-0x3a special characters
128 #define AB_COMMENT 0x3b
129 // 0x3c-0x40 unused ?
130 // 0x41-0x5f upper case screen codes (used for comments)
131 // 0x60-0x7f unused ?
132 // 0x80-0xec differ between AssBlaster 3.x and Flash8-AssBlaster
133 // 0xed-0xfe unused ?
134 // 0xff end-of-line
135 #define AB_PSEUDOOFFSET_MACRODEF 5 // in AB3 and F8AB
136 #define AB_PSEUDOOFFSET_MACROCALL 7 // indices in PO table
137 #define AB_PSEUDOOFFSET_OUTFILE 10 // are equal
138 // after mnemonic or pseudo opcode, numbers may follow:
139 #define AB_NUMVAL_FLAGBIT 0x80 // indicates packed number
140
141 // pre- and postfixes for addressing modes
142 // don't care whether argument is 8, 16 or 24 bits wide
143 const char *addressing_modes[][2] = {
144 {"", "" }, // ($00=%.....) implied
145 {" ", "" }, // ($01=%....1) absolute
146 {" ", ", x" }, // ($02=%...1.) absolute,x
147 {" ", ", y" }, // ($03=%...11) absolute,y
148 {" #", "" }, // ($04=%..1..) immediate
149 {NULL, NULL }, // ($05=%..1.1) unused (indirect-y)
150 {NULL, NULL }, // ($06=%..11.) unused (indirect-y)
151 {NULL, NULL }, // ($07=%..111) unused (indirect-y)
152 {" (", "), y" }, // ($08=%.1...) indirect-y
153 {" (", ", x)" }, // ($09=%.1..1) indirect-x
154 {" ", "" }, // ($0a=%.1.1.) relative (=absolute, actually)
155 {" (", ")" }, // ($0b=%.1.11) indirect
156 // above: used by both AB3 and F8AB (except $0a, which is no longer
157 // used by F8AB. But it's indistinguishable from $01 anyway).
158 // FIXME - what does AB3 do with the other unused addressing modes?
159 // I think old AB3 sources may also use mode 0c!
160 // below: used by F8AB only
161 {NULL, NULL }, // ($0c=%.11..) unused (indirect-x)
162 {" [", "]" }, // ($0d=%.11.1) indirect long
163 {NULL, NULL }, // ($0e=%.111.) unused (absolute)
164 {NULL, NULL }, // ($0f=%.1111) unused (absolute-x)
165 {" ", "" }, // ($10=%1....) MVP/MVN in F8AB: arg1.arg2
166 #define MVP_MVN_ADDRMODE 0x10
167 {NULL, NULL }, // ($11=%1...1) unused (indirect)
168 {NULL, NULL }, // ($12=%1..1.) unused (indirect long)
169 {" [", "], y" }, // ($13=%1..11) indirect-y long
170 {NULL, NULL }, // ($14=%1.1..) unused (absolute)
171 {" ", ", s" }, // ($15=%1.1.1) stack-relative
172 {" (", ", s), y" }, // ($16=%1.11.) stack-relative-indirect-y
173 // from here on, unused (indirect-y)
174 // addressing mode $10 (for MVP/MVN) is displayed and stored by F8AB
175 // as "arg1.arg2" instead of "arg1,arg2". Therefore the following
176 // constant is used to fix it on-the-fly.
177 };
178
179
180 // variables
181 struct vab *conf;
182
183
184 // functions
185
186
187 //
188 static void generate_errors(int err_bits)
189 {
190 if (err_bits & AB_ERRBIT_UNKNOWN_ADDRMODE) {
191 fputs(error_unknown_addressing, stderr);
192 fprintf(global_output_stream, "; ToACME: %s", error_unknown_addressing);
193 }
194 if (err_bits & AB_ERRBIT_UNKNOWN_NUMBER_COMPRESSION) {
195 fputs(error_unknown_compression, stderr);
196 fprintf(global_output_stream, "; ToACME: %s", error_unknown_compression);
197 }
198 if (err_bits & AB_ERRBIT_UNKNOWN_NUMBER_FORMAT) {
199 fputs(warning_unknown_number_format, stderr);
200 fprintf(global_output_stream, "; ToACME: %s", warning_unknown_number_format);
201 }
202 }
203
204
205 // convert macro/label name character.
206 // AssBlaster allows '^', '[' and ']' in names, so replace these chars.
207 static char conv_name_char(char byte)
208 {
209 byte = SCR2ISO(byte);
210 switch (byte) {
211 case 0x40:
212 return AB_LABELSPECIAL_NUL;
213 case '[':
214 return AB_LABELSPECIAL_LEFT;
215 case '\\':
216 return AB_LABELSPECIAL_BACK;
217 case ']':
218 return AB_LABELSPECIAL_RIGHT;
219 case '^':
220 return AB_LABELSPECIAL_UP;
221 default:
222 return byte;
223 }
224 }
225
226
227 // output binary representation of value
228 void AB_output_binary(unsigned long int value)
229 {
230 int mask = 128;
231
232 if (value > 0xff)
233 AB_output_binary(value >> 8);
234 value &= 0xff;
235 while (mask) {
236 IO_put_byte((value & mask) ? '1' : '0');
237 mask >>= 1;
238 }
239 }
240
241
242 // output hex representation of value
243 void AB_output_hexadecimal(unsigned long int value)
244 {
245 if (value > 0xff)
246 AB_output_hexadecimal(value >> 8);
247 IO_put_low_byte_hex(value);
248 }
249
250
251 // convert and send macro/label name (until illegal character comes along)
252 static void pipe_global_name(void)
253 {
254 while ((GotByte < 0x20) || ((GotByte >= '0') && (GotByte <= '9'))) {
255 IO_put_byte(conv_name_char(GotByte));
256 IO_get_byte();
257 }
258 }
259
260
261 // convert and send label name (until illegal character comes along)
262 // level 1
263 static void pipe_name(void)
264 {
265 if (conf->flags & VABFLAG_ADD_DOT) {
266 // Dieser kleine Hack macht alle lokalen ABL-Labels
267 // Auch unter ACME lokal. nur mit '^' global markierte
268 // Labels werden auch global übernommen ...
269 if (GotByte == SCREENCODE_UPARROW)
270 IO_get_byte(); // global: ^witharrow => witharrow
271 else
272 IO_put_byte('.'); // local: allothers => .allothers
273 }
274 pipe_global_name(); // this does exactly what is needed
275 }
276
277
278 // parse quoted strings
279 static void parse_quoted(void) // now GotByte = unhandled opening quote
280 {
281 IO_put_byte('"');
282 IO_get_byte();
283 while ((GotByte != AB_ENDOFLINE) && (GotByte != '"')) {
284 IO_put_byte(SCR2ISO(GotByte));
285 IO_get_byte();
286 }
287 IO_put_byte('"');
288 // closing quote is handled, but EndOfLine must remain unhandled
289 if (GotByte == '"')
290 IO_get_byte();
291 }
292
293
294 // parse label names, quoted strings, operators, literal values etc.
295 // read until AB_ENDOFLINE or AB_COMMENT. Returns error bits.
296 // level 1
297 // AB uses a full stop character ('.') in some inconvenient places, for example
298 // after macro names (at definitions and calls) and in the MVP/MVN addressing
299 // mode. The kluge variable "dot_replacement" is used to replace the '.'
300 // character with the correct character for ACME.
301 static int parse_unspecified(char dot_replacement)
302 {
303 int err_bits = 0;
304
305 while ((GotByte != AB_ENDOFLINE) && (GotByte != AB_COMMENT)) {
306 // kluge: replace '.' character with current replacement and
307 // remember not to replace anymore from now on.
308 if (GotByte == '.') {
309 GotByte = dot_replacement; // use replacement
310 dot_replacement = '.'; // in future, keep
311 }
312 if (GotByte & AB_NUMVAL_FLAGBIT) {
313 err_bits |= conf->number_parser();
314 continue;
315 }
316 if (GotByte < 0x20) {
317 pipe_name();
318 continue;
319 }
320 if (GotByte == '"') {
321 parse_quoted();
322 continue;
323 }
324 IO_put_byte(SCR2ISO(GotByte));
325 IO_get_byte();
326 }
327 return err_bits;
328 }
329
330
331 // parse macro call or start of definition (beware of full stops).
332 // returns error bits.
333 static int parse_macro_stuff(void) // now GotByte = unhandled byte
334 {
335 // I guess local macros are useless, so don't
336 // do the scope fixing as for macro names!
337 pipe_global_name();
338 return parse_unspecified(SPACE); // output macro arguments
339 }
340
341
342 // process mnemonics (real opcodes). returns error bits.
343 // level 1
344 static int parse_mnemo(int mnemonic_offset)
345 {
346 const char *mnemonic,
347 *pre,
348 *post;
349 int addressing_mode,
350 dot_replacement = '.',
351 err_bits = 0;
352
353 if (conf->address_mode_count > 2) {
354 addressing_mode = IO_get_byte(); // get addressing mode
355 } else {
356 addressing_mode = 1; // dummy mode for VisAss
357 }
358 IO_get_byte(); // and fetch next (not handled here)
359 mnemonic = conf->mnemonics[mnemonic_offset];
360 if (mnemonic == NULL) {
361 fputs("Found unused mnemo code in input file.\n", stderr);
362 mnemonic = "!error \"ToACME found unused mnemo code in input file\":";
363 }
364 fprintf(global_output_stream, "\t\t%s", mnemonic);
365 // determine prefix and postfix of addressing mode
366 if (addressing_mode < conf->address_mode_count) {
367 pre = addressing_modes[addressing_mode][0];
368 post = addressing_modes[addressing_mode][1];
369 if (addressing_mode == MVP_MVN_ADDRMODE)
370 dot_replacement = ','; // replace '.' with ','
371 } else {
372 pre = NULL;
373 post = NULL;
374 }
375 // if addressing mode is invalid, set error bit
376 // output prefix (or space if invalid)
377 if ((pre == NULL) || (post == NULL)) {
378 err_bits |= AB_ERRBIT_UNKNOWN_ADDRMODE;
379 fprintf(stderr, "Found an unknown addressing mode bit pattern ($%x). Please tell my programmer.\n", addressing_mode);
380 }
381 if (pre)
382 IO_put_string(pre);
383 else
384 IO_put_byte(SPACE);
385 err_bits |= parse_unspecified(dot_replacement); // output arg
386 if (post)
387 IO_put_string(post);
388 return err_bits;
389 }
390
391
392 // process pseudo opcodes. returns error bits.
393 // level 1
394 static int parse_pseudo_opcode(int pseudo_offset)
395 {
396 const char *pseudo_opcode;
397 int err_bits = 0;
398
399 IO_get_byte(); // and fetch next (not handled here)
400 pseudo_opcode = conf->pseudo_opcodes[pseudo_offset];
401 if (pseudo_opcode != nothing)
402 IO_put_string("\t\t");
403 else
404 pseudo_opcode = NULL;
405 if (pseudo_opcode) {
406 IO_put_string(pseudo_opcode);
407 }
408 // check for special cases
409 switch (pseudo_offset) {
410 case AB_PSEUDOOFFSET_MACROCALL: // (in ACME: '+')
411 // ACME does not like spaces after the macro call char
412 err_bits |= parse_macro_stuff();
413 break;
414 case AB_PSEUDOOFFSET_MACRODEF: // macro definition
415 IO_put_byte(SPACE); // but here a space looks good :)
416 err_bits |= parse_macro_stuff();
417 IO_put_string(" {");
418 break;
419 case AB_PSEUDOOFFSET_OUTFILE: // outfile selection
420 IO_put_byte(SPACE); // but here a space looks good :)
421 err_bits |= parse_unspecified('.'); // output arg(s)
422 IO_put_string(ACME_cbmformat);
423 break;
424 default: // all other pseudo opcodes
425 if ((pseudo_opcode)
426 && (GotByte != AB_ENDOFLINE)
427 && (GotByte != AB_COMMENT))
428 IO_put_byte(SPACE); // but here a space looks good :)
429 err_bits |= parse_unspecified('.'); // output pseudo opcode's arg(s)
430 }
431 return err_bits;
432 }
433
434
435 // main routine for AssBlaster conversion (works for both AB3 and F8AB).
436 // call with first byte of first line pre-read (in GotByte)!
437 void AB_main(struct vab *client_config)
438 {
439 int err_bits;
440 const char *comment_indent;
441 int handled;
442
443 conf = client_config;
444 ACME_switch_to_pet();
445 // convert lines until EndOfFile
446 while (!IO_reached_eof) {
447 err_bits = 0; // no errors yet (in this line)
448 handled = 0;
449 if (conf->flags & VABFLAG_HASLENGTHBYTE) {
450 // ignore length byte
451 IO_get_byte();
452 }
453 comment_indent = "\t";
454 if (GotByte >= conf->first_mnemonic
455 && (GotByte < conf->first_mnemonic + conf->mnemonic_count)) {
456 handled = 1;
457 err_bits |= parse_mnemo(GotByte - conf->first_mnemonic);
458 }
459 if (GotByte >= conf->first_pseudo_opcode
460 && (GotByte < conf->first_pseudo_opcode + conf->pseudo_opcode_count)) {
461 handled = 1;
462 err_bits |= parse_pseudo_opcode(GotByte - conf->first_pseudo_opcode);
463 }
464 if (GotByte >= conf->first_unused_byte_value
465 && (GotByte != AB_ENDOFLINE)) {
466 handled = 1;
467 fprintf(global_output_stream, "; ToACME: AssBlaster file used unknown code ($%x). ", GotByte);
468 IO_get_byte(); // fetch next
469 err_bits |= parse_unspecified('.'); // output remainder
470 }
471 if (handled == 0) {
472 switch (GotByte) {
473 case 0: // empty line
474 IO_get_byte(); // skip this byte
475 break;
476 case AB_COMMENT: // early comment
477 comment_indent = "";
478 break; // ignore now, act later
479 case AB_SPACE: // empty line or late comment
480 comment_indent = "\t\t\t\t";
481 IO_get_byte(); // skip this space
482 // output whatever found
483 err_bits |= parse_unspecified('.');
484 break;
485 default: // implied label definition
486 pipe_name();
487 }
488 }
489
490 // everything might be followed by a comment, so check
491 if (GotByte == AB_COMMENT) {
492 // skip empty comments by checking next byte
493 if (IO_get_byte() != AB_ENDOFLINE) {
494 // something's there, so pipe until end of line
495 IO_put_string(comment_indent);
496 IO_put_byte(';');
497 do
498 IO_put_byte(SCR2ISO(GotByte));
499 while (IO_get_byte() != AB_ENDOFLINE);
500 }
501 }
502
503 // now check whether line generated any errors
504 if (err_bits)
505 generate_errors(err_bits);
506
507 // if not at end-of-line, something's fishy
508 if (GotByte != AB_ENDOFLINE) {
509 if (GotByte == '\0') {
510 IO_put_string("; ToACME: found $00 - looks like end-of-file marker.");
511 } else {
512 fputs("Found data instead of end-of-line indicator!?.\n", stderr);
513 IO_put_string("; ToACME: Garbage at end-of-line:");
514 do
515 IO_put_byte(SCR2ISO(GotByte));
516 while (IO_get_byte() != AB_ENDOFLINE);
517 }
518 }
519 IO_put_byte('\n');
520 // read first byte of next line
521 IO_get_byte();
522 }
523 }