1 /* code generator for the Hitachi H8/300 architecture simulator.
2 Copyright (C) 1993 Free Software Foundation, Inc.
3 Hacked by Steve Chamberlain of Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This program reads the H8/300 opcode table and writes out
22 a large switch statement to understand the opcodes (with ifs if
23 there is more than one opcode per case) and code to do the stuff */
30 #include"opcode/h8300.h"
35 static struct h8_opcode *h8_opcodes_sorted[PTWO][MAXSAME];
40 /* How to get at nibble n from the instruction */
53 /* how to get at the 3 bit immediate in the instruction */
63 /* How to get at a byte register from an index in the instruction at
71 /* How to get at a word register from an index in the instruction at
80 #define sorted_key noperands
82 /* sort the opcode table into h8_opcodes_sorted[0..255] */
89 for (p = h8_opcodes; p->name; p++)
95 for (j = 0; p->data.nib[j] != E; j++)
97 if ((int) p->data.nib[j] == ABS16ORREL8SRC)
98 p->data.nib[j] = ABS16SRC;
99 if ((int) p->data.nib[j] == ABS16OR8SRC)
100 p->data.nib[j] = ABS16SRC;
101 if ((int) p->data.nib[j] == ABS16OR8DST)
102 p->data.nib[j] = ABS16DST;
104 if ((int) p->data.nib[0] < 16)
106 n1 = (int) p->data.nib[0];
110 if ((int) p->data.nib[1] < 16)
112 n2 = (int) p->data.nib[1];
116 for (i = 0; i < MAXSAME; i++)
118 int j = /* ((n3 >> 3) * 512) + ((n4 >> 3) * 256) + */ n1 * 16 + n2;
120 if (h8_opcodes_sorted[j][i] == (struct h8_opcode *) NULL)
122 h8_opcodes_sorted[j][i] = p;
131 /* Just make sure there are an even number of nibbles in it, and
132 that the count is the same s the length */
133 for (i = 0; p->data.nib[i] != E; i++)
139 for (i = 0; i < PTWO; i++)
141 if (h8_opcodes_sorted[i][0])
142 p = h8_opcodes_sorted[i][0];
144 h8_opcodes_sorted[i][0] = p;
148 /* either fetch srca&srcb or store dst */
151 decode (p, fetch, size)
157 char *ss = size == 8 ? "BYTE" : "WORD";
159 for (i = 0; p->data.nib[i] != E; i++)
161 switch (p->data.nib[i])
165 printf ("srca = %s;\n", breg[i]);
171 printf ("srca = %s;\n", wreg[i]);
175 printf ("srcb = %s;\n", breg[i]);
177 printf ("%s = dst;\n", breg[i]);
183 printf ("srcb = %s;\n", wreg[i]);
185 printf ("%s =dst;\n", wreg[i]);
189 printf ("srca = b1;\n");
197 printf ("srca = %s_MEM(%s);\n", ss, wreg[i]);
198 printf ("%s+=%d;\n", wreg[i], size / 8);
206 printf ("lval = %s;\n", wreg[i]);
207 printf ("srca = %s_MEM(lval);\n", ss);
216 printf ("lval = %s;\n", wreg[i]);
217 printf ("srcb = %s_MEM(lval);\n", ss);
221 printf ("SET_%s_MEM(lval,dst);\n", ss);
228 printf ("lval = pc[1];\n");
236 printf ("%s -=%d;\n", wreg[i], size / 8);
237 printf ("SET_%s_MEM(%s, dst);\n", ss, wreg[i]);
245 printf ("srca = %s;\n", imm3[i]);
249 printf ("srca =( pc[1]);\n");
255 printf ("lval = (0xff00) + b1;\n");
256 printf ("srca = BYTE_MEM(lval);\n");
263 printf ("lval = (0xff00) + b1;\n");
264 printf ("srcb = BYTE_MEM(lval);\n");
268 printf ("SET_BYTE_MEM(lval,dst);\n");
273 printf ("srca = ((b1&0x80)?2:1);\n");
279 printf ("lval = pc[1];\n");
280 printf ("srca = %s_MEM(lval);\n", size == 8 ? "BYTE" : "WORD");
286 printf ("rn = %s & 0x7;\n", nibs[i]);
291 printf ("lval = 0xffff&(pc[1] +reg[rn]);\n");
292 printf ("srca = %s_MEM(lval);\n", size == 8 ? "BYTE" : "WORD");
298 printf ("lval = 0xffff&(pc[1] +reg[rn]);\n");
299 printf ("srcb = %s_MEM(lval);\n", size == 8 ? "BYTE" : "WORD");
303 printf ("SET_%s_MEM(lval,dst);\n", ss);
309 printf ("lval = (pc[1]);\n");
310 printf ("srcb = %s_MEM(lval);\n", ss);
314 printf ("SET_%s_MEM(lval,dst);\n", ss);
320 printf (" /* DISP8 handled in opcode */\n");
323 if (p->data.nib[i] > HexF)
325 printf ("saved_state.exception = SIGILL;\n");
334 printf ("saved_state.exception = SIGSTOP;\n");
343 printf ("dst = srca;\n");
350 printf ("SET_CCR(GET_CCR() & srca);\n");
357 printf ("dst = srca + srcb+ (c != 0);\n");
364 printf ("dst = srcb - srca - (c != 0);\n");
389 printf ("if (%s) npc += ((char )b1)>>1;\n", a);
397 printf ("reg[7]-=2;\n");
398 printf ("tmp = reg[7];\n");
399 printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n");
400 printf ("npc += ((char)b1)>>1;\n");
410 printf ("srca = -srca;\n");
411 printf ("dst = srca + srcb;\n");
421 printf ("if (b1 == 0xc4) {\n");
422 printf ("printf(\"%%c\", reg[2]);\n");
425 printf ("reg[7]-=2;\n");
426 printf ("tmp = reg[7];\n");
427 printf ("SET_WORD_MEM(tmp, (npc-saved_state.mem)*2);\n");
428 printf ("npc = (lval>>1) + saved_state.mem;\n");
438 printf ("npc = (lval>>1) + saved_state.mem;\n");
447 printf ("tmp = reg[7];\n");
448 printf ("reg[7]+=2;\n");
449 printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");
458 printf ("reg[7]+=2;\n");
459 printf ("tmp = reg[7];\n");
460 printf ("reg[7]+=2;\n");
461 printf ("SET_CCR(tmp);\n");
462 printf ("npc = saved_state.mem + (WORD_MEM(tmp)>>1);\n");
471 printf ("tmp = GET_CCR();\n");
472 printf ("tmp %s= srca;\n", a);
481 printf ("saved_state.exception = SIGTRAP;\n");
482 printf ("npc = pc;\n");
491 printf ("dst = srcb %s srca;\n", a);
500 printf ("dst = %s srcb ;\n", a);
511 printf ("c = srcb & 0x80;\n");
512 printf ("dst = srcb << 1;\n");
513 printf ("if (c) dst|=1;\n");
519 printf ("c = srcb & 1;\n");
520 printf ("dst = srcb >> 1;\n");
521 printf ("if (c) dst|=0x80;\n");
527 printf ("tmp = srcb & 0x80;\n");
528 printf ("dst = srcb << 1;\n");
529 printf ("if (c) dst|=1;\n");
530 printf ("c = tmp;\n");
536 printf ("tmp = srcb & 1;\n");
537 printf ("dst = srcb >> 1;\n");
538 printf ("if (c) dst|=0x80;\n");
539 printf ("c = tmp;\n");
545 printf ("c = srcb&0x80;\n");
546 printf ("dst = srcb << 1;\n");
553 printf ("c = srcb&0x1;\n");
554 printf ("if (srcb&0x80) dst = (srcb>>1) | 0x80;\n");
555 printf ("else dst = (srcb>>1) &~ 0x80;\n");
562 printf ("c = srcb&0x80;\n");
563 printf ("dst = srcb << 1;\n");
570 printf ("c = srcb&0x1;\n");
571 printf ("dst = (srcb>>1) &~ 0x80;\n");
578 printf ("srca = %s;\n", breg[2]);
579 printf ("srcb = %s;\n", wreg[3]);
580 printf ("n = srca & 0x80;\n");
581 printf ("z = !srca;\n");
582 printf ("if (srca) dst = srcb / srca;tmp = srcb %% srca;\n");
583 printf ("%s = (dst & 0xff) | (tmp << 8);\n", wreg[3]);
590 printf ("srca = %s;\n", breg[2]);
591 printf ("srcb = %s;\n", wreg[3]);
593 printf ("dst = (srcb&0xff) * srca;\n");
594 printf ("%s = dst;\n", wreg[3]);
601 printf ("dst = %s;\n", breg[3]);
602 printf ("v = (dst==0x7f);\n");
604 printf ("%s= dst;\n", breg[3]);
621 printf ("dst = %s;\n", breg[3]);
622 printf ("v = (dst==0x80);\n");
624 printf ("%s = dst;\n", breg[3]);
627 char saf[] = "goto setflags;";
628 char sf[] = "goto shiftflags;";
629 char af8[] = "goto aluflags8;";
630 char af16[] = "goto aluflags16;";
631 char lf[] = "goto logflags;";
632 char icf[] = "goto incflags;";
633 char mf8[] = "goto movflags8;";
634 char mf16[] = "goto movflags16;";
635 char nx[] = "goto next;";
651 nx, 1, "bld", bit, "dst = srcb; c = (srcb>>srca)&1;", 8
655 nx, 1, "bild", bit, "dst = srcb; c = !((srcb>>srca)&1);", 8
659 nx, 1, "band", bit, "dst = srcb; c = C &&((srcb>>srca)&1);", 8
663 nx, 1, "biand", bit, "dst = srcb; c = C &&(!((srcb>>srca)&1));", 8
667 nx, 1, "bior", bit, "dst = srcb; c = C ||(!((srcb>>srca)&1));", 8
671 nx, 1, "bor", bit, "dst = srcb; c = C ||(((srcb>>srca)&1));", 8
675 nx, 1, "bixor", bit, "dst = srcb; c = C ^(!((srcb>>srca)&1));", 8
679 nx, 1, "bxor", bit, "dst = srcb; c = C ^(((srcb>>srca)&1));", 8
683 nx, 1, "bnot", bit, "dst = srcb ^ (1<<srca);", 8
687 nx, 1, "bclr", bit, "dst = srcb & ~(1<<srca);", 8
691 nx, 1, "bset", bit, "dst = srcb | (1<<srca);", 8
695 nx, 1, "bst", bit, "dst = (srcb & ~(1<<srca))| ((C)<<srca);", 8
699 nx, 1, "bist", bit, "dst = (srcb & ~(1<<srca))| ((!C)<<srca);", 8
703 nx, 1, "btst", bit, "dst = srcb; z = !((srcb>>srca)&1);", 8
707 icf, 0, "dec", dec, 0, 0
711 icf, 0, "inc", inc, 0, 0
715 saf, 1, "orc", setf, "|", 0
719 saf, 1, "xorc", setf, "^", 0
723 saf, 1, "andc", setf, "&", 0
727 nx, 1, "nop", nop, 0, 0
731 nx, 1, "bra", bra, "1", 0
735 nx, 1, "brn", bra, "0", 0
739 nx, 1, "bhi", bra, "(C||Z)==0", 0
743 nx, 1, "bls", bra, "(C||Z)==1", 0
747 nx, 1, "bcs", bra, "C==1", 0
751 nx, 1, "bcc", bra, "C==0", 0
755 nx, 1, "bpl", bra, "N==0", 0
759 nx, 1, "bmi", bra, "N==1", 0
763 nx, 1, "bvs", bra, "V==1", 0
767 nx, 1, "bvc", bra, "V==0", 0
771 nx, 1, "bge", bra, "(N^V)==0", 0
775 nx, 1, "bgt", bra, "(Z|(N^V))==0", 0
779 nx, 1, "blt", bra, "(N^V)==1", 0
783 nx, 1, "ble", bra, "(Z|(N^V))==1", 0
787 nx, 1, "beq", bra, "Z==1", 0
791 nx, 1, "bne", bra, "Z==0", 0
795 nx, 1, "bsr", bsr, "", 0
799 nx, 1, "jsr", jsr, 0, 0
803 nx, 1, "jmp", jmp, 0, 0
807 nx, 0, "rts", rts, 0, 0
811 nx, 0, "rte", rte, 0, 0
815 nx, 1, "andc", andc, 0, 0
819 sf, 1, "shal", shal, 0, 0
823 sf, 1, "shar", shar, 0, 0
827 sf, 1, "shll", shll, 0, 0
831 sf, 1, "shlr", shlr, 0, 0
835 sf, 1, "rotxl", rotxl, 0, 0
839 sf, 1, "rotxr", rotxr, 0, 0
843 sf, 1, "rotl", rotl, 0, 0
847 sf, 1, "rotr", rotr, 0, 0
851 lf, 1, "xor", log, "^", 0
855 lf, 1, "and", log, "&", 0
859 lf, 1, "or", log, "|", 0
863 lf, 1, "not", ulog, " ~", 0
867 lf, 1, "neg", ulog, " - ", 0
871 nx, 1, "adds", adds, "dst = srca + srcb", 0
875 nx, 1, "subs", adds, "srca = -srca; dst = srcb + srca", 0
879 af8, 1, "add.b", add, "dst = srca + srcb", 8
883 af16, 1, "add.w", add, "dst = srca + srcb", 16
887 af16, 1, "sub.w", add, "srca = -srca; dst = srcb + srca", 16
891 af8, 1, "sub.b", add, "srca = -srca; dst = srcb + srca", 8
895 af8, 1, "addx", addx, 0, 8
899 af8, 1, "subx", subx, 0, 8
903 af8, 0, "cmp.b", cmp, 0, 8
907 af16, 0, "cmp.w", cmp, 0, 16
911 nx, 1, "sleep", esleep, 0, 0
915 nx, 0, "bpt", bpt, 0, 8
919 nx, 0, "divxu", divxu, 0, 0
923 nx, 0, "mulxu", mulxu, 0, 0
927 mf8, 1, "mov.b", mov, 0, 8
931 mf8, 1, "movtpe", mov, 0, 8
935 mf8, 1, "movfpe", mov, 0, 8
939 mf16, 1, "mov.w", mov, 0, 16
954 printf ("%s %s %s\n", cs, p->name, ce);
956 for (i = 0; table[i].name; i++)
958 if (strcmp (table[i].name, p->name) == 0)
962 decode (p, 1, table[i].size);
963 printf ("cycles += %d;\n", p->time);
964 printf ("npc = pc + %d;\n", p->length / 2);
965 table[i].func (p, table[i].arg, table[i].size);
967 decode (p, 0, table[i].size);
969 printf (table[i].ftype);
971 printf ("goto next;\n");
976 printf ("%s not found %s\n", cs, ce);
977 printf ("saved_state.exception = SIGILL;\n");
986 /* write if statements to select the right opcode */
987 struct h8_opcode **p;
990 p = h8_opcodes_sorted[i];
991 printf ("case 0x%03x:\n", i);
995 /* See if the next few also match */
996 while (h8_opcodes_sorted[i + 1][0] == *p)
999 printf ("case 0x%03x:\n", i);
1002 /* Dont need any if's this is the only one */
1009 /* start two nibbles in since we know we match in the first byte */
1018 for (c = 0; c < 5; c++)
1023 printf ("%s %x%x", cs, (*p)->data.nib[0], (*p)->data.nib[1]);
1024 while ((c = (*p)->data.nib[nib]) != E)
1028 /* bit 3 must be zero */
1029 mask0[byte] |= 0x8 << nibshift;
1035 /* bit 3 must be one */
1036 mask1[byte] |= 0x8 << nibshift;
1042 mask0[byte] |= ((~c) & 0xf) << nibshift;
1043 mask1[byte] |= (c & 0xf) << nibshift;
1067 for (c = 1; c < byte; c++)
1069 if (mask0[c] | mask1[c])
1079 if (c / 2 == 0 && sh == 0)
1080 printf ("((b1&0x%x)==0x%x)", mask0[c] | mask1[c],
1084 printf ("((pc[%d]&(0x%02x<<%d))==(0x%x<<%d))",
1085 c / 2, mask0[c] | mask1[c], sh,
1106 struct h8_opcode *s;
1107 struct h8_opcode *d;
1109 for (d = s = h8_opcodes; s->name; s++)
1113 if (strcmp (s->name, "push") == 0)
1115 if (strcmp (s->name, "bhs") == 0)
1117 if (strcmp (s->name, "blo") == 0)
1119 if (strcmp (s->name, "bt") == 0)
1121 if (strcmp (s->name, "bf") == 0)
1123 if (strcmp (s->name, "pop") == 0)
1141 printf ("%s do the operation %s\n", cs, ce);
1142 printf ("switch (b0) \n{\n");
1143 for (i = 0; i < PTWO; i++)