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.
This commit is contained in:
parent
77d37dd455
commit
6a5b0b728e
20
cpu.c
20
cpu.c
@ -81,10 +81,8 @@ peek(uint16_t addr)
|
|||||||
static uint16_t
|
static uint16_t
|
||||||
peek16(uint16_t addr)
|
peek16(uint16_t addr)
|
||||||
{
|
{
|
||||||
MEMORY_MIRROR(addr);
|
|
||||||
|
|
||||||
/* bytes are stored in little-endian (low then high) */
|
/* 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
|
static void
|
||||||
@ -108,7 +106,7 @@ memwrite16(uint16_t addr, uint16_t word)
|
|||||||
static uint8_t
|
static uint8_t
|
||||||
opcode_arg(enum addressing_mode mode)
|
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)
|
if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y)
|
||||||
arg = peek(regs.pc++);
|
arg = peek(regs.pc++);
|
||||||
@ -149,14 +147,15 @@ opcode_arg(enum addressing_mode mode)
|
|||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("arg: $%04X $%04X\n", arg, val);
|
||||||
|
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t
|
static uint16_t
|
||||||
opcode_mem(enum addressing_mode mode)
|
opcode_mem(enum addressing_mode mode)
|
||||||
{
|
{
|
||||||
uint8_t arg;
|
uint16_t arg, val;
|
||||||
uint16_t val;
|
|
||||||
|
|
||||||
if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y)
|
if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y)
|
||||||
arg = peek(regs.pc++);
|
arg = peek(regs.pc++);
|
||||||
@ -173,6 +172,7 @@ opcode_mem(enum addressing_mode mode)
|
|||||||
case AM_ZP_Y:
|
case AM_ZP_Y:
|
||||||
val = (arg + regs.y) % 256;
|
val = (arg + regs.y) % 256;
|
||||||
break;
|
break;
|
||||||
|
case AM_IMM:
|
||||||
case AM_ABS:
|
case AM_ABS:
|
||||||
val = arg;
|
val = arg;
|
||||||
break;
|
break;
|
||||||
@ -1059,7 +1059,7 @@ interpret(void)
|
|||||||
cycles += 2;
|
cycles += 2;
|
||||||
break;
|
break;
|
||||||
case 0x4c:
|
case 0x4c:
|
||||||
jmp(opcode_arg(AM_ABS));
|
jmp(opcode_mem(AM_ABS));
|
||||||
cycles += 3;
|
cycles += 3;
|
||||||
break;
|
break;
|
||||||
case 0x6c:
|
case 0x6c:
|
||||||
@ -1111,7 +1111,7 @@ interpret(void)
|
|||||||
cycles += 5;
|
cycles += 5;
|
||||||
break;
|
break;
|
||||||
case 0xa2:
|
case 0xa2:
|
||||||
ldx(opcode_arg(AM_IMM));
|
ldx(opcode_mem(AM_IMM));
|
||||||
cycles += 2;
|
cycles += 2;
|
||||||
break;
|
break;
|
||||||
case 0xa6:
|
case 0xa6:
|
||||||
@ -1467,8 +1467,8 @@ main(int argc, char *argv[])
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memwrite16(0xFFFC, 0x8000);
|
memwrite16(0xFFFC, 0xC000);
|
||||||
regs.pc = 0x8000;
|
regs.pc = 0xC000;
|
||||||
|
|
||||||
printf("status: %i%i%i%i%i%i%i%i\n", regs.status.carry,
|
printf("status: %i%i%i%i%i%i%i%i\n", regs.status.carry,
|
||||||
regs.status.zero, regs.status.interrupt_disable,
|
regs.status.zero, regs.status.interrupt_disable,
|
||||||
|
Loading…
Reference in New Issue
Block a user