From fa9d26d34db98c483e0ef10209356b3ed22254a9 Mon Sep 17 00:00:00 2001 From: Nathaniel Walizer Date: Mon, 6 Jan 2025 22:14:21 -0800 Subject: [PATCH] Add raw instruction debug output; add option to ignore illegal instructions --- e6502.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/e6502.c b/e6502.c index 28e49bb..f467261 100644 --- a/e6502.c +++ b/e6502.c @@ -515,7 +515,9 @@ static inline void sbc(e6502_Core *core, e6502_Address adr) { static inline void inc(e6502_Core *core, e6502_Address adr) { // TODO: Emulate double-read or double-write - uint8_t val = e6502_adr_r8(core, adr) + 1; + uint8_t val = e6502_adr_r8(core, adr); + e6502_adr_w8(core, adr, val); + val += 1; core->registers.P &= ~(e6502_Status_Z | e6502_Status_N); core->registers.P |= (val & e6502_Status_N); if (!val) core->registers.P |= e6502_Status_Z; @@ -869,7 +871,7 @@ static const e6502_Instruction e6502_instructions[256] = { }; -/* +#ifdef E6502_DEBUG int e6502_instr_size(uint8_t opcode) { const e6502_Instruction* instr = &e6502_instructions[opcode]; if ( instr->address == adr_imp || @@ -884,7 +886,7 @@ int e6502_instr_size(uint8_t opcode) { } } -int e6502_dump_instr(e6502_Core* core, e6502_Mem_Addr addr, +void e6502_dump_instr(e6502_Core* core, e6502_Mem_Addr addr, FILE* file) { uint8_t opcode = e6502_r8(core, addr); int size = e6502_instr_size(opcode); @@ -894,11 +896,9 @@ int e6502_dump_instr(e6502_Core* core, e6502_Mem_Addr addr, } else if (size == 3) { fprintf(file, " $%04x", e6502_r16(core, addr + 1)); } - fputc('\n', file); +// fputc('\n', file); } -*/ -#ifdef E6502_DEBUG void e6502_dump_regs(e6502_Core* core, FILE* file) { fprintf(file, "S:%02x A:%02x X:%02x Y:%02x P:%02x\n", core->registers.S, core->registers.A, @@ -938,9 +938,14 @@ int e6502_step(e6502_Core* core) { &e6502_instructions[opcode]; if (!instr->operator) { #ifdef E6502_DEBUG - fprintf(stdout, "$%04x: $%02x\n", last_pc, opcode); + fprintf(stdout, "$%04x: $%02x -> ILLEGAL\n", last_pc, opcode); #endif +#ifndef E6502_ILLEGAL core->cycle = -1; +#else + core->registers.PC++; + core->cycle += 1; +#endif } else { core->registers.PC++; @@ -954,6 +959,8 @@ int e6502_step(e6502_Core* core) { } e6502_dump_regs(core, stdout); fprintf(stdout, "$%04x: ", last_pc); + e6502_dump_instr(core, last_pc, stdout); + fputs(" -> ", stdout); e6502_fprintf_instr(stdout, opcode, (uint8_t*)&val); fputc('\n', stdout); #endif