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"
26 /* Run through the opcodes and sort them into order to make them easy
29 bfd_h8_disassemble_init ()
34 for (p = h8_opcodes; p->name; p++)
39 if ((int) p->data.nib[0] < 16)
40 n1 = (int) p->data.nib[0];
44 if ((int) p->data.nib[1] < 16)
45 n2 = (int) p->data.nib[1];
49 /* Just make sure there are an even number of nibbles in it, and
50 that the count is the same as the length. */
51 for (i = 0; p->data.nib[i] != E; i++)
62 bfd_h8_disassemble (addr, info, mode)
64 disassemble_info *info;
67 /* Find the first entry in the table for this opcode. */
68 static CONST char *regnames[] =
70 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
71 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"
73 static CONST char *wregnames[] =
75 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
76 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
78 static CONST char *lregnames[] =
80 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
81 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
89 static boolean init = 0;
91 char CONST **pregnames = mode != 0 ? lregnames : wregnames;
94 unsigned char data[20];
95 void *stream = info->stream;
96 fprintf_ftype fprintf = info->fprintf_func;
100 bfd_h8_disassemble_init ();
104 status = info->read_memory_func (addr, data, 2, info);
107 info->memory_error_func (status, addr, info);
111 for (l = 2; status == 0 && l < 10; l += 2)
112 status = info->read_memory_func (addr + l, data + l, 2, info);
114 /* Find the exact opcode/arg combo. */
115 for (q = h8_opcodes; q->name; q++)
117 op_type *nib = q->data.nib;
118 unsigned int len = 0;
122 op_type looking_for = *nib;
123 int thisnib = data[len >> 1];
125 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
127 if (looking_for < 16 && looking_for >= 0)
129 if (looking_for != thisnib)
134 if ((int) looking_for & (int) B31)
136 if (!(((int) thisnib & 0x8) != 0))
139 looking_for = (op_type) ((int) looking_for & ~(int) B31);
142 if ((int) looking_for & (int) B30)
144 if (!(((int) thisnib & 0x8) == 0))
147 looking_for = (op_type) ((int) looking_for & ~(int) B30);
150 if (looking_for & DBIT)
152 /* Exclude adds/subs by looking at bit 0 and 2, and
153 make sure the operand size, either w or l,
154 matches by looking at bit 1. */
155 if ((looking_for & 7) != (thisnib & 7))
158 abs = (thisnib & 0x8) ? 2 : 1;
160 else if (looking_for & (REG | IND | INC | DEC))
162 if (looking_for & SRC)
167 else if (looking_for & L_16)
169 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
172 else if (looking_for & ABSJMP)
174 abs = (data[1] << 16) | (data[2] << 8) | (data[3]);
176 else if (looking_for & MEMIND)
180 else if (looking_for & L_32)
184 abs = (data[i] << 24)
185 | (data[i + 1] << 16)
191 else if (looking_for & L_24)
195 abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
198 else if (looking_for & IGNORE)
202 else if (looking_for & DISPREG)
206 else if (looking_for & KBIT)
223 else if (looking_for & L_8)
226 abs = data[len >> 1];
228 else if (looking_for & L_3)
232 else if (looking_for & L_2)
237 else if (looking_for & MACREG)
239 abs = (thisnib == 3);
241 else if (looking_for == E)
245 for (i = 0; i < q->length; i++)
246 fprintf (stream, "%02x ", data[i]);
249 fprintf (stream, " ");
251 fprintf (stream, "%s\t", q->name);
253 /* Gross. Disgusting. */
254 if (strcmp (q->name, "ldm.l") == 0)
258 count = (data[1] >> 4) & 0x3;
259 high = data[3] & 0x7;
261 fprintf (stream, "@sp+,er%d-er%d", high - count, high);
265 if (strcmp (q->name, "stm.l") == 0)
269 count = (data[1] >> 4) & 0x3;
272 fprintf (stream, "er%d-er%d,@-sp", low, low + count);
276 /* Fill in the args. */
278 op_type *args = q->args.nib;
286 fprintf (stream, ",");
290 fprintf (stream, "#0x%x", (unsigned) bit);
292 else if (x & (IMM | KBIT | DBIT))
294 /* Bletch. For shal #2,er0 and friends. */
295 if (*(args + 1) & SRC_IN_DST)
298 fprintf (stream, "#0x%x", (unsigned) abs);
302 int rn = (x & DST) ? rd : rs;
307 fprintf (stream, "%s", regnames[rn]);
310 fprintf (stream, "%s", wregnames[rn]);
314 fprintf (stream, "%s", lregnames[rn]);
320 fprintf (stream, "mac%c", abs ? 'l' : 'h');
324 fprintf (stream, "@%s+", pregnames[rs]);
328 fprintf (stream, "@-%s", pregnames[rd]);
332 int rn = (x & DST) ? rd : rs;
333 fprintf (stream, "@%s", pregnames[rn]);
335 else if (x & ABS8MEM)
337 fprintf (stream, "@0x%x:8", (unsigned) abs);
339 else if (x & (ABS | ABSJMP))
341 fprintf (stream, "@0x%x:%d", (unsigned) abs, plen);
345 fprintf (stream, "@@%d (%x)", abs, abs);
354 (short) abs > 0 ? "+" : "",
355 (short) abs, addr + (short) abs + 2);
361 (char) abs > 0 ? "+" : "",
362 (char) abs, addr + (char) abs + 2);
367 fprintf (stream, "@(0x%x:%d,%s)",
368 abs, plen, pregnames[rdisp]);
372 fprintf (stream, "ccr");
376 fprintf (stream, "exr");
379 /* xgettext:c-format */
380 fprintf (stream, _("Hmmmm %x"), x);
390 /* xgettext:c-format */
391 fprintf (stream, _("Don't understand %x \n"), looking_for);
402 /* Fell off the end. */
403 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);