-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.c
178 lines (143 loc) · 4.29 KB
/
mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "mem.h"
#include "ppu.h"
#include "apu.h"
#include "panic.h"
void mem_init(mem_t *mem)
{
int i;
/* Initialize all to 0xFF since 0x00 is the opcode for BRK. */
for (i = 0; i < MEM_SIZE_RAM; i++) {
mem->ram[i] = 0xff;
}
for (i = 0; i < MEM_SIZE_CART; i++) {
mem->cart[i] = 0xff;
}
mem->ppu_read = NULL;
mem->ppu_write = NULL;
mem->apu_read = NULL;
mem->apu_write = NULL;
mem->fds_read = NULL;
mem->fds_write = NULL;
mem->ppu = NULL;
mem->apu = NULL;
mem->fds = NULL;
}
uint8_t mem_read(mem_t *mem, uint16_t address)
{
if (address <= 0x07FF) {
return mem->ram[address];
} else if (address <= 0x0FFF) {
if (mem->fds_read != NULL && mem->fds != NULL) {
return (mem->fds_read)(mem->fds, address);
} else {
return mem->ram[address - 0x800]; /* Mirror */
}
} else if (address <= 0x17FF) {
if (mem->fds_read != NULL && mem->fds != NULL) {
return (mem->fds_read)(mem->fds, address);
} else {
return mem->ram[address - 0x1000]; /* Mirror */
}
} else if (address <= 0x1FFF) {
if (mem->fds_read != NULL && mem->fds != NULL) {
return (mem->fds_read)(mem->fds, address);
} else {
return mem->ram[address - 0x1800]; /* Mirror */
}
} else if (address <= 0x3FFF) {
if (mem->ppu_read != NULL && mem->ppu != NULL) {
return (mem->ppu_read)(mem->ppu, (address % 0x8) + 0x2000);
} else {
panic("PPU read hook not installed! Address: 0x%04x\n", address);
}
} else if (address <= 0x401F) {
if (mem->apu_read != NULL && mem->apu != NULL) {
return (mem->apu_read)(mem->apu, address);
} else {
panic("APU read hook not installed! Address: 0x%04x\n", address);
}
} else if (address <= 0x403F && mem->fds_read != NULL && mem->fds != NULL) {
return (mem->fds_read)(mem->fds, address);
}
return mem->cart[address - 0x4020];
}
void mem_write(mem_t *mem, uint16_t address, uint8_t value)
{
if (address <= 0x07FF) {
mem->ram[address] = value;
} else if (address <= 0x0FFF) {
if (mem->fds_write != NULL && mem->fds != NULL) {
(mem->fds_write)(mem->fds, address, value);
} else {
mem->ram[address - 0x800] = value; /* Mirror */
}
} else if (address <= 0x17FF) {
if (mem->fds_write != NULL && mem->fds != NULL) {
(mem->fds_write)(mem->fds, address, value);
} else {
mem->ram[address - 0x1000] = value; /* Mirror */
}
} else if (address <= 0x1FFF) {
if (mem->fds_write != NULL && mem->fds != NULL) {
(mem->fds_write)(mem->fds, address, value);
} else {
mem->ram[address - 0x1800] = value; /* Mirror */
}
} else if (address <= 0x3FFF) {
if (mem->ppu_write != NULL && mem->ppu != NULL) {
(mem->ppu_write)(mem->ppu, (address % 0x8) + 0x2000, value);
} else {
panic("PPU write hook not installed! Address: 0x%04x\n", address);
}
} else if (address == APU_OAM_DMA) {
/* Special sprite data DMA transfer. */
if (mem->ppu != NULL) {
for (int i = 0; i < PPU_SIZE_SPRITE_RAM; i++) {
((ppu_t *)mem->ppu)->sprite_ram[i] = mem_read(mem, (value * 256) + i);
}
} else {
panic("PPU reference not installed!\n");
}
} else if (address <= 0x401F) {
if (mem->apu_write != NULL && mem->apu != NULL) {
(mem->apu_write)(mem->apu, address, value);
} else {
panic("APU write hook not installed! Address: 0x%04x\n", address);
}
} else if (address <= 0x403F && mem->fds_write != NULL && mem->fds != NULL) {
(mem->fds_write)(mem->fds, address, value);
} else {
mem->cart[address - 0x4020] = value;
}
}
void mem_dump(FILE *fh, mem_t *mem, uint16_t start, uint16_t end)
{
int i;
if (start <= 0x07FF && end <= 0x07FF) {
for (i = start; i <= end; i++) {
if (i % 16 == 0) {
fprintf(fh, "$%04x ", i);
}
fprintf(fh, "%02x ", mem->ram[i]);
if (i % 16 == 15) {
fprintf(fh, "\n");
}
}
} else if (start >= 0x4020 && end >= 0x4020) {
for (i = start; i <= end; i++) {
if (i % 16 == 0) {
fprintf(fh, "$%04x ", i);
}
fprintf(fh, "%02x ", mem->cart[i - 0x4020]);
if (i % 16 == 15) {
fprintf(fh, "\n");
}
}
} else {
fprintf(fh, "???\n");
}
}