Fix warnings when compiling z80-dis.c
[qemu/z80.git] / z80-dis.c
blob0afdd483d58a3f7c95c9ff3f38b89a17ec32c8c5
1 /* Print Z80 and R800 instructions
2 Copyright 2005 Free Software Foundation, Inc.
3 Contributed by Arnold Metselaar <arnold_m@operamail.com>
5 Taken from GDB
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. */
22 #include "dis-asm.h"
23 #include <stdio.h>
25 struct buffer
27 bfd_vma base;
28 int n_fetch;
29 int n_used;
30 signed char data[4];
31 } ;
33 typedef int (*func)(struct buffer *, disassemble_info *, const char *);
35 struct tab_elt
37 unsigned char val;
38 unsigned char mask;
39 func fp;
40 const char * text;
41 } ;
43 #define TXTSIZ 24
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 "
54 } ;
56 static int
57 fetch_data (struct buffer *buf, disassemble_info * info, int n)
59 int r;
61 if (buf->n_fetch + n > 4)
62 abort ();
64 r = info->read_memory_func (buf->base + buf->n_fetch,
65 (unsigned char*) buf->data + buf->n_fetch,
66 n, info);
67 if (r == 0)
68 buf->n_fetch += n;
69 return !r;
72 static int
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;
77 return 1;
80 static int
81 prt_e (struct buffer *buf, disassemble_info * info, const char *txt)
83 char e;
84 int target_addr;
86 if (fetch_data (buf, info, 1))
88 e = buf->data[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);
93 else
94 buf->n_used = -1;
96 return buf->n_used;
99 static int
100 jr_cc (struct buffer *buf, disassemble_info * info, const char *txt)
102 char mytxt[TXTSIZ];
104 snprintf (mytxt, TXTSIZ, txt, cc_str[(buf->data[0] >> 3) & 3]);
105 return prt_e (buf, info, mytxt);
108 static int
109 prt_nn (struct buffer *buf, disassemble_info * info, const char *txt)
111 int nn;
112 unsigned char *p;
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;
121 else
122 buf->n_used = -1;
123 return buf->n_used;
126 static int
127 prt_rr_nn (struct buffer *buf, disassemble_info * info, const char *txt)
129 char mytxt[TXTSIZ];
131 snprintf (mytxt, TXTSIZ, txt, rr_str[(buf->data[0] >> 4) & 3]);
132 return prt_nn (buf, info, mytxt);
135 static int
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;
141 return buf->n_used;
144 static int
145 prt_n (struct buffer *buf, disassemble_info * info, const char *txt)
147 int n;
148 unsigned char *p;
150 p = (unsigned char*) buf->data + buf->n_fetch;
152 if (fetch_data (buf, info, 1))
154 n = p[0];
155 info->fprintf_func (info->stream, txt, n);
156 buf->n_used = buf->n_fetch;
158 else
159 buf->n_used = -1;
161 return buf->n_used;
164 static int
165 ld_r_n (struct buffer *buf, disassemble_info * info, const char *txt)
167 char mytxt[TXTSIZ];
169 snprintf (mytxt, TXTSIZ, txt, r_str[(buf->data[0] >> 3) & 7]);
170 return prt_n (buf, info, mytxt);
173 static int
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;
179 return buf->n_used;
182 static int
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;
189 return buf->n_used;
192 static int
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;
199 return buf->n_used;
202 static int
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;
208 return buf->n_used;
211 static int
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;
219 return buf->n_used;
223 static int
224 jp_cc_nn (struct buffer *buf, disassemble_info * info, const char *txt)
226 char mytxt[TXTSIZ];
228 snprintf (mytxt,TXTSIZ,
229 "%s%s,0x%%04x", txt, cc_str[(buf->data[0] >> 3) & 7]);
230 return prt_nn (buf, info, mytxt);
233 static int
234 arit_n (struct buffer *buf, disassemble_info * info, const char *txt)
236 char mytxt[TXTSIZ];
238 snprintf (mytxt,TXTSIZ, txt, arit_str[(buf->data[0] >> 3) & 7]);
239 return prt_n (buf, info, mytxt);
242 static int
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;
247 return buf->n_used;
251 static int
252 cis (struct buffer *buf, disassemble_info * info, const char *txt ATTRIBUTE_UNUSED)
254 static const char * opar[] = { "ld", "cp", "in", "out" };
255 const char * op;
256 char c;
258 c = buf->data[1];
259 op = ((0x13 & c) == 0x13) ? "ot" : (opar[c & 3]);
260 info->fprintf_func (info->stream,
261 "%s%c%s", op,
262 (c & 0x08) ? 'd' : 'i',
263 (c & 0x10) ? "r" : "");
264 buf->n_used = 2;
265 return buf->n_used;
268 static int
269 dump (struct buffer *buf, disassemble_info * info, const char *txt)
271 int i;
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]);
277 buf->n_used = i;
278 return buf->n_used;
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" }
311 static int
312 pref_ed (struct buffer * buf, disassemble_info * info,
313 const char* txt ATTRIBUTE_UNUSED)
315 struct tab_elt *p;
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);
323 else
324 buf->n_used = -1;
326 return buf->n_used;
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"
337 static int
338 pref_cb (struct buffer * buf, disassemble_info * info,
339 const char* txt ATTRIBUTE_UNUSED)
341 if (fetch_data (buf, info, 1))
343 buf->n_used = 2;
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]);
348 else
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]);
354 else
355 buf->n_used = -1;
357 return buf->n_used;
360 static int
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;
368 static int
369 ld_v_v (struct buffer * buf, disassemble_info * info, const char* txt)
371 char mytxt[TXTSIZ];
373 snprintf (mytxt, TXTSIZ, "ld %s%%s,%s%%s", txt, txt);
374 return ld_r_r (buf, info, mytxt);
377 static int
378 prt_d (struct buffer *buf, disassemble_info * info, const char *txt)
380 int d;
381 signed char *p;
383 p = buf->data + buf->n_fetch;
385 if (fetch_data (buf, info, 1))
387 d = p[0];
388 info->fprintf_func (info->stream, txt, d);
389 buf->n_used = buf->n_fetch;
391 else
392 buf->n_used = -1;
394 return buf->n_used;
397 static int
398 prt_d_n (struct buffer *buf, disassemble_info * info, const char *txt)
400 char mytxt[TXTSIZ];
401 int d;
402 signed char *p;
404 p = buf->data + buf->n_fetch;
406 if (fetch_data (buf, info, 1))
408 d = p[0];
409 snprintf (mytxt, TXTSIZ, txt, d);
410 return prt_n (buf, info, mytxt);
412 else
413 buf->n_used = -1;
415 return buf->n_used;
418 static int
419 arit_d (struct buffer *buf, disassemble_info * info, const char *txt)
421 char mytxt[TXTSIZ];
422 signed char c;
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);
429 static int
430 ld_r_d (struct buffer *buf, disassemble_info * info, const char *txt)
432 char mytxt[TXTSIZ];
433 signed char c;
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);
440 static int
441 ld_d_r(struct buffer *buf, disassemble_info * info, const char *txt)
443 char mytxt[TXTSIZ];
444 signed char c;
446 c = buf->data[buf->n_fetch - 1];
447 snprintf (mytxt, TXTSIZ, txt, r_str[c & 7]);
448 return prt_d (buf, info, mytxt);
451 static int
452 pref_xd_cb (struct buffer * buf, disassemble_info * info, const char* txt)
454 if (fetch_data (buf, info, 2))
456 int d;
457 char arg[TXTSIZ];
458 signed char *p;
460 buf->n_used = 4;
461 p = buf->data;
462 d = p[2];
464 if (((p[3] & 0xC0) == 0x40) || ((p[3] & 7) == 0x06))
465 snprintf (arg, TXTSIZ, "(%s%+d)", txt, d);
466 else
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],
472 arg);
473 else
474 info->fprintf_func (info->stream, "%s %d,%s",
475 cb1_str[(buf->data[3] >> 6) & 3],
476 (buf->data[3] >> 3) & 7,
477 arg);
479 else
480 buf->n_used = -1;
482 return buf->n_used;
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, "?" },
521 static int
522 pref_ind (struct buffer * buf, disassemble_info * info, const char* txt)
524 if (fetch_data (buf, info, 1))
526 char mytxt[TXTSIZ];
527 struct tab_elt *p;
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);
534 else
535 buf->n_used = -1;
537 return buf->n_used;
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)
606 struct buffer buf;
607 struct tab_elt *p;
609 buf.base = addr;
610 buf.n_fetch = 0;
611 buf.n_used = 0;
613 if (! fetch_data (& buf, info, 1))
614 return -1;
616 for (p = opc_main; p->val != (buf.data[0] & p->mask); ++p)
618 p->fp (& buf, info, p->text);
620 return buf.n_used;