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. */
32 typedef int (*func
)(struct buffer
*, disassemble_info
*, char *);
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 "
56 fetch_data (struct buffer
*buf
, disassemble_info
* info
, int n
)
60 if (buf
->n_fetch
+ n
> 4)
63 r
= info
->read_memory_func (buf
->base
+ buf
->n_fetch
,
64 buf
->data
+ buf
->n_fetch
,
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
;
80 prt_e (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
85 if (fetch_data (buf
, info
, 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
);
99 jr_cc (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
103 snprintf (mytxt
, TXTSIZ
, txt
, cc_str
[(buf
->data
[0] >> 3) & 3]);
104 return prt_e (buf
, info
, mytxt
);
108 prt_nn (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
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
;
126 prt_rr_nn (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
130 snprintf (mytxt
, TXTSIZ
, txt
, rr_str
[(buf
->data
[0] >> 4) & 3]);
131 return prt_nn (buf
, info
, mytxt
);
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
;
144 prt_n (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
149 p
= (unsigned char*) buf
->data
+ buf
->n_fetch
;
151 if (fetch_data (buf
, info
, 1))
154 info
->fprintf_func (info
->stream
, txt
, n
);
155 buf
->n_used
= buf
->n_fetch
;
164 ld_r_n (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
168 snprintf (mytxt
, TXTSIZ
, txt
, r_str
[(buf
->data
[0] >> 3) & 7]);
169 return prt_n (buf
, info
, mytxt
);
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
;
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
;
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
;
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
;
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
;
223 jp_cc_nn (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
227 snprintf (mytxt
,TXTSIZ
,
228 "%s%s,0x%%04x", txt
, cc_str
[(buf
->data
[0] >> 3) & 7]);
229 return prt_nn (buf
, info
, mytxt
);
233 arit_n (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
237 snprintf (mytxt
,TXTSIZ
, txt
, arit_str
[(buf
->data
[0] >> 3) & 7]);
238 return prt_n (buf
, info
, mytxt
);
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
;
251 cis (struct buffer
*buf
, disassemble_info
* info
, char *txt ATTRIBUTE_UNUSED
)
253 static char * opar
[] = { "ld", "cp", "in", "out" };
258 op
= ((0x13 & c
) == 0x13) ? "ot" : (opar
[c
& 3]);
259 info
->fprintf_func (info
->stream
,
261 (c
& 0x08) ? 'd' : 'i',
262 (c
& 0x10) ? "r" : "");
268 dump (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
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
]);
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" }
311 pref_ed (struct buffer
* buf
, disassemble_info
* info
,
312 char* txt ATTRIBUTE_UNUSED
)
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
);
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"
337 pref_cb (struct buffer
* buf
, disassemble_info
* info
,
338 char* txt ATTRIBUTE_UNUSED
)
340 if (fetch_data (buf
, info
, 1))
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]);
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]);
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
;
368 ld_v_v (struct buffer
* buf
, disassemble_info
* info
, char* txt
)
372 snprintf (mytxt
, TXTSIZ
, "ld %s%%s,%s%%s", txt
, txt
);
373 return ld_r_r (buf
, info
, mytxt
);
377 prt_d (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
382 p
= (unsigned char*) buf
->data
+ buf
->n_fetch
;
384 if (fetch_data (buf
, info
, 1))
387 info
->fprintf_func (info
->stream
, txt
, d
);
388 buf
->n_used
= buf
->n_fetch
;
397 prt_d_n (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
403 p
= (unsigned char*) buf
->data
+ buf
->n_fetch
;
405 if (fetch_data (buf
, info
, 1))
408 snprintf (mytxt
, TXTSIZ
, txt
, d
);
409 return prt_n (buf
, info
, mytxt
);
418 arit_d (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
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
);
429 ld_r_d (struct buffer
*buf
, disassemble_info
* info
, char *txt
)
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
);
440 ld_d_r(struct buffer
*buf
, disassemble_info
* info
, char *txt
)
445 c
= buf
->data
[buf
->n_fetch
- 1];
446 snprintf (mytxt
, TXTSIZ
, txt
, r_str
[c
& 7]);
447 return prt_d (buf
, info
, mytxt
);
451 pref_xd_cb (struct buffer
* buf
, disassemble_info
* info
, char* txt
)
453 if (fetch_data (buf
, info
, 2))
463 if (((p
[3] & 0xC0) == 0x40) || ((p
[3] & 7) == 0x06))
464 snprintf (arg
, TXTSIZ
, "(%s+%d)", txt
, d
);
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],
473 info
->fprintf_func (info
->stream
, "%s %d,%s",
474 cb1_str
[(buf
->data
[3] >> 6) & 3],
475 (buf
->data
[3] >> 3) & 7,
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
, "?" },
521 pref_ind (struct buffer
* buf
, disassemble_info
* info
, char* txt
)
523 if (fetch_data (buf
, info
, 1))
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
);
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
)
612 if (! fetch_data (& buf
, info
, 1))
615 for (p
= opc_main
; p
->val
!= (buf
.data
[0] & p
->mask
); ++p
)
617 p
->fp (& buf
, info
, p
->text
);