1 /* Print Z80 and R800 instructions
2 Copyright (C) 2005-2019 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. */
23 #include "disassemble.h"
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
)
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
);
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
;
148 prt_n (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
153 p
= (unsigned char*) buf
->data
+ buf
->n_fetch
;
155 if (fetch_data (buf
, info
, 1))
158 info
->fprintf_func (info
->stream
, txt
, n
);
159 buf
->n_used
= buf
->n_fetch
;
168 ld_r_n (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
172 snprintf (mytxt
, TXTSIZ
, txt
, r_str
[(buf
->data
[0] >> 3) & 7]);
173 return prt_n (buf
, info
, mytxt
);
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
;
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
;
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
;
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
;
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
;
227 jp_cc_nn (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
231 snprintf (mytxt
,TXTSIZ
,
232 "%s%s,0x%%04x", txt
, cc_str
[(buf
->data
[0] >> 3) & 7]);
233 return prt_nn (buf
, info
, mytxt
);
237 arit_n (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
241 snprintf (mytxt
,TXTSIZ
, txt
, arit_str
[(buf
->data
[0] >> 3) & 7]);
242 return prt_n (buf
, info
, mytxt
);
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
;
255 cis (struct buffer
*buf
, disassemble_info
* info
, char *txt ATTRIBUTE_UNUSED
)
257 static char * opar
[] = { "ld", "cp", "in", "out" };
262 op
= ((0x13 & c
) == 0x13) ? "ot" : (opar
[c
& 3]);
263 info
->fprintf_func (info
->stream
,
265 (c
& 0x08) ? 'd' : 'i',
266 (c
& 0x10) ? "r" : "");
272 dump (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
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
]);
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" }
317 pref_ed (struct buffer
* buf
, disassemble_info
* info
,
318 char* txt ATTRIBUTE_UNUSED
)
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
);
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"
343 pref_cb (struct buffer
* buf
, disassemble_info
* info
,
344 char* txt ATTRIBUTE_UNUSED
)
346 if (fetch_data (buf
, info
, 1))
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]);
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]);
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
;
374 ld_v_v (struct buffer
* buf
, disassemble_info
* info
, char* txt
)
378 snprintf (mytxt
, TXTSIZ
, "ld %s%%s,%s%%s", txt
, txt
);
379 return ld_r_r (buf
, info
, mytxt
);
383 prt_d (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
388 p
= buf
->data
+ buf
->n_fetch
;
390 if (fetch_data (buf
, info
, 1))
393 info
->fprintf_func (info
->stream
, txt
, d
);
394 buf
->n_used
= buf
->n_fetch
;
403 prt_d_n (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
409 p
= buf
->data
+ buf
->n_fetch
;
411 if (fetch_data (buf
, info
, 1))
414 snprintf (mytxt
, TXTSIZ
, txt
, d
);
415 return prt_n (buf
, info
, mytxt
);
424 arit_d (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
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
);
435 ld_r_d (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
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
);
446 ld_d_r(struct buffer
*buf
, disassemble_info
* info
, char *txt
)
451 c
= buf
->data
[buf
->n_fetch
- 1];
452 snprintf (mytxt
, TXTSIZ
, txt
, r_str
[c
& 7]);
453 return prt_d (buf
, info
, mytxt
);
457 pref_xd_cb (struct buffer
* buf
, disassemble_info
* info
, char* txt
)
459 if (fetch_data (buf
, info
, 2))
469 if (((p
[3] & 0xC0) == 0x40) || ((p
[3] & 7) == 0x06))
470 snprintf (arg
, TXTSIZ
, "(%s%+d)", txt
, d
);
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],
479 info
->fprintf_func (info
->stream
, "%s %d,%s",
480 cb1_str
[(buf
->data
[3] >> 6) & 3],
481 (buf
->data
[3] >> 3) & 7,
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
, "?" },
527 pref_ind (struct buffer
* buf
, disassemble_info
* info
, char* txt
)
529 if (fetch_data (buf
, info
, 1))
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
);
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
)
618 if (! fetch_data (& buf
, info
, 1))
621 for (p
= opc_main
; p
->val
!= (buf
.data
[0] & p
->mask
); ++p
)
623 p
->fp (& buf
, info
, p
->text
);
This page took 0.058828 seconds and 4 git commands to generate.