1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
|
/* disassemble.c: Fuse's disassembler
Copyright (c) 2002-2003 Darren Salt, Philip Kendall
$Id: disassemble.c 4547 2011-09-27 11:50:15Z fredm $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
Darren: linux@youmustbejoking.demon.co.uk
Philip: philip-fuse@shadowmagic.org.uk
*/
#include <config.h>
#include <stdio.h>
#include <libspectrum.h>
#include "debugger.h"
#include "fuse.h"
#include "memory.h"
/* Used to flag whether we're after a DD or FD prefix */
enum hl_type { USE_HL, USE_IX, USE_IY };
static void disassemble_main( libspectrum_word address, char *buffer,
size_t buflen, size_t *length,
enum hl_type use_hl );
static void disassemble_00xxxxxx( libspectrum_word address, char *buffer,
size_t buflen, size_t *length,
enum hl_type use_hl );
static void disassemble_00xxx010( libspectrum_word address, char *buffer,
size_t buflen, size_t *length,
enum hl_type use_hl );
static void disassemble_00xxx110( libspectrum_word address, char *buffer,
size_t buflen, size_t *length,
enum hl_type use_hl );
static void disassemble_11xxxxxx( libspectrum_word address, char *buffer,
size_t buflen, size_t *length,
enum hl_type use_hl );
static void disassemble_11xxx001( libspectrum_byte b, char *buffer,
size_t buflen, size_t *length,
enum hl_type use_hl );
static void disassemble_11xxx011( libspectrum_word address, char *buffer,
size_t buflen, size_t *length,
enum hl_type use_hl );
static void disassemble_11xxx101( libspectrum_word address, char *buffer,
size_t buflen, size_t *length,
enum hl_type use_hl );
static void disassemble_cb( libspectrum_word address, char *buffer,
size_t buflen, size_t *length );
static void disassemble_ed( libspectrum_word address, char *buffer,
size_t buflen, size_t *length );
static void disassemble_ddfd_cb( libspectrum_word address, char offset,
enum hl_type use_hl, char *buffer,
size_t buflen, size_t *length );
static void get_byte( char *buffer, size_t buflen, libspectrum_byte b );
static void get_word( char *buffer, size_t buflen, libspectrum_word address );
static void get_offset( char *buffer, size_t buflen, libspectrum_word address,
libspectrum_byte offset );
static const char *reg_pair( libspectrum_byte b, enum hl_type use_hl );
static const char *hl_ix_iy( enum hl_type use_hl );
static void ix_iy_offset( char *buffer, size_t buflen, enum hl_type use_hl,
libspectrum_byte offset );
static int source_reg( libspectrum_word address, enum hl_type use_hl,
char *buffer, size_t buflen );
static int dest_reg( libspectrum_word address, enum hl_type use_hl,
char *buffer, size_t buflen );
static int single_reg( int i, enum hl_type use_hl, libspectrum_byte offset,
char *buffer, size_t buflen );
static const char *addition_op( libspectrum_byte b );
static const char *condition( libspectrum_byte b );
static const char *rotate_op( libspectrum_byte b );
static const char *bit_op( libspectrum_byte b );
static int bit_op_bit( libspectrum_byte b );
/* A very thin wrapper to avoid exposing the USE_HL constant */
void
debugger_disassemble( char *buffer, size_t buflen, size_t *length,
libspectrum_word address )
{
disassemble_main( address, buffer, buflen, length, USE_HL );
}
/* Disassemble one instruction */
static void
disassemble_main( libspectrum_word address, char *buffer, size_t buflen,
size_t *length, enum hl_type use_hl )
{
libspectrum_byte b;
char buffer2[40], buffer3[40];
b = readbyte_internal( address );
if( b < 0x40 ) {
disassemble_00xxxxxx( address, buffer, buflen, length, use_hl );
} else if( b == 0x76 ) {
snprintf( buffer, buflen, "HALT" ); *length = 1;
} else if( b < 0x80 ) {
if( ( b & 0x07 ) == 0x06 ) { /* LD something,(HL) */
dest_reg( address, USE_HL, buffer2, 40 );
source_reg( address, use_hl, buffer3, 40 );
*length = ( use_hl == USE_HL ? 1 : 2 );
} else if( ( ( b >> 3 ) & 0x07 ) == 0x06 ) { /* LD (HL),something */
dest_reg( address, use_hl, buffer2, 40 );
source_reg( address, USE_HL, buffer3, 40 );
*length = ( use_hl == USE_HL ? 1 : 2 );
} else { /* Does not involve (HL) at all */
dest_reg( address, use_hl, buffer2, 40 );
source_reg( address, use_hl, buffer3, 40 );
*length = 1;
}
/* Note LD (HL),(HL) does not exist */
snprintf( buffer, buflen, "LD %s,%s", buffer2, buffer3 );
} else if( b < 0xc0 ) {
*length = 1 + source_reg( address, use_hl, buffer2, 40 );
snprintf( buffer, buflen, addition_op( b ), buffer2 );
} else {
disassemble_11xxxxxx( address, buffer, buflen, length, use_hl );
}
}
/* Disassemble something of the form 00xxxxxx */
static void
disassemble_00xxxxxx( libspectrum_word address, char *buffer, size_t buflen,
size_t *length, enum hl_type use_hl )
{
const char *opcode_00xxx000[] = {
"NOP", "EX AF,AF'", "DJNZ ", "JR ", "JR NZ,", "JR Z,", "JR NC,", "JR C,"
};
const char *opcode_00xxx111[] = {
"RLCA", "RRCA", "RLA", "RRA", "DAA", "CPL", "SCF", "CCF"
};
char buffer2[40], buffer3[40];
libspectrum_byte b = readbyte_internal( address );
switch( b & 0x0f ) {
case 0x00: case 0x08:
if( b <= 0x08 ) {
snprintf( buffer, buflen, "%s", opcode_00xxx000[ b >> 3 ] ); *length = 1;
} else {
get_offset( buffer2, 40, address + 2, readbyte_internal( address + 1 ) );
snprintf( buffer, buflen, "%s%s", opcode_00xxx000[ b >> 3 ], buffer2 );
*length = 2;
}
break;
case 0x01:
get_word( buffer2, 40, address + 1 );
snprintf( buffer, buflen, "LD %s,%s", reg_pair( b, use_hl ), buffer2 );
*length = 3;
break;
case 0x02:
disassemble_00xxx010( address, buffer, buflen, length, use_hl );
break;
case 0x03:
snprintf( buffer, buflen, "INC %s", reg_pair( b, use_hl ) ); *length = 1;
break;
case 0x04: case 0x0c:
*length = 1 + dest_reg( address, use_hl, buffer2, 40 );
snprintf( buffer, buflen, "INC %s", buffer2 );
break;
case 0x05: case 0x0d:
*length = 1 + dest_reg( address, use_hl, buffer2, 40 );
snprintf( buffer, buflen, "DEC %s", buffer2 );
break;
case 0x06: case 0x0e:
*length = 2 + dest_reg( address, use_hl, buffer2, 40 );
get_byte( buffer3, 40, readbyte_internal( address + *length - 1 ) );
snprintf( buffer, buflen, "LD %s,%s", buffer2, buffer3 );
break;
case 0x07: case 0x0f:
snprintf( buffer, buflen, "%s", opcode_00xxx111[ b >> 3 ] ); *length = 1;
break;
case 0x09:
snprintf( buffer, buflen, "ADD %s,%s", hl_ix_iy( use_hl ),
reg_pair( b, use_hl ) );
*length = 1;
break;
case 0x0a:
disassemble_00xxx110( address, buffer, buflen, length, use_hl );
break;
case 0x0b:
snprintf( buffer, buflen, "DEC %s", reg_pair( b, use_hl ) );
*length = 1;
break;
}
}
/* Disassemble something of the form 00xxx010 */
static void
disassemble_00xxx010( libspectrum_word address, char *buffer, size_t buflen,
size_t *length, enum hl_type use_hl )
{
char buffer2[40];
libspectrum_byte b = readbyte_internal( address );
switch( b >> 4 ) {
case 0: case 1:
snprintf( buffer, buflen, "LD (%s),A", reg_pair( b, use_hl ) );
*length = 1;
break;
case 2:
get_word( buffer2, 40, address + 1 );
snprintf( buffer, buflen, "LD (%s),%s", buffer2, hl_ix_iy( use_hl ) );
*length = 3;
break;
case 3:
get_word( buffer2, 40, address + 1 );
snprintf( buffer, buflen, "LD (%s),A", buffer2 ); *length = 3;
break;
}
}
/* Disassemble something of the form 00xxx110 */
static void
disassemble_00xxx110( libspectrum_word address, char *buffer, size_t buflen,
size_t *length, enum hl_type use_hl )
{
char buffer2[40];
libspectrum_byte b = readbyte_internal( address );
switch( b >> 4 ) {
case 0: case 1:
snprintf( buffer, buflen, "LD A,(%s)", reg_pair( b, use_hl ) );
*length = 1;
break;
case 2:
get_word( buffer2, 40, address + 1 );
snprintf( buffer, buflen, "LD %s,(%s)", hl_ix_iy( use_hl ), buffer2 );
*length = 3;
break;
case 3:
get_word( buffer2, 40, address + 1 );
snprintf( buffer, buflen, "LD A,(%s)", buffer2 ); *length = 3;
break;
}
}
/* Disassemble something of the form 11xxxxxx */
static void
disassemble_11xxxxxx( libspectrum_word address, char *buffer, size_t buflen,
size_t *length, enum hl_type use_hl )
{
char buffer2[40];
libspectrum_byte b = readbyte_internal( address );
switch( b & 0x07 ) {
case 0x00:
snprintf( buffer, buflen, "RET %s", condition( b ) ); *length = 1;
break;
case 0x01:
disassemble_11xxx001( b, buffer, buflen, length, use_hl );
break;
case 0x02:
get_word( buffer2, 40, address + 1 );
snprintf( buffer, buflen, "JP %s,%s", condition( b ), buffer2 );
*length = 3;
break;
case 0x03:
disassemble_11xxx011( address, buffer, buflen, length, use_hl );
break;
case 0x04:
get_word( buffer2, 40, address + 1 );
snprintf( buffer, buflen, "CALL %s,%s", condition( b ), buffer2 );
*length = 3;
break;
case 0x05:
disassemble_11xxx101( address, buffer, buflen, length, use_hl );
break;
case 0x06:
get_byte( buffer2, 40, readbyte_internal( address + 1 ) );
snprintf( buffer, buflen, addition_op( b ), buffer2 );
*length = 2;
break;
case 0x07:
snprintf( buffer, buflen, "RST %X", 0x08 * ( ( b >> 3 ) - 0x18 ) );
*length = 1;
break;
}
}
/* Disassemble something for the form 11xxx001 */
static void
disassemble_11xxx001( libspectrum_byte b, char *buffer, size_t buflen,
size_t *length, enum hl_type use_hl )
{
switch( ( b >> 3 ) - 0x18 ) {
case 0x00: case 0x02: case 0x04:
snprintf( buffer, buflen, "POP %s", reg_pair( b, use_hl ) ); *length = 1;
break;
case 0x01: snprintf( buffer, buflen, "RET" ); *length = 1; break;
case 0x03: snprintf( buffer, buflen, "EXX" ); *length = 1; break;
case 0x05:
snprintf( buffer, buflen, "JP %s", hl_ix_iy( use_hl ) ); *length = 1;
break;
case 0x06: snprintf( buffer, buflen, "POP AF" ); *length = 1; break;
case 0x07:
snprintf( buffer, buflen, "LD SP,%s", hl_ix_iy( use_hl ) ); *length = 1;
break;
}
}
/* Disassemble something for the form 11xxx011 */
static void
disassemble_11xxx011( libspectrum_word address, char *buffer, size_t buflen,
size_t *length, enum hl_type use_hl )
{
char buffer2[40];
libspectrum_byte b = readbyte_internal( address );
switch( ( b >> 3 ) - 0x18 ) {
case 0x00:
get_word( buffer2, 40, address + 1 );
snprintf( buffer, buflen, "JP %s", buffer2 ); *length = 3;
break;
case 0x01:
if( use_hl != USE_HL ) {
char offset = readbyte_internal( address + 1 );
disassemble_ddfd_cb( address+2, offset, use_hl, buffer, buflen,
length );
(*length) += 2;
} else {
disassemble_cb( address+1, buffer, buflen, length ); (*length)++;
}
break;
case 0x02:
get_byte( buffer2, 40, readbyte_internal( address + 1 ) );
snprintf( buffer, buflen, "OUT (%s),A", buffer2 ); *length = 2;
break;
case 0x03:
get_byte( buffer2, 40, readbyte_internal( address + 1 ) );
snprintf( buffer, buflen, "IN A,(%s)", buffer2 ); *length = 2;
break;
case 0x04:
snprintf( buffer, buflen, "EX (SP),%s", hl_ix_iy( use_hl ) ); *length = 1;
break;
case 0x05:
/* Note: does not get modified by DD or FD */
snprintf( buffer, buflen, "EX DE,HL" ); *length = 1;
break;
case 0x06:
snprintf( buffer, buflen, "DI" ); *length = 1;
break;
case 0x07:
snprintf( buffer, buflen, "EI" ); *length = 1;
break;
}
}
/* Disassemble something for the form 11xxx101 */
static void
disassemble_11xxx101( libspectrum_word address, char *buffer, size_t buflen,
size_t *length, enum hl_type use_hl )
{
char buffer2[40];
libspectrum_byte b = readbyte_internal( address );
switch( ( b >> 3 ) - 0x18 ) {
case 0x00: case 0x02: case 0x04:
snprintf( buffer, buflen, "PUSH %s", reg_pair( b, use_hl ) ); *length = 1;
break;
case 0x01:
get_word( buffer2, 40, address + 1 );
snprintf( buffer, buflen, "CALL %s", buffer2 ); *length = 3;
break;
case 0x03:
disassemble_main( address+1, buffer, buflen, length, USE_IX ); (*length)++;
break;
case 0x05:
disassemble_ed( address+1, buffer, buflen, length ); (*length)++;
break;
case 0x06:
snprintf( buffer, buflen, "PUSH AF" ); *length = 1;
break;
case 0x07:
disassemble_main( address+1, buffer, buflen, length, USE_IY ); (*length)++;
break;
}
}
/* Disassemble an instruction after a CB prefix */
static void
disassemble_cb( libspectrum_word address, char *buffer, size_t buflen,
size_t *length )
{
char buffer2[40];
libspectrum_byte b = readbyte_internal( address );
source_reg( address, USE_HL, buffer2, 40 );
if( b < 0x40 ) {
snprintf( buffer, buflen, "%s %s", rotate_op( b ), buffer2 );
*length = 1;
} else {
snprintf( buffer, buflen, "%s %d,%s", bit_op( b ), bit_op_bit( b ),
buffer2 );
*length = 1;
}
}
/* Disassemble an instruction after an ED prefix */
static void
disassemble_ed( libspectrum_word address, char *buffer, size_t buflen,
size_t *length )
{
libspectrum_byte b;
char buffer2[40];
const char *opcode_01xxx111[] = {
"LD I,A", "LD R,A", "LD A,I", "LD A,R", "RRD", "RLD", "NOPD", "NOPD"
};
/* Note 0xbc to 0xbf removed before this table is used */
const char *opcode_101xxxxx[] = {
"LDI", "CPI", "INI", "OUTI", "NOPD", "NOPD", "NOPD", "NOPD",
"LDD", "CPD", "IND", "OUTD", "NOPD", "NOPD", "NOPD", "NOPD",
"LDIR", "CPIR", "INIR", "OTIR", "NOPD", "NOPD", "NOPD", "NOPD",
"LDDR", "CPDR", "INDR", "OTDR"
};
/* The order in which the IM x instructions appear */
const int im_modes[] = { 0, 0, 1, 2 };
b = readbyte_internal( address );
if( b < 0x40 || b > 0xbb ) {
snprintf( buffer, buflen, "NOPD" ); *length = 1;
} else if( b < 0x80 ) {
switch( b & 0x0f ) {
case 0x00: case 0x08:
if( b == 0x70 ) {
snprintf( buffer, buflen, "IN F,(C)" ); *length = 1;
} else {
dest_reg( address, USE_HL, buffer2, 40 );
snprintf( buffer, buflen, "IN %s,(C)", buffer2 ); *length = 1;
}
break;
case 0x01: case 0x09:
if( b == 0x71 ) {
snprintf( buffer, buflen, "OUT (C),0" ); *length = 1;
} else {
dest_reg( address, USE_HL, buffer2, 40 );
snprintf( buffer, buflen, "OUT (C),%s", buffer2 ); *length = 1;
}
break;
case 0x02:
snprintf( buffer, buflen, "SBC HL,%s", reg_pair( b, USE_HL ) );
*length = 1;
break;
case 0x03:
get_word( buffer2, 40, address + 1 );
snprintf( buffer, buflen, "LD (%s),%s", buffer2, reg_pair( b, USE_HL ) );
*length = 3;
break;
case 0x04: case 0x0c:
snprintf( buffer, buflen, "NEG" ); *length = 1;
break;
case 0x05: case 0x0d:
if( b == 0x4d ) {
snprintf( buffer, buflen, "RETI" ); *length = 1;
} else {
snprintf( buffer, buflen, "RETN" ); *length = 1;
}
break;
case 0x06: case 0x0e:
snprintf( buffer, buflen, "IM %d", im_modes[ ( b >> 3 ) & 0x03 ] );
*length = 1;
break;
case 0x07: case 0x0f:
snprintf( buffer, buflen, "%s", opcode_01xxx111[ ( b >> 3 ) & 0x07 ] );
*length = 1;
break;
case 0x0a:
snprintf( buffer, buflen, "ADC HL,%s", reg_pair( b, USE_HL ) );
*length = 1;
break;
case 0x0b:
get_word( buffer2, 40, address + 1 );
snprintf( buffer, buflen, "LD %s,(%s)", reg_pair( b, USE_HL ), buffer2 );
*length = 3;
break;
}
} else if( b < 0xa0 ) {
snprintf( buffer, buflen, "NOPD" ); *length = 1; *length = 1;
} else {
/* Note: 0xbc to 0xbf already removed */
snprintf( buffer, buflen, "%s", opcode_101xxxxx[ b & 0x1f ] ); *length = 1;
}
}
/* Disassemble an instruction after DD/FD CB prefixes */
static void
disassemble_ddfd_cb( libspectrum_word address, char offset,
enum hl_type use_hl, char *buffer, size_t buflen,
size_t *length )
{
libspectrum_byte b = readbyte_internal( address );
char buffer2[40], buffer3[40];
if( b < 0x40 ) {
if( ( b & 0x07 ) == 0x06 ) {
ix_iy_offset( buffer2, 40, use_hl, offset );
snprintf( buffer, buflen, "%s %s", rotate_op( b ), buffer2 );
*length = 1;
} else {
source_reg( address, USE_HL, buffer2, 40 );
ix_iy_offset( buffer3, 40, use_hl, offset );
snprintf( buffer, buflen, "LD %s,%s %s", buffer2,
rotate_op( b ), buffer3 );
*length = 1;
}
} else if( b < 0x80 ) {
ix_iy_offset( buffer2, 40, use_hl, offset );
snprintf( buffer, buflen, "BIT %d,%s", ( b >> 3 ) & 0x07, buffer2 );
*length = 1;
} else {
if( ( b & 0x07 ) == 0x06 ) {
ix_iy_offset( buffer2, 40, use_hl, offset );
snprintf( buffer, buflen, "%s %d,%s", bit_op( b ), bit_op_bit( b ),
buffer2 );
*length = 1;
} else {
source_reg( address, USE_HL, buffer2, 40 );
ix_iy_offset( buffer3, 40, use_hl, offset );
snprintf( buffer, buflen, "LD %s,%s %s", buffer2, bit_op( b ), buffer3 );
*length = 1;
}
}
}
/* Get a text representation of a one-byte number */
static void
get_byte( char *buffer, size_t buflen, libspectrum_byte b )
{
snprintf( buffer, buflen, debugger_output_base == 10 ? "%d" : "%02X", b );
}
/* Get a text representation of an (LSB) two-byte number */
static void
get_word( char *buffer, size_t buflen, libspectrum_word address )
{
libspectrum_word w;
w = readbyte_internal( address + 1 ); w <<= 8;
w += readbyte_internal( address );
snprintf( buffer, buflen, debugger_output_base == 10 ? "%d" : "%04X", w );
}
/* Get a text representation of ( 'address' + 'offset' ) */
static void
get_offset( char *buffer, size_t buflen, libspectrum_word address,
libspectrum_byte offset )
{
address += ( offset >= 0x80 ? offset-0x100 : offset );
snprintf( buffer, buflen, debugger_output_base == 10 ? "%d" : "%04X",
address );
}
/* Select the appropriate register pair from BC, DE, HL (or IX, IY) or
SP, depending on bits 4 and 5 of the opcode */
static const char *
reg_pair( libspectrum_byte b, enum hl_type use_hl )
{
switch( ( b >> 4 ) & 0x03 ) {
case 0: return "BC";
case 1: return "DE";
case 2: return hl_ix_iy( use_hl );
case 3: return "SP";
}
return "* INTERNAL ERROR *"; /* Should never happen */
}
/* Get whichever of HL, IX or IY is in use here */
static const char *
hl_ix_iy( enum hl_type use_hl )
{
switch( use_hl ) {
case USE_HL: return "HL";
case USE_IX: return "IX";
case USE_IY: return "IY";
}
return "* INTERNAL ERROR *"; /* Should never happen */
}
/* Get a text representation of '(IX+03)' or similar things */
static void
ix_iy_offset( char *buffer, size_t buflen, enum hl_type use_hl,
libspectrum_byte offset )
{
if( offset < 0x80 ) {
snprintf( buffer, buflen,
debugger_output_base == 10 ? "(%s+%d)" : "(%s+%02X)",
hl_ix_iy( use_hl ), offset );
} else {
snprintf( buffer, buflen,
debugger_output_base == 10 ? "(%s-%d)" : "(%s-%02X)",
hl_ix_iy( use_hl ), 0x100 - offset );
}
}
/* Get an 8-bit register, based on bits 0-2 of the opcode at 'address' */
static int
source_reg( libspectrum_word address, enum hl_type use_hl, char *buffer,
size_t buflen )
{
return single_reg( readbyte_internal( address ) & 0x07, use_hl,
readbyte_internal( address + 1 ), buffer, buflen );
}
/* Get an 8-bit register, based on bits 3-5 of the opcode at 'address' */
static int
dest_reg( libspectrum_word address, enum hl_type use_hl, char *buffer,
size_t buflen )
{
return single_reg( ( readbyte_internal( address ) >> 3 ) & 0x07, use_hl,
readbyte_internal( address + 1 ), buffer, buflen );
}
/* Get an 8-bit register name, including (HL). Also substitutes
IXh, IXl and (IX+nn) and the IY versions if appropriate */
static int
single_reg( int i, enum hl_type use_hl, libspectrum_byte offset,
char *buffer, size_t buflen )
{
char buffer2[40];
if( i == 0x04 && use_hl != USE_HL ) {
snprintf( buffer, buflen, "%sh", hl_ix_iy( use_hl ) );
return 0;
} else if( i == 0x05 && use_hl != USE_HL ) {
snprintf( buffer, buflen, "%sl", hl_ix_iy( use_hl ) );
return 0;
} else if( i == 0x06 && use_hl != USE_HL ) {
ix_iy_offset( buffer2, 40, use_hl, offset );
snprintf( buffer, buflen, "%s", buffer2 );
return 1;
} else {
const char *regs[] = { "B", "C", "D", "E", "H", "L", "(HL)", "A" };
snprintf( buffer, buflen, "%s", regs[i] );
return 0;
}
}
/* Various lookup tables for opcodes */
/* Addition/subtraction opcodes:
10xxxrrr: 'xxx' selects the opcode and 'rrr' the register to be added
11xxx110: 'xxx' selects the opcode and add a constant
*/
static const char *
addition_op( libspectrum_byte b )
{
const char *ops[] = { "ADD A,%s", "ADC A,%s", "SUB %s", "SBC A,%s",
"AND %s", "XOR %s", "OR %s", "CP %s" };
return ops[ ( b >> 3 ) & 0x07 ];
}
/* Conditions for jumps, etc:
11xxx000: RET condition
11xxx010: JP condition,nnnn
11xxx100: CALL condition,nnnn
*/
static const char *
condition( libspectrum_byte b )
{
const char *conds[] = { "NZ", "Z", "NC", "C", "PO", "PE", "P", "M" };
return conds[ ( b >> 3 ) & 0x07 ];
}
/* Shift/rotate operations:
CB 00xxxrrr: 'xxx' selects the opcode and 'rrr' the register
DD/FD CB <offset> 00xxxrrr: the documented rotate/shifts on (IX+nn) etc
and the undocumented rotate-and-store opcodes
*/
static const char *
rotate_op( libspectrum_byte b )
{
const char *ops[] = { "RLC", "RRC", "RL", "RR", "SLA", "SRA", "SLL", "SRL" };
return ops[ b >> 3 ];
}
/* Bit operations:
CB oobbbrrr: 'oo' (not 00) selects operation
'bbb' selects bit
'rrr' selects register
DD/FD CB <offset> oobbbrrr: the documented bit ops on (IX+nn) etc and the
undocumented bit-op-and store
*/
static const char *
bit_op( libspectrum_byte b )
{
const char *ops[] = { "BIT", "RES", "SET" };
return ops[ ( b >> 6 ) - 1 ];
}
/* Which bit is used by a BIT, RES or SET with this opcode (bits 3-5) */
static int
bit_op_bit( libspectrum_byte b )
{
return ( b >> 3 ) & 0x07;
}
|