1*3d8817e4Smiod /* Disassemble z8000 code.
2*3d8817e4Smiod    Copyright 1992, 1993, 1998, 2000, 2001, 2002, 2003
3*3d8817e4Smiod    Free Software Foundation, Inc.
4*3d8817e4Smiod 
5*3d8817e4Smiod    This file is part of GNU Binutils.
6*3d8817e4Smiod 
7*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
8*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
9*3d8817e4Smiod    the Free Software Foundation; either version 2 of the License, or
10*3d8817e4Smiod    (at your option) any later version.
11*3d8817e4Smiod 
12*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
13*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*3d8817e4Smiod    GNU General Public License for more details.
16*3d8817e4Smiod 
17*3d8817e4Smiod    You should have received a copy of the GNU General Public License
18*3d8817e4Smiod    along with this program; if not, write to the Free Software
19*3d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
20*3d8817e4Smiod    USA.  */
21*3d8817e4Smiod 
22*3d8817e4Smiod #include "sysdep.h"
23*3d8817e4Smiod #include "dis-asm.h"
24*3d8817e4Smiod 
25*3d8817e4Smiod #define DEFINE_TABLE
26*3d8817e4Smiod #include "z8k-opc.h"
27*3d8817e4Smiod 
28*3d8817e4Smiod #include <setjmp.h>
29*3d8817e4Smiod 
30*3d8817e4Smiod typedef struct
31*3d8817e4Smiod {
32*3d8817e4Smiod   /* These are all indexed by nibble number (i.e only every other entry
33*3d8817e4Smiod      of bytes is used, and every 4th entry of words).  */
34*3d8817e4Smiod   unsigned char nibbles[24];
35*3d8817e4Smiod   unsigned char bytes[24];
36*3d8817e4Smiod   unsigned short words[24];
37*3d8817e4Smiod 
38*3d8817e4Smiod   /* Nibble number of first word not yet fetched.  */
39*3d8817e4Smiod   int max_fetched;
40*3d8817e4Smiod   bfd_vma insn_start;
41*3d8817e4Smiod   jmp_buf bailout;
42*3d8817e4Smiod 
43*3d8817e4Smiod   int tabl_index;
44*3d8817e4Smiod   char instr_asmsrc[80];
45*3d8817e4Smiod   unsigned long arg_reg[0x0f];
46*3d8817e4Smiod   unsigned long immediate;
47*3d8817e4Smiod   unsigned long displacement;
48*3d8817e4Smiod   unsigned long address;
49*3d8817e4Smiod   unsigned long cond_code;
50*3d8817e4Smiod   unsigned long ctrl_code;
51*3d8817e4Smiod   unsigned long flags;
52*3d8817e4Smiod   unsigned long interrupts;
53*3d8817e4Smiod }
54*3d8817e4Smiod instr_data_s;
55*3d8817e4Smiod 
56*3d8817e4Smiod /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
57*3d8817e4Smiod    to ADDR (exclusive) are valid.  Returns 1 for success, longjmps
58*3d8817e4Smiod    on error.  */
59*3d8817e4Smiod #define FETCH_DATA(info, nibble) \
60*3d8817e4Smiod   ((nibble) < ((instr_data_s *) (info->private_data))->max_fetched \
61*3d8817e4Smiod    ? 1 : fetch_data ((info), (nibble)))
62*3d8817e4Smiod 
63*3d8817e4Smiod static int
fetch_data(struct disassemble_info * info,int nibble)64*3d8817e4Smiod fetch_data (struct disassemble_info *info, int nibble)
65*3d8817e4Smiod {
66*3d8817e4Smiod   unsigned char mybuf[20];
67*3d8817e4Smiod   int status;
68*3d8817e4Smiod   instr_data_s *priv = (instr_data_s *) info->private_data;
69*3d8817e4Smiod 
70*3d8817e4Smiod   if ((nibble % 4) != 0)
71*3d8817e4Smiod     abort ();
72*3d8817e4Smiod 
73*3d8817e4Smiod   status = (*info->read_memory_func) (priv->insn_start,
74*3d8817e4Smiod 				      (bfd_byte *) mybuf,
75*3d8817e4Smiod 				      nibble / 2,
76*3d8817e4Smiod 				      info);
77*3d8817e4Smiod   if (status != 0)
78*3d8817e4Smiod     {
79*3d8817e4Smiod       (*info->memory_error_func) (status, priv->insn_start, info);
80*3d8817e4Smiod       longjmp (priv->bailout, 1);
81*3d8817e4Smiod     }
82*3d8817e4Smiod 
83*3d8817e4Smiod   {
84*3d8817e4Smiod     int i;
85*3d8817e4Smiod     unsigned char *p = mybuf;
86*3d8817e4Smiod 
87*3d8817e4Smiod     for (i = 0; i < nibble;)
88*3d8817e4Smiod       {
89*3d8817e4Smiod 	priv->words[i] = (p[0] << 8) | p[1];
90*3d8817e4Smiod 
91*3d8817e4Smiod 	priv->bytes[i] = *p;
92*3d8817e4Smiod 	priv->nibbles[i++] = *p >> 4;
93*3d8817e4Smiod 	priv->nibbles[i++] = *p & 0xf;
94*3d8817e4Smiod 
95*3d8817e4Smiod 	++p;
96*3d8817e4Smiod 	priv->bytes[i] = *p;
97*3d8817e4Smiod 	priv->nibbles[i++] = *p >> 4;
98*3d8817e4Smiod 	priv->nibbles[i++] = *p & 0xf;
99*3d8817e4Smiod 
100*3d8817e4Smiod 	++p;
101*3d8817e4Smiod       }
102*3d8817e4Smiod   }
103*3d8817e4Smiod   priv->max_fetched = nibble;
104*3d8817e4Smiod   return 1;
105*3d8817e4Smiod }
106*3d8817e4Smiod 
107*3d8817e4Smiod static char *codes[16] =
108*3d8817e4Smiod   {
109*3d8817e4Smiod     "f",
110*3d8817e4Smiod     "lt",
111*3d8817e4Smiod     "le",
112*3d8817e4Smiod     "ule",
113*3d8817e4Smiod     "ov/pe",
114*3d8817e4Smiod     "mi",
115*3d8817e4Smiod     "eq",
116*3d8817e4Smiod     "c/ult",
117*3d8817e4Smiod     "t",
118*3d8817e4Smiod     "ge",
119*3d8817e4Smiod     "gt",
120*3d8817e4Smiod     "ugt",
121*3d8817e4Smiod     "nov/po",
122*3d8817e4Smiod     "pl",
123*3d8817e4Smiod     "ne",
124*3d8817e4Smiod     "nc/uge"
125*3d8817e4Smiod   };
126*3d8817e4Smiod 
127*3d8817e4Smiod static char *ctrl_names[8] =
128*3d8817e4Smiod   {
129*3d8817e4Smiod     "<invld>",
130*3d8817e4Smiod     "flags",
131*3d8817e4Smiod     "fcw",
132*3d8817e4Smiod     "refresh",
133*3d8817e4Smiod     "psapseg",
134*3d8817e4Smiod     "psapoff",
135*3d8817e4Smiod     "nspseg",
136*3d8817e4Smiod     "nspoff"
137*3d8817e4Smiod   };
138*3d8817e4Smiod 
139*3d8817e4Smiod static int seg_length;
140*3d8817e4Smiod int z8k_lookup_instr (unsigned char *, disassemble_info *);
141*3d8817e4Smiod static void output_instr (instr_data_s *, unsigned long, disassemble_info *);
142*3d8817e4Smiod static void unpack_instr (instr_data_s *, int, disassemble_info *);
143*3d8817e4Smiod static void unparse_instr (instr_data_s *, int);
144*3d8817e4Smiod 
145*3d8817e4Smiod static int
print_insn_z8k(bfd_vma addr,disassemble_info * info,int is_segmented)146*3d8817e4Smiod print_insn_z8k (bfd_vma addr, disassemble_info *info, int is_segmented)
147*3d8817e4Smiod {
148*3d8817e4Smiod   instr_data_s instr_data;
149*3d8817e4Smiod 
150*3d8817e4Smiod   info->private_data = (PTR) &instr_data;
151*3d8817e4Smiod   instr_data.max_fetched = 0;
152*3d8817e4Smiod   instr_data.insn_start = addr;
153*3d8817e4Smiod   if (setjmp (instr_data.bailout) != 0)
154*3d8817e4Smiod     /* Error return.  */
155*3d8817e4Smiod     return -1;
156*3d8817e4Smiod 
157*3d8817e4Smiod   info->bytes_per_chunk = 2;
158*3d8817e4Smiod   info->bytes_per_line = 6;
159*3d8817e4Smiod   info->display_endian = BFD_ENDIAN_BIG;
160*3d8817e4Smiod 
161*3d8817e4Smiod   instr_data.tabl_index = z8k_lookup_instr (instr_data.nibbles, info);
162*3d8817e4Smiod   if (instr_data.tabl_index >= 0)
163*3d8817e4Smiod     {
164*3d8817e4Smiod       unpack_instr (&instr_data, is_segmented, info);
165*3d8817e4Smiod       unparse_instr (&instr_data, is_segmented);
166*3d8817e4Smiod       output_instr (&instr_data, addr, info);
167*3d8817e4Smiod       return z8k_table[instr_data.tabl_index].length + seg_length;
168*3d8817e4Smiod     }
169*3d8817e4Smiod   else
170*3d8817e4Smiod     {
171*3d8817e4Smiod       FETCH_DATA (info, 4);
172*3d8817e4Smiod       (*info->fprintf_func) (info->stream, ".word %02x%02x",
173*3d8817e4Smiod 			     instr_data.bytes[0], instr_data.bytes[2]);
174*3d8817e4Smiod       return 2;
175*3d8817e4Smiod     }
176*3d8817e4Smiod }
177*3d8817e4Smiod 
178*3d8817e4Smiod int
print_insn_z8001(bfd_vma addr,disassemble_info * info)179*3d8817e4Smiod print_insn_z8001 (bfd_vma addr, disassemble_info *info)
180*3d8817e4Smiod {
181*3d8817e4Smiod   return print_insn_z8k (addr, info, 1);
182*3d8817e4Smiod }
183*3d8817e4Smiod 
184*3d8817e4Smiod int
print_insn_z8002(bfd_vma addr,disassemble_info * info)185*3d8817e4Smiod print_insn_z8002 (bfd_vma addr, disassemble_info *info)
186*3d8817e4Smiod {
187*3d8817e4Smiod   return print_insn_z8k (addr, info, 0);
188*3d8817e4Smiod }
189*3d8817e4Smiod 
190*3d8817e4Smiod int
z8k_lookup_instr(unsigned char * nibbles,disassemble_info * info)191*3d8817e4Smiod z8k_lookup_instr (unsigned char *nibbles, disassemble_info *info)
192*3d8817e4Smiod {
193*3d8817e4Smiod   int nibl_index, tabl_index;
194*3d8817e4Smiod   int nibl_matched;
195*3d8817e4Smiod   int need_fetch = 0;
196*3d8817e4Smiod   unsigned short instr_nibl;
197*3d8817e4Smiod   unsigned short tabl_datum, datum_class, datum_value;
198*3d8817e4Smiod 
199*3d8817e4Smiod   nibl_matched = 0;
200*3d8817e4Smiod   tabl_index = 0;
201*3d8817e4Smiod   FETCH_DATA (info, 4);
202*3d8817e4Smiod   while (!nibl_matched && z8k_table[tabl_index].name)
203*3d8817e4Smiod     {
204*3d8817e4Smiod       nibl_matched = 1;
205*3d8817e4Smiod       for (nibl_index = 0;
206*3d8817e4Smiod 	   nibl_index < z8k_table[tabl_index].length * 2 && nibl_matched;
207*3d8817e4Smiod 	   nibl_index++)
208*3d8817e4Smiod 	{
209*3d8817e4Smiod 	  if ((nibl_index % 4) == 0)
210*3d8817e4Smiod             {
211*3d8817e4Smiod               /* Fetch data only if it isn't already there.  */
212*3d8817e4Smiod               if (nibl_index >= 4 || (nibl_index < 4 && need_fetch))
213*3d8817e4Smiod                 FETCH_DATA (info, nibl_index + 4);   /* Fetch one word at a time.  */
214*3d8817e4Smiod               if (nibl_index < 4)
215*3d8817e4Smiod                 need_fetch = 0;
216*3d8817e4Smiod               else
217*3d8817e4Smiod                 need_fetch = 1;
218*3d8817e4Smiod             }
219*3d8817e4Smiod 	  instr_nibl = nibbles[nibl_index];
220*3d8817e4Smiod 
221*3d8817e4Smiod 	  tabl_datum = z8k_table[tabl_index].byte_info[nibl_index];
222*3d8817e4Smiod 	  datum_class = tabl_datum & CLASS_MASK;
223*3d8817e4Smiod 	  datum_value = ~CLASS_MASK & tabl_datum;
224*3d8817e4Smiod 
225*3d8817e4Smiod 	  switch (datum_class)
226*3d8817e4Smiod 	    {
227*3d8817e4Smiod 	    case CLASS_BIT:
228*3d8817e4Smiod 	      if (datum_value != instr_nibl)
229*3d8817e4Smiod 		nibl_matched = 0;
230*3d8817e4Smiod 	      break;
231*3d8817e4Smiod 	    case CLASS_IGNORE:
232*3d8817e4Smiod 	      break;
233*3d8817e4Smiod 	    case CLASS_00II:
234*3d8817e4Smiod 	      if (!((~instr_nibl) & 0x4))
235*3d8817e4Smiod 		nibl_matched = 0;
236*3d8817e4Smiod 	      break;
237*3d8817e4Smiod 	    case CLASS_01II:
238*3d8817e4Smiod 	      if (!(instr_nibl & 0x4))
239*3d8817e4Smiod 		nibl_matched = 0;
240*3d8817e4Smiod 	      break;
241*3d8817e4Smiod 	    case CLASS_0CCC:
242*3d8817e4Smiod 	      if (!((~instr_nibl) & 0x8))
243*3d8817e4Smiod 		nibl_matched = 0;
244*3d8817e4Smiod 	      break;
245*3d8817e4Smiod 	    case CLASS_1CCC:
246*3d8817e4Smiod 	      if (!(instr_nibl & 0x8))
247*3d8817e4Smiod 		nibl_matched = 0;
248*3d8817e4Smiod 	      break;
249*3d8817e4Smiod 	    case CLASS_0DISP7:
250*3d8817e4Smiod 	      if (!((~instr_nibl) & 0x8))
251*3d8817e4Smiod 		nibl_matched = 0;
252*3d8817e4Smiod 	      nibl_index += 1;
253*3d8817e4Smiod 	      break;
254*3d8817e4Smiod 	    case CLASS_1DISP7:
255*3d8817e4Smiod 	      if (!(instr_nibl & 0x8))
256*3d8817e4Smiod 		nibl_matched = 0;
257*3d8817e4Smiod 	      nibl_index += 1;
258*3d8817e4Smiod 	      break;
259*3d8817e4Smiod 	    case CLASS_REGN0:
260*3d8817e4Smiod 	      if (instr_nibl == 0)
261*3d8817e4Smiod 		nibl_matched = 0;
262*3d8817e4Smiod 	      break;
263*3d8817e4Smiod 	    case CLASS_BIT_1OR2:
264*3d8817e4Smiod 	      if ((instr_nibl | 0x2) != (datum_value | 0x2))
265*3d8817e4Smiod 		nibl_matched = 0;
266*3d8817e4Smiod 	      break;
267*3d8817e4Smiod 	    default:
268*3d8817e4Smiod 	      break;
269*3d8817e4Smiod 	    }
270*3d8817e4Smiod 	}
271*3d8817e4Smiod 
272*3d8817e4Smiod       if (nibl_matched)
273*3d8817e4Smiod 	return tabl_index;
274*3d8817e4Smiod 
275*3d8817e4Smiod       tabl_index++;
276*3d8817e4Smiod     }
277*3d8817e4Smiod   return -1;
278*3d8817e4Smiod }
279*3d8817e4Smiod 
280*3d8817e4Smiod static void
output_instr(instr_data_s * instr_data,unsigned long addr ATTRIBUTE_UNUSED,disassemble_info * info)281*3d8817e4Smiod output_instr (instr_data_s *instr_data,
282*3d8817e4Smiod               unsigned long addr ATTRIBUTE_UNUSED,
283*3d8817e4Smiod               disassemble_info *info)
284*3d8817e4Smiod {
285*3d8817e4Smiod   int num_bytes;
286*3d8817e4Smiod   char out_str[100];
287*3d8817e4Smiod 
288*3d8817e4Smiod   out_str[0] = 0;
289*3d8817e4Smiod 
290*3d8817e4Smiod   num_bytes = (z8k_table[instr_data->tabl_index].length + seg_length) * 2;
291*3d8817e4Smiod   FETCH_DATA (info, num_bytes);
292*3d8817e4Smiod 
293*3d8817e4Smiod   strcat (out_str, instr_data->instr_asmsrc);
294*3d8817e4Smiod 
295*3d8817e4Smiod   (*info->fprintf_func) (info->stream, "%s", out_str);
296*3d8817e4Smiod }
297*3d8817e4Smiod 
298*3d8817e4Smiod static void
unpack_instr(instr_data_s * instr_data,int is_segmented,disassemble_info * info)299*3d8817e4Smiod unpack_instr (instr_data_s *instr_data, int is_segmented, disassemble_info *info)
300*3d8817e4Smiod {
301*3d8817e4Smiod   int nibl_count, loop;
302*3d8817e4Smiod   unsigned short instr_nibl, instr_byte, instr_word;
303*3d8817e4Smiod   long instr_long;
304*3d8817e4Smiod   unsigned int tabl_datum, datum_class;
305*3d8817e4Smiod   unsigned short datum_value;
306*3d8817e4Smiod 
307*3d8817e4Smiod   nibl_count = 0;
308*3d8817e4Smiod   loop = 0;
309*3d8817e4Smiod   seg_length = 0;
310*3d8817e4Smiod 
311*3d8817e4Smiod   while (z8k_table[instr_data->tabl_index].byte_info[loop] != 0)
312*3d8817e4Smiod     {
313*3d8817e4Smiod       FETCH_DATA (info, nibl_count + 4 - (nibl_count % 4));
314*3d8817e4Smiod       instr_nibl = instr_data->nibbles[nibl_count];
315*3d8817e4Smiod       instr_byte = instr_data->bytes[nibl_count & ~1];
316*3d8817e4Smiod       instr_word = instr_data->words[nibl_count & ~3];
317*3d8817e4Smiod 
318*3d8817e4Smiod       tabl_datum = z8k_table[instr_data->tabl_index].byte_info[loop];
319*3d8817e4Smiod       datum_class = tabl_datum & CLASS_MASK;
320*3d8817e4Smiod       datum_value = tabl_datum & ~CLASS_MASK;
321*3d8817e4Smiod 
322*3d8817e4Smiod       switch (datum_class)
323*3d8817e4Smiod 	{
324*3d8817e4Smiod 	case CLASS_DISP:
325*3d8817e4Smiod 	  switch (datum_value)
326*3d8817e4Smiod 	    {
327*3d8817e4Smiod 	    case ARG_DISP16:
328*3d8817e4Smiod 	      instr_data->displacement = instr_data->insn_start + 4
329*3d8817e4Smiod 		+ (signed short) (instr_word & 0xffff);
330*3d8817e4Smiod 	      nibl_count += 3;
331*3d8817e4Smiod 	      break;
332*3d8817e4Smiod 	    case ARG_DISP12:
333*3d8817e4Smiod 	      if (instr_word & 0x800)
334*3d8817e4Smiod 		/* Negative 12 bit displacement.  */
335*3d8817e4Smiod 		instr_data->displacement = instr_data->insn_start + 2
336*3d8817e4Smiod 		  - (signed short) ((instr_word & 0xfff) | 0xf000) * 2;
337*3d8817e4Smiod 	      else
338*3d8817e4Smiod 		instr_data->displacement = instr_data->insn_start + 2
339*3d8817e4Smiod 		  - (instr_word & 0x0fff) * 2;
340*3d8817e4Smiod 
341*3d8817e4Smiod 	      nibl_count += 2;
342*3d8817e4Smiod 	      break;
343*3d8817e4Smiod 	    default:
344*3d8817e4Smiod 	      break;
345*3d8817e4Smiod 	    }
346*3d8817e4Smiod 	  break;
347*3d8817e4Smiod 	case CLASS_IMM:
348*3d8817e4Smiod 	  switch (datum_value)
349*3d8817e4Smiod 	    {
350*3d8817e4Smiod 	    case ARG_IMM4:
351*3d8817e4Smiod 	      instr_data->immediate = instr_nibl;
352*3d8817e4Smiod 	      break;
353*3d8817e4Smiod 	    case ARG_NIM4:
354*3d8817e4Smiod 	      instr_data->immediate = (- instr_nibl) & 0xf;
355*3d8817e4Smiod 	      break;
356*3d8817e4Smiod 	    case ARG_NIM8:
357*3d8817e4Smiod 	      instr_data->immediate = (- instr_byte) & 0xff;
358*3d8817e4Smiod 	      nibl_count += 1;
359*3d8817e4Smiod 	      break;
360*3d8817e4Smiod 	    case ARG_IMM8:
361*3d8817e4Smiod 	      instr_data->immediate = instr_byte;
362*3d8817e4Smiod 	      nibl_count += 1;
363*3d8817e4Smiod 	      break;
364*3d8817e4Smiod 	    case ARG_IMM16:
365*3d8817e4Smiod 	      instr_data->immediate = instr_word;
366*3d8817e4Smiod 	      nibl_count += 3;
367*3d8817e4Smiod 	      break;
368*3d8817e4Smiod 	    case ARG_IMM32:
369*3d8817e4Smiod 	      FETCH_DATA (info, nibl_count + 8);
370*3d8817e4Smiod 	      instr_long = (instr_data->words[nibl_count] << 16)
371*3d8817e4Smiod 		| (instr_data->words[nibl_count + 4]);
372*3d8817e4Smiod 	      instr_data->immediate = instr_long;
373*3d8817e4Smiod 	      nibl_count += 7;
374*3d8817e4Smiod 	      break;
375*3d8817e4Smiod 	    case ARG_IMMN:
376*3d8817e4Smiod 	      instr_data->immediate = instr_nibl - 1;
377*3d8817e4Smiod 	      break;
378*3d8817e4Smiod 	    case ARG_IMM4M1:
379*3d8817e4Smiod 	      instr_data->immediate = instr_nibl + 1;
380*3d8817e4Smiod 	      break;
381*3d8817e4Smiod 	    case ARG_IMM_1:
382*3d8817e4Smiod 	      instr_data->immediate = 1;
383*3d8817e4Smiod 	      break;
384*3d8817e4Smiod 	    case ARG_IMM_2:
385*3d8817e4Smiod 	      instr_data->immediate = 2;
386*3d8817e4Smiod 	      break;
387*3d8817e4Smiod 	    case ARG_IMM2:
388*3d8817e4Smiod 	      instr_data->immediate = instr_nibl & 0x3;
389*3d8817e4Smiod 	      break;
390*3d8817e4Smiod 	    default:
391*3d8817e4Smiod 	      break;
392*3d8817e4Smiod 	    }
393*3d8817e4Smiod 	  break;
394*3d8817e4Smiod 	case CLASS_CC:
395*3d8817e4Smiod 	  instr_data->cond_code = instr_nibl;
396*3d8817e4Smiod 	  break;
397*3d8817e4Smiod 	case CLASS_ADDRESS:
398*3d8817e4Smiod 	  if (is_segmented)
399*3d8817e4Smiod 	    {
400*3d8817e4Smiod 	      if (instr_nibl & 0x8)
401*3d8817e4Smiod 		{
402*3d8817e4Smiod 		  FETCH_DATA (info, nibl_count + 8);
403*3d8817e4Smiod 		  instr_long = (instr_data->words[nibl_count] << 16)
404*3d8817e4Smiod 		    | (instr_data->words[nibl_count + 4]);
405*3d8817e4Smiod 		  instr_data->address = ((instr_word & 0x7f00) << 16)
406*3d8817e4Smiod 		    + (instr_long & 0xffff);
407*3d8817e4Smiod 		  nibl_count += 7;
408*3d8817e4Smiod 		  seg_length = 2;
409*3d8817e4Smiod 		}
410*3d8817e4Smiod 	      else
411*3d8817e4Smiod 		{
412*3d8817e4Smiod 		  instr_data->address = ((instr_word & 0x7f00) << 16)
413*3d8817e4Smiod 		    + (instr_word & 0x00ff);
414*3d8817e4Smiod 		  nibl_count += 3;
415*3d8817e4Smiod 		}
416*3d8817e4Smiod 	    }
417*3d8817e4Smiod 	  else
418*3d8817e4Smiod 	    {
419*3d8817e4Smiod 	      instr_data->address = instr_word;
420*3d8817e4Smiod 	      nibl_count += 3;
421*3d8817e4Smiod 	    }
422*3d8817e4Smiod 	  break;
423*3d8817e4Smiod 	case CLASS_0CCC:
424*3d8817e4Smiod 	case CLASS_1CCC:
425*3d8817e4Smiod 	  instr_data->ctrl_code = instr_nibl & 0x7;
426*3d8817e4Smiod 	  break;
427*3d8817e4Smiod 	case CLASS_0DISP7:
428*3d8817e4Smiod 	  instr_data->displacement =
429*3d8817e4Smiod 	    instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
430*3d8817e4Smiod 	  nibl_count += 1;
431*3d8817e4Smiod 	  break;
432*3d8817e4Smiod 	case CLASS_1DISP7:
433*3d8817e4Smiod 	  instr_data->displacement =
434*3d8817e4Smiod 	    instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
435*3d8817e4Smiod 	  nibl_count += 1;
436*3d8817e4Smiod 	  break;
437*3d8817e4Smiod 	case CLASS_01II:
438*3d8817e4Smiod 	  instr_data->interrupts = instr_nibl & 0x3;
439*3d8817e4Smiod 	  break;
440*3d8817e4Smiod 	case CLASS_00II:
441*3d8817e4Smiod 	  instr_data->interrupts = instr_nibl & 0x3;
442*3d8817e4Smiod 	  break;
443*3d8817e4Smiod 	case CLASS_IGNORE:
444*3d8817e4Smiod 	case CLASS_BIT:
445*3d8817e4Smiod 	  instr_data->ctrl_code = instr_nibl & 0x7;
446*3d8817e4Smiod 	  break;
447*3d8817e4Smiod 	case CLASS_FLAGS:
448*3d8817e4Smiod 	  instr_data->flags = instr_nibl;
449*3d8817e4Smiod 	  break;
450*3d8817e4Smiod 	case CLASS_REG:
451*3d8817e4Smiod 	  instr_data->arg_reg[datum_value] = instr_nibl;
452*3d8817e4Smiod 	  break;
453*3d8817e4Smiod 	case CLASS_REGN0:
454*3d8817e4Smiod 	  instr_data->arg_reg[datum_value] = instr_nibl;
455*3d8817e4Smiod 	  break;
456*3d8817e4Smiod 	case CLASS_DISP8:
457*3d8817e4Smiod 	  instr_data->displacement =
458*3d8817e4Smiod 	    instr_data->insn_start + 2 + (signed char) instr_byte * 2;
459*3d8817e4Smiod 	  nibl_count += 1;
460*3d8817e4Smiod 	  break;
461*3d8817e4Smiod         case CLASS_BIT_1OR2:
462*3d8817e4Smiod           instr_data->immediate = ((instr_nibl >> 1) & 0x1) + 1;
463*3d8817e4Smiod           nibl_count += 1;
464*3d8817e4Smiod 	  break;
465*3d8817e4Smiod 	default:
466*3d8817e4Smiod 	  abort ();
467*3d8817e4Smiod 	  break;
468*3d8817e4Smiod 	}
469*3d8817e4Smiod 
470*3d8817e4Smiod       loop += 1;
471*3d8817e4Smiod       nibl_count += 1;
472*3d8817e4Smiod     }
473*3d8817e4Smiod }
474*3d8817e4Smiod 
475*3d8817e4Smiod static void
print_intr(char * tmp_str,unsigned long interrupts)476*3d8817e4Smiod print_intr(char *tmp_str, unsigned long interrupts)
477*3d8817e4Smiod {
478*3d8817e4Smiod   int comma = 0;
479*3d8817e4Smiod 
480*3d8817e4Smiod   *tmp_str = 0;
481*3d8817e4Smiod   if (! (interrupts & 2))
482*3d8817e4Smiod     {
483*3d8817e4Smiod       strcat (tmp_str, "vi");
484*3d8817e4Smiod       comma = 1;
485*3d8817e4Smiod     }
486*3d8817e4Smiod   if (! (interrupts & 1))
487*3d8817e4Smiod     {
488*3d8817e4Smiod       if (comma) strcat (tmp_str, ",");
489*3d8817e4Smiod       strcat (tmp_str, "nvi");
490*3d8817e4Smiod     }
491*3d8817e4Smiod }
492*3d8817e4Smiod 
493*3d8817e4Smiod static void
print_flags(char * tmp_str,unsigned long flags)494*3d8817e4Smiod print_flags(char *tmp_str, unsigned long flags)
495*3d8817e4Smiod {
496*3d8817e4Smiod   int comma = 0;
497*3d8817e4Smiod 
498*3d8817e4Smiod   *tmp_str = 0;
499*3d8817e4Smiod   if (flags & 8)
500*3d8817e4Smiod     {
501*3d8817e4Smiod       strcat (tmp_str, "c");
502*3d8817e4Smiod       comma = 1;
503*3d8817e4Smiod     }
504*3d8817e4Smiod   if (flags & 4)
505*3d8817e4Smiod     {
506*3d8817e4Smiod       if (comma) strcat (tmp_str, ",");
507*3d8817e4Smiod       strcat (tmp_str, "z");
508*3d8817e4Smiod       comma = 1;
509*3d8817e4Smiod     }
510*3d8817e4Smiod   if (flags & 2)
511*3d8817e4Smiod     {
512*3d8817e4Smiod       if (comma) strcat (tmp_str, ",");
513*3d8817e4Smiod       strcat (tmp_str, "s");
514*3d8817e4Smiod       comma = 1;
515*3d8817e4Smiod     }
516*3d8817e4Smiod   if (flags & 1)
517*3d8817e4Smiod     {
518*3d8817e4Smiod       if (comma) strcat (tmp_str, ",");
519*3d8817e4Smiod       strcat (tmp_str, "p");
520*3d8817e4Smiod     }
521*3d8817e4Smiod }
522*3d8817e4Smiod 
523*3d8817e4Smiod static void
unparse_instr(instr_data_s * instr_data,int is_segmented)524*3d8817e4Smiod unparse_instr (instr_data_s *instr_data, int is_segmented)
525*3d8817e4Smiod {
526*3d8817e4Smiod   unsigned short datum_value;
527*3d8817e4Smiod   unsigned int tabl_datum, datum_class;
528*3d8817e4Smiod   int loop, loop_limit;
529*3d8817e4Smiod   char out_str[80], tmp_str[25];
530*3d8817e4Smiod 
531*3d8817e4Smiod   sprintf (out_str, "%s\t", z8k_table[instr_data->tabl_index].name);
532*3d8817e4Smiod 
533*3d8817e4Smiod   loop_limit = z8k_table[instr_data->tabl_index].noperands;
534*3d8817e4Smiod   for (loop = 0; loop < loop_limit; loop++)
535*3d8817e4Smiod     {
536*3d8817e4Smiod       if (loop)
537*3d8817e4Smiod 	strcat (out_str, ",");
538*3d8817e4Smiod 
539*3d8817e4Smiod       tabl_datum = z8k_table[instr_data->tabl_index].arg_info[loop];
540*3d8817e4Smiod       datum_class = tabl_datum & CLASS_MASK;
541*3d8817e4Smiod       datum_value = tabl_datum & ~CLASS_MASK;
542*3d8817e4Smiod 
543*3d8817e4Smiod       switch (datum_class)
544*3d8817e4Smiod 	{
545*3d8817e4Smiod 	case CLASS_X:
546*3d8817e4Smiod           sprintf (tmp_str, "0x%0lx(r%ld)", instr_data->address,
547*3d8817e4Smiod                    instr_data->arg_reg[datum_value]);
548*3d8817e4Smiod 	  strcat (out_str, tmp_str);
549*3d8817e4Smiod 	  break;
550*3d8817e4Smiod 	case CLASS_BA:
551*3d8817e4Smiod           if (is_segmented)
552*3d8817e4Smiod             sprintf (tmp_str, "rr%ld(#0x%lx)", instr_data->arg_reg[datum_value],
553*3d8817e4Smiod                      instr_data->immediate);
554*3d8817e4Smiod           else
555*3d8817e4Smiod             sprintf (tmp_str, "r%ld(#0x%lx)", instr_data->arg_reg[datum_value],
556*3d8817e4Smiod                      instr_data->immediate);
557*3d8817e4Smiod 	  strcat (out_str, tmp_str);
558*3d8817e4Smiod 	  break;
559*3d8817e4Smiod 	case CLASS_BX:
560*3d8817e4Smiod           if (is_segmented)
561*3d8817e4Smiod             sprintf (tmp_str, "rr%ld(r%ld)", instr_data->arg_reg[datum_value],
562*3d8817e4Smiod                      instr_data->arg_reg[ARG_RX]);
563*3d8817e4Smiod           else
564*3d8817e4Smiod             sprintf (tmp_str, "r%ld(r%ld)", instr_data->arg_reg[datum_value],
565*3d8817e4Smiod                      instr_data->arg_reg[ARG_RX]);
566*3d8817e4Smiod 	  strcat (out_str, tmp_str);
567*3d8817e4Smiod 	  break;
568*3d8817e4Smiod 	case CLASS_DISP:
569*3d8817e4Smiod 	  sprintf (tmp_str, "0x%0lx", instr_data->displacement);
570*3d8817e4Smiod 	  strcat (out_str, tmp_str);
571*3d8817e4Smiod 	  break;
572*3d8817e4Smiod 	case CLASS_IMM:
573*3d8817e4Smiod 	  if (datum_value == ARG_IMM2)	/* True with EI/DI instructions only.  */
574*3d8817e4Smiod 	    {
575*3d8817e4Smiod 	      print_intr (tmp_str, instr_data->interrupts);
576*3d8817e4Smiod 	      strcat (out_str, tmp_str);
577*3d8817e4Smiod 	      break;
578*3d8817e4Smiod 	    }
579*3d8817e4Smiod 	  sprintf (tmp_str, "#0x%0lx", instr_data->immediate);
580*3d8817e4Smiod 	  strcat (out_str, tmp_str);
581*3d8817e4Smiod 	  break;
582*3d8817e4Smiod 	case CLASS_CC:
583*3d8817e4Smiod 	  sprintf (tmp_str, "%s", codes[instr_data->cond_code]);
584*3d8817e4Smiod 	  strcat (out_str, tmp_str);
585*3d8817e4Smiod 	  break;
586*3d8817e4Smiod 	case CLASS_CTRL:
587*3d8817e4Smiod 	  sprintf (tmp_str, "%s", ctrl_names[instr_data->ctrl_code]);
588*3d8817e4Smiod 	  strcat (out_str, tmp_str);
589*3d8817e4Smiod 	  break;
590*3d8817e4Smiod 	case CLASS_DA:
591*3d8817e4Smiod 	case CLASS_ADDRESS:
592*3d8817e4Smiod 	  sprintf (tmp_str, "0x%0lx", instr_data->address);
593*3d8817e4Smiod 	  strcat (out_str, tmp_str);
594*3d8817e4Smiod 	  break;
595*3d8817e4Smiod 	case CLASS_IR:
596*3d8817e4Smiod 	  if (is_segmented)
597*3d8817e4Smiod 	    sprintf (tmp_str, "@rr%ld", instr_data->arg_reg[datum_value]);
598*3d8817e4Smiod 	  else
599*3d8817e4Smiod 	    sprintf (tmp_str, "@r%ld", instr_data->arg_reg[datum_value]);
600*3d8817e4Smiod 	  strcat (out_str, tmp_str);
601*3d8817e4Smiod 	  break;
602*3d8817e4Smiod 	case CLASS_IRO:
603*3d8817e4Smiod           sprintf (tmp_str, "@r%ld", instr_data->arg_reg[datum_value]);
604*3d8817e4Smiod 	  strcat (out_str, tmp_str);
605*3d8817e4Smiod 	  break;
606*3d8817e4Smiod 	case CLASS_FLAGS:
607*3d8817e4Smiod 	  print_flags(tmp_str, instr_data->flags);
608*3d8817e4Smiod 	  strcat (out_str, tmp_str);
609*3d8817e4Smiod 	  break;
610*3d8817e4Smiod 	case CLASS_REG_BYTE:
611*3d8817e4Smiod 	  if (instr_data->arg_reg[datum_value] >= 0x8)
612*3d8817e4Smiod 	    sprintf (tmp_str, "rl%ld",
613*3d8817e4Smiod 		     instr_data->arg_reg[datum_value] - 0x8);
614*3d8817e4Smiod 	  else
615*3d8817e4Smiod 	    sprintf (tmp_str, "rh%ld", instr_data->arg_reg[datum_value]);
616*3d8817e4Smiod 	  strcat (out_str, tmp_str);
617*3d8817e4Smiod 	  break;
618*3d8817e4Smiod 	case CLASS_REG_WORD:
619*3d8817e4Smiod 	  sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
620*3d8817e4Smiod 	  strcat (out_str, tmp_str);
621*3d8817e4Smiod 	  break;
622*3d8817e4Smiod 	case CLASS_REG_QUAD:
623*3d8817e4Smiod 	  sprintf (tmp_str, "rq%ld", instr_data->arg_reg[datum_value]);
624*3d8817e4Smiod 	  strcat (out_str, tmp_str);
625*3d8817e4Smiod 	  break;
626*3d8817e4Smiod 	case CLASS_REG_LONG:
627*3d8817e4Smiod 	  sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
628*3d8817e4Smiod 	  strcat (out_str, tmp_str);
629*3d8817e4Smiod 	  break;
630*3d8817e4Smiod 	case CLASS_PR:
631*3d8817e4Smiod 	  if (is_segmented)
632*3d8817e4Smiod 	    sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
633*3d8817e4Smiod 	  else
634*3d8817e4Smiod 	    sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
635*3d8817e4Smiod 	  strcat (out_str, tmp_str);
636*3d8817e4Smiod 	  break;
637*3d8817e4Smiod 	default:
638*3d8817e4Smiod 	  abort ();
639*3d8817e4Smiod 	  break;
640*3d8817e4Smiod 	}
641*3d8817e4Smiod     }
642*3d8817e4Smiod 
643*3d8817e4Smiod   strcpy (instr_data->instr_asmsrc, out_str);
644*3d8817e4Smiod }
645