1 /* Print Z80 and R800 instructions 2 Copyright 2005 Free Software Foundation, Inc. 3 Contributed by Arnold Metselaar <arnold_m@operamail.com> 4 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. 9 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. 14 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. */ 19 20 #include "sysdep.h" 21 #include "dis-asm.h" 22 #include <stdio.h> 23 24 struct buffer 25 { 26 bfd_vma base; 27 int n_fetch; 28 int n_used; 29 signed char data[4]; 30 } ; 31 32 typedef int (*func)(struct buffer *, disassemble_info *, char *); 33 34 struct tab_elt 35 { 36 unsigned char val; 37 unsigned char mask; 38 func fp; 39 char * text; 40 } ; 41 42 #define TXTSIZ 24 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[] = 51 { 52 "add a,", "adc a,", "sub ", "sbc a,", "and ", "xor ", "or ", "cp " 53 } ; 54 55 static int 56 fetch_data (struct buffer *buf, disassemble_info * info, int n) 57 { 58 int r; 59 60 if (buf->n_fetch + n > 4) 61 abort (); 62 63 r = info->read_memory_func (buf->base + buf->n_fetch, 64 (unsigned char*) buf->data + buf->n_fetch, 65 n, info); 66 if (r == 0) 67 buf->n_fetch += n; 68 return !r; 69 } 70 71 static int 72 prt (struct buffer *buf, disassemble_info * info, char *txt) 73 { 74 info->fprintf_func (info->stream, "%s", txt); 75 buf->n_used = buf->n_fetch; 76 return 1; 77 } 78 79 static int 80 prt_e (struct buffer *buf, disassemble_info * info, char *txt) 81 { 82 char e; 83 int target_addr; 84 85 if (fetch_data (buf, info, 1)) 86 { 87 e = buf->data[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); 91 } 92 else 93 buf->n_used = -1; 94 95 return buf->n_used; 96 } 97 98 static int 99 jr_cc (struct buffer *buf, disassemble_info * info, char *txt) 100 { 101 char mytxt[TXTSIZ]; 102 103 snprintf (mytxt, TXTSIZ, txt, cc_str[(buf->data[0] >> 3) & 3]); 104 return prt_e (buf, info, mytxt); 105 } 106 107 static int 108 prt_nn (struct buffer *buf, disassemble_info * info, char *txt) 109 { 110 int nn; 111 unsigned char *p; 112 113 p = (unsigned char*) buf->data + buf->n_fetch; 114 if (fetch_data (buf, info, 2)) 115 { 116 nn = p[0] + (p[1] << 8); 117 info->fprintf_func (info->stream, txt, nn); 118 buf->n_used = buf->n_fetch; 119 } 120 else 121 buf->n_used = -1; 122 return buf->n_used; 123 } 124 125 static int 126 prt_rr_nn (struct buffer *buf, disassemble_info * info, char *txt) 127 { 128 char mytxt[TXTSIZ]; 129 130 snprintf (mytxt, TXTSIZ, txt, rr_str[(buf->data[0] >> 4) & 3]); 131 return prt_nn (buf, info, mytxt); 132 } 133 134 static int 135 prt_rr (struct buffer *buf, disassemble_info * info, char *txt) 136 { 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; 140 return buf->n_used; 141 } 142 143 static int 144 prt_n (struct buffer *buf, disassemble_info * info, char *txt) 145 { 146 int n; 147 unsigned char *p; 148 149 p = (unsigned char*) buf->data + buf->n_fetch; 150 151 if (fetch_data (buf, info, 1)) 152 { 153 n = p[0]; 154 info->fprintf_func (info->stream, txt, n); 155 buf->n_used = buf->n_fetch; 156 } 157 else 158 buf->n_used = -1; 159 160 return buf->n_used; 161 } 162 163 static int 164 ld_r_n (struct buffer *buf, disassemble_info * info, char *txt) 165 { 166 char mytxt[TXTSIZ]; 167 168 snprintf (mytxt, TXTSIZ, txt, r_str[(buf->data[0] >> 3) & 7]); 169 return prt_n (buf, info, mytxt); 170 } 171 172 static int 173 prt_r (struct buffer *buf, disassemble_info * info, char *txt) 174 { 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; 178 return buf->n_used; 179 } 180 181 static int 182 ld_r_r (struct buffer *buf, disassemble_info * info, char *txt) 183 { 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; 188 return buf->n_used; 189 } 190 191 static int 192 arit_r (struct buffer *buf, disassemble_info * info, char *txt) 193 { 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; 198 return buf->n_used; 199 } 200 201 static int 202 prt_cc (struct buffer *buf, disassemble_info * info, char *txt) 203 { 204 info->fprintf_func (info->stream, "%s%s", txt, 205 cc_str[(buf->data[0] >> 3) & 7]); 206 buf->n_used = buf->n_fetch; 207 return buf->n_used; 208 } 209 210 static int 211 pop_rr (struct buffer *buf, disassemble_info * info, char *txt) 212 { 213 static char *rr_stack[] = { "bc","de","hl","af"}; 214 215 info->fprintf_func (info->stream, "%s %s", txt, 216 rr_stack[(buf->data[0] >> 4) & 3]); 217 buf->n_used = buf->n_fetch; 218 return buf->n_used; 219 } 220 221 222 static int 223 jp_cc_nn (struct buffer *buf, disassemble_info * info, char *txt) 224 { 225 char mytxt[TXTSIZ]; 226 227 snprintf (mytxt,TXTSIZ, 228 "%s%s,0x%%04x", txt, cc_str[(buf->data[0] >> 3) & 7]); 229 return prt_nn (buf, info, mytxt); 230 } 231 232 static int 233 arit_n (struct buffer *buf, disassemble_info * info, char *txt) 234 { 235 char mytxt[TXTSIZ]; 236 237 snprintf (mytxt,TXTSIZ, txt, arit_str[(buf->data[0] >> 3) & 7]); 238 return prt_n (buf, info, mytxt); 239 } 240 241 static int 242 rst (struct buffer *buf, disassemble_info * info, char *txt) 243 { 244 info->fprintf_func (info->stream, txt, buf->data[0] & 0x38); 245 buf->n_used = buf->n_fetch; 246 return buf->n_used; 247 } 248 249 250 static int 251 cis (struct buffer *buf, disassemble_info * info, char *txt ATTRIBUTE_UNUSED) 252 { 253 static char * opar[] = { "ld", "cp", "in", "out" }; 254 char * op; 255 char c; 256 257 c = buf->data[1]; 258 op = ((0x13 & c) == 0x13) ? "ot" : (opar[c & 3]); 259 info->fprintf_func (info->stream, 260 "%s%c%s", op, 261 (c & 0x08) ? 'd' : 'i', 262 (c & 0x10) ? "r" : ""); 263 buf->n_used = 2; 264 return buf->n_used; 265 } 266 267 static int 268 dump (struct buffer *buf, disassemble_info * info, char *txt) 269 { 270 int i; 271 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]); 276 buf->n_used = i; 277 return buf->n_used; 278 } 279 280 /* Table to disassemble machine codes with prefix 0xED. */ 281 struct tab_elt opc_ed[] = 282 { 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" } 308 }; 309 310 static int 311 pref_ed (struct buffer * buf, disassemble_info * info, 312 char* txt ATTRIBUTE_UNUSED) 313 { 314 struct tab_elt *p; 315 316 if (fetch_data(buf, info, 1)) 317 { 318 for (p = opc_ed; p->val != (buf->data[1] & p->mask); ++p) 319 ; 320 p->fp (buf, info, p->text); 321 } 322 else 323 buf->n_used = -1; 324 325 return buf->n_used; 326 } 327 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[] = 332 { 333 "rlc", "rrc", "rl", "rr", "sla", "sra", "sli", "srl" 334 }; 335 336 static int 337 pref_cb (struct buffer * buf, disassemble_info * info, 338 char* txt ATTRIBUTE_UNUSED) 339 { 340 if (fetch_data (buf, info, 1)) 341 { 342 buf->n_used = 2; 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]); 347 else 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]); 352 } 353 else 354 buf->n_used = -1; 355 356 return buf->n_used; 357 } 358 359 static int 360 addvv (struct buffer * buf, disassemble_info * info, char* txt) 361 { 362 info->fprintf_func (info->stream, "add %s,%s", txt, txt); 363 364 return buf->n_used = buf->n_fetch; 365 } 366 367 static int 368 ld_v_v (struct buffer * buf, disassemble_info * info, char* txt) 369 { 370 char mytxt[TXTSIZ]; 371 372 snprintf (mytxt, TXTSIZ, "ld %s%%s,%s%%s", txt, txt); 373 return ld_r_r (buf, info, mytxt); 374 } 375 376 static int 377 prt_d (struct buffer *buf, disassemble_info * info, char *txt) 378 { 379 int d; 380 signed char *p; 381 382 p = buf->data + buf->n_fetch; 383 384 if (fetch_data (buf, info, 1)) 385 { 386 d = p[0]; 387 info->fprintf_func (info->stream, txt, d); 388 buf->n_used = buf->n_fetch; 389 } 390 else 391 buf->n_used = -1; 392 393 return buf->n_used; 394 } 395 396 static int 397 prt_d_n (struct buffer *buf, disassemble_info * info, char *txt) 398 { 399 char mytxt[TXTSIZ]; 400 int d; 401 signed char *p; 402 403 p = buf->data + buf->n_fetch; 404 405 if (fetch_data (buf, info, 1)) 406 { 407 d = p[0]; 408 snprintf (mytxt, TXTSIZ, txt, d); 409 return prt_n (buf, info, mytxt); 410 } 411 else 412 buf->n_used = -1; 413 414 return buf->n_used; 415 } 416 417 static int 418 arit_d (struct buffer *buf, disassemble_info * info, char *txt) 419 { 420 char mytxt[TXTSIZ]; 421 signed char c; 422 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); 426 } 427 428 static int 429 ld_r_d (struct buffer *buf, disassemble_info * info, char *txt) 430 { 431 char mytxt[TXTSIZ]; 432 signed char c; 433 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); 437 } 438 439 static int 440 ld_d_r(struct buffer *buf, disassemble_info * info, char *txt) 441 { 442 char mytxt[TXTSIZ]; 443 signed char c; 444 445 c = buf->data[buf->n_fetch - 1]; 446 snprintf (mytxt, TXTSIZ, txt, r_str[c & 7]); 447 return prt_d (buf, info, mytxt); 448 } 449 450 static int 451 pref_xd_cb (struct buffer * buf, disassemble_info * info, char* txt) 452 { 453 if (fetch_data (buf, info, 2)) 454 { 455 int d; 456 char arg[TXTSIZ]; 457 signed char *p; 458 459 buf->n_used = 4; 460 p = buf->data; 461 d = p[2]; 462 463 if (((p[3] & 0xC0) == 0x40) || ((p[3] & 7) == 0x06)) 464 snprintf (arg, TXTSIZ, "(%s%+d)", txt, d); 465 else 466 snprintf (arg, TXTSIZ, "(%s%+d),%s", txt, d, r_str[p[3] & 7]); 467 468 if ((p[3] & 0xc0) == 0) 469 info->fprintf_func (info->stream, "%s %s", 470 cb2_str[(buf->data[3] >> 3) & 7], 471 arg); 472 else 473 info->fprintf_func (info->stream, "%s %d,%s", 474 cb1_str[(buf->data[3] >> 6) & 3], 475 (buf->data[3] >> 3) & 7, 476 arg); 477 } 478 else 479 buf->n_used = -1; 480 481 return buf->n_used; 482 } 483 484 /* Table to disassemble machine codes with prefix 0xDD or 0xFD. */ 485 static struct tab_elt opc_ind[] = 486 { 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" }, 500 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" }, 507 508 { 0x86, 0xC7, arit_d, "%%s(%s%%%%+d)" }, 509 { 0x84, 0xC6, arit_r, "%%s%s%%s" }, 510 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, "?" }, 518 } ; 519 520 static int 521 pref_ind (struct buffer * buf, disassemble_info * info, char* txt) 522 { 523 if (fetch_data (buf, info, 1)) 524 { 525 char mytxt[TXTSIZ]; 526 struct tab_elt *p; 527 528 for (p = opc_ind; p->val != (buf->data[1] & p->mask); ++p) 529 ; 530 snprintf (mytxt, TXTSIZ, p->text, txt); 531 p->fp (buf, info, mytxt); 532 } 533 else 534 buf->n_used = -1; 535 536 return buf->n_used; 537 } 538 539 /* Table to disassemble machine codes without prefix. */ 540 static struct tab_elt opc_main[] = 541 { 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" }, 570 571 { 0x76, 0xFF, prt, "halt" }, 572 { 0x40, 0xC0, ld_r_r, "ld %s,%s"}, 573 574 { 0x80, 0xC0, arit_r, "%s%s" }, 575 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, "????" }, 600 } ; 601 602 int 603 print_insn_z80 (bfd_vma addr, disassemble_info * info) 604 { 605 struct buffer buf; 606 struct tab_elt *p; 607 608 buf.base = addr; 609 buf.n_fetch = 0; 610 buf.n_used = 0; 611 612 if (! fetch_data (& buf, info, 1)) 613 return -1; 614 615 for (p = opc_main; p->val != (buf.data[0] & p->mask); ++p) 616 ; 617 p->fp (& buf, info, p->text); 618 619 return buf.n_used; 620 } 621