opcodes/
[binutils-gdb.git] / opcodes / z80-dis.c
blobbdb2a4ff08fc1545b63dc6aa6a2706bd5a8b26fa
1 /* Print Z80 and R800 instructions
2 Copyright 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3 Contributed by Arnold Metselaar <arnold_m@operamail.com>
5 This file is part of the GNU opcodes library.
7 This library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 It is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "dis-asm.h"
24 #include <stdio.h>
26 struct buffer
28 bfd_vma base;
29 int n_fetch;
30 int n_used;
31 signed char data[4];
32 } ;
34 typedef int (*func)(struct buffer *, disassemble_info *, char *);
36 struct tab_elt
38 unsigned char val;
39 unsigned char mask;
40 func fp;
41 char * text;
42 } ;
44 #define TXTSIZ 24
45 /* Names of 16-bit registers. */
46 static char * rr_str[] = { "bc", "de", "hl", "sp" };
47 /* Names of 8-bit registers. */
48 static char * r_str[] = { "b", "c", "d", "e", "h", "l", "(hl)", "a" };
49 /* Texts for condition codes. */
50 static char * cc_str[] = { "nz", "z", "nc", "c", "po", "pe", "p", "m" };
51 /* Instruction names for 8-bit arithmetic, operand "a" is often implicit */
52 static char * arit_str[] =
54 "add a,", "adc a,", "sub ", "sbc a,", "and ", "xor ", "or ", "cp "
55 } ;
57 static int
58 fetch_data (struct buffer *buf, disassemble_info * info, int n)
60 int r;
62 if (buf->n_fetch + n > 4)
63 abort ();
65 r = info->read_memory_func (buf->base + buf->n_fetch,
66 (unsigned char*) buf->data + buf->n_fetch,
67 n, info);
68 if (r == 0)
69 buf->n_fetch += n;
70 return !r;
73 static int
74 prt (struct buffer *buf, disassemble_info * info, char *txt)
76 info->fprintf_func (info->stream, "%s", txt);
77 buf->n_used = buf->n_fetch;
78 return 1;
81 static int
82 prt_e (struct buffer *buf, disassemble_info * info, char *txt)
84 char e;
85 int target_addr;
87 if (fetch_data (buf, info, 1))
89 e = buf->data[1];
90 target_addr = (buf->base + 2 + e) & 0xffff;
91 buf->n_used = buf->n_fetch;
92 info->fprintf_func (info->stream, "%s0x%04x", txt, target_addr);
94 else
95 buf->n_used = -1;
97 return buf->n_used;
100 static int
101 jr_cc (struct buffer *buf, disassemble_info * info, char *txt)
103 char mytxt[TXTSIZ];
105 snprintf (mytxt, TXTSIZ, txt, cc_str[(buf->data[0] >> 3) & 3]);
106 return prt_e (buf, info, mytxt);
109 static int
110 prt_nn (struct buffer *buf, disassemble_info * info, char *txt)
112 int nn;
113 unsigned char *p;
115 p = (unsigned char*) buf->data + buf->n_fetch;
116 if (fetch_data (buf, info, 2))
118 nn = p[0] + (p[1] << 8);
119 info->fprintf_func (info->stream, txt, nn);
120 buf->n_used = buf->n_fetch;
122 else
123 buf->n_used = -1;
124 return buf->n_used;
127 static int
128 prt_rr_nn (struct buffer *buf, disassemble_info * info, char *txt)
130 char mytxt[TXTSIZ];
131 int rr;
133 rr = (buf->data[buf->n_fetch - 1] >> 4) & 3;
134 snprintf (mytxt, TXTSIZ, txt, rr_str[rr]);
135 return prt_nn (buf, info, mytxt);
138 static int
139 prt_rr (struct buffer *buf, disassemble_info * info, char *txt)
141 info->fprintf_func (info->stream, "%s%s", txt,
142 rr_str[(buf->data[buf->n_fetch - 1] >> 4) & 3]);
143 buf->n_used = buf->n_fetch;
144 return buf->n_used;
147 static int
148 prt_n (struct buffer *buf, disassemble_info * info, char *txt)
150 int n;
151 unsigned char *p;
153 p = (unsigned char*) buf->data + buf->n_fetch;
155 if (fetch_data (buf, info, 1))
157 n = p[0];
158 info->fprintf_func (info->stream, txt, n);
159 buf->n_used = buf->n_fetch;
161 else
162 buf->n_used = -1;
164 return buf->n_used;
167 static int
168 ld_r_n (struct buffer *buf, disassemble_info * info, char *txt)
170 char mytxt[TXTSIZ];
172 snprintf (mytxt, TXTSIZ, txt, r_str[(buf->data[0] >> 3) & 7]);
173 return prt_n (buf, info, mytxt);
176 static int
177 prt_r (struct buffer *buf, disassemble_info * info, char *txt)
179 info->fprintf_func (info->stream, txt,
180 r_str[(buf->data[buf->n_fetch - 1] >> 3) & 7]);
181 buf->n_used = buf->n_fetch;
182 return buf->n_used;
185 static int
186 ld_r_r (struct buffer *buf, disassemble_info * info, char *txt)
188 info->fprintf_func (info->stream, txt,
189 r_str[(buf->data[buf->n_fetch - 1] >> 3) & 7],
190 r_str[buf->data[buf->n_fetch - 1] & 7]);
191 buf->n_used = buf->n_fetch;
192 return buf->n_used;
195 static int
196 arit_r (struct buffer *buf, disassemble_info * info, char *txt)
198 info->fprintf_func (info->stream, txt,
199 arit_str[(buf->data[buf->n_fetch - 1] >> 3) & 7],
200 r_str[buf->data[buf->n_fetch - 1] & 7]);
201 buf->n_used = buf->n_fetch;
202 return buf->n_used;
205 static int
206 prt_cc (struct buffer *buf, disassemble_info * info, char *txt)
208 info->fprintf_func (info->stream, "%s%s", txt,
209 cc_str[(buf->data[0] >> 3) & 7]);
210 buf->n_used = buf->n_fetch;
211 return buf->n_used;
214 static int
215 pop_rr (struct buffer *buf, disassemble_info * info, char *txt)
217 static char *rr_stack[] = { "bc","de","hl","af"};
219 info->fprintf_func (info->stream, "%s %s", txt,
220 rr_stack[(buf->data[0] >> 4) & 3]);
221 buf->n_used = buf->n_fetch;
222 return buf->n_used;
226 static int
227 jp_cc_nn (struct buffer *buf, disassemble_info * info, char *txt)
229 char mytxt[TXTSIZ];
231 snprintf (mytxt,TXTSIZ,
232 "%s%s,0x%%04x", txt, cc_str[(buf->data[0] >> 3) & 7]);
233 return prt_nn (buf, info, mytxt);
236 static int
237 arit_n (struct buffer *buf, disassemble_info * info, char *txt)
239 char mytxt[TXTSIZ];
241 snprintf (mytxt,TXTSIZ, txt, arit_str[(buf->data[0] >> 3) & 7]);
242 return prt_n (buf, info, mytxt);
245 static int
246 rst (struct buffer *buf, disassemble_info * info, char *txt)
248 info->fprintf_func (info->stream, txt, buf->data[0] & 0x38);
249 buf->n_used = buf->n_fetch;
250 return buf->n_used;
254 static int
255 cis (struct buffer *buf, disassemble_info * info, char *txt ATTRIBUTE_UNUSED)
257 static char * opar[] = { "ld", "cp", "in", "out" };
258 char * op;
259 char c;
261 c = buf->data[1];
262 op = ((0x13 & c) == 0x13) ? "ot" : (opar[c & 3]);
263 info->fprintf_func (info->stream,
264 "%s%c%s", op,
265 (c & 0x08) ? 'd' : 'i',
266 (c & 0x10) ? "r" : "");
267 buf->n_used = 2;
268 return buf->n_used;
271 static int
272 dump (struct buffer *buf, disassemble_info * info, char *txt)
274 int i;
276 info->fprintf_func (info->stream, "defb ");
277 for (i = 0; txt[i]; ++i)
278 info->fprintf_func (info->stream, i ? ", 0x%02x" : "0x%02x",
279 (unsigned char) buf->data[i]);
280 buf->n_used = i;
281 return buf->n_used;
284 /* Table to disassemble machine codes with prefix 0xED. */
285 struct tab_elt opc_ed[] =
287 { 0x70, 0xFF, prt, "in f,(c)" },
288 { 0x70, 0xFF, dump, "xx" },
289 { 0x40, 0xC7, prt_r, "in %s,(c)" },
290 { 0x71, 0xFF, prt, "out (c),0" },
291 { 0x70, 0xFF, dump, "xx" },
292 { 0x41, 0xC7, prt_r, "out (c),%s" },
293 { 0x42, 0xCF, prt_rr, "sbc hl," },
294 { 0x43, 0xCF, prt_rr_nn, "ld (0x%%04x),%s" },
295 { 0x44, 0xFF, prt, "neg" },
296 { 0x45, 0xFF, prt, "retn" },
297 { 0x46, 0xFF, prt, "im 0" },
298 { 0x47, 0xFF, prt, "ld i,a" },
299 { 0x4A, 0xCF, prt_rr, "adc hl," },
300 { 0x4B, 0xCF, prt_rr_nn, "ld %s,(0x%%04x)" },
301 { 0x4D, 0xFF, prt, "reti" },
302 { 0x4F, 0xFF, prt, "ld r,a" },
303 { 0x56, 0xFF, prt, "im 1" },
304 { 0x57, 0xFF, prt, "ld a,i" },
305 { 0x5E, 0xFF, prt, "im 2" },
306 { 0x5F, 0xFF, prt, "ld a,r" },
307 { 0x67, 0xFF, prt, "rrd" },
308 { 0x6F, 0xFF, prt, "rld" },
309 { 0xA0, 0xE4, cis, "" },
310 { 0xC3, 0xFF, prt, "muluw hl,bc" },
311 { 0xC5, 0xE7, prt_r, "mulub a,%s" },
312 { 0xF3, 0xFF, prt, "muluw hl,sp" },
313 { 0x00, 0x00, dump, "xx" }
316 static int
317 pref_ed (struct buffer * buf, disassemble_info * info,
318 char* txt ATTRIBUTE_UNUSED)
320 struct tab_elt *p;
322 if (fetch_data(buf, info, 1))
324 for (p = opc_ed; p->val != (buf->data[1] & p->mask); ++p)
326 p->fp (buf, info, p->text);
328 else
329 buf->n_used = -1;
331 return buf->n_used;
334 /* Instruction names for the instructions addressing single bits. */
335 static char *cb1_str[] = { "", "bit", "res", "set"};
336 /* Instruction names for shifts and rotates. */
337 static char *cb2_str[] =
339 "rlc", "rrc", "rl", "rr", "sla", "sra", "sli", "srl"
342 static int
343 pref_cb (struct buffer * buf, disassemble_info * info,
344 char* txt ATTRIBUTE_UNUSED)
346 if (fetch_data (buf, info, 1))
348 buf->n_used = 2;
349 if ((buf->data[1] & 0xc0) == 0)
350 info->fprintf_func (info->stream, "%s %s",
351 cb2_str[(buf->data[1] >> 3) & 7],
352 r_str[buf->data[1] & 7]);
353 else
354 info->fprintf_func (info->stream, "%s %d,%s",
355 cb1_str[(buf->data[1] >> 6) & 3],
356 (buf->data[1] >> 3) & 7,
357 r_str[buf->data[1] & 7]);
359 else
360 buf->n_used = -1;
362 return buf->n_used;
365 static int
366 addvv (struct buffer * buf, disassemble_info * info, char* txt)
368 info->fprintf_func (info->stream, "add %s,%s", txt, txt);
370 return buf->n_used = buf->n_fetch;
373 static int
374 ld_v_v (struct buffer * buf, disassemble_info * info, char* txt)
376 char mytxt[TXTSIZ];
378 snprintf (mytxt, TXTSIZ, "ld %s%%s,%s%%s", txt, txt);
379 return ld_r_r (buf, info, mytxt);
382 static int
383 prt_d (struct buffer *buf, disassemble_info * info, char *txt)
385 int d;
386 signed char *p;
388 p = buf->data + buf->n_fetch;
390 if (fetch_data (buf, info, 1))
392 d = p[0];
393 info->fprintf_func (info->stream, txt, d);
394 buf->n_used = buf->n_fetch;
396 else
397 buf->n_used = -1;
399 return buf->n_used;
402 static int
403 prt_d_n (struct buffer *buf, disassemble_info * info, char *txt)
405 char mytxt[TXTSIZ];
406 int d;
407 signed char *p;
409 p = buf->data + buf->n_fetch;
411 if (fetch_data (buf, info, 1))
413 d = p[0];
414 snprintf (mytxt, TXTSIZ, txt, d);
415 return prt_n (buf, info, mytxt);
417 else
418 buf->n_used = -1;
420 return buf->n_used;
423 static int
424 arit_d (struct buffer *buf, disassemble_info * info, char *txt)
426 char mytxt[TXTSIZ];
427 signed char c;
429 c = buf->data[buf->n_fetch - 1];
430 snprintf (mytxt, TXTSIZ, txt, arit_str[(c >> 3) & 7]);
431 return prt_d (buf, info, mytxt);
434 static int
435 ld_r_d (struct buffer *buf, disassemble_info * info, char *txt)
437 char mytxt[TXTSIZ];
438 signed char c;
440 c = buf->data[buf->n_fetch - 1];
441 snprintf (mytxt, TXTSIZ, txt, r_str[(c >> 3) & 7]);
442 return prt_d (buf, info, mytxt);
445 static int
446 ld_d_r(struct buffer *buf, disassemble_info * info, char *txt)
448 char mytxt[TXTSIZ];
449 signed char c;
451 c = buf->data[buf->n_fetch - 1];
452 snprintf (mytxt, TXTSIZ, txt, r_str[c & 7]);
453 return prt_d (buf, info, mytxt);
456 static int
457 pref_xd_cb (struct buffer * buf, disassemble_info * info, char* txt)
459 if (fetch_data (buf, info, 2))
461 int d;
462 char arg[TXTSIZ];
463 signed char *p;
465 buf->n_used = 4;
466 p = buf->data;
467 d = p[2];
469 if (((p[3] & 0xC0) == 0x40) || ((p[3] & 7) == 0x06))
470 snprintf (arg, TXTSIZ, "(%s%+d)", txt, d);
471 else
472 snprintf (arg, TXTSIZ, "(%s%+d),%s", txt, d, r_str[p[3] & 7]);
474 if ((p[3] & 0xc0) == 0)
475 info->fprintf_func (info->stream, "%s %s",
476 cb2_str[(buf->data[3] >> 3) & 7],
477 arg);
478 else
479 info->fprintf_func (info->stream, "%s %d,%s",
480 cb1_str[(buf->data[3] >> 6) & 3],
481 (buf->data[3] >> 3) & 7,
482 arg);
484 else
485 buf->n_used = -1;
487 return buf->n_used;
490 /* Table to disassemble machine codes with prefix 0xDD or 0xFD. */
491 static struct tab_elt opc_ind[] =
493 { 0x24, 0xF7, prt_r, "inc %s%%s" },
494 { 0x25, 0xF7, prt_r, "dec %s%%s" },
495 { 0x26, 0xF7, ld_r_n, "ld %s%%s,0x%%%%02x" },
496 { 0x21, 0xFF, prt_nn, "ld %s,0x%%04x" },
497 { 0x22, 0xFF, prt_nn, "ld (0x%%04x),%s" },
498 { 0x2A, 0xFF, prt_nn, "ld %s,(0x%%04x)" },
499 { 0x23, 0xFF, prt, "inc %s" },
500 { 0x2B, 0xFF, prt, "dec %s" },
501 { 0x29, 0xFF, addvv, "%s" },
502 { 0x09, 0xCF, prt_rr, "add %s," },
503 { 0x34, 0xFF, prt_d, "inc (%s%%+d)" },
504 { 0x35, 0xFF, prt_d, "dec (%s%%+d)" },
505 { 0x36, 0xFF, prt_d_n, "ld (%s%%+d),0x%%%%02x" },
507 { 0x76, 0xFF, dump, "h" },
508 { 0x46, 0xC7, ld_r_d, "ld %%s,(%s%%%%+d)" },
509 { 0x70, 0xF8, ld_d_r, "ld (%s%%%%+d),%%s" },
510 { 0x64, 0xF6, ld_v_v, "%s" },
511 { 0x60, 0xF0, ld_r_r, "ld %s%%s,%%s" },
512 { 0x44, 0xC6, ld_r_r, "ld %%s,%s%%s" },
514 { 0x86, 0xC7, arit_d, "%%s(%s%%%%+d)" },
515 { 0x84, 0xC6, arit_r, "%%s%s%%s" },
517 { 0xE1, 0xFF, prt, "pop %s" },
518 { 0xE5, 0xFF, prt, "push %s" },
519 { 0xCB, 0xFF, pref_xd_cb, "%s" },
520 { 0xE3, 0xFF, prt, "ex (sp),%s" },
521 { 0xE9, 0xFF, prt, "jp (%s)" },
522 { 0xF9, 0xFF, prt, "ld sp,%s" },
523 { 0x00, 0x00, dump, "?" },
526 static int
527 pref_ind (struct buffer * buf, disassemble_info * info, char* txt)
529 if (fetch_data (buf, info, 1))
531 char mytxt[TXTSIZ];
532 struct tab_elt *p;
534 for (p = opc_ind; p->val != (buf->data[1] & p->mask); ++p)
536 snprintf (mytxt, TXTSIZ, p->text, txt);
537 p->fp (buf, info, mytxt);
539 else
540 buf->n_used = -1;
542 return buf->n_used;
545 /* Table to disassemble machine codes without prefix. */
546 static struct tab_elt opc_main[] =
548 { 0x00, 0xFF, prt, "nop" },
549 { 0x01, 0xCF, prt_rr_nn, "ld %s,0x%%04x" },
550 { 0x02, 0xFF, prt, "ld (bc),a" },
551 { 0x03, 0xCF, prt_rr, "inc " },
552 { 0x04, 0xC7, prt_r, "inc %s" },
553 { 0x05, 0xC7, prt_r, "dec %s" },
554 { 0x06, 0xC7, ld_r_n, "ld %s,0x%%02x" },
555 { 0x07, 0xFF, prt, "rlca" },
556 { 0x08, 0xFF, prt, "ex af,af'" },
557 { 0x09, 0xCF, prt_rr, "add hl," },
558 { 0x0A, 0xFF, prt, "ld a,(bc)" },
559 { 0x0B, 0xCF, prt_rr, "dec " },
560 { 0x0F, 0xFF, prt, "rrca" },
561 { 0x10, 0xFF, prt_e, "djnz " },
562 { 0x12, 0xFF, prt, "ld (de),a" },
563 { 0x17, 0xFF, prt, "rla" },
564 { 0x18, 0xFF, prt_e, "jr "},
565 { 0x1A, 0xFF, prt, "ld a,(de)" },
566 { 0x1F, 0xFF, prt, "rra" },
567 { 0x20, 0xE7, jr_cc, "jr %s,"},
568 { 0x22, 0xFF, prt_nn, "ld (0x%04x),hl" },
569 { 0x27, 0xFF, prt, "daa"},
570 { 0x2A, 0xFF, prt_nn, "ld hl,(0x%04x)" },
571 { 0x2F, 0xFF, prt, "cpl" },
572 { 0x32, 0xFF, prt_nn, "ld (0x%04x),a" },
573 { 0x37, 0xFF, prt, "scf" },
574 { 0x3A, 0xFF, prt_nn, "ld a,(0x%04x)" },
575 { 0x3F, 0xFF, prt, "ccf" },
577 { 0x76, 0xFF, prt, "halt" },
578 { 0x40, 0xC0, ld_r_r, "ld %s,%s"},
580 { 0x80, 0xC0, arit_r, "%s%s" },
582 { 0xC0, 0xC7, prt_cc, "ret " },
583 { 0xC1, 0xCF, pop_rr, "pop" },
584 { 0xC2, 0xC7, jp_cc_nn, "jp " },
585 { 0xC3, 0xFF, prt_nn, "jp 0x%04x" },
586 { 0xC4, 0xC7, jp_cc_nn, "call " },
587 { 0xC5, 0xCF, pop_rr, "push" },
588 { 0xC6, 0xC7, arit_n, "%s0x%%02x" },
589 { 0xC7, 0xC7, rst, "rst 0x%02x" },
590 { 0xC9, 0xFF, prt, "ret" },
591 { 0xCB, 0xFF, pref_cb, "" },
592 { 0xCD, 0xFF, prt_nn, "call 0x%04x" },
593 { 0xD3, 0xFF, prt_n, "out (0x%02x),a" },
594 { 0xD9, 0xFF, prt, "exx" },
595 { 0xDB, 0xFF, prt_n, "in a,(0x%02x)" },
596 { 0xDD, 0xFF, pref_ind, "ix" },
597 { 0xE3, 0xFF, prt, "ex (sp),hl" },
598 { 0xE9, 0xFF, prt, "jp (hl)" },
599 { 0xEB, 0xFF, prt, "ex de,hl" },
600 { 0xED, 0xFF, pref_ed, ""},
601 { 0xF3, 0xFF, prt, "di" },
602 { 0xF9, 0xFF, prt, "ld sp,hl" },
603 { 0xFB, 0xFF, prt, "ei" },
604 { 0xFD, 0xFF, pref_ind, "iy" },
605 { 0x00, 0x00, prt, "????" },
609 print_insn_z80 (bfd_vma addr, disassemble_info * info)
611 struct buffer buf;
612 struct tab_elt *p;
614 buf.base = addr;
615 buf.n_fetch = 0;
616 buf.n_used = 0;
618 if (! fetch_data (& buf, info, 1))
619 return -1;
621 for (p = opc_main; p->val != (buf.data[0] & p->mask); ++p)
623 p->fp (& buf, info, p->text);
625 return buf.n_used;