-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.cs
More file actions
395 lines (353 loc) · 13.9 KB
/
Memory.cs
File metadata and controls
395 lines (353 loc) · 13.9 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using System;
using System.Drawing;
// References
// - https://github.com/CTurt/Cinoop mostly code from memory.c like IO and some registers
// - http://marc.rawer.de/Gameboy/Docs/GBCPUman.pdf probably the best GameBoy CPU/memory manual
// - https://knight.sc/reverse%20engineering/2018/11/19/game-boy-boot-sequence.html on Boot sequence
// - https://gist.github.com/drhelius/6063288 on Boot sequence
// - https://retrocomputing.stackexchange.com/questions/11732/how-does-the-gameboys-memory-bank-switching-work on MBC1 implementation
namespace ZarthGB
{
class Memory
{
private Sound sound;
private byte[] memory;
private byte[] cartridge;
public int Ticks { get; set; }
public bool TimerEnabled { get; private set; }
public long TimerTick { get; private set; }
#region Keyboard
public bool KeyUp { get; set; }
public bool KeyDown { get; set; }
public bool KeyLeft { get; set; }
public bool KeyRight { get; set; }
public bool KeyA { get; set; }
public bool KeyB { get; set; }
public bool KeyStart { get; set; }
public bool KeySelect { get; set; }
private byte Keys1 => (byte)((KeyA ? 0 : 1 << 0) |
(KeyB ? 0 : 1 << 1) |
(KeySelect ? 0 : 1 << 2) |
(KeyStart ? 0 : 1 << 3));
private byte Keys2 => (byte)((KeyRight ? 0 : 1 << 0) |
(KeyLeft ? 0 : 1 << 1) |
(KeyUp ? 0 : 1 << 2) |
(KeyDown ? 0 : 1 << 3));
#endregion
#region Video
public byte[,,] Tiles;
public Color[] Palette { get; } = new Color[4]
{
Color.White,
Color.LightGray,
Color.DarkGray,
Color.Black,
};
public Color[] BackgroundPalette { get; } = new Color[4]
{
Color.White,
Color.LightGray,
Color.DarkGray,
Color.Black,
};
public Color[,] SpritePalette { get; } = new Color[2,4]
{
{
Color.White,
Color.LightGray,
Color.DarkGray,
Color.Black,
},
{
Color.White,
Color.LightGray,
Color.DarkGray,
Color.Black,
}
};
#endregion
#region Initial state
private byte[] dmgBoot =
{
0x31, 0xFE, 0xFF, 0xAF, 0x21, 0xFF, 0x9F, 0x32, 0xCB, 0x7C, 0x20, 0xFB,
0x21, 0x26, 0xFF, 0x0E, 0x11, 0x3E, 0x80, 0x32, 0xE2, 0x0C, 0x3E, 0xF3,
0xE2, 0x32, 0x3E, 0x77, 0x77, 0x3E, 0xFC, 0xE0, 0x47, 0x11, 0x04, 0x01,
0x21, 0x10, 0x80, 0x1A, 0xCD, 0x95, 0x00, 0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, 0x6E, 0xE6, 0xDD, 0xDD, 0xD9, 0x99,
0xBB, 0xBB, 0x67, 0x63, 0x6E, 0x0E, 0xEC, 0xCC, 0xDD, 0xDC, 0x99, 0x9F, 0xBB, 0xB9, 0x33, 0x3E,
0x45, 0xEC, 0x52, 0xFA, 0x08, 0xB7, 0x07, 0x5D, 0x01, 0xFD, 0xC0, 0xFF, 0x08, 0xFC, 0x00, 0xE5,
0x0B, 0xF8, 0xC2, 0xCE, 0xF4, 0xF9, 0x0F, 0x7F, 0x45, 0x6D, 0x3D, 0xFE, 0x46, 0x97, 0x33, 0x5E,
0x08, 0xEF, 0xF1, 0xFF, 0x86, 0x83, 0x24, 0x74, 0x12, 0xFC, 0x00, 0x9F, 0xB4, 0xB7, 0x06, 0xD5,
0xD0, 0x7A, 0x00, 0x9E, 0x04, 0x5F, 0x41, 0x2F, 0x1D, 0x77, 0x36, 0x75, 0x81, 0xAA, 0x70, 0x3A,
0x98, 0xD1, 0x71, 0x02, 0x4D, 0x01, 0xC1, 0xFF, 0x0D, 0x00, 0xD3, 0x05, 0xF9, 0x00, 0x0B, 0x00
};
#endregion
#region MBC1
private bool Mbc1Enabled;
private bool RamEnabled;
private int BankSelected;
private bool Mbc1ModeRam;
private int RamBankSelected;
private byte[] Ram = new byte[32 * 1024];
#endregion
public Memory()
{
Reset();
sound = new Sound(this);
}
public void IncrementDiv()
{
memory[0xff04]++;
// Modulo 16 (1024 times per second or less to keep sound channels synchronized)
//const int soundStep = (16384 / Sound.PlayStep);
const int soundStep = 256;
if (memory[0xff04] % soundStep == 0)
SoundStep();
}
public void SoundStep()
{
sound.Play();
}
public void Reset()
{
Tiles = new byte[384,8,8];
Ticks = 0;
memory = new byte[0x10000];
for (int i = 0; i < dmgBoot.Length; i++)
memory[i] = dmgBoot[i];
for (int i = 0; i < ioReset.Length; i++)
memory[0xff00 + i] = ioReset[i];
#region Init FF registers
memory[0xFF05] = 0x00;
memory[0xFF06] = 0x00;
memory[0xFF07] = 0x00;
memory[0xFF10] = 0x80;
memory[0xFF11] = 0xBF;
memory[0xFF12] = 0xF3;
memory[0xFF14] = 0xBF;
memory[0xFF16] = 0x3F;
memory[0xFF17] = 0x00;
memory[0xFF19] = 0xBF;
memory[0xFF1A] = 0x7F;
memory[0xFF1B] = 0xFF;
memory[0xFF1C] = 0x9F;
memory[0xFF1E] = 0xBF;
memory[0xFF20] = 0xFF;
memory[0xFF21] = 0x00;
memory[0xFF22] = 0x00;
memory[0xFF23] = 0xBF;
memory[0xFF24] = 0x77;
memory[0xFF25] = 0xF3;
memory[0xFF26] = 0xF1;
memory[0xFF40] = 0x91;
memory[0xFF42] = 0x00;
memory[0xFF43] = 0x00;
memory[0xFF45] = 0x00;
memory[0xFF47] = 0xFC;
memory[0xFF48] = 0xFF;
memory[0xFF49] = 0xFF;
memory[0xFF4A] = 0x00;
memory[0xFF4B] = 0x00;
memory[0xFF50] = 0x00; // Enable Boot ROM (explicitly added)
memory[0xFFFF] = 0x00;
#endregion
}
public void SetCartridge(byte[] cart, Cartridge.RomType romType)
{
Mbc1Enabled = romType == Cartridge.RomType.Mbc1;
if (!Mbc1Enabled && cart.Length != 32 * 1024)
throw new InvalidOperationException("Wrong plain cartridge length!");
cartridge = new byte[cart.Length];
for (int i = 0; i < cart.Length; i++)
cartridge[i] = cart[i];
BankSelected = 1;
RamBankSelected = 0;
Mbc1ModeRam = false;
}
public byte this[int address]
{
get
{
// If requesting low bytes and boot rom is not disabled (inverted bool)
if (address <= 0x00FF && memory[0xFF50] == 0x00)
return dmgBoot[address];
if (Mbc1Enabled)
{
if (address <= 0x3FFF)
return cartridge[address];
if (address <= 0x7FFF)
return cartridge[(BankSelected * 16 * 1024) + (address - 0x4000)];
if (RamEnabled && Mbc1ModeRam && address >= 0xA000 && address <= 0xBFFFF)
return Ram[(RamBankSelected * 16 * 1024) + (address - 0xA000)];
}
if (!Mbc1Enabled)
if (address <= 0x7FFF)
return cartridge[address];
if (address == 0xff00)
{
byte io = memory[address];
if((io & 0x20) == 0)
return (byte)(0xc0 | Keys1 | 0x10);
if((io & 0x10) == 0)
return (byte)(0xc0 | Keys2 | 0x20);
if((io & 0x30) == 0)
return 0xff;
return 0;
}
return memory[address];
}
set
{
if (Mbc1Enabled)
{
if (address <= 0x1FFF)
{
RamEnabled = ((value & 0x0F) == 0x0A);
RamBankSelected = 0;
return;
}
if (address <= 0x3FFF)
{
int bankSelectedLow5 = value & 0x1F;
if (bankSelectedLow5 == 0)
bankSelectedLow5 = 1;
BankSelected = (BankSelected & 0x60) | bankSelectedLow5;
return;
}
if (address <= 0x5FFF)
{
int bankSelectedHigh2 = value & 3; // Only two bits
if(Mbc1ModeRam)
RamBankSelected = bankSelectedHigh2;
else
BankSelected = (BankSelected & 0x1F) | (bankSelectedHigh2 << 5);
return;
}
if (address <= 0x7FFF)
{
Mbc1ModeRam = value == 1;
return;
}
if (Mbc1ModeRam && address >= 0xA000 && address <= 0xBFFF)
{
Ram[(RamBankSelected * 16 * 1024) + (address - 0xA000)] = value;
return;
}
}
memory[address] = value;
// Tileset changed (video ram)
if(address >= 0x8000 && address <= 0x97ff)
UpdateTile(address, value);
// Echo of 8KB internal RAM (0xC00-0xDE00 = 0xE000-0xFE00)
else if (address >= 0xC000 && address < 0xDE00)
memory[address+0x2000] = value;
else if (address >= 0xE000 && address < 0xFE00)
memory[address-0x2000] = value;
// Serial transfer start (a futile attempt to prevent Tetris 2P hang)
/*else if (address == 0xff02 && (value >> 7) > 0)
{
// Mark transfer finished -we don't have serial transfers here
memory[address-1] = 0xFF; // No other GameBoy present
memory[address] = (byte)(value & 0x7F);
// but the moment we launch the interruption, the 2P game starts...!
if((memory[0xffff] & Cpu.InterruptsSerial) > 0)
memory[0xff0f] |= Cpu.InterruptsSerial;
}*/
// DIV (timer) reset -writing to it resets it to 0
else if (address == 0xff04)
memory[address] = 0;
// Configurable Timer
else if (address == 0xff07)
{
switch (value & 0x03)
{
// Clock cycles: 4.19 MHz = 4.393.533,44 vs frequency (below)
case 0: TimerTick = 1072; break; // 4,096 KHz -> 1072,64 clock cycles
case 1: TimerTick = 17; break; // 262,144 KHz -> 16,76
case 2: TimerTick = 67; break; // 65,536 KHz -> 67,04
case 3: TimerTick = 268; break; // 16,384 KHz -> 268,16
}
if ((value & 0x04) > 0)
TimerEnabled = true;
else
TimerEnabled = false;
}
// Sound
else if (address == 0xff14)
sound.StartSound1();
else if (address == 0xff19)
sound.StartSound2();
else if (address == 0xff1e)
sound.StartSound3();
else if (address == 0xff23)
sound.StartSound4();
else if ((address == 0xff24 && value != memory[0xff24]) ||
(address == 0xff25 && value != memory[0xff25]))
sound.SetSoundOutput();
// OAM DMA
else if (address == 0xff46)
{
int sourceAddress = value << 8;
for (int j = 0; j < 160; j++)
memory[0xfe00 + j] = memory[sourceAddress++];
}
// Alter background palette
else if(address == 0xff47) // write only
{
for(int i = 0; i < 4; i++) BackgroundPalette[i] = Palette[(value >> (i * 2)) & 3];
}
// Alter sprite palettes
else if(address == 0xff48) // write only
{
for(int i = 0; i < 4; i++) SpritePalette[0,i] = Palette[(value >> (i * 2)) & 3];
}
else if(address == 0xff49) // write only
{
for(int i = 0; i < 4; i++) SpritePalette[1,i] = Palette[(value >> (i * 2)) & 3];
}
}
}
private void UpdateTile(int address, byte value)
{
address &= 0xfffe;
ushort tile = (ushort) ((address >> 4) & 0x1FF);
ushort y = (ushort) ((address >> 1) & 7);
byte x, bitIndex;
for(x = 0; x < 8; x++)
{
bitIndex = (byte) (1 << (7 - x));
Tiles[tile,y,x] = (byte) ((((memory[address] & bitIndex) > 0) ? 1 : 0) +
(((memory[address + 1] & bitIndex) > 0) ? 2 : 0));
}
}
}
}