Refactored disassembly code to combine steps
This commit is contained in:
parent
7985830ecd
commit
4c9a2daeb4
146
dcc6502.c
146
dcc6502.c
|
@ -43,8 +43,8 @@
|
||||||
#define ABSIY 6 /* Absolute indexed with Y */
|
#define ABSIY 6 /* Absolute indexed with Y */
|
||||||
#define ZEPIX 7 /* Zero page indexed with X */
|
#define ZEPIX 7 /* Zero page indexed with X */
|
||||||
#define ZEPIY 8 /* Zero page indexed with Y */
|
#define ZEPIY 8 /* Zero page indexed with Y */
|
||||||
#define INDIN 9 /* Indexed indirect (with x) */
|
#define INDIN 9 /* Indexed indirect (with X) */
|
||||||
#define ININD 10 /* Indirect indexed (with y) */
|
#define ININD 10 /* Indirect indexed (with Y) */
|
||||||
#define RELAT 11 /* Relative */
|
#define RELAT 11 /* Relative */
|
||||||
#define ACCUM 12 /* Accumulator */
|
#define ACCUM 12 /* Accumulator */
|
||||||
|
|
||||||
|
@ -304,7 +304,7 @@ void emit_header(char *filename, int fsize, uint16_t org) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This function appends cycle counting to the comment block */
|
/* This function appends cycle counting to the comment block */
|
||||||
void append_cycle(char *input, uint8_t entry, uint16_t arg, uint16_t cur_PC) {
|
char *append_cycle(char *input, uint8_t entry) {
|
||||||
char tmpstr[256];
|
char tmpstr[256];
|
||||||
int cycles = opcode_table[entry].cycles;
|
int cycles = opcode_table[entry].cycles;
|
||||||
|
|
||||||
|
@ -316,6 +316,7 @@ void append_cycle(char *input, uint8_t entry, uint16_t arg, uint16_t cur_PC) {
|
||||||
}
|
}
|
||||||
|
|
||||||
strcat(input, tmpstr);
|
strcat(input, tmpstr);
|
||||||
|
return (input + strlen(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_nes_str(char *instr, char *instr2) {
|
void add_nes_str(char *instr, char *instr2) {
|
||||||
|
@ -368,14 +369,13 @@ void append_nes(char *input, uint16_t arg) {
|
||||||
// FIXME: Refactor code to reduce line duplication and make more readable
|
// FIXME: Refactor code to reduce line duplication and make more readable
|
||||||
/* This function disassembles the opcode at the PC and outputs it in *output */
|
/* This function disassembles the opcode at the PC and outputs it in *output */
|
||||||
void disassemble(char *output) {
|
void disassemble(char *output) {
|
||||||
uint8_t tmp_byte1, opcode;
|
|
||||||
char tmpstr[256], opcode_repr[256], hex_dump[256];
|
char tmpstr[256], opcode_repr[256], hex_dump[256];
|
||||||
word tmp_word;
|
|
||||||
int i;
|
int i;
|
||||||
int entry = 0;
|
int entry = 0;
|
||||||
int found = 0;
|
int found = 0;
|
||||||
int len = 0;
|
uint8_t tmp_byte1, opcode;
|
||||||
unsigned int current_addr = org + PC;
|
word tmp_word = 0;
|
||||||
|
uint16_t current_addr = org + PC;
|
||||||
|
|
||||||
opcode = buffer[current_addr - org];
|
opcode = buffer[current_addr - org];
|
||||||
|
|
||||||
|
@ -390,12 +390,11 @@ void disassemble(char *output) {
|
||||||
sprintf(opcode_repr, ".byte $%02x", opcode);
|
sprintf(opcode_repr, ".byte $%02x", opcode);
|
||||||
if (hex_output) {
|
if (hex_output) {
|
||||||
sprintf(hex_dump, "$%04X> %02X:", current_addr, opcode);
|
sprintf(hex_dump, "$%04X> %02X:", current_addr, opcode);
|
||||||
len = sprintf(output, "%-16s%-16s; INVALID OPCODE !!!\n", hex_dump, opcode_repr);
|
sprintf(output, "%-16s%-16s; INVALID OPCODE !!!\n", hex_dump, opcode_repr);
|
||||||
} else {
|
} else {
|
||||||
sprintf(hex_dump, "$%04X", current_addr);
|
sprintf(hex_dump, "$%04X", current_addr);
|
||||||
len = sprintf(output, "%-8s%-16s; INVALID OPCODE !!!\n", hex_dump, opcode_repr);
|
sprintf(output, "%-8s%-16s; INVALID OPCODE !!!\n", hex_dump, opcode_repr);
|
||||||
}
|
}
|
||||||
output += len;
|
|
||||||
} else {
|
} else {
|
||||||
switch (opcode_table[entry].addressing) {
|
switch (opcode_table[entry].addressing) {
|
||||||
case IMMED:
|
case IMMED:
|
||||||
|
@ -407,38 +406,24 @@ void disassemble(char *output) {
|
||||||
} else {
|
} else {
|
||||||
sprintf(hex_dump, "$%04X", current_addr);
|
sprintf(hex_dump, "$%04X", current_addr);
|
||||||
}
|
}
|
||||||
len = sprintf(output, DUMP_FORMAT, hex_dump, opcode_repr);
|
sprintf(output, DUMP_FORMAT, hex_dump, opcode_repr);
|
||||||
output += len;
|
|
||||||
|
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(output, entry, current_addr, current_addr);
|
|
||||||
}
|
|
||||||
PC++;
|
PC++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ABSOL:
|
case ABSOL:
|
||||||
/* Get address */
|
/* Get address */
|
||||||
tmp_word = LOAD_WORD(buffer, PC);
|
tmp_word = LOAD_WORD(buffer, PC);
|
||||||
|
|
||||||
if (hex_output)
|
sprintf(opcode_repr, "%s $%02X%02X", name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
|
||||||
sprintf(tmpstr, "$%04X> %02X %02X%02X:\t%s $%02X%02X\t;", current_addr, opcode, LOW_PART(tmp_word), HIGH_PART(tmp_word), name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
|
if (hex_output) {
|
||||||
else
|
sprintf(hex_dump, "$%04X> %02X %02X%02X:", current_addr, opcode, LOW_PART(tmp_word), HIGH_PART(tmp_word));
|
||||||
sprintf(tmpstr, "$%04X\t%s $%02X%02X\t;", current_addr, name_table[opcode_table[entry].name], HIGH_PART(tmp_word), LOW_PART(tmp_word));
|
} else {
|
||||||
|
sprintf(hex_dump, "$%04X", current_addr);
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(tmpstr, entry, tmp_word, current_addr);
|
|
||||||
}
|
}
|
||||||
|
sprintf(output, DUMP_FORMAT, hex_dump, opcode_repr);
|
||||||
|
|
||||||
/* Add NES port info if necessary */
|
|
||||||
if (nes_mode) {
|
|
||||||
append_nes(tmpstr, tmp_word);
|
|
||||||
}
|
|
||||||
strncpy(output, tmpstr, 254);
|
|
||||||
PC += 2;
|
PC += 2;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ZEROP:
|
case ZEROP:
|
||||||
PC++;
|
PC++;
|
||||||
tmp_byte1 = buffer[PC]; /* Get low byte of address */
|
tmp_byte1 = buffer[PC]; /* Get low byte of address */
|
||||||
|
@ -448,15 +433,8 @@ void disassemble(char *output) {
|
||||||
} else {
|
} else {
|
||||||
sprintf(tmpstr, "$%04X\t%s $%02X\t\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
|
sprintf(tmpstr, "$%04X\t%s $%02X\t\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(tmpstr, entry, org+PC-1, org+PC-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(output, tmpstr, 254);
|
strncpy(output, tmpstr, 254);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IMPLI:
|
case IMPLI:
|
||||||
if (hex_output) {
|
if (hex_output) {
|
||||||
sprintf(tmpstr, "$%04X> %02X:\t%s\t\t;", org+PC, opcode, name_table[opcode_table[entry].name]);
|
sprintf(tmpstr, "$%04X> %02X:\t%s\t\t;", org+PC, opcode, name_table[opcode_table[entry].name]);
|
||||||
|
@ -464,14 +442,8 @@ void disassemble(char *output) {
|
||||||
sprintf(tmpstr, "$%04X\t%s\t\t;", org+PC, name_table[opcode_table[entry].name]);
|
sprintf(tmpstr, "$%04X\t%s\t\t;", org+PC, name_table[opcode_table[entry].name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(tmpstr, entry, org+PC, org+PC);
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(output, tmpstr, 254);
|
strncpy(output, tmpstr, 254);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case INDIA:
|
case INDIA:
|
||||||
PC++;
|
PC++;
|
||||||
PC++;
|
PC++;
|
||||||
|
@ -483,14 +455,8 @@ void disassemble(char *output) {
|
||||||
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));
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(tmpstr, entry, tmp_word, org+PC-2);
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(output, tmpstr, 254);
|
strncpy(output, tmpstr, 254);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ABSIX:
|
case ABSIX:
|
||||||
PC++;
|
PC++;
|
||||||
PC++;
|
PC++;
|
||||||
|
@ -502,19 +468,8 @@ void disassemble(char *output) {
|
||||||
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));
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(tmpstr, entry, tmp_word, org+PC-2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Add NES port info if necessary */
|
|
||||||
if (nes_mode) {
|
|
||||||
append_nes(tmpstr, tmp_word);
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(output, tmpstr, 254);
|
strncpy(output, tmpstr, 254);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ABSIY:
|
case ABSIY:
|
||||||
PC++;
|
PC++;
|
||||||
PC++;
|
PC++;
|
||||||
|
@ -526,19 +481,8 @@ void disassemble(char *output) {
|
||||||
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));
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(tmpstr, entry, tmp_word, org+PC-2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Add NES port info if necessary */
|
|
||||||
if (nes_mode) {
|
|
||||||
append_nes(tmpstr, tmp_word);
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(output, tmpstr, 254);
|
strncpy(output, tmpstr, 254);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ZEPIX:
|
case ZEPIX:
|
||||||
PC++;
|
PC++;
|
||||||
tmp_byte1 = buffer[PC]; /* Get low byte of address */
|
tmp_byte1 = buffer[PC]; /* Get low byte of address */
|
||||||
|
@ -549,14 +493,8 @@ void disassemble(char *output) {
|
||||||
sprintf(tmpstr, "$%04X\t%s $%02X,X\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
|
sprintf(tmpstr, "$%04X\t%s $%02X,X\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(tmpstr, entry, org+PC-1, org+PC-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(output, tmpstr, 254);
|
strncpy(output, tmpstr, 254);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ZEPIY:
|
case ZEPIY:
|
||||||
PC++;
|
PC++;
|
||||||
tmp_byte1 = buffer[PC]; /* Get low byte of address */
|
tmp_byte1 = buffer[PC]; /* Get low byte of address */
|
||||||
|
@ -567,14 +505,8 @@ void disassemble(char *output) {
|
||||||
sprintf(tmpstr, "$%04X\t%s $%02X,Y\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
|
sprintf(tmpstr, "$%04X\t%s $%02X,Y\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(tmpstr, entry, org+PC-1, org+PC-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(output, tmpstr, 254);
|
strncpy(output, tmpstr, 254);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case INDIN:
|
case INDIN:
|
||||||
PC++;
|
PC++;
|
||||||
tmp_byte1 = buffer[PC]; /* Get low byte of address */
|
tmp_byte1 = buffer[PC]; /* Get low byte of address */
|
||||||
|
@ -585,14 +517,8 @@ void disassemble(char *output) {
|
||||||
sprintf(tmpstr, "$%04X\t%s ($%02X,X)\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
|
sprintf(tmpstr, "$%04X\t%s ($%02X,X)\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(tmpstr, entry, org+PC-1, org+PC-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(output, tmpstr, 254);
|
strncpy(output, tmpstr, 254);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ININD:
|
case ININD:
|
||||||
PC++;
|
PC++;
|
||||||
tmp_byte1 = buffer[PC]; /* Get low byte of address */
|
tmp_byte1 = buffer[PC]; /* Get low byte of address */
|
||||||
|
@ -603,32 +529,21 @@ void disassemble(char *output) {
|
||||||
sprintf(tmpstr, "$%04X\t%s ($%02X),Y\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
|
sprintf(tmpstr, "$%04X\t%s ($%02X),Y\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(tmpstr, entry, org+PC-1, org+PC-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(output, tmpstr, 254);
|
strncpy(output, tmpstr, 254);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case RELAT:
|
case RELAT:
|
||||||
PC++;
|
PC++;
|
||||||
tmp_byte1 = buffer[PC]; /* Get relative modifier */
|
tmp_byte1 = buffer[PC]; /* Get relative modifier */
|
||||||
|
|
||||||
|
// FIXME: Resolve undefined behavior of cast for signed relative addressing
|
||||||
if (hex_output) {
|
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);
|
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 {
|
} else {
|
||||||
sprintf(tmpstr, "$%04X\t%s $%04X\t;", org+PC-1, name_table[opcode_table[entry].name], (org+PC)+(signed char)(tmp_byte1)+1);
|
sprintf(tmpstr, "$%04X\t%s $%04X\t;", org+PC-1, name_table[opcode_table[entry].name], (org+PC)+(signed char)(tmp_byte1)+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(tmpstr, entry, org+PC, org+PC);
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(output, tmpstr, 254);
|
strncpy(output, tmpstr, 254);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ACCUM:
|
case ACCUM:
|
||||||
if (hex_output) {
|
if (hex_output) {
|
||||||
sprintf(tmpstr, "$%04X> %02X:\t%s A\t\t;", org+PC, opcode, name_table[opcode_table[entry].name]);
|
sprintf(tmpstr, "$%04X> %02X:\t%s A\t\t;", org+PC, opcode, name_table[opcode_table[entry].name]);
|
||||||
|
@ -636,17 +551,32 @@ void disassemble(char *output) {
|
||||||
sprintf(tmpstr, "$%04X\t%s A\t\t;", org+PC, name_table[opcode_table[entry].name]);
|
sprintf(tmpstr, "$%04X\t%s A\t\t;", org+PC, name_table[opcode_table[entry].name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add cycle count if necessary */
|
|
||||||
if (cycle_counting) {
|
|
||||||
append_cycle(tmpstr, entry, org+PC, org+PC);
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(output, tmpstr, 254);
|
strncpy(output, tmpstr, 254);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output += strlen(output);
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue