summaryrefslogtreecommitdiff
path: root/cpu.c
diff options
context:
space:
mode:
Diffstat (limited to 'cpu.c')
-rw-r--r--cpu.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/cpu.c b/cpu.c
index 8eaeff1..0fe6cbd 100644
--- a/cpu.c
+++ b/cpu.c
@@ -10,6 +10,12 @@
10#define STATUS_UPDATE_NEGATIVE(r) \ 10#define STATUS_UPDATE_NEGATIVE(r) \
11 (regs.status.negative = ((r & (1 << 7)) > 0)) 11 (regs.status.negative = ((r & (1 << 7)) > 0))
12 12
13#define MEMORY_MIRROR(addr) \
14 if (addr < 0x2000) \
15 addr &= 0x07FF; \
16 else if (addr < 0x4000) \
17 addr &= 0x2007;
18
13#define PUSH(b) \ 19#define PUSH(b) \
14 (memwrite(0x0100 + regs.sp--, b)) 20 (memwrite(0x0100 + regs.sp--, b))
15#define PULL() \ 21#define PULL() \
@@ -58,12 +64,16 @@ uint32_t cycles = 0;
58static uint8_t 64static uint8_t
59peek(uint16_t addr) 65peek(uint16_t addr)
60{ 66{
67 MEMORY_MIRROR(addr);
68
61 return memory[addr]; 69 return memory[addr];
62} 70}
63 71
64static uint16_t 72static uint16_t
65peek16(uint16_t addr) 73peek16(uint16_t addr)
66{ 74{
75 MEMORY_MIRROR(addr);
76
67 /* bytes are stored in little-endian (low then high) */ 77 /* bytes are stored in little-endian (low then high) */
68 return (uint16_t)memory[addr] | ((uint16_t)memory[addr + 1] << 8); 78 return (uint16_t)memory[addr] | ((uint16_t)memory[addr + 1] << 8);
69} 79}
@@ -71,12 +81,16 @@ peek16(uint16_t addr)
71static void 81static void
72memwrite(uint16_t addr, uint8_t byte) 82memwrite(uint16_t addr, uint8_t byte)
73{ 83{
84 MEMORY_MIRROR(addr);
85
74 memory[addr] = byte; 86 memory[addr] = byte;
75} 87}
76 88
77static void 89static void
78memwrite16(uint16_t addr, uint16_t word) 90memwrite16(uint16_t addr, uint16_t word)
79{ 91{
92 MEMORY_MIRROR(addr);
93
80 /* bytes are stored in little-endian (low then high) */ 94 /* bytes are stored in little-endian (low then high) */
81 memory[addr] = word & 0xFF; 95 memory[addr] = word & 0xFF;
82 memory[addr + 1] = (word & 0xFF00) >> 8; 96 memory[addr + 1] = (word & 0xFF00) >> 8;