| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <inttypes.h> |
| 8 |
#include <string.h> |
| 9 |
#include "memory.h" |
| 10 |
#include "glue.h" |
| 11 |
|
| 12 |
#include "cpu/mnemonics.h" |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
int disasm(uint16_t pc, uint8_t bank, uint8_t *RAM, char *line, unsigned int max_line, int16_t x16Bank, uint8_t implied_status, int32_t *eff_addr) { |
| 22 |
uint8_t opcode = debug_read6502(pc, bank, x16Bank); |
| 23 |
const char *mnemonic = regs.is65c816 ? mnemonics_c816[opcode] : mnemonics_c02[opcode]; |
| 24 |
|
| 25 |
*eff_addr = -1; |
| 26 |
|
| 27 |
int isZprel = 0; |
| 28 |
int isRel16 = 0; |
| 29 |
int isYrel = 0; |
| 30 |
int isXrel = 0; |
| 31 |
int isIndirect = 0; |
| 32 |
int isBlockMove = 0; |
| 33 |
int isImmediate = 0; |
| 34 |
int isStackRel = 0; |
| 35 |
|
| 36 |
if (regs.is65c816) { |
| 37 |
|
| 38 |
switch (opcode) { |
| 39 |
case 0xC2: |
| 40 |
case 0xE2: |
| 41 |
isImmediate = 1; |
| 42 |
;; |
| 43 |
default: |
| 44 |
;; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
switch (opcode) { |
| 49 |
case 0x1F: |
| 50 |
case 0x3F: |
| 51 |
case 0x5F: |
| 52 |
case 0x7F: |
| 53 |
case 0x9F: |
| 54 |
case 0xBF: |
| 55 |
case 0xDF: |
| 56 |
case 0xFC: |
| 57 |
case 0xFF: |
| 58 |
isXrel = 1; |
| 59 |
;; |
| 60 |
default: |
| 61 |
;; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
switch (opcode) { |
| 66 |
case 0x62: |
| 67 |
case 0x82: |
| 68 |
isRel16 = 1; |
| 69 |
default: |
| 70 |
;; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
switch (opcode) { |
| 75 |
case 0x13: |
| 76 |
case 0x17: |
| 77 |
case 0x33: |
| 78 |
case 0x37: |
| 79 |
case 0x53: |
| 80 |
case 0x57: |
| 81 |
case 0x73: |
| 82 |
case 0x77: |
| 83 |
case 0x93: |
| 84 |
case 0x97: |
| 85 |
case 0xB3: |
| 86 |
case 0xB7: |
| 87 |
case 0xD3: |
| 88 |
case 0xD7: |
| 89 |
case 0xF3: |
| 90 |
case 0xF7: |
| 91 |
isYrel = 1; |
| 92 |
;; |
| 93 |
default: |
| 94 |
;; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
switch (opcode) { |
| 99 |
case 0x07: |
| 100 |
case 0x13: |
| 101 |
case 0x17: |
| 102 |
case 0x27: |
| 103 |
case 0x33: |
| 104 |
case 0x37: |
| 105 |
case 0x47: |
| 106 |
case 0x53: |
| 107 |
case 0x57: |
| 108 |
case 0x67: |
| 109 |
case 0x73: |
| 110 |
case 0x77: |
| 111 |
case 0x87: |
| 112 |
case 0x93: |
| 113 |
case 0x97: |
| 114 |
case 0xA7: |
| 115 |
case 0xB3: |
| 116 |
case 0xB7: |
| 117 |
case 0xC7: |
| 118 |
case 0xD3: |
| 119 |
case 0xD4: |
| 120 |
case 0xD7: |
| 121 |
case 0xDC: |
| 122 |
case 0xE7: |
| 123 |
case 0xF3: |
| 124 |
case 0xF7: |
| 125 |
case 0xFC: |
| 126 |
isIndirect = 1; |
| 127 |
;; |
| 128 |
default: |
| 129 |
;; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
isStackRel = (opcode & 0x0F) == 0x03; |
| 134 |
|
| 135 |
|
| 136 |
isBlockMove = opcode == 0x44 || opcode == 0x54; |
| 137 |
|
| 138 |
} else { |
| 139 |
|
| 140 |
isZprel = (opcode & 0x0F) == 0x0F; |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
switch (opcode) { |
| 152 |
case 0x01: |
| 153 |
case 0x15: |
| 154 |
case 0x16: |
| 155 |
case 0x1D: |
| 156 |
case 0x1E: |
| 157 |
case 0x21: |
| 158 |
case 0x34: |
| 159 |
case 0x35: |
| 160 |
case 0x36: |
| 161 |
case 0x3C: |
| 162 |
case 0x3D: |
| 163 |
case 0x41: |
| 164 |
case 0x55: |
| 165 |
case 0x56: |
| 166 |
case 0x5d: |
| 167 |
case 0x5e: |
| 168 |
case 0x61: |
| 169 |
case 0x74: |
| 170 |
case 0x75: |
| 171 |
case 0x76: |
| 172 |
case 0x7C: |
| 173 |
case 0x7D: |
| 174 |
case 0x7e: |
| 175 |
case 0x81: |
| 176 |
case 0x94: |
| 177 |
case 0x95: |
| 178 |
case 0x9D: |
| 179 |
case 0x9E: |
| 180 |
case 0xA1: |
| 181 |
case 0xB4: |
| 182 |
case 0xB5: |
| 183 |
case 0xBC: |
| 184 |
case 0xBD: |
| 185 |
case 0xC1: |
| 186 |
case 0xD5: |
| 187 |
case 0xD6: |
| 188 |
case 0xDD: |
| 189 |
case 0xDE: |
| 190 |
case 0xD1: |
| 191 |
case 0xF5: |
| 192 |
case 0xF6: |
| 193 |
case 0xFC: |
| 194 |
case 0xFD: |
| 195 |
case 0xFE: |
| 196 |
isXrel = 1; |
| 197 |
;; |
| 198 |
default: |
| 199 |
;; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
int isRel8 = (opcode == 0x80) || ((opcode & 0x1F) == 0x10); |
| 207 |
|
| 208 |
|
| 209 |
switch (opcode) { |
| 210 |
case 0x09: |
| 211 |
case 0x29: |
| 212 |
case 0x49: |
| 213 |
case 0x69: |
| 214 |
case 0x89: |
| 215 |
case 0xA0: |
| 216 |
case 0xA2: |
| 217 |
case 0xA9: |
| 218 |
case 0xC0: |
| 219 |
case 0xC9: |
| 220 |
case 0xE0: |
| 221 |
case 0xE9: |
| 222 |
isImmediate = 1; |
| 223 |
;; |
| 224 |
default: |
| 225 |
;; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
switch (opcode) { |
| 230 |
case 0x11: |
| 231 |
case 0x19: |
| 232 |
case 0x31: |
| 233 |
case 0x39: |
| 234 |
case 0x51: |
| 235 |
case 0x59: |
| 236 |
case 0x71: |
| 237 |
case 0x79: |
| 238 |
case 0x91: |
| 239 |
case 0x96: |
| 240 |
case 0x99: |
| 241 |
case 0xB1: |
| 242 |
case 0xB6: |
| 243 |
case 0xB9: |
| 244 |
case 0xBE: |
| 245 |
case 0xD1: |
| 246 |
case 0xD9: |
| 247 |
case 0xF1: |
| 248 |
case 0xF9: |
| 249 |
isYrel = 1; |
| 250 |
;; |
| 251 |
default: |
| 252 |
;; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
switch (opcode) { |
| 258 |
case 0x01: |
| 259 |
case 0x11: |
| 260 |
case 0x12: |
| 261 |
case 0x21: |
| 262 |
case 0x31: |
| 263 |
case 0x32: |
| 264 |
case 0x41: |
| 265 |
case 0x51: |
| 266 |
case 0x52: |
| 267 |
case 0x61: |
| 268 |
case 0x6C: |
| 269 |
case 0x71: |
| 270 |
case 0x72: |
| 271 |
case 0x7C: |
| 272 |
case 0x81: |
| 273 |
case 0x91: |
| 274 |
case 0x92: |
| 275 |
case 0xA1: |
| 276 |
case 0xB1: |
| 277 |
case 0xB2: |
| 278 |
case 0xC1: |
| 279 |
case 0xD1: |
| 280 |
case 0xD2: |
| 281 |
case 0xE1: |
| 282 |
case 0xF1: |
| 283 |
case 0xF2: |
| 284 |
isIndirect = 1; |
| 285 |
;; |
| 286 |
default: |
| 287 |
;; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
int isBrkOrCop = opcode == 0x00 || opcode == 0x02; |
| 292 |
|
| 293 |
int length = 1; |
| 294 |
|
| 295 |
char *where = strstr(mnemonic, "%%0%hhux"); |
| 296 |
if (where) { |
| 297 |
int isImmediateIndex = (opcode == 0xa0) || (opcode == 0xa2) || (opcode == 0xc0) || (opcode == 0xe0); |
| 298 |
int len = snprintf(line, max_line, mnemonic, (implied_status & (isImmediateIndex ? FLAG_INDEX_WIDTH : FLAG_MEMORY_WIDTH)) ? 2 : 4); |
| 299 |
if (len == -1) { |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
mnemonic = malloc(len + 1); |
| 304 |
memcpy((char *) mnemonic, line, len + 1); |
| 305 |
} |
| 306 |
else { |
| 307 |
strncpy(line,mnemonic,max_line); |
| 308 |
} |
| 309 |
|
| 310 |
if (isBlockMove) { |
| 311 |
snprintf(line, max_line, mnemonic, debug_read6502(pc + 2, bank, x16Bank), debug_read6502(pc + 1, bank, x16Bank)); |
| 312 |
length = 3; |
| 313 |
if (regs.c != 0xFFFF) *eff_addr = regs.y; |
| 314 |
} |
| 315 |
else if (isZprel) { |
| 316 |
snprintf(line, max_line, mnemonic, debug_read6502(pc + 1, bank, x16Bank), pc + 3 + (int8_t)debug_read6502(pc + 2, bank, x16Bank)); |
| 317 |
length = 3; |
| 318 |
} |
| 319 |
else if (isRel16) { |
| 320 |
snprintf(line, max_line, mnemonic, (pc + 3 + (int16_t)(debug_read6502(pc + 1, bank, x16Bank) | (debug_read6502(pc + 2, bank, x16Bank) << 8))) & 0xffff); |
| 321 |
length = 3; |
| 322 |
} |
| 323 |
else if (isBrkOrCop) { |
| 324 |
snprintf(line, max_line, mnemonic, debug_read6502(pc + 1, bank, x16Bank)); |
| 325 |
length = 2; |
| 326 |
} else { |
| 327 |
if (strstr(line, "%02x")) { |
| 328 |
length = 2; |
| 329 |
if (isRel8) { |
| 330 |
snprintf(line, max_line, mnemonic, pc + 2 + (int8_t)debug_read6502(pc + 1, bank, x16Bank)); |
| 331 |
} else { |
| 332 |
snprintf(line, max_line, mnemonic, debug_read6502(pc + 1, bank, x16Bank)); |
| 333 |
if (isStackRel) { |
| 334 |
uint16_t ptr = regs.sp + debug_read6502(pc + 1, bank, x16Bank); |
| 335 |
uint8_t ind_bank = ptr < 0xc000 ? memory_get_ram_bank() : memory_get_rom_bank(); |
| 336 |
if (isIndirect && isYrel) { |
| 337 |
*eff_addr = (debug_read6502(ptr, bank, ind_bank) | (debug_read6502(ptr + 1, bank, ind_bank) << 8)) + regs.y; |
| 338 |
} else { |
| 339 |
*eff_addr = ptr; |
| 340 |
} |
| 341 |
} else if (isIndirect) { |
| 342 |
uint16_t ptr = debug_read6502(pc + 1, bank, x16Bank); |
| 343 |
if (isXrel) |
| 344 |
ptr += regs.x; |
| 345 |
*eff_addr = debug_read6502(ptr, bank, x16Bank) | (debug_read6502(ptr + 1, bank, x16Bank) << 8); |
| 346 |
if (isYrel) |
| 347 |
*eff_addr += regs.y; |
| 348 |
} else if (!isImmediate) { |
| 349 |
*eff_addr = debug_read6502(pc + 1, bank, x16Bank); |
| 350 |
if (isXrel) |
| 351 |
*eff_addr += regs.x; |
| 352 |
if (isYrel) |
| 353 |
*eff_addr += regs.y; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
if (strstr(line, "%04x")) { |
| 358 |
length = 3; |
| 359 |
snprintf(line, max_line, mnemonic, debug_read6502(pc + 1, bank, x16Bank) | debug_read6502(pc + 2, bank, x16Bank) << 8); |
| 360 |
if (isIndirect) { |
| 361 |
uint16_t ptr = debug_read6502(pc + 1, bank, x16Bank) | (debug_read6502(pc + 2, bank, x16Bank) << 8); |
| 362 |
if (isXrel) |
| 363 |
ptr += regs.x; |
| 364 |
uint8_t ind_bank = ptr < 0xc000 ? memory_get_ram_bank() : memory_get_rom_bank(); |
| 365 |
*eff_addr = debug_read6502(ptr, bank, ind_bank) | (debug_read6502(ptr + 1, bank, ind_bank) << 8); |
| 366 |
if (isYrel) |
| 367 |
*eff_addr += regs.y; |
| 368 |
} else if (!isImmediate) { |
| 369 |
*eff_addr = debug_read6502(pc + 1, bank, x16Bank) | (debug_read6502(pc + 2, bank, x16Bank) << 8); |
| 370 |
if (isXrel) |
| 371 |
*eff_addr += regs.x; |
| 372 |
if (isYrel) |
| 373 |
*eff_addr += regs.y; |
| 374 |
} |
| 375 |
} |
| 376 |
if (strstr(line, "%06x")) { |
| 377 |
length = 4; |
| 378 |
snprintf(line, max_line, mnemonic, debug_read6502(pc + 1, bank, x16Bank) | debug_read6502(pc + 2, bank, x16Bank) << 8 | debug_read6502(pc + 3, bank, x16Bank) << 16); |
| 379 |
if (isIndirect) { |
| 380 |
uint16_t ptr = debug_read6502(pc + 1, bank, x16Bank) | (debug_read6502(pc + 2, bank, x16Bank) << 8); |
| 381 |
if (isXrel) |
| 382 |
ptr += regs.x; |
| 383 |
uint8_t ind_bank = ptr < 0xc000 ? memory_get_ram_bank() : memory_get_rom_bank(); |
| 384 |
*eff_addr = debug_read6502(ptr, bank, ind_bank) | (debug_read6502(ptr + 1, bank, ind_bank) << 8); |
| 385 |
if (isYrel) |
| 386 |
*eff_addr += regs.y; |
| 387 |
} else { |
| 388 |
*eff_addr = debug_read6502(pc + 1, bank, x16Bank) | (debug_read6502(pc + 2, bank, x16Bank) << 8); |
| 389 |
if (isXrel) |
| 390 |
*eff_addr += regs.x; |
| 391 |
if (isYrel) |
| 392 |
*eff_addr += regs.y; |
| 393 |
} |
| 394 |
} |
| 395 |
if (opcode == 0x00 || opcode == 0x02) { |
| 396 |
|
| 397 |
|
| 398 |
length = 2; |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
if (where) { |
| 403 |
free((char *) mnemonic); |
| 404 |
} |
| 405 |
|
| 406 |
if (*eff_addr >= 0x10000) { |
| 407 |
*eff_addr &= 0xffff; |
| 408 |
} |
| 409 |
|
| 410 |
return length; |
| 411 |
} |