From 6a5b0b728e522cd4e90a2ae775666ad6c36d3c5a Mon Sep 17 00:00:00 2001 From: vin Date: Mon, 17 Jun 2024 15:11:45 +0530 Subject: [PATCH] start fixing bugs with memory access So this is why tests should be written while writing the program and instructions instead of all at once later. If this were all to be rewritten (which it probably will), I should add tests for each opcode instead of waiting until the end for ROM loading support. --- cpu.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cpu.c b/cpu.c index 968fcd8..594c462 100644 --- a/cpu.c +++ b/cpu.c @@ -81,10 +81,8 @@ peek(uint16_t addr) static uint16_t peek16(uint16_t addr) { - MEMORY_MIRROR(addr); - /* bytes are stored in little-endian (low then high) */ - return (uint16_t)memory[addr] | ((uint16_t)memory[addr + 1] << 8); + return peek(addr) | (peek(addr + 1) << 8); } static void @@ -108,7 +106,7 @@ memwrite16(uint16_t addr, uint16_t word) static uint8_t opcode_arg(enum addressing_mode mode) { - uint8_t arg, val; + uint16_t arg, val; if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y) arg = peek(regs.pc++); @@ -149,14 +147,15 @@ opcode_arg(enum addressing_mode mode) abort(); } + printf("arg: $%04X $%04X\n", arg, val); + return val; } static uint16_t opcode_mem(enum addressing_mode mode) { - uint8_t arg; - uint16_t val; + uint16_t arg, val; if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y) arg = peek(regs.pc++); @@ -173,6 +172,7 @@ opcode_mem(enum addressing_mode mode) case AM_ZP_Y: val = (arg + regs.y) % 256; break; + case AM_IMM: case AM_ABS: val = arg; break; @@ -1059,7 +1059,7 @@ interpret(void) cycles += 2; break; case 0x4c: - jmp(opcode_arg(AM_ABS)); + jmp(opcode_mem(AM_ABS)); cycles += 3; break; case 0x6c: @@ -1111,7 +1111,7 @@ interpret(void) cycles += 5; break; case 0xa2: - ldx(opcode_arg(AM_IMM)); + ldx(opcode_mem(AM_IMM)); cycles += 2; break; case 0xa6: @@ -1467,8 +1467,8 @@ main(int argc, char *argv[]) return 1; } - memwrite16(0xFFFC, 0x8000); - regs.pc = 0x8000; + memwrite16(0xFFFC, 0xC000); + regs.pc = 0xC000; printf("status: %i%i%i%i%i%i%i%i\n", regs.status.carry, regs.status.zero, regs.status.interrupt_disable,