1 /* Print Z80 and R800 instructions
2 Copyright 2005 Free Software Foundation, Inc.
3 Contributed by Arnold Metselaar <arnold_m@operamail.com>
7 This file 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 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public 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. */
33 typedef int (*func
)(struct buffer
*, disassemble_info
*, const char *);
44 /* Names of 16-bit registers. */
45 static const char * rr_str
[] = { "bc", "de", "hl", "sp" };
46 /* Names of 8-bit registers. */
47 static const char * r_str
[] = { "b", "c", "d", "e", "h", "l", "(hl)", "a" };
48 /* Texts for condition codes. */
49 static const char * cc_str
[] = { "nz", "z", "nc", "c", "po", "pe", "p", "m" };
50 /* Instruction names for 8-bit arithmetic, operand "a" is often implicit */
51 static const char * arit_str
[] =
53 "add a,", "adc a,", "sub ", "sbc a,", "and ", "xor ", "or ", "cp "
57 fetch_data (struct buffer
*buf
, disassemble_info
* info
, int n
)
61 if (buf
->n_fetch
+ n
> 4)
64 r
= info
->read_memory_func (buf
->base
+ buf
->n_fetch
,
65 (unsigned char*) buf
->data
+ buf
->n_fetch
,
73 prt (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
75 info
->fprintf_func (info
->stream
, "%s", txt
);
76 buf
->n_used
= buf
->n_fetch
;
81 prt_e (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
86 if (fetch_data (buf
, info
, 1))
89 target_addr
= (buf
->base
+ 2 + e
) & 0xffff;
90 buf
->n_used
= buf
->n_fetch
;
91 info
->fprintf_func (info
->stream
, "%s0x%04x", txt
, target_addr
);
100 jr_cc (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
104 snprintf (mytxt
, TXTSIZ
, txt
, cc_str
[(buf
->data
[0] >> 3) & 3]);
105 return prt_e (buf
, info
, mytxt
);
109 prt_nn (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
114 p
= (unsigned char*) buf
->data
+ buf
->n_fetch
;
115 if (fetch_data (buf
, info
, 2))
117 nn
= p
[0] + (p
[1] << 8);
118 info
->fprintf_func (info
->stream
, txt
, nn
);
119 buf
->n_used
= buf
->n_fetch
;
127 prt_rr_nn (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
131 snprintf (mytxt
, TXTSIZ
, txt
, rr_str
[(buf
->data
[0] >> 4) & 3]);
132 return prt_nn (buf
, info
, mytxt
);
136 prt_rr (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
138 info
->fprintf_func (info
->stream
, "%s%s", txt
,
139 rr_str
[(buf
->data
[buf
->n_fetch
- 1] >> 4) & 3]);
140 buf
->n_used
= buf
->n_fetch
;
145 prt_n (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
150 p
= (unsigned char*) buf
->data
+ buf
->n_fetch
;
152 if (fetch_data (buf
, info
, 1))
155 info
->fprintf_func (info
->stream
, txt
, n
);
156 buf
->n_used
= buf
->n_fetch
;
165 ld_r_n (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
169 snprintf (mytxt
, TXTSIZ
, txt
, r_str
[(buf
->data
[0] >> 3) & 7]);
170 return prt_n (buf
, info
, mytxt
);
174 prt_r (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
176 info
->fprintf_func (info
->stream
, txt
,
177 r_str
[(buf
->data
[buf
->n_fetch
- 1] >> 3) & 7]);
178 buf
->n_used
= buf
->n_fetch
;
183 ld_r_r (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
185 info
->fprintf_func (info
->stream
, txt
,
186 r_str
[(buf
->data
[buf
->n_fetch
- 1] >> 3) & 7],
187 r_str
[buf
->data
[buf
->n_fetch
- 1] & 7]);
188 buf
->n_used
= buf
->n_fetch
;
193 arit_r (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
195 info
->fprintf_func (info
->stream
, txt
,
196 arit_str
[(buf
->data
[buf
->n_fetch
- 1] >> 3) & 7],
197 r_str
[buf
->data
[buf
->n_fetch
- 1] & 7]);
198 buf
->n_used
= buf
->n_fetch
;
203 prt_cc (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
205 info
->fprintf_func (info
->stream
, "%s%s", txt
,
206 cc_str
[(buf
->data
[0] >> 3) & 7]);
207 buf
->n_used
= buf
->n_fetch
;
212 pop_rr (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
214 static const char *rr_stack
[] = { "bc","de","hl","af"};
216 info
->fprintf_func (info
->stream
, "%s %s", txt
,
217 rr_stack
[(buf
->data
[0] >> 4) & 3]);
218 buf
->n_used
= buf
->n_fetch
;
224 jp_cc_nn (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
228 snprintf (mytxt
,TXTSIZ
,
229 "%s%s,0x%%04x", txt
, cc_str
[(buf
->data
[0] >> 3) & 7]);
230 return prt_nn (buf
, info
, mytxt
);
234 arit_n (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
238 snprintf (mytxt
,TXTSIZ
, txt
, arit_str
[(buf
->data
[0] >> 3) & 7]);
239 return prt_n (buf
, info
, mytxt
);
243 rst (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
245 info
->fprintf_func (info
->stream
, txt
, buf
->data
[0] & 0x38);
246 buf
->n_used
= buf
->n_fetch
;
252 cis (struct buffer
*buf
, disassemble_info
* info
, const char *txt ATTRIBUTE_UNUSED
)
254 static const char * opar
[] = { "ld", "cp", "in", "out" };
259 op
= ((0x13 & c
) == 0x13) ? "ot" : (opar
[c
& 3]);
260 info
->fprintf_func (info
->stream
,
262 (c
& 0x08) ? 'd' : 'i',
263 (c
& 0x10) ? "r" : "");
269 dump (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
273 info
->fprintf_func (info
->stream
, "defb ");
274 for (i
= 0; txt
[i
]; ++i
)
275 info
->fprintf_func (info
->stream
, i
? ", 0x%02x" : "0x%02x",
276 (unsigned char) buf
->data
[i
]);
281 /* Table to disassemble machine codes with prefix 0xED. */
282 struct tab_elt opc_ed
[] =
284 { 0x70, 0xFF, prt
, "in f,(c)" },
285 { 0x70, 0xFF, dump
, "xx" },
286 { 0x40, 0xC7, prt_r
, "in %s,(c)" },
287 { 0x71, 0xFF, prt
, "out (c),0" },
288 { 0x70, 0xFF, dump
, "xx" },
289 { 0x41, 0xC7, prt_r
, "out (c),%s" },
290 { 0x42, 0xCF, prt_rr
, "sbc hl," },
291 { 0x43, 0xCF, prt_rr_nn
, "ld (0x%%04x),%s" },
292 { 0x44, 0xFF, prt
, "neg" },
293 { 0x45, 0xFF, prt
, "retn" },
294 { 0x46, 0xFF, prt
, "im 0" },
295 { 0x47, 0xFF, prt
, "ld i,a" },
296 { 0x4A, 0xCF, prt_rr
, "adc hl," },
297 { 0x4B, 0xCF, prt_rr_nn
, "ld %s,(0x%%04x)" },
298 { 0x4D, 0xFF, prt
, "reti" },
299 { 0x56, 0xFF, prt
, "im 1" },
300 { 0x57, 0xFF, prt
, "ld a,i" },
301 { 0x5E, 0xFF, prt
, "im 2" },
302 { 0x67, 0xFF, prt
, "rrd" },
303 { 0x6F, 0xFF, prt
, "rld" },
304 { 0xA0, 0xE4, cis
, "" },
305 { 0xC3, 0xFF, prt
, "muluw hl,bc" },
306 { 0xC5, 0xE7, prt_r
, "mulub a,%s" },
307 { 0xF3, 0xFF, prt
, "muluw hl,sp" },
308 { 0x00, 0x00, dump
, "xx" }
312 pref_ed (struct buffer
* buf
, disassemble_info
* info
,
313 const char* txt ATTRIBUTE_UNUSED
)
317 if (fetch_data(buf
, info
, 1))
319 for (p
= opc_ed
; p
->val
!= (buf
->data
[1] & p
->mask
); ++p
)
321 p
->fp (buf
, info
, p
->text
);
329 /* Instruction names for the instructions addressing single bits. */
330 static const char *cb1_str
[] = { "", "bit", "res", "set"};
331 /* Instruction names for shifts and rotates. */
332 static const char *cb2_str
[] =
334 "rlc", "rrc", "rl", "rr", "sla", "sra", "sli", "srl"
338 pref_cb (struct buffer
* buf
, disassemble_info
* info
,
339 const char* txt ATTRIBUTE_UNUSED
)
341 if (fetch_data (buf
, info
, 1))
344 if ((buf
->data
[1] & 0xc0) == 0)
345 info
->fprintf_func (info
->stream
, "%s %s",
346 cb2_str
[(buf
->data
[1] >> 3) & 7],
347 r_str
[buf
->data
[1] & 7]);
349 info
->fprintf_func (info
->stream
, "%s %d,%s",
350 cb1_str
[(buf
->data
[1] >> 6) & 3],
351 (buf
->data
[1] >> 3) & 7,
352 r_str
[buf
->data
[1] & 7]);
361 addvv (struct buffer
* buf
, disassemble_info
* info
, const char* txt
)
363 info
->fprintf_func (info
->stream
, "add %s,%s", txt
, txt
);
365 return buf
->n_used
= buf
->n_fetch
;
369 ld_v_v (struct buffer
* buf
, disassemble_info
* info
, const char* txt
)
373 snprintf (mytxt
, TXTSIZ
, "ld %s%%s,%s%%s", txt
, txt
);
374 return ld_r_r (buf
, info
, mytxt
);
378 prt_d (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
383 p
= buf
->data
+ buf
->n_fetch
;
385 if (fetch_data (buf
, info
, 1))
388 info
->fprintf_func (info
->stream
, txt
, d
);
389 buf
->n_used
= buf
->n_fetch
;
398 prt_d_n (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
404 p
= buf
->data
+ buf
->n_fetch
;
406 if (fetch_data (buf
, info
, 1))
409 snprintf (mytxt
, TXTSIZ
, txt
, d
);
410 return prt_n (buf
, info
, mytxt
);
419 arit_d (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
424 c
= buf
->data
[buf
->n_fetch
- 1];
425 snprintf (mytxt
, TXTSIZ
, txt
, arit_str
[(c
>> 3) & 7]);
426 return prt_d (buf
, info
, mytxt
);
430 ld_r_d (struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
435 c
= buf
->data
[buf
->n_fetch
- 1];
436 snprintf (mytxt
, TXTSIZ
, txt
, r_str
[(c
>> 3) & 7]);
437 return prt_d (buf
, info
, mytxt
);
441 ld_d_r(struct buffer
*buf
, disassemble_info
* info
, const char *txt
)
446 c
= buf
->data
[buf
->n_fetch
- 1];
447 snprintf (mytxt
, TXTSIZ
, txt
, r_str
[c
& 7]);
448 return prt_d (buf
, info
, mytxt
);
452 pref_xd_cb (struct buffer
* buf
, disassemble_info
* info
, const char* txt
)
454 if (fetch_data (buf
, info
, 2))
464 if (((p
[3] & 0xC0) == 0x40) || ((p
[3] & 7) == 0x06))
465 snprintf (arg
, TXTSIZ
, "(%s%+d)", txt
, d
);
467 snprintf (arg
, TXTSIZ
, "(%s%+d),%s", txt
, d
, r_str
[p
[3] & 7]);
469 if ((p
[3] & 0xc0) == 0)
470 info
->fprintf_func (info
->stream
, "%s %s",
471 cb2_str
[(buf
->data
[3] >> 3) & 7],
474 info
->fprintf_func (info
->stream
, "%s %d,%s",
475 cb1_str
[(buf
->data
[3] >> 6) & 3],
476 (buf
->data
[3] >> 3) & 7,
485 /* Table to disassemble machine codes with prefix 0xDD or 0xFD. */
486 static struct tab_elt opc_ind
[] =
488 { 0x24, 0xF7, prt_r
, "inc %s%%s" },
489 { 0x25, 0xF7, prt_r
, "dec %s%%s" },
490 { 0x26, 0xF7, ld_r_n
, "ld %s%%s,0x%%%%02x" },
491 { 0x21, 0xFF, prt_nn
, "ld %s,0x%%04x" },
492 { 0x22, 0xFF, prt_nn
, "ld (0x%%04x),%s" },
493 { 0x2A, 0xFF, prt_nn
, "ld %s,(0x%%04x)" },
494 { 0x23, 0xFF, prt
, "inc %s" },
495 { 0x2B, 0xFF, prt
, "dec %s" },
496 { 0x29, 0xFF, addvv
, "%s" },
497 { 0x09, 0xCF, prt_rr
, "add %s," },
498 { 0x34, 0xFF, prt_d
, "inc (%s%%+d)" },
499 { 0x35, 0xFF, prt_d
, "dec (%s%%+d)" },
500 { 0x36, 0xFF, prt_d_n
, "ld (%s%%+d),0x%%%%02x" },
502 { 0x76, 0xFF, dump
, "h" },
503 { 0x46, 0xC7, ld_r_d
, "ld %%s,(%s%%%%+d)" },
504 { 0x70, 0xF8, ld_d_r
, "ld (%s%%%%+d),%%s" },
505 { 0x64, 0xF6, ld_v_v
, "%s" },
506 { 0x60, 0xF0, ld_r_r
, "ld %s%%s,%%s" },
507 { 0x44, 0xC6, ld_r_r
, "ld %%s,%s%%s" },
509 { 0x86, 0xC7, arit_d
, "%%s(%s%%%%+d)" },
510 { 0x84, 0xC6, arit_r
, "%%s%s%%s" },
512 { 0xE1, 0xFF, prt
, "pop %s" },
513 { 0xE5, 0xFF, prt
, "push %s" },
514 { 0xCB, 0xFF, pref_xd_cb
, "%s" },
515 { 0xE3, 0xFF, prt
, "ex (sp),%s" },
516 { 0xE9, 0xFF, prt
, "jp (%s)" },
517 { 0xF9, 0xFF, prt
, "ld sp,%s" },
518 { 0x00, 0x00, dump
, "?" },
522 pref_ind (struct buffer
* buf
, disassemble_info
* info
, const char* txt
)
524 if (fetch_data (buf
, info
, 1))
529 for (p
= opc_ind
; p
->val
!= (buf
->data
[1] & p
->mask
); ++p
)
531 snprintf (mytxt
, TXTSIZ
, p
->text
, txt
);
532 p
->fp (buf
, info
, mytxt
);
540 /* Table to disassemble machine codes without prefix. */
541 static struct tab_elt opc_main
[] =
543 { 0x00, 0xFF, prt
, "nop" },
544 { 0x01, 0xCF, prt_rr_nn
, "ld %s,0x%%04x" },
545 { 0x02, 0xFF, prt
, "ld (bc),a" },
546 { 0x03, 0xCF, prt_rr
, "inc " },
547 { 0x04, 0xC7, prt_r
, "inc %s" },
548 { 0x05, 0xC7, prt_r
, "dec %s" },
549 { 0x06, 0xC7, ld_r_n
, "ld %s,0x%%02x" },
550 { 0x07, 0xFF, prt
, "rlca" },
551 { 0x08, 0xFF, prt
, "ex af,af'" },
552 { 0x09, 0xCF, prt_rr
, "add hl," },
553 { 0x0A, 0xFF, prt
, "ld a,(bc)" },
554 { 0x0B, 0xCF, prt_rr
, "dec " },
555 { 0x0F, 0xFF, prt
, "rrca" },
556 { 0x10, 0xFF, prt_e
, "djnz " },
557 { 0x12, 0xFF, prt
, "ld (de),a" },
558 { 0x17, 0xFF, prt
, "rla" },
559 { 0x18, 0xFF, prt_e
, "jr "},
560 { 0x1A, 0xFF, prt
, "ld a,(de)" },
561 { 0x1F, 0xFF, prt
, "rra" },
562 { 0x20, 0xE7, jr_cc
, "jr %s,"},
563 { 0x22, 0xFF, prt_nn
, "ld (0x%04x),hl" },
564 { 0x27, 0xFF, prt
, "daa"},
565 { 0x2A, 0xFF, prt_nn
, "ld hl,(0x%04x)" },
566 { 0x2F, 0xFF, prt
, "cpl" },
567 { 0x32, 0xFF, prt_nn
, "ld (0x%04x),a" },
568 { 0x37, 0xFF, prt
, "scf" },
569 { 0x3A, 0xFF, prt_nn
, "ld a,(0x%04x)" },
570 { 0x3F, 0xFF, prt
, "ccf" },
572 { 0x76, 0xFF, prt
, "halt" },
573 { 0x40, 0xC0, ld_r_r
, "ld %s,%s"},
575 { 0x80, 0xC0, arit_r
, "%s%s" },
577 { 0xC0, 0xC7, prt_cc
, "ret " },
578 { 0xC1, 0xCF, pop_rr
, "pop" },
579 { 0xC2, 0xC7, jp_cc_nn
, "jp " },
580 { 0xC3, 0xFF, prt_nn
, "jp 0x%04x" },
581 { 0xC4, 0xC7, jp_cc_nn
, "call " },
582 { 0xC5, 0xCF, pop_rr
, "push" },
583 { 0xC6, 0xC7, arit_n
, "%s0x%%02x" },
584 { 0xC7, 0xC7, rst
, "rst 0x%02x" },
585 { 0xC9, 0xFF, prt
, "ret" },
586 { 0xCB, 0xFF, pref_cb
, "" },
587 { 0xCD, 0xFF, prt_nn
, "call 0x%04x" },
588 { 0xD3, 0xFF, prt_n
, "out (0x%02x),a" },
589 { 0xD9, 0xFF, prt
, "exx" },
590 { 0xDB, 0xFF, prt_n
, "in a,(0x%02x)" },
591 { 0xDD, 0xFF, pref_ind
, "ix" },
592 { 0xE3, 0xFF, prt
, "ex (sp),hl" },
593 { 0xE9, 0xFF, prt
, "jp (hl)" },
594 { 0xEB, 0xFF, prt
, "ex de,hl" },
595 { 0xED, 0xFF, pref_ed
, ""},
596 { 0xF3, 0xFF, prt
, "di" },
597 { 0xF9, 0xFF, prt
, "ld sp,hl" },
598 { 0xFB, 0xFF, prt
, "ei" },
599 { 0xFD, 0xFF, pref_ind
, "iy" },
600 { 0x00, 0x00, prt
, "????" },
604 print_insn_z80 (bfd_vma addr
, disassemble_info
* info
)
613 if (! fetch_data (& buf
, info
, 1))
616 for (p
= opc_main
; p
->val
!= (buf
.data
[0] & p
->mask
); ++p
)
618 p
->fp (& buf
, info
, p
->text
);