| 1 | /* Disassemble h8300 instructions. |
| 2 | Copyright (C) 1993 Free Software Foundation, Inc. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 2 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 17 | |
| 18 | #define DEFINE_TABLE |
| 19 | |
| 20 | #define h8_opcodes h8ops |
| 21 | #include "opcode/h8300.h" |
| 22 | #include "dis-asm.h" |
| 23 | |
| 24 | |
| 25 | /* Run through the opcodes and sort them into order to make them easy |
| 26 | to disassemble |
| 27 | */ |
| 28 | static void |
| 29 | bfd_h8_disassemble_init () |
| 30 | { |
| 31 | unsigned int i; |
| 32 | |
| 33 | |
| 34 | struct h8_opcode *p; |
| 35 | |
| 36 | for (p = h8_opcodes; p->name; p++) |
| 37 | { |
| 38 | int n1 = 0; |
| 39 | int n2 = 0; |
| 40 | |
| 41 | if ((int) p->data.nib[0] < 16) |
| 42 | { |
| 43 | n1 = (int) p->data.nib[0]; |
| 44 | } |
| 45 | else |
| 46 | n1 = 0; |
| 47 | if ((int) p->data.nib[1] < 16) |
| 48 | { |
| 49 | n2 = (int) p->data.nib[1]; |
| 50 | } |
| 51 | else |
| 52 | n2 = 0; |
| 53 | |
| 54 | /* Just make sure there are an even number of nibbles in it, and |
| 55 | that the count is the same s the length */ |
| 56 | for (i = 0; p->data.nib[i] != E; i++) |
| 57 | /*EMPTY*/ ; |
| 58 | if (i & 1) |
| 59 | abort (); |
| 60 | p->length = i / 2; |
| 61 | } |
| 62 | |
| 63 | } |
| 64 | |
| 65 | |
| 66 | unsigned int |
| 67 | bfd_h8_disassemble (addr, info, mode) |
| 68 | bfd_vma addr; |
| 69 | disassemble_info *info; |
| 70 | int mode; |
| 71 | { |
| 72 | /* Find the first entry in the table for this opcode */ |
| 73 | static CONST char *regnames[] = |
| 74 | { |
| 75 | "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h", |
| 76 | "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"}; |
| 77 | |
| 78 | static CONST char *wregnames[] = |
| 79 | { |
| 80 | "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", |
| 81 | "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7" |
| 82 | }; |
| 83 | |
| 84 | static CONST char *lregnames[] = |
| 85 | { |
| 86 | "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7", |
| 87 | "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7" |
| 88 | } |
| 89 | ; |
| 90 | |
| 91 | int rs = 0; |
| 92 | int rd = 0; |
| 93 | int rdisp = 0; |
| 94 | int abs = 0; |
| 95 | int bit = 0; |
| 96 | int plen = 0; |
| 97 | static boolean init = 0; |
| 98 | struct h8_opcode *q = h8_opcodes; |
| 99 | char CONST **pregnames = mode != 0 ? lregnames : wregnames; |
| 100 | int status; |
| 101 | int l; |
| 102 | |
| 103 | unsigned char data[20]; |
| 104 | void *stream = info->stream; |
| 105 | fprintf_ftype fprintf = info->fprintf_func; |
| 106 | |
| 107 | if (!init) |
| 108 | { |
| 109 | bfd_h8_disassemble_init (); |
| 110 | init = 1; |
| 111 | } |
| 112 | |
| 113 | status = info->read_memory_func(addr, data, 2, info); |
| 114 | if (status != 0) |
| 115 | { |
| 116 | info->memory_error_func(status, addr, info); |
| 117 | return -1; |
| 118 | } |
| 119 | for (l = 2; status == 0 && l < 10; l+=2) |
| 120 | { |
| 121 | status = info->read_memory_func(addr+l, data+l, 2, info); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | /* Find the exact opcode/arg combo */ |
| 127 | while (q->name) |
| 128 | { |
| 129 | op_type *nib; |
| 130 | unsigned int len = 0; |
| 131 | |
| 132 | nib = q->data.nib; |
| 133 | |
| 134 | while (1) |
| 135 | { |
| 136 | op_type looking_for = *nib; |
| 137 | int thisnib = data[len >> 1]; |
| 138 | |
| 139 | thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf); |
| 140 | |
| 141 | if (looking_for < 16 && looking_for >=0) |
| 142 | { |
| 143 | |
| 144 | if (looking_for != thisnib) |
| 145 | goto fail; |
| 146 | } |
| 147 | |
| 148 | else |
| 149 | { |
| 150 | |
| 151 | if ((int) looking_for & (int) B31) |
| 152 | { |
| 153 | if (! (((int) thisnib & 0x8) != 0)) |
| 154 | goto fail; |
| 155 | looking_for = (op_type) ((int) looking_for & ~(int) B31); |
| 156 | } |
| 157 | if ((int) looking_for & (int) B30) |
| 158 | { |
| 159 | if (!(((int) thisnib & 0x8) == 0)) |
| 160 | goto fail; |
| 161 | looking_for = (op_type) ((int) looking_for & ~(int) B30); |
| 162 | } |
| 163 | |
| 164 | if (looking_for & DBIT) |
| 165 | { |
| 166 | if ((looking_for & 5) != (thisnib &5)) goto fail; |
| 167 | abs = (thisnib & 0x8) ? 2 : 1; |
| 168 | } |
| 169 | |
| 170 | else if (looking_for & (REG | IND|INC|DEC)) |
| 171 | { |
| 172 | if (looking_for & SRC) |
| 173 | { |
| 174 | rs = thisnib; |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | rd = thisnib; |
| 179 | } |
| 180 | } |
| 181 | else if (looking_for & L_16) |
| 182 | { |
| 183 | abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1]; |
| 184 | plen = 16; |
| 185 | |
| 186 | } |
| 187 | else if(looking_for & ABSJMP) |
| 188 | { |
| 189 | abs = |
| 190 | (data[1] << 16) |
| 191 | | (data[2] << 8) |
| 192 | | (data[3]); |
| 193 | } |
| 194 | else if(looking_for & MEMIND) |
| 195 | { |
| 196 | abs = data[1]; |
| 197 | } |
| 198 | else if (looking_for & L_32) |
| 199 | { |
| 200 | int i = len >> 1; |
| 201 | abs = (data[i] << 24) |
| 202 | | (data[i + 1] << 16) |
| 203 | | (data[i + 2] << 8) |
| 204 | | (data[i+ 3]); |
| 205 | |
| 206 | plen =32; |
| 207 | |
| 208 | } |
| 209 | else if (looking_for & L_24) |
| 210 | { |
| 211 | int i = len >> 1; |
| 212 | abs = (data[i] << 16) | (data[i + 1] << 8)| (data[i+ |
| 213 | 2]); |
| 214 | plen =24; |
| 215 | } |
| 216 | else if (looking_for & IGNORE) |
| 217 | { |
| 218 | |
| 219 | } |
| 220 | else if (looking_for & DISPREG) |
| 221 | { |
| 222 | rdisp = thisnib; |
| 223 | } |
| 224 | else if (looking_for & KBIT) |
| 225 | { |
| 226 | switch (thisnib) |
| 227 | { |
| 228 | case 9: |
| 229 | abs = 4; |
| 230 | break; |
| 231 | case 8: |
| 232 | abs = 2; |
| 233 | break; |
| 234 | case 0: |
| 235 | abs = 1; |
| 236 | break; |
| 237 | default: |
| 238 | goto fail; |
| 239 | } |
| 240 | } |
| 241 | else if (looking_for & L_8) |
| 242 | { |
| 243 | plen = 8; |
| 244 | abs = data[len >> 1]; |
| 245 | } |
| 246 | else if (looking_for & L_3) |
| 247 | { |
| 248 | bit = thisnib & 0x7; |
| 249 | } |
| 250 | else if (looking_for & L_2) |
| 251 | { |
| 252 | plen = 2; |
| 253 | abs = thisnib & 0x3; |
| 254 | } |
| 255 | else if (looking_for == E) |
| 256 | { |
| 257 | |
| 258 | { |
| 259 | int i; |
| 260 | |
| 261 | for (i = 0; i < q->length; i++) |
| 262 | { |
| 263 | fprintf (stream, "%02x ", data[i]); |
| 264 | } |
| 265 | for (; i < 6; i++) |
| 266 | { |
| 267 | fprintf (stream, " "); |
| 268 | } |
| 269 | } |
| 270 | fprintf (stream, "%s\t", q->name); |
| 271 | |
| 272 | /* Gross. Disgusting. */ |
| 273 | if (strcmp (q->name, "ldm.l") == 0) |
| 274 | { |
| 275 | int count, high; |
| 276 | |
| 277 | count = (data[1] >> 4) & 0x3; |
| 278 | high = data[3] & 0x7; |
| 279 | |
| 280 | fprintf (stream, "@sp+,er%d-er%d", high - count, high); |
| 281 | return q->length; |
| 282 | } |
| 283 | |
| 284 | if (strcmp (q->name, "stm.l") == 0) |
| 285 | { |
| 286 | int count, low; |
| 287 | |
| 288 | count = (data[1] >> 4) & 0x3; |
| 289 | low = data[3] & 0x7; |
| 290 | |
| 291 | fprintf (stream, "er%d-er%d,@-sp", low, low + count); |
| 292 | return q->length; |
| 293 | } |
| 294 | |
| 295 | /* Fill in the args */ |
| 296 | { |
| 297 | op_type *args = q->args.nib; |
| 298 | int hadone = 0; |
| 299 | |
| 300 | |
| 301 | while (*args != E) |
| 302 | { |
| 303 | int x = *args; |
| 304 | if (hadone) |
| 305 | fprintf (stream, ","); |
| 306 | |
| 307 | |
| 308 | if (x & L_3) |
| 309 | { |
| 310 | fprintf (stream, "#0x%x", (unsigned) bit); |
| 311 | } |
| 312 | else if (x & (IMM|KBIT|DBIT)) |
| 313 | { |
| 314 | /* Bletch. For shal #2,er0 and friends. */ |
| 315 | if (*(args+1) & SRC_IN_DST) |
| 316 | abs = 2; |
| 317 | |
| 318 | fprintf (stream, "#0x%x", (unsigned) abs); |
| 319 | } |
| 320 | else if (x & REG) |
| 321 | { |
| 322 | int rn = (x & DST) ? rd : rs; |
| 323 | switch (x & SIZE) |
| 324 | { |
| 325 | case L_8: |
| 326 | fprintf (stream, "%s", regnames[rn]); |
| 327 | break; |
| 328 | case L_16: |
| 329 | fprintf (stream, "%s", wregnames[rn]); |
| 330 | break; |
| 331 | case L_P: |
| 332 | case L_32: |
| 333 | fprintf (stream, "%s", lregnames[rn]); |
| 334 | break; |
| 335 | |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | else if (x & INC) |
| 340 | { |
| 341 | fprintf (stream, "@%s+", pregnames[rs]); |
| 342 | } |
| 343 | else if (x & DEC) |
| 344 | { |
| 345 | fprintf (stream, "@-%s", pregnames[rd]); |
| 346 | } |
| 347 | |
| 348 | else if (x & IND) |
| 349 | { |
| 350 | int rn = (x & DST) ? rd : rs; |
| 351 | fprintf (stream, "@%s", pregnames[rn]); |
| 352 | } |
| 353 | |
| 354 | else if (x & ABS8MEM) |
| 355 | { |
| 356 | fprintf (stream, "@0x%x:8", (unsigned) abs); |
| 357 | } |
| 358 | |
| 359 | else if (x & (ABS|ABSJMP)) |
| 360 | { |
| 361 | fprintf (stream, "@0x%x:%d", (unsigned) abs, plen); |
| 362 | } |
| 363 | |
| 364 | else if (x & MEMIND) |
| 365 | { |
| 366 | fprintf (stream, "@@%d (%x)", abs, abs); |
| 367 | } |
| 368 | |
| 369 | else if (x & PCREL) |
| 370 | { |
| 371 | if (x & L_16) |
| 372 | { |
| 373 | abs +=2; |
| 374 | fprintf (stream, ".%s%d (%x)", (short) abs > 0 ? "+" : "", (short) abs, |
| 375 | addr + (short) abs + 2); |
| 376 | } |
| 377 | else { |
| 378 | fprintf (stream, ".%s%d (%x)", (char) abs > 0 ? "+" : "", (char) abs, |
| 379 | addr + (char) abs + 2); |
| 380 | } |
| 381 | } |
| 382 | else if (x & DISP) |
| 383 | { |
| 384 | fprintf (stream, "@(0x%x:%d,%s)", abs,plen, pregnames[rdisp]); |
| 385 | } |
| 386 | |
| 387 | else if (x & CCR) |
| 388 | { |
| 389 | fprintf (stream, "ccr"); |
| 390 | } |
| 391 | else if (x & EXR) |
| 392 | { |
| 393 | fprintf (stream, "exr"); |
| 394 | } |
| 395 | |
| 396 | else |
| 397 | fprintf (stream, "Hmmmm %x", x); |
| 398 | hadone = 1; |
| 399 | args++; |
| 400 | } |
| 401 | } |
| 402 | return q->length; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | else |
| 407 | { |
| 408 | fprintf (stream, "Dont understand %x \n", looking_for); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | len++; |
| 413 | nib++; |
| 414 | } |
| 415 | |
| 416 | fail: |
| 417 | q++; |
| 418 | } |
| 419 | |
| 420 | /* Fell of the end */ |
| 421 | fprintf (stream, "%02x %02x .word\tH'%x,H'%x", |
| 422 | data[0], data[1], |
| 423 | data[0], data[1]); |
| 424 | return 2; |
| 425 | } |
| 426 | |
| 427 | int |
| 428 | print_insn_h8300 (addr, info) |
| 429 | bfd_vma addr; |
| 430 | disassemble_info *info; |
| 431 | { |
| 432 | return bfd_h8_disassemble (addr, info , 0); |
| 433 | } |
| 434 | |
| 435 | int |
| 436 | print_insn_h8300h (addr, info) |
| 437 | bfd_vma addr; |
| 438 | disassemble_info *info; |
| 439 | { |
| 440 | return bfd_h8_disassemble (addr, info , 1); |
| 441 | } |
| 442 | |
| 443 | int |
| 444 | print_insn_h8300s (addr, info) |
| 445 | bfd_vma addr; |
| 446 | disassemble_info *info; |
| 447 | { |
| 448 | return bfd_h8_disassemble (addr, info , 2); |
| 449 | } |