add memory mirroring for system and ppu memory

This commit is contained in:
Vineet K 2024-06-11 15:38:58 +05:30
parent 44273e69b9
commit 745496ccd1

14
cpu.c
View File

@ -10,6 +10,12 @@
#define STATUS_UPDATE_NEGATIVE(r) \ #define STATUS_UPDATE_NEGATIVE(r) \
(regs.status.negative = ((r & (1 << 7)) > 0)) (regs.status.negative = ((r & (1 << 7)) > 0))
#define MEMORY_MIRROR(addr) \
if (addr < 0x2000) \
addr &= 0x07FF; \
else if (addr < 0x4000) \
addr &= 0x2007;
#define PUSH(b) \ #define PUSH(b) \
(memwrite(0x0100 + regs.sp--, b)) (memwrite(0x0100 + regs.sp--, b))
#define PULL() \ #define PULL() \
@ -58,12 +64,16 @@ uint32_t cycles = 0;
static uint8_t static uint8_t
peek(uint16_t addr) peek(uint16_t addr)
{ {
MEMORY_MIRROR(addr);
return memory[addr]; return memory[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 (uint16_t)memory[addr] | ((uint16_t)memory[addr + 1] << 8);
} }
@ -71,12 +81,16 @@ peek16(uint16_t addr)
static void static void
memwrite(uint16_t addr, uint8_t byte) memwrite(uint16_t addr, uint8_t byte)
{ {
MEMORY_MIRROR(addr);
memory[addr] = byte; memory[addr] = byte;
} }
static void static void
memwrite16(uint16_t addr, uint16_t word) memwrite16(uint16_t addr, uint16_t word)
{ {
MEMORY_MIRROR(addr);
/* bytes are stored in little-endian (low then high) */ /* bytes are stored in little-endian (low then high) */
memory[addr] = word & 0xFF; memory[addr] = word & 0xFF;
memory[addr + 1] = (word & 0xFF00) >> 8; memory[addr + 1] = (word & 0xFF00) >> 8;