1 /* Disassemble h8300 instructions.
2 Copyright (C) 1993, 1998, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #define h8_opcodes h8ops
22 #include "opcode/h8300.h"
27 /* Run through the opcodes and sort them into order to make them easy
30 bfd_h8_disassemble_init ()
35 for (p = h8_opcodes; p->name; p++)
40 if ((int) p->data.nib[0] < 16)
41 n1 = (int) p->data.nib[0];
45 if ((int) p->data.nib[1] < 16)
46 n2 = (int) p->data.nib[1];
50 /* Just make sure there are an even number of nibbles in it, and
51 that the count is the same as the length. */
52 for (i = 0; p->data.nib[i] != E; i++)
64 bfd_h8_disassemble (addr, info, mode)
66 disassemble_info *info;
69 /* Find the first entry in the table for this opcode. */
70 static CONST char *regnames[] =
72 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
73 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"
75 static CONST char *wregnames[] =
77 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
78 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
80 static CONST char *lregnames[] =
82 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
83 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
91 static boolean init = 0;
92 struct h8_opcode *q = h8_opcodes;
93 char CONST **pregnames = mode != 0 ? lregnames : wregnames;
96 unsigned char data[20];
97 void *stream = info->stream;
98 fprintf_ftype fprintf = info->fprintf_func;
102 bfd_h8_disassemble_init ();
106 status = info->read_memory_func (addr, data, 2, info);
109 info->memory_error_func (status, addr, info);
113 for (l = 2; status == 0 && l < 10; l += 2)
114 status = info->read_memory_func (addr + l, data+l, 2, info);
116 /* Find the exact opcode/arg combo. */
120 unsigned int len = 0;
126 op_type looking_for = *nib;
127 int thisnib = data[len >> 1];
129 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
131 if (looking_for < 16 && looking_for >= 0)
133 if (looking_for != thisnib)
138 if ((int) looking_for & (int) B31)
140 if (! (((int) thisnib & 0x8) != 0))
143 looking_for = (op_type) ((int) looking_for & ~(int) B31);
146 if ((int) looking_for & (int) B30)
148 if (!(((int) thisnib & 0x8) == 0))
151 looking_for = (op_type) ((int) looking_for & ~(int) B30);
154 if (looking_for & DBIT)
156 if ((looking_for & 2) != (thisnib & 2))
159 abs = (thisnib & 0x8) ? 2 : 1;
161 else if (looking_for & (REG | IND | INC | DEC))
163 if (looking_for & SRC)
168 else if (looking_for & L_16)
170 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
173 else if (looking_for & ABSJMP)
175 abs = (data[1] << 16) | (data[2] << 8) | (data[3]);
177 else if (looking_for & MEMIND)
181 else if (looking_for & L_32)
185 abs = (data[i] << 24)
186 | (data[i + 1] << 16)
192 else if (looking_for & L_24)
196 abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
199 else if (looking_for & IGNORE)
203 else if (looking_for & DISPREG)
207 else if (looking_for & KBIT)
224 else if (looking_for & L_8)
227 abs = data[len >> 1];
229 else if (looking_for & L_3)
233 else if (looking_for & L_2)
238 else if (looking_for & MACREG)
240 abs = (thisnib == 3);
242 else if (looking_for == E)
246 for (i = 0; i < q->length; i++)
247 fprintf (stream, "%02x ", data[i]);
250 fprintf (stream, " ");
252 fprintf (stream, "%s\t", q->name);
254 /* Gross. Disgusting. */
255 if (strcmp (q->name, "ldm.l") == 0)
259 count = (data[1] >> 4) & 0x3;
260 high = data[3] & 0x7;
262 fprintf (stream, "@sp+,er%d-er%d", high - count, high);
266 if (strcmp (q->name, "stm.l") == 0)
270 count = (data[1] >> 4) & 0x3;
273 fprintf (stream, "er%d-er%d,@-sp", low, low + count);
277 /* Fill in the args. */
279 op_type *args = q->args.nib;
287 fprintf (stream, ",");
291 fprintf (stream, "#0x%x", (unsigned) bit);
293 else if (x & (IMM | KBIT | DBIT))
295 /* Bletch. For shal #2,er0 and friends. */
296 if (*(args + 1) & SRC_IN_DST)
299 fprintf (stream, "#0x%x", (unsigned) abs);
303 int rn = (x & DST) ? rd : rs;
308 fprintf (stream, "%s", regnames[rn]);
311 fprintf (stream, "%s", wregnames[rn]);
315 fprintf (stream, "%s", lregnames[rn]);
321 fprintf (stream, "mac%c", abs ? 'l' : 'h');
325 fprintf (stream, "@%s+", pregnames[rs]);
329 fprintf (stream, "@-%s", pregnames[rd]);
333 int rn = (x & DST) ? rd : rs;
334 fprintf (stream, "@%s", pregnames[rn]);
336 else if (x & ABS8MEM)
338 fprintf (stream, "@0x%x:8", (unsigned) abs);
340 else if (x & (ABS | ABSJMP))
342 fprintf (stream, "@0x%x:%d", (unsigned) abs, plen);
346 fprintf (stream, "@@%d (%x)", abs, abs);
354 ".%s%d (%x)", (short) abs > 0 ? "+" : "",
355 (short) abs, addr + (short) abs + 2);
360 ".%s%d (%x)", (char) abs > 0 ? "+" : "",
361 (char) abs, addr + (char) abs + 2);
366 fprintf (stream, "@(0x%x:%d,%s)",
367 abs,plen, pregnames[rdisp]);
371 fprintf (stream, "ccr");
375 fprintf (stream, "exr");
378 /* xgettext:c-format */
379 fprintf (stream, _("Hmmmm %x"), x);
389 /* xgettext:c-format */
390 fprintf (stream, _("Don't understand %x \n"), looking_for);
401 /* Fell off the end. */
402 fprintf (stream, "%02x %02x .word\tH'%x,H'%x",
410 print_insn_h8300 (addr, info)
412 disassemble_info *info;
414 return bfd_h8_disassemble (addr, info, 0);
418 print_insn_h8300h (addr, info)
420 disassemble_info *info;
422 return bfd_h8_disassemble (addr, info, 1);
426 print_insn_h8300s (addr, info)
428 disassemble_info *info;
430 return bfd_h8_disassemble (addr, info, 2);