summaryrefslogtreecommitdiff
path: root/cpu.c
diff options
context:
space:
mode:
authorvin <git@vineetk.net>2024-06-17 15:11:45 +0530
committervin <git@vineetk.net>2024-06-17 15:11:45 +0530
commit6a5b0b728e522cd4e90a2ae775666ad6c36d3c5a (patch)
tree5c00833ff654217b4d7c73b9e4dfe93ad4f9ee02 /cpu.c
parent77d37dd455f804c79969979eb39d525b3c5ddfe7 (diff)
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.
Diffstat (limited to 'cpu.c')
-rw-r--r--cpu.c20
1 files 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,