2017-12-28 03:21:11 +00:00
|
|
|
/*
|
|
|
|
* mos6502.dis.c
|
|
|
|
*
|
2018-02-13 03:15:20 +00:00
|
|
|
* Disassembly of the mos6502 machine code into an assembly notation. I
|
|
|
|
* should note that there is no formal grammar (that I know of!) for
|
|
|
|
* 6502 assembly--just an informal notation that is de-facto supported
|
|
|
|
* by one assembler or another. The general format we use is as follows:
|
|
|
|
*
|
|
|
|
* LABEL:
|
|
|
|
* INS $OPER ; comment
|
|
|
|
*
|
|
|
|
* Where LABEL is a--well, a label; INS is an instruction; $OPER is a
|
|
|
|
* hexadecimal number; and a semicolon denotes a comment follows until
|
|
|
|
* the end of the line.
|
|
|
|
*
|
|
|
|
* You will find a number of variants of `$OPER`, as the assembly
|
|
|
|
* notation uses those variants to denote a specific kind of address
|
|
|
|
* mode. `$OPER` is absolute mode; `$OP` (just two hex digits) is
|
|
|
|
* zero-page mode; `$(OP),Y` is indirect-indexed mode; etc. (Please
|
|
|
|
* refer to mos6502.addr.c for more details on those modes!)
|
|
|
|
*
|
|
|
|
* The code here generally pushes disassembled notation into FILE stream
|
|
|
|
* objects. If you need them in a string, for instance, you can mess
|
|
|
|
* with `setvbuf()` (as we indeed do in our unit-testing code!).
|
2017-12-28 03:21:11 +00:00
|
|
|
*/
|
|
|
|
|
2017-12-29 23:08:25 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2018-04-07 05:26:31 +00:00
|
|
|
#include "mos6502/mos6502.h"
|
|
|
|
#include "mos6502/dis.h"
|
|
|
|
#include "mos6502/enums.h"
|
2017-12-28 03:21:11 +00:00
|
|
|
|
2018-02-24 03:35:53 +00:00
|
|
|
static char s_bytes[10],
|
2018-02-24 02:46:24 +00:00
|
|
|
s_inst[4],
|
|
|
|
s_operand[11];
|
2017-12-29 23:08:25 +00:00
|
|
|
|
2017-12-28 03:21:11 +00:00
|
|
|
static char *instruction_strings[] = {
|
|
|
|
"ADC",
|
|
|
|
"AND",
|
|
|
|
"ASL",
|
2018-01-21 18:48:34 +00:00
|
|
|
"BAD",
|
2017-12-28 03:21:11 +00:00
|
|
|
"BCC",
|
|
|
|
"BCS",
|
|
|
|
"BEQ",
|
|
|
|
"BIT",
|
2018-02-22 03:01:46 +00:00
|
|
|
"BIM",
|
2017-12-28 03:21:11 +00:00
|
|
|
"BMI",
|
|
|
|
"BNE",
|
|
|
|
"BPL",
|
2018-02-22 03:57:21 +00:00
|
|
|
"BRA",
|
2017-12-28 03:21:11 +00:00
|
|
|
"BRK",
|
|
|
|
"BVC",
|
|
|
|
"BVS",
|
|
|
|
"CLC",
|
|
|
|
"CLD",
|
|
|
|
"CLI",
|
|
|
|
"CLV",
|
|
|
|
"CMP",
|
|
|
|
"CPX",
|
|
|
|
"CPY",
|
|
|
|
"DEC",
|
|
|
|
"DEX",
|
|
|
|
"DEY",
|
|
|
|
"EOR",
|
|
|
|
"INC",
|
|
|
|
"INX",
|
|
|
|
"INY",
|
|
|
|
"JMP",
|
|
|
|
"JSR",
|
|
|
|
"LDA",
|
|
|
|
"LDX",
|
|
|
|
"LDY",
|
|
|
|
"LSR",
|
|
|
|
"NOP",
|
2018-02-22 20:07:05 +00:00
|
|
|
"NP2",
|
|
|
|
"NP3",
|
2017-12-28 03:21:11 +00:00
|
|
|
"ORA",
|
|
|
|
"PHA",
|
|
|
|
"PHP",
|
2018-02-22 05:32:57 +00:00
|
|
|
"PHX",
|
|
|
|
"PHY",
|
2017-12-28 03:21:11 +00:00
|
|
|
"PLA",
|
|
|
|
"PLP",
|
2018-02-22 05:32:57 +00:00
|
|
|
"PLX",
|
|
|
|
"PLY",
|
2017-12-28 03:21:11 +00:00
|
|
|
"ROL",
|
|
|
|
"ROR",
|
|
|
|
"RTI",
|
|
|
|
"RTS",
|
|
|
|
"SBC",
|
|
|
|
"SEC",
|
|
|
|
"SED",
|
|
|
|
"SEI",
|
|
|
|
"STA",
|
|
|
|
"STX",
|
|
|
|
"STY",
|
2018-02-22 06:02:57 +00:00
|
|
|
"STZ",
|
2017-12-28 03:21:11 +00:00
|
|
|
"TAX",
|
|
|
|
"TAY",
|
2018-02-22 06:39:33 +00:00
|
|
|
"TRB",
|
2018-02-22 19:39:48 +00:00
|
|
|
"TSB",
|
2017-12-28 03:21:11 +00:00
|
|
|
"TSX",
|
|
|
|
"TXA",
|
|
|
|
"TXS",
|
|
|
|
"TYA",
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a stream, address mode and 16-bit value, print the value out in
|
|
|
|
* the form that is expected given the address mode. The value is not
|
|
|
|
* necessarily going to truly be 16-bit; most address modes use one
|
|
|
|
* 8-bit operand. But we can contain all possible values with the 16-bit
|
|
|
|
* type.
|
|
|
|
*/
|
|
|
|
void
|
2017-12-31 05:02:30 +00:00
|
|
|
mos6502_dis_operand(mos6502 *cpu,
|
2018-01-19 06:34:57 +00:00
|
|
|
char *str,
|
|
|
|
int len,
|
2017-12-31 04:50:23 +00:00
|
|
|
int address,
|
|
|
|
int addr_mode,
|
|
|
|
vm_16bit value)
|
2017-12-28 03:21:11 +00:00
|
|
|
{
|
2017-12-29 23:08:25 +00:00
|
|
|
int rel_address;
|
|
|
|
|
2017-12-28 03:21:11 +00:00
|
|
|
switch (addr_mode) {
|
|
|
|
case ACC:
|
|
|
|
break;
|
|
|
|
case ABS:
|
2018-01-19 06:34:57 +00:00
|
|
|
snprintf(str, len, "$%04X", value);
|
2017-12-28 03:21:11 +00:00
|
|
|
break;
|
|
|
|
case ABX:
|
2018-01-19 06:34:57 +00:00
|
|
|
snprintf(str, len, "$%04X,X", value);
|
2017-12-28 03:21:11 +00:00
|
|
|
break;
|
|
|
|
case ABY:
|
2018-01-19 06:34:57 +00:00
|
|
|
snprintf(str, len, "$%04X,Y", value);
|
2017-12-28 03:21:11 +00:00
|
|
|
break;
|
|
|
|
case IMM:
|
2018-01-19 06:34:57 +00:00
|
|
|
snprintf(str, len, "#$%02X", value);
|
2017-12-28 03:21:11 +00:00
|
|
|
break;
|
|
|
|
case IMP:
|
2018-02-24 02:46:24 +00:00
|
|
|
snprintf(str, len, "");
|
2017-12-28 03:21:11 +00:00
|
|
|
break;
|
|
|
|
case IND:
|
2018-02-24 02:46:24 +00:00
|
|
|
snprintf(str, len, "($%04X)", value);
|
2017-12-28 03:21:11 +00:00
|
|
|
break;
|
|
|
|
case IDX:
|
2018-01-19 06:34:57 +00:00
|
|
|
snprintf(str, len, "($%02X,X)", value);
|
2017-12-28 03:21:11 +00:00
|
|
|
break;
|
|
|
|
case IDY:
|
2018-01-19 06:34:57 +00:00
|
|
|
snprintf(str, len, "($%02X),Y", value);
|
2017-12-28 03:21:11 +00:00
|
|
|
break;
|
|
|
|
case REL:
|
2017-12-29 23:08:25 +00:00
|
|
|
rel_address = address + value;
|
|
|
|
if (value > 127) {
|
|
|
|
rel_address -= 256;
|
|
|
|
}
|
|
|
|
|
2018-02-24 02:46:24 +00:00
|
|
|
snprintf(str, len, "<%04x>", rel_address);
|
2017-12-28 03:21:11 +00:00
|
|
|
break;
|
|
|
|
case ZPG:
|
2018-01-09 03:21:09 +00:00
|
|
|
// We add a couple of spaces here to help our output
|
|
|
|
// comments line up.
|
2018-01-19 06:34:57 +00:00
|
|
|
snprintf(str, len, "$%02X", value);
|
2017-12-28 03:21:11 +00:00
|
|
|
break;
|
|
|
|
case ZPX:
|
2018-01-19 06:34:57 +00:00
|
|
|
snprintf(str, len, "$%02X,X", value);
|
2017-12-28 03:21:11 +00:00
|
|
|
break;
|
|
|
|
case ZPY:
|
2018-01-19 06:34:57 +00:00
|
|
|
snprintf(str, len, "$%02X,Y", value);
|
2017-12-28 03:21:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function will write to the stream the instruction that the given
|
|
|
|
* opcode maps to.
|
|
|
|
*/
|
|
|
|
void
|
2018-01-19 06:34:57 +00:00
|
|
|
mos6502_dis_instruction(char *str, int len, int inst_code)
|
2017-12-28 03:21:11 +00:00
|
|
|
{
|
|
|
|
// Arguably this could or should be done as fputs(), which is
|
2018-01-19 06:34:57 +00:00
|
|
|
// presumably a simpler output method. But, since we use snprintf()
|
2017-12-28 03:21:11 +00:00
|
|
|
// in other places, I think we should continue to do so. Further, we
|
|
|
|
// use a simple format string (%s) to avoid the linter's complaints
|
|
|
|
// about potential security issues.
|
2018-01-19 06:34:57 +00:00
|
|
|
snprintf(str, len, "%s", instruction_strings[inst_code]);
|
2017-12-28 03:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function returns the number of bytes that the given opcode is
|
|
|
|
* expecting to work with. For instance, if the opcode is in absolute
|
|
|
|
* address mode, then we will need to read the next two bytes in the
|
|
|
|
* stream to compose a full 16-bit address to work with. If our opcode
|
|
|
|
* is in immediate mode, then we only need to read one byte. Many
|
|
|
|
* opcodes will read no bytes at all from the stream (in which we return
|
|
|
|
* zero).
|
|
|
|
*/
|
|
|
|
int
|
2017-12-29 05:47:36 +00:00
|
|
|
mos6502_dis_expected_bytes(int addr_mode)
|
2017-12-28 03:21:11 +00:00
|
|
|
{
|
|
|
|
switch (addr_mode) {
|
2018-02-22 20:07:05 +00:00
|
|
|
// This is kind of not a real address mode? We use it to tell
|
|
|
|
// the code to skip three bytes for opcodes that use it.
|
|
|
|
case BY3:
|
|
|
|
return 3;
|
|
|
|
|
2017-12-28 03:21:11 +00:00
|
|
|
// These are 16-bit operands, because they work with absolute
|
|
|
|
// addresses in memory.
|
|
|
|
case ABS:
|
|
|
|
case ABY:
|
|
|
|
case ABX:
|
2018-02-22 20:07:05 +00:00
|
|
|
case BY2: // (also not a real address mode!)
|
2017-12-28 03:21:11 +00:00
|
|
|
case IND:
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
// These are the 8-bit operand address modes.
|
|
|
|
case IMM:
|
|
|
|
case IDX:
|
|
|
|
case IDY:
|
|
|
|
case REL:
|
|
|
|
case ZPG:
|
|
|
|
case ZPX:
|
|
|
|
case ZPY:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// These two address modes have implied arguments; ACC is
|
|
|
|
// the accumulator, and IMP basically means it operates on
|
|
|
|
// some specific (presumably obvious) thing and no operand
|
|
|
|
// is necessary.
|
|
|
|
case ACC:
|
|
|
|
case IMP:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// I don't know how we got here, outside of foul magicks and cruel
|
|
|
|
// trickery. Let's fearfully return zero!
|
|
|
|
return 0;
|
|
|
|
}
|
2017-12-29 03:47:35 +00:00
|
|
|
|
|
|
|
/*
|
2017-12-31 05:02:30 +00:00
|
|
|
* Scan memory (with a given address) and write the opcode at that
|
2017-12-29 03:47:35 +00:00
|
|
|
* point to the given file stream. This function will also write an
|
|
|
|
* operand to the file stream if one is warranted. We return the number
|
|
|
|
* of bytes consumed by scanning past the opcode and/or operand.
|
|
|
|
*/
|
|
|
|
int
|
2017-12-31 05:02:30 +00:00
|
|
|
mos6502_dis_opcode(mos6502 *cpu, FILE *stream, int address)
|
2017-12-29 03:47:35 +00:00
|
|
|
{
|
|
|
|
vm_8bit opcode;
|
|
|
|
vm_16bit operand;
|
2017-12-29 05:47:36 +00:00
|
|
|
int addr_mode;
|
2017-12-29 23:08:25 +00:00
|
|
|
int inst_code;
|
2017-12-29 03:47:35 +00:00
|
|
|
int expected;
|
2018-03-12 04:28:28 +00:00
|
|
|
char status[9];
|
2017-12-29 03:47:35 +00:00
|
|
|
|
2018-02-25 22:06:27 +00:00
|
|
|
memset(s_bytes, 0, sizeof(s_bytes));
|
|
|
|
memset(s_inst, 0, sizeof(s_inst));
|
|
|
|
memset(s_operand, 0, sizeof(s_operand));
|
|
|
|
|
2017-12-29 03:47:35 +00:00
|
|
|
// The next byte is assumed to be the opcode we work with.
|
2018-01-11 03:28:05 +00:00
|
|
|
opcode = mos6502_get(cpu, address);
|
2017-12-29 03:47:35 +00:00
|
|
|
|
|
|
|
// And given that opcode, we need to see how many bytes large our
|
|
|
|
// operand will be.
|
2017-12-29 05:47:36 +00:00
|
|
|
addr_mode = mos6502_addr_mode(opcode);
|
|
|
|
expected = mos6502_dis_expected_bytes(addr_mode);
|
2017-12-29 03:47:35 +00:00
|
|
|
|
2017-12-29 23:08:25 +00:00
|
|
|
// The specific instruction we mean to execute
|
|
|
|
inst_code = mos6502_instruction(opcode);
|
|
|
|
|
2017-12-29 03:47:35 +00:00
|
|
|
// The operand itself defaults to zero... in cases where this
|
|
|
|
// doesn't change, the instruction related to the opcode will
|
|
|
|
// probably not even use it.
|
|
|
|
operand = 0;
|
|
|
|
|
|
|
|
// And we need to skip ahead of the opcode.
|
|
|
|
address++;
|
|
|
|
|
|
|
|
switch (expected) {
|
|
|
|
case 2:
|
2018-01-09 03:21:09 +00:00
|
|
|
// Remember that the 6502 is little-endian, so the operand
|
|
|
|
// needs to be retrieved with the LSB first and the MSB
|
|
|
|
// second.
|
2018-01-11 03:28:05 +00:00
|
|
|
operand |= mos6502_get(cpu, address++);
|
|
|
|
operand |= mos6502_get(cpu, address++) << 8;
|
2018-01-09 03:21:09 +00:00
|
|
|
break;
|
2017-12-29 03:47:35 +00:00
|
|
|
|
|
|
|
case 1:
|
2018-01-11 03:28:05 +00:00
|
|
|
operand |= mos6502_get(cpu, address++);
|
2018-01-09 03:21:09 +00:00
|
|
|
break;
|
2017-12-29 03:47:35 +00:00
|
|
|
|
|
|
|
// And, in any other case (e.g. 0), we are done; we don't
|
|
|
|
// read anything, and we leave the operand as it is.
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-29 23:08:25 +00:00
|
|
|
// It's totally possible that we are not expected to print out the
|
|
|
|
// contents of our inspection of the opcode. (For example, we may
|
|
|
|
// just want to set the jump table in a lookahead operation.)
|
|
|
|
if (stream) {
|
|
|
|
// Print out the instruction code that our opcode represents.
|
2018-01-19 06:34:57 +00:00
|
|
|
mos6502_dis_instruction(s_inst, sizeof(s_inst), inst_code);
|
2017-12-29 23:08:25 +00:00
|
|
|
|
2018-01-09 03:21:09 +00:00
|
|
|
if (expected) {
|
2017-12-29 23:08:25 +00:00
|
|
|
// Print out the operand given the proper address mode.
|
2018-02-24 02:46:24 +00:00
|
|
|
mos6502_dis_operand(cpu, s_operand, sizeof(s_operand),
|
|
|
|
address, addr_mode, operand);
|
2018-01-09 03:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// And three, the operand, if any. Remembering that the operand
|
|
|
|
// should be shown in little-endian order.
|
|
|
|
if (expected == 2) {
|
2018-02-24 02:46:24 +00:00
|
|
|
snprintf(s_bytes, sizeof(s_bytes) - 1, "%02X %02X %02X",
|
2018-01-19 06:34:57 +00:00
|
|
|
opcode, operand & 0xff, operand >> 8);
|
2018-01-09 03:21:09 +00:00
|
|
|
} else if (expected == 1) {
|
2018-02-24 02:46:24 +00:00
|
|
|
snprintf(s_bytes, sizeof(s_bytes) - 1, "%02X %02X",
|
2018-01-19 06:34:57 +00:00
|
|
|
opcode, operand & 0xff);
|
|
|
|
} else {
|
2018-02-24 02:46:24 +00:00
|
|
|
snprintf(s_bytes, sizeof(s_bytes) - 1, "%02X", opcode);
|
2017-12-29 23:08:25 +00:00
|
|
|
}
|
2018-01-19 06:34:57 +00:00
|
|
|
|
2018-03-12 04:28:28 +00:00
|
|
|
snprintf(status, sizeof(status), "%c%c_%c%c%c%c%c",
|
|
|
|
cpu->P & MOS_NEGATIVE ? 'N' : '_',
|
|
|
|
cpu->P & MOS_OVERFLOW ? 'V' : '_',
|
|
|
|
cpu->P & MOS_BREAK ? 'B' : '_',
|
|
|
|
cpu->P & MOS_DECIMAL ? 'D' : '_',
|
|
|
|
cpu->P & MOS_INTERRUPT ? 'I' : '_',
|
|
|
|
cpu->P & MOS_ZERO ? 'Z' : '_',
|
|
|
|
cpu->P & MOS_CARRY ? 'C' : '_');
|
|
|
|
|
|
|
|
fprintf(stream, "%04X:%-9s%20s %-20s; A:%02X X:%02X Y:%02X P:%02X<%s> S:%02X\n",
|
|
|
|
cpu->PC, s_bytes, s_inst, s_operand,
|
|
|
|
cpu->A, cpu->X, cpu->Y, cpu->P, status, cpu->S);
|
2018-03-08 03:24:29 +00:00
|
|
|
}
|
2017-12-29 03:47:35 +00:00
|
|
|
|
|
|
|
// The expected number of bytes here is for the operand, but we need
|
|
|
|
// to add one for the opcode to return the true number that this
|
|
|
|
// opcode sequence would consume.
|
|
|
|
return expected + 1;
|
|
|
|
}
|
2017-12-29 21:31:05 +00:00
|
|
|
|
2018-01-07 22:07:29 +00:00
|
|
|
/*
|
|
|
|
* Scan the CPU memory, from a given position until a given end, and
|
|
|
|
* print the results into a given file stream.
|
|
|
|
*/
|
2017-12-29 21:31:05 +00:00
|
|
|
void
|
2017-12-31 05:02:30 +00:00
|
|
|
mos6502_dis_scan(mos6502 *cpu, FILE *stream, int pos, int end)
|
2017-12-29 21:31:05 +00:00
|
|
|
{
|
2018-03-02 03:37:10 +00:00
|
|
|
vm_16bit pc;
|
|
|
|
|
|
|
|
pc = cpu->PC;
|
2017-12-29 21:31:05 +00:00
|
|
|
while (pos < end) {
|
2018-03-02 03:37:10 +00:00
|
|
|
cpu->PC = pos;
|
2017-12-31 05:02:30 +00:00
|
|
|
pos += mos6502_dis_opcode(cpu, stream, pos);
|
2017-12-29 21:31:05 +00:00
|
|
|
}
|
2018-03-02 03:37:10 +00:00
|
|
|
|
|
|
|
cpu->PC = pc;
|
2017-12-29 21:31:05 +00:00
|
|
|
}
|