1 /* Disassemble z8000 code.
2 Copyright (C) 1992-2020 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. */
22 #include "disassemble.h"
23 #include "libiberty.h"
32 /* These are all indexed by nibble number (i.e only every other entry
33 of bytes is used, and every 4th entry of words). */
34 unsigned char nibbles
[24];
35 unsigned char bytes
[24];
36 unsigned short words
[24];
38 /* Nibble number of first word not yet fetched. */
39 unsigned int max_fetched
;
41 OPCODES_SIGJMP_BUF bailout
;
44 char instr_asmsrc
[80];
45 unsigned long arg_reg
[0x0f];
46 unsigned long immediate
;
47 unsigned long displacement
;
48 unsigned long address
;
49 unsigned long cond_code
;
50 unsigned long ctrl_code
;
52 unsigned long interrupts
;
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 (struct disassemble_info
*info
, int nibble
)
66 unsigned char mybuf
[20];
68 instr_data_s
*priv
= (instr_data_s
*) info
->private_data
;
70 if ((nibble
% 4) != 0)
73 status
= (*info
->read_memory_func
) (priv
->insn_start
,
79 (*info
->memory_error_func
) (status
, priv
->insn_start
, info
);
80 OPCODES_SIGLONGJMP (priv
->bailout
, 1);
85 unsigned char *p
= mybuf
;
87 for (i
= 0; i
< nibble
;)
89 priv
->words
[i
] = (p
[0] << 8) | p
[1];
92 priv
->nibbles
[i
++] = *p
>> 4;
93 priv
->nibbles
[i
++] = *p
& 0xf;
97 priv
->nibbles
[i
++] = *p
>> 4;
98 priv
->nibbles
[i
++] = *p
& 0xf;
103 priv
->max_fetched
= nibble
;
107 static char *codes
[16] =
127 static char *ctrl_names
[8] =
139 static int seg_length
;
140 int z8k_lookup_instr (unsigned char *, disassemble_info
*);
141 static void output_instr (instr_data_s
*, unsigned long, disassemble_info
*);
142 static void unpack_instr (instr_data_s
*, int, disassemble_info
*);
143 static void unparse_instr (instr_data_s
*, int);
146 print_insn_z8k (bfd_vma addr
, disassemble_info
*info
, int is_segmented
)
148 instr_data_s instr_data
;
150 info
->private_data
= (PTR
) &instr_data
;
151 instr_data
.max_fetched
= 0;
152 instr_data
.insn_start
= addr
;
153 if (OPCODES_SIGSETJMP (instr_data
.bailout
) != 0)
157 info
->bytes_per_chunk
= 2;
158 info
->bytes_per_line
= 6;
159 info
->display_endian
= BFD_ENDIAN_BIG
;
161 instr_data
.tabl_index
= z8k_lookup_instr (instr_data
.nibbles
, info
);
162 if (instr_data
.tabl_index
>= 0)
164 unpack_instr (&instr_data
, is_segmented
, info
);
165 unparse_instr (&instr_data
, is_segmented
);
166 output_instr (&instr_data
, addr
, info
);
167 return z8k_table
[instr_data
.tabl_index
].length
+ seg_length
;
171 FETCH_DATA (info
, 4);
172 (*info
->fprintf_func
) (info
->stream
, ".word %02x%02x",
173 instr_data
.bytes
[0], instr_data
.bytes
[2]);
179 print_insn_z8001 (bfd_vma addr
, disassemble_info
*info
)
181 return print_insn_z8k (addr
, info
, 1);
185 print_insn_z8002 (bfd_vma addr
, disassemble_info
*info
)
187 return print_insn_z8k (addr
, info
, 0);
191 z8k_lookup_instr (unsigned char *nibbles
, disassemble_info
*info
)
193 unsigned int nibl_index
, tabl_index
;
196 unsigned short instr_nibl
;
197 unsigned short tabl_datum
, datum_class
, datum_value
;
201 FETCH_DATA (info
, 4);
202 while (!nibl_matched
&& z8k_table
[tabl_index
].name
)
207 && nibl_index
< ARRAY_SIZE (z8k_table
[0].byte_info
)
208 && nibl_index
< z8k_table
[tabl_index
].length
* 2;
211 if ((nibl_index
% 4) == 0)
213 /* Fetch data only if it isn't already there. */
214 if (nibl_index
>= 4 || (nibl_index
< 4 && need_fetch
))
215 FETCH_DATA (info
, nibl_index
+ 4); /* Fetch one word at a time. */
221 instr_nibl
= nibbles
[nibl_index
];
223 tabl_datum
= z8k_table
[tabl_index
].byte_info
[nibl_index
];
224 datum_class
= tabl_datum
& CLASS_MASK
;
225 datum_value
= ~CLASS_MASK
& tabl_datum
;
230 if (datum_value
!= instr_nibl
)
236 if (!((~instr_nibl
) & 0x4))
240 if (!(instr_nibl
& 0x4))
244 if (!((~instr_nibl
) & 0x8))
248 if (!(instr_nibl
& 0x8))
252 if (!((~instr_nibl
) & 0x8))
257 if (!(instr_nibl
& 0x8))
266 if ((instr_nibl
| 0x2) != (datum_value
| 0x2))
283 output_instr (instr_data_s
*instr_data
,
284 unsigned long addr ATTRIBUTE_UNUSED
,
285 disassemble_info
*info
)
287 unsigned int num_bytes
;
292 num_bytes
= (z8k_table
[instr_data
->tabl_index
].length
+ seg_length
) * 2;
293 FETCH_DATA (info
, num_bytes
);
295 strcat (out_str
, instr_data
->instr_asmsrc
);
297 (*info
->fprintf_func
) (info
->stream
, "%s", out_str
);
301 unpack_instr (instr_data_s
*instr_data
, int is_segmented
, disassemble_info
*info
)
303 unsigned int nibl_count
, loop
;
304 unsigned short instr_nibl
, instr_byte
, instr_word
;
306 unsigned int tabl_datum
, datum_class
;
307 unsigned short datum_value
;
313 while (z8k_table
[instr_data
->tabl_index
].byte_info
[loop
] != 0)
315 FETCH_DATA (info
, nibl_count
+ 4 - (nibl_count
% 4));
316 instr_nibl
= instr_data
->nibbles
[nibl_count
];
317 instr_byte
= instr_data
->bytes
[nibl_count
& ~1];
318 instr_word
= instr_data
->words
[nibl_count
& ~3];
320 tabl_datum
= z8k_table
[instr_data
->tabl_index
].byte_info
[loop
];
321 datum_class
= tabl_datum
& CLASS_MASK
;
322 datum_value
= tabl_datum
& ~CLASS_MASK
;
330 instr_data
->displacement
= instr_data
->insn_start
+ 4
331 + (signed short) (instr_word
& 0xffff);
335 if (instr_word
& 0x800)
336 /* Negative 12 bit displacement. */
337 instr_data
->displacement
= instr_data
->insn_start
+ 2
338 - (signed short) ((instr_word
& 0xfff) | 0xf000) * 2;
340 instr_data
->displacement
= instr_data
->insn_start
+ 2
341 - (instr_word
& 0x0fff) * 2;
353 instr_data
->immediate
= instr_nibl
;
356 instr_data
->immediate
= (- instr_nibl
) & 0xf;
359 instr_data
->immediate
= (- instr_byte
) & 0xff;
363 instr_data
->immediate
= instr_byte
;
367 instr_data
->immediate
= instr_word
;
371 FETCH_DATA (info
, nibl_count
+ 8);
372 instr_long
= ((unsigned) instr_data
->words
[nibl_count
] << 16
373 | instr_data
->words
[nibl_count
+ 4]);
374 instr_data
->immediate
= instr_long
;
378 instr_data
->immediate
= instr_nibl
- 1;
381 instr_data
->immediate
= instr_nibl
+ 1;
384 instr_data
->immediate
= 1;
387 instr_data
->immediate
= 2;
390 instr_data
->immediate
= instr_nibl
& 0x3;
397 instr_data
->cond_code
= instr_nibl
;
402 if (instr_nibl
& 0x8)
404 FETCH_DATA (info
, nibl_count
+ 8);
405 instr_long
= ((unsigned) instr_data
->words
[nibl_count
] << 16
406 | instr_data
->words
[nibl_count
+ 4]);
407 instr_data
->address
= ((instr_word
& 0x7f00) << 16
408 | (instr_long
& 0xffff));
414 instr_data
->address
= ((instr_word
& 0x7f00) << 16
415 | (instr_word
& 0x00ff));
421 instr_data
->address
= instr_word
;
427 instr_data
->ctrl_code
= instr_nibl
& 0x7;
430 instr_data
->displacement
=
431 instr_data
->insn_start
+ 2 - (instr_byte
& 0x7f) * 2;
435 instr_data
->displacement
=
436 instr_data
->insn_start
+ 2 - (instr_byte
& 0x7f) * 2;
440 instr_data
->interrupts
= instr_nibl
& 0x3;
443 instr_data
->interrupts
= instr_nibl
& 0x3;
447 instr_data
->ctrl_code
= instr_nibl
& 0x7;
450 instr_data
->flags
= instr_nibl
;
453 instr_data
->arg_reg
[datum_value
] = instr_nibl
;
456 instr_data
->arg_reg
[datum_value
] = instr_nibl
;
459 instr_data
->displacement
=
460 instr_data
->insn_start
+ 2 + (signed char) instr_byte
* 2;
464 instr_data
->immediate
= ((instr_nibl
>> 1) & 0x1) + 1;
478 print_intr(char *tmp_str
, unsigned long interrupts
)
483 if (! (interrupts
& 2))
485 strcat (tmp_str
, "vi");
488 if (! (interrupts
& 1))
490 if (comma
) strcat (tmp_str
, ",");
491 strcat (tmp_str
, "nvi");
496 print_flags(char *tmp_str
, unsigned long flags
)
503 strcat (tmp_str
, "c");
508 if (comma
) strcat (tmp_str
, ",");
509 strcat (tmp_str
, "z");
514 if (comma
) strcat (tmp_str
, ",");
515 strcat (tmp_str
, "s");
520 if (comma
) strcat (tmp_str
, ",");
521 strcat (tmp_str
, "p");
526 unparse_instr (instr_data_s
*instr_data
, int is_segmented
)
528 unsigned short datum_value
;
529 unsigned int tabl_datum
, datum_class
;
530 int loop
, loop_limit
;
531 char out_str
[80], tmp_str
[25];
533 sprintf (out_str
, "%s\t", z8k_table
[instr_data
->tabl_index
].name
);
535 loop_limit
= z8k_table
[instr_data
->tabl_index
].noperands
;
536 for (loop
= 0; loop
< loop_limit
; loop
++)
539 strcat (out_str
, ",");
541 tabl_datum
= z8k_table
[instr_data
->tabl_index
].arg_info
[loop
];
542 datum_class
= tabl_datum
& CLASS_MASK
;
543 datum_value
= tabl_datum
& ~CLASS_MASK
;
548 sprintf (tmp_str
, "0x%0lx(r%ld)", instr_data
->address
,
549 instr_data
->arg_reg
[datum_value
]);
550 strcat (out_str
, tmp_str
);
554 sprintf (tmp_str
, "rr%ld(#0x%lx)", instr_data
->arg_reg
[datum_value
],
555 instr_data
->immediate
);
557 sprintf (tmp_str
, "r%ld(#0x%lx)", instr_data
->arg_reg
[datum_value
],
558 instr_data
->immediate
);
559 strcat (out_str
, tmp_str
);
563 sprintf (tmp_str
, "rr%ld(r%ld)", instr_data
->arg_reg
[datum_value
],
564 instr_data
->arg_reg
[ARG_RX
]);
566 sprintf (tmp_str
, "r%ld(r%ld)", instr_data
->arg_reg
[datum_value
],
567 instr_data
->arg_reg
[ARG_RX
]);
568 strcat (out_str
, tmp_str
);
571 sprintf (tmp_str
, "0x%0lx", instr_data
->displacement
);
572 strcat (out_str
, tmp_str
);
575 if (datum_value
== ARG_IMM2
) /* True with EI/DI instructions only. */
577 print_intr (tmp_str
, instr_data
->interrupts
);
578 strcat (out_str
, tmp_str
);
581 sprintf (tmp_str
, "#0x%0lx", instr_data
->immediate
);
582 strcat (out_str
, tmp_str
);
585 sprintf (tmp_str
, "%s", codes
[instr_data
->cond_code
]);
586 strcat (out_str
, tmp_str
);
589 sprintf (tmp_str
, "%s", ctrl_names
[instr_data
->ctrl_code
]);
590 strcat (out_str
, tmp_str
);
594 sprintf (tmp_str
, "0x%0lx", instr_data
->address
);
595 strcat (out_str
, tmp_str
);
599 sprintf (tmp_str
, "@rr%ld", instr_data
->arg_reg
[datum_value
]);
601 sprintf (tmp_str
, "@r%ld", instr_data
->arg_reg
[datum_value
]);
602 strcat (out_str
, tmp_str
);
605 sprintf (tmp_str
, "@r%ld", instr_data
->arg_reg
[datum_value
]);
606 strcat (out_str
, tmp_str
);
609 print_flags(tmp_str
, instr_data
->flags
);
610 strcat (out_str
, tmp_str
);
613 if (instr_data
->arg_reg
[datum_value
] >= 0x8)
614 sprintf (tmp_str
, "rl%ld",
615 instr_data
->arg_reg
[datum_value
] - 0x8);
617 sprintf (tmp_str
, "rh%ld", instr_data
->arg_reg
[datum_value
]);
618 strcat (out_str
, tmp_str
);
621 sprintf (tmp_str
, "r%ld", instr_data
->arg_reg
[datum_value
]);
622 strcat (out_str
, tmp_str
);
625 sprintf (tmp_str
, "rq%ld", instr_data
->arg_reg
[datum_value
]);
626 strcat (out_str
, tmp_str
);
629 sprintf (tmp_str
, "rr%ld", instr_data
->arg_reg
[datum_value
]);
630 strcat (out_str
, tmp_str
);
634 sprintf (tmp_str
, "rr%ld", instr_data
->arg_reg
[datum_value
]);
636 sprintf (tmp_str
, "r%ld", instr_data
->arg_reg
[datum_value
]);
637 strcat (out_str
, tmp_str
);
645 strcpy (instr_data
->instr_asmsrc
, out_str
);