1 /* Disassemble h8300 instructions.
2 Copyright 1993, 1994, 1996, 1998, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #define h8_opcodes h8ops
23 #include "opcode/h8300.h"
26 #include "libiberty.h"
31 const struct h8_opcode *opcode;
34 struct h8_instruction *h8_instructions;
36 static void bfd_h8_disassemble_init PARAMS ((void));
37 static unsigned int bfd_h8_disassemble
38 PARAMS ((bfd_vma, disassemble_info *, int));
40 /* Run through the opcodes and sort them into order to make them easy
43 bfd_h8_disassemble_init ()
46 unsigned int nopcodes;
47 const struct h8_opcode *p;
48 struct h8_instruction *pi;
50 nopcodes = sizeof (h8_opcodes) / sizeof (struct h8_opcode);
52 h8_instructions = (struct h8_instruction *)
53 xmalloc (nopcodes * sizeof (struct h8_instruction));
55 for (p = h8_opcodes, pi = h8_instructions; p->name; p++, pi++)
60 if ((int) p->data.nib[0] < 16)
61 n1 = (int) p->data.nib[0];
65 if ((int) p->data.nib[1] < 16)
66 n2 = (int) p->data.nib[1];
70 /* Just make sure there are an even number of nibbles in it, and
71 that the count is the same as the length. */
72 for (i = 0; p->data.nib[i] != E; i++)
82 /* Add entry for the NULL vector terminator. */
88 bfd_h8_disassemble (addr, info, mode)
90 disassemble_info *info;
93 /* Find the first entry in the table for this opcode. */
94 static const char *regnames[] =
96 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
97 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"
99 static const char *wregnames[] =
101 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
102 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
104 static const char *lregnames[] =
106 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
107 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
115 static boolean init = 0;
116 const struct h8_instruction *qi;
117 char const **pregnames = mode != 0 ? lregnames : wregnames;
120 unsigned char data[20];
121 void *stream = info->stream;
122 fprintf_ftype fprintf = info->fprintf_func;
126 bfd_h8_disassemble_init ();
130 status = info->read_memory_func (addr, data, 2, info);
133 info->memory_error_func (status, addr, info);
137 for (l = 2; status == 0 && l < 10; l += 2)
138 status = info->read_memory_func (addr + l, data + l, 2, info);
140 /* Find the exact opcode/arg combo. */
141 for (qi = h8_instructions; qi->opcode->name; qi++)
143 const struct h8_opcode *q = qi->opcode;
144 op_type *nib = q->data.nib;
145 unsigned int len = 0;
149 op_type looking_for = *nib;
150 int thisnib = data[len >> 1];
152 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
154 if (looking_for < 16 && looking_for >= 0)
156 if (looking_for != thisnib)
161 if ((int) looking_for & (int) B31)
163 if (!(((int) thisnib & 0x8) != 0))
166 looking_for = (op_type) ((int) looking_for & ~(int) B31);
169 if ((int) looking_for & (int) B30)
171 if (!(((int) thisnib & 0x8) == 0))
174 looking_for = (op_type) ((int) looking_for & ~(int) B30);
177 if (looking_for & DBIT)
179 /* Exclude adds/subs by looking at bit 0 and 2, and
180 make sure the operand size, either w or l,
181 matches by looking at bit 1. */
182 if ((looking_for & 7) != (thisnib & 7))
185 abs = (thisnib & 0x8) ? 2 : 1;
187 else if (looking_for & (REG | IND | INC | DEC))
189 if (looking_for & SRC)
194 else if (looking_for & L_16)
196 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
199 else if (looking_for & ABSJMP)
201 abs = (data[1] << 16) | (data[2] << 8) | (data[3]);
203 else if (looking_for & MEMIND)
207 else if (looking_for & L_32)
211 abs = (data[i] << 24)
212 | (data[i + 1] << 16)
218 else if (looking_for & L_24)
222 abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
225 else if (looking_for & IGNORE)
229 else if (looking_for & DISPREG)
233 else if (looking_for & KBIT)
250 else if (looking_for & L_8)
253 abs = data[len >> 1];
255 else if (looking_for & L_3)
259 else if (looking_for & L_2)
264 else if (looking_for & MACREG)
266 abs = (thisnib == 3);
268 else if (looking_for == E)
272 for (i = 0; i < qi->length; i++)
273 fprintf (stream, "%02x ", data[i]);
276 fprintf (stream, " ");
278 fprintf (stream, "%s\t", q->name);
280 /* Gross. Disgusting. */
281 if (strcmp (q->name, "ldm.l") == 0)
285 count = (data[1] >> 4) & 0x3;
286 high = data[3] & 0x7;
288 fprintf (stream, "@sp+,er%d-er%d", high - count, high);
292 if (strcmp (q->name, "stm.l") == 0)
296 count = (data[1] >> 4) & 0x3;
299 fprintf (stream, "er%d-er%d,@-sp", low, low + count);
303 /* Fill in the args. */
305 op_type *args = q->args.nib;
313 fprintf (stream, ",");
317 fprintf (stream, "#0x%x", (unsigned) bit);
319 else if (x & (IMM | KBIT | DBIT))
321 /* Bletch. For shal #2,er0 and friends. */
322 if (*(args + 1) & SRC_IN_DST)
325 fprintf (stream, "#0x%x", (unsigned) abs);
329 int rn = (x & DST) ? rd : rs;
334 fprintf (stream, "%s", regnames[rn]);
337 fprintf (stream, "%s", wregnames[rn]);
341 fprintf (stream, "%s", lregnames[rn]);
347 fprintf (stream, "mac%c", abs ? 'l' : 'h');
351 fprintf (stream, "@%s+", pregnames[rs]);
355 fprintf (stream, "@-%s", pregnames[rd]);
359 int rn = (x & DST) ? rd : rs;
360 fprintf (stream, "@%s", pregnames[rn]);
362 else if (x & ABS8MEM)
364 fprintf (stream, "@0x%x:8", (unsigned) abs);
366 else if (x & (ABS | ABSJMP))
368 fprintf (stream, "@0x%x:%d", (unsigned) abs, plen);
372 fprintf (stream, "@@%d (%x)", abs, abs);
381 (short) abs > 0 ? "+" : "",
382 (short) abs, addr + (short) abs + 2);
388 (char) abs > 0 ? "+" : "",
389 (char) abs, addr + (char) abs + 2);
394 fprintf (stream, "@(0x%x:%d,%s)",
395 abs, plen, pregnames[rdisp]);
399 fprintf (stream, "ccr");
403 fprintf (stream, "exr");
406 /* xgettext:c-format */
407 fprintf (stream, _("Hmmmm %x"), x);
417 /* xgettext:c-format */
418 fprintf (stream, _("Don't understand %x \n"), looking_for);
429 /* Fell off the end. */
430 fprintf (stream, "%02x %02x .word\tH'%x,H'%x",
437 print_insn_h8300 (addr, info)
439 disassemble_info *info;
441 return bfd_h8_disassemble (addr, info, 0);
445 print_insn_h8300h (addr, info)
447 disassemble_info *info;
449 return bfd_h8_disassemble (addr, info, 1);
453 print_insn_h8300s (addr, info)
455 disassemble_info *info;
457 return bfd_h8_disassemble (addr, info, 2);