1 /* Disassemble z8000 code.
2 Copyright 1992, 1993, 1998, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 /* These are all indexed by nibble number (i.e only every other entry
31 of bytes is used, and every 4th entry of words). */
32 unsigned char nibbles[24];
33 unsigned char bytes[24];
34 unsigned short words[24];
36 /* Nibble number of first word not yet fetched. */
42 char instr_asmsrc[80];
43 unsigned long arg_reg[0x0f];
44 unsigned long immediate;
45 unsigned long displacement;
46 unsigned long address;
47 unsigned long cond_code;
48 unsigned long ctrl_code;
50 unsigned long interrupts;
53 static int fetch_data PARAMS ((struct disassemble_info *, int));
56 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
57 to ADDR (exclusive) are valid. Returns 1 for success, longjmps
59 #define FETCH_DATA(info, nibble) \
60 ((nibble) < ((instr_data_s *) (info->private_data))->max_fetched \
61 ? 1 : fetch_data ((info), (nibble)))
64 fetch_data (info, nibble)
65 struct disassemble_info *info;
68 unsigned char mybuf[20];
70 instr_data_s *priv = (instr_data_s *) info->private_data;
72 if ((nibble % 4) != 0)
75 status = (*info->read_memory_func) (priv->insn_start,
81 (*info->memory_error_func) (status, priv->insn_start, info);
82 longjmp (priv->bailout, 1);
87 unsigned char *p = mybuf;
89 for (i = 0; i < nibble;)
91 priv->words[i] = (p[0] << 8) | p[1];
94 priv->nibbles[i++] = *p >> 4;
95 priv->nibbles[i++] = *p & 0xf;
99 priv->nibbles[i++] = *p >> 4;
100 priv->nibbles[i++] = *p & 0xf;
105 priv->max_fetched = nibble;
109 static char *codes[16] = {
128 static char *ctrl_names[8] = {
139 static int seg_length;
140 static int print_insn_z8k PARAMS ((bfd_vma, disassemble_info *, int));
141 int z8k_lookup_instr PARAMS ((unsigned char *, disassemble_info *));
142 static void output_instr
143 PARAMS ((instr_data_s *, unsigned long, disassemble_info *));
144 static void unpack_instr PARAMS ((instr_data_s *, int, disassemble_info *));
145 static void unparse_instr PARAMS ((instr_data_s *, int));
148 print_insn_z8k (addr, info, is_segmented)
150 disassemble_info *info;
153 instr_data_s instr_data;
155 info->private_data = (PTR) &instr_data;
156 instr_data.max_fetched = 0;
157 instr_data.insn_start = addr;
158 if (setjmp (instr_data.bailout) != 0)
162 instr_data.tabl_index = z8k_lookup_instr (instr_data.nibbles, info);
163 if (instr_data.tabl_index > 0)
165 unpack_instr (&instr_data, is_segmented, info);
166 unparse_instr (&instr_data, is_segmented);
167 output_instr (&instr_data, addr, info);
168 return z8k_table[instr_data.tabl_index].length + seg_length;
172 FETCH_DATA (info, 4);
173 (*info->fprintf_func) (info->stream, ".word %02x%02x",
174 instr_data.bytes[0], instr_data.bytes[2]);
180 print_insn_z8001 (addr, info)
182 disassemble_info *info;
184 return print_insn_z8k (addr, info, 1);
188 print_insn_z8002 (addr, info)
190 disassemble_info *info;
192 return print_insn_z8k (addr, info, 0);
196 z8k_lookup_instr (nibbles, info)
197 unsigned char *nibbles;
198 disassemble_info *info;
201 int nibl_index, tabl_index;
203 unsigned short instr_nibl;
204 unsigned short tabl_datum, datum_class, datum_value;
208 while (!nibl_matched && z8k_table[tabl_index].name)
212 nibl_index < z8k_table[tabl_index].length * 2 && nibl_matched;
215 if ((nibl_index % 4) == 0)
216 /* Fetch one word at a time. */
217 FETCH_DATA (info, nibl_index + 4);
218 instr_nibl = nibbles[nibl_index];
220 tabl_datum = z8k_table[tabl_index].byte_info[nibl_index];
221 datum_class = tabl_datum & CLASS_MASK;
222 datum_value = ~CLASS_MASK & tabl_datum;
227 if (datum_value != instr_nibl)
231 if (!((~instr_nibl) & 0x4))
235 if (!(instr_nibl & 0x4))
239 if (!((~instr_nibl) & 0x8))
243 if (!(instr_nibl & 0x8))
247 if (!((~instr_nibl) & 0x8))
252 if (!(instr_nibl & 0x8))
261 if ((instr_nibl | 0x2) != (datum_value | 0x2))
279 output_instr (instr_data, addr, info)
280 instr_data_s *instr_data;
281 unsigned long addr ATTRIBUTE_UNUSED;
282 disassemble_info *info;
284 int loop, loop_limit;
288 strcpy (out_str, "\t");
290 loop_limit = (z8k_table[instr_data->tabl_index].length + seg_length) * 2;
291 FETCH_DATA (info, loop_limit);
292 for (loop = 0; loop < loop_limit; loop++)
294 sprintf (tmp_str, "%x", instr_data->nibbles[loop]);
295 strcat (out_str, tmp_str);
300 strcat (out_str, " ");
303 strcat (out_str, instr_data->instr_asmsrc);
305 (*info->fprintf_func) (info->stream, "%s", out_str);
309 unpack_instr (instr_data, is_segmented, info)
310 instr_data_s *instr_data;
312 disassemble_info *info;
314 int nibl_count, loop;
315 unsigned short instr_nibl, instr_byte, instr_word;
317 unsigned int tabl_datum, datum_class;
318 unsigned short datum_value;
323 while (z8k_table[instr_data->tabl_index].byte_info[loop] != 0)
325 FETCH_DATA (info, nibl_count + 4 - (nibl_count % 4));
326 instr_nibl = instr_data->nibbles[nibl_count];
327 instr_byte = instr_data->bytes[nibl_count & ~1];
328 instr_word = instr_data->words[nibl_count & ~3];
330 tabl_datum = z8k_table[instr_data->tabl_index].byte_info[loop];
331 datum_class = tabl_datum & CLASS_MASK;
332 datum_value = tabl_datum & ~CLASS_MASK;
340 instr_data->displacement = instr_data->insn_start + 4
341 + (signed short) (instr_word & 0xffff);
345 if (instr_word & 0x800)
347 /* neg. 12 bit displacement */
348 instr_data->displacement = instr_data->insn_start + 2
349 - (signed short) ((instr_word & 0xfff) | 0xf000) * 2;
353 instr_data->displacement = instr_data->insn_start + 2
354 - (instr_word & 0x0fff) * 2;
366 instr_data->immediate = instr_nibl;
369 instr_data->immediate = (-instr_byte);
373 instr_data->immediate = instr_byte;
377 instr_data->immediate = instr_word;
381 FETCH_DATA (info, nibl_count + 8);
382 instr_long = (instr_data->words[nibl_count] << 16)
383 | (instr_data->words[nibl_count + 4]);
384 instr_data->immediate = instr_long;
388 instr_data->immediate = instr_nibl - 1;
391 instr_data->immediate = instr_nibl + 1;
394 instr_data->immediate = 1;
397 instr_data->immediate = 2;
400 instr_data->immediate = instr_nibl & 0x3;
407 instr_data->cond_code = instr_nibl;
412 if (instr_nibl & 0x8)
414 FETCH_DATA (info, nibl_count + 8);
415 instr_long = (instr_data->words[nibl_count] << 16)
416 | (instr_data->words[nibl_count + 4]);
417 instr_data->address = ((instr_word & 0x7f00) << 8)
418 + (instr_long & 0xffff);
424 instr_data->address = ((instr_word & 0x7f00) << 8)
425 + (instr_word & 0x00ff);
431 instr_data->address = instr_word;
437 instr_data->ctrl_code = instr_nibl & 0x7;
440 instr_data->displacement =
441 instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
445 instr_data->displacement =
446 instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
450 instr_data->interrupts = instr_nibl & 0x3;
453 instr_data->interrupts = instr_nibl & 0x3;
456 instr_data->ctrl_code = instr_nibl & 0x7;
459 instr_data->flags = instr_nibl;
462 instr_data->arg_reg[datum_value] = instr_nibl;
465 instr_data->arg_reg[datum_value] = instr_nibl;
468 instr_data->displacement =
469 instr_data->insn_start + 2 + (signed char) instr_byte * 2;
483 unparse_instr (instr_data, is_segmented)
484 instr_data_s *instr_data;
487 unsigned short datum_value;
488 unsigned int tabl_datum, datum_class;
489 int loop, loop_limit;
490 char out_str[80], tmp_str[25];
492 sprintf (out_str, "\t%s\t", z8k_table[instr_data->tabl_index].name);
494 loop_limit = z8k_table[instr_data->tabl_index].noperands;
495 for (loop = 0; loop < loop_limit; loop++)
498 strcat (out_str, ",");
500 tabl_datum = z8k_table[instr_data->tabl_index].arg_info[loop];
501 datum_class = tabl_datum & CLASS_MASK;
502 datum_value = tabl_datum & ~CLASS_MASK;
507 sprintf (tmp_str, "0x%0lx(R%ld)", instr_data->address,
508 instr_data->arg_reg[datum_value]);
509 strcat (out_str, tmp_str);
512 sprintf (tmp_str, "r%ld(#%lx)", instr_data->arg_reg[datum_value],
513 instr_data->immediate);
514 strcat (out_str, tmp_str);
517 sprintf (tmp_str, "r%ld(R%ld)", instr_data->arg_reg[datum_value],
518 instr_data->arg_reg[ARG_RX]);
519 strcat (out_str, tmp_str);
522 sprintf (tmp_str, "0x%0lx", instr_data->displacement);
523 strcat (out_str, tmp_str);
526 sprintf (tmp_str, "#0x%0lx", instr_data->immediate);
527 strcat (out_str, tmp_str);
530 sprintf (tmp_str, "%s", codes[instr_data->cond_code]);
531 strcat (out_str, tmp_str);
534 sprintf (tmp_str, "%s", ctrl_names[instr_data->ctrl_code]);
535 strcat (out_str, tmp_str);
539 sprintf (tmp_str, "0x%0lx", instr_data->address);
540 strcat (out_str, tmp_str);
544 sprintf (tmp_str, "@rr%ld", instr_data->arg_reg[datum_value]);
546 sprintf (tmp_str, "@r%ld", instr_data->arg_reg[datum_value]);
547 strcat (out_str, tmp_str);
550 sprintf (tmp_str, "0x%0lx", instr_data->flags);
551 strcat (out_str, tmp_str);
554 if (instr_data->arg_reg[datum_value] >= 0x8)
555 sprintf (tmp_str, "rl%ld",
556 instr_data->arg_reg[datum_value] - 0x8);
558 sprintf (tmp_str, "rh%ld", instr_data->arg_reg[datum_value]);
559 strcat (out_str, tmp_str);
562 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
563 strcat (out_str, tmp_str);
566 sprintf (tmp_str, "rq%ld", instr_data->arg_reg[datum_value]);
567 strcat (out_str, tmp_str);
570 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
571 strcat (out_str, tmp_str);
575 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
577 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
578 strcat (out_str, tmp_str);
586 strcpy (instr_data->instr_asmsrc, out_str);