1 /* Disassemble z8000 code.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
4 This file is part of the GNU opcodes library.
6 This library 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 3, or (at your option)
11 It is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this file; see the file COPYING. If not, write to the
18 Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
31 /* These are all indexed by nibble number (i.e only every other entry
32 of bytes is used, and every 4th entry of words). */
33 unsigned char nibbles[24];
34 unsigned char bytes[24];
35 unsigned short words[24];
37 /* Nibble number of first word not yet fetched. */
40 OPCODES_SIGJMP_BUF bailout;
43 char instr_asmsrc[80];
44 unsigned long arg_reg[0x0f];
45 unsigned long immediate;
46 unsigned long displacement;
47 unsigned long address;
48 unsigned long cond_code;
49 unsigned long ctrl_code;
51 unsigned long interrupts;
55 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
56 to ADDR (exclusive) are valid. Returns 1 for success, longjmps
58 #define FETCH_DATA(info, nibble) \
59 ((nibble) < ((instr_data_s *) (info->private_data))->max_fetched \
60 ? 1 : fetch_data ((info), (nibble)))
63 fetch_data (struct disassemble_info *info, int nibble)
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 OPCODES_SIGLONGJMP (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] =
126 static char *ctrl_names[8] =
138 static int seg_length;
139 int z8k_lookup_instr (unsigned char *, disassemble_info *);
140 static void output_instr (instr_data_s *, unsigned long, disassemble_info *);
141 static void unpack_instr (instr_data_s *, int, disassemble_info *);
142 static void unparse_instr (instr_data_s *, int);
145 print_insn_z8k (bfd_vma addr, disassemble_info *info, int is_segmented)
147 instr_data_s instr_data;
149 info->private_data = (PTR) &instr_data;
150 instr_data.max_fetched = 0;
151 instr_data.insn_start = addr;
152 if (OPCODES_SIGSETJMP (instr_data.bailout) != 0)
156 info->bytes_per_chunk = 2;
157 info->bytes_per_line = 6;
158 info->display_endian = BFD_ENDIAN_BIG;
160 instr_data.tabl_index = z8k_lookup_instr (instr_data.nibbles, info);
161 if (instr_data.tabl_index >= 0)
163 unpack_instr (&instr_data, is_segmented, info);
164 unparse_instr (&instr_data, is_segmented);
165 output_instr (&instr_data, addr, info);
166 return z8k_table[instr_data.tabl_index].length + seg_length;
170 FETCH_DATA (info, 4);
171 (*info->fprintf_func) (info->stream, ".word %02x%02x",
172 instr_data.bytes[0], instr_data.bytes[2]);
178 print_insn_z8001 (bfd_vma addr, disassemble_info *info)
180 return print_insn_z8k (addr, info, 1);
184 print_insn_z8002 (bfd_vma addr, disassemble_info *info)
186 return print_insn_z8k (addr, info, 0);
190 z8k_lookup_instr (unsigned char *nibbles, disassemble_info *info)
192 int nibl_index, tabl_index;
195 unsigned short instr_nibl;
196 unsigned short tabl_datum, datum_class, datum_value;
200 FETCH_DATA (info, 4);
201 while (!nibl_matched && z8k_table[tabl_index].name)
205 nibl_index < z8k_table[tabl_index].length * 2 && nibl_matched;
208 if ((nibl_index % 4) == 0)
210 /* Fetch data only if it isn't already there. */
211 if (nibl_index >= 4 || (nibl_index < 4 && need_fetch))
212 FETCH_DATA (info, nibl_index + 4); /* Fetch one word at a time. */
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)
233 if (!((~instr_nibl) & 0x4))
237 if (!(instr_nibl & 0x4))
241 if (!((~instr_nibl) & 0x8))
245 if (!(instr_nibl & 0x8))
249 if (!((~instr_nibl) & 0x8))
254 if (!(instr_nibl & 0x8))
263 if ((instr_nibl | 0x2) != (datum_value | 0x2))
280 output_instr (instr_data_s *instr_data,
281 unsigned long addr ATTRIBUTE_UNUSED,
282 disassemble_info *info)
289 num_bytes = (z8k_table[instr_data->tabl_index].length + seg_length) * 2;
290 FETCH_DATA (info, num_bytes);
292 strcat (out_str, instr_data->instr_asmsrc);
294 (*info->fprintf_func) (info->stream, "%s", out_str);
298 unpack_instr (instr_data_s *instr_data, int is_segmented, disassemble_info *info)
300 int nibl_count, loop;
301 unsigned short instr_nibl, instr_byte, instr_word;
303 unsigned int tabl_datum, datum_class;
304 unsigned short datum_value;
310 while (z8k_table[instr_data->tabl_index].byte_info[loop] != 0)
312 FETCH_DATA (info, nibl_count + 4 - (nibl_count % 4));
313 instr_nibl = instr_data->nibbles[nibl_count];
314 instr_byte = instr_data->bytes[nibl_count & ~1];
315 instr_word = instr_data->words[nibl_count & ~3];
317 tabl_datum = z8k_table[instr_data->tabl_index].byte_info[loop];
318 datum_class = tabl_datum & CLASS_MASK;
319 datum_value = tabl_datum & ~CLASS_MASK;
327 instr_data->displacement = instr_data->insn_start + 4
328 + (signed short) (instr_word & 0xffff);
332 if (instr_word & 0x800)
333 /* Negative 12 bit displacement. */
334 instr_data->displacement = instr_data->insn_start + 2
335 - (signed short) ((instr_word & 0xfff) | 0xf000) * 2;
337 instr_data->displacement = instr_data->insn_start + 2
338 - (instr_word & 0x0fff) * 2;
350 instr_data->immediate = instr_nibl;
353 instr_data->immediate = (- instr_nibl) & 0xf;
356 instr_data->immediate = (- instr_byte) & 0xff;
360 instr_data->immediate = instr_byte;
364 instr_data->immediate = instr_word;
368 FETCH_DATA (info, nibl_count + 8);
369 instr_long = (instr_data->words[nibl_count] << 16)
370 | (instr_data->words[nibl_count + 4]);
371 instr_data->immediate = instr_long;
375 instr_data->immediate = instr_nibl - 1;
378 instr_data->immediate = instr_nibl + 1;
381 instr_data->immediate = 1;
384 instr_data->immediate = 2;
387 instr_data->immediate = instr_nibl & 0x3;
394 instr_data->cond_code = instr_nibl;
399 if (instr_nibl & 0x8)
401 FETCH_DATA (info, nibl_count + 8);
402 instr_long = (instr_data->words[nibl_count] << 16)
403 | (instr_data->words[nibl_count + 4]);
404 instr_data->address = ((instr_word & 0x7f00) << 16)
405 + (instr_long & 0xffff);
411 instr_data->address = ((instr_word & 0x7f00) << 16)
412 + (instr_word & 0x00ff);
418 instr_data->address = instr_word;
424 instr_data->ctrl_code = instr_nibl & 0x7;
427 instr_data->displacement =
428 instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
432 instr_data->displacement =
433 instr_data->insn_start + 2 - (instr_byte & 0x7f) * 2;
437 instr_data->interrupts = instr_nibl & 0x3;
440 instr_data->interrupts = instr_nibl & 0x3;
444 instr_data->ctrl_code = instr_nibl & 0x7;
447 instr_data->flags = instr_nibl;
450 instr_data->arg_reg[datum_value] = instr_nibl;
453 instr_data->arg_reg[datum_value] = instr_nibl;
456 instr_data->displacement =
457 instr_data->insn_start + 2 + (signed char) instr_byte * 2;
461 instr_data->immediate = ((instr_nibl >> 1) & 0x1) + 1;
475 print_intr(char *tmp_str, unsigned long interrupts)
480 if (! (interrupts & 2))
482 strcat (tmp_str, "vi");
485 if (! (interrupts & 1))
487 if (comma) strcat (tmp_str, ",");
488 strcat (tmp_str, "nvi");
493 print_flags(char *tmp_str, unsigned long flags)
500 strcat (tmp_str, "c");
505 if (comma) strcat (tmp_str, ",");
506 strcat (tmp_str, "z");
511 if (comma) strcat (tmp_str, ",");
512 strcat (tmp_str, "s");
517 if (comma) strcat (tmp_str, ",");
518 strcat (tmp_str, "p");
523 unparse_instr (instr_data_s *instr_data, int is_segmented)
525 unsigned short datum_value;
526 unsigned int tabl_datum, datum_class;
527 int loop, loop_limit;
528 char out_str[80], tmp_str[25];
530 sprintf (out_str, "%s\t", z8k_table[instr_data->tabl_index].name);
532 loop_limit = z8k_table[instr_data->tabl_index].noperands;
533 for (loop = 0; loop < loop_limit; loop++)
536 strcat (out_str, ",");
538 tabl_datum = z8k_table[instr_data->tabl_index].arg_info[loop];
539 datum_class = tabl_datum & CLASS_MASK;
540 datum_value = tabl_datum & ~CLASS_MASK;
545 sprintf (tmp_str, "0x%0lx(r%ld)", instr_data->address,
546 instr_data->arg_reg[datum_value]);
547 strcat (out_str, tmp_str);
551 sprintf (tmp_str, "rr%ld(#0x%lx)", instr_data->arg_reg[datum_value],
552 instr_data->immediate);
554 sprintf (tmp_str, "r%ld(#0x%lx)", instr_data->arg_reg[datum_value],
555 instr_data->immediate);
556 strcat (out_str, tmp_str);
560 sprintf (tmp_str, "rr%ld(r%ld)", instr_data->arg_reg[datum_value],
561 instr_data->arg_reg[ARG_RX]);
563 sprintf (tmp_str, "r%ld(r%ld)", instr_data->arg_reg[datum_value],
564 instr_data->arg_reg[ARG_RX]);
565 strcat (out_str, tmp_str);
568 sprintf (tmp_str, "0x%0lx", instr_data->displacement);
569 strcat (out_str, tmp_str);
572 if (datum_value == ARG_IMM2) /* True with EI/DI instructions only. */
574 print_intr (tmp_str, instr_data->interrupts);
575 strcat (out_str, tmp_str);
578 sprintf (tmp_str, "#0x%0lx", instr_data->immediate);
579 strcat (out_str, tmp_str);
582 sprintf (tmp_str, "%s", codes[instr_data->cond_code]);
583 strcat (out_str, tmp_str);
586 sprintf (tmp_str, "%s", ctrl_names[instr_data->ctrl_code]);
587 strcat (out_str, tmp_str);
591 sprintf (tmp_str, "0x%0lx", instr_data->address);
592 strcat (out_str, tmp_str);
596 sprintf (tmp_str, "@rr%ld", instr_data->arg_reg[datum_value]);
598 sprintf (tmp_str, "@r%ld", instr_data->arg_reg[datum_value]);
599 strcat (out_str, tmp_str);
602 sprintf (tmp_str, "@r%ld", instr_data->arg_reg[datum_value]);
603 strcat (out_str, tmp_str);
606 print_flags(tmp_str, instr_data->flags);
607 strcat (out_str, tmp_str);
610 if (instr_data->arg_reg[datum_value] >= 0x8)
611 sprintf (tmp_str, "rl%ld",
612 instr_data->arg_reg[datum_value] - 0x8);
614 sprintf (tmp_str, "rh%ld", instr_data->arg_reg[datum_value]);
615 strcat (out_str, tmp_str);
618 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
619 strcat (out_str, tmp_str);
622 sprintf (tmp_str, "rq%ld", instr_data->arg_reg[datum_value]);
623 strcat (out_str, tmp_str);
626 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
627 strcat (out_str, tmp_str);
631 sprintf (tmp_str, "rr%ld", instr_data->arg_reg[datum_value]);
633 sprintf (tmp_str, "r%ld", instr_data->arg_reg[datum_value]);
634 strcat (out_str, tmp_str);
642 strcpy (instr_data->instr_asmsrc, out_str);