1 /* Disassemble h8300 instructions.
2 Copyright (C) 1993, 1998 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
31 bfd_h8_disassemble_init ()
38 for (p = h8_opcodes; p->name; p++)
43 if ((int) p->data.nib[0] < 16)
45 n1 = (int) p->data.nib[0];
49 if ((int) p->data.nib[1] < 16)
51 n2 = (int) p->data.nib[1];
56 /* Just make sure there are an even number of nibbles in it, and
57 that the count is the same s the length */
58 for (i = 0; p->data.nib[i] != E; i++)
69 bfd_h8_disassemble (addr, info, mode)
71 disassemble_info *info;
74 /* Find the first entry in the table for this opcode */
75 static CONST char *regnames[] =
77 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
78 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"};
80 static CONST char *wregnames[] =
82 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
83 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
86 static CONST char *lregnames[] =
88 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
89 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
99 static boolean init = 0;
100 struct h8_opcode *q = h8_opcodes;
101 char CONST **pregnames = mode != 0 ? lregnames : wregnames;
105 unsigned char data[20];
106 void *stream = info->stream;
107 fprintf_ftype fprintf = info->fprintf_func;
111 bfd_h8_disassemble_init ();
115 status = info->read_memory_func(addr, data, 2, info);
118 info->memory_error_func(status, addr, info);
121 for (l = 2; status == 0 && l < 10; l+=2)
123 status = info->read_memory_func(addr+l, data+l, 2, info);
128 /* Find the exact opcode/arg combo */
132 unsigned int len = 0;
138 op_type looking_for = *nib;
139 int thisnib = data[len >> 1];
141 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
143 if (looking_for < 16 && looking_for >=0)
146 if (looking_for != thisnib)
153 if ((int) looking_for & (int) B31)
155 if (! (((int) thisnib & 0x8) != 0))
157 looking_for = (op_type) ((int) looking_for & ~(int) B31);
159 if ((int) looking_for & (int) B30)
161 if (!(((int) thisnib & 0x8) == 0))
163 looking_for = (op_type) ((int) looking_for & ~(int) B30);
166 if (looking_for & DBIT)
168 if ((looking_for & 5) != (thisnib &5)) goto fail;
169 abs = (thisnib & 0x8) ? 2 : 1;
172 else if (looking_for & (REG | IND|INC|DEC))
174 if (looking_for & SRC)
183 else if (looking_for & L_16)
185 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
189 else if(looking_for & ABSJMP)
196 else if(looking_for & MEMIND)
200 else if (looking_for & L_32)
203 abs = (data[i] << 24)
204 | (data[i + 1] << 16)
211 else if (looking_for & L_24)
214 abs = (data[i] << 16) | (data[i + 1] << 8)| (data[i+2]);
217 else if (looking_for & IGNORE)
221 else if (looking_for & DISPREG)
225 else if (looking_for & KBIT)
242 else if (looking_for & L_8)
245 abs = data[len >> 1];
247 else if (looking_for & L_3)
251 else if (looking_for & L_2)
256 else if (looking_for & MACREG)
258 abs = (thisnib == 3);
260 else if (looking_for == E)
266 for (i = 0; i < q->length; i++)
268 fprintf (stream, "%02x ", data[i]);
272 fprintf (stream, " ");
275 fprintf (stream, "%s\t", q->name);
277 /* Gross. Disgusting. */
278 if (strcmp (q->name, "ldm.l") == 0)
282 count = (data[1] >> 4) & 0x3;
283 high = data[3] & 0x7;
285 fprintf (stream, "@sp+,er%d-er%d", high - count, high);
289 if (strcmp (q->name, "stm.l") == 0)
293 count = (data[1] >> 4) & 0x3;
296 fprintf (stream, "er%d-er%d,@-sp", low, low + count);
300 /* Fill in the args */
302 op_type *args = q->args.nib;
310 fprintf (stream, ",");
315 fprintf (stream, "#0x%x", (unsigned) bit);
317 else if (x & (IMM|KBIT|DBIT))
319 /* Bletch. For shal #2,er0 and friends. */
320 if (*(args+1) & SRC_IN_DST)
323 fprintf (stream, "#0x%x", (unsigned) abs);
327 int rn = (x & DST) ? rd : rs;
331 fprintf (stream, "%s", regnames[rn]);
334 fprintf (stream, "%s", wregnames[rn]);
338 fprintf (stream, "%s", lregnames[rn]);
345 fprintf (stream, "mac%c", abs ? 'l' : 'h');
349 fprintf (stream, "@%s+", pregnames[rs]);
353 fprintf (stream, "@-%s", pregnames[rd]);
358 int rn = (x & DST) ? rd : rs;
359 fprintf (stream, "@%s", pregnames[rn]);
362 else if (x & ABS8MEM)
364 fprintf (stream, "@0x%x:8", (unsigned) abs);
367 else if (x & (ABS|ABSJMP))
369 fprintf (stream, "@0x%x:%d", (unsigned) abs, plen);
374 fprintf (stream, "@@%d (%x)", abs, abs);
382 fprintf (stream, ".%s%d (%x)", (short) abs > 0 ? "+" : "", (short) abs,
383 addr + (short) abs + 2);
386 fprintf (stream, ".%s%d (%x)", (char) abs > 0 ? "+" : "", (char) abs,
387 addr + (char) abs + 2);
392 fprintf (stream, "@(0x%x:%d,%s)", abs,plen, pregnames[rdisp]);
397 fprintf (stream, "ccr");
401 fprintf (stream, "exr");
404 /* xgettext:c-format */
405 fprintf (stream, _("Hmmmm %x"), x);
416 /* xgettext:c-format */
417 fprintf (stream, _("Don't understand %x \n"), looking_for);
429 /* Fell of 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);