This commit is contained in:
Vineet K 2024-06-04 17:48:34 +05:30
parent 36a8eefd00
commit bd85b80ba7

18
cpu.c
View File

@ -47,12 +47,12 @@ uint8_t memory[0x16000];
uint8_t program[] = { 0xa9, 0xc0, 0xaa, 0xe8, 0x00 }; uint8_t program[] = { 0xa9, 0xc0, 0xaa, 0xe8, 0x00 };
uint8_t uint8_t
peek(uint16_t addr) peek(uint8_t addr)
{ {
return memory[addr]; return memory[addr];
} }
uint16_t uint8_t
peek16(uint16_t addr) peek16(uint16_t addr)
{ {
/* bytes are stored in little-endian (low then high) */ /* bytes are stored in little-endian (low then high) */
@ -93,7 +93,10 @@ lda(enum addressing_mode mode)
{ {
uint8_t arg, val; uint8_t arg, val;
arg = peek(regs.pc++); if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y)
arg = peek(regs.pc++);
else
arg = peek16(regs.pc++);
switch (mode) { switch (mode) {
case AM_IMM: /* $A9 */ case AM_IMM: /* $A9 */
@ -103,16 +106,16 @@ lda(enum addressing_mode mode)
val = peek(arg % 256); val = peek(arg % 256);
break; break;
case AM_ABS: /* $AD */ case AM_ABS: /* $AD */
val = peek(arg); val = peek16(arg);
break; break;
case AM_ZP_X: /* $B5 */ case AM_ZP_X: /* $B5 */
val = peek((arg + regs.x) % 256); val = peek((arg + regs.x) % 256);
break; break;
case AM_ABS_X: /* $BD */ case AM_ABS_X: /* $BD */
val = peek(arg + regs.x); val = peek16(arg + regs.x);
break; break;
case AM_ABS_Y: /* $B9 */ case AM_ABS_Y: /* $B9 */
val = peek(arg + regs.y); val = peek16(arg + regs.y);
break; break;
case AM_IND_X: /* $A1 */ case AM_IND_X: /* $A1 */
val = peek(peek((arg + regs.x) % 256) + peek((arg + regs.x + 1) % 256) * 256); val = peek(peek((arg + regs.x) % 256) + peek((arg + regs.x + 1) % 256) * 256);
@ -121,7 +124,8 @@ lda(enum addressing_mode mode)
val = peek(peek(arg) + peek((arg + 1) % 256) * 256 + regs.y); val = peek(peek(arg) + peek((arg + 1) % 256) * 256 + regs.y);
break; break;
default: default:
regs.pc--; fprintf(stderr, "INVALID LDA ADDRESSING MODE\n");
abort();
return; return;
} }