summaryrefslogtreecommitdiff
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
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.
-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)
81static uint16_t 81static uint16_t
82peek16(uint16_t addr) 82peek16(uint16_t addr)
83{ 83{
84 MEMORY_MIRROR(addr);
85
86 /* bytes are stored in little-endian (low then high) */ 84 /* bytes are stored in little-endian (low then high) */
87 return (uint16_t)memory[addr] | ((uint16_t)memory[addr + 1] << 8); 85 return peek(addr) | (peek(addr + 1) << 8);
88} 86}
89 87
90static void 88static void
@@ -108,7 +106,7 @@ memwrite16(uint16_t addr, uint16_t word)
108static uint8_t 106static uint8_t
109opcode_arg(enum addressing_mode mode) 107opcode_arg(enum addressing_mode mode)
110{ 108{
111 uint8_t arg, val; 109 uint16_t arg, val;
112 110
113 if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y) 111 if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y)
114 arg = peek(regs.pc++); 112 arg = peek(regs.pc++);
@@ -149,14 +147,15 @@ opcode_arg(enum addressing_mode mode)
149 abort(); 147 abort();
150 } 148 }
151 149
150 printf("arg: $%04X $%04X\n", arg, val);
151
152 return val; 152 return val;
153} 153}
154 154
155static uint16_t 155static uint16_t
156opcode_mem(enum addressing_mode mode) 156opcode_mem(enum addressing_mode mode)
157{ 157{
158 uint8_t arg; 158 uint16_t arg, val;
159 uint16_t val;
160 159
161 if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y) 160 if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y)
162 arg = peek(regs.pc++); 161 arg = peek(regs.pc++);
@@ -173,6 +172,7 @@ opcode_mem(enum addressing_mode mode)
173 case AM_ZP_Y: 172 case AM_ZP_Y:
174 val = (arg + regs.y) % 256; 173 val = (arg + regs.y) % 256;
175 break; 174 break;
175 case AM_IMM:
176 case AM_ABS: 176 case AM_ABS:
177 val = arg; 177 val = arg;
178 break; 178 break;
@@ -1059,7 +1059,7 @@ interpret(void)
1059 cycles += 2; 1059 cycles += 2;
1060 break; 1060 break;
1061 case 0x4c: 1061 case 0x4c:
1062 jmp(opcode_arg(AM_ABS)); 1062 jmp(opcode_mem(AM_ABS));
1063 cycles += 3; 1063 cycles += 3;
1064 break; 1064 break;
1065 case 0x6c: 1065 case 0x6c:
@@ -1111,7 +1111,7 @@ interpret(void)
1111 cycles += 5; 1111 cycles += 5;
1112 break; 1112 break;
1113 case 0xa2: 1113 case 0xa2:
1114 ldx(opcode_arg(AM_IMM)); 1114 ldx(opcode_mem(AM_IMM));
1115 cycles += 2; 1115 cycles += 2;
1116 break; 1116 break;
1117 case 0xa6: 1117 case 0xa6:
@@ -1467,8 +1467,8 @@ main(int argc, char *argv[])
1467 return 1; 1467 return 1;
1468 } 1468 }
1469 1469
1470 memwrite16(0xFFFC, 0x8000); 1470 memwrite16(0xFFFC, 0xC000);
1471 regs.pc = 0x8000; 1471 regs.pc = 0xC000;
1472 1472
1473 printf("status: %i%i%i%i%i%i%i%i\n", regs.status.carry, 1473 printf("status: %i%i%i%i%i%i%i%i\n", regs.status.carry,
1474 regs.status.zero, regs.status.interrupt_disable, 1474 regs.status.zero, regs.status.interrupt_disable,