1999-09-11 Donn Terry <donn@interix.com>
[binutils.git] / opcodes / z8k-dis.c
blob7123622a291cb49d5ec09a7a1d74e75ba2e2bb29
1 /* Disassemble z8000 code.
2 Copyright 1992, 1993, 1995, 1998 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <ansidecl.h>
21 #include "sysdep.h"
22 #include "dis-asm.h"
24 #define DEFINE_TABLE
25 #include "z8k-opc.h"
28 #include <setjmp.h>
31 typedef struct
33 /* These are all indexed by nibble number (i.e only every other entry
34 of bytes is used, and every 4th entry of words). */
35 unsigned char nibbles[24];
36 unsigned char bytes[24];
37 unsigned short words[24];
39 /* Nibble number of first word not yet fetched. */
40 int max_fetched;
41 bfd_vma insn_start;
42 jmp_buf bailout;
44 long tabl_index;
45 char instr_asmsrc[80];
46 unsigned long arg_reg[0x0f];
47 unsigned long immediate;
48 unsigned long displacement;
49 unsigned long address;
50 unsigned long cond_code;
51 unsigned long ctrl_code;
52 unsigned long flags;
53 unsigned long interrupts;
55 instr_data_s;
57 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
58 to ADDR (exclusive) are valid. Returns 1 for success, longjmps
59 on error. */
60 #define FETCH_DATA(info, nibble) \
61 ((nibble) < ((instr_data_s *)(info->private_data))->max_fetched \
62 ? 1 : fetch_data ((info), (nibble)))
64 static int
65 fetch_data (info, nibble)
66 struct disassemble_info *info;
67 int nibble;
69 unsigned char mybuf[20];
70 int status;
71 instr_data_s *priv = (instr_data_s *)info->private_data;
73 if ((nibble % 4) != 0)
74 abort ();
76 status = (*info->read_memory_func) (priv->insn_start,
77 (bfd_byte *) mybuf,
78 nibble / 2,
79 info);
80 if (status != 0)
82 (*info->memory_error_func) (status, priv->insn_start, info);
83 longjmp (priv->bailout, 1);
87 int i;
88 unsigned char *p = mybuf ;
90 for (i = 0; i < nibble;)
92 priv->words[i] = (p[0] << 8) | p[1];
94 priv->bytes[i] = *p;
95 priv->nibbles[i++] = *p >> 4;
96 priv->nibbles[i++] = *p &0xf;
98 ++p;
99 priv->bytes[i] = *p;
100 priv->nibbles[i++] = *p >> 4;
101 priv->nibbles[i++] = *p & 0xf;
103 ++p;
106 priv->max_fetched = nibble;
107 return 1;
110 static char *codes[16] =
112 "f",
113 "lt",
114 "le",
115 "ule",
116 "ov/pe",
117 "mi",
118 "eq",
119 "c/ult",
120 "t",
121 "ge",
122 "gt",
123 "ugt",
124 "nov/po",
125 "pl",
126 "ne",
127 "nc/uge"
130 int z8k_lookup_instr PARAMS ((unsigned char*, disassemble_info *));
131 static void output_instr
132 PARAMS ((instr_data_s *, unsigned long, disassemble_info *));
133 static void unpack_instr PARAMS ((instr_data_s *, int, disassemble_info *));
134 static void unparse_instr PARAMS ((instr_data_s *));
136 static int
137 print_insn_z8k (addr, info, is_segmented)
138 bfd_vma addr;
139 disassemble_info *info;
140 int is_segmented;
142 instr_data_s instr_data;
144 info->private_data = (PTR) &instr_data;
145 instr_data.max_fetched = 0;
146 instr_data.insn_start = addr;
147 if (setjmp (instr_data.bailout) != 0)
148 /* Error return. */
149 return -1;
151 instr_data.tabl_index = z8k_lookup_instr (instr_data.nibbles, info);
152 if (instr_data.tabl_index > 0)
154 unpack_instr (&instr_data, is_segmented, info);
155 unparse_instr (&instr_data);
156 output_instr (&instr_data, addr, info);
157 return z8k_table[instr_data.tabl_index].length;
159 else
161 FETCH_DATA (info, 4);
162 (*info->fprintf_func) (info->stream, ".word %02x%02x",
163 instr_data.bytes[0], instr_data.bytes[2]);
164 return 2;
169 print_insn_z8001 (addr, info)
170 bfd_vma addr;
171 disassemble_info *info;
173 return print_insn_z8k (addr, info, 1);
177 print_insn_z8002 (addr, info)
178 bfd_vma addr;
179 disassemble_info *info;
181 return print_insn_z8k (addr, info, 0);
185 z8k_lookup_instr (nibbles, info)
186 unsigned char *nibbles;
187 disassemble_info *info;
190 int nibl_index, tabl_index;
191 int nibl_matched;
192 unsigned short instr_nibl;
193 unsigned short tabl_datum, datum_class, datum_value;
195 nibl_matched = 0;
196 tabl_index = 0;
197 while (!nibl_matched && z8k_table[tabl_index].name)
199 nibl_matched = 1;
200 for (nibl_index = 0; nibl_index < z8k_table[tabl_index].length * 2 && nibl_matched; nibl_index++)
202 if ((nibl_index % 4) == 0)
203 /* Fetch one word at a time. */
204 FETCH_DATA (info, nibl_index + 4);
205 instr_nibl = nibbles[nibl_index];
207 tabl_datum = z8k_table[tabl_index].byte_info[nibl_index];
208 datum_class = tabl_datum & CLASS_MASK;
209 datum_value = ~CLASS_MASK & tabl_datum;
211 switch (datum_class)
213 case CLASS_BIT:
214 if (datum_value != instr_nibl)
215 nibl_matched = 0;
216 break;
217 case CLASS_00II:
218 if (!((~instr_nibl) & 0x4))
219 nibl_matched = 0;
220 break;
221 case CLASS_01II:
222 if (!(instr_nibl & 0x4))
223 nibl_matched = 0;
224 break;
225 case CLASS_0CCC:
226 if (!((~instr_nibl) & 0x8))
227 nibl_matched = 0;
228 break;
229 case CLASS_1CCC:
230 if (!(instr_nibl & 0x8))
231 nibl_matched = 0;
232 break;
233 case CLASS_0DISP7:
234 if (!((~instr_nibl) & 0x8))
235 nibl_matched = 0;
236 nibl_index += 1;
237 break;
238 case CLASS_1DISP7:
239 if (!(instr_nibl & 0x8))
240 nibl_matched = 0;
241 nibl_index += 1;
242 break;
243 case CLASS_REGN0:
244 if (instr_nibl == 0)
245 nibl_matched = 0;
246 break;
247 case CLASS_BIT_1OR2:
248 if ((instr_nibl | 0x2) != (datum_value | 0x2))
249 nibl_matched = 0;
250 break;
251 default:
252 break;
255 if (nibl_matched)
257 return tabl_index;
260 tabl_index++;
262 return -1;
266 static void
267 output_instr (instr_data, addr, info)
268 instr_data_s *instr_data;
269 unsigned long addr;
270 disassemble_info *info;
272 int loop, loop_limit;
273 char tmp_str[20];
274 char out_str[100];
276 strcpy (out_str, "\t");
278 loop_limit = z8k_table[instr_data->tabl_index].length * 2;
279 FETCH_DATA (info, loop_limit);
280 for (loop = 0; loop < loop_limit; loop++)
282 sprintf (tmp_str, "%x", instr_data->nibbles[loop]);
283 strcat (out_str, tmp_str);
286 while (loop++ < 8)
288 strcat (out_str, " ");
291 strcat (out_str, instr_data->instr_asmsrc);
293 (*info->fprintf_func) (info->stream, "%s", out_str);
296 static void
297 unpack_instr (instr_data, is_segmented, info)
298 instr_data_s *instr_data;
299 int is_segmented;
300 disassemble_info *info;
302 int nibl_count, loop;
303 unsigned short instr_nibl, instr_byte, instr_word;
304 long instr_long;
305 unsigned short tabl_datum, datum_class, datum_value;
307 nibl_count = 0;
308 loop = 0;
309 while (z8k_table[instr_data->tabl_index].byte_info[loop] != 0)
311 FETCH_DATA (info, nibl_count + 4 - (nibl_count % 4));
312 instr_nibl = instr_data->nibbles[nibl_count];
313 instr_byte = instr_data->bytes[nibl_count];
314 instr_word = instr_data->words[nibl_count];
316 tabl_datum = z8k_table[instr_data->tabl_index].byte_info[loop];
317 datum_class = tabl_datum & CLASS_MASK;
318 datum_value = tabl_datum & ~CLASS_MASK;
320 switch (datum_class)
322 case CLASS_X:
323 instr_data->address = instr_nibl;
324 break;
325 case CLASS_BA:
326 instr_data->displacement = instr_nibl;
327 break;
328 case CLASS_BX:
329 instr_data->arg_reg[datum_value] = instr_nibl;
330 break;
331 case CLASS_DISP:
332 switch (datum_value)
334 case ARG_DISP16:
335 instr_data->displacement = instr_word;
336 nibl_count += 3;
337 break;
338 case ARG_DISP12:
339 instr_data->displacement = instr_word & 0x0fff;
340 nibl_count += 2;
341 break;
342 default:
343 break;
345 break;
346 case CLASS_IMM:
347 switch (datum_value)
349 case ARG_IMM4:
350 instr_data->immediate = instr_nibl;
351 break;
352 case ARG_NIM8:
353 instr_data->immediate = (-instr_byte);
354 nibl_count += 1;
355 break;
356 case ARG_IMM8:
357 instr_data->immediate = instr_byte;
358 nibl_count += 1;
359 break;
360 case ARG_IMM16:
361 instr_data->immediate = instr_word;
362 nibl_count += 3;
363 break;
364 case ARG_IMM32:
365 FETCH_DATA (info, nibl_count + 8);
366 instr_long = (instr_data->words[nibl_count] << 16)
367 | (instr_data->words[nibl_count + 4]);
368 instr_data->immediate = instr_long;
369 nibl_count += 7;
370 break;
371 case ARG_IMMN:
372 instr_data->immediate = instr_nibl - 1;
373 break;
374 case ARG_IMM4M1:
375 instr_data->immediate = instr_nibl + 1;
376 break;
377 case ARG_IMM_1:
378 instr_data->immediate = 1;
379 break;
380 case ARG_IMM_2:
381 instr_data->immediate = 2;
382 break;
383 case ARG_IMM2:
384 instr_data->immediate = instr_nibl & 0x3;
385 break;
386 default:
387 break;
389 break;
390 case CLASS_CC:
391 instr_data->cond_code = instr_nibl;
392 break;
393 case CLASS_CTRL:
394 instr_data->ctrl_code = instr_nibl;
395 break;
396 case CLASS_DA:
397 case CLASS_ADDRESS:
398 if (is_segmented)
400 if (instr_nibl & 0x8)
402 FETCH_DATA (info, nibl_count + 8);
403 instr_long = (instr_data->words[nibl_count] << 16)
404 | (instr_data->words[nibl_count + 4]);
405 instr_data->address = ((instr_word & 0x7f00) << 8) +
406 (instr_long & 0xffff);
407 nibl_count += 7;
409 else
411 instr_data->address = ((instr_word & 0x7f00) << 8) +
412 (instr_word & 0x00ff);
413 nibl_count += 3;
416 else
418 instr_data->address = instr_word;
419 nibl_count += 3;
421 break;
422 case CLASS_0CCC:
423 instr_data->cond_code = instr_nibl & 0x7;
424 break;
425 case CLASS_1CCC:
426 instr_data->cond_code = instr_nibl & 0x7;
427 break;
428 case CLASS_0DISP7:
429 instr_data->displacement = instr_byte & 0x7f;
430 nibl_count += 1;
431 break;
432 case CLASS_1DISP7:
433 instr_data->displacement = instr_byte & 0x7f;
434 nibl_count += 1;
435 break;
436 case CLASS_01II:
437 instr_data->interrupts = instr_nibl & 0x3;
438 break;
439 case CLASS_00II:
440 instr_data->interrupts = instr_nibl & 0x3;
441 break;
442 case CLASS_BIT:
443 /* do nothing */
444 break;
445 case CLASS_IR:
446 instr_data->arg_reg[datum_value] = instr_nibl;
447 break;
448 case CLASS_FLAGS:
449 instr_data->flags = instr_nibl;
450 break;
451 case CLASS_REG:
452 instr_data->arg_reg[datum_value] = instr_nibl;
453 break;
454 case CLASS_REG_BYTE:
455 instr_data->arg_reg[datum_value] = instr_nibl;
456 break;
457 case CLASS_REG_WORD:
458 instr_data->arg_reg[datum_value] = instr_nibl;
459 break;
460 case CLASS_REG_QUAD:
461 instr_data->arg_reg[datum_value] = instr_nibl;
462 break;
463 case CLASS_REG_LONG:
464 instr_data->arg_reg[datum_value] = instr_nibl;
465 break;
466 case CLASS_REGN0:
467 instr_data->arg_reg[datum_value] = instr_nibl;
468 break;
469 default:
470 break;
473 loop += 1;
474 nibl_count += 1;
478 static void
479 unparse_instr (instr_data)
480 instr_data_s *instr_data;
482 unsigned short tabl_datum, datum_class, datum_value;
483 int loop, loop_limit;
484 char out_str[80], tmp_str[25];
486 sprintf (out_str, "\t%s\t", z8k_table[instr_data->tabl_index].name);
488 loop_limit = z8k_table[instr_data->tabl_index].noperands;
489 for (loop = 0; loop < loop_limit; loop++)
491 if (loop)
492 strcat (out_str, ",");
494 tabl_datum = z8k_table[instr_data->tabl_index].arg_info[loop];
495 datum_class = tabl_datum & CLASS_MASK;
496 datum_value = tabl_datum & ~CLASS_MASK;
498 switch (datum_class)
500 case CLASS_X:
501 sprintf (tmp_str, "0x%0lx(R%ld)", instr_data->address,
502 instr_data->arg_reg[datum_value]);
503 strcat (out_str, tmp_str);
504 break;
505 case CLASS_BA:
506 sprintf (tmp_str, "r%ld(#%lx)", instr_data->arg_reg[datum_value],
507 instr_data->immediate);
508 strcat (out_str, tmp_str);
509 break;
510 case CLASS_BX:
511 sprintf (tmp_str, "r%ld(R%ld)", instr_data->arg_reg[datum_value],
512 instr_data->arg_reg[ARG_RX]);
513 strcat (out_str, tmp_str);
514 break;
515 case CLASS_DISP:
516 sprintf (tmp_str, "#0x%0lx", instr_data->displacement);
517 strcat (out_str, tmp_str);
518 break;
519 case CLASS_IMM:
520 sprintf (tmp_str, "#0x%0lx", instr_data->immediate);
521 strcat (out_str, tmp_str);
522 break;
523 case CLASS_CC:
524 sprintf (tmp_str, "%s", codes[instr_data->cond_code]);
525 strcat (out_str, tmp_str);
526 break;
527 case CLASS_CTRL:
528 sprintf (tmp_str, "0x%0lx", instr_data->ctrl_code);
529 strcat (out_str, tmp_str);
530 break;
531 case CLASS_DA:
532 case CLASS_ADDRESS:
533 sprintf (tmp_str, "#0x%0lx", instr_data->address);
534 strcat (out_str, tmp_str);
535 break;
536 case CLASS_IR:
537 sprintf (tmp_str, "@R%ld", instr_data->arg_reg[datum_value]);
538 strcat (out_str, tmp_str);
539 break;
540 case CLASS_FLAGS:
541 sprintf (tmp_str, "0x%0lx", instr_data->flags);
542 strcat (out_str, tmp_str);
543 break;
544 case CLASS_REG_BYTE:
545 if (instr_data->arg_reg[datum_value] >= 0x8)
547 sprintf (tmp_str, "rl%ld",
548 instr_data->arg_reg[datum_value] - 0x8);
550 else
552 sprintf (tmp_str, "rh%ld", instr_data->arg_reg[datum_value]);
554 strcat (out_str, tmp_str);
555 break;
556 case CLASS_REG_WORD:
557 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
558 strcat (out_str, tmp_str);
559 break;
560 case CLASS_REG_QUAD:
561 sprintf (tmp_str, "rq%ld", instr_data->arg_reg[datum_value]);
562 strcat (out_str, tmp_str);
563 break;
564 case CLASS_REG_LONG:
565 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
566 strcat (out_str, tmp_str);
567 break;
568 default:
569 break;
573 strcpy (instr_data->instr_asmsrc, out_str);