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"
27 static void bfd_h8_disassemble_init
PARAMS ((void));
28 static unsigned int bfd_h8_disassemble
29 PARAMS ((bfd_vma
, disassemble_info
*, int));
31 /* Run through the opcodes and sort them into order to make them easy
34 bfd_h8_disassemble_init ()
39 for (p
= h8_opcodes
; p
->name
; p
++)
44 if ((int) p
->data
.nib
[0] < 16)
45 n1
= (int) p
->data
.nib
[0];
49 if ((int) p
->data
.nib
[1] < 16)
50 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
++)
67 bfd_h8_disassemble (addr
, info
, mode
)
69 disassemble_info
*info
;
72 /* Find the first entry in the table for this opcode. */
73 static const char *regnames
[] =
75 "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
76 "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"
78 static const char *wregnames
[] =
80 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
81 "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
83 static const char *lregnames
[] =
85 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
86 "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
94 static boolean init
= 0;
96 char const **pregnames
= mode
!= 0 ? lregnames
: wregnames
;
99 unsigned char data
[20];
100 void *stream
= info
->stream
;
101 fprintf_ftype fprintf
= info
->fprintf_func
;
105 bfd_h8_disassemble_init ();
109 status
= info
->read_memory_func (addr
, data
, 2, info
);
112 info
->memory_error_func (status
, addr
, info
);
116 for (l
= 2; status
== 0 && l
< 10; l
+= 2)
117 status
= info
->read_memory_func (addr
+ l
, data
+ l
, 2, info
);
119 /* Find the exact opcode/arg combo. */
120 for (q
= h8_opcodes
; q
->name
; q
++)
122 op_type
*nib
= q
->data
.nib
;
123 unsigned int len
= 0;
127 op_type looking_for
= *nib
;
128 int thisnib
= data
[len
>> 1];
130 thisnib
= (len
& 1) ? (thisnib
& 0xf) : ((thisnib
>> 4) & 0xf);
132 if (looking_for
< 16 && looking_for
>= 0)
134 if (looking_for
!= thisnib
)
139 if ((int) looking_for
& (int) B31
)
141 if (!(((int) thisnib
& 0x8) != 0))
144 looking_for
= (op_type
) ((int) looking_for
& ~(int) B31
);
147 if ((int) looking_for
& (int) B30
)
149 if (!(((int) thisnib
& 0x8) == 0))
152 looking_for
= (op_type
) ((int) looking_for
& ~(int) B30
);
155 if (looking_for
& DBIT
)
157 /* Exclude adds/subs by looking at bit 0 and 2, and
158 make sure the operand size, either w or l,
159 matches by looking at bit 1. */
160 if ((looking_for
& 7) != (thisnib
& 7))
163 abs
= (thisnib
& 0x8) ? 2 : 1;
165 else if (looking_for
& (REG
| IND
| INC
| DEC
))
167 if (looking_for
& SRC
)
172 else if (looking_for
& L_16
)
174 abs
= (data
[len
>> 1]) * 256 + data
[(len
+ 2) >> 1];
177 else if (looking_for
& ABSJMP
)
179 abs
= (data
[1] << 16) | (data
[2] << 8) | (data
[3]);
181 else if (looking_for
& MEMIND
)
185 else if (looking_for
& L_32
)
189 abs
= (data
[i
] << 24)
190 | (data
[i
+ 1] << 16)
196 else if (looking_for
& L_24
)
200 abs
= (data
[i
] << 16) | (data
[i
+ 1] << 8) | (data
[i
+ 2]);
203 else if (looking_for
& IGNORE
)
207 else if (looking_for
& DISPREG
)
211 else if (looking_for
& KBIT
)
228 else if (looking_for
& L_8
)
231 abs
= data
[len
>> 1];
233 else if (looking_for
& L_3
)
237 else if (looking_for
& L_2
)
242 else if (looking_for
& MACREG
)
244 abs
= (thisnib
== 3);
246 else if (looking_for
== E
)
250 for (i
= 0; i
< q
->length
; i
++)
251 fprintf (stream
, "%02x ", data
[i
]);
254 fprintf (stream
, " ");
256 fprintf (stream
, "%s\t", q
->name
);
258 /* Gross. Disgusting. */
259 if (strcmp (q
->name
, "ldm.l") == 0)
263 count
= (data
[1] >> 4) & 0x3;
264 high
= data
[3] & 0x7;
266 fprintf (stream
, "@sp+,er%d-er%d", high
- count
, high
);
270 if (strcmp (q
->name
, "stm.l") == 0)
274 count
= (data
[1] >> 4) & 0x3;
277 fprintf (stream
, "er%d-er%d,@-sp", low
, low
+ count
);
281 /* Fill in the args. */
283 op_type
*args
= q
->args
.nib
;
291 fprintf (stream
, ",");
295 fprintf (stream
, "#0x%x", (unsigned) bit
);
297 else if (x
& (IMM
| KBIT
| DBIT
))
299 /* Bletch. For shal #2,er0 and friends. */
300 if (*(args
+ 1) & SRC_IN_DST
)
303 fprintf (stream
, "#0x%x", (unsigned) abs
);
307 int rn
= (x
& DST
) ? rd
: rs
;
312 fprintf (stream
, "%s", regnames
[rn
]);
315 fprintf (stream
, "%s", wregnames
[rn
]);
319 fprintf (stream
, "%s", lregnames
[rn
]);
325 fprintf (stream
, "mac%c", abs
? 'l' : 'h');
329 fprintf (stream
, "@%s+", pregnames
[rs
]);
333 fprintf (stream
, "@-%s", pregnames
[rd
]);
337 int rn
= (x
& DST
) ? rd
: rs
;
338 fprintf (stream
, "@%s", pregnames
[rn
]);
340 else if (x
& ABS8MEM
)
342 fprintf (stream
, "@0x%x:8", (unsigned) abs
);
344 else if (x
& (ABS
| ABSJMP
))
346 fprintf (stream
, "@0x%x:%d", (unsigned) abs
, plen
);
350 fprintf (stream
, "@@%d (%x)", abs
, abs
);
359 (short) abs
> 0 ? "+" : "",
360 (short) abs
, addr
+ (short) abs
+ 2);
366 (char) abs
> 0 ? "+" : "",
367 (char) abs
, addr
+ (char) abs
+ 2);
372 fprintf (stream
, "@(0x%x:%d,%s)",
373 abs
, plen
, pregnames
[rdisp
]);
377 fprintf (stream
, "ccr");
381 fprintf (stream
, "exr");
384 /* xgettext:c-format */
385 fprintf (stream
, _("Hmmmm %x"), x
);
395 /* xgettext:c-format */
396 fprintf (stream
, _("Don't understand %x \n"), looking_for
);
407 /* Fell off the end. */
408 fprintf (stream
, "%02x %02x .word\tH'%x,H'%x",
415 print_insn_h8300 (addr
, info
)
417 disassemble_info
*info
;
419 return bfd_h8_disassemble (addr
, info
, 0);
423 print_insn_h8300h (addr
, info
)
425 disassemble_info
*info
;
427 return bfd_h8_disassemble (addr
, info
, 1);
431 print_insn_h8300s (addr
, info
)
433 disassemble_info
*info
;
435 return bfd_h8_disassemble (addr
, info
, 2);