1 /* Print Z80 and R800 instructions
2 Copyright 2005, 2007 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)
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. */
34 typedef int (*func
)(struct buffer
*, disassemble_info
*, char *);
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 "
58 fetch_data (struct buffer
*buf
, disassemble_info
* info
, int n
)
62 if (buf
->n_fetch
+ n
> 4)
65 r
= info
->read_memory_func (buf
->base
+ buf
->n_fetch
,
66 (unsigned char*) buf
->data
+ buf
->n_fetch
,
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
;
82 prt_e (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
87 if (fetch_data (buf
, info
, 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
);
101 jr_cc (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
105 snprintf (mytxt
, TXTSIZ
, txt
, cc_str
[(buf
->data
[0] >> 3) & 3]);
106 return prt_e (buf
, info
, mytxt
);
110 prt_nn (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
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
;
128 prt_rr_nn (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
132 snprintf (mytxt
, TXTSIZ
, txt
, rr_str
[(buf
->data
[0] >> 4) & 3]);
133 return prt_nn (buf
, info
, mytxt
);
137 prt_rr (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
139 info
->fprintf_func (info
->stream
, "%s%s", txt
,
140 rr_str
[(buf
->data
[buf
->n_fetch
- 1] >> 4) & 3]);
141 buf
->n_used
= buf
->n_fetch
;
146 prt_n (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
151 p
= (unsigned char*) buf
->data
+ buf
->n_fetch
;
153 if (fetch_data (buf
, info
, 1))
156 info
->fprintf_func (info
->stream
, txt
, n
);
157 buf
->n_used
= buf
->n_fetch
;
166 ld_r_n (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
170 snprintf (mytxt
, TXTSIZ
, txt
, r_str
[(buf
->data
[0] >> 3) & 7]);
171 return prt_n (buf
, info
, mytxt
);
175 prt_r (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
177 info
->fprintf_func (info
->stream
, txt
,
178 r_str
[(buf
->data
[buf
->n_fetch
- 1] >> 3) & 7]);
179 buf
->n_used
= buf
->n_fetch
;
184 ld_r_r (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
186 info
->fprintf_func (info
->stream
, txt
,
187 r_str
[(buf
->data
[buf
->n_fetch
- 1] >> 3) & 7],
188 r_str
[buf
->data
[buf
->n_fetch
- 1] & 7]);
189 buf
->n_used
= buf
->n_fetch
;
194 arit_r (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
196 info
->fprintf_func (info
->stream
, txt
,
197 arit_str
[(buf
->data
[buf
->n_fetch
- 1] >> 3) & 7],
198 r_str
[buf
->data
[buf
->n_fetch
- 1] & 7]);
199 buf
->n_used
= buf
->n_fetch
;
204 prt_cc (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
206 info
->fprintf_func (info
->stream
, "%s%s", txt
,
207 cc_str
[(buf
->data
[0] >> 3) & 7]);
208 buf
->n_used
= buf
->n_fetch
;
213 pop_rr (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
215 static char *rr_stack
[] = { "bc","de","hl","af"};
217 info
->fprintf_func (info
->stream
, "%s %s", txt
,
218 rr_stack
[(buf
->data
[0] >> 4) & 3]);
219 buf
->n_used
= buf
->n_fetch
;
225 jp_cc_nn (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
229 snprintf (mytxt
,TXTSIZ
,
230 "%s%s,0x%%04x", txt
, cc_str
[(buf
->data
[0] >> 3) & 7]);
231 return prt_nn (buf
, info
, mytxt
);
235 arit_n (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
239 snprintf (mytxt
,TXTSIZ
, txt
, arit_str
[(buf
->data
[0] >> 3) & 7]);
240 return prt_n (buf
, info
, mytxt
);
244 rst (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
246 info
->fprintf_func (info
->stream
, txt
, buf
->data
[0] & 0x38);
247 buf
->n_used
= buf
->n_fetch
;
253 cis (struct buffer
*buf
, disassemble_info
* info
, char *txt ATTRIBUTE_UNUSED
)
255 static char * opar
[] = { "ld", "cp", "in", "out" };
260 op
= ((0x13 & c
) == 0x13) ? "ot" : (opar
[c
& 3]);
261 info
->fprintf_func (info
->stream
,
263 (c
& 0x08) ? 'd' : 'i',
264 (c
& 0x10) ? "r" : "");
270 dump (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
274 info
->fprintf_func (info
->stream
, "defb ");
275 for (i
= 0; txt
[i
]; ++i
)
276 info
->fprintf_func (info
->stream
, i
? ", 0x%02x" : "0x%02x",
277 (unsigned char) buf
->data
[i
]);
282 /* Table to disassemble machine codes with prefix 0xED. */
283 struct tab_elt opc_ed
[] =
285 { 0x70, 0xFF, prt
, "in f,(c)" },
286 { 0x70, 0xFF, dump
, "xx" },
287 { 0x40, 0xC7, prt_r
, "in %s,(c)" },
288 { 0x71, 0xFF, prt
, "out (c),0" },
289 { 0x70, 0xFF, dump
, "xx" },
290 { 0x41, 0xC7, prt_r
, "out (c),%s" },
291 { 0x42, 0xCF, prt_rr
, "sbc hl," },
292 { 0x43, 0xCF, prt_rr_nn
, "ld (0x%%04x),%s" },
293 { 0x44, 0xFF, prt
, "neg" },
294 { 0x45, 0xFF, prt
, "retn" },
295 { 0x46, 0xFF, prt
, "im 0" },
296 { 0x47, 0xFF, prt
, "ld i,a" },
297 { 0x4A, 0xCF, prt_rr
, "adc hl," },
298 { 0x4B, 0xCF, prt_rr_nn
, "ld %s,(0x%%04x)" },
299 { 0x4D, 0xFF, prt
, "reti" },
300 { 0x56, 0xFF, prt
, "im 1" },
301 { 0x57, 0xFF, prt
, "ld a,i" },
302 { 0x5E, 0xFF, prt
, "im 2" },
303 { 0x67, 0xFF, prt
, "rrd" },
304 { 0x6F, 0xFF, prt
, "rld" },
305 { 0xA0, 0xE4, cis
, "" },
306 { 0xC3, 0xFF, prt
, "muluw hl,bc" },
307 { 0xC5, 0xE7, prt_r
, "mulub a,%s" },
308 { 0xF3, 0xFF, prt
, "muluw hl,sp" },
309 { 0x00, 0x00, dump
, "xx" }
313 pref_ed (struct buffer
* buf
, disassemble_info
* info
,
314 char* txt ATTRIBUTE_UNUSED
)
318 if (fetch_data(buf
, info
, 1))
320 for (p
= opc_ed
; p
->val
!= (buf
->data
[1] & p
->mask
); ++p
)
322 p
->fp (buf
, info
, p
->text
);
330 /* Instruction names for the instructions addressing single bits. */
331 static char *cb1_str
[] = { "", "bit", "res", "set"};
332 /* Instruction names for shifts and rotates. */
333 static char *cb2_str
[] =
335 "rlc", "rrc", "rl", "rr", "sla", "sra", "sli", "srl"
339 pref_cb (struct buffer
* buf
, disassemble_info
* info
,
340 char* txt ATTRIBUTE_UNUSED
)
342 if (fetch_data (buf
, info
, 1))
345 if ((buf
->data
[1] & 0xc0) == 0)
346 info
->fprintf_func (info
->stream
, "%s %s",
347 cb2_str
[(buf
->data
[1] >> 3) & 7],
348 r_str
[buf
->data
[1] & 7]);
350 info
->fprintf_func (info
->stream
, "%s %d,%s",
351 cb1_str
[(buf
->data
[1] >> 6) & 3],
352 (buf
->data
[1] >> 3) & 7,
353 r_str
[buf
->data
[1] & 7]);
362 addvv (struct buffer
* buf
, disassemble_info
* info
, char* txt
)
364 info
->fprintf_func (info
->stream
, "add %s,%s", txt
, txt
);
366 return buf
->n_used
= buf
->n_fetch
;
370 ld_v_v (struct buffer
* buf
, disassemble_info
* info
, char* txt
)
374 snprintf (mytxt
, TXTSIZ
, "ld %s%%s,%s%%s", txt
, txt
);
375 return ld_r_r (buf
, info
, mytxt
);
379 prt_d (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
384 p
= buf
->data
+ buf
->n_fetch
;
386 if (fetch_data (buf
, info
, 1))
389 info
->fprintf_func (info
->stream
, txt
, d
);
390 buf
->n_used
= buf
->n_fetch
;
399 prt_d_n (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
405 p
= buf
->data
+ buf
->n_fetch
;
407 if (fetch_data (buf
, info
, 1))
410 snprintf (mytxt
, TXTSIZ
, txt
, d
);
411 return prt_n (buf
, info
, mytxt
);
420 arit_d (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
425 c
= buf
->data
[buf
->n_fetch
- 1];
426 snprintf (mytxt
, TXTSIZ
, txt
, arit_str
[(c
>> 3) & 7]);
427 return prt_d (buf
, info
, mytxt
);
431 ld_r_d (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
436 c
= buf
->data
[buf
->n_fetch
- 1];
437 snprintf (mytxt
, TXTSIZ
, txt
, r_str
[(c
>> 3) & 7]);
438 return prt_d (buf
, info
, mytxt
);
442 ld_d_r(struct buffer
*buf
, disassemble_info
* info
, char *txt
)
447 c
= buf
->data
[buf
->n_fetch
- 1];
448 snprintf (mytxt
, TXTSIZ
, txt
, r_str
[c
& 7]);
449 return prt_d (buf
, info
, mytxt
);
453 pref_xd_cb (struct buffer
* buf
, disassemble_info
* info
, char* txt
)
455 if (fetch_data (buf
, info
, 2))
465 if (((p
[3] & 0xC0) == 0x40) || ((p
[3] & 7) == 0x06))
466 snprintf (arg
, TXTSIZ
, "(%s%+d)", txt
, d
);
468 snprintf (arg
, TXTSIZ
, "(%s%+d),%s", txt
, d
, r_str
[p
[3] & 7]);
470 if ((p
[3] & 0xc0) == 0)
471 info
->fprintf_func (info
->stream
, "%s %s",
472 cb2_str
[(buf
->data
[3] >> 3) & 7],
475 info
->fprintf_func (info
->stream
, "%s %d,%s",
476 cb1_str
[(buf
->data
[3] >> 6) & 3],
477 (buf
->data
[3] >> 3) & 7,
486 /* Table to disassemble machine codes with prefix 0xDD or 0xFD. */
487 static struct tab_elt opc_ind
[] =
489 { 0x24, 0xF7, prt_r
, "inc %s%%s" },
490 { 0x25, 0xF7, prt_r
, "dec %s%%s" },
491 { 0x26, 0xF7, ld_r_n
, "ld %s%%s,0x%%%%02x" },
492 { 0x21, 0xFF, prt_nn
, "ld %s,0x%%04x" },
493 { 0x22, 0xFF, prt_nn
, "ld (0x%%04x),%s" },
494 { 0x2A, 0xFF, prt_nn
, "ld %s,(0x%%04x)" },
495 { 0x23, 0xFF, prt
, "inc %s" },
496 { 0x2B, 0xFF, prt
, "dec %s" },
497 { 0x29, 0xFF, addvv
, "%s" },
498 { 0x09, 0xCF, prt_rr
, "add %s," },
499 { 0x34, 0xFF, prt_d
, "inc (%s%%+d)" },
500 { 0x35, 0xFF, prt_d
, "dec (%s%%+d)" },
501 { 0x36, 0xFF, prt_d_n
, "ld (%s%%+d),0x%%%%02x" },
503 { 0x76, 0xFF, dump
, "h" },
504 { 0x46, 0xC7, ld_r_d
, "ld %%s,(%s%%%%+d)" },
505 { 0x70, 0xF8, ld_d_r
, "ld (%s%%%%+d),%%s" },
506 { 0x64, 0xF6, ld_v_v
, "%s" },
507 { 0x60, 0xF0, ld_r_r
, "ld %s%%s,%%s" },
508 { 0x44, 0xC6, ld_r_r
, "ld %%s,%s%%s" },
510 { 0x86, 0xC7, arit_d
, "%%s(%s%%%%+d)" },
511 { 0x84, 0xC6, arit_r
, "%%s%s%%s" },
513 { 0xE1, 0xFF, prt
, "pop %s" },
514 { 0xE5, 0xFF, prt
, "push %s" },
515 { 0xCB, 0xFF, pref_xd_cb
, "%s" },
516 { 0xE3, 0xFF, prt
, "ex (sp),%s" },
517 { 0xE9, 0xFF, prt
, "jp (%s)" },
518 { 0xF9, 0xFF, prt
, "ld sp,%s" },
519 { 0x00, 0x00, dump
, "?" },
523 pref_ind (struct buffer
* buf
, disassemble_info
* info
, char* txt
)
525 if (fetch_data (buf
, info
, 1))
530 for (p
= opc_ind
; p
->val
!= (buf
->data
[1] & p
->mask
); ++p
)
532 snprintf (mytxt
, TXTSIZ
, p
->text
, txt
);
533 p
->fp (buf
, info
, mytxt
);
541 /* Table to disassemble machine codes without prefix. */
542 static struct tab_elt opc_main
[] =
544 { 0x00, 0xFF, prt
, "nop" },
545 { 0x01, 0xCF, prt_rr_nn
, "ld %s,0x%%04x" },
546 { 0x02, 0xFF, prt
, "ld (bc),a" },
547 { 0x03, 0xCF, prt_rr
, "inc " },
548 { 0x04, 0xC7, prt_r
, "inc %s" },
549 { 0x05, 0xC7, prt_r
, "dec %s" },
550 { 0x06, 0xC7, ld_r_n
, "ld %s,0x%%02x" },
551 { 0x07, 0xFF, prt
, "rlca" },
552 { 0x08, 0xFF, prt
, "ex af,af'" },
553 { 0x09, 0xCF, prt_rr
, "add hl," },
554 { 0x0A, 0xFF, prt
, "ld a,(bc)" },
555 { 0x0B, 0xCF, prt_rr
, "dec " },
556 { 0x0F, 0xFF, prt
, "rrca" },
557 { 0x10, 0xFF, prt_e
, "djnz " },
558 { 0x12, 0xFF, prt
, "ld (de),a" },
559 { 0x17, 0xFF, prt
, "rla" },
560 { 0x18, 0xFF, prt_e
, "jr "},
561 { 0x1A, 0xFF, prt
, "ld a,(de)" },
562 { 0x1F, 0xFF, prt
, "rra" },
563 { 0x20, 0xE7, jr_cc
, "jr %s,"},
564 { 0x22, 0xFF, prt_nn
, "ld (0x%04x),hl" },
565 { 0x27, 0xFF, prt
, "daa"},
566 { 0x2A, 0xFF, prt_nn
, "ld hl,(0x%04x)" },
567 { 0x2F, 0xFF, prt
, "cpl" },
568 { 0x32, 0xFF, prt_nn
, "ld (0x%04x),a" },
569 { 0x37, 0xFF, prt
, "scf" },
570 { 0x3A, 0xFF, prt_nn
, "ld a,(0x%04x)" },
571 { 0x3F, 0xFF, prt
, "ccf" },
573 { 0x76, 0xFF, prt
, "halt" },
574 { 0x40, 0xC0, ld_r_r
, "ld %s,%s"},
576 { 0x80, 0xC0, arit_r
, "%s%s" },
578 { 0xC0, 0xC7, prt_cc
, "ret " },
579 { 0xC1, 0xCF, pop_rr
, "pop" },
580 { 0xC2, 0xC7, jp_cc_nn
, "jp " },
581 { 0xC3, 0xFF, prt_nn
, "jp 0x%04x" },
582 { 0xC4, 0xC7, jp_cc_nn
, "call " },
583 { 0xC5, 0xCF, pop_rr
, "push" },
584 { 0xC6, 0xC7, arit_n
, "%s0x%%02x" },
585 { 0xC7, 0xC7, rst
, "rst 0x%02x" },
586 { 0xC9, 0xFF, prt
, "ret" },
587 { 0xCB, 0xFF, pref_cb
, "" },
588 { 0xCD, 0xFF, prt_nn
, "call 0x%04x" },
589 { 0xD3, 0xFF, prt_n
, "out (0x%02x),a" },
590 { 0xD9, 0xFF, prt
, "exx" },
591 { 0xDB, 0xFF, prt_n
, "in a,(0x%02x)" },
592 { 0xDD, 0xFF, pref_ind
, "ix" },
593 { 0xE3, 0xFF, prt
, "ex (sp),hl" },
594 { 0xE9, 0xFF, prt
, "jp (hl)" },
595 { 0xEB, 0xFF, prt
, "ex de,hl" },
596 { 0xED, 0xFF, pref_ed
, ""},
597 { 0xF3, 0xFF, prt
, "di" },
598 { 0xF9, 0xFF, prt
, "ld sp,hl" },
599 { 0xFB, 0xFF, prt
, "ei" },
600 { 0xFD, 0xFF, pref_ind
, "iy" },
601 { 0x00, 0x00, prt
, "????" },
605 print_insn_z80 (bfd_vma addr
, disassemble_info
* info
)
614 if (! fetch_data (& buf
, info
, 1))
617 for (p
= opc_main
; p
->val
!= (buf
.data
[0] & p
->mask
); ++p
)
619 p
->fp (& buf
, info
, p
->text
);