Skip to content

Commit

Permalink
Greatly simplified disassembly code
Browse files Browse the repository at this point in the history
  • Loading branch information
tcarmelveilleux committed Jul 24, 2014
1 parent 4c9a2da commit 0ffacd9
Showing 1 changed file with 185 additions and 182 deletions.
367 changes: 185 additions & 182 deletions dcc6502.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,23 +369,30 @@ void append_nes(char *input, uint16_t arg) {
// FIXME: Refactor code to reduce line duplication and make more readable
/* This function disassembles the opcode at the PC and outputs it in *output */
void disassemble(char *output) {
char tmpstr[256], opcode_repr[256], hex_dump[256];
char opcode_repr[256], hex_dump[256];
int i;
int len = 0;
int entry = 0;
int found = 0;
uint8_t tmp_byte1, opcode;
word tmp_word = 0;
uint16_t current_addr = org + PC;

opcode = buffer[current_addr - org];
opcode_repr[0] = '\0';
hex_dump[0] = '\0';

// Linear search for opcode
for (i = 0; i < NUMBER_OPCODES; i++) {
if (opcode == opcode_table[i].number) {
found = 1; /* Found the opcode */
entry = i; /* Note the entry number in the table */
/* Found the opcode, record its table index */
found = 1;
entry = i;
}
}

// TODO: Normalize %02x versus %02X
// For opcode not found, terminate early
if (!found) {
sprintf(opcode_repr, ".byte $%02x", opcode);
if (hex_output) {
Expand All @@ -395,188 +402,184 @@ void disassemble(char *output) {
sprintf(hex_dump, "$%04X", current_addr);
sprintf(output, "%-8s%-16s; INVALID OPCODE !!!\n", hex_dump, opcode_repr);
}
} else {
switch (opcode_table[entry].addressing) {
case IMMED:
tmp_byte1 = buffer[PC + 1]; /* Get immediate value */

sprintf(opcode_repr, "%s #$%02x", name_table[opcode_table[entry].name], tmp_byte1);
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X:", current_addr, opcode, tmp_byte1);
} else {
sprintf(hex_dump, "$%04X", current_addr);
}
sprintf(output, DUMP_FORMAT, hex_dump, opcode_repr);

PC++;
break;
case ABSOL:
/* Get address */
tmp_word = LOAD_WORD(buffer, PC);

sprintf(opcode_repr, "%s $%02X%02X", name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X%02X:", current_addr, opcode, LOW_PART(tmp_word), HIGH_PART(tmp_word));
} else {
sprintf(hex_dump, "$%04X", current_addr);
}
sprintf(output, DUMP_FORMAT, hex_dump, opcode_repr);

PC += 2;
break;
case ZEROP:
PC++;
tmp_byte1 = buffer[PC]; /* Get low byte of address */

if (hex_output) {
sprintf(tmpstr, "$%04X> %02X %02X:\t%s $%02X\t\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], tmp_byte1);
} else {
sprintf(tmpstr, "$%04X\t%s $%02X\t\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
}
strncpy(output, tmpstr, 254);
break;
case IMPLI:
if (hex_output) {
sprintf(tmpstr, "$%04X> %02X:\t%s\t\t;", org+PC, opcode, name_table[opcode_table[entry].name]);
} else {
sprintf(tmpstr, "$%04X\t%s\t\t;", org+PC, name_table[opcode_table[entry].name]);
}

strncpy(output, tmpstr, 254);
break;
case INDIA:
PC++;
PC++;
tmp_word = LOAD_WORD(buffer, PC-2);

if (hex_output) {
sprintf(tmpstr, "$%04X> %02X %02X%02X:\t%s ($%02X%02X)\t;", org+PC-2, opcode, LOW_PART(tmp_word), HIGH_PART(tmp_word), name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
} else {
sprintf(tmpstr, "$%04X\t%s ($%02X%02X)\t;", org+PC-2, name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
}

strncpy(output, tmpstr, 254);
break;
case ABSIX:
PC++;
PC++;
tmp_word = LOAD_WORD(buffer, PC-2);

if (hex_output) {
sprintf(tmpstr, "$%04X> %02X %02X%02X:\t%s $%02X%02X,X\t;", org+PC-2, opcode, LOW_PART(tmp_word), HIGH_PART(tmp_word), name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
} else {
sprintf(tmpstr, "$%04X\t%s $%02X%02X,X\t;", org+PC-2, name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
}

strncpy(output, tmpstr, 254);
break;
case ABSIY:
PC++;
PC++;
tmp_word = LOAD_WORD(buffer, PC-2);

if (hex_output) {
sprintf(tmpstr, "$%04X> %02X %02X%02X:\t%s $%02X%02X,Y\t;", org+PC-2, opcode, LOW_PART(tmp_word), HIGH_PART(tmp_word), name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
} else {
sprintf(tmpstr, "$%04X\t%s $%02X%02X,Y\t;", org+PC-2, name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
}

strncpy(output, tmpstr, 254);
break;
case ZEPIX:
PC++;
tmp_byte1 = buffer[PC]; /* Get low byte of address */

if (hex_output) {
sprintf(tmpstr, "$%04X> %02X %02X:\t%s $%02X,X\t\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], tmp_byte1);
} else {
sprintf(tmpstr, "$%04X\t%s $%02X,X\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
}

strncpy(output, tmpstr, 254);
break;
case ZEPIY:
PC++;
tmp_byte1 = buffer[PC]; /* Get low byte of address */

if (hex_output) {
sprintf(tmpstr, "$%04X> %02X %02X:\t%s $%02X,Y\t\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], tmp_byte1);
} else {
sprintf(tmpstr, "$%04X\t%s $%02X,Y\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
}

strncpy(output, tmpstr, 254);
break;
case INDIN:
PC++;
tmp_byte1 = buffer[PC]; /* Get low byte of address */

if (hex_output) {
sprintf(tmpstr, "$%04X> %02X %02X:\t%s ($%02X,X)\t\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], tmp_byte1);
} else {
sprintf(tmpstr, "$%04X\t%s ($%02X,X)\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
}

strncpy(output, tmpstr, 254);
break;
case ININD:
PC++;
tmp_byte1 = buffer[PC]; /* Get low byte of address */

if (hex_output) {
sprintf(tmpstr, "$%04X> %02X %02X:\t%s ($%02X),Y\t\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], tmp_byte1);
} else {
sprintf(tmpstr, "$%04X\t%s ($%02X),Y\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
}

strncpy(output, tmpstr, 254);
break;
case RELAT:
PC++;
tmp_byte1 = buffer[PC]; /* Get relative modifier */

// FIXME: Resolve undefined behavior of cast for signed relative addressing
if (hex_output) {
sprintf(tmpstr, "$%04X> %02X %02X:\t%s $%04X\t\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], (org+PC)+(signed char)(tmp_byte1)+1);
} else {
sprintf(tmpstr, "$%04X\t%s $%04X\t;", org+PC-1, name_table[opcode_table[entry].name], (org+PC)+(signed char)(tmp_byte1)+1);
}

strncpy(output, tmpstr, 254);
break;
case ACCUM:
if (hex_output) {
sprintf(tmpstr, "$%04X> %02X:\t%s A\t\t;", org+PC, opcode, name_table[opcode_table[entry].name]);
} else {
sprintf(tmpstr, "$%04X\t%s A\t\t;", org+PC, name_table[opcode_table[entry].name]);
}

strncpy(output, tmpstr, 254);
break;
default:
break;
}
return;
}

output += strlen(output);
// Opcode found in table: disassemble properly according to addressing mode

/* Add cycle count if necessary */
if (cycle_counting) {
output = append_cycle(output, entry);
}
// Set hex dump to default single address format. Will be overwritten
// by more complex output in case of hex dump mode enabled
sprintf(hex_dump, "$%04X", current_addr);

/* Add NES port info if necessary */
switch (opcode_table[entry].addressing) {
case ABSOL:
case ABSIX:
case ABSIY:
if (nes_mode) {
append_nes(output, tmp_word);
}
break;
default:
/* Other addressing modes: not enough info to add NES register annotation */
break;
}
switch (opcode_table[entry].addressing) {
case IMMED:
/* Get immediate value operand */
tmp_byte1 = buffer[PC + 1];
PC++;

sprintf(opcode_repr, "%s #$%02x", name_table[opcode_table[entry].name], tmp_byte1);
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X:", current_addr, opcode, tmp_byte1);
}

break;
case ABSOL:
/* Get absolute address operand */
tmp_word = LOAD_WORD(buffer, PC);
PC += 2;

sprintf(opcode_repr, "%s $%02X%02X", name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X%02X:", current_addr, opcode, LOW_PART(tmp_word), HIGH_PART(tmp_word));
}

break;
case ZEROP:
/* Get zero page address */
tmp_byte1 = buffer[PC + 1];
PC++;

sprintf(opcode_repr, "%s $%02X", name_table[opcode_table[entry].name], tmp_byte1);
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X:", current_addr, opcode, tmp_byte1);
}

break;
case IMPLI:
sprintf(opcode_repr, "%s", name_table[opcode_table[entry].name]);
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X:", current_addr, opcode);
}

break;
case INDIA:
/* Get indirection address */
tmp_word = LOAD_WORD(buffer, PC);
PC += 2;

sprintf(opcode_repr, "%s ($%02X%02X)", name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X%02X:", current_addr, opcode, LOW_PART(tmp_word), HIGH_PART(tmp_word));
}

break;
case ABSIX:
/* Get base address */
tmp_word = LOAD_WORD(buffer, PC);
PC += 2;

sprintf(opcode_repr, "%s $%02X%02X,X", name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X%02X:", current_addr, opcode, LOW_PART(tmp_word), HIGH_PART(tmp_word));
}

break;
case ABSIY:
/* Get baser address */
tmp_word = LOAD_WORD(buffer, PC);
PC += 2;

sprintf(opcode_repr, "%s $%02X%02X,Y", name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X%02X:", current_addr, opcode, LOW_PART(tmp_word), HIGH_PART(tmp_word));
}

break;
case ZEPIX:
/* Get zero-page base address */
tmp_byte1 = buffer[PC + 1];
PC++;

sprintf(opcode_repr, "%s $%02X,X", name_table[opcode_table[entry].name], tmp_byte1);
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X:", current_addr, opcode, tmp_byte1);
}

break;
case ZEPIY:
/* Get zero-page base address */
tmp_byte1 = buffer[PC + 1];
PC++;

sprintf(opcode_repr, "%s $%02X,Y", name_table[opcode_table[entry].name], tmp_byte1);
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X:", current_addr, opcode, tmp_byte1);
}

break;
case INDIN:
/* Get zero-page base address */
tmp_byte1 = buffer[PC + 1];
PC++;

sprintf(opcode_repr, "%s ($%02X,X)", name_table[opcode_table[entry].name], tmp_byte1);
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X:", current_addr, opcode, tmp_byte1);
}

break;
case ININD:
/* Get zero-page base address */
tmp_byte1 = buffer[PC + 1];
PC++;

sprintf(opcode_repr, "%s ($%02X),Y", name_table[opcode_table[entry].name], tmp_byte1);
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X:", current_addr, opcode, tmp_byte1);
}

break;
case RELAT:
/* Get relative modifier */
tmp_byte1 = buffer[PC + 1];
PC++;

// Compute displacement from first byte after full instruction.
tmp_word = current_addr + 2;
if (tmp_byte1 > 0x7Fu) {
tmp_word -= ((~tmp_byte1 & 0x7Fu) + 1);
} else {
tmp_word += tmp_byte1 & 0x7Fu;
}

sprintf(opcode_repr, "%s $%04X", name_table[opcode_table[entry].name], tmp_word);
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X %02X:", current_addr, opcode, tmp_byte1);
}

break;
case ACCUM:
sprintf(opcode_repr, "%s A", name_table[opcode_table[entry].name]);
if (hex_output) {
sprintf(hex_dump, "$%04X> %02X:", current_addr, opcode);
}

break;
default:
// Will not happen since each entry in opcode_table has address mode set
break;
}

len = sprintf(output, DUMP_FORMAT, hex_dump, opcode_repr);
output += len;

/* Add cycle count if necessary */
if (cycle_counting) {
output = append_cycle(output, entry);
}

/* Add NES port info if necessary */
switch (opcode_table[entry].addressing) {
case ABSOL:
case ABSIX:
case ABSIY:
if (nes_mode) {
append_nes(output, tmp_word);
}
break;
default:
/* Other addressing modes: not enough info to add NES register annotation */
break;
}
}

Expand Down

0 comments on commit 0ffacd9

Please sign in to comment.