* Makefile.am: Run "make dep-am".
[binutils.git] / opcodes / z80-dis.c
blob38ce4d118098a87bfc3d130909fc4565efe79e64
1 /* Print Z80 and R800 instructions
2 Copyright 2005 Free Software Foundation, Inc.
3 Contributed by Arnold Metselaar <arnold_m@operamail.com>
5 This file 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., 51 Franklin Street - Fifth Floor, Boston,
18 MA 02110-1301, USA. */
20 #include "sysdep.h"
21 #include "dis-asm.h"
22 #include <stdio.h>
24 struct buffer
26 bfd_vma base;
27 int n_fetch;
28 int n_used;
29 char data[4];
30 } ;
32 typedef int (*func)(struct buffer *, disassemble_info *, char *);
34 struct tab_elt
36 unsigned char val;
37 unsigned char mask;
38 func fp;
39 char * text;
40 } ;
42 #define TXTSIZ 16
43 /* Names of 16-bit registers. */
44 static char * rr_str[] = { "bc", "de", "hl", "sp" };
45 /* Names of 8-bit registers. */
46 static char * r_str[] = { "b", "c", "d", "e", "h", "l", "(hl)", "a" };
47 /* Texts for condition codes. */
48 static char * cc_str[] = { "nz", "z", "nc", "c", "po", "pe", "p", "m" };
49 /* Instruction names for 8-bit arithmetic, operand "a" is often implicit */
50 static char * arit_str[] =
52 "add a,", "adc a,", "sub ", "sbc a,", "and ", "xor ", "or ", "cp "
53 } ;
55 static int
56 fetch_data (struct buffer *buf, disassemble_info * info, int n)
58 int r;
60 if (buf->n_fetch + n > 4)
61 abort ();
63 r = info->read_memory_func (buf->base + buf->n_fetch,
64 buf->data + buf->n_fetch,
65 n, info);
66 if (r == 0)
67 buf->n_fetch += n;
68 return !r;
71 static int
72 prt (struct buffer *buf, disassemble_info * info, char *txt)
74 info->fprintf_func (info->stream, "%s", txt);
75 buf->n_used = buf->n_fetch;
76 return 1;
79 static int
80 prt_e (struct buffer *buf, disassemble_info * info, char *txt)
82 char e;
83 int target_addr;
85 if (fetch_data (buf, info, 1))
87 e = buf->data[1];
88 target_addr = (buf->base + 2 + e) & 0xffff;
89 buf->n_used = buf->n_fetch;
90 info->fprintf_func (info->stream, "%s0x%04x", txt, target_addr);
92 else
93 buf->n_used = -1;
95 return buf->n_used;
98 static int
99 jr_cc (struct buffer *buf, disassemble_info * info, char *txt)
101 char mytxt[TXTSIZ];
103 snprintf (mytxt, TXTSIZ, txt, cc_str[(buf->data[0] >> 3) & 3]);
104 return prt_e (buf, info, mytxt);
107 static int
108 prt_nn (struct buffer *buf, disassemble_info * info, char *txt)
110 int nn;
111 unsigned char *p;
113 p = (unsigned char*) buf->data + buf->n_fetch;
114 if (fetch_data (buf, info, 2))
116 nn = p[0] + (p[1] << 8);
117 info->fprintf_func (info->stream, txt, nn);
118 buf->n_used = buf->n_fetch;
120 else
121 buf->n_used = -1;
122 return buf->n_used;
125 static int
126 prt_rr_nn (struct buffer *buf, disassemble_info * info, char *txt)
128 char mytxt[TXTSIZ];
130 snprintf (mytxt, TXTSIZ, txt, rr_str[(buf->data[0] >> 4) & 3]);
131 return prt_nn (buf, info, mytxt);
134 static int
135 prt_rr (struct buffer *buf, disassemble_info * info, char *txt)
137 info->fprintf_func (info->stream, "%s%s", txt,
138 rr_str[(buf->data[buf->n_fetch - 1] >> 4) & 3]);
139 buf->n_used = buf->n_fetch;
140 return buf->n_used;
143 static int
144 prt_n (struct buffer *buf, disassemble_info * info, char *txt)
146 int n;
147 unsigned char *p;
149 p = (unsigned char*) buf->data + buf->n_fetch;
151 if (fetch_data (buf, info, 1))
153 n = p[0];
154 info->fprintf_func (info->stream, txt, n);
155 buf->n_used = buf->n_fetch;
157 else
158 buf->n_used = -1;
160 return buf->n_used;
163 static int
164 ld_r_n (struct buffer *buf, disassemble_info * info, char *txt)
166 char mytxt[TXTSIZ];
168 snprintf (mytxt, TXTSIZ, txt, r_str[(buf->data[0] >> 3) & 7]);
169 return prt_n (buf, info, mytxt);
172 static int
173 prt_r (struct buffer *buf, disassemble_info * info, char *txt)
175 info->fprintf_func (info->stream, txt,
176 r_str[(buf->data[buf->n_fetch - 1] >> 3) & 7]);
177 buf->n_used = buf->n_fetch;
178 return buf->n_used;
181 static int
182 ld_r_r (struct buffer *buf, disassemble_info * info, char *txt)
184 info->fprintf_func (info->stream, txt,
185 r_str[(buf->data[buf->n_fetch - 1] >> 3) & 7],
186 r_str[buf->data[buf->n_fetch - 1] & 7]);
187 buf->n_used = buf->n_fetch;
188 return buf->n_used;
191 static int
192 arit_r (struct buffer *buf, disassemble_info * info, char *txt)
194 info->fprintf_func (info->stream, txt,
195 arit_str[(buf->data[buf->n_fetch - 1] >> 3) & 7],
196 r_str[buf->data[buf->n_fetch - 1] & 7]);
197 buf->n_used = buf->n_fetch;
198 return buf->n_used;
201 static int
202 prt_cc (struct buffer *buf, disassemble_info * info, char *txt)
204 info->fprintf_func (info->stream, "%s%s", txt,
205 cc_str[(buf->data[0] >> 3) & 7]);
206 buf->n_used = buf->n_fetch;
207 return buf->n_used;
210 static int
211 pop_rr (struct buffer *buf, disassemble_info * info, char *txt)
213 static char *rr_stack[] = { "bc","de","hl","af"};
215 info->fprintf_func (info->stream, "%s %s", txt,
216 rr_stack[(buf->data[0] >> 4) & 3]);
217 buf->n_used = buf->n_fetch;
218 return buf->n_used;
222 static int
223 jp_cc_nn (struct buffer *buf, disassemble_info * info, char *txt)
225 char mytxt[TXTSIZ];
227 snprintf (mytxt,TXTSIZ,
228 "%s%s,0x%%04x", txt, cc_str[(buf->data[0] >> 3) & 7]);
229 return prt_nn (buf, info, mytxt);
232 static int
233 arit_n (struct buffer *buf, disassemble_info * info, char *txt)
235 char mytxt[TXTSIZ];
237 snprintf (mytxt,TXTSIZ, txt, arit_str[(buf->data[0] >> 3) & 7]);
238 return prt_n (buf, info, mytxt);
241 static int
242 rst (struct buffer *buf, disassemble_info * info, char *txt)
244 info->fprintf_func (info->stream, txt, buf->data[0] & 0x38);
245 buf->n_used = buf->n_fetch;
246 return buf->n_used;
250 static int
251 cis (struct buffer *buf, disassemble_info * info, char *txt ATTRIBUTE_UNUSED)
253 static char * opar[] = { "ld", "cp", "in", "out" };
254 char * op;
255 char c;
257 c = buf->data[1];
258 op = ((0x13 & c) == 0x13) ? "ot" : (opar[c & 3]);
259 info->fprintf_func (info->stream,
260 "%s%c%s", op,
261 (c & 0x08) ? 'd' : 'i',
262 (c & 0x10) ? "r" : "");
263 buf->n_used = 2;
264 return buf->n_used;
267 static int
268 dump (struct buffer *buf, disassemble_info * info, char *txt)
270 int i;
272 info->fprintf_func (info->stream, "defb ");
273 for (i = 0; txt[i]; ++i)
274 info->fprintf_func (info->stream, i ? ", 0x%02x" : "0x%02x",
275 (unsigned char) buf->data[i]);
276 buf->n_used = i;
277 return buf->n_used;
280 /* Table to disassemble machine codes with prefix 0xED. */
281 struct tab_elt opc_ed[] =
283 { 0x70, 0xFF, prt, "in f,(c)" },
284 { 0x70, 0xFF, dump, "xx" },
285 { 0x40, 0xC7, prt_r, "in %s,(c)" },
286 { 0x71, 0xFF, prt, "out (c),0" },
287 { 0x70, 0xFF, dump, "xx" },
288 { 0x41, 0xC7, prt_r, "out (c),%s" },
289 { 0x42, 0xCF, prt_rr, "sbc hl," },
290 { 0x43, 0xCF, prt_rr_nn, "ld (0x%%04x),%s" },
291 { 0x44, 0xFF, prt, "neg" },
292 { 0x45, 0xFF, prt, "retn" },
293 { 0x46, 0xFF, prt, "im 0" },
294 { 0x47, 0xFF, prt, "ld i,a" },
295 { 0x4A, 0xCF, prt_rr, "adc hl," },
296 { 0x4B, 0xCF, prt_rr_nn, "ld %s,(0x%%04x)" },
297 { 0x4D, 0xFF, prt, "reti" },
298 { 0x56, 0xFF, prt, "im 1" },
299 { 0x57, 0xFF, prt, "ld a,i" },
300 { 0x5E, 0xFF, prt, "im 2" },
301 { 0x67, 0xFF, prt, "rrd" },
302 { 0x6F, 0xFF, prt, "rld" },
303 { 0xA0, 0xE4, cis, "" },
304 { 0xC3, 0xFF, prt, "muluw hl,bc" },
305 { 0xC5, 0xE7, prt_r, "mulub a,%s" },
306 { 0xF3, 0xFF, prt, "muluw hl,sp" },
307 { 0x00, 0x00, dump, "xx" }
310 static int
311 pref_ed (struct buffer * buf, disassemble_info * info,
312 char* txt ATTRIBUTE_UNUSED)
314 struct tab_elt *p;
316 if (fetch_data(buf, info, 1))
318 for (p = opc_ed; p->val != (buf->data[1] & p->mask); ++p)
320 p->fp (buf, info, p->text);
322 else
323 buf->n_used = -1;
325 return buf->n_used;
328 /* Instruction names for the instructions addressing single bits. */
329 static char *cb1_str[] = { "", "bit", "res", "set"};
330 /* Instruction names for shifts and rotates. */
331 static char *cb2_str[] =
333 "rlc", "rrc", "rl", "rr", "sla", "sra", "sli", "srl"
336 static int
337 pref_cb (struct buffer * buf, disassemble_info * info,
338 char* txt ATTRIBUTE_UNUSED)
340 if (fetch_data (buf, info, 1))
342 buf->n_used = 2;
343 if ((buf->data[1] & 0xc0) == 0)
344 info->fprintf_func (info->stream, "%s %s",
345 cb2_str[(buf->data[1] >> 3) & 7],
346 r_str[buf->data[1] & 7]);
347 else
348 info->fprintf_func (info->stream, "%s %d,%s",
349 cb1_str[(buf->data[1] >> 6) & 3],
350 (buf->data[1] >> 3) & 7,
351 r_str[buf->data[1] & 7]);
353 else
354 buf->n_used = -1;
356 return buf->n_used;
359 static int
360 addvv (struct buffer * buf, disassemble_info * info, char* txt)
362 info->fprintf_func (info->stream, "add %s,%s", txt, txt);
364 return buf->n_used = buf->n_fetch;
367 static int
368 ld_v_v (struct buffer * buf, disassemble_info * info, char* txt)
370 char mytxt[TXTSIZ];
372 snprintf (mytxt, TXTSIZ, "ld %s%%s,%s%%s", txt, txt);
373 return ld_r_r (buf, info, mytxt);
376 static int
377 prt_d (struct buffer *buf, disassemble_info * info, char *txt)
379 int d;
380 signed char *p;
382 p = (unsigned char*) buf->data + buf->n_fetch;
384 if (fetch_data (buf, info, 1))
386 d = p[0];
387 info->fprintf_func (info->stream, txt, d);
388 buf->n_used = buf->n_fetch;
390 else
391 buf->n_used = -1;
393 return buf->n_used;
396 static int
397 prt_d_n (struct buffer *buf, disassemble_info * info, char *txt)
399 char mytxt[TXTSIZ];
400 int d;
401 signed char *p;
403 p = (unsigned char*) buf->data + buf->n_fetch;
405 if (fetch_data (buf, info, 1))
407 d = p[0];
408 snprintf (mytxt, TXTSIZ, txt, d);
409 return prt_n (buf, info, mytxt);
411 else
412 buf->n_used = -1;
414 return buf->n_used;
417 static int
418 arit_d (struct buffer *buf, disassemble_info * info, char *txt)
420 char mytxt[TXTSIZ];
421 unsigned char c;
423 c = buf->data[buf->n_fetch - 1];
424 snprintf (mytxt, TXTSIZ, txt, arit_str[(c >> 3) & 7]);
425 return prt_d (buf, info, mytxt);
428 static int
429 ld_r_d (struct buffer *buf, disassemble_info * info, char *txt)
431 char mytxt[TXTSIZ];
432 unsigned char c;
434 c = buf->data[buf->n_fetch - 1];
435 snprintf (mytxt, TXTSIZ, txt, r_str[(c >> 3) & 7]);
436 return prt_d (buf, info, mytxt);
439 static int
440 ld_d_r(struct buffer *buf, disassemble_info * info, char *txt)
442 char mytxt[TXTSIZ];
443 unsigned char c;
445 c = buf->data[buf->n_fetch - 1];
446 snprintf (mytxt, TXTSIZ, txt, r_str[c & 7]);
447 return prt_d (buf, info, mytxt);
450 static int
451 pref_xd_cb (struct buffer * buf, disassemble_info * info, char* txt)
453 if (fetch_data (buf, info, 2))
455 int d;
456 char arg[TXTSIZ];
457 signed char *p;
459 buf->n_used = 4;
460 p = buf->data;
461 d = p[2];
463 if (((p[3] & 0xC0) == 0x40) || ((p[3] & 7) == 0x06))
464 snprintf (arg, TXTSIZ, "(%s+%d)", txt, d);
465 else
466 snprintf (arg, TXTSIZ, "(%s+%d),%s", txt, d, r_str[p[3] & 7]);
468 if ((p[3] & 0xc0) == 0)
469 info->fprintf_func (info->stream, "%s %s",
470 cb2_str[(buf->data[3] >> 3) & 7],
471 arg);
472 else
473 info->fprintf_func (info->stream, "%s %d,%s",
474 cb1_str[(buf->data[3] >> 6) & 3],
475 (buf->data[3] >> 3) & 7,
476 arg);
478 else
479 buf->n_used = -1;
481 return buf->n_used;
484 /* Table to disassemble machine codes with prefix 0xDD or 0xFD. */
485 static struct tab_elt opc_ind[] =
487 { 0x24, 0xF7, prt_r, "inc %s%%s" },
488 { 0x25, 0xF7, prt_r, "dec %s%%s" },
489 { 0x26, 0xF7, ld_r_n, "ld %s%%s,0x%%%%02x" },
490 { 0x21, 0xFF, prt_nn, "ld %s,0x%%04x" },
491 { 0x22, 0xFF, prt_nn, "ld (0x%%04x),%s" },
492 { 0x2A, 0xFF, prt_nn, "ld %s,(0x%%04x)" },
493 { 0x23, 0xFF, prt, "inc %s" },
494 { 0x2B, 0xFF, prt, "dec %s" },
495 { 0x29, 0xFF, addvv, "%s" },
496 { 0x09, 0xCF, prt_rr, "add %s," },
497 { 0x34, 0xFF, prt_d, "inc (%s+%%d)" },
498 { 0x35, 0xFF, prt_d, "dec (%s+%%d)" },
499 { 0x36, 0xFF, prt_d_n, "ld (%s+%%d),0x%%02x" },
501 { 0x76, 0xFF, dump, "h" },
502 { 0x46, 0xC7, ld_r_d, "ld %%s,(%s+%%%%d)" },
503 { 0x70, 0xF8, ld_d_r, "ld (%s+%%%%d),%%s" },
504 { 0x64, 0xF6, ld_v_v, "%s" },
505 { 0x60, 0xF0, ld_r_r, "ld %s%%s,%%s" },
506 { 0x44, 0xC6, ld_r_r, "ld %%s,%s%%s" },
508 { 0x86, 0xC7, arit_d, "%%s(%s+%%%%d)" },
509 { 0x84, 0xC6, arit_r, "%%s%s%%s" },
511 { 0xE1, 0xFF, prt, "pop %s" },
512 { 0xE5, 0xFF, prt, "push %s" },
513 { 0xCB, 0xFF, pref_xd_cb, "%s" },
514 { 0xE3, 0xFF, prt, "ex (sp),%s" },
515 { 0xE9, 0xFF, prt, "jp (%s)" },
516 { 0xF9, 0xFF, prt, "ld sp,%s" },
517 { 0x00, 0x00, dump, "?" },
520 static int
521 pref_ind (struct buffer * buf, disassemble_info * info, char* txt)
523 if (fetch_data (buf, info, 1))
525 char mytxt[TXTSIZ];
526 struct tab_elt *p;
528 for (p = opc_ind; p->val != (buf->data[1] & p->mask); ++p)
530 snprintf (mytxt, TXTSIZ, p->text, txt);
531 p->fp (buf, info, mytxt);
533 else
534 buf->n_used = -1;
536 return buf->n_used;
539 /* Table to disassemble machine codes without prefix. */
540 static struct tab_elt opc_main[] =
542 { 0x00, 0xFF, prt, "nop" },
543 { 0x01, 0xCF, prt_rr_nn, "ld %s,0x%%04x" },
544 { 0x02, 0xFF, prt, "ld (bc),a" },
545 { 0x03, 0xCF, prt_rr, "inc " },
546 { 0x04, 0xC7, prt_r, "inc %s" },
547 { 0x05, 0xC7, prt_r, "dec %s" },
548 { 0x06, 0xC7, ld_r_n, "ld %s,0x%%02x" },
549 { 0x07, 0xFF, prt, "rlca" },
550 { 0x08, 0xFF, prt, "ex af,af'" },
551 { 0x09, 0xCF, prt_rr, "add hl," },
552 { 0x0A, 0xFF, prt, "ld a,(bc)" },
553 { 0x0B, 0xCF, prt_rr, "dec " },
554 { 0x0F, 0xFF, prt, "rrca" },
555 { 0x10, 0xFF, prt_e, "djnz " },
556 { 0x12, 0xFF, prt, "ld (de),a" },
557 { 0x17, 0xFF, prt, "rla" },
558 { 0x18, 0xFF, prt_e, "jr "},
559 { 0x1A, 0xFF, prt, "ld a,(de)" },
560 { 0x1F, 0xFF, prt, "rra" },
561 { 0x20, 0xE7, jr_cc, "jr %s,"},
562 { 0x22, 0xFF, prt_nn, "ld (0x%04x),hl" },
563 { 0x27, 0xFF, prt, "daa"},
564 { 0x2A, 0xFF, prt_nn, "ld hl,(0x%04x)" },
565 { 0x2F, 0xFF, prt, "cpl" },
566 { 0x32, 0xFF, prt_nn, "ld (0x%04x),a" },
567 { 0x37, 0xFF, prt, "scf" },
568 { 0x3A, 0xFF, prt_nn, "ld a,(0x%04x)" },
569 { 0x3F, 0xFF, prt, "ccf" },
571 { 0x76, 0xFF, prt, "halt" },
572 { 0x40, 0xC0, ld_r_r, "ld %s,%s"},
574 { 0x80, 0xC0, arit_r, "%s%s" },
576 { 0xC0, 0xC7, prt_cc, "ret " },
577 { 0xC1, 0xCF, pop_rr, "pop" },
578 { 0xC2, 0xC7, jp_cc_nn, "jp " },
579 { 0xC3, 0xFF, prt_nn, "jp 0x%04x" },
580 { 0xC4, 0xC7, jp_cc_nn, "call " },
581 { 0xC5, 0xCF, pop_rr, "push" },
582 { 0xC6, 0xC7, arit_n, "%s0x%%02x" },
583 { 0xC7, 0xC7, rst, "rst 0x%02x" },
584 { 0xC9, 0xFF, prt, "ret" },
585 { 0xCB, 0xFF, pref_cb, "" },
586 { 0xCD, 0xFF, prt_nn, "call 0x%04x" },
587 { 0xD3, 0xFF, prt_n, "out (0x%02x),a" },
588 { 0xD9, 0xFF, prt, "exx" },
589 { 0xDB, 0xFF, prt_n, "in a,(0x%02x)" },
590 { 0xDD, 0xFF, pref_ind, "ix" },
591 { 0xE3, 0xFF, prt, "ex (sp),hl" },
592 { 0xE9, 0xFF, prt, "jp (hl)" },
593 { 0xEB, 0xFF, prt, "ex de,hl" },
594 { 0xED, 0xFF, pref_ed, ""},
595 { 0xF3, 0xFF, prt, "di" },
596 { 0xF9, 0xFF, prt, "ld sp,hl" },
597 { 0xFB, 0xFF, prt, "ei" },
598 { 0xFD, 0xFF, pref_ind, "iy" },
599 { 0x00, 0x00, prt, "????" },
603 print_insn_z80 (bfd_vma addr, disassemble_info * info)
605 struct buffer buf;
606 struct tab_elt *p;
608 buf.base = addr;
609 buf.n_fetch = 0;
610 buf.n_used = 0;
612 if (! fetch_data (& buf, info, 1))
613 return -1;
615 for (p = opc_main; p->val != (buf.data[0] & p->mask); ++p)
617 p->fp (& buf, info, p->text);
619 return buf.n_used;