1 /* Disassemble z8000 code.
2 Copyright 1992, 1993, 1998, 2000, 2001, 2002, 2003
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,
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. */
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 static int fetch_data
PARAMS ((struct disassemble_info
*, int));
59 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
60 to ADDR (exclusive) are valid. Returns 1 for success, longjmps
62 #define FETCH_DATA(info, nibble) \
63 ((nibble) < ((instr_data_s *) (info->private_data))->max_fetched \
64 ? 1 : fetch_data ((info), (nibble)))
67 fetch_data (info
, nibble
)
68 struct disassemble_info
*info
;
71 unsigned char mybuf
[20];
73 instr_data_s
*priv
= (instr_data_s
*) info
->private_data
;
75 if ((nibble
% 4) != 0)
78 status
= (*info
->read_memory_func
) (priv
->insn_start
,
84 (*info
->memory_error_func
) (status
, priv
->insn_start
, info
);
85 longjmp (priv
->bailout
, 1);
90 unsigned char *p
= mybuf
;
92 for (i
= 0; i
< nibble
;)
94 priv
->words
[i
] = (p
[0] << 8) | p
[1];
97 priv
->nibbles
[i
++] = *p
>> 4;
98 priv
->nibbles
[i
++] = *p
& 0xf;
102 priv
->nibbles
[i
++] = *p
>> 4;
103 priv
->nibbles
[i
++] = *p
& 0xf;
108 priv
->max_fetched
= nibble
;
112 static char *codes
[16] =
132 static char *ctrl_names
[8] =
144 static int seg_length
;
145 static int print_insn_z8k
PARAMS ((bfd_vma
, disassemble_info
*, int));
146 int z8k_lookup_instr
PARAMS ((unsigned char *, disassemble_info
*));
147 static void output_instr
148 PARAMS ((instr_data_s
*, unsigned long, disassemble_info
*));
149 static void unpack_instr
PARAMS ((instr_data_s
*, int, disassemble_info
*));
150 static void unparse_instr
PARAMS ((instr_data_s
*, int));
153 print_insn_z8k (addr
, info
, is_segmented
)
155 disassemble_info
*info
;
158 instr_data_s instr_data
;
160 info
->private_data
= (PTR
) &instr_data
;
161 instr_data
.max_fetched
= 0;
162 instr_data
.insn_start
= addr
;
163 if (setjmp (instr_data
.bailout
) != 0)
167 info
->bytes_per_chunk
= 2;
168 info
->bytes_per_line
= 6;
169 info
->display_endian
= BFD_ENDIAN_BIG
;
171 instr_data
.tabl_index
= z8k_lookup_instr (instr_data
.nibbles
, info
);
172 if (instr_data
.tabl_index
>= 0)
174 unpack_instr (&instr_data
, is_segmented
, info
);
175 unparse_instr (&instr_data
, is_segmented
);
176 output_instr (&instr_data
, addr
, info
);
177 return z8k_table
[instr_data
.tabl_index
].length
+ seg_length
;
181 FETCH_DATA (info
, 4);
182 (*info
->fprintf_func
) (info
->stream
, ".word %02x%02x",
183 instr_data
.bytes
[0], instr_data
.bytes
[2]);
189 print_insn_z8001 (addr
, info
)
191 disassemble_info
*info
;
193 return print_insn_z8k (addr
, info
, 1);
197 print_insn_z8002 (addr
, info
)
199 disassemble_info
*info
;
201 return print_insn_z8k (addr
, info
, 0);
205 z8k_lookup_instr (nibbles
, info
)
206 unsigned char *nibbles
;
207 disassemble_info
*info
;
210 int nibl_index
, tabl_index
;
213 unsigned short instr_nibl
;
214 unsigned short tabl_datum
, datum_class
, datum_value
;
218 FETCH_DATA (info
, 4);
219 while (!nibl_matched
&& z8k_table
[tabl_index
].name
)
223 nibl_index
< z8k_table
[tabl_index
].length
* 2 && nibl_matched
;
226 if ((nibl_index
% 4) == 0)
228 /* Fetch data only if it isn't already there. */
229 if (nibl_index
>= 4 || (nibl_index
< 4 && need_fetch
))
230 FETCH_DATA (info
, nibl_index
+ 4); /* Fetch one word at a time. */
236 instr_nibl
= nibbles
[nibl_index
];
238 tabl_datum
= z8k_table
[tabl_index
].byte_info
[nibl_index
];
239 datum_class
= tabl_datum
& CLASS_MASK
;
240 datum_value
= ~CLASS_MASK
& tabl_datum
;
245 if (datum_value
!= instr_nibl
)
251 if (!((~instr_nibl
) & 0x4))
255 if (!(instr_nibl
& 0x4))
259 if (!((~instr_nibl
) & 0x8))
263 if (!(instr_nibl
& 0x8))
267 if (!((~instr_nibl
) & 0x8))
272 if (!(instr_nibl
& 0x8))
281 if ((instr_nibl
| 0x2) != (datum_value
| 0x2))
298 output_instr (instr_data
, addr
, info
)
299 instr_data_s
*instr_data
;
300 unsigned long addr ATTRIBUTE_UNUSED
;
301 disassemble_info
*info
;
308 num_bytes
= (z8k_table
[instr_data
->tabl_index
].length
+ seg_length
) * 2;
309 FETCH_DATA (info
, num_bytes
);
311 strcat (out_str
, instr_data
->instr_asmsrc
);
313 (*info
->fprintf_func
) (info
->stream
, "%s", out_str
);
317 unpack_instr (instr_data
, is_segmented
, info
)
318 instr_data_s
*instr_data
;
320 disassemble_info
*info
;
322 int nibl_count
, loop
;
323 unsigned short instr_nibl
, instr_byte
, instr_word
;
325 unsigned int tabl_datum
, datum_class
;
326 unsigned short datum_value
;
332 while (z8k_table
[instr_data
->tabl_index
].byte_info
[loop
] != 0)
334 FETCH_DATA (info
, nibl_count
+ 4 - (nibl_count
% 4));
335 instr_nibl
= instr_data
->nibbles
[nibl_count
];
336 instr_byte
= instr_data
->bytes
[nibl_count
& ~1];
337 instr_word
= instr_data
->words
[nibl_count
& ~3];
339 tabl_datum
= z8k_table
[instr_data
->tabl_index
].byte_info
[loop
];
340 datum_class
= tabl_datum
& CLASS_MASK
;
341 datum_value
= tabl_datum
& ~CLASS_MASK
;
349 instr_data
->displacement
= instr_data
->insn_start
+ 4
350 + (signed short) (instr_word
& 0xffff);
354 if (instr_word
& 0x800)
355 /* Negative 12 bit displacement. */
356 instr_data
->displacement
= instr_data
->insn_start
+ 2
357 - (signed short) ((instr_word
& 0xfff) | 0xf000) * 2;
359 instr_data
->displacement
= instr_data
->insn_start
+ 2
360 - (instr_word
& 0x0fff) * 2;
372 instr_data
->immediate
= instr_nibl
;
375 instr_data
->immediate
= (- instr_nibl
) & 0xf;
378 instr_data
->immediate
= (- instr_byte
) & 0xff;
382 instr_data
->immediate
= instr_byte
;
386 instr_data
->immediate
= instr_word
;
390 FETCH_DATA (info
, nibl_count
+ 8);
391 instr_long
= (instr_data
->words
[nibl_count
] << 16)
392 | (instr_data
->words
[nibl_count
+ 4]);
393 instr_data
->immediate
= instr_long
;
397 instr_data
->immediate
= instr_nibl
- 1;
400 instr_data
->immediate
= instr_nibl
+ 1;
403 instr_data
->immediate
= 1;
406 instr_data
->immediate
= 2;
409 instr_data
->immediate
= instr_nibl
& 0x3;
416 instr_data
->cond_code
= instr_nibl
;
421 if (instr_nibl
& 0x8)
423 FETCH_DATA (info
, nibl_count
+ 8);
424 instr_long
= (instr_data
->words
[nibl_count
] << 16)
425 | (instr_data
->words
[nibl_count
+ 4]);
426 instr_data
->address
= ((instr_word
& 0x7f00) << 16)
427 + (instr_long
& 0xffff);
433 instr_data
->address
= ((instr_word
& 0x7f00) << 16)
434 + (instr_word
& 0x00ff);
440 instr_data
->address
= instr_word
;
446 instr_data
->ctrl_code
= instr_nibl
& 0x7;
449 instr_data
->displacement
=
450 instr_data
->insn_start
+ 2 - (instr_byte
& 0x7f) * 2;
454 instr_data
->displacement
=
455 instr_data
->insn_start
+ 2 - (instr_byte
& 0x7f) * 2;
459 instr_data
->interrupts
= instr_nibl
& 0x3;
462 instr_data
->interrupts
= instr_nibl
& 0x3;
466 instr_data
->ctrl_code
= instr_nibl
& 0x7;
469 instr_data
->flags
= instr_nibl
;
472 instr_data
->arg_reg
[datum_value
] = instr_nibl
;
475 instr_data
->arg_reg
[datum_value
] = instr_nibl
;
478 instr_data
->displacement
=
479 instr_data
->insn_start
+ 2 + (signed char) instr_byte
* 2;
483 instr_data
->immediate
= ((instr_nibl
>> 1) & 0x1) + 1;
496 static char *intr_names
[] = {
504 unparse_instr (instr_data
, is_segmented
)
505 instr_data_s
*instr_data
;
508 unsigned short datum_value
;
509 unsigned int tabl_datum
, datum_class
;
510 int loop
, loop_limit
;
511 char out_str
[80], tmp_str
[25];
513 sprintf (out_str
, "%s\t", z8k_table
[instr_data
->tabl_index
].name
);
515 loop_limit
= z8k_table
[instr_data
->tabl_index
].noperands
;
516 for (loop
= 0; loop
< loop_limit
; loop
++)
519 strcat (out_str
, ",");
521 tabl_datum
= z8k_table
[instr_data
->tabl_index
].arg_info
[loop
];
522 datum_class
= tabl_datum
& CLASS_MASK
;
523 datum_value
= tabl_datum
& ~CLASS_MASK
;
528 sprintf (tmp_str
, "0x%0lx(r%ld)", instr_data
->address
,
529 instr_data
->arg_reg
[datum_value
]);
530 strcat (out_str
, tmp_str
);
534 sprintf (tmp_str
, "rr%ld(#0x%lx)", instr_data
->arg_reg
[datum_value
],
535 instr_data
->immediate
);
537 sprintf (tmp_str
, "r%ld(#0x%lx)", instr_data
->arg_reg
[datum_value
],
538 instr_data
->immediate
);
539 strcat (out_str
, tmp_str
);
543 sprintf (tmp_str
, "rr%ld(r%ld)", instr_data
->arg_reg
[datum_value
],
544 instr_data
->arg_reg
[ARG_RX
]);
546 sprintf (tmp_str
, "r%ld(r%ld)", instr_data
->arg_reg
[datum_value
],
547 instr_data
->arg_reg
[ARG_RX
]);
548 strcat (out_str
, tmp_str
);
551 sprintf (tmp_str
, "0x%0lx", instr_data
->displacement
);
552 strcat (out_str
, tmp_str
);
555 if (datum_value
== ARG_IMM2
) /* True with EI/DI instructions only. */
557 sprintf (tmp_str
, "%s", intr_names
[instr_data
->interrupts
]);
558 strcat (out_str
, tmp_str
);
561 sprintf (tmp_str
, "#0x%0lx", instr_data
->immediate
);
562 strcat (out_str
, tmp_str
);
565 sprintf (tmp_str
, "%s", codes
[instr_data
->cond_code
]);
566 strcat (out_str
, tmp_str
);
569 sprintf (tmp_str
, "%s", ctrl_names
[instr_data
->ctrl_code
]);
570 strcat (out_str
, tmp_str
);
574 sprintf (tmp_str
, "0x%0lx", instr_data
->address
);
575 strcat (out_str
, tmp_str
);
579 sprintf (tmp_str
, "@rr%ld", instr_data
->arg_reg
[datum_value
]);
581 sprintf (tmp_str
, "@r%ld", instr_data
->arg_reg
[datum_value
]);
582 strcat (out_str
, tmp_str
);
585 sprintf (tmp_str
, "@r%ld", instr_data
->arg_reg
[datum_value
]);
586 strcat (out_str
, tmp_str
);
589 sprintf (tmp_str
, "0x%0lx", instr_data
->flags
);
590 strcat (out_str
, tmp_str
);
593 if (instr_data
->arg_reg
[datum_value
] >= 0x8)
594 sprintf (tmp_str
, "rl%ld",
595 instr_data
->arg_reg
[datum_value
] - 0x8);
597 sprintf (tmp_str
, "rh%ld", instr_data
->arg_reg
[datum_value
]);
598 strcat (out_str
, tmp_str
);
601 sprintf (tmp_str
, "r%ld", instr_data
->arg_reg
[datum_value
]);
602 strcat (out_str
, tmp_str
);
605 sprintf (tmp_str
, "rq%ld", instr_data
->arg_reg
[datum_value
]);
606 strcat (out_str
, tmp_str
);
609 sprintf (tmp_str
, "rr%ld", instr_data
->arg_reg
[datum_value
]);
610 strcat (out_str
, tmp_str
);
614 sprintf (tmp_str
, "rr%ld", instr_data
->arg_reg
[datum_value
]);
616 sprintf (tmp_str
, "r%ld", instr_data
->arg_reg
[datum_value
]);
617 strcat (out_str
, tmp_str
);
625 strcpy (instr_data
->instr_asmsrc
, out_str
);