-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
376 lines (331 loc) · 8.27 KB
/
Copy pathmemory.cpp
File metadata and controls
376 lines (331 loc) · 8.27 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include "memory.h"
Memory::Memory()
{
WRAM = vector<Byte>(0x2000); // $C000 - $DFFF, 8kB Working RAM
ZRAM = vector<Byte>(0x0100); // $FF00 - $FFFF, 256 bytes of RAM
VRAM = vector<Byte>(0x2000); // $8000 - $9FFF, 8kB Video RAM
OAM = vector<Byte>(0x0100); // $FE00 - $FEFF, OAM Sprite RAM, IO RAM
// Initialize Memory Register objects for easy reference
P1 = MemoryRegister(&ZRAM[0x00]);
DIV = MemoryRegister(&ZRAM[0x04]);
TIMA = MemoryRegister(&ZRAM[0x05]);
TMA = MemoryRegister(&ZRAM[0x06]);
TAC = MemoryRegister(&ZRAM[0x07]);
LCDC = MemoryRegister(&ZRAM[0x40]);
STAT = MemoryRegister(&ZRAM[0x41]);
SCY = MemoryRegister(&ZRAM[0x42]);
SCX = MemoryRegister(&ZRAM[0x43]);
LY = MemoryRegister(&ZRAM[0x44]);
LYC = MemoryRegister(&ZRAM[0x45]);
DMA = MemoryRegister(&ZRAM[0x46]);
BGP = MemoryRegister(&ZRAM[0x47]);
OBP0 = MemoryRegister(&ZRAM[0x48]);
OBP1 = MemoryRegister(&ZRAM[0x49]);
WY = MemoryRegister(&ZRAM[0x4A]);
WX = MemoryRegister(&ZRAM[0x4B]);
IF = MemoryRegister(&ZRAM[0x0F]);
IE = MemoryRegister(&ZRAM[0xFF]);
reset();
}
void Memory::reset()
{
fill(WRAM.begin(), WRAM.end(), 0);
fill(ZRAM.begin(), ZRAM.end(), 0);
fill(VRAM.begin(), VRAM.end(), 0);
fill(OAM.begin(), OAM.end(), 0);
// The following memory locations are set to the following values after gameboy BIOS runs
P1.set(0x00);
DIV.set(0x00);
TIMA.set(0x00);
TMA.set(0x00);
TAC.set(0x00);
LCDC.set(0x91);
SCY.set(0x00);
SCX.set(0x00);
LYC.set(0x00);
BGP.set(0xFC);
OBP0.set(0xFF);
OBP1.set(0xFF);
WY.set(0x00);
WX.set(0x00);
IF.set(0x00);
IE.set(0x00);
// Initialize input to HIGH state (unpressed)
joypad_buttons = 0xF;
joypad_arrows = 0xF;
}
void Memory::load_rom(std::string location)
{
ifstream input(location, ios::binary);
vector<Byte> buffer((istreambuf_iterator<char>(input)), (istreambuf_iterator<char>()));
// print cartrige data
string title = "";
for (int i = 0x0134; i <= 0x142; i++)
{
Byte character = buffer[i];
if (character == 0)
break;
else
title.push_back(tolower(character));
}
rom_name = title;
cout << "Title: " << title << endl;
Byte gb_type = buffer[0x0143];
cout << "Gameboy Type: " << ((gb_type == 0x80) ? "GB Color" : "GB") << endl;
Byte functions = buffer[0x0146];
cout << "Use " << ((functions == 0x3) ? "Super " : "") << "Gameboy functions" << endl;
string cart_types[0x100];
cart_types[0x0] = "ROM ONLY";
cart_types[0x1] = "ROM+MBC1";
cart_types[0x2] = "ROM+MBC1+RAM";
cart_types[0x3] = "ROM+MBC1+RAM+BATT";
cart_types[0x5] = "ROM+MBC2";
cart_types[0x6] = "ROM+MBC2+BATTERY";
cart_types[0x8] = "ROM+RAM";
cart_types[0x9] = "ROM+RAM+BATTERY";
cart_types[0xB] = "ROM+MMM01";
cart_types[0xC] = "ROM+MMM01+SRAM";
cart_types[0xD] = "ROM+MMM01+SRAM+BATT";
cart_types[0xF] = "ROM+MBC3+TIMER+BATT";
cart_types[0x10] = "ROM+MBC3+TIMER+RAM+BATT";
cart_types[0x11] = "ROM+MBC3";
cart_types[0x12] = "ROM+MBC3+RAM";
cart_types[0x13] = "ROM+MBC3+RAM+BATT";
cart_types[0x19] = "ROM+MBC5";
cart_types[0x1A] = "ROM+MBC5+RAM";
cart_types[0x1B] = "ROM+MBC5+RAM+BATT";
cart_types[0x1C] = "ROM+MBC5+RUMBLE";
cart_types[0x1D] = "ROM+MBC5+RUMBLE+SRAM";
cart_types[0x1E] = "ROM+MBC5+RUMBLE+SRAM+BATT";
cart_types[0x1F] = "Pocket Camera";
cart_types[0xFD] = "Bandai TAMA5";
cart_types[0xFE] = "Hudson HuC-3";
cart_types[0xFF] = "Hudson HuC-1";
Byte cart = buffer[0x0147];
cout << "Cartridge Type: " << cart_types[cart] << endl;
delete controller;
// Assign memory controller based on cartridge specification
switch (cart)
{
case 0x01:
case 0x02:
case 0x03:
controller = new MemoryController1();
break;
case 0x05:
case 0x06:
cout << "CONTROLLER NOT IMPLEMENTED" << endl;
controller = new MemoryController2();
break;
case 0x0F:
case 0x10:
case 0x11:
case 0x12:
case 0x13:
controller = new MemoryController3();
break;
default:
controller = new MemoryController0();
break;
}
// Initialize controller with cartridge data
controller->init(buffer);
Byte rsize = buffer[0x0148];
cout << "ROM Size: " << (32 << rsize) << "kB " << pow(2, rsize + 1) << " banks" << endl;
int size, banks;
switch (buffer[0x149])
{
case 1: size = 2; banks = 1;
case 2: size = 8; banks = 1;
case 3: size = 32; banks = 4;
case 4: size = 128; banks = 16;
default: size = 0; banks = 0;
}
cout << "RAM Size: " << size << "kB " << banks << " banks" << endl;
cout << "Destination Code: " << (buffer[0x014A] == 1 ? "Non-" : "") << "Japanese" << endl;
}
void Memory::save_state(ofstream &file)
{
write_vector(file, VRAM);
write_vector(file, OAM);
write_vector(file, WRAM);
write_vector(file, ZRAM);
// save ERAM
vector<Byte> eram = controller->get_ram();
write_vector(file, eram);
controller->save_state(file);
}
void Memory::load_state(ifstream &file)
{
load_vector(file, VRAM);
load_vector(file, OAM);
load_vector(file, WRAM);
load_vector(file, ZRAM);
// Load ERAM
vector<Byte> eram(0x8000);
load_vector(file, eram);
controller->set_ram(eram);
controller->load_state(file);
}
void Memory::write_vector(ofstream &file, vector<Byte> &vec)
{
file.write((char*)&vec[0], vec.size());
}
void Memory::load_vector(ifstream &file, vector<Byte> &vec)
{
file.read((char*)&vec[0], vec.size());
}
void Memory::do_dma_transfer()
{
Byte_2 address = DMA.get() << 8; // multiply by 100
for (int i = 0; i < 0xA0; i++)
{
write((0xFE00 + i), read(address + i));
}
}
Byte Memory::get_joypad_state()
{
Byte request = P1.get();
switch (request)
{
case 0x10:
return joypad_buttons;
case 0x20:
return joypad_arrows;
default:
return 0xFF;
}
}
Byte Memory::read(Address location)
{
switch (location & 0xF000)
{
// ROM
case 0x0000:
case 0x1000:
case 0x2000:
case 0x3000:
case 0x4000:
case 0x5000:
case 0x6000:
case 0x7000:
return controller->read(location);
// Graphics VRAM
case 0x8000:
case 0x9000:
return VRAM[location & 0x1FFF];
// External RAM
case 0xA000:
case 0xB000:
return controller->read(location);
// Working RAM (8kB) and RAM Shadow
case 0xC000:
case 0xD000:
case 0xE000:
return WRAM[location & 0x1FFF];
// Remaining Working RAM Shadow, I/O, Zero page RAM
case 0xF000:
switch (location & 0x0F00)
{
// Remaining Working RAM
case 0x000: case 0x100: case 0x200: case 0x300:
case 0x400: case 0x500: case 0x600: case 0x700:
case 0x800: case 0x900: case 0xA00: case 0xB00:
case 0xC00: case 0xD00:
return WRAM[location & 0x1FFF];
// Sprite OAM
case 0xE00:
return OAM[location & 0xFF];
case 0xF00:
if (location == 0xFF00)
return get_joypad_state();
else
return ZRAM[location & 0xFF];
}
default:
return 0xFF;
}
}
void Memory::write(Address location, Byte data)
{
switch (location & 0xF000)
{
// ROM
case 0x0000:
case 0x1000:
case 0x2000:
case 0x3000:
case 0x4000:
case 0x5000:
case 0x6000:
case 0x7000:
controller->write(location, data);
break;
// Graphics VRAM
case 0x8000:
case 0x9000:
// Cannot write to VRAM during mode 3
VRAM[location & 0x1FFF] = data;
break;
// External RAM
case 0xA000:
case 0xB000:
controller->write(location, data);
break;
// Working RAM (8kB) and RAM Shadow
case 0xC000:
case 0xD000:
case 0xE000:
WRAM[location & 0x1FFF] = data;
break;
// Remaining Working RAM Shadow, I/O, Zero page RAM
case 0xF000:
switch (location & 0x0F00)
{
// Remaining Working RAM
case 0x000: case 0x100: case 0x200: case 0x300:
case 0x400: case 0x500: case 0x600: case 0x700:
case 0x800: case 0x900: case 0xA00: case 0xB00:
case 0xC00: case 0xD00:
WRAM[location & 0x1FFF] = data;
break;
// Sprite OAM
case 0xE00:
OAM[location & 0xFF] = data;
break;
case 0xF00:
write_zero_page(location, data);
break;
}
}
}
void Memory::write_zero_page(Address location, Byte data)
{
switch (location)
{
// Joypad Register - only bits 4 & 5 can be written to
case 0xFF00:
ZRAM[0x00] = (data & 0x30);
break;
// Divider Register - Write as zero no matter content
case 0xFF04:
ZRAM[0x04] = 0;
break;
// TODO: STAT - writing to match flag resets flag but doesn't change mode
case 0xFF41:
ZRAM[0x41] = (data & 0xFC) | (STAT.get() & 0x03);
break;
// LY Register - Game cannot write to this register directly
case 0xFF44:
ZRAM[0x44] = 0;
break;
// DMA transfer request
case 0xFF46:
ZRAM[0x46] = data;
do_dma_transfer();
break;
default:
ZRAM[location & 0xFF] = data;
break;
}
}