From 2b52c43605b2403b6097869379b717c4366601dc Mon Sep 17 00:00:00 2001 From: jborza Date: Mon, 29 Apr 2019 06:37:56 +0200 Subject: [PATCH] attempt to run nestest --- cpu.c | 6 ++++++ cpu.h | 3 ++- debugger_windows.c | 2 +- disassembler.c | 6 ++---- emu6502.c | 45 +++++++++++++++++++++++++++++---------------- emu6502.vcxproj | 1 + test6502.c | 3 +++ 7 files changed, 44 insertions(+), 22 deletions(-) diff --git a/cpu.c b/cpu.c index 2a8eef5..e6f8f22 100644 --- a/cpu.c +++ b/cpu.c @@ -30,6 +30,12 @@ void set_NZ_flags(State6502 * state, byte value) { state->flags.n = is_negative(value); } +byte flags_as_byte(State6502* state) { + byte flags_value; + memcpy(&flags_value, &state->flags, sizeof(Flags)); + return flags_value; +} + void clear_flags(State6502 * state) { state->flags.b = state->flags.c = diff --git a/cpu.h b/cpu.h index dea0364..81bb98f 100644 --- a/cpu.h +++ b/cpu.h @@ -7,4 +7,5 @@ void* unimplemented_instruction(State6502* state); int emulate_6502_op(State6502* state); void clear_flags(State6502* state); -void clear_state(State6502* state); \ No newline at end of file +void clear_state(State6502* state); +byte flags_as_byte(State6502* state); \ No newline at end of file diff --git a/debugger_windows.c b/debugger_windows.c index a83604c..f737cbd 100644 --- a/debugger_windows.c +++ b/debugger_windows.c @@ -134,7 +134,7 @@ void print_frame() { } } -int main(int argc, char* argv[]) { +int main2(int argc, char* argv[]) { State6502 state; clear_state(&state); state.memory = malloc(MEMORY_SIZE); diff --git a/disassembler.c b/disassembler.c index a58fc09..8b54f0b 100644 --- a/disassembler.c +++ b/disassembler.c @@ -4,7 +4,7 @@ #include void disassemble_6502(byte* buffer, word pc) { - printf("%04X ", pc); + printf("%04X ", pc); byte* code = &buffer[pc]; int bytes = 1; char op[128] = ""; @@ -182,7 +182,5 @@ void disassemble_6502(byte* buffer, word pc) { else { printf(" "); } - printf(op); - - printf("\n"); + printf(" %s", op); } \ No newline at end of file diff --git a/emu6502.c b/emu6502.c index 2996d62..9ce6848 100644 --- a/emu6502.c +++ b/emu6502.c @@ -9,33 +9,46 @@ #include "disassembler.h" #include "opcodes.h" #include "test6502.h" +#include +#include -byte* read_game() { - FILE* file = fopen("..\\bins\\tetris.bin", "r"); - //byte* buffer = malloc(0x800); - byte buffer[32]; - int read = fread(&buffer, sizeof(byte), 32, file); +#define NESTEST_SIZE 0x4000 +#define NESTEST_DST 0xC000 +#define MEMORY_SIZE 0xFFFF + +byte* read_nestest() { + FILE* file = fopen("nestest\\nestest.bin", "rb"); + if (!file) { + int err = errno; + exit(1); + } + byte buffer[NESTEST_SIZE]; + int read = fread(&buffer, sizeof(byte), NESTEST_SIZE, file); fclose(file); return buffer; } - - -void emulate_game() { +void run_nestest() { State6502 state; - //FILE* file = fopen("..\\bins\\tetris.bin", "r"); clear_state(&state); - state.memory = read_game(); - for (int i = 0; i < 100; i++) { - disassemble_6502(state.memory, i); - } + state.memory = malloc(MEMORY_SIZE); + memset(state.memory, 0, MEMORY_SIZE); + byte* bin = read_nestest(); + //const word TARGET = 0xC000; + memcpy(state.memory + NESTEST_DST, bin, NESTEST_SIZE); + memcpy(state.memory + 0x8000, bin, NESTEST_SIZE); + state.pc = NESTEST_DST; + do{ + disassemble_6502(state.memory, state.pc); + emulate_6502_op(&state); + printf(" A:%02X X:%02X Y:%02X P:%02X SP:%02X\n", state.a, state.x, state.y, flags_as_byte(&state), state.sp); + } while (state.flags.b != 1); } int main() { - //emulate_game(); - run_tests(); - printf("All tests succeeded.\n"); + run_nestest(); + //run_tests(); return 0; } diff --git a/emu6502.vcxproj b/emu6502.vcxproj index 488157f..22a99ad 100644 --- a/emu6502.vcxproj +++ b/emu6502.vcxproj @@ -152,6 +152,7 @@ + diff --git a/test6502.c b/test6502.c index f913081..881ce17 100644 --- a/test6502.c +++ b/test6502.c @@ -40,6 +40,7 @@ void print_all(State6502 * state) { void test_step(State6502 * state) { print_all(state); disassemble_6502(state->memory, state->pc); + printf("\n"); emulate_6502_op(state); print_all(state); } @@ -48,6 +49,7 @@ void test_step_until_break(State6502 * state) { do { print_all(state); disassemble_6502(state->memory, state->pc); + printf("\n"); emulate_6502_op(state); } while (state->flags.b != 1); print_all(state); @@ -1870,6 +1872,7 @@ void test_PHA_PLA() { //act for (int i = 0; i < 9; i++) { disassemble_6502(state.memory, state.pc); + printf("\n"); emulate_6502_op(&state); }