From bd85b80ba7a7919a80ce6fd742b37d3e183f2e3a Mon Sep 17 00:00:00 2001 From: vin Date: Tue, 4 Jun 2024 17:48:34 +0530 Subject: [PATCH] fix peek --- cpu.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/cpu.c b/cpu.c index a8c6ceb..f018a44 100644 --- a/cpu.c +++ b/cpu.c @@ -47,12 +47,12 @@ uint8_t memory[0x16000]; uint8_t program[] = { 0xa9, 0xc0, 0xaa, 0xe8, 0x00 }; uint8_t -peek(uint16_t addr) +peek(uint8_t addr) { return memory[addr]; } -uint16_t +uint8_t peek16(uint16_t addr) { /* bytes are stored in little-endian (low then high) */ @@ -93,7 +93,10 @@ lda(enum addressing_mode mode) { 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) { case AM_IMM: /* $A9 */ @@ -103,16 +106,16 @@ lda(enum addressing_mode mode) val = peek(arg % 256); break; case AM_ABS: /* $AD */ - val = peek(arg); + val = peek16(arg); break; case AM_ZP_X: /* $B5 */ val = peek((arg + regs.x) % 256); break; case AM_ABS_X: /* $BD */ - val = peek(arg + regs.x); + val = peek16(arg + regs.x); break; case AM_ABS_Y: /* $B9 */ - val = peek(arg + regs.y); + val = peek16(arg + regs.y); break; case AM_IND_X: /* $A1 */ 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); break; default: - regs.pc--; + fprintf(stderr, "INVALID LDA ADDRESSING MODE\n"); + abort(); return; }