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)
42 n1 = (int) p->data.nib[0];
47 if ((int) p->data.nib[1] < 16)
49 n2 = (int) p->data.nib[1];
54 /* Just make sure there are an even number of nibbles in it, and
55 that the count is the same as the length. */
56 for (i = 0; p->data.nib[i] != E; i++)
68 bfd_h8_disassemble (addr, info, mode)
70 disassemble_info *info;
73 /* Find the first entry in the table for this opcode. */
74 static CONST char *regnames[] =
76 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
77 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"
79 static CONST char *wregnames[] =
81 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
82 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
85 static CONST char *lregnames[] =
87 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
88 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
97 static boolean init = 0;
98 struct h8_opcode *q = h8_opcodes;
99 char CONST **pregnames = mode != 0 ? lregnames : wregnames;
102 unsigned char data[20];
103 void *stream = info->stream;
104 fprintf_ftype fprintf = info->fprintf_func;
108 bfd_h8_disassemble_init ();
112 status = info->read_memory_func (addr, data, 2, info);
115 info->memory_error_func (status, addr, info);
119 for (l = 2; status == 0 && l < 10; l += 2)
120 status = info->read_memory_func(addr+l, data+l, 2, info);
122 /* Find the exact opcode/arg combo. */
126 unsigned int len = 0;
132 op_type looking_for = *nib;
133 int thisnib = data[len >> 1];
135 thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
137 if (looking_for < 16 && looking_for >=0)
139 if (looking_for != thisnib)
144 if ((int) looking_for & (int) B31)
146 if (! (((int) thisnib & 0x8) != 0))
148 looking_for = (op_type) ((int) looking_for & ~(int) B31);
151 if ((int) looking_for & (int) B30)
153 if (!(((int) thisnib & 0x8) == 0))
155 looking_for = (op_type) ((int) looking_for & ~(int) B30);
158 if (looking_for & DBIT)
160 if ((looking_for & 2) != (thisnib & 2))
162 abs = (thisnib & 0x8) ? 2 : 1;
164 else if (looking_for & (REG | IND|INC|DEC))
166 if (looking_for & SRC)
171 else if (looking_for & L_16)
173 abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
176 else if (looking_for & ABSJMP)
183 else if (looking_for & MEMIND)
187 else if (looking_for & L_32)
191 abs = (data[i] << 24)
192 | (data[i + 1] << 16)
198 else if (looking_for & L_24)
201 abs = (data[i] << 16) | (data[i + 1] << 8)| (data[i+2]);
204 else if (looking_for & IGNORE)
208 else if (looking_for & DISPREG)
212 else if (looking_for & KBIT)
229 else if (looking_for & L_8)
232 abs = data[len >> 1];
234 else if (looking_for & L_3)
238 else if (looking_for & L_2)
243 else if (looking_for & MACREG)
245 abs = (thisnib == 3);
247 else if (looking_for == E)
251 for (i = 0; i < q->length; i++)
252 fprintf (stream, "%02x ", data[i]);
255 fprintf (stream, " ");
257 fprintf (stream, "%s\t", q->name);
259 /* Gross. Disgusting. */
260 if (strcmp (q->name, "ldm.l") == 0)
264 count = (data[1] >> 4) & 0x3;
265 high = data[3] & 0x7;
267 fprintf (stream, "@sp+,er%d-er%d", high - count, high);
271 if (strcmp (q->name, "stm.l") == 0)
275 count = (data[1] >> 4) & 0x3;
278 fprintf (stream, "er%d-er%d,@-sp", low, low + count);
282 /* Fill in the args. */
284 op_type *args = q->args.nib;
292 fprintf (stream, ",");
296 fprintf (stream, "#0x%x", (unsigned) bit);
298 else if (x & (IMM|KBIT|DBIT))
300 /* Bletch. For shal #2,er0 and friends. */
301 if (*(args+1) & SRC_IN_DST)
304 fprintf (stream, "#0x%x", (unsigned) abs);
308 int rn = (x & DST) ? rd : rs;
313 fprintf (stream, "%s", regnames[rn]);
316 fprintf (stream, "%s", wregnames[rn]);
320 fprintf (stream, "%s", lregnames[rn]);
326 fprintf (stream, "mac%c", abs ? 'l' : 'h');
330 fprintf (stream, "@%s+", pregnames[rs]);
334 fprintf (stream, "@-%s", pregnames[rd]);
338 int rn = (x & DST) ? rd : rs;
339 fprintf (stream, "@%s", pregnames[rn]);
341 else if (x & ABS8MEM)
343 fprintf (stream, "@0x%x:8", (unsigned) abs);
345 else if (x & (ABS|ABSJMP))
347 fprintf (stream, "@0x%x:%d", (unsigned) abs, plen);
351 fprintf (stream, "@@%d (%x)", abs, abs);
358 fprintf (stream, ".%s%d (%x)", (short) abs > 0 ? "+" : "", (short) abs,
359 addr + (short) abs + 2);
363 fprintf (stream, ".%s%d (%x)", (char) abs > 0 ? "+" : "", (char) abs,
364 addr + (char) abs + 2);
369 fprintf (stream, "@(0x%x:%d,%s)", abs,plen, pregnames[rdisp]);
373 fprintf (stream, "ccr");
377 fprintf (stream, "exr");
380 /* xgettext:c-format */
381 fprintf (stream, _("Hmmmm %x"), x);
391 /* xgettext:c-format */
392 fprintf (stream, _("Don't understand %x \n"), looking_for);
404 /* Fell of the end. */
405 fprintf (stream, "%02x %02x .word\tH'%x,H'%x",
412 print_insn_h8300 (addr, info)
414 disassemble_info *info;
416 return bfd_h8_disassemble (addr, info , 0);
420 print_insn_h8300h (addr, info)
422 disassemble_info *info;
424 return bfd_h8_disassemble (addr, info , 1);
428 print_insn_h8300s (addr, info)
430 disassemble_info *info;
432 return bfd_h8_disassemble (addr, info , 2);