1*184b2d41Schristos /* Print Z80, Z180, EZ80 and R800 instructions
2*184b2d41Schristos    Copyright (C) 2005-2020 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"
23dc268d07Schristos #include "disassemble.h"
24a1ba9ba4Schristos #include <stdio.h>
25a1ba9ba4Schristos 
26a1ba9ba4Schristos struct buffer
27a1ba9ba4Schristos {
28a1ba9ba4Schristos   bfd_vma base;
29a1ba9ba4Schristos   int n_fetch;
30a1ba9ba4Schristos   int n_used;
31*184b2d41Schristos   signed char data[6];
32*184b2d41Schristos   long inss; /* instruction set bit mask, taken from bfd_mach */
33*184b2d41Schristos   int nn_len; /* address length: 2 - Z80 mode, 3 - ADL mode*/
34a1ba9ba4Schristos } ;
35a1ba9ba4Schristos 
36*184b2d41Schristos typedef int (*func)(struct buffer *, disassemble_info *, const char *);
37a1ba9ba4Schristos 
38a1ba9ba4Schristos struct tab_elt
39a1ba9ba4Schristos {
40a1ba9ba4Schristos   unsigned char val;
41a1ba9ba4Schristos   unsigned char mask;
42a1ba9ba4Schristos   func          fp;
43*184b2d41Schristos   const char *  text;
44*184b2d41Schristos   unsigned      inss; /* bit mask of supported bfd_mach_* or 0 for all mach */
45a1ba9ba4Schristos } ;
46a1ba9ba4Schristos 
47*184b2d41Schristos #define INSS_ALL 0
48*184b2d41Schristos #define INSS_Z80 ((1 << bfd_mach_z80) | (1 << bfd_mach_z80strict) | (1 << bfd_mach_z80full))
49*184b2d41Schristos #define INSS_R800 (1 << bfd_mach_r800)
50*184b2d41Schristos #define INSS_GBZ80 (1 << bfd_mach_gbz80)
51*184b2d41Schristos #define INSS_Z180 (1 << bfd_mach_z180)
52*184b2d41Schristos #define INSS_EZ80_Z80 (1 << bfd_mach_ez80_z80)
53*184b2d41Schristos #define INSS_EZ80_ADL (1 << bfd_mach_ez80_adl)
54*184b2d41Schristos #define INSS_EZ80 (INSS_EZ80_ADL | INSS_EZ80_Z80)
55*184b2d41Schristos #define INSS_Z80N (1 << bfd_mach_z80n)
56*184b2d41Schristos 
57a1ba9ba4Schristos #define TXTSIZ 24
58a1ba9ba4Schristos /* Names of 16-bit registers.  */
59*184b2d41Schristos static const char * rr_str[] = { "bc", "de", "hl", "sp" };
60a1ba9ba4Schristos /* Names of 8-bit registers.  */
61*184b2d41Schristos static const char * r_str[]  = { "b", "c", "d", "e", "h", "l", "(hl)", "a" };
62a1ba9ba4Schristos /* Texts for condition codes.  */
63*184b2d41Schristos static const char * cc_str[] = { "nz", "z", "nc", "c", "po", "pe", "p", "m" };
64a1ba9ba4Schristos /* Instruction names for 8-bit arithmetic, operand "a" is often implicit */
65*184b2d41Schristos static const char * arit_str[] =
66a1ba9ba4Schristos {
67a1ba9ba4Schristos   "add a,", "adc a,", "sub ", "sbc a,", "and ", "xor ", "or ", "cp "
68a1ba9ba4Schristos } ;
69*184b2d41Schristos static const char * arit_str_gbz80[] =
70*184b2d41Schristos {
71*184b2d41Schristos   "add a,", "adc a,", "sub a,", "sbc a,", "and ", "xor ", "or ", "cp "
72*184b2d41Schristos } ;
73*184b2d41Schristos static const char * arit_str_ez80[] =
74*184b2d41Schristos {
75*184b2d41Schristos   "add a,", "adc a,", "sub a,", "sbc a,", "and a,", "xor a,", "or a,", "cp a,"
76*184b2d41Schristos } ;
77*184b2d41Schristos 
78a1ba9ba4Schristos 
79a1ba9ba4Schristos static int
mach_inst(struct buffer * buf,const struct tab_elt * p)80*184b2d41Schristos mach_inst (struct buffer *buf, const struct tab_elt *p)
81*184b2d41Schristos {
82*184b2d41Schristos   return !p->inss || (p->inss & buf->inss);
83*184b2d41Schristos }
84*184b2d41Schristos 
85*184b2d41Schristos static int
fetch_data(struct buffer * buf,disassemble_info * info,int n)86a1ba9ba4Schristos fetch_data (struct buffer *buf, disassemble_info * info, int n)
87a1ba9ba4Schristos {
88a1ba9ba4Schristos   int r;
89a1ba9ba4Schristos 
90*184b2d41Schristos   if (buf->n_fetch + n > (int)sizeof (buf->data))
91a1ba9ba4Schristos     abort ();
92a1ba9ba4Schristos 
93a1ba9ba4Schristos   r = info->read_memory_func (buf->base + buf->n_fetch,
94a1ba9ba4Schristos 			      (unsigned char*) buf->data + buf->n_fetch,
95a1ba9ba4Schristos 			      n, info);
96a1ba9ba4Schristos   if (r == 0)
97a1ba9ba4Schristos     buf->n_fetch += n;
98a1ba9ba4Schristos   return !r;
99a1ba9ba4Schristos }
100a1ba9ba4Schristos 
101a1ba9ba4Schristos static int
prt(struct buffer * buf,disassemble_info * info,const char * txt)102*184b2d41Schristos prt (struct buffer *buf, disassemble_info * info, const char *txt)
103a1ba9ba4Schristos {
104a1ba9ba4Schristos   info->fprintf_func (info->stream, "%s", txt);
105a1ba9ba4Schristos   buf->n_used = buf->n_fetch;
106a1ba9ba4Schristos   return 1;
107a1ba9ba4Schristos }
108a1ba9ba4Schristos 
109a1ba9ba4Schristos static int
prt_e(struct buffer * buf,disassemble_info * info,const char * txt)110*184b2d41Schristos prt_e (struct buffer *buf, disassemble_info * info, const char *txt)
111a1ba9ba4Schristos {
112a1ba9ba4Schristos   char e;
113a1ba9ba4Schristos   int target_addr;
114a1ba9ba4Schristos 
115a1ba9ba4Schristos   if (fetch_data (buf, info, 1))
116a1ba9ba4Schristos     {
117a1ba9ba4Schristos       e = buf->data[1];
118a1ba9ba4Schristos       target_addr = (buf->base + 2 + e) & 0xffff;
119a1ba9ba4Schristos       buf->n_used = buf->n_fetch;
120a1ba9ba4Schristos       info->fprintf_func (info->stream, "%s0x%04x", txt, target_addr);
121a1ba9ba4Schristos     }
122a1ba9ba4Schristos   else
123a1ba9ba4Schristos     buf->n_used = -1;
124a1ba9ba4Schristos 
125a1ba9ba4Schristos   return buf->n_used;
126a1ba9ba4Schristos }
127a1ba9ba4Schristos 
128a1ba9ba4Schristos static int
jr_cc(struct buffer * buf,disassemble_info * info,const char * txt)129*184b2d41Schristos jr_cc (struct buffer *buf, disassemble_info * info, const char *txt)
130a1ba9ba4Schristos {
131a1ba9ba4Schristos   char mytxt[TXTSIZ];
132a1ba9ba4Schristos 
133a1ba9ba4Schristos   snprintf (mytxt, TXTSIZ, txt, cc_str[(buf->data[0] >> 3) & 3]);
134a1ba9ba4Schristos   return prt_e (buf, info, mytxt);
135a1ba9ba4Schristos }
136a1ba9ba4Schristos 
137a1ba9ba4Schristos static int
prt_nn(struct buffer * buf,disassemble_info * info,const char * txt)138*184b2d41Schristos prt_nn (struct buffer *buf, disassemble_info * info, const char *txt)
139a1ba9ba4Schristos {
140a1ba9ba4Schristos   int nn;
141a1ba9ba4Schristos   unsigned char *p;
142*184b2d41Schristos   int i;
143a1ba9ba4Schristos 
144a1ba9ba4Schristos   p = (unsigned char*) buf->data + buf->n_fetch;
145*184b2d41Schristos   if (fetch_data (buf, info, buf->nn_len))
146a1ba9ba4Schristos     {
147*184b2d41Schristos       nn = 0;
148*184b2d41Schristos       i = buf->nn_len;
149*184b2d41Schristos       while (i--)
150*184b2d41Schristos         nn = nn * 0x100 + p[i];
151a1ba9ba4Schristos       info->fprintf_func (info->stream, txt, nn);
152a1ba9ba4Schristos       buf->n_used = buf->n_fetch;
153a1ba9ba4Schristos     }
154a1ba9ba4Schristos   else
155a1ba9ba4Schristos     buf->n_used = -1;
156a1ba9ba4Schristos   return buf->n_used;
157a1ba9ba4Schristos }
158a1ba9ba4Schristos 
159a1ba9ba4Schristos static int
prt_rr_nn(struct buffer * buf,disassemble_info * info,const char * txt)160*184b2d41Schristos prt_rr_nn (struct buffer *buf, disassemble_info * info, const char *txt)
161a1ba9ba4Schristos {
162a1ba9ba4Schristos   char mytxt[TXTSIZ];
163a1ba9ba4Schristos   int rr;
164a1ba9ba4Schristos 
165a1ba9ba4Schristos   rr = (buf->data[buf->n_fetch - 1] >> 4) & 3;
166a1ba9ba4Schristos   snprintf (mytxt, TXTSIZ, txt, rr_str[rr]);
167a1ba9ba4Schristos   return prt_nn (buf, info, mytxt);
168a1ba9ba4Schristos }
169a1ba9ba4Schristos 
170a1ba9ba4Schristos static int
prt_rr(struct buffer * buf,disassemble_info * info,const char * txt)171*184b2d41Schristos prt_rr (struct buffer *buf, disassemble_info * info, const char *txt)
172a1ba9ba4Schristos {
173a1ba9ba4Schristos   info->fprintf_func (info->stream, "%s%s", txt,
174a1ba9ba4Schristos 		      rr_str[(buf->data[buf->n_fetch - 1] >> 4) & 3]);
175a1ba9ba4Schristos   buf->n_used = buf->n_fetch;
176a1ba9ba4Schristos   return buf->n_used;
177a1ba9ba4Schristos }
178a1ba9ba4Schristos 
179a1ba9ba4Schristos static int
prt_n(struct buffer * buf,disassemble_info * info,const char * txt)180*184b2d41Schristos prt_n (struct buffer *buf, disassemble_info * info, const char *txt)
181a1ba9ba4Schristos {
182a1ba9ba4Schristos   int n;
183a1ba9ba4Schristos   unsigned char *p;
184a1ba9ba4Schristos 
185a1ba9ba4Schristos   p = (unsigned char*) buf->data + buf->n_fetch;
186a1ba9ba4Schristos 
187a1ba9ba4Schristos   if (fetch_data (buf, info, 1))
188a1ba9ba4Schristos     {
189a1ba9ba4Schristos       n = p[0];
190a1ba9ba4Schristos       info->fprintf_func (info->stream, txt, n);
191a1ba9ba4Schristos       buf->n_used = buf->n_fetch;
192a1ba9ba4Schristos     }
193a1ba9ba4Schristos   else
194a1ba9ba4Schristos     buf->n_used = -1;
195a1ba9ba4Schristos 
196a1ba9ba4Schristos   return buf->n_used;
197a1ba9ba4Schristos }
198a1ba9ba4Schristos 
199a1ba9ba4Schristos static int
prt_n_n(struct buffer * buf,disassemble_info * info,const char * txt)200*184b2d41Schristos prt_n_n (struct buffer *buf, disassemble_info * info, const char *txt)
201a1ba9ba4Schristos {
202a1ba9ba4Schristos   char mytxt[TXTSIZ];
203*184b2d41Schristos   int n;
204*184b2d41Schristos   unsigned char *p;
205a1ba9ba4Schristos 
206*184b2d41Schristos   p = (unsigned char*) buf->data + buf->n_fetch;
207*184b2d41Schristos 
208*184b2d41Schristos   if (fetch_data (buf, info, 1))
209*184b2d41Schristos     {
210*184b2d41Schristos       n = p[0];
211*184b2d41Schristos       snprintf (mytxt, TXTSIZ, txt, n);
212*184b2d41Schristos       buf->n_used = buf->n_fetch;
213*184b2d41Schristos     }
214*184b2d41Schristos   else
215*184b2d41Schristos     buf->n_used = -1;
216*184b2d41Schristos 
217a1ba9ba4Schristos   return prt_n (buf, info, mytxt);
218a1ba9ba4Schristos }
219a1ba9ba4Schristos 
220a1ba9ba4Schristos static int
prt_r_n(struct buffer * buf,disassemble_info * info,const char * txt)221*184b2d41Schristos prt_r_n (struct buffer *buf, disassemble_info * info, const char *txt)
222*184b2d41Schristos {
223*184b2d41Schristos   char mytxt[TXTSIZ];
224*184b2d41Schristos   int r;
225*184b2d41Schristos 
226*184b2d41Schristos   r = (buf->data[buf->n_fetch - 1] >> 3) & 7;
227*184b2d41Schristos   snprintf (mytxt, TXTSIZ, txt, r_str[r]);
228*184b2d41Schristos   return prt_n (buf, info, mytxt);
229*184b2d41Schristos }
230*184b2d41Schristos 
231*184b2d41Schristos static int
ld_r_n(struct buffer * buf,disassemble_info * info,const char * txt)232*184b2d41Schristos ld_r_n (struct buffer *buf, disassemble_info * info, const char *txt)
233*184b2d41Schristos {
234*184b2d41Schristos   char mytxt[TXTSIZ];
235*184b2d41Schristos 
236*184b2d41Schristos   snprintf (mytxt, TXTSIZ, txt, r_str[(buf->data[buf->n_fetch - 1] >> 3) & 7]);
237*184b2d41Schristos   return prt_n (buf, info, mytxt);
238*184b2d41Schristos }
239*184b2d41Schristos 
240*184b2d41Schristos static int
prt_r(struct buffer * buf,disassemble_info * info,const char * txt)241*184b2d41Schristos prt_r (struct buffer *buf, disassemble_info * info, const char *txt)
242a1ba9ba4Schristos {
243a1ba9ba4Schristos   info->fprintf_func (info->stream, txt,
244a1ba9ba4Schristos 		      r_str[(buf->data[buf->n_fetch - 1] >> 3) & 7]);
245a1ba9ba4Schristos   buf->n_used = buf->n_fetch;
246a1ba9ba4Schristos   return buf->n_used;
247a1ba9ba4Schristos }
248a1ba9ba4Schristos 
249a1ba9ba4Schristos static int
ld_r_r(struct buffer * buf,disassemble_info * info,const char * txt)250*184b2d41Schristos ld_r_r (struct buffer *buf, disassemble_info * info, const char *txt)
251a1ba9ba4Schristos {
252a1ba9ba4Schristos   info->fprintf_func (info->stream, txt,
253a1ba9ba4Schristos 		      r_str[(buf->data[buf->n_fetch - 1] >> 3) & 7],
254a1ba9ba4Schristos 		      r_str[buf->data[buf->n_fetch - 1] & 7]);
255a1ba9ba4Schristos   buf->n_used = buf->n_fetch;
256a1ba9ba4Schristos   return buf->n_used;
257a1ba9ba4Schristos }
258a1ba9ba4Schristos 
259a1ba9ba4Schristos static int
prt_d(struct buffer * buf,disassemble_info * info,const char * txt)260*184b2d41Schristos prt_d (struct buffer *buf, disassemble_info * info, const char *txt)
261a1ba9ba4Schristos {
262a1ba9ba4Schristos   int d;
263a1ba9ba4Schristos   signed char *p;
264a1ba9ba4Schristos 
265a1ba9ba4Schristos   p = buf->data + buf->n_fetch;
266a1ba9ba4Schristos 
267a1ba9ba4Schristos   if (fetch_data (buf, info, 1))
268a1ba9ba4Schristos     {
269a1ba9ba4Schristos       d = p[0];
270a1ba9ba4Schristos       info->fprintf_func (info->stream, txt, d);
271a1ba9ba4Schristos       buf->n_used = buf->n_fetch;
272a1ba9ba4Schristos     }
273a1ba9ba4Schristos   else
274a1ba9ba4Schristos     buf->n_used = -1;
275a1ba9ba4Schristos 
276a1ba9ba4Schristos   return buf->n_used;
277a1ba9ba4Schristos }
278a1ba9ba4Schristos 
279a1ba9ba4Schristos static int
prt_rr_d(struct buffer * buf,disassemble_info * info,const char * txt)280*184b2d41Schristos prt_rr_d (struct buffer *buf, disassemble_info * info, const char *txt)
281*184b2d41Schristos {
282*184b2d41Schristos   char mytxt[TXTSIZ];
283*184b2d41Schristos   int rr;
284*184b2d41Schristos 
285*184b2d41Schristos   rr = (buf->data[buf->n_fetch - 1] >> 4) & 3;
286*184b2d41Schristos   if (rr == 3) /* SP is not supported */
287*184b2d41Schristos     return 0;
288*184b2d41Schristos 
289*184b2d41Schristos   snprintf (mytxt, TXTSIZ, txt, rr_str[rr]);
290*184b2d41Schristos   return prt_d (buf, info, mytxt);
291*184b2d41Schristos }
292*184b2d41Schristos 
293*184b2d41Schristos static int
arit_r(struct buffer * buf,disassemble_info * info,const char * txt)294*184b2d41Schristos arit_r (struct buffer *buf, disassemble_info * info, const char *txt)
295*184b2d41Schristos {
296*184b2d41Schristos   const char * const *arit;
297*184b2d41Schristos 
298*184b2d41Schristos   if (buf->inss & INSS_EZ80)
299*184b2d41Schristos     arit = arit_str_ez80;
300*184b2d41Schristos   else if (buf->inss & INSS_GBZ80)
301*184b2d41Schristos     arit = arit_str_gbz80;
302*184b2d41Schristos   else
303*184b2d41Schristos     arit = arit_str;
304*184b2d41Schristos 
305*184b2d41Schristos   info->fprintf_func (info->stream, txt,
306*184b2d41Schristos                       arit[(buf->data[buf->n_fetch - 1] >> 3) & 7],
307*184b2d41Schristos                       r_str[buf->data[buf->n_fetch - 1] & 7]);
308*184b2d41Schristos   buf->n_used = buf->n_fetch;
309*184b2d41Schristos   return buf->n_used;
310*184b2d41Schristos }
311*184b2d41Schristos 
312*184b2d41Schristos static int
prt_cc(struct buffer * buf,disassemble_info * info,const char * txt)313*184b2d41Schristos prt_cc (struct buffer *buf, disassemble_info * info, const char *txt)
314*184b2d41Schristos {
315*184b2d41Schristos   info->fprintf_func (info->stream, "%s%s", txt,
316*184b2d41Schristos 		      cc_str[(buf->data[0] >> 3) & 7]);
317*184b2d41Schristos   buf->n_used = buf->n_fetch;
318*184b2d41Schristos   return buf->n_used;
319*184b2d41Schristos }
320*184b2d41Schristos 
321*184b2d41Schristos static int
pop_rr(struct buffer * buf,disassemble_info * info,const char * txt)322*184b2d41Schristos pop_rr (struct buffer *buf, disassemble_info * info, const char *txt)
323*184b2d41Schristos {
324*184b2d41Schristos   static char *rr_stack[] = { "bc","de","hl","af"};
325*184b2d41Schristos 
326*184b2d41Schristos   info->fprintf_func (info->stream, "%s %s", txt,
327*184b2d41Schristos 		      rr_stack[(buf->data[0] >> 4) & 3]);
328*184b2d41Schristos   buf->n_used = buf->n_fetch;
329*184b2d41Schristos   return buf->n_used;
330*184b2d41Schristos }
331*184b2d41Schristos 
332*184b2d41Schristos 
333*184b2d41Schristos static int
jp_cc_nn(struct buffer * buf,disassemble_info * info,const char * txt)334*184b2d41Schristos jp_cc_nn (struct buffer *buf, disassemble_info * info, const char *txt)
335*184b2d41Schristos {
336*184b2d41Schristos   char mytxt[TXTSIZ];
337*184b2d41Schristos 
338*184b2d41Schristos   snprintf (mytxt,TXTSIZ,
339*184b2d41Schristos 	    "%s%s,0x%%04x", txt, cc_str[(buf->data[0] >> 3) & 7]);
340*184b2d41Schristos   return prt_nn (buf, info, mytxt);
341*184b2d41Schristos }
342*184b2d41Schristos 
343*184b2d41Schristos static int
arit_n(struct buffer * buf,disassemble_info * info,const char * txt)344*184b2d41Schristos arit_n (struct buffer *buf, disassemble_info * info, const char *txt)
345*184b2d41Schristos {
346*184b2d41Schristos   char mytxt[TXTSIZ];
347*184b2d41Schristos   const char * const *arit;
348*184b2d41Schristos 
349*184b2d41Schristos   if (buf->inss & INSS_EZ80)
350*184b2d41Schristos     arit = arit_str_ez80;
351*184b2d41Schristos   else if (buf->inss & INSS_GBZ80)
352*184b2d41Schristos     arit = arit_str_gbz80;
353*184b2d41Schristos   else
354*184b2d41Schristos     arit = arit_str;
355*184b2d41Schristos 
356*184b2d41Schristos   snprintf (mytxt,TXTSIZ, txt, arit[(buf->data[0] >> 3) & 7]);
357*184b2d41Schristos   return prt_n (buf, info, mytxt);
358*184b2d41Schristos }
359*184b2d41Schristos 
360*184b2d41Schristos static int
rst(struct buffer * buf,disassemble_info * info,const char * txt)361*184b2d41Schristos rst (struct buffer *buf, disassemble_info * info, const char *txt)
362*184b2d41Schristos {
363*184b2d41Schristos   info->fprintf_func (info->stream, txt, buf->data[0] & 0x38);
364*184b2d41Schristos   buf->n_used = buf->n_fetch;
365*184b2d41Schristos   return buf->n_used;
366*184b2d41Schristos }
367*184b2d41Schristos 
368*184b2d41Schristos 
369*184b2d41Schristos static int
cis(struct buffer * buf,disassemble_info * info,const char * txt ATTRIBUTE_UNUSED)370*184b2d41Schristos cis (struct buffer *buf, disassemble_info * info, const char *txt ATTRIBUTE_UNUSED)
371*184b2d41Schristos {
372*184b2d41Schristos   static char * opar[] = { "ld", "cp", "in", "out" };
373*184b2d41Schristos   char * op;
374*184b2d41Schristos   char c;
375*184b2d41Schristos 
376*184b2d41Schristos   c = buf->data[1];
377*184b2d41Schristos   op = ((0x13 & c) == 0x13) ? "ot" : (opar[c & 3]);
378*184b2d41Schristos   info->fprintf_func (info->stream,
379*184b2d41Schristos 		      "%s%c%s", op,
380*184b2d41Schristos 		      (c & 0x08) ? 'd' : 'i',
381*184b2d41Schristos 		      (c & 0x10) ? "r" : "");
382*184b2d41Schristos   buf->n_used = 2;
383*184b2d41Schristos   return buf->n_used;
384*184b2d41Schristos }
385*184b2d41Schristos 
386*184b2d41Schristos static int
cism(struct buffer * buf,disassemble_info * info,const char * txt ATTRIBUTE_UNUSED)387*184b2d41Schristos cism (struct buffer *buf, disassemble_info * info, const char *txt ATTRIBUTE_UNUSED)
388*184b2d41Schristos {
389*184b2d41Schristos   static char * opar[] = { "in%cm%s", "ot%cm%s" };
390*184b2d41Schristos   char * op;
391*184b2d41Schristos   char c;
392*184b2d41Schristos 
393*184b2d41Schristos   c = buf->data[1];
394*184b2d41Schristos   op = opar[c & 1];
395*184b2d41Schristos   info->fprintf_func (info->stream,
396*184b2d41Schristos                       op,
397*184b2d41Schristos                       (c & 0x08) ? 'd' : 'i',
398*184b2d41Schristos                       (c & 0x10) ? "r" : "");
399*184b2d41Schristos   buf->n_used = 2;
400*184b2d41Schristos   return buf->n_used;
401*184b2d41Schristos }
402*184b2d41Schristos 
403*184b2d41Schristos static int
dump(struct buffer * buf,disassemble_info * info,const char * txt)404*184b2d41Schristos dump (struct buffer *buf, disassemble_info * info, const char *txt)
405*184b2d41Schristos {
406*184b2d41Schristos   int i;
407*184b2d41Schristos 
408*184b2d41Schristos   info->fprintf_func (info->stream, "defb ");
409*184b2d41Schristos   for (i = 0; txt[i]; ++i)
410*184b2d41Schristos     info->fprintf_func (info->stream, i ? ", 0x%02x" : "0x%02x",
411*184b2d41Schristos 			(unsigned char) buf->data[i]);
412*184b2d41Schristos   buf->n_used = i;
413*184b2d41Schristos   return buf->n_used;
414*184b2d41Schristos }
415*184b2d41Schristos 
416*184b2d41Schristos /* Table to disassemble machine codes with prefix 0xED.  */
417*184b2d41Schristos struct tab_elt opc_ed[] =
418*184b2d41Schristos {
419*184b2d41Schristos   { 0x30, 0xFF, prt, "mul d,e", INSS_Z80N },
420*184b2d41Schristos   { 0x31, 0xFF, prt, "add hl,a", INSS_Z80N },
421*184b2d41Schristos   { 0x31, 0xFF, prt, "ld iy,(hl)", INSS_EZ80 },
422*184b2d41Schristos   { 0x30, 0xFE, dump, "xx", INSS_ALL }, /* do not move this line */
423*184b2d41Schristos   { 0x00, 0xC7, prt_r_n, "in0 %s,(0x%%02x)", INSS_Z180|INSS_EZ80 },
424*184b2d41Schristos   { 0x01, 0xC7, prt_r_n, "out0 (0x%%02x),%s", INSS_Z180|INSS_EZ80 },
425*184b2d41Schristos   { 0x32, 0xFF, prt_d, "lea ix,ix%+d", INSS_EZ80 },
426*184b2d41Schristos   { 0x33, 0xFF, prt_d, "lea iy,iy%+d", INSS_EZ80 },
427*184b2d41Schristos   { 0x02, 0xCF, prt_rr_d, "lea %s,ix%%+d", INSS_EZ80 },
428*184b2d41Schristos   { 0x03, 0xCF, prt_rr_d, "lea %s,iy%%+d", INSS_EZ80 },
429*184b2d41Schristos   { 0x04, 0xC7, prt_r, "tst %s", INSS_Z180},
430*184b2d41Schristos   { 0x04, 0xC7, prt_r, "tst a,%s", INSS_EZ80 },
431*184b2d41Schristos   { 0x07, 0xFF, prt, "ld bc,(hl)", INSS_EZ80 },
432*184b2d41Schristos   { 0x3F, 0xFF, prt, "ld (hl),ix", INSS_EZ80 },
433*184b2d41Schristos   { 0x0F, 0xCF, prt_rr, "ld (hl),", INSS_EZ80 },
434*184b2d41Schristos   { 0x17, 0xFF, prt, "ld de,(hl)", INSS_EZ80 },
435*184b2d41Schristos   { 0x23, 0xFF, prt, "swapnib", INSS_Z80N },
436*184b2d41Schristos   { 0x24, 0xFF, prt, "mirror", INSS_Z80N },
437*184b2d41Schristos   { 0x27, 0xFF, prt, "ld hl,(hl)", INSS_EZ80 },
438*184b2d41Schristos   { 0x27, 0xFF, prt_n, "test 0x%02x", INSS_Z80N },
439*184b2d41Schristos   { 0x28, 0xFF, prt, "bsla de,b", INSS_Z80N },
440*184b2d41Schristos   { 0x29, 0xFF, prt, "bsra de,b", INSS_Z80N },
441*184b2d41Schristos   { 0x2A, 0xFF, prt, "bsrl de,b", INSS_Z80N },
442*184b2d41Schristos   { 0x2B, 0xFF, prt, "bsrf de,b", INSS_Z80N },
443*184b2d41Schristos   { 0x2C, 0xFF, prt, "bslc de,b", INSS_Z80N },
444*184b2d41Schristos   { 0x32, 0xFF, prt, "add de,a", INSS_Z80N },
445*184b2d41Schristos   { 0x33, 0xFF, prt, "add bc,a", INSS_Z80N },
446*184b2d41Schristos   { 0x34, 0xFF, prt_nn, "add hl,0x%04x", INSS_Z80N },
447*184b2d41Schristos   { 0x35, 0xFF, prt_nn, "add de,0x%04x", INSS_Z80N },
448*184b2d41Schristos   { 0x36, 0xFF, prt_nn, "add bc,0x%04x", INSS_Z80N },
449*184b2d41Schristos   { 0x37, 0xFF, prt, "ld ix,(hl)", INSS_EZ80 },
450*184b2d41Schristos   { 0x3E, 0xFF, prt, "ld (hl),iy", INSS_EZ80 },
451*184b2d41Schristos   { 0x70, 0xFF, prt, "in f,(c)", INSS_Z80 | INSS_R800 | INSS_Z80N },
452*184b2d41Schristos   { 0x70, 0xFF, dump, "xx", INSS_ALL },
453*184b2d41Schristos   { 0x40, 0xC7, prt_r, "in %s,(bc)", INSS_EZ80 },
454*184b2d41Schristos   { 0x40, 0xC7, prt_r, "in %s,(c)", INSS_ALL },
455*184b2d41Schristos   { 0x71, 0xFF, prt, "out (c),0", INSS_Z80 | INSS_Z80N },
456*184b2d41Schristos   { 0x71, 0xFF, dump, "xx", INSS_ALL },
457*184b2d41Schristos   { 0x41, 0xC7, prt_r, "out (bc),%s", INSS_EZ80 },
458*184b2d41Schristos   { 0x41, 0xC7, prt_r, "out (c),%s", INSS_ALL },
459*184b2d41Schristos   { 0x42, 0xCF, prt_rr, "sbc hl,", INSS_ALL },
460*184b2d41Schristos   { 0x43, 0xCF, prt_rr_nn, "ld (0x%%04x),%s", INSS_ALL },
461*184b2d41Schristos   { 0x44, 0xFF, prt, "neg", INSS_ALL },
462*184b2d41Schristos   { 0x45, 0xFF, prt, "retn", INSS_ALL },
463*184b2d41Schristos   { 0x46, 0xFF, prt, "im 0", INSS_ALL },
464*184b2d41Schristos   { 0x47, 0xFF, prt, "ld i,a", INSS_ALL },
465*184b2d41Schristos   { 0x4A, 0xCF, prt_rr, "adc hl,", INSS_ALL },
466*184b2d41Schristos   { 0x4B, 0xCF, prt_rr_nn, "ld %s,(0x%%04x)", INSS_ALL },
467*184b2d41Schristos   { 0x4C, 0xCF, prt_rr, "mlt ", INSS_Z180|INSS_EZ80 },
468*184b2d41Schristos   { 0x4D, 0xFF, prt, "reti", INSS_ALL },
469*184b2d41Schristos   { 0x4F, 0xFF, prt, "ld r,a", INSS_ALL },
470*184b2d41Schristos   { 0x54, 0xFF, prt_d, "lea ix,iy%+d", INSS_EZ80 },
471*184b2d41Schristos   { 0x55, 0xFF, prt_d, "lea iy,ix%+d", INSS_EZ80 },
472*184b2d41Schristos   { 0x56, 0xFF, prt, "im 1", INSS_ALL },
473*184b2d41Schristos   { 0x57, 0xFF, prt, "ld a,i", INSS_ALL },
474*184b2d41Schristos   { 0x5E, 0xFF, prt, "im 2", INSS_ALL },
475*184b2d41Schristos   { 0x5F, 0xFF, prt, "ld a,r", INSS_ALL },
476*184b2d41Schristos   { 0x64, 0xFF, prt_n, "tst 0x%02x", INSS_Z180 },
477*184b2d41Schristos   { 0x64, 0xFF, prt_n, "tst a,0x%02x", INSS_EZ80 },
478*184b2d41Schristos   { 0x65, 0xFF, prt_d, "pea ix%+d", INSS_EZ80 },
479*184b2d41Schristos   { 0x66, 0xFF, prt_d, "pea iy%+d", INSS_EZ80 },
480*184b2d41Schristos   { 0x67, 0xFF, prt, "rrd", INSS_ALL },
481*184b2d41Schristos   { 0x6D, 0xFF, prt, "ld mb,a", INSS_EZ80 },
482*184b2d41Schristos   { 0x6E, 0xFF, prt, "ld a,mb", INSS_EZ80 },
483*184b2d41Schristos   { 0x6F, 0xFF, prt, "rld", INSS_ALL },
484*184b2d41Schristos   { 0x74, 0xFF, prt_n, "tstio 0x%02x", INSS_Z180|INSS_EZ80 },
485*184b2d41Schristos   { 0x76, 0xFF, prt, "slp", INSS_Z180|INSS_EZ80 },
486*184b2d41Schristos   { 0x7D, 0xFF, prt, "stmix", INSS_EZ80 },
487*184b2d41Schristos   { 0x7E, 0xFF, prt, "rsmix", INSS_EZ80 },
488*184b2d41Schristos   { 0x82, 0xE6, cism, "", INSS_Z180|INSS_EZ80 },
489*184b2d41Schristos   { 0x84, 0xFF, prt, "ini2", INSS_EZ80 },
490*184b2d41Schristos   { 0x8A, 0xFF, prt_n_n, "push 0x%02x%%02x", INSS_Z80N },
491*184b2d41Schristos   { 0x8C, 0xFF, prt, "ind2", INSS_EZ80 },
492*184b2d41Schristos   { 0x90, 0xFF, prt, "outinb", INSS_Z80N },
493*184b2d41Schristos   { 0x91, 0xFF, prt_n_n, "nextreg 0x%02x,0x%%02x", INSS_Z80N },
494*184b2d41Schristos   { 0x92, 0xFF, prt_n, "nextreg 0x%02x,a", INSS_Z80N },
495*184b2d41Schristos   { 0x93, 0xFF, prt, "pixeldn", INSS_Z80N },
496*184b2d41Schristos   { 0x94, 0xFF, prt, "ini2r", INSS_EZ80 },
497*184b2d41Schristos   { 0x94, 0xFF, prt, "pixelad", INSS_Z80N },
498*184b2d41Schristos   { 0x95, 0xFF, prt, "setae", INSS_Z80N },
499*184b2d41Schristos   { 0x98, 0xFF, prt, "jp (c)", INSS_Z80N },
500*184b2d41Schristos   { 0x9c, 0xFF, prt, "ind2r", INSS_EZ80 },
501*184b2d41Schristos   { 0xA0, 0xE4, cis, "", INSS_ALL },
502*184b2d41Schristos   { 0xA4, 0xFF, prt, "outi2", INSS_EZ80 },
503*184b2d41Schristos   { 0xA4, 0xFF, prt, "ldix", INSS_Z80N },
504*184b2d41Schristos   { 0xAC, 0xFF, prt, "outd2", INSS_EZ80 },
505*184b2d41Schristos   { 0xAC, 0xFF, prt, "lddx", INSS_Z80N },
506*184b2d41Schristos   { 0xA5, 0xFF, prt, "ldws", INSS_Z80N },
507*184b2d41Schristos   { 0xB4, 0xFF, prt, "oti2r", INSS_EZ80 },
508*184b2d41Schristos   { 0xB4, 0xFF, prt, "ldirx", INSS_Z80N },
509*184b2d41Schristos   { 0xB7, 0xFF, prt, "ldpirx", INSS_Z80N },
510*184b2d41Schristos   { 0xBC, 0xFF, prt, "otd2r", INSS_EZ80 },
511*184b2d41Schristos   { 0xBC, 0xFF, prt, "lddrx", INSS_Z80N },
512*184b2d41Schristos   { 0xC2, 0xFF, prt, "inirx", INSS_EZ80 },
513*184b2d41Schristos   { 0xC3, 0xFF, prt, "otirx", INSS_EZ80 },
514*184b2d41Schristos   { 0xC7, 0xFF, prt, "ld i,hl", INSS_EZ80 },
515*184b2d41Schristos   { 0xCA, 0xFF, prt, "indrx", INSS_EZ80 },
516*184b2d41Schristos   { 0xCB, 0xFF, prt, "otdrx", INSS_EZ80 },
517*184b2d41Schristos   { 0xC3, 0xFF, prt, "muluw hl,bc", INSS_R800 },
518*184b2d41Schristos   { 0xC5, 0xE7, prt_r, "mulub a,%s", INSS_R800 },
519*184b2d41Schristos   { 0xD7, 0xFF, prt, "ld hl,i", INSS_EZ80 },
520*184b2d41Schristos   { 0xF3, 0xFF, prt, "muluw hl,sp", INSS_R800 },
521*184b2d41Schristos   { 0x00, 0x00, dump, "xx", INSS_ALL }
522*184b2d41Schristos };
523*184b2d41Schristos 
524*184b2d41Schristos static int
pref_ed(struct buffer * buf,disassemble_info * info,const char * txt ATTRIBUTE_UNUSED)525*184b2d41Schristos pref_ed (struct buffer *buf, disassemble_info *info,
526*184b2d41Schristos          const char *txt ATTRIBUTE_UNUSED)
527*184b2d41Schristos {
528*184b2d41Schristos   struct tab_elt *p;
529*184b2d41Schristos 
530*184b2d41Schristos   if (fetch_data (buf, info, 1))
531*184b2d41Schristos     {
532*184b2d41Schristos       for (p = opc_ed; p->val != (buf->data[1] & p->mask) || !mach_inst (buf, p); ++p)
533*184b2d41Schristos         ;
534*184b2d41Schristos       p->fp (buf, info, p->text);
535*184b2d41Schristos     }
536*184b2d41Schristos   else
537*184b2d41Schristos     buf->n_used = -1;
538*184b2d41Schristos 
539*184b2d41Schristos   return buf->n_used;
540*184b2d41Schristos }
541*184b2d41Schristos 
542*184b2d41Schristos /* Instruction names for the instructions addressing single bits.  */
543*184b2d41Schristos static char *cb1_str[] = { "", "bit", "res", "set"};
544*184b2d41Schristos /* Instruction names for shifts and rotates.  */
545*184b2d41Schristos static char *cb2_str[] =
546*184b2d41Schristos {
547*184b2d41Schristos   "rlc", "rrc", "rl", "rr", "sla", "sra", "sli", "srl"
548*184b2d41Schristos };
549*184b2d41Schristos 
550*184b2d41Schristos static int
pref_cb(struct buffer * buf,disassemble_info * info,const char * txt ATTRIBUTE_UNUSED)551*184b2d41Schristos pref_cb (struct buffer *buf, disassemble_info *info,
552*184b2d41Schristos          const char *txt ATTRIBUTE_UNUSED)
553*184b2d41Schristos {
554*184b2d41Schristos   const char *op_txt;
555*184b2d41Schristos   int idx;
556*184b2d41Schristos   if (fetch_data (buf, info, 1))
557*184b2d41Schristos     {
558*184b2d41Schristos       buf->n_used = 2;
559*184b2d41Schristos       if ((buf->data[1] & 0xc0) == 0)
560*184b2d41Schristos         {
561*184b2d41Schristos           idx = (buf->data[1] >> 3) & 7;
562*184b2d41Schristos           if ((buf->inss & INSS_GBZ80) && (idx == 6))
563*184b2d41Schristos             op_txt = "swap";
564*184b2d41Schristos           else
565*184b2d41Schristos             op_txt = cb2_str[idx];
566*184b2d41Schristos           info->fprintf_func (info->stream, "%s %s",
567*184b2d41Schristos                               op_txt,
568*184b2d41Schristos                               r_str[buf->data[1] & 7]);
569*184b2d41Schristos         }
570*184b2d41Schristos       else
571*184b2d41Schristos 	info->fprintf_func (info->stream, "%s %d,%s",
572*184b2d41Schristos 			    cb1_str[(buf->data[1] >> 6) & 3],
573*184b2d41Schristos 			    (buf->data[1] >> 3) & 7,
574*184b2d41Schristos 			    r_str[buf->data[1] & 7]);
575*184b2d41Schristos     }
576*184b2d41Schristos   else
577*184b2d41Schristos     buf->n_used = -1;
578*184b2d41Schristos 
579*184b2d41Schristos   return buf->n_used;
580*184b2d41Schristos }
581*184b2d41Schristos 
582*184b2d41Schristos static int
addvv(struct buffer * buf,disassemble_info * info,const char * txt)583*184b2d41Schristos addvv (struct buffer * buf, disassemble_info * info, const char *txt)
584*184b2d41Schristos {
585*184b2d41Schristos   info->fprintf_func (info->stream, "add %s,%s", txt, txt);
586*184b2d41Schristos 
587*184b2d41Schristos   return buf->n_used = buf->n_fetch;
588*184b2d41Schristos }
589*184b2d41Schristos 
590*184b2d41Schristos static int
ld_v_v(struct buffer * buf,disassemble_info * info,const char * txt)591*184b2d41Schristos ld_v_v (struct buffer * buf, disassemble_info * info, const char *txt)
592*184b2d41Schristos {
593*184b2d41Schristos   char mytxt[TXTSIZ];
594*184b2d41Schristos 
595*184b2d41Schristos   snprintf (mytxt, TXTSIZ, "ld %s%%s,%s%%s", txt, txt);
596*184b2d41Schristos   return ld_r_r (buf, info, mytxt);
597*184b2d41Schristos }
598*184b2d41Schristos 
599*184b2d41Schristos static int
prt_d_n(struct buffer * buf,disassemble_info * info,const char * txt)600*184b2d41Schristos prt_d_n (struct buffer *buf, disassemble_info * info, const char *txt)
601a1ba9ba4Schristos {
602a1ba9ba4Schristos   char mytxt[TXTSIZ];
603a1ba9ba4Schristos   int d;
604a1ba9ba4Schristos   signed char *p;
605a1ba9ba4Schristos 
606a1ba9ba4Schristos   p = buf->data + buf->n_fetch;
607a1ba9ba4Schristos 
608a1ba9ba4Schristos   if (fetch_data (buf, info, 1))
609a1ba9ba4Schristos     {
610a1ba9ba4Schristos       d = p[0];
611a1ba9ba4Schristos       snprintf (mytxt, TXTSIZ, txt, d);
612a1ba9ba4Schristos       return prt_n (buf, info, mytxt);
613a1ba9ba4Schristos     }
614a1ba9ba4Schristos   else
615a1ba9ba4Schristos     buf->n_used = -1;
616a1ba9ba4Schristos 
617a1ba9ba4Schristos   return buf->n_used;
618a1ba9ba4Schristos }
619a1ba9ba4Schristos 
620a1ba9ba4Schristos static int
arit_d(struct buffer * buf,disassemble_info * info,const char * txt)621*184b2d41Schristos arit_d (struct buffer *buf, disassemble_info * info, const char *txt)
622a1ba9ba4Schristos {
623a1ba9ba4Schristos   char mytxt[TXTSIZ];
624a1ba9ba4Schristos   signed char c;
625*184b2d41Schristos   const char * const *arit;
626a1ba9ba4Schristos 
627*184b2d41Schristos   arit = (buf->inss & INSS_EZ80) ? arit_str_ez80 : arit_str;
628a1ba9ba4Schristos   c = buf->data[buf->n_fetch - 1];
629*184b2d41Schristos   snprintf (mytxt, TXTSIZ, txt, arit[(c >> 3) & 7]);
630a1ba9ba4Schristos   return prt_d (buf, info, mytxt);
631a1ba9ba4Schristos }
632a1ba9ba4Schristos 
633a1ba9ba4Schristos static int
ld_r_d(struct buffer * buf,disassemble_info * info,const char * txt)634*184b2d41Schristos ld_r_d (struct buffer *buf, disassemble_info * info, const char *txt)
635a1ba9ba4Schristos {
636a1ba9ba4Schristos   char mytxt[TXTSIZ];
637a1ba9ba4Schristos   signed char c;
638a1ba9ba4Schristos 
639a1ba9ba4Schristos   c = buf->data[buf->n_fetch - 1];
640a1ba9ba4Schristos   snprintf (mytxt, TXTSIZ, txt, r_str[(c >> 3) & 7]);
641a1ba9ba4Schristos   return prt_d (buf, info, mytxt);
642a1ba9ba4Schristos }
643a1ba9ba4Schristos 
644a1ba9ba4Schristos static int
ld_d_r(struct buffer * buf,disassemble_info * info,const char * txt)645*184b2d41Schristos ld_d_r (struct buffer *buf, disassemble_info * info, const char *txt)
646a1ba9ba4Schristos {
647a1ba9ba4Schristos   char mytxt[TXTSIZ];
648a1ba9ba4Schristos   signed char c;
649a1ba9ba4Schristos 
650a1ba9ba4Schristos   c = buf->data[buf->n_fetch - 1];
651a1ba9ba4Schristos   snprintf (mytxt, TXTSIZ, txt, r_str[c & 7]);
652a1ba9ba4Schristos   return prt_d (buf, info, mytxt);
653a1ba9ba4Schristos }
654a1ba9ba4Schristos 
655a1ba9ba4Schristos static int
ld_ii_ii(struct buffer * buf,disassemble_info * info,const char * txt)656*184b2d41Schristos ld_ii_ii (struct buffer *buf, disassemble_info * info, const char *txt)
657*184b2d41Schristos {
658*184b2d41Schristos   char mytxt[TXTSIZ];
659*184b2d41Schristos   signed char c;
660*184b2d41Schristos   int p;
661*184b2d41Schristos   static const char *ii[2] = { "ix", "iy" };
662*184b2d41Schristos 
663*184b2d41Schristos   p = (buf->data[buf->n_fetch - 2] == (signed char) 0xdd) ? 0 : 1;
664*184b2d41Schristos   c = buf->data[buf->n_fetch - 1];
665*184b2d41Schristos   if ((c & 0x07) != 0x07)
666*184b2d41Schristos     p = 1 - p; /* 0 -> 1, 1 -> 0 */
667*184b2d41Schristos   snprintf (mytxt, TXTSIZ, txt, ii[p]);
668*184b2d41Schristos   return prt_d (buf, info, mytxt);
669*184b2d41Schristos }
670*184b2d41Schristos 
671*184b2d41Schristos static int
pref_xd_cb(struct buffer * buf,disassemble_info * info,const char * txt)672*184b2d41Schristos pref_xd_cb (struct buffer * buf, disassemble_info * info, const char *txt)
673a1ba9ba4Schristos {
674a1ba9ba4Schristos   if (fetch_data (buf, info, 2))
675a1ba9ba4Schristos     {
676a1ba9ba4Schristos       int d;
677a1ba9ba4Schristos       char arg[TXTSIZ];
678a1ba9ba4Schristos       signed char *p;
679a1ba9ba4Schristos 
680a1ba9ba4Schristos       buf->n_used = 4;
681a1ba9ba4Schristos       p = buf->data;
682a1ba9ba4Schristos       d = p[2];
683a1ba9ba4Schristos 
684a1ba9ba4Schristos       if (((p[3] & 0xC0) == 0x40) || ((p[3] & 7) == 0x06))
685a1ba9ba4Schristos 	snprintf (arg, TXTSIZ, "(%s%+d)", txt, d);
686a1ba9ba4Schristos       else
687a1ba9ba4Schristos 	snprintf (arg, TXTSIZ, "(%s%+d),%s", txt, d, r_str[p[3] & 7]);
688a1ba9ba4Schristos 
689a1ba9ba4Schristos       if ((p[3] & 0xc0) == 0)
690a1ba9ba4Schristos 	info->fprintf_func (info->stream, "%s %s",
691a1ba9ba4Schristos 			    cb2_str[(buf->data[3] >> 3) & 7],
692a1ba9ba4Schristos 			    arg);
693a1ba9ba4Schristos       else
694a1ba9ba4Schristos 	info->fprintf_func (info->stream, "%s %d,%s",
695a1ba9ba4Schristos 			    cb1_str[(buf->data[3] >> 6) & 3],
696a1ba9ba4Schristos 			    (buf->data[3] >> 3) & 7,
697a1ba9ba4Schristos 			    arg);
698a1ba9ba4Schristos     }
699a1ba9ba4Schristos   else
700a1ba9ba4Schristos     buf->n_used = -1;
701a1ba9ba4Schristos 
702a1ba9ba4Schristos   return buf->n_used;
703a1ba9ba4Schristos }
704a1ba9ba4Schristos 
705a1ba9ba4Schristos /* Table to disassemble machine codes with prefix 0xDD or 0xFD.  */
706a1ba9ba4Schristos static struct tab_elt opc_ind[] =
707a1ba9ba4Schristos {
708*184b2d41Schristos   { 0x07, 0xFF, prt_d, "ld bc,(%s%%+d)", INSS_EZ80 },
709*184b2d41Schristos   { 0x0F, 0xFF, prt_d, "ld (%s%%+d),bc", INSS_EZ80 },
710*184b2d41Schristos   { 0x17, 0xFF, prt_d, "ld de,(%s%%+d)", INSS_EZ80 },
711*184b2d41Schristos   { 0x1F, 0xFF, prt_d, "ld (%s%%+d),de", INSS_EZ80 },
712*184b2d41Schristos   { 0x24, 0xF7, prt_r, "inc %s%%s", INSS_ALL },
713*184b2d41Schristos   { 0x25, 0xF7, prt_r, "dec %s%%s", INSS_ALL },
714*184b2d41Schristos   { 0x26, 0xF7, ld_r_n, "ld %s%%s,0x%%%%02x", INSS_ALL },
715*184b2d41Schristos   { 0x27, 0xFF, prt_d, "ld hl,(%s%%+d)", INSS_EZ80 },
716*184b2d41Schristos   { 0x2F, 0xFF, prt_d, "ld (%s%%+d),hl", INSS_EZ80 },
717*184b2d41Schristos   { 0x21, 0xFF, prt_nn, "ld %s,0x%%04x", INSS_ALL },
718*184b2d41Schristos   { 0x22, 0xFF, prt_nn, "ld (0x%%04x),%s", INSS_ALL },
719*184b2d41Schristos   { 0x2A, 0xFF, prt_nn, "ld %s,(0x%%04x)", INSS_ALL },
720*184b2d41Schristos   { 0x23, 0xFF, prt, "inc %s", INSS_ALL },
721*184b2d41Schristos   { 0x2B, 0xFF, prt, "dec %s", INSS_ALL },
722*184b2d41Schristos   { 0x29, 0xFF, addvv, "%s", INSS_ALL },
723*184b2d41Schristos   { 0x31, 0xFF, ld_ii_ii, "ld %%s,(%s%%%%+d)", INSS_EZ80 },
724*184b2d41Schristos   { 0x37, 0xFF, ld_ii_ii, "ld %%s,(%s%%%%+d)", INSS_EZ80 },
725*184b2d41Schristos   { 0x3E, 0xFE, ld_ii_ii, "ld (%s%%%%+d),%%s", INSS_EZ80 },
726*184b2d41Schristos   { 0x09, 0xCF, prt_rr, "add %s,", INSS_ALL },
727*184b2d41Schristos   { 0x34, 0xFF, prt_d, "inc (%s%%+d)", INSS_ALL },
728*184b2d41Schristos   { 0x35, 0xFF, prt_d, "dec (%s%%+d)", INSS_ALL },
729*184b2d41Schristos   { 0x36, 0xFF, prt_d_n, "ld (%s%%+d),0x%%%%02x", INSS_ALL },
730a1ba9ba4Schristos 
731*184b2d41Schristos   { 0x76, 0xFF, dump, "h", INSS_ALL },
732*184b2d41Schristos   { 0x46, 0xC7, ld_r_d, "ld %%s,(%s%%%%+d)", INSS_ALL },
733*184b2d41Schristos   { 0x70, 0xF8, ld_d_r, "ld (%s%%%%+d),%%s", INSS_ALL },
734*184b2d41Schristos   { 0x64, 0xF6, ld_v_v, "%s", INSS_ALL },
735*184b2d41Schristos   { 0x60, 0xF0, ld_r_r, "ld %s%%s,%%s", INSS_ALL },
736*184b2d41Schristos   { 0x44, 0xC6, ld_r_r, "ld %%s,%s%%s", INSS_ALL },
737a1ba9ba4Schristos 
738*184b2d41Schristos   { 0x86, 0xC7, arit_d, "%%s(%s%%%%+d)", INSS_ALL },
739*184b2d41Schristos   { 0x84, 0xC6, arit_r, "%%s%s%%s", INSS_ALL },
740a1ba9ba4Schristos 
741*184b2d41Schristos   { 0xE1, 0xFF, prt, "pop %s", INSS_ALL },
742*184b2d41Schristos   { 0xE5, 0xFF, prt, "push %s", INSS_ALL },
743*184b2d41Schristos   { 0xCB, 0xFF, pref_xd_cb, "%s", INSS_ALL },
744*184b2d41Schristos   { 0xE3, 0xFF, prt, "ex (sp),%s", INSS_ALL },
745*184b2d41Schristos   { 0xE9, 0xFF, prt, "jp (%s)", INSS_ALL },
746*184b2d41Schristos   { 0xF9, 0xFF, prt, "ld sp,%s", INSS_ALL },
747*184b2d41Schristos   { 0x00, 0x00, dump, "?", INSS_ALL },
748a1ba9ba4Schristos } ;
749a1ba9ba4Schristos 
750a1ba9ba4Schristos static int
pref_ind(struct buffer * buf,disassemble_info * info,const char * txt)751*184b2d41Schristos pref_ind (struct buffer *buf, disassemble_info *info, const char *txt)
752a1ba9ba4Schristos {
753a1ba9ba4Schristos   if (fetch_data (buf, info, 1))
754a1ba9ba4Schristos     {
755a1ba9ba4Schristos       char mytxt[TXTSIZ];
756a1ba9ba4Schristos       struct tab_elt *p;
757a1ba9ba4Schristos 
758*184b2d41Schristos       for (p = opc_ind; p->val != (buf->data[1] & p->mask) || !mach_inst (buf, p); ++p)
759a1ba9ba4Schristos         ;
760a1ba9ba4Schristos       snprintf (mytxt, TXTSIZ, p->text, txt);
761a1ba9ba4Schristos       p->fp (buf, info, mytxt);
762a1ba9ba4Schristos     }
763a1ba9ba4Schristos   else
764a1ba9ba4Schristos     buf->n_used = -1;
765a1ba9ba4Schristos 
766a1ba9ba4Schristos   return buf->n_used;
767a1ba9ba4Schristos }
768a1ba9ba4Schristos 
769*184b2d41Schristos static int
770*184b2d41Schristos print_insn_z80_buf (struct buffer *buf, disassemble_info *info);
771*184b2d41Schristos 
772*184b2d41Schristos static int
suffix(struct buffer * buf,disassemble_info * info,const char * txt)773*184b2d41Schristos suffix (struct buffer *buf, disassemble_info *info, const char *txt)
774a1ba9ba4Schristos {
775*184b2d41Schristos   char mybuf[TXTSIZ*4];
776*184b2d41Schristos   fprintf_ftype old_fprintf;
777*184b2d41Schristos   void *old_stream;
778*184b2d41Schristos   char *p;
779a1ba9ba4Schristos 
780*184b2d41Schristos   switch (txt[2])
781*184b2d41Schristos     {
782*184b2d41Schristos     case 'l': /* SIL or LIL */
783*184b2d41Schristos       buf->nn_len = 3;
784*184b2d41Schristos       break;
785*184b2d41Schristos     case 's': /* SIS or LIS */
786*184b2d41Schristos       buf->nn_len = 2;
787*184b2d41Schristos       break;
788*184b2d41Schristos     default:
789*184b2d41Schristos       abort ();
790*184b2d41Schristos     }
791*184b2d41Schristos   if (!fetch_data (buf, info, 1)
792*184b2d41Schristos       || buf->data[1] == 0x40
793*184b2d41Schristos       || buf->data[1] == 0x49
794*184b2d41Schristos       || buf->data[1] == 0x52
795*184b2d41Schristos       || buf->data[1] == 0x5b)
796*184b2d41Schristos     {
797*184b2d41Schristos       /* Double prefix, or end of data.  */
798*184b2d41Schristos       info->fprintf_func (info->stream, ".db 0x%02x ; %s", (unsigned)buf->data[0], txt);
799*184b2d41Schristos       buf->n_used = 1;
800*184b2d41Schristos       return buf->n_used;
801*184b2d41Schristos     }
802a1ba9ba4Schristos 
803*184b2d41Schristos   old_fprintf = info->fprintf_func;
804*184b2d41Schristos   old_stream = info->stream;
805*184b2d41Schristos   info->fprintf_func = (fprintf_ftype) &sprintf;
806*184b2d41Schristos   info->stream = mybuf;
807*184b2d41Schristos   mybuf[0] = 0;
808*184b2d41Schristos   buf->base++;
809*184b2d41Schristos   if (print_insn_z80_buf (buf, info) >= 0)
810*184b2d41Schristos     buf->n_used++;
811*184b2d41Schristos   info->fprintf_func = old_fprintf;
812*184b2d41Schristos   info->stream = old_stream;
813a1ba9ba4Schristos 
814*184b2d41Schristos   for (p = mybuf; *p; ++p)
815*184b2d41Schristos     if (*p == ' ')
816*184b2d41Schristos       break;
817*184b2d41Schristos   if (*p)
818*184b2d41Schristos     {
819*184b2d41Schristos       *p++ = '\0';
820*184b2d41Schristos       info->fprintf_func (info->stream, "%s.%s %s", mybuf, txt, p);
821*184b2d41Schristos     }
822*184b2d41Schristos   else
823*184b2d41Schristos     info->fprintf_func (info->stream, "%s.%s", mybuf, txt);
824*184b2d41Schristos   return buf->n_used;
825*184b2d41Schristos }
826*184b2d41Schristos 
827*184b2d41Schristos /* Table to disassemble machine codes without prefix.  */
828*184b2d41Schristos static const struct tab_elt
829*184b2d41Schristos opc_main[] =
830*184b2d41Schristos {
831*184b2d41Schristos   { 0x00, 0xFF, prt, "nop", INSS_ALL },
832*184b2d41Schristos   { 0x01, 0xCF, prt_rr_nn, "ld %s,0x%%04x", INSS_ALL },
833*184b2d41Schristos   { 0x02, 0xFF, prt, "ld (bc),a", INSS_ALL },
834*184b2d41Schristos   { 0x03, 0xCF, prt_rr, "inc ", INSS_ALL },
835*184b2d41Schristos   { 0x04, 0xC7, prt_r, "inc %s", INSS_ALL },
836*184b2d41Schristos   { 0x05, 0xC7, prt_r, "dec %s", INSS_ALL },
837*184b2d41Schristos   { 0x06, 0xC7, ld_r_n, "ld %s,0x%%02x", INSS_ALL },
838*184b2d41Schristos   { 0x07, 0xFF, prt, "rlca", INSS_ALL },
839*184b2d41Schristos   { 0x08, 0xFF, prt, "ex af,af'", ~INSS_GBZ80 },
840*184b2d41Schristos   { 0x09, 0xCF, prt_rr, "add hl,", INSS_ALL },
841*184b2d41Schristos   { 0x0A, 0xFF, prt, "ld a,(bc)", INSS_ALL },
842*184b2d41Schristos   { 0x0B, 0xCF, prt_rr, "dec ", INSS_ALL },
843*184b2d41Schristos   { 0x0F, 0xFF, prt, "rrca", INSS_ALL },
844*184b2d41Schristos   { 0x10, 0xFF, prt_e, "djnz ", ~INSS_GBZ80 },
845*184b2d41Schristos   { 0x12, 0xFF, prt, "ld (de),a", INSS_ALL },
846*184b2d41Schristos   { 0x17, 0xFF, prt, "rla", INSS_ALL },
847*184b2d41Schristos   { 0x18, 0xFF, prt_e, "jr ", INSS_ALL },
848*184b2d41Schristos   { 0x1A, 0xFF, prt, "ld a,(de)", INSS_ALL },
849*184b2d41Schristos   { 0x1F, 0xFF, prt, "rra", INSS_ALL },
850*184b2d41Schristos   { 0x20, 0xE7, jr_cc, "jr %s,", INSS_ALL },
851*184b2d41Schristos   { 0x22, 0xFF, prt_nn, "ld (0x%04x),hl", ~INSS_GBZ80 },
852*184b2d41Schristos   { 0x27, 0xFF, prt, "daa", INSS_ALL },
853*184b2d41Schristos   { 0x2A, 0xFF, prt_nn, "ld hl,(0x%04x)", ~INSS_GBZ80 },
854*184b2d41Schristos   { 0x2F, 0xFF, prt, "cpl", INSS_ALL },
855*184b2d41Schristos   { 0x32, 0xFF, prt_nn, "ld (0x%04x),a", INSS_ALL },
856*184b2d41Schristos   { 0x37, 0xFF, prt, "scf", INSS_ALL },
857*184b2d41Schristos   { 0x3A, 0xFF, prt_nn, "ld a,(0x%04x)", INSS_ALL },
858*184b2d41Schristos   { 0x3F, 0xFF, prt, "ccf", INSS_ALL },
859*184b2d41Schristos 
860*184b2d41Schristos   { 0x76, 0xFF, prt, "halt", INSS_ALL },
861*184b2d41Schristos 
862*184b2d41Schristos   { 0x40, 0xFF, suffix, "sis", INSS_EZ80 },
863*184b2d41Schristos   { 0x49, 0xFF, suffix, "lis", INSS_EZ80 },
864*184b2d41Schristos   { 0x52, 0xFF, suffix, "sil", INSS_EZ80 },
865*184b2d41Schristos   { 0x5B, 0xFF, suffix, "lil", INSS_EZ80 },
866*184b2d41Schristos 
867*184b2d41Schristos   { 0x40, 0xC0, ld_r_r, "ld %s,%s", INSS_ALL},
868*184b2d41Schristos 
869*184b2d41Schristos   { 0x80, 0xC0, arit_r, "%s%s", INSS_ALL },
870*184b2d41Schristos 
871*184b2d41Schristos   { 0xC0, 0xC7, prt_cc, "ret ", INSS_ALL },
872*184b2d41Schristos   { 0xC1, 0xCF, pop_rr, "pop", INSS_ALL },
873*184b2d41Schristos   { 0xC2, 0xC7, jp_cc_nn, "jp ", INSS_ALL },
874*184b2d41Schristos   { 0xC3, 0xFF, prt_nn, "jp 0x%04x", INSS_ALL },
875*184b2d41Schristos   { 0xC4, 0xC7, jp_cc_nn, "call ", INSS_ALL },
876*184b2d41Schristos   { 0xC5, 0xCF, pop_rr, "push", INSS_ALL },
877*184b2d41Schristos   { 0xC6, 0xC7, arit_n, "%s0x%%02x", INSS_ALL },
878*184b2d41Schristos   { 0xC7, 0xC7, rst, "rst 0x%02x", INSS_ALL },
879*184b2d41Schristos   { 0xC9, 0xFF, prt, "ret", INSS_ALL },
880*184b2d41Schristos   { 0xCB, 0xFF, pref_cb, "", INSS_ALL },
881*184b2d41Schristos   { 0xCD, 0xFF, prt_nn, "call 0x%04x", INSS_ALL },
882*184b2d41Schristos   { 0xD3, 0xFF, prt_n, "out (0x%02x),a", ~INSS_GBZ80 },
883*184b2d41Schristos   { 0xD9, 0xFF, prt, "exx", ~INSS_GBZ80 },
884*184b2d41Schristos   { 0xDB, 0xFF, prt_n, "in a,(0x%02x)", ~INSS_GBZ80 },
885*184b2d41Schristos   { 0xDD, 0xFF, pref_ind, "ix", ~INSS_GBZ80 },
886*184b2d41Schristos   { 0xE3, 0xFF, prt, "ex (sp),hl", ~INSS_GBZ80 },
887*184b2d41Schristos   { 0xE9, 0xFF, prt, "jp (hl)", INSS_ALL },
888*184b2d41Schristos   { 0xEB, 0xFF, prt, "ex de,hl", ~INSS_GBZ80 },
889*184b2d41Schristos   { 0xED, 0xFF, pref_ed, "", ~INSS_GBZ80 },
890*184b2d41Schristos   { 0xF3, 0xFF, prt, "di", INSS_ALL },
891*184b2d41Schristos   { 0xF9, 0xFF, prt, "ld sp,hl", INSS_ALL },
892*184b2d41Schristos   { 0xFB, 0xFF, prt, "ei", INSS_ALL },
893*184b2d41Schristos   { 0xFD, 0xFF, pref_ind, "iy", ~INSS_GBZ80 },
894*184b2d41Schristos   { 0x00, 0x00, prt, "????", INSS_ALL },
895*184b2d41Schristos } ;
896*184b2d41Schristos 
897*184b2d41Schristos /* Separate GBZ80 main opcode table due to many differences */
898*184b2d41Schristos static const struct tab_elt
899*184b2d41Schristos opc_main_gbz80[] =
900*184b2d41Schristos {
901*184b2d41Schristos   { 0x00, 0xFF, prt,"nop", INSS_ALL },
902*184b2d41Schristos   { 0x01, 0xCF, prt_rr_nn, "ld %s,0x%%04x", INSS_ALL },
903*184b2d41Schristos   { 0x02, 0xFF, prt, "ld (bc),a", INSS_ALL },
904*184b2d41Schristos   { 0x03, 0xCF, prt_rr, "inc ", INSS_ALL },
905*184b2d41Schristos   { 0x04, 0xC7, prt_r, "inc %s", INSS_ALL },
906*184b2d41Schristos   { 0x05, 0xC7, prt_r, "dec %s", INSS_ALL },
907*184b2d41Schristos   { 0x06, 0xC7, ld_r_n, "ld %s,0x%%02x", INSS_ALL },
908*184b2d41Schristos   { 0x07, 0xFF, prt, "rlca", INSS_ALL },
909*184b2d41Schristos   { 0x08, 0xFF, prt_nn, "ld (0x%04x),sp", INSS_GBZ80 },
910*184b2d41Schristos   { 0x09, 0xCF, prt_rr, "add hl,", INSS_ALL },
911*184b2d41Schristos   { 0x0A, 0xFF, prt, "ld a,(bc)", INSS_ALL },
912*184b2d41Schristos   { 0x0B, 0xCF, prt_rr, "dec ", INSS_ALL },
913*184b2d41Schristos   { 0x0F, 0xFF, prt, "rrca", INSS_ALL },
914*184b2d41Schristos   { 0x10, 0xFF, prt, "stop", INSS_GBZ80 },
915*184b2d41Schristos   { 0x12, 0xFF, prt, "ld (de),a", INSS_ALL },
916*184b2d41Schristos   { 0x17, 0xFF, prt, "rla", INSS_ALL },
917*184b2d41Schristos   { 0x18, 0xFF, prt_e, "jr ", INSS_ALL },
918*184b2d41Schristos   { 0x1A, 0xFF, prt, "ld a,(de)", INSS_ALL },
919*184b2d41Schristos   { 0x1F, 0xFF, prt, "rra", INSS_ALL },
920*184b2d41Schristos   { 0x20, 0xE7, jr_cc, "jr %s,", INSS_ALL },
921*184b2d41Schristos   { 0x22, 0xFF, prt, "ld (hl+),a", INSS_GBZ80 },
922*184b2d41Schristos   { 0x27, 0xFF, prt, "daa", INSS_ALL },
923*184b2d41Schristos   { 0x2A, 0xFF, prt, "ld a,(hl+)", INSS_GBZ80 },
924*184b2d41Schristos   { 0x2F, 0xFF, prt, "cpl", INSS_ALL },
925*184b2d41Schristos   { 0x32, 0xFF, prt, "ld (hl-),a", INSS_GBZ80 },
926*184b2d41Schristos   { 0x37, 0xFF, prt, "scf", INSS_ALL },
927*184b2d41Schristos   { 0x3A, 0xFF, prt, "ld a,(hl-)", INSS_GBZ80 },
928*184b2d41Schristos   { 0x3F, 0xFF, prt, "ccf", INSS_ALL },
929*184b2d41Schristos   { 0x76, 0xFF, prt, "halt", INSS_ALL },
930*184b2d41Schristos   { 0x40, 0xC0, ld_r_r, "ld %s,%s", INSS_ALL},
931*184b2d41Schristos   { 0x80, 0xC0, arit_r, "%s%s", INSS_ALL },
932*184b2d41Schristos   { 0xC0, 0xE7, prt_cc, "ret ", INSS_ALL },
933*184b2d41Schristos   { 0xC1, 0xCF, pop_rr, "pop", INSS_ALL },
934*184b2d41Schristos   { 0xC2, 0xE7, jp_cc_nn, "jp ", INSS_ALL },
935*184b2d41Schristos   { 0xC3, 0xFF, prt_nn, "jp 0x%04x", INSS_ALL },
936*184b2d41Schristos   { 0xC4, 0xE7, jp_cc_nn, "call ", INSS_ALL },
937*184b2d41Schristos   { 0xC5, 0xCF, pop_rr, "push", INSS_ALL },
938*184b2d41Schristos   { 0xC6, 0xC7, arit_n, "%s0x%%02x", INSS_ALL },
939*184b2d41Schristos   { 0xC7, 0xC7, rst, "rst 0x%02x", INSS_ALL },
940*184b2d41Schristos   { 0xC9, 0xFF, prt, "ret", INSS_ALL },
941*184b2d41Schristos   { 0xCB, 0xFF, pref_cb, "", INSS_ALL },
942*184b2d41Schristos   { 0xCD, 0xFF, prt_nn, "call 0x%04x", INSS_ALL },
943*184b2d41Schristos   { 0xD9, 0xFF, prt, "reti", INSS_GBZ80 },
944*184b2d41Schristos   { 0xE0, 0xFF, prt_n, "ldh (0x%02x),a", INSS_GBZ80 },
945*184b2d41Schristos   { 0xE2, 0xFF, prt, "ldh (c),a", INSS_GBZ80 },
946*184b2d41Schristos   { 0xE8, 0xFF, prt_d, "add sp,%d", INSS_GBZ80 },
947*184b2d41Schristos   { 0xE9, 0xFF, prt, "jp (hl)", INSS_ALL },
948*184b2d41Schristos   { 0xEA, 0xFF, prt_nn, "ld (0x%04x),a", INSS_GBZ80 },
949*184b2d41Schristos   { 0xF0, 0xFF, prt_n, "ldh a,(0x%02x)", INSS_GBZ80 },
950*184b2d41Schristos   { 0xF2, 0xFF, prt, "ldh a,(c)", INSS_GBZ80 },
951*184b2d41Schristos   { 0xF3, 0xFF, prt, "di", INSS_ALL },
952*184b2d41Schristos   { 0xF8, 0xFF, prt_d, "ldhl sp,%d", INSS_GBZ80 },
953*184b2d41Schristos   { 0xF9, 0xFF, prt, "ld sp,hl", INSS_ALL },
954*184b2d41Schristos   { 0xFA, 0xFF, prt_nn, "ld a,(0x%04x)", INSS_GBZ80 },
955*184b2d41Schristos   { 0xFB, 0xFF, prt, "ei", INSS_ALL },
956*184b2d41Schristos   { 0x00, 0x00, dump, "?", INSS_ALL },
957a1ba9ba4Schristos } ;
958a1ba9ba4Schristos 
959a1ba9ba4Schristos int
print_insn_z80(bfd_vma addr,disassemble_info * info)960a1ba9ba4Schristos print_insn_z80 (bfd_vma addr, disassemble_info * info)
961a1ba9ba4Schristos {
962a1ba9ba4Schristos   struct buffer buf;
963a1ba9ba4Schristos 
964a1ba9ba4Schristos   buf.base = addr;
965*184b2d41Schristos   buf.inss = 1 << info->mach;
966*184b2d41Schristos   buf.nn_len = info->mach == bfd_mach_ez80_adl ? 3 : 2;
967*184b2d41Schristos   info->bytes_per_line = (buf.inss & INSS_EZ80) ? 6 : 4; /* <ss pp oo nn mm MM> OR <pp oo nn mm> */
968a1ba9ba4Schristos 
969*184b2d41Schristos   return print_insn_z80_buf (&buf, info);
970*184b2d41Schristos }
971*184b2d41Schristos 
972*184b2d41Schristos static int
print_insn_z80_buf(struct buffer * buf,disassemble_info * info)973*184b2d41Schristos print_insn_z80_buf (struct buffer *buf, disassemble_info *info)
974*184b2d41Schristos {
975*184b2d41Schristos   const struct tab_elt *p;
976*184b2d41Schristos 
977*184b2d41Schristos   buf->n_fetch = 0;
978*184b2d41Schristos   buf->n_used = 0;
979*184b2d41Schristos   if (! fetch_data (buf, info, 1))
980a1ba9ba4Schristos     return -1;
981a1ba9ba4Schristos 
982*184b2d41Schristos   p = (buf->inss & INSS_GBZ80) ? opc_main_gbz80 : opc_main;
983a1ba9ba4Schristos 
984*184b2d41Schristos   for (; p->val != (buf->data[0] & p->mask) || !mach_inst (buf, p); ++p)
985*184b2d41Schristos     ;
986*184b2d41Schristos   p->fp (buf, info, p->text);
987*184b2d41Schristos 
988*184b2d41Schristos   return buf->n_used;
989a1ba9ba4Schristos }
990