-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMU.cpp
332 lines (255 loc) · 7.36 KB
/
MMU.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "MMU.h"
MMU::MMU() {
log = fopen("log.txt", "w");
}
MMU::~MMU() {
fclose(log);
log = NULL;
}
// The CPU can handle 2^16(PC is 16 bits), this is equal
// 64KB, however the internal memory has 2MB(2^21), to solve
// this problem, we use the first three bits of logical space
// to select the MPR/Memory Segment and with it, we do an OR
// with the logical space and it will give to us 21 bits
// Memory Segments
// Segment 0xFF -> IO RAM
// Segment 0xFC ~ 0xFE -> unused
// Segment 0xF8 ~ 0xFB -> WRAM (segment F9,FA and FB mirror segment FB)
// Segment 0x00 ~ 0xF7 -> HuCard ROM
// TODO: All Mirror
unsigned char MMU::readMemory(unsigned short addr) {
unsigned char MPR = mpr[((addr >> 13) & 0x7)];
char debug[20];
// Debug Purpose
//printf("MPR Num: 0x%X Addr: 0x%X\n", MPR, addr);
sprintf(debug, "MPR Num: 0x%X Addr: 0x%X\n", MPR, addr);
writeLog(debug);
for (int i = 0; i < 8; i++) {
sprintf(debug, "MPR %d Value: %X\n", i, mpr[i]);
writeLog(debug);
}
// Get Only 13 bits !
addr = addr & 0x1FFF;
unsigned int fAddr = 0;
// HuCard ROM
if ((MPR >= 0x00) && (MPR <= 0xF7)) {
fAddr = (addr | (MPR << 13));
sprintf(debug, "Addr: 0x%X, MPR: 0x%X, ROM Physical Value: 0x%X\n", addr, MPR, fAddr);
writeLog(debug);
return HuCardROM[fAddr];
}
// WRAM
if ((MPR >= 0xF8) && (MPR <= 0xFB)) {
return wram[(addr & 0x7FFF)];
}
// Unused
if ((MPR >= 0xFC) && (MPR <= 0xFE)) {
return 0xFF;
}
// Interrupt, timers, I/O ports
// VDC/VCE(HuC6270 and HuC6260)
if (MPR == 0xFF) {
// VDC (Video Display Controller)
// registers mirrored every 4 bytes
if ((addr >= 0x0000) && (addr <= 0x03FF)) {
VDC->readVDC(addr);
}
// VCE (Video Color Encoder)
// registers mirrored every 8 bytes
if ((addr >= 0x0400) && (addr <= 0x07FF)) {
// 0x0400 - Control Register
// 0x0402 - Color Table Address
// 0x0403 - Color Table Address
// 0x0404 - Color Table Data
// 0x0405 - Color Table Data
}
// PSG (Programmable Sound Generator)
if ((addr >= 0x0800) && (addr <= 0x0BFF)) {
}
// Timer
// registers mirrored every 2 bytes
if ((addr >= 0x0C00) && (addr <= 0x0FFF)) {
// 0x0C00 Timer Counter/Value
if (addr == 0x0C00)
return timerStart;
// 0x0C01 Time Enable
if (addr == 0x0C01)
return timerEnable;
}
// I/O port
// mirrored every 2 bytes
if ((addr >= 0x1000) && (addr <= 0x13FF)) {
// GamePAD I/O 0x1000
// bit 7, bit5 and bit 4 = unused
// bit 6 = country (1 = JPN, 0 = USA)
// bit 0 - 3 = gamepad Data
// test
return 0xBF;
}
// Interrupt Control Register
// registers mirrored every 4 bytes
if ((addr >= 0x1400) && (addr <= 0x17FF)) {
// 0x1402 - IRQ MASK
// bit 0 IRQ2D = enable/disable
// bit 1 IRQ1D = enable/disable
// bit 2 TIQD = enable/disable
// bit 3 ~ 7 unused
if (addr == 0x1402)
return interruptMask;
// 0x1403 - Interrupt status
if (addr == 0x1403)
std::cout << "Interrupt Status" << std::endl;
}
// 0x1800 ~ 0x1FFF always return FF
// however there is documentation talking about
// the region 0x1800 ~ 0x1BFF that is the CD-ROM Section
return 0xFF;
}
return 0xFF;
}
unsigned char MMU::readMPRi(unsigned char n) { return (mpr[n]); }
unsigned char MMU::getTimerStart() { return timerStart; }
unsigned char MMU::readStack(unsigned short addr) { return wram[addr]; }
unsigned char* MMU::getVRAM() { return (vram); }
void MMU::writeMemory(unsigned short addr, unsigned char data) {
unsigned char MPR = mpr[((addr >> 13) & 0x7)];
char debug[20];
// Get Only 13 bits !
addr = addr & 0x1FFF;
//printf("Addr: 0x%X - Data: 0x%X\n", addr, data);
// HuCard ROM
if ((MPR >= 0x00) && (MPR <= 0xF7)) {
// Only for debug purpose
writeLog("Trying write on HuCardROM !\n");
sprintf(debug, "Addr: 0x%X - Data: 0x%X\n", addr, data);
writeLog(debug);
exit(0);
}
// WRAM
if ((MPR >= 0xF8) && (MPR <= 0xFB)) {
wram[(addr & 0x7FFF)] = data;
}
// Unused
if ((MPR >= 0xFC) && (MPR <= 0xFE)) {
printf("Write on unused, data: 0x%X\n", data);
}
// Hardware Page
// Interrupt, timers, I/O etc...
if (MPR == 0xFF) {
writeIO((addr & 0x1FFF), data);
}
}
void MMU::writeVRAM(unsigned short addr, unsigned short data) {
*(unsigned short*)&vram[addr] = data;
}
// There are instroctuions (STO, ST1 and ST2) which
// written direct to the registers of HuC6270
// besides this we have the writeMemory
void MMU::writeIO(unsigned short addr, unsigned char data) {
char debug[20];
sprintf(debug,"Write I/O Addr: 0x%X | Data: 0x%X\n", addr, data);
writeLog(debug);
// VDC (Video Display Controller)
// registers mirrored every 4 bytes
if ((addr >= 0x0000) && (addr <= 0x03FF)) {
// 0xE000 -> VDC Address Select
// Only used the bits 0 ~ 4, bits 5 ~ 7 are ignored
// 0xE002 -> Low Data register
// 0xE003 -> High Data register
VDC->writeVDC((addr & 3), data);
return;
}
// VCE (Video Color Encoder)
// registers mirrored every 8 bytes
if ((addr >= 0x0400) && (addr <= 0x07FF)) {
// 0x0400 - Control Register
// 0x0402 - Color Table Address
// 0x0403 - Color Table Address
// 0x0404 - Color Table Data
// 0x0405 - Color Table Data
}
// PSG (Programmable Sound Generator)
if ((addr >= 0x0800) && (addr <= 0x0BFF)) {
}
// Timer
// registers mirrored every 2 bytes
if ((addr >= 0x0C00) && (addr <= 0x0FFF)) {
// 0x0C00 Timer Counter/Value
if (addr == 0x0C00)
timerStart = (data & 0x7F);
// 0x0C01 Time Enable
if (addr == 0x0C01)
timerEnable = (addr & 0x1);
}
// I/O port
// mirrored every 2 bytes
if ((addr >= 0x1000) && (addr <= 0x13FF)) {
// 0x1000 Joypad
if (addr == 0x1000)
std::cout << "joypad" << std::endl;
}
// Interrupt Control Register
// registers mirrored every 4 bytes
if ((addr >= 0x1400) && (addr <= 0x17FF)) {
// 0x1402 - IRQ MASK
// bit 0 IRQ2 = enable/disable
// bit 1 IRQ1 = enable/disable
// bit 2 timer = enable/disable
// bit 3 ~ 7 unused
if (addr == 0x1402) {
interruptMask = data;
// debug purpose
printf("TIMER: %d\n", (data & 0x4));
printf("IRQ1: %d\n", (data & 0x2));
printf("IRQ2: %d\n", (data & 0x1));
}
// Timer Interrupt
if (addr == 0x1403) {
CPU->handleInterrupt(INTERRUPT_TIME);
}
}
}
void MMU::setMPRi(unsigned char n, unsigned char data) {
mpr[n] = data;
}
void MMU::setupVDC(HuC6270 *vdc) {
VDC = vdc;
}
void MMU::setupCPU(HuC6280 *cpu) {
CPU = cpu;
}
void MMU::setupRender(render *r) {
mRender = r;
}
// It will clear all memory segments
// and will clear the memory mapping registers
// normally the only MPR7 is set to 0 and the
// other has random numbers, in this case
// we will use 0 to all.
void MMU::clearMPR() {
std::memset(&mpr, 0x0, 0x7);
std::memset(&wram, 0x0, 0x7FFF);
std::memset(&vram, 0x0, 0x10000);
interruptMask = 0;
}
void MMU::writeStack(unsigned short addr, unsigned char data) {
wram[addr] = data;
}
void MMU::writeLog(std::string text) {
//fprintf(log, text.c_str());
mRender->drawText(text, 100, 100);
mRender->renderScene();
mRender->handleKeyboard();
}
bool MMU::isTimerEnable() { return timerEnable; }
bool MMU::startMemory() {
std::string gameName;
std::cout << "File name " << std::endl;
std::cin >> gameName;
if (pceLoader.PCE_LoadFile(gameName)) {
std::memcpy(HuCardROM, pceLoader.buffer, pceLoader.size);
return true;
} else
return false;
}
unsigned short MMU::readVRAM(unsigned short addr) { return (*(unsigned short*)&vram[addr]); }