77625ccb5adc53ea35cd23e6168080698d6f0c56
1 /* BFD library support routines for the Hitachi H8/300 architecture.
2 Copyright (C) 1990-1991 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. */
26 #include"opcode/h8300.h"
31 static struct h8_opcode
*h8_opcodes_sorted
[PTWO
][MAXSAME
];
33 /* Run through the opcodes and sort them into order to make them easy
60 #define sorted_key noperands
79 for (p
= h8_opcodes
; p
->name
; p
++)
84 for (j
= 0; p
->data
.nib
[j
] != E
; j
++)
86 if ((int)p
->data
.nib
[j
] == ABS16ORREL8SRC
)
87 p
->data
.nib
[j
] = ABS16SRC
;
88 if ((int)p
->data
.nib
[j
] == ABS16OR8SRC
)
89 p
->data
.nib
[j
] = ABS16SRC
;
90 if ((int)p
->data
.nib
[j
] == ABS16OR8DST
)
91 p
->data
.nib
[j
] = ABS16DST
;
93 /* Kill push and pop, they're duplicates */
94 if (strcmp(p
->name
,"push")==0) {
97 if (strcmp(p
->name
,"pop")==0) {
102 if ((int) p
->data
.nib
[0] < 16)
104 n1
= (int) p
->data
.nib
[0];
108 if ((int) p
->data
.nib
[1] < 16)
110 n2
= (int) p
->data
.nib
[1];
115 if ((int) p
->data
.nib
[2] < 16)
117 n3
= (int) p
->data
.nib
[2];
119 else if ((int) p
->data
.nib
[2] & B30
)
123 else if ((int) p
->data
.nib
[2] & B31
)
129 if ((int) p
->data
.nib
[3] < 16)
131 n4
= (int) p
->data
.nib
[3];
133 else if ((int) p
->data
.nib
[3] & B30
)
137 else if ((int) p
->data
.nib
[3] & B31
)
142 for (i
= 0; i
< MAXSAME
; i
++)
144 int j
= /* ((n3 >> 3) * 512) + ((n4 >> 3) * 256) + */ n1
* 16 + n2
;
146 if (h8_opcodes_sorted
[j
][i
] == (struct h8_opcode
*) NULL
)
148 h8_opcodes_sorted
[j
][i
] = p
;
157 /* Just make sure there are an even number of nibbles in it, and
158 that the count is the same s the length */
159 for (i
= 0; p
->data
.nib
[i
] != E
; i
++)
165 for (i
= 0; i
< PTWO
; i
++)
167 if (h8_opcodes_sorted
[i
][0])
168 p
= h8_opcodes_sorted
[i
][0];
170 h8_opcodes_sorted
[i
][0] = p
;
175 DEFUN (bfd_h8_disassemble
, (addr
, data
, stream
),
177 CONST bfd_byte
* data AND
180 /* Find the first entry in the table for this opcode */
181 CONST
static char *regnames
[] =
183 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
184 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"};
190 struct h8_opcode
**p
= h8_opcodes_sorted
[(unsigned) (data
[0])];
193 /* Find the exact opcode/arg combo */
197 unsigned int len
= 0;
203 op_type looking_for
= *nib
;
204 int thisnib
= data
[len
>> 1];
206 thisnib
= (len
& 1) ? (thisnib
& 0xf) : ((thisnib
>> 4) & 0xf);
207 if ((int) looking_for
& (int) B31
)
209 if (((int) thisnib
& 0x8) == 0)
211 looking_for
= (op_type
) ((int) looking_for
& ~(int) B31
);
213 if ((int) looking_for
& (int) B30
)
215 if (((int) thisnib
& 0x8) != 0)
217 looking_for
= (op_type
) ((int) looking_for
& ~(int) B30
);
237 if ((int) looking_for
!= thisnib
)
248 abs
= (data
[len
>> 1]) * 256 + data
[(len
+ 2) >> 1];
256 abs
= thisnib
== 0x8 ? 2 : 1;
263 abs
= data
[len
>> 1];
283 fprintf (stream
, "Dont understand \n");
294 fprintf (stream
, "%02x %02x .word\tH'%x,H'%x",
302 for (i
= 0; i
< q
->length
; i
++)
304 fprintf (stream
, "%02x", data
[i
]);
308 fprintf (stream
, " ");
311 fprintf (stream
, "%s\t", q
->name
);
312 /* Now print out the args */
314 op_type
*args
= q
->args
.nib
;
320 fprintf (stream
, ",");
321 switch ((int) (*args
) & ~((int) B30
| (int) B31
))
326 fprintf (stream
, "#0x%x", (unsigned) abs
);
329 fprintf (stream
, "%s", regnames
[rd
]);
332 fprintf (stream
, "%s", regnames
[rs
]);
335 fprintf (stream
, "r%d", rd
& 0x7);
338 fprintf (stream
, "r%d", rs
& 0x7);
341 fprintf (stream
, "@r%d+", rs
& 0x7);
344 fprintf (stream
, "@-r%d", rd
& 0x7);
347 fprintf (stream
, "@r%d", rd
& 0x7);
350 fprintf (stream
, "@r%d", rs
& 0x7);
359 fprintf (stream
, "@0x%x", (unsigned) abs
);
362 fprintf (stream
, "@@%d (%x)", abs
, abs
);
365 fprintf (stream
, ".%s%d (%x)", (char) abs
> 0 ? "+" : "", (char) abs
,
366 addr
+ (char) abs
+ 2);
370 fprintf (stream
, "@(0x%x,r%d)", abs
, rdisp
& 0x7);
373 fprintf (stream
, "ccr");
376 fprintf (stream
, "#%d", abs
);
389 decode (p
, fetch
, size
)
395 char *ss
= size
== 8 ? "BYTE" : "WORD";
397 for (i
= 0; p
->data
.nib
[i
] != E
; i
++)
399 switch (p
->data
.nib
[i
])
403 printf ("srca = BYTE_REG(%s);\n", nibs
[i
]);
409 printf ("srca = WORD_REG(%s);\n", nibs
[i
]);
413 printf ("srcb = BYTE_REG(%s);\n", nibs
[i
]);
415 printf ("SET_BYTE_REG(%s,dst);\n", nibs
[i
]);
421 printf ("srcb = WORD_REG(%s);\n", nibs
[i
]);
423 printf ("SET_WORD_REG(%s,dst);\n", nibs
[i
]);
427 printf ("srca = b1;\n", i
);
435 printf("rn = %s &0x7;\n", nibs
[i
]);
436 printf ("srca = %s_MEM(reg[rn]);\n", size
== 8 ? "BYTE"
438 printf("reg[rn]+=%d;\n", size
/8);
445 printf("lval = WORD_REG(%s);\n", nibs
[i
]);
446 printf("srca = %s_MEM(lval);\n", ss
);
454 printf("rn = %s & 0x7;\n", nibs
[i
] );
455 printf("lval = reg[rn];\n");
456 printf("srcb = %s_MEM(lval);\n", size
==8 ? "BYTE" : "WORD");
460 printf("SET_%s_MEM(lval,dst);\n",ss
);
467 printf("lval = WORD_MEM(b1);\n");
475 printf("rn = %s & 0x7;\n", nibs
[i
]);
476 printf("reg[rn]-=%d;\n", size
/8);
477 printf ("SET_%s_MEM(reg[rn], dst);\n",
478 size
== 8 ? "BYTE" : "WORD");
484 printf ("srca =WORD_IMM;\n", i
);
489 printf("lval = (0xff00) + b1;\n");
490 printf ("srca = BYTE_MEM(lval);\n");
497 printf("lval = (0xff00) + b1;\n");
498 printf ("srcb = BYTE_MEM(lval);\n");
502 printf ("SET_BYTE_MEM(lval,dst);\n");
507 printf ("srca = KBIT(%d);\n", i
);
513 printf("lval = ((pc[2] << 8) + pc[3]);\n");
514 printf("srca = %s_MEM(lval);\n", size
==8 ? "BYTE" : "WORD");
520 printf("rn = %s & 0x7;\n", nibs
[i
]);
525 printf("lval = 0xffff&((pc[2] << 8) + pc[3] +reg[rn]);\n");
526 printf("srca = %s_MEM(lval);\n", size
== 8 ? "BYTE" : "WORD");
532 printf("lval = 0xffff&((pc[2] << 8) + pc[3] +reg[rn]);\n");
533 printf("srcb = %s_MEM(lval);\n", size
== 8 ? "BYTE" : "WORD");
536 printf("SET_%s_MEM(lval,dst);\n",ss
);
542 printf("lval = ((pc[2] << 8) + pc[3]);\n");
543 printf("srcb = WORD_MEM(lval);\n");
547 printf ("SET_WORD_MEM(lval,dst);\n", i
);
553 printf(" /* DISP8 handled in opcode */\n");
556 if (p
->data
.nib
[i
] > HexF
)
558 printf("exception = SIGILL;\n");
566 typedef int keytype
[NNIBS
];
571 /* struct h8_opcode *match[200];*/
583 for (j
= 0; (p
->data
.nib
[j
] != E
); j
++)
585 if (p
->data
.nib
[j
] == RS8
|| p
->data
.nib
[j
] == RD8
)
589 if (p
->data
.nib
[j
] > HexF
)
590 key
[j
] = p
->data
.nib
[j
];
594 for (; j
< NNIBS
; j
++)
603 /* See if this key matches one already found */
604 for (n
= 0; n
< nkeys
; n
++)
608 for (i
= 0; i
< NNIBS
; i
++)
610 if ((keys
[n
].k
[i
]&0x3f) != (key
[i
]&0x3f))
619 if (x
== 0x7c) return 0;
620 if (x
== 0x7d) return 0;
621 if (x
== 0x7e) return 0;
622 if (x
== 0x7f) return 0;
628 /* look through all the addressing modes, work out which ones are unique*/
629 struct h8_opcode
**p
;
635 memset (keys
, 0, sizeof (keys
));
638 for (i
= 0; i
< PTWO
; i
++)
640 p
= h8_opcodes_sorted
[i
];
646 if (BADNEWS((*p
)->sorted_key
)) {
652 memcpy (keys
[n
].k
, key
, sizeof (keys
[n
].k
));
657 /*e*/ keys
[n
].match
[keys
[n
].nmatch
++] = *p
;
666 for (n
= 0; n
< nkeys
; n
++)
668 printf ("%d %d %d %d %d %d %d %d\n", keys
[n
][0], keys
[n
][1], keys
[n
][2], keys
[n
][3],
669 keys
[n
][4], keys
[n
][5], keys
[n
][6], keys
[n
][7]);
678 printf ("%s Nop does nothing %s", cs
, ce
);
684 printf ("exception = SIGSTOP;\n");
698 printf("dst = srca;\n");
703 /* printf ("n = dst & 0x%x;\n", s == 8 ? 0x80 : 0x8000);*/
708 /* printf ("z = ~(dst & 0x%x);\n", s == 8 ? 0xff : 0xffff);*/
713 /* printf ("v = (( (~(srca ^ srcb) & (srca ^ dst))) & (1<<%d));\n", s);*/
718 printf ("c = dst & 0x%x;\n", 1 << s
);
740 printf ("dst = srca & ~(1<<srcb);\n");
747 printf ("c =(C && ! ((srca >> srcb)&1));\n");
754 printf ("c = ( ! ((srca >> srcb)&1));\n");
762 printf ("SET_CCR(GET_CCR() & srca);\n");
769 printf ("dst = srca + srcb+ (c != 0);\n");
776 printf ("dst = srcb - srca - (c != 0);\n");
803 printf ("if (%s) npc += (((char *)pc)[1]);\n", a
);
812 printf("reg[7]-=2;\n");
813 printf("tmp = reg[7];\n");
814 printf ("SET_WORD_MEM(tmp, npc-mem);\n");
815 printf ("npc += (((char *)pc)[1]);\n");
825 printf("srca = -srca;\n");
826 printf ("dst = srca + srcb;\n");
835 printf("if (b1 == 0xc4) {\n");
836 printf("printf(\"%%c\", reg[2]);\n");
839 printf("reg[7]-=2;\n");
840 printf("tmp = reg[7];\n");
841 printf ("SET_WORD_MEM(tmp, npc-mem);\n");
842 printf ("npc = lval + mem;\n");
852 printf ("npc = lval + mem;\n");
861 printf ("tmp = reg[7];\n");
862 printf ("reg[7]+=2;\n");
863 printf ("npc = mem + WORD_MEM(tmp);\n");
872 printf ("tmp = GET_CCR();\n");
873 printf ("tmp %s= srca;\n",a
);
882 printf ("exception = SIGTRAP;\n");
883 printf( "npc = pc;\n");
892 printf ("dst = srcb %s srca;\n", a
);
901 printf ("dst = %s srcb ;\n", a
);
912 printf("c = srcb & 0x80;\n");
913 printf("dst = srcb << 1;\n");
914 printf("if (c) dst|=1;\n");
919 printf("c = srcb & 1;\n");
920 printf("dst = srcb >> 1;\n");
921 printf("if (c) dst|=0x80;\n");
927 printf("tmp = srcb & 0x80;\n");
928 printf("dst = srcb << 1;\n");
929 printf("if (c) dst|=1;\n");
930 printf("c = tmp;\n");
935 printf("tmp = srcb & 1;\n");
936 printf("dst = srcb >> 1;\n");
937 printf("if (c) dst|=0x80;\n");
938 printf("c = tmp;\n");
945 printf("c = srcb&0x80;\n");
946 printf("dst = srcb << 1;\n");
952 printf("c = srcb&0x1;\n");
953 printf("if (srcb&0x80) dst = (srcb>>1) | 0x80;\n");
954 printf("else dst = (srcb>>1) &~ 0x80;\n");
960 printf("c = srcb&0x80;\n");
961 printf("dst = srcb << 1;\n");
967 printf("c = srcb&0x1;\n");
968 printf("dst = (srcb>>1) &~ 0x80;\n");
974 printf("srca = BYTE_REG(pc[1]>>4);\n");
975 printf("srcb = WORD_REG(pc[1]&0x7);\n");
976 printf("n = srca & 0x80\;\n");
977 printf("z = !srca;\n");
978 printf("if (srca) dst = srcb / srca;tmp = srcb %% srca;\n");
979 printf("reg[pc[1]&0x7] = (dst & 0xff) | (tmp << 8);\n");
984 printf("srca = BYTE_REG(pc[1]>>4);\n");
985 printf("srcb = WORD_REG(pc[1]&0x7);\n");
986 printf("dst = (srcb&0xff) * srca;\n");
987 printf("reg[pc[1]&0x7] = dst;\n");
989 char saf
[]="goto setflags;";
990 char sf
[] = "goto shiftflags;";
991 char af8
[] = "goto aluflags8;";
992 char af16
[] = "goto aluflags16;";
993 char lf
[] = "goto logflags;";
994 char mf8
[]= "goto movflags8;";
995 char mf16
[]= "goto movflags16;";
996 char nx
[] = "goto next;";
1011 saf
,1,"orc",setf
,"|",0,
1012 saf
,1,"xorc",setf
,"^",0,
1013 saf
,1,"andc",setf
,"&",0,
1014 0,1, "bild", bild
, 0, 0,
1015 0,1, "nop", nop
, 0, 0,
1016 0,1, "bclr", bclr
, 0, 0,
1017 0,1, "biand", biand
, 0, 0,
1018 0,1, "bra", bra
, "1", 0,
1019 0,1, "brn", bra
, "0", 0,
1020 0,1, "bf", bra
, "0", 0,
1021 0,1, "bhi", bra
, "(C||Z)==0", 0,
1022 0,1, "bls", bra
, "(C||Z)==1", 0,
1023 0,1, "bcs", bra
, "C==1", 0,
1024 0,1, "bcc", bra
, "C==0", 0,
1025 0,1, "bpl", bra
, "N==0", 0,
1026 0,1, "bmi", bra
, "N==1", 0,
1027 0,1, "bvs", bra
, "V==1", 0,
1028 0,1, "bvc", bra
, "V==0", 0,
1029 0,1, "bge", bra
, "(N^V)==0", 0,
1030 0,1, "bgt", bra
, "(Z|(N^V))==0", 0,
1031 0,1, "blt", bra
, "(N^V)==1", 0,
1032 0,1, "ble", bra
, "(Z|(N^V))==1", 0,
1033 0,1, "beq", bra
, "Z==1", 0,
1034 0,1, "bne", bra
, "Z==0", 0,
1035 0,1, "bsr", bsr
, "", 0,
1036 0,1, "jsr", jsr
, 0, 0,
1037 0,1, "jmp", jmp
, 0, 0,
1038 0,0, "rts", rts
, 0, 0,
1039 0,1, "andc", andc
, 0, 0,
1040 0,1, "nop", enop
, 0, 0,
1041 sf
,1, "shal",shal
,0,0,
1042 sf
,1, "shar",shar
,0,0,
1043 sf
,1, "shll",shll
,0,0,
1044 sf
,1, "shlr",shlr
,0,0,
1045 sf
,1, "rotxl",rotxl
,0,0,
1046 sf
,1, "rotxr",rotxr
,0,0,
1047 sf
,1, "rotl",rotl
,0,0,
1048 sf
,1, "rotr",rotr
,0,0,
1049 lf
,1, "xor", log
, "^",0,
1050 lf
,1, "and", log
, "&",0,
1051 lf
,1, "or", log
, "|",0,
1052 lf
,1,"not", ulog
," ~",0,
1053 lf
,1,"neg", ulog
," - ",0,
1054 0,1, "adds", adds
, "dst = srca + srcb", 0,
1055 0,1, "subs", adds
, "srca = -srca; dst = srcb + srca", 0,
1056 af8
,1, "add.b", add
, "dst = srca + srcb", 8,
1057 af16
,1, "add.w", add
, "dst = srca + srcb", 16,
1058 af16
,1, "sub.w", add
, "srca = -srca; dst = srcb + srca", 16,
1059 af8
,1, "sub.b", add
, "srca = -srca; dst = srcb + srca", 8,
1060 af8
,1, "addx", addx
, 0, 8,
1061 af8
,1, "subx", subx
, 0, 8,
1062 af8
,0, "cmp.b", cmp
, 0, 8,
1063 af16
,0, "cmp.w", cmp
, 0, 16,
1064 0,1, "sleep", esleep
, 0, 0,
1065 0,0, "bpt", bpt
, 0, 8,
1066 nx
,0, "divxu", divxu
,0,0,
1067 nx
,0, "mulxu", mulxu
,0,0,
1068 mf8
,1, "mov.b", mov
, 0, 8,
1069 mf16
,1, "mov.w", mov
, 0, 16,
1074 struct h8_opcode
*p
;
1078 printf ("%s %s %s\n", cs
, p
->name
, ce
);
1080 for (i
= 0; table
[i
].name
; i
++)
1082 if (strcmp (table
[i
].name
, p
->name
) == 0)
1085 if (table
[i
].decode
) decode(p
, 1,p
->size
);
1086 printf ("cycles += %d;\n", p
->time
);
1087 printf ("npc = pc + %d;\n", p
->length
);
1088 table
[i
].func (p
, table
[i
].arg
, table
[i
].size
);
1089 if (table
[i
].decode
) decode(p
, 0,p
->size
);
1090 if (table
[i
].ftype
) printf(table
[i
].ftype
);
1091 else printf ("goto next;\n");
1096 printf ("%s not found %s\n", cs
, ce
);
1097 printf("exception = SIGILL;\n");
1098 printf ("break;\n");
1104 /* write if statements to select the right opcode */
1105 struct h8_opcode
**p
;
1108 p
= h8_opcodes_sorted
[i
];
1109 printf ("case 0x%03x:\n", i
);
1113 /* See if the next few also match */
1114 while (h8_opcodes_sorted
[i
+ 1][0] == *p
)
1117 printf ("case 0x%03x:\n", i
);
1120 /* Dont need any if's this is the only one */
1127 /* start two nibbles in since we know we match in the first byte */
1136 for (c
= 0; c
< 5; c
++)
1141 printf ("%s %x%x", cs
, (*p
)->data
.nib
[0], (*p
)->data
.nib
[1]);
1142 while ((c
= (*p
)->data
.nib
[nib
]) != E
)
1146 /* bit 3 must be zero */
1147 mask0
[byte
] |= 0x8 << nibshift
;
1153 /* bit 3 must be one */
1154 mask1
[byte
] |= 0x8 << nibshift
;
1160 mask0
[byte
] |= ((~c
) & 0xf) << nibshift
;
1161 mask1
[byte
] |= (c
& 0xf) << nibshift
;
1185 for (c
= 1; c
< byte
; c
++)
1187 if (mask0
[c
] | mask1
[c
])
1191 printf ("((pc[%d]&0x%02x)==0x%x)",
1192 c
, mask0
[c
] | mask1
[c
], mask1
[c
]);
1211 for (j
= 0; j
< 8; j
++)
1213 /* printf ("(0x%02x ", keys[i].k[j]);*/
1214 if (keys
[i
].k
[j
] & B30
)
1216 else if (keys
[i
].k
[j
] & B31
)
1218 switch (keys
[i
].k
[j
])
1251 printf ("ABS8SDST");
1257 printf ("ABS16SRC");
1260 printf ("ABS16OR8SRC");
1263 printf ("ABS16SDST");
1266 printf ("ABS16OR8SDST");
1272 printf ("DISPSDST");
1292 case ABS16ORREL8SRC
:
1293 printf ("ABS16ORREL8SRC");
1304 printf ("%s\n", ce
);
1312 memset (done_for
, 0, sizeof (done_for
));
1313 printf ("switch (b0) {\n");
1314 for (i
= 0; i
< nkeys
; i
++)
1316 struct h8_opcode
*p
;
1317 struct h8_opcode
*l
= 0;
1320 printf ("%s KEY %d %s\n", cs
,i
,ce
);
1327 for (p
= h8_opcodes
; p
->name
; p
++)
1331 if (p
->idx
== i
&& p
->name
!= empty
)
1334 if (done_for
[p
->sorted_key
] != i
+ 1000)
1336 done_for
[p
->sorted_key
] = i
+ 1000;
1337 printf ("case 0x%03x:\n", p
->sorted_key
);
1338 printf("%s %s %s\n", cs
, p
->name
, ce
);
1345 decode (l
, fetch
, l
->size
);
1346 printf ("break;\n");
1349 printf (" /* NO */\n");
1357 struct h8_opcode
*s
;
1358 struct h8_opcode
*d
;
1359 for (d
= s
= h8_opcodes
; s
->name
; s
++)
1362 if (strcmp(s
->name
,"push") ==0) doit
= 0;
1363 if (strcmp(s
->name
,"pop") ==0) doit
= 0;
1380 printf("%s fetch the args %s\n", cs
,ce
);
1383 printf("%s do the operation %s\n", cs
,ce
);
1384 printf ("switch (b0) \n{\n");
1385 for (i
= 0; i
< PTWO
; i
++)
1391 printf("%s store the result %s\n", cs
,ce
);
This page took 0.082581 seconds and 4 git commands to generate.