Debian Codegraph @

src/mnef8.c

raw symbols SCIP
1 /*
2 the DASM macro assembler (aka small systems cross assembler)
3
4 Copyright (c) 1988-2002 by Matthew Dillon.
5 Copyright (c) 1995 by Olaf "Rhialto" Seibert.
6 Copyright (c) 2003-2008 by Andrew Davie.
7 Copyright (c) 2008 by Peter H. Froehlich.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 /*
25 * MNEF8.C
26 *
27 * Fairchild F8 support code for DASM
28 * Copyright (c) 2004 by Thomas Mathys.
29 */
30
31 #include <ctype.h>
32 #include <strings.h>
33
34 #include "asm.h"
35
36 /*
37 * special registers. must use numbers from 16 and up,
38 * since numbers below 16 are used for scratchpad registers.
39 *
40 * there is no REG_J, since J is really just scratchpad register 9.
41 */
42 enum REGISTERS {
43 REG_A = 16,
44 REG_DC0,
45 REG_H,
46 REG_IS,
47 REG_K,
48 REG_KU,
49 REG_KL,
50 REG_PC0,
51 REG_PC1,
52 REG_Q,
53 REG_QU,
54 REG_QL,
55 REG_W,
56 REG_NONE,
57 };
58
59
60 /*
61 * used to print error messages.
62 * mnename and opstring are copied into a single error message,
63 * which is passed to asmerr.
64 *
65 * err : error code (ERROR_xxx constant, passed to asmerr)
66 * mnename : name of the mnemonic
67 * opstring : operand string
68 * abort : false = don't abort assembly
69 * true = abort assembly
70 */
71 static void f8err(int err, const char *mnename, const char *opstring, bool bAbort) {
72
73 char *buf;
74
75 buf = ckmalloc(strlen(mnename) + strlen(opstring) + 64);
76 strcpy(buf, mnename);
77 strcat(buf, " ");
78 strcat(buf, opstring);
79 asmerr(err, bAbort, buf);
80 free(buf);
81 }
82
83
84 /*
85 * emits a one byte opcode.
86 */
87 static void emit_opcode1(unsigned char opcode) {
88 Glen = 1;
89 Gen[0] = opcode;
90 generate();
91 }
92
93
94 /*
95 * emits a two byte opcode
96 *
97 * byte0 : first byte (lower address)
98 * byte1 : second byte (higher address)
99 */
100 static void emit_opcode2(unsigned char byte0, unsigned char byte1) {
101 Glen = 2;
102 Gen[0] = byte0;
103 Gen[1] = byte1;
104 generate();
105 }
106
107
108 /*
109 * emits a three byte opcode
110 *
111 * byte0 : first byte (lowest address)
112 * byte1 : second byte (middle address)
113 * byte2 : third byte (highest address)
114 */
115 static void emit_opcode3(unsigned char byte0, unsigned char byte1, unsigned char byte2) {
116 Glen = 3;
117 Gen[0] = byte0;
118 Gen[1] = byte1;
119 Gen[2] = byte2;
120 generate();
121 }
122
123
124
125 /*
126 * check wether the current program counter is known.
127 *
128 * result : zero = current program counter is unknown
129 * nonzero = current program counter is known
130 */
131 static int isPCKnown(void) {
132 unsigned char pcf;
133 pcf= (Csegment->flags & SF_RORG) ? Csegment->rflags : Csegment->flags;
134 return ((pcf & (SF_UNKNOWN|2)) == 0) ? 1 : 0;
135 }
136
137
138 /*
139 * returns the current program counter
140 */
141 static long getPC(void) {
142 return (Csegment->flags & SF_RORG) ? Csegment->rorg : Csegment->org;
143 }
144
145
146 /*
147 * attempts to parse a 32 bit unsigned value from a string.
148 *
149 * str : string to parse the value from
150 * value : parsed value is stored here
151 *
152 * result : zero = ok or syntax error
153 * nonzero = unresolved expression
154 */
155 static int parse_value(char *str, unsigned long *value) {
156
157 SYMBOL *sym;
158 int result = 0;
159
160 *value = 0;
161 sym = eval(str, 0);
162
163 if (NULL != sym->next || AM_BYTEADR != sym->addrmode) {
164 asmerr(ERROR_SYNTAX_ERROR, true, str);
165 }
166 else if (sym->flags & SYM_UNKNOWN) {
167 ++Redo;
168 Redo_why |= REASON_MNEMONIC_NOT_RESOLVED;
169 result = 1;
170 }
171 else {
172 *value = sym->value;
173 }
174 FreeSymbolList(sym);
175
176 return result;
177 }
178
179
180 /*
181 * attempts to parse a scratchpad register name.
182 * register numbers are parsed as expressions.
183 * if an expression is invalid, asmerr is called
184 * and the assembly aborted.
185 *
186 * accepts the following input:
187 *
188 * - numbers 0..14 (as expressions, numbers 12-14 map to S, I and D)
189 * - J (alias for register 9)
190 * - HU (alias for register 10)
191 * - HL (alias for register 11)
192 * - S and (IS)
193 * - I and (IS)+
194 * - D and (IS)-
195 *
196 * str : string to parse the scratchpad register from
197 * reg : parsed scratchpad register is stored here.
198 * this is the value which will become the lower
199 * nibble of the opcodes.
200 *
201 * result : zero = ok or syntax error
202 * nonzero = unresolved expression
203 */
204 static int parse_scratchpad_register(char *str, unsigned char *reg) {
205
206 unsigned long regnum;
207
208 /* parse special cases where ISAR is used as index */
209 if (!strcasecmp("s", str) || !strcasecmp("(is)", str)) {
210 *reg = 0x0c;
211 return 0;
212 }
213 if (!strcasecmp("i", str) || !strcasecmp("(is)+", str)) {
214 *reg = 0x0d;
215 return 0;
216 }
217 if (!strcasecmp("d", str) || !strcasecmp("(is)-", str)) {
218 *reg = 0x0e;
219 return 0;
220 }
221
222 /* parse aliases for scratchpad registers */
223 if (!strcasecmp("j", str)) {
224 *reg = 0x09;
225 return 0;
226 }
227 if (!strcasecmp("hu", str)) {
228 *reg = 0x0a;
229 return 0;
230 }
231 if (!strcasecmp("hl", str)) {
232 *reg = 0x0b;
233 return 0;
234 }
235
236 /* parse register number */
237 if (parse_value(str, &regnum)) {
238 return 1; /* unresolved expr */
239 } else {
240 if (regnum > 14) {
241 asmerr(ERROR_VALUE_MUST_BE_LT_F, true, str);
242 }
243 *reg = regnum;
244 return 0;
245 }
246 }
247
248
249 /*
250 * attempts to parse a special register name from str
251 *
252 * result : one of the REG_xxx constants (possibly also REG_NONE)
253 */
254 static int parse_special_register(char *str) {
255
256 if (!strcasecmp("a", str)) {
257 return REG_A;
258 }
259 if (!strcasecmp("dc0", str) || !strcasecmp("dc", str) ) {
260 return REG_DC0;
261 }
262 if (!strcasecmp("h", str)) {
263 return REG_H;
264 }
265 if (!strcasecmp("is", str)) {
266 return REG_IS;
267 }
268 if (!strcasecmp("k", str)) {
269 return REG_K;
270 }
271 if (!strcasecmp("ku", str)) {
272 return REG_KU;
273 }
274 if (!strcasecmp("kl", str)) {
275 return REG_KL;
276 }
277 if (!strcasecmp("pc0", str) || !strcasecmp("p0", str)) {
278 return REG_PC0;
279 }
280 if (!strcasecmp("pc1", str) || !strcasecmp("p", str)) {
281 return REG_PC1;
282 }
283 if (!strcasecmp("q", str)) {
284 return REG_Q;
285 }
286 if (!strcasecmp("qu", str)) {
287 return REG_QU;
288 }
289 if (!strcasecmp("ql", str)) {
290 return REG_QL;
291 }
292 if (!strcasecmp("w", str)) {
293 return REG_W;
294 }
295 else {
296 return REG_NONE;
297 }
298 }
299
300
301 static void v_ins_outs(char *str, MNEMONIC *mne) {
302
303 unsigned long operand;
304
305 programlabel();
306 parse_value(str, &operand);
307 if (operand > 15) {
308 f8err(ERROR_VALUE_MUST_BE_LT_10, mne->name, str, false);
309 }
310 emit_opcode1(mne->opcode[0] | (operand & 15));
311 }
312
313
314 static void v_sl_sr(char *str, MNEMONIC *mne) {
315
316 unsigned long operand;
317
318 programlabel();
319
320 if (parse_value(str, &operand)) {
321 /* unresolved expression, reserve space */
322 emit_opcode1(0);
323 } else {
324 switch (operand) {
325 case 1:
326 emit_opcode1(mne->opcode[0]);
327 break;
328 case 4:
329 emit_opcode1(mne->opcode[0] + 2);
330 break;
331 default:
332 f8err(ERROR_VALUE_MUST_BE_1_OR_4, mne->name, str, false);
333 emit_opcode1(0);
334 break;
335 }
336 }
337 }
338
339
340 static void v_lis(char *str, MNEMONIC *mne) {
341
342 unsigned long operand;
343
344 programlabel();
345 parse_value(str, &operand);
346 if (operand > 15) {
347 f8err(ERROR_VALUE_MUST_BE_LT_10, mne->name, str, false);
348 }
349 emit_opcode1(0x70 | (operand & 15));
350 }
351
352
353 static void v_lisu_lisl(char *str, MNEMONIC *mne) {
354
355 unsigned long operand;
356
357 programlabel();
358 parse_value(str, &operand);
359 if (operand > 7) {
360 f8err(ERROR_VALUE_MUST_BE_LT_8, mne->name, str, false);
361 }
362 emit_opcode1(mne->opcode[0] | (operand & 7));
363 }
364
365
366 /*
367 * handles opcodes with a scratchpad register operand:
368 * as, asd, ds, ns, xs
369 */
370 static void v_sreg_op(char *str, MNEMONIC *mne) {
371
372 unsigned char reg;
373
374 programlabel();
375 parse_scratchpad_register(str, &reg);
376 emit_opcode1(mne->opcode[0] | reg);
377 }
378
379
380 static void v_lr(char *str, MNEMONIC *mne) {
381
382 int i;
383 int ncommas;
384 int cindex;
385 char *op1;
386 char *op2;
387 unsigned char reg_dst;
388 unsigned char reg_src;
389 int opcode;
390
391 programlabel();
392
393 /* a valid operand string must contain exactly one comma. find it. */
394 ncommas = 0;
395 cindex = 0;
396 for (i=0; str[i]; i++) {
397 if (',' == str[i]) {
398 ncommas++;
399 cindex = i;
400 }
401 }
402 if (1 != ncommas) {
403 f8err(ERROR_SYNTAX_ERROR, mne->name, str, false);
404 return;
405 }
406
407 /* extract operand strings */
408 str[cindex] = 0;
409 op1 = str;
410 op2 = &str[cindex+1];
411 if ( (0 != cindex) && (isspace(str[cindex-1])) ) {
412 str[cindex-1] = 0;
413 }
414 if (isspace(*op2)) {
415 op2++;
416 }
417
418 /* parse operand strings for register names */
419 reg_dst = parse_special_register(op1);
420 if (REG_NONE == reg_dst) {
421 if (parse_scratchpad_register(op1, &reg_dst)) {
422 /* unresolved expression, reserve space */
423 emit_opcode1(0);
424 return;
425 }
426 }
427 reg_src = parse_special_register(op2);
428 if (REG_NONE == reg_src) {
429 if (parse_scratchpad_register(op2, &reg_src)) {
430 /* unresolved expression, reserve space */
431 emit_opcode1(0);
432 return;
433 }
434 }
435
436 /* restore operand string */
437 str[cindex] = ',';
438 if ( (0 != cindex) && (0 == str[cindex-1])) {
439 str[cindex-1] = ' ';
440 }
441
442 /* generate opcode */
443 opcode = -1;
444 switch (reg_dst) {
445 case REG_A: /* lr a,xxx */
446 switch (reg_src) {
447 case REG_IS: opcode = 0x0a; break;
448 case REG_KL: opcode = 0x01; break;
449 case REG_KU: opcode = 0x00; break;
450 case REG_QL: opcode = 0x03; break;
451 case REG_QU: opcode = 0x02; break;
452 default:
453 if (reg_src < 15) {
454 opcode = 0x40 | reg_src;
455 }
456 break;
457 }
458 break;
459 case REG_DC0:
460 switch (reg_src) {
461 case REG_H: opcode = 0x10; break;
462 case REG_Q: opcode = 0x0f; break;
463 }
464 break;
465 case REG_H:
466 if (REG_DC0 == reg_src) opcode = 0x11;
467 break;
468 case REG_IS:
469 if (REG_A == reg_src) opcode = 0x0b;
470 break;
471 case REG_K:
472 if (REG_PC1 == reg_src) opcode = 0x08;
473 break;
474 case REG_KL:
475 if (REG_A == reg_src) opcode = 0x05;
476 break;
477 case REG_KU:
478 if (REG_A == reg_src) opcode = 0x04;
479 break;
480 case REG_PC0:
481 if (REG_Q == reg_src) opcode = 0x0d;
482 break;
483 case REG_PC1:
484 if (REG_K == reg_src) opcode = 0x09;
485 break;
486 case REG_Q:
487 if (REG_DC0 == reg_src) opcode = 0x0e;
488 break;
489 case REG_QL:
490 if (REG_A == reg_src) opcode = 0x07;
491 break;
492 case REG_QU:
493 if (REG_A == reg_src) opcode = 0x06;
494 break;
495 case REG_W:
496 if (0x09 == reg_src) opcode = 0x1d;
497 break;
498 default: /* lr sreg,xxx*/
499 if ( (15 > reg_dst) && (REG_A == reg_src) ) {
500 /* lr sreg,a */
501 opcode = 0x50 | reg_dst;
502 }
503 else if ( (9 == reg_dst) && (REG_W == reg_src) ) {
504 /* special case : lr j,w */
505 opcode = 0x1e;
506 }
507 break;
508 }
509 if (opcode < 0) {
510 f8err(ERROR_ILLEGAL_OPERAND_COMBINATION, mne->name, str, true);
511 } else {
512 emit_opcode1(opcode);
513 }
514 }
515
516 extern int pass;
517
518 /*
519 * generates branch opcodes
520 *
521 * opcode : opcode of the branch (for instance 0x8f for BR7)
522 * str : operand string
523 */
524 static void generate_branch(unsigned char opcode, char *str) {
525
526 unsigned long target_adr;
527 long disp;
528
529 programlabel();
530
531 /* get target address */
532 if (parse_value(str, &target_adr)) {
533 /* unresolved target address, reserve space */
534 emit_opcode2(0, 0);
535 return;
536 }
537
538 /* calculate displacement */
539 if (isPCKnown()) {
540 disp = target_adr - getPC() - 1;
541
542 if (disp > 127 || disp < -128)
543 {
544 char buf[64];
545 sprintf(buf, "%d", (int)disp);
546 asmerr(ERROR_BRANCH_OUT_OF_RANGE, false, buf);
547 }
548 } else {
549 /* unknown pc, will be (hopefully) resolved in future passes */
550 disp = 0;
551 }
552
553 emit_opcode2(opcode, disp & 255);
554 }
555
556
557 /*
558 * handles the following branch mnemonics:
559 * bc, bm, bnc, bno, bnz, bp, br, br7, bz
560 */
561 static void v_branch(char *str, MNEMONIC *mne) {
562 generate_branch(mne->opcode[0], str);
563 }
564
565
566 static void v_bf_bt(char *str, MNEMONIC *mne) {
567
568 int ncommas;
569 int cindex;
570 int i;
571 char *op1;
572 char *op2;
573 unsigned long value;
574
575 /* a valid operand string must contain exactly one comma. find it. */
576 ncommas = 0;
577 cindex = 0;
578 for (i=0; str[i]; i++) {
579 if (',' == str[i]) {
580 ncommas++;
581 cindex = i;
582 }
583 }
584 if (1 != ncommas) {
585 f8err(ERROR_SYNTAX_ERROR, mne->name, str, false);
586 return;
587 }
588
589 /* extract operands */
590 str[cindex] = 0;
591 op1 = str;
592 op2 = &str[cindex+1];
593
594 /* parse first operand*/
595 if (parse_value(op1, &value)) {
596 /* unresolved expression, reserve space */
597 emit_opcode2(0, 0);
598 return;
599 }
600
601 /* check first operand */
602 str[cindex] = ','; /* restore operand string */
603 if ('f' == mne->name[1]) {
604 /* bf */
605 if (value > 15) {
606 f8err(ERROR_VALUE_MUST_BE_LT_10, mne->name, str, false);
607 value &= 15;
608 }
609 } else {
610 /* bt */
611 if (value > 7) {
612 f8err(ERROR_VALUE_MUST_BE_LT_8, mne->name, str, false);
613 value &= 7;
614 }
615 }
616
617 generate_branch(mne->opcode[0] | value, op2);
618 }
619
620
621 /*
622 * handles instructions that take a word operand:
623 * dci, jmp, pi
624 */
625 static void v_wordop(char *str, MNEMONIC *mne) {
626
627 unsigned long value;
628
629 programlabel();
630 parse_value(str, &value);
631 if (value > 0xffff) {
632 f8err(ERROR_VALUE_MUST_BE_LT_10000, mne->name, str, false);
633 }
634 emit_opcode3(mne->opcode[0], (value >> 8) & 0xff, value & 0xff);
635 }
636
637
638 /*
639 * handles instructions that take a byte operand:
640 * ai, ci, in, li, ni, oi, out, xi
641 */
642 static void v_byteop(char *str, MNEMONIC *mne) {
643
644 unsigned long value;
645
646 programlabel();
647 parse_value(str, &value);
648 if (value > 0xff) {
649 f8err(ERROR_ADDRESS_MUST_BE_LT_100, mne->name, str, false);
650 }
651 emit_opcode2(mne->opcode[0], value & 0xff);
652 }
653
654
655 MNEMONIC MneF8[] = {
656
657 /* ds is an f8 opcode, so we replace the ds directive by res */
658 {NULL, v_ds, "res", 0, 0, {0,}},
659
660 /* add db/dw/dd directives for f8tool compatibility */
661 {NULL, v_dc, "db", 0, 0, {0,}},
662 {NULL, v_dc, "dw", 0, 0, {0,}},
663 {NULL, v_dc, "dd", 0, 0, {0,}},
664
665 /*
666 * f8 opcodes
667 *
668 * some instructions have AF_IMP in der addressflag, although
669 * they are handled by own handlers and have explicit operands.
670 * this is to keep dasm from clearing the opcode array when
671 * adding the hashtable.
672 *
673 * the only instructions that are handled by v_mnemonic are
674 * those with implicit operands.
675 *
676 * other f8 instructions have register operands, which v_mnemonic
677 * can't handle.
678 *
679 * or they have byte and word operands (values or addresses).
680 * these could theoretically be handled by v_mnemonic, but
681 * we do it ourselves anyway, since this allows us to have
682 * expressions with parentheses as operands.
683 */
684 {NULL, v_mnemonic, "adc", 0, AF_IMP, {0x8e}},
685 {NULL, v_byteop, "ai" , 0, AF_IMP, {0x24}},
686 {NULL, v_mnemonic, "am" , 0, AF_IMP, {0x88}},
687 {NULL, v_mnemonic, "amd", 0, AF_IMP, {0x89}},
688 {NULL, v_sreg_op, "as" , 0, AF_IMP, {0xc0}}, /* base opcode */
689 {NULL, v_sreg_op, "asd", 0, AF_IMP, {0xd0}}, /* base opcode */
690 {NULL, v_branch, "bc" , 0, AF_IMP, {0x82}},
691 {NULL, v_bf_bt, "bf" , 0, AF_IMP, {0x90}}, /* base opcode */
692 {NULL, v_branch, "bm" , 0, AF_IMP, {0x91}},
693 {NULL, v_branch, "bnc", 0, AF_IMP, {0x92}},
694 {NULL, v_branch, "bno", 0, AF_IMP, {0x98}},
695 {NULL, v_branch, "bnz", 0, AF_IMP, {0x94}},
696 {NULL, v_branch, "bp" , 0, AF_IMP, {0x81}},
697 {NULL, v_branch, "br" , 0, AF_IMP, {0x90}},
698 {NULL, v_branch, "br7", 0, AF_IMP, {0x8f}},
699 {NULL, v_bf_bt, "bt" , 0, AF_IMP, {0x80}}, /* base opcode */
700 {NULL, v_branch, "bz" , 0, AF_IMP, {0x84}},
701 {NULL, v_byteop, "ci" , 0, AF_IMP, {0x25}},
702 {NULL, v_mnemonic, "clr", 0, AF_IMP, {0x70}},
703 {NULL, v_mnemonic, "cm" , 0, AF_IMP, {0x8d}},
704 {NULL, v_mnemonic, "com", 0, AF_IMP, {0x18}},
705 {NULL, v_wordop, "dci", 0, AF_IMP, {0x2a}},
706 {NULL, v_mnemonic, "di" , 0, AF_IMP, {0x1a}},
707 {NULL, v_sreg_op, "ds" , 0, AF_IMP, {0x30}}, /* base opcode */
708 {NULL, v_mnemonic, "ei" , 0, AF_IMP, {0x1b}},
709 {NULL, v_byteop, "in" , 0, AF_IMP, {0x26}},
710 {NULL, v_mnemonic, "inc", 0, AF_IMP, {0x1f}},
711 {NULL, v_ins_outs, "ins", 0, AF_IMP, {0xa0}}, /* base opcode */
712 {NULL, v_wordop, "jmp", 0, AF_IMP, {0x29}},
713 {NULL, v_byteop, "li" , 0, AF_IMP, {0x20}},
714 {NULL, v_lis, "lis", 0, 0, {0,}},
715 {NULL, v_lisu_lisl,"lisl",0, AF_IMP, {0x68}}, /* base opcode */
716 {NULL, v_lisu_lisl,"lisu",0, AF_IMP, {0x60}}, /* base opcode */
717 {NULL, v_mnemonic, "lm" , 0, AF_IMP, {0x16}},
718 {NULL, v_mnemonic, "lnk", 0, AF_IMP, {0x19}},
719 {NULL, v_lr, "lr" , 0, 0, {0,}},
720 {NULL, v_byteop, "ni" , 0, AF_IMP, {0x21}},
721 {NULL, v_mnemonic, "nm" , 0, AF_IMP, {0x8a}},
722 {NULL, v_mnemonic, "nop", 0, AF_IMP, {0x2b}},
723 {NULL, v_sreg_op, "ns" , 0, AF_IMP, {0xf0}}, /* base opcode */
724 {NULL, v_byteop, "oi" , 0, AF_IMP, {0x22}},
725 {NULL, v_mnemonic, "om" , 0, AF_IMP, {0x8b}},
726 {NULL, v_byteop, "out", 0, AF_IMP, {0x27}},
727 {NULL, v_ins_outs, "outs",0, AF_IMP, {0xb0}}, /* base opcode */
728 {NULL, v_wordop, "pi" , 0, AF_IMP, {0x28}},
729 {NULL, v_mnemonic, "pk" , 0, AF_IMP, {0x0c}},
730 {NULL, v_mnemonic, "pop", 0, AF_IMP, {0x1c}},
731 {NULL, v_sl_sr, "sl" , 0, AF_IMP, {0x13}}, /* base opcode for "sl 1" */
732 {NULL, v_sl_sr, "sr" , 0, AF_IMP, {0x12}}, /* base opcode for "sr 1" */
733 {NULL, v_mnemonic, "st" , 0, AF_IMP, {0x17}},
734 {NULL, v_mnemonic, "xdc", 0, AF_IMP, {0x2c}},
735 {NULL, v_byteop, "xi" , 0, AF_IMP, {0x23}},
736 {NULL, v_mnemonic, "xm" , 0, AF_IMP, {0x8c}},
737 {NULL, v_sreg_op, "xs" , 0, AF_IMP, {0xe0}}, /* base opcode */
738 MNEMONIC_NULL
739 };
740