1 /* Disassemble z8000 code.
2 Copyright 1992, 1993, 1998, 2000, 2001, 2002
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
;
212 unsigned short instr_nibl
;
213 unsigned short tabl_datum
, datum_class
, datum_value
;
217 while (!nibl_matched
&& z8k_table
[tabl_index
].name
)
221 nibl_index
< z8k_table
[tabl_index
].length
* 2 && nibl_matched
;
224 if ((nibl_index
% 4) == 0)
225 /* Fetch one word at a time. */
226 FETCH_DATA (info
, nibl_index
+ 4);
227 instr_nibl
= nibbles
[nibl_index
];
229 tabl_datum
= z8k_table
[tabl_index
].byte_info
[nibl_index
];
230 datum_class
= tabl_datum
& CLASS_MASK
;
231 datum_value
= ~CLASS_MASK
& tabl_datum
;
236 if (datum_value
!= instr_nibl
)
242 if (!((~instr_nibl
) & 0x4))
246 if (!(instr_nibl
& 0x4))
250 if (!((~instr_nibl
) & 0x8))
254 if (!(instr_nibl
& 0x8))
258 if (!((~instr_nibl
) & 0x8))
263 if (!(instr_nibl
& 0x8))
272 if ((instr_nibl
| 0x2) != (datum_value
| 0x2))
289 output_instr (instr_data
, addr
, info
)
290 instr_data_s
*instr_data
;
291 unsigned long addr ATTRIBUTE_UNUSED
;
292 disassemble_info
*info
;
299 num_bytes
= (z8k_table
[instr_data
->tabl_index
].length
+ seg_length
) * 2;
300 FETCH_DATA (info
, num_bytes
);
302 strcat (out_str
, instr_data
->instr_asmsrc
);
304 (*info
->fprintf_func
) (info
->stream
, "%s", out_str
);
308 unpack_instr (instr_data
, is_segmented
, info
)
309 instr_data_s
*instr_data
;
311 disassemble_info
*info
;
313 int nibl_count
, loop
;
314 unsigned short instr_nibl
, instr_byte
, instr_word
;
316 unsigned int tabl_datum
, datum_class
;
317 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)
346 /* Negative 12 bit displacement. */
347 instr_data
->displacement
= instr_data
->insn_start
+ 2
348 - (signed short) ((instr_word
& 0xfff) | 0xf000) * 2;
350 instr_data
->displacement
= instr_data
->insn_start
+ 2
351 - (instr_word
& 0x0fff) * 2;
363 instr_data
->immediate
= instr_nibl
;
366 instr_data
->immediate
= (- instr_nibl
) & 0xf;
369 instr_data
->immediate
= (- instr_byte
) & 0xff;
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;
457 instr_data
->ctrl_code
= instr_nibl
& 0x7;
460 instr_data
->flags
= instr_nibl
;
463 instr_data
->arg_reg
[datum_value
] = instr_nibl
;
466 instr_data
->arg_reg
[datum_value
] = instr_nibl
;
469 instr_data
->displacement
=
470 instr_data
->insn_start
+ 2 + (signed char) instr_byte
* 2;
474 instr_data
->immediate
= ((instr_nibl
>> 1) & 0x1) + 1;
488 unparse_instr (instr_data
, is_segmented
)
489 instr_data_s
*instr_data
;
492 unsigned short datum_value
;
493 unsigned int tabl_datum
, datum_class
;
494 int loop
, loop_limit
;
495 char out_str
[80], tmp_str
[25];
497 sprintf (out_str
, "%s\t", z8k_table
[instr_data
->tabl_index
].name
);
499 loop_limit
= z8k_table
[instr_data
->tabl_index
].noperands
;
500 for (loop
= 0; loop
< loop_limit
; loop
++)
503 strcat (out_str
, ",");
505 tabl_datum
= z8k_table
[instr_data
->tabl_index
].arg_info
[loop
];
506 datum_class
= tabl_datum
& CLASS_MASK
;
507 datum_value
= tabl_datum
& ~CLASS_MASK
;
512 sprintf (tmp_str
, "0x%0lx(r%ld)", instr_data
->address
,
513 instr_data
->arg_reg
[datum_value
]);
514 strcat (out_str
, tmp_str
);
518 sprintf (tmp_str
, "rr%ld(#%lx)", instr_data
->arg_reg
[datum_value
],
519 instr_data
->immediate
);
521 sprintf (tmp_str
, "r%ld(#%lx)", instr_data
->arg_reg
[datum_value
],
522 instr_data
->immediate
);
523 strcat (out_str
, tmp_str
);
527 sprintf (tmp_str
, "rr%ld(r%ld)", instr_data
->arg_reg
[datum_value
],
528 instr_data
->arg_reg
[ARG_RX
]);
530 sprintf (tmp_str
, "r%ld(r%ld)", instr_data
->arg_reg
[datum_value
],
531 instr_data
->arg_reg
[ARG_RX
]);
532 strcat (out_str
, tmp_str
);
535 sprintf (tmp_str
, "0x%0lx", instr_data
->displacement
);
536 strcat (out_str
, tmp_str
);
539 sprintf (tmp_str
, "#0x%0lx", instr_data
->immediate
);
540 strcat (out_str
, tmp_str
);
543 sprintf (tmp_str
, "%s", codes
[instr_data
->cond_code
]);
544 strcat (out_str
, tmp_str
);
547 sprintf (tmp_str
, "%s", ctrl_names
[instr_data
->ctrl_code
]);
548 strcat (out_str
, tmp_str
);
552 sprintf (tmp_str
, "0x%0lx", instr_data
->address
);
553 strcat (out_str
, tmp_str
);
557 sprintf (tmp_str
, "@rr%ld", instr_data
->arg_reg
[datum_value
]);
559 sprintf (tmp_str
, "@r%ld", instr_data
->arg_reg
[datum_value
]);
560 strcat (out_str
, tmp_str
);
563 sprintf (tmp_str
, "0x%0lx", instr_data
->flags
);
564 strcat (out_str
, tmp_str
);
567 if (instr_data
->arg_reg
[datum_value
] >= 0x8)
568 sprintf (tmp_str
, "rl%ld",
569 instr_data
->arg_reg
[datum_value
] - 0x8);
571 sprintf (tmp_str
, "rh%ld", instr_data
->arg_reg
[datum_value
]);
572 strcat (out_str
, tmp_str
);
575 sprintf (tmp_str
, "r%ld", instr_data
->arg_reg
[datum_value
]);
576 strcat (out_str
, tmp_str
);
579 sprintf (tmp_str
, "rq%ld", instr_data
->arg_reg
[datum_value
]);
580 strcat (out_str
, tmp_str
);
583 sprintf (tmp_str
, "rr%ld", instr_data
->arg_reg
[datum_value
]);
584 strcat (out_str
, tmp_str
);
588 sprintf (tmp_str
, "rr%ld", instr_data
->arg_reg
[datum_value
]);
590 sprintf (tmp_str
, "r%ld", instr_data
->arg_reg
[datum_value
]);
591 strcat (out_str
, tmp_str
);
599 strcpy (instr_data
->instr_asmsrc
, out_str
);