diff --git a/cpu.c b/cpu.c index 14c2ce3..c7e76b8 100644 --- a/cpu.c +++ b/cpu.c @@ -1,5 +1,6 @@ #include #include +#include #define STATUS_UPDATE_ZERO(r) \ (regs.status.zero = r == 0) @@ -41,7 +42,7 @@ enum addressing_mode { AM_INDIRECT_INDEXED, }; -uint8_t addrbus[0x10000]; +uint8_t memory[0x10000]; /* example program taken from https://bugzmanov.github.io/nes_ebook/chapter_3_1.html */ uint8_t program[] = { 0xa9, 0xc0, 0xaa, 0xe8, 0x00 }; @@ -82,7 +83,7 @@ lda(enum addressing_mode mode) switch (mode) { case AM_IMMEDIATE: /* $A9 */ - val = program[regs.pc++]; + val = memory[regs.pc++]; printf("arg1 $%02X\n", val); regs.a = val; break; @@ -119,22 +120,9 @@ void interpret(void) { uint8_t opcode; - regs.pc = 0; - regs.status.unused = 1; - - printf("Initial State:\n"); - printf("status: %i%i%i%i%i%i%i%i\n", regs.status.carry, - regs.status.zero, regs.status.interrupt_disable, - regs.status.decimal_mode, regs.status.brk, - regs.status.unused, regs.status.overflow, - regs.status.negative); - printf("A: $%02X, X: $%02X, Y: $%02X, SP: $%02X, PC: $%02X\n", - regs.a, regs.x, regs.y, regs.sp, regs.pc); - - putchar('\n'); for (;;) { - opcode = program[regs.pc++]; + opcode = memory[regs.pc++]; printf("opcode: $%02X\n", opcode); @@ -190,6 +178,26 @@ interpret(void) int main(void) { + if (sizeof(program) > (0x10000 - 0x8000)) { + fprintf(stderr, "program is too big for memory\n"); + return 1; + } + memcpy(memory + 0x8000, program, sizeof(program)); + regs.pc = 0x8000; + + regs.status.unused = 1; + + printf("Initial State:\n"); + printf("status: %i%i%i%i%i%i%i%i\n", regs.status.carry, + regs.status.zero, regs.status.interrupt_disable, + regs.status.decimal_mode, regs.status.brk, + regs.status.unused, regs.status.overflow, + regs.status.negative); + printf("A: $%02X, X: $%02X, Y: $%02X, SP: $%02X, PC: $%02X\n", + regs.a, regs.x, regs.y, regs.sp, regs.pc); + + putchar('\n'); + interpret(); return 0;