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 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
54 to ADDR (exclusive) are valid. Returns 1 for success, longjmps
56 #define FETCH_DATA(info, nibble) \
57 ((nibble) < ((instr_data_s *) (info->private_data))->max_fetched \
58 ? 1 : fetch_data ((info), (nibble)))
61 fetch_data (info, nibble)
62 struct disassemble_info *info;
65 unsigned char mybuf[20];
67 instr_data_s *priv = (instr_data_s *) info->private_data;
69 if ((nibble % 4) != 0)
72 status = (*info->read_memory_func) (priv->insn_start,
78 (*info->memory_error_func) (status, priv->insn_start, info);
79 longjmp (priv->bailout, 1);
84 unsigned char *p = mybuf;
86 for (i = 0; i < nibble;)
88 priv->words[i] = (p[0] << 8) | p[1];
91 priv->nibbles[i++] = *p >> 4;
92 priv->nibbles[i++] = *p & 0xf;
96 priv->nibbles[i++] = *p >> 4;
97 priv->nibbles[i++] = *p & 0xf;
102 priv->max_fetched = nibble;
106 static char *codes[16] = {
125 static char *ctrl_names[8] = {
136 static int seg_length;
137 int z8k_lookup_instr PARAMS ((unsigned char *, disassemble_info *));
138 static void output_instr
139 PARAMS ((instr_data_s *, unsigned long, disassemble_info *));
140 static void unpack_instr PARAMS ((instr_data_s *, int, disassemble_info *));
141 static void unparse_instr PARAMS ((instr_data_s *, int));
144 print_insn_z8k (addr, info, is_segmented)
146 disassemble_info *info;
149 instr_data_s instr_data;
151 info->private_data = (PTR) &instr_data;
152 instr_data.max_fetched = 0;
153 instr_data.insn_start = addr;
154 if (setjmp (instr_data.bailout) != 0)
158 instr_data.tabl_index = z8k_lookup_instr (instr_data.nibbles, info);
159 if (instr_data.tabl_index > 0)
161 unpack_instr (&instr_data, is_segmented, info);
162 unparse_instr (&instr_data, is_segmented);
163 output_instr (&instr_data, addr, info);
164 return z8k_table[instr_data.tabl_index].length + seg_length;
168 FETCH_DATA (info, 4);
169 (*info->fprintf_func) (info->stream, ".word %02x%02x",
170 instr_data.bytes[0], instr_data.bytes[2]);
176 print_insn_z8001 (addr, info)
178 disassemble_info *info;
180 return print_insn_z8k (addr, info, 1);
184 print_insn_z8002 (addr, info)
186 disassemble_info *info;
188 return print_insn_z8k (addr, info, 0);
192 z8k_lookup_instr (nibbles, info)
193 unsigned char *nibbles;
194 disassemble_info *info;
197 int nibl_index, tabl_index;
199 unsigned short instr_nibl;
200 unsigned short tabl_datum, datum_class, datum_value;
204 while (!nibl_matched && z8k_table[tabl_index].name)
208 nibl_index < z8k_table[tabl_index].length * 2 && nibl_matched;
211 if ((nibl_index % 4) == 0)
212 /* Fetch one word at a time. */
213 FETCH_DATA (info, nibl_index + 4);
214 instr_nibl = nibbles[nibl_index];
216 tabl_datum = z8k_table[tabl_index].byte_info[nibl_index];
217 datum_class = tabl_datum & CLASS_MASK;
218 datum_value = ~CLASS_MASK & tabl_datum;
223 if (datum_value != instr_nibl)
227 if (!((~instr_nibl) & 0x4))
231 if (!(instr_nibl & 0x4))
235 if (!((~instr_nibl) & 0x8))
239 if (!(instr_nibl & 0x8))
243 if (!((~instr_nibl) & 0x8))
248 if (!(instr_nibl & 0x8))
257 if ((instr_nibl | 0x2) != (datum_value | 0x2))
275 output_instr (instr_data, addr, info)
276 instr_data_s *instr_data;
278 disassemble_info *info;
280 int loop, loop_limit;
284 strcpy (out_str, "\t");
286 loop_limit = (z8k_table[instr_data->tabl_index].length + seg_length) * 2;
287 FETCH_DATA (info, loop_limit);
288 for (loop = 0; loop < loop_limit; loop++)
290 sprintf (tmp_str, "%x", instr_data->nibbles[loop]);
291 strcat (out_str, tmp_str);
296 strcat (out_str, " ");
299 strcat (out_str, instr_data->instr_asmsrc);
301 (*info->fprintf_func) (info->stream, "%s", out_str);
305 unpack_instr (instr_data, is_segmented, info)
306 instr_data_s *instr_data;
308 disassemble_info *info;
310 int nibl_count, loop;
311 unsigned short instr_nibl, instr_byte, instr_word;
313 unsigned int tabl_datum, datum_class;
314 unsigned short datum_value;
319 while (z8k_table[instr_data->tabl_index].byte_info[loop] != 0)
321 FETCH_DATA (info, nibl_count + 4 - (nibl_count % 4));
322 instr_nibl = instr_data->nibbles[nibl_count];
323 instr_byte = instr_data->bytes[nibl_count & ~1];
324 instr_word = instr_data->words[nibl_count & ~3];
326 tabl_datum = z8k_table[instr_data->tabl_index].byte_info[loop];
327 datum_class = tabl_datum & CLASS_MASK;
328 datum_value = tabl_datum & ~CLASS_MASK;
336 instr_data->displacement = instr_data->insn_start + 4
337 + (signed short) (instr_word & 0xffff);
341 if (instr_word & 0x800)
343 /* neg. 12 bit displacement */
344 instr_data->displacement = instr_data->insn_start + 2
345 - (signed short) ((instr_word & 0xfff) | 0xf000) * 2;
349 instr_data->displacement = instr_data->insn_start + 2
350 - (instr_word & 0x0fff) * 2;
362 instr_data->immediate = instr_nibl;
365 instr_data->immediate = (-instr_byte);
369 instr_data->immediate = instr_byte;
373 instr_data->immediate = instr_word;
377 FETCH_DATA (info, nibl_count + 8);
378 instr_long = (instr_data->words[nibl_count] << 16)
379 | (instr_data->words[nibl_count + 4]);
380 instr_data->immediate = instr_long;
384 instr_data->immediate = instr_nibl - 1;
387 instr_data->immediate = instr_nibl + 1;
390 instr_data->immediate = 1;
393 instr_data->immediate = 2;
396 instr_data->immediate = instr_nibl & 0x3;
403 instr_data->cond_code = instr_nibl;
408 if (instr_nibl & 0x8)
410 FETCH_DATA (info, nibl_count + 8);
411 instr_long = (instr_data->words[nibl_count] << 16)
412 | (instr_data->words[nibl_count + 4]);
413 instr_data->address = ((instr_word & 0x7f00) << 8)
414 + (instr_long & 0xffff);
420 instr_data->address = ((instr_word & 0x7f00) << 8)
421 + (instr_word & 0x00ff);
427 instr_data->address = instr_word;
433 instr_data->ctrl_code = instr_nibl & 0x7;
436 instr_data->displacement =
437 instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
441 instr_data->displacement =
442 instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
446 instr_data->interrupts = instr_nibl & 0x3;
449 instr_data->interrupts = instr_nibl & 0x3;
452 instr_data->ctrl_code = instr_nibl & 0x7;
455 instr_data->flags = instr_nibl;
458 instr_data->arg_reg[datum_value] = instr_nibl;
461 instr_data->arg_reg[datum_value] = instr_nibl;
464 instr_data->displacement =
465 instr_data->insn_start + 2 + (signed char) instr_byte * 2;
479 unparse_instr (instr_data, is_segmented)
480 instr_data_s *instr_data;
483 unsigned short datum_value;
484 unsigned int tabl_datum, datum_class;
485 int loop, loop_limit;
486 char out_str[80], tmp_str[25];
488 sprintf (out_str, "\t%s\t", z8k_table[instr_data->tabl_index].name);
490 loop_limit = z8k_table[instr_data->tabl_index].noperands;
491 for (loop = 0; loop < loop_limit; loop++)
494 strcat (out_str, ",");
496 tabl_datum = z8k_table[instr_data->tabl_index].arg_info[loop];
497 datum_class = tabl_datum & CLASS_MASK;
498 datum_value = tabl_datum & ~CLASS_MASK;
503 sprintf (tmp_str, "0x%0lx(R%ld)", instr_data->address,
504 instr_data->arg_reg[datum_value]);
505 strcat (out_str, tmp_str);
508 sprintf (tmp_str, "r%ld(#%lx)", instr_data->arg_reg[datum_value],
509 instr_data->immediate);
510 strcat (out_str, tmp_str);
513 sprintf (tmp_str, "r%ld(R%ld)", instr_data->arg_reg[datum_value],
514 instr_data->arg_reg[ARG_RX]);
515 strcat (out_str, tmp_str);
518 sprintf (tmp_str, "0x%0lx", instr_data->displacement);
519 strcat (out_str, tmp_str);
522 sprintf (tmp_str, "#0x%0lx", instr_data->immediate);
523 strcat (out_str, tmp_str);
526 sprintf (tmp_str, "%s", codes[instr_data->cond_code]);
527 strcat (out_str, tmp_str);
530 sprintf (tmp_str, "%s", ctrl_names[instr_data->ctrl_code]);
531 strcat (out_str, tmp_str);
535 sprintf (tmp_str, "0x%0lx", instr_data->address);
536 strcat (out_str, tmp_str);
539 sprintf (tmp_str, "@R%ld", instr_data->arg_reg[datum_value]);
540 strcat (out_str, tmp_str);
543 sprintf (tmp_str, "0x%0lx", instr_data->flags);
544 strcat (out_str, tmp_str);
547 if (instr_data->arg_reg[datum_value] >= 0x8)
549 sprintf (tmp_str, "rl%ld",
550 instr_data->arg_reg[datum_value] - 0x8);
554 sprintf (tmp_str, "rh%ld", instr_data->arg_reg[datum_value]);
556 strcat (out_str, tmp_str);
559 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
560 strcat (out_str, tmp_str);
563 sprintf (tmp_str, "rq%ld", instr_data->arg_reg[datum_value]);
564 strcat (out_str, tmp_str);
567 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
568 strcat (out_str, tmp_str);
572 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
574 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
575 strcat (out_str, tmp_str);
583 strcpy (instr_data->instr_asmsrc, out_str);