1a1ba9ba4Schristos /* Print Z80 and R800 instructions 2*dc268d07Schristos Copyright (C) 2005-2019 Free Software Foundation, Inc. 3a1ba9ba4Schristos Contributed by Arnold Metselaar <arnold_m@operamail.com> 4a1ba9ba4Schristos 5a1ba9ba4Schristos This file is part of the GNU opcodes library. 6a1ba9ba4Schristos 7a1ba9ba4Schristos This library is free software; you can redistribute it and/or modify 8a1ba9ba4Schristos it under the terms of the GNU General Public License as published by 9a1ba9ba4Schristos the Free Software Foundation; either version 3, or (at your option) 10a1ba9ba4Schristos any later version. 11a1ba9ba4Schristos 12a1ba9ba4Schristos It is distributed in the hope that it will be useful, but WITHOUT 13a1ba9ba4Schristos ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 14a1ba9ba4Schristos or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 15a1ba9ba4Schristos License for more details. 16a1ba9ba4Schristos 17a1ba9ba4Schristos You should have received a copy of the GNU General Public License 18a1ba9ba4Schristos along with this program; if not, write to the Free Software 19a1ba9ba4Schristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 20a1ba9ba4Schristos MA 02110-1301, USA. */ 21a1ba9ba4Schristos 22a1ba9ba4Schristos #include "sysdep.h" 23*dc268d07Schristos #include "disassemble.h" 24a1ba9ba4Schristos #include <stdio.h> 25a1ba9ba4Schristos 26a1ba9ba4Schristos struct buffer 27a1ba9ba4Schristos { 28a1ba9ba4Schristos bfd_vma base; 29a1ba9ba4Schristos int n_fetch; 30a1ba9ba4Schristos int n_used; 31a1ba9ba4Schristos signed char data[4]; 32a1ba9ba4Schristos } ; 33a1ba9ba4Schristos 34a1ba9ba4Schristos typedef int (*func)(struct buffer *, disassemble_info *, char *); 35a1ba9ba4Schristos 36a1ba9ba4Schristos struct tab_elt 37a1ba9ba4Schristos { 38a1ba9ba4Schristos unsigned char val; 39a1ba9ba4Schristos unsigned char mask; 40a1ba9ba4Schristos func fp; 41a1ba9ba4Schristos char * text; 42a1ba9ba4Schristos } ; 43a1ba9ba4Schristos 44a1ba9ba4Schristos #define TXTSIZ 24 45a1ba9ba4Schristos /* Names of 16-bit registers. */ 46a1ba9ba4Schristos static char * rr_str[] = { "bc", "de", "hl", "sp" }; 47a1ba9ba4Schristos /* Names of 8-bit registers. */ 48a1ba9ba4Schristos static char * r_str[] = { "b", "c", "d", "e", "h", "l", "(hl)", "a" }; 49a1ba9ba4Schristos /* Texts for condition codes. */ 50a1ba9ba4Schristos static char * cc_str[] = { "nz", "z", "nc", "c", "po", "pe", "p", "m" }; 51a1ba9ba4Schristos /* Instruction names for 8-bit arithmetic, operand "a" is often implicit */ 52a1ba9ba4Schristos static char * arit_str[] = 53a1ba9ba4Schristos { 54a1ba9ba4Schristos "add a,", "adc a,", "sub ", "sbc a,", "and ", "xor ", "or ", "cp " 55a1ba9ba4Schristos } ; 56a1ba9ba4Schristos 57a1ba9ba4Schristos static int 58a1ba9ba4Schristos fetch_data (struct buffer *buf, disassemble_info * info, int n) 59a1ba9ba4Schristos { 60a1ba9ba4Schristos int r; 61a1ba9ba4Schristos 62a1ba9ba4Schristos if (buf->n_fetch + n > 4) 63a1ba9ba4Schristos abort (); 64a1ba9ba4Schristos 65a1ba9ba4Schristos r = info->read_memory_func (buf->base + buf->n_fetch, 66a1ba9ba4Schristos (unsigned char*) buf->data + buf->n_fetch, 67a1ba9ba4Schristos n, info); 68a1ba9ba4Schristos if (r == 0) 69a1ba9ba4Schristos buf->n_fetch += n; 70a1ba9ba4Schristos return !r; 71a1ba9ba4Schristos } 72a1ba9ba4Schristos 73a1ba9ba4Schristos static int 74a1ba9ba4Schristos prt (struct buffer *buf, disassemble_info * info, char *txt) 75a1ba9ba4Schristos { 76a1ba9ba4Schristos info->fprintf_func (info->stream, "%s", txt); 77a1ba9ba4Schristos buf->n_used = buf->n_fetch; 78a1ba9ba4Schristos return 1; 79a1ba9ba4Schristos } 80a1ba9ba4Schristos 81a1ba9ba4Schristos static int 82a1ba9ba4Schristos prt_e (struct buffer *buf, disassemble_info * info, char *txt) 83a1ba9ba4Schristos { 84a1ba9ba4Schristos char e; 85a1ba9ba4Schristos int target_addr; 86a1ba9ba4Schristos 87a1ba9ba4Schristos if (fetch_data (buf, info, 1)) 88a1ba9ba4Schristos { 89a1ba9ba4Schristos e = buf->data[1]; 90a1ba9ba4Schristos target_addr = (buf->base + 2 + e) & 0xffff; 91a1ba9ba4Schristos buf->n_used = buf->n_fetch; 92a1ba9ba4Schristos info->fprintf_func (info->stream, "%s0x%04x", txt, target_addr); 93a1ba9ba4Schristos } 94a1ba9ba4Schristos else 95a1ba9ba4Schristos buf->n_used = -1; 96a1ba9ba4Schristos 97a1ba9ba4Schristos return buf->n_used; 98a1ba9ba4Schristos } 99a1ba9ba4Schristos 100a1ba9ba4Schristos static int 101a1ba9ba4Schristos jr_cc (struct buffer *buf, disassemble_info * info, char *txt) 102a1ba9ba4Schristos { 103a1ba9ba4Schristos char mytxt[TXTSIZ]; 104a1ba9ba4Schristos 105a1ba9ba4Schristos snprintf (mytxt, TXTSIZ, txt, cc_str[(buf->data[0] >> 3) & 3]); 106a1ba9ba4Schristos return prt_e (buf, info, mytxt); 107a1ba9ba4Schristos } 108a1ba9ba4Schristos 109a1ba9ba4Schristos static int 110a1ba9ba4Schristos prt_nn (struct buffer *buf, disassemble_info * info, char *txt) 111a1ba9ba4Schristos { 112a1ba9ba4Schristos int nn; 113a1ba9ba4Schristos unsigned char *p; 114a1ba9ba4Schristos 115a1ba9ba4Schristos p = (unsigned char*) buf->data + buf->n_fetch; 116a1ba9ba4Schristos if (fetch_data (buf, info, 2)) 117a1ba9ba4Schristos { 118a1ba9ba4Schristos nn = p[0] + (p[1] << 8); 119a1ba9ba4Schristos info->fprintf_func (info->stream, txt, nn); 120a1ba9ba4Schristos buf->n_used = buf->n_fetch; 121a1ba9ba4Schristos } 122a1ba9ba4Schristos else 123a1ba9ba4Schristos buf->n_used = -1; 124a1ba9ba4Schristos return buf->n_used; 125a1ba9ba4Schristos } 126a1ba9ba4Schristos 127a1ba9ba4Schristos static int 128a1ba9ba4Schristos prt_rr_nn (struct buffer *buf, disassemble_info * info, char *txt) 129a1ba9ba4Schristos { 130a1ba9ba4Schristos char mytxt[TXTSIZ]; 131a1ba9ba4Schristos int rr; 132a1ba9ba4Schristos 133a1ba9ba4Schristos rr = (buf->data[buf->n_fetch - 1] >> 4) & 3; 134a1ba9ba4Schristos snprintf (mytxt, TXTSIZ, txt, rr_str[rr]); 135a1ba9ba4Schristos return prt_nn (buf, info, mytxt); 136a1ba9ba4Schristos } 137a1ba9ba4Schristos 138a1ba9ba4Schristos static int 139a1ba9ba4Schristos prt_rr (struct buffer *buf, disassemble_info * info, char *txt) 140a1ba9ba4Schristos { 141a1ba9ba4Schristos info->fprintf_func (info->stream, "%s%s", txt, 142a1ba9ba4Schristos rr_str[(buf->data[buf->n_fetch - 1] >> 4) & 3]); 143a1ba9ba4Schristos buf->n_used = buf->n_fetch; 144a1ba9ba4Schristos return buf->n_used; 145a1ba9ba4Schristos } 146a1ba9ba4Schristos 147a1ba9ba4Schristos static int 148a1ba9ba4Schristos prt_n (struct buffer *buf, disassemble_info * info, char *txt) 149a1ba9ba4Schristos { 150a1ba9ba4Schristos int n; 151a1ba9ba4Schristos unsigned char *p; 152a1ba9ba4Schristos 153a1ba9ba4Schristos p = (unsigned char*) buf->data + buf->n_fetch; 154a1ba9ba4Schristos 155a1ba9ba4Schristos if (fetch_data (buf, info, 1)) 156a1ba9ba4Schristos { 157a1ba9ba4Schristos n = p[0]; 158a1ba9ba4Schristos info->fprintf_func (info->stream, txt, n); 159a1ba9ba4Schristos buf->n_used = buf->n_fetch; 160a1ba9ba4Schristos } 161a1ba9ba4Schristos else 162a1ba9ba4Schristos buf->n_used = -1; 163a1ba9ba4Schristos 164a1ba9ba4Schristos return buf->n_used; 165a1ba9ba4Schristos } 166a1ba9ba4Schristos 167a1ba9ba4Schristos static int 168a1ba9ba4Schristos ld_r_n (struct buffer *buf, disassemble_info * info, char *txt) 169a1ba9ba4Schristos { 170a1ba9ba4Schristos char mytxt[TXTSIZ]; 171a1ba9ba4Schristos 172a1ba9ba4Schristos snprintf (mytxt, TXTSIZ, txt, r_str[(buf->data[0] >> 3) & 7]); 173a1ba9ba4Schristos return prt_n (buf, info, mytxt); 174a1ba9ba4Schristos } 175a1ba9ba4Schristos 176a1ba9ba4Schristos static int 177a1ba9ba4Schristos prt_r (struct buffer *buf, disassemble_info * info, char *txt) 178a1ba9ba4Schristos { 179a1ba9ba4Schristos info->fprintf_func (info->stream, txt, 180a1ba9ba4Schristos r_str[(buf->data[buf->n_fetch - 1] >> 3) & 7]); 181a1ba9ba4Schristos buf->n_used = buf->n_fetch; 182a1ba9ba4Schristos return buf->n_used; 183a1ba9ba4Schristos } 184a1ba9ba4Schristos 185a1ba9ba4Schristos static int 186a1ba9ba4Schristos ld_r_r (struct buffer *buf, disassemble_info * info, char *txt) 187a1ba9ba4Schristos { 188a1ba9ba4Schristos info->fprintf_func (info->stream, txt, 189a1ba9ba4Schristos r_str[(buf->data[buf->n_fetch - 1] >> 3) & 7], 190a1ba9ba4Schristos r_str[buf->data[buf->n_fetch - 1] & 7]); 191a1ba9ba4Schristos buf->n_used = buf->n_fetch; 192a1ba9ba4Schristos return buf->n_used; 193a1ba9ba4Schristos } 194a1ba9ba4Schristos 195a1ba9ba4Schristos static int 196a1ba9ba4Schristos arit_r (struct buffer *buf, disassemble_info * info, char *txt) 197a1ba9ba4Schristos { 198a1ba9ba4Schristos info->fprintf_func (info->stream, txt, 199a1ba9ba4Schristos arit_str[(buf->data[buf->n_fetch - 1] >> 3) & 7], 200a1ba9ba4Schristos r_str[buf->data[buf->n_fetch - 1] & 7]); 201a1ba9ba4Schristos buf->n_used = buf->n_fetch; 202a1ba9ba4Schristos return buf->n_used; 203a1ba9ba4Schristos } 204a1ba9ba4Schristos 205a1ba9ba4Schristos static int 206a1ba9ba4Schristos prt_cc (struct buffer *buf, disassemble_info * info, char *txt) 207a1ba9ba4Schristos { 208a1ba9ba4Schristos info->fprintf_func (info->stream, "%s%s", txt, 209a1ba9ba4Schristos cc_str[(buf->data[0] >> 3) & 7]); 210a1ba9ba4Schristos buf->n_used = buf->n_fetch; 211a1ba9ba4Schristos return buf->n_used; 212a1ba9ba4Schristos } 213a1ba9ba4Schristos 214a1ba9ba4Schristos static int 215a1ba9ba4Schristos pop_rr (struct buffer *buf, disassemble_info * info, char *txt) 216a1ba9ba4Schristos { 217a1ba9ba4Schristos static char *rr_stack[] = { "bc","de","hl","af"}; 218a1ba9ba4Schristos 219a1ba9ba4Schristos info->fprintf_func (info->stream, "%s %s", txt, 220a1ba9ba4Schristos rr_stack[(buf->data[0] >> 4) & 3]); 221a1ba9ba4Schristos buf->n_used = buf->n_fetch; 222a1ba9ba4Schristos return buf->n_used; 223a1ba9ba4Schristos } 224a1ba9ba4Schristos 225a1ba9ba4Schristos 226a1ba9ba4Schristos static int 227a1ba9ba4Schristos jp_cc_nn (struct buffer *buf, disassemble_info * info, char *txt) 228a1ba9ba4Schristos { 229a1ba9ba4Schristos char mytxt[TXTSIZ]; 230a1ba9ba4Schristos 231a1ba9ba4Schristos snprintf (mytxt,TXTSIZ, 232a1ba9ba4Schristos "%s%s,0x%%04x", txt, cc_str[(buf->data[0] >> 3) & 7]); 233a1ba9ba4Schristos return prt_nn (buf, info, mytxt); 234a1ba9ba4Schristos } 235a1ba9ba4Schristos 236a1ba9ba4Schristos static int 237a1ba9ba4Schristos arit_n (struct buffer *buf, disassemble_info * info, char *txt) 238a1ba9ba4Schristos { 239a1ba9ba4Schristos char mytxt[TXTSIZ]; 240a1ba9ba4Schristos 241a1ba9ba4Schristos snprintf (mytxt,TXTSIZ, txt, arit_str[(buf->data[0] >> 3) & 7]); 242a1ba9ba4Schristos return prt_n (buf, info, mytxt); 243a1ba9ba4Schristos } 244a1ba9ba4Schristos 245a1ba9ba4Schristos static int 246a1ba9ba4Schristos rst (struct buffer *buf, disassemble_info * info, char *txt) 247a1ba9ba4Schristos { 248a1ba9ba4Schristos info->fprintf_func (info->stream, txt, buf->data[0] & 0x38); 249a1ba9ba4Schristos buf->n_used = buf->n_fetch; 250a1ba9ba4Schristos return buf->n_used; 251a1ba9ba4Schristos } 252a1ba9ba4Schristos 253a1ba9ba4Schristos 254a1ba9ba4Schristos static int 255a1ba9ba4Schristos cis (struct buffer *buf, disassemble_info * info, char *txt ATTRIBUTE_UNUSED) 256a1ba9ba4Schristos { 257a1ba9ba4Schristos static char * opar[] = { "ld", "cp", "in", "out" }; 258a1ba9ba4Schristos char * op; 259a1ba9ba4Schristos char c; 260a1ba9ba4Schristos 261a1ba9ba4Schristos c = buf->data[1]; 262a1ba9ba4Schristos op = ((0x13 & c) == 0x13) ? "ot" : (opar[c & 3]); 263a1ba9ba4Schristos info->fprintf_func (info->stream, 264a1ba9ba4Schristos "%s%c%s", op, 265a1ba9ba4Schristos (c & 0x08) ? 'd' : 'i', 266a1ba9ba4Schristos (c & 0x10) ? "r" : ""); 267a1ba9ba4Schristos buf->n_used = 2; 268a1ba9ba4Schristos return buf->n_used; 269a1ba9ba4Schristos } 270a1ba9ba4Schristos 271a1ba9ba4Schristos static int 272a1ba9ba4Schristos dump (struct buffer *buf, disassemble_info * info, char *txt) 273a1ba9ba4Schristos { 274a1ba9ba4Schristos int i; 275a1ba9ba4Schristos 276a1ba9ba4Schristos info->fprintf_func (info->stream, "defb "); 277a1ba9ba4Schristos for (i = 0; txt[i]; ++i) 278a1ba9ba4Schristos info->fprintf_func (info->stream, i ? ", 0x%02x" : "0x%02x", 279a1ba9ba4Schristos (unsigned char) buf->data[i]); 280a1ba9ba4Schristos buf->n_used = i; 281a1ba9ba4Schristos return buf->n_used; 282a1ba9ba4Schristos } 283a1ba9ba4Schristos 284a1ba9ba4Schristos /* Table to disassemble machine codes with prefix 0xED. */ 285a1ba9ba4Schristos struct tab_elt opc_ed[] = 286a1ba9ba4Schristos { 287a1ba9ba4Schristos { 0x70, 0xFF, prt, "in f,(c)" }, 288a1ba9ba4Schristos { 0x70, 0xFF, dump, "xx" }, 289a1ba9ba4Schristos { 0x40, 0xC7, prt_r, "in %s,(c)" }, 290a1ba9ba4Schristos { 0x71, 0xFF, prt, "out (c),0" }, 291a1ba9ba4Schristos { 0x70, 0xFF, dump, "xx" }, 292a1ba9ba4Schristos { 0x41, 0xC7, prt_r, "out (c),%s" }, 293a1ba9ba4Schristos { 0x42, 0xCF, prt_rr, "sbc hl," }, 294a1ba9ba4Schristos { 0x43, 0xCF, prt_rr_nn, "ld (0x%%04x),%s" }, 295a1ba9ba4Schristos { 0x44, 0xFF, prt, "neg" }, 296a1ba9ba4Schristos { 0x45, 0xFF, prt, "retn" }, 297a1ba9ba4Schristos { 0x46, 0xFF, prt, "im 0" }, 298a1ba9ba4Schristos { 0x47, 0xFF, prt, "ld i,a" }, 299a1ba9ba4Schristos { 0x4A, 0xCF, prt_rr, "adc hl," }, 300a1ba9ba4Schristos { 0x4B, 0xCF, prt_rr_nn, "ld %s,(0x%%04x)" }, 301a1ba9ba4Schristos { 0x4D, 0xFF, prt, "reti" }, 302a1ba9ba4Schristos { 0x4F, 0xFF, prt, "ld r,a" }, 303a1ba9ba4Schristos { 0x56, 0xFF, prt, "im 1" }, 304a1ba9ba4Schristos { 0x57, 0xFF, prt, "ld a,i" }, 305a1ba9ba4Schristos { 0x5E, 0xFF, prt, "im 2" }, 306a1ba9ba4Schristos { 0x5F, 0xFF, prt, "ld a,r" }, 307a1ba9ba4Schristos { 0x67, 0xFF, prt, "rrd" }, 308a1ba9ba4Schristos { 0x6F, 0xFF, prt, "rld" }, 309a1ba9ba4Schristos { 0xA0, 0xE4, cis, "" }, 310a1ba9ba4Schristos { 0xC3, 0xFF, prt, "muluw hl,bc" }, 311a1ba9ba4Schristos { 0xC5, 0xE7, prt_r, "mulub a,%s" }, 312a1ba9ba4Schristos { 0xF3, 0xFF, prt, "muluw hl,sp" }, 313a1ba9ba4Schristos { 0x00, 0x00, dump, "xx" } 314a1ba9ba4Schristos }; 315a1ba9ba4Schristos 316a1ba9ba4Schristos static int 317a1ba9ba4Schristos pref_ed (struct buffer * buf, disassemble_info * info, 318a1ba9ba4Schristos char* txt ATTRIBUTE_UNUSED) 319a1ba9ba4Schristos { 320a1ba9ba4Schristos struct tab_elt *p; 321a1ba9ba4Schristos 322a1ba9ba4Schristos if (fetch_data(buf, info, 1)) 323a1ba9ba4Schristos { 324a1ba9ba4Schristos for (p = opc_ed; p->val != (buf->data[1] & p->mask); ++p) 325a1ba9ba4Schristos ; 326a1ba9ba4Schristos p->fp (buf, info, p->text); 327a1ba9ba4Schristos } 328a1ba9ba4Schristos else 329a1ba9ba4Schristos buf->n_used = -1; 330a1ba9ba4Schristos 331a1ba9ba4Schristos return buf->n_used; 332a1ba9ba4Schristos } 333a1ba9ba4Schristos 334a1ba9ba4Schristos /* Instruction names for the instructions addressing single bits. */ 335a1ba9ba4Schristos static char *cb1_str[] = { "", "bit", "res", "set"}; 336a1ba9ba4Schristos /* Instruction names for shifts and rotates. */ 337a1ba9ba4Schristos static char *cb2_str[] = 338a1ba9ba4Schristos { 339a1ba9ba4Schristos "rlc", "rrc", "rl", "rr", "sla", "sra", "sli", "srl" 340a1ba9ba4Schristos }; 341a1ba9ba4Schristos 342a1ba9ba4Schristos static int 343a1ba9ba4Schristos pref_cb (struct buffer * buf, disassemble_info * info, 344a1ba9ba4Schristos char* txt ATTRIBUTE_UNUSED) 345a1ba9ba4Schristos { 346a1ba9ba4Schristos if (fetch_data (buf, info, 1)) 347a1ba9ba4Schristos { 348a1ba9ba4Schristos buf->n_used = 2; 349a1ba9ba4Schristos if ((buf->data[1] & 0xc0) == 0) 350a1ba9ba4Schristos info->fprintf_func (info->stream, "%s %s", 351a1ba9ba4Schristos cb2_str[(buf->data[1] >> 3) & 7], 352a1ba9ba4Schristos r_str[buf->data[1] & 7]); 353a1ba9ba4Schristos else 354a1ba9ba4Schristos info->fprintf_func (info->stream, "%s %d,%s", 355a1ba9ba4Schristos cb1_str[(buf->data[1] >> 6) & 3], 356a1ba9ba4Schristos (buf->data[1] >> 3) & 7, 357a1ba9ba4Schristos r_str[buf->data[1] & 7]); 358a1ba9ba4Schristos } 359a1ba9ba4Schristos else 360a1ba9ba4Schristos buf->n_used = -1; 361a1ba9ba4Schristos 362a1ba9ba4Schristos return buf->n_used; 363a1ba9ba4Schristos } 364a1ba9ba4Schristos 365a1ba9ba4Schristos static int 366a1ba9ba4Schristos addvv (struct buffer * buf, disassemble_info * info, char* txt) 367a1ba9ba4Schristos { 368a1ba9ba4Schristos info->fprintf_func (info->stream, "add %s,%s", txt, txt); 369a1ba9ba4Schristos 370a1ba9ba4Schristos return buf->n_used = buf->n_fetch; 371a1ba9ba4Schristos } 372a1ba9ba4Schristos 373a1ba9ba4Schristos static int 374a1ba9ba4Schristos ld_v_v (struct buffer * buf, disassemble_info * info, char* txt) 375a1ba9ba4Schristos { 376a1ba9ba4Schristos char mytxt[TXTSIZ]; 377a1ba9ba4Schristos 378a1ba9ba4Schristos snprintf (mytxt, TXTSIZ, "ld %s%%s,%s%%s", txt, txt); 379a1ba9ba4Schristos return ld_r_r (buf, info, mytxt); 380a1ba9ba4Schristos } 381a1ba9ba4Schristos 382a1ba9ba4Schristos static int 383a1ba9ba4Schristos prt_d (struct buffer *buf, disassemble_info * info, char *txt) 384a1ba9ba4Schristos { 385a1ba9ba4Schristos int d; 386a1ba9ba4Schristos signed char *p; 387a1ba9ba4Schristos 388a1ba9ba4Schristos p = buf->data + buf->n_fetch; 389a1ba9ba4Schristos 390a1ba9ba4Schristos if (fetch_data (buf, info, 1)) 391a1ba9ba4Schristos { 392a1ba9ba4Schristos d = p[0]; 393a1ba9ba4Schristos info->fprintf_func (info->stream, txt, d); 394a1ba9ba4Schristos buf->n_used = buf->n_fetch; 395a1ba9ba4Schristos } 396a1ba9ba4Schristos else 397a1ba9ba4Schristos buf->n_used = -1; 398a1ba9ba4Schristos 399a1ba9ba4Schristos return buf->n_used; 400a1ba9ba4Schristos } 401a1ba9ba4Schristos 402a1ba9ba4Schristos static int 403a1ba9ba4Schristos prt_d_n (struct buffer *buf, disassemble_info * info, char *txt) 404a1ba9ba4Schristos { 405a1ba9ba4Schristos char mytxt[TXTSIZ]; 406a1ba9ba4Schristos int d; 407a1ba9ba4Schristos signed char *p; 408a1ba9ba4Schristos 409a1ba9ba4Schristos p = buf->data + buf->n_fetch; 410a1ba9ba4Schristos 411a1ba9ba4Schristos if (fetch_data (buf, info, 1)) 412a1ba9ba4Schristos { 413a1ba9ba4Schristos d = p[0]; 414a1ba9ba4Schristos snprintf (mytxt, TXTSIZ, txt, d); 415a1ba9ba4Schristos return prt_n (buf, info, mytxt); 416a1ba9ba4Schristos } 417a1ba9ba4Schristos else 418a1ba9ba4Schristos buf->n_used = -1; 419a1ba9ba4Schristos 420a1ba9ba4Schristos return buf->n_used; 421a1ba9ba4Schristos } 422a1ba9ba4Schristos 423a1ba9ba4Schristos static int 424a1ba9ba4Schristos arit_d (struct buffer *buf, disassemble_info * info, char *txt) 425a1ba9ba4Schristos { 426a1ba9ba4Schristos char mytxt[TXTSIZ]; 427a1ba9ba4Schristos signed char c; 428a1ba9ba4Schristos 429a1ba9ba4Schristos c = buf->data[buf->n_fetch - 1]; 430a1ba9ba4Schristos snprintf (mytxt, TXTSIZ, txt, arit_str[(c >> 3) & 7]); 431a1ba9ba4Schristos return prt_d (buf, info, mytxt); 432a1ba9ba4Schristos } 433a1ba9ba4Schristos 434a1ba9ba4Schristos static int 435a1ba9ba4Schristos ld_r_d (struct buffer *buf, disassemble_info * info, char *txt) 436a1ba9ba4Schristos { 437a1ba9ba4Schristos char mytxt[TXTSIZ]; 438a1ba9ba4Schristos signed char c; 439a1ba9ba4Schristos 440a1ba9ba4Schristos c = buf->data[buf->n_fetch - 1]; 441a1ba9ba4Schristos snprintf (mytxt, TXTSIZ, txt, r_str[(c >> 3) & 7]); 442a1ba9ba4Schristos return prt_d (buf, info, mytxt); 443a1ba9ba4Schristos } 444a1ba9ba4Schristos 445a1ba9ba4Schristos static int 446a1ba9ba4Schristos ld_d_r(struct buffer *buf, disassemble_info * info, char *txt) 447a1ba9ba4Schristos { 448a1ba9ba4Schristos char mytxt[TXTSIZ]; 449a1ba9ba4Schristos signed char c; 450a1ba9ba4Schristos 451a1ba9ba4Schristos c = buf->data[buf->n_fetch - 1]; 452a1ba9ba4Schristos snprintf (mytxt, TXTSIZ, txt, r_str[c & 7]); 453a1ba9ba4Schristos return prt_d (buf, info, mytxt); 454a1ba9ba4Schristos } 455a1ba9ba4Schristos 456a1ba9ba4Schristos static int 457a1ba9ba4Schristos pref_xd_cb (struct buffer * buf, disassemble_info * info, char* txt) 458a1ba9ba4Schristos { 459a1ba9ba4Schristos if (fetch_data (buf, info, 2)) 460a1ba9ba4Schristos { 461a1ba9ba4Schristos int d; 462a1ba9ba4Schristos char arg[TXTSIZ]; 463a1ba9ba4Schristos signed char *p; 464a1ba9ba4Schristos 465a1ba9ba4Schristos buf->n_used = 4; 466a1ba9ba4Schristos p = buf->data; 467a1ba9ba4Schristos d = p[2]; 468a1ba9ba4Schristos 469a1ba9ba4Schristos if (((p[3] & 0xC0) == 0x40) || ((p[3] & 7) == 0x06)) 470a1ba9ba4Schristos snprintf (arg, TXTSIZ, "(%s%+d)", txt, d); 471a1ba9ba4Schristos else 472a1ba9ba4Schristos snprintf (arg, TXTSIZ, "(%s%+d),%s", txt, d, r_str[p[3] & 7]); 473a1ba9ba4Schristos 474a1ba9ba4Schristos if ((p[3] & 0xc0) == 0) 475a1ba9ba4Schristos info->fprintf_func (info->stream, "%s %s", 476a1ba9ba4Schristos cb2_str[(buf->data[3] >> 3) & 7], 477a1ba9ba4Schristos arg); 478a1ba9ba4Schristos else 479a1ba9ba4Schristos info->fprintf_func (info->stream, "%s %d,%s", 480a1ba9ba4Schristos cb1_str[(buf->data[3] >> 6) & 3], 481a1ba9ba4Schristos (buf->data[3] >> 3) & 7, 482a1ba9ba4Schristos arg); 483a1ba9ba4Schristos } 484a1ba9ba4Schristos else 485a1ba9ba4Schristos buf->n_used = -1; 486a1ba9ba4Schristos 487a1ba9ba4Schristos return buf->n_used; 488a1ba9ba4Schristos } 489a1ba9ba4Schristos 490a1ba9ba4Schristos /* Table to disassemble machine codes with prefix 0xDD or 0xFD. */ 491a1ba9ba4Schristos static struct tab_elt opc_ind[] = 492a1ba9ba4Schristos { 493a1ba9ba4Schristos { 0x24, 0xF7, prt_r, "inc %s%%s" }, 494a1ba9ba4Schristos { 0x25, 0xF7, prt_r, "dec %s%%s" }, 495a1ba9ba4Schristos { 0x26, 0xF7, ld_r_n, "ld %s%%s,0x%%%%02x" }, 496a1ba9ba4Schristos { 0x21, 0xFF, prt_nn, "ld %s,0x%%04x" }, 497a1ba9ba4Schristos { 0x22, 0xFF, prt_nn, "ld (0x%%04x),%s" }, 498a1ba9ba4Schristos { 0x2A, 0xFF, prt_nn, "ld %s,(0x%%04x)" }, 499a1ba9ba4Schristos { 0x23, 0xFF, prt, "inc %s" }, 500a1ba9ba4Schristos { 0x2B, 0xFF, prt, "dec %s" }, 501a1ba9ba4Schristos { 0x29, 0xFF, addvv, "%s" }, 502a1ba9ba4Schristos { 0x09, 0xCF, prt_rr, "add %s," }, 503a1ba9ba4Schristos { 0x34, 0xFF, prt_d, "inc (%s%%+d)" }, 504a1ba9ba4Schristos { 0x35, 0xFF, prt_d, "dec (%s%%+d)" }, 505a1ba9ba4Schristos { 0x36, 0xFF, prt_d_n, "ld (%s%%+d),0x%%%%02x" }, 506a1ba9ba4Schristos 507a1ba9ba4Schristos { 0x76, 0xFF, dump, "h" }, 508a1ba9ba4Schristos { 0x46, 0xC7, ld_r_d, "ld %%s,(%s%%%%+d)" }, 509a1ba9ba4Schristos { 0x70, 0xF8, ld_d_r, "ld (%s%%%%+d),%%s" }, 510a1ba9ba4Schristos { 0x64, 0xF6, ld_v_v, "%s" }, 511a1ba9ba4Schristos { 0x60, 0xF0, ld_r_r, "ld %s%%s,%%s" }, 512a1ba9ba4Schristos { 0x44, 0xC6, ld_r_r, "ld %%s,%s%%s" }, 513a1ba9ba4Schristos 514a1ba9ba4Schristos { 0x86, 0xC7, arit_d, "%%s(%s%%%%+d)" }, 515a1ba9ba4Schristos { 0x84, 0xC6, arit_r, "%%s%s%%s" }, 516a1ba9ba4Schristos 517a1ba9ba4Schristos { 0xE1, 0xFF, prt, "pop %s" }, 518a1ba9ba4Schristos { 0xE5, 0xFF, prt, "push %s" }, 519a1ba9ba4Schristos { 0xCB, 0xFF, pref_xd_cb, "%s" }, 520a1ba9ba4Schristos { 0xE3, 0xFF, prt, "ex (sp),%s" }, 521a1ba9ba4Schristos { 0xE9, 0xFF, prt, "jp (%s)" }, 522a1ba9ba4Schristos { 0xF9, 0xFF, prt, "ld sp,%s" }, 523a1ba9ba4Schristos { 0x00, 0x00, dump, "?" }, 524a1ba9ba4Schristos } ; 525a1ba9ba4Schristos 526a1ba9ba4Schristos static int 527a1ba9ba4Schristos pref_ind (struct buffer * buf, disassemble_info * info, char* txt) 528a1ba9ba4Schristos { 529a1ba9ba4Schristos if (fetch_data (buf, info, 1)) 530a1ba9ba4Schristos { 531a1ba9ba4Schristos char mytxt[TXTSIZ]; 532a1ba9ba4Schristos struct tab_elt *p; 533a1ba9ba4Schristos 534a1ba9ba4Schristos for (p = opc_ind; p->val != (buf->data[1] & p->mask); ++p) 535a1ba9ba4Schristos ; 536a1ba9ba4Schristos snprintf (mytxt, TXTSIZ, p->text, txt); 537a1ba9ba4Schristos p->fp (buf, info, mytxt); 538a1ba9ba4Schristos } 539a1ba9ba4Schristos else 540a1ba9ba4Schristos buf->n_used = -1; 541a1ba9ba4Schristos 542a1ba9ba4Schristos return buf->n_used; 543a1ba9ba4Schristos } 544a1ba9ba4Schristos 545a1ba9ba4Schristos /* Table to disassemble machine codes without prefix. */ 546a1ba9ba4Schristos static struct tab_elt opc_main[] = 547a1ba9ba4Schristos { 548a1ba9ba4Schristos { 0x00, 0xFF, prt, "nop" }, 549a1ba9ba4Schristos { 0x01, 0xCF, prt_rr_nn, "ld %s,0x%%04x" }, 550a1ba9ba4Schristos { 0x02, 0xFF, prt, "ld (bc),a" }, 551a1ba9ba4Schristos { 0x03, 0xCF, prt_rr, "inc " }, 552a1ba9ba4Schristos { 0x04, 0xC7, prt_r, "inc %s" }, 553a1ba9ba4Schristos { 0x05, 0xC7, prt_r, "dec %s" }, 554a1ba9ba4Schristos { 0x06, 0xC7, ld_r_n, "ld %s,0x%%02x" }, 555a1ba9ba4Schristos { 0x07, 0xFF, prt, "rlca" }, 556a1ba9ba4Schristos { 0x08, 0xFF, prt, "ex af,af'" }, 557a1ba9ba4Schristos { 0x09, 0xCF, prt_rr, "add hl," }, 558a1ba9ba4Schristos { 0x0A, 0xFF, prt, "ld a,(bc)" }, 559a1ba9ba4Schristos { 0x0B, 0xCF, prt_rr, "dec " }, 560a1ba9ba4Schristos { 0x0F, 0xFF, prt, "rrca" }, 561a1ba9ba4Schristos { 0x10, 0xFF, prt_e, "djnz " }, 562a1ba9ba4Schristos { 0x12, 0xFF, prt, "ld (de),a" }, 563a1ba9ba4Schristos { 0x17, 0xFF, prt, "rla" }, 564a1ba9ba4Schristos { 0x18, 0xFF, prt_e, "jr "}, 565a1ba9ba4Schristos { 0x1A, 0xFF, prt, "ld a,(de)" }, 566a1ba9ba4Schristos { 0x1F, 0xFF, prt, "rra" }, 567a1ba9ba4Schristos { 0x20, 0xE7, jr_cc, "jr %s,"}, 568a1ba9ba4Schristos { 0x22, 0xFF, prt_nn, "ld (0x%04x),hl" }, 569a1ba9ba4Schristos { 0x27, 0xFF, prt, "daa"}, 570a1ba9ba4Schristos { 0x2A, 0xFF, prt_nn, "ld hl,(0x%04x)" }, 571a1ba9ba4Schristos { 0x2F, 0xFF, prt, "cpl" }, 572a1ba9ba4Schristos { 0x32, 0xFF, prt_nn, "ld (0x%04x),a" }, 573a1ba9ba4Schristos { 0x37, 0xFF, prt, "scf" }, 574a1ba9ba4Schristos { 0x3A, 0xFF, prt_nn, "ld a,(0x%04x)" }, 575a1ba9ba4Schristos { 0x3F, 0xFF, prt, "ccf" }, 576a1ba9ba4Schristos 577a1ba9ba4Schristos { 0x76, 0xFF, prt, "halt" }, 578a1ba9ba4Schristos { 0x40, 0xC0, ld_r_r, "ld %s,%s"}, 579a1ba9ba4Schristos 580a1ba9ba4Schristos { 0x80, 0xC0, arit_r, "%s%s" }, 581a1ba9ba4Schristos 582a1ba9ba4Schristos { 0xC0, 0xC7, prt_cc, "ret " }, 583a1ba9ba4Schristos { 0xC1, 0xCF, pop_rr, "pop" }, 584a1ba9ba4Schristos { 0xC2, 0xC7, jp_cc_nn, "jp " }, 585a1ba9ba4Schristos { 0xC3, 0xFF, prt_nn, "jp 0x%04x" }, 586a1ba9ba4Schristos { 0xC4, 0xC7, jp_cc_nn, "call " }, 587a1ba9ba4Schristos { 0xC5, 0xCF, pop_rr, "push" }, 588a1ba9ba4Schristos { 0xC6, 0xC7, arit_n, "%s0x%%02x" }, 589a1ba9ba4Schristos { 0xC7, 0xC7, rst, "rst 0x%02x" }, 590a1ba9ba4Schristos { 0xC9, 0xFF, prt, "ret" }, 591a1ba9ba4Schristos { 0xCB, 0xFF, pref_cb, "" }, 592a1ba9ba4Schristos { 0xCD, 0xFF, prt_nn, "call 0x%04x" }, 593a1ba9ba4Schristos { 0xD3, 0xFF, prt_n, "out (0x%02x),a" }, 594a1ba9ba4Schristos { 0xD9, 0xFF, prt, "exx" }, 595a1ba9ba4Schristos { 0xDB, 0xFF, prt_n, "in a,(0x%02x)" }, 596a1ba9ba4Schristos { 0xDD, 0xFF, pref_ind, "ix" }, 597a1ba9ba4Schristos { 0xE3, 0xFF, prt, "ex (sp),hl" }, 598a1ba9ba4Schristos { 0xE9, 0xFF, prt, "jp (hl)" }, 599a1ba9ba4Schristos { 0xEB, 0xFF, prt, "ex de,hl" }, 600a1ba9ba4Schristos { 0xED, 0xFF, pref_ed, ""}, 601a1ba9ba4Schristos { 0xF3, 0xFF, prt, "di" }, 602a1ba9ba4Schristos { 0xF9, 0xFF, prt, "ld sp,hl" }, 603a1ba9ba4Schristos { 0xFB, 0xFF, prt, "ei" }, 604a1ba9ba4Schristos { 0xFD, 0xFF, pref_ind, "iy" }, 605a1ba9ba4Schristos { 0x00, 0x00, prt, "????" }, 606a1ba9ba4Schristos } ; 607a1ba9ba4Schristos 608a1ba9ba4Schristos int 609a1ba9ba4Schristos print_insn_z80 (bfd_vma addr, disassemble_info * info) 610a1ba9ba4Schristos { 611a1ba9ba4Schristos struct buffer buf; 612a1ba9ba4Schristos struct tab_elt *p; 613a1ba9ba4Schristos 614a1ba9ba4Schristos buf.base = addr; 615a1ba9ba4Schristos buf.n_fetch = 0; 616a1ba9ba4Schristos buf.n_used = 0; 617a1ba9ba4Schristos 618a1ba9ba4Schristos if (! fetch_data (& buf, info, 1)) 619a1ba9ba4Schristos return -1; 620a1ba9ba4Schristos 621a1ba9ba4Schristos for (p = opc_main; p->val != (buf.data[0] & p->mask); ++p) 622a1ba9ba4Schristos ; 623a1ba9ba4Schristos p->fp (& buf, info, p->text); 624a1ba9ba4Schristos 625a1ba9ba4Schristos return buf.n_used; 626a1ba9ba4Schristos } 627