all repos — mgba @ 3227d74e4d434d497673e5a77589b674e811c969

mGBA Game Boy Advance Emulator

src/gb/memory.c (view raw)

  1/* Copyright (c) 2013-2016 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include <mgba/internal/gb/memory.h>
  7
  8#include <mgba/core/interface.h>
  9#include <mgba/internal/gb/gb.h>
 10#include <mgba/internal/gb/io.h>
 11#include <mgba/internal/gb/mbc.h>
 12#include <mgba/internal/gb/serialize.h>
 13#include <mgba/internal/lr35902/lr35902.h>
 14
 15#include <mgba-util/memory.h>
 16
 17mLOG_DEFINE_CATEGORY(GB_MEM, "GB Memory", "gb.memory");
 18
 19struct OAMBlock {
 20	uint16_t low;
 21	uint16_t high;
 22};
 23
 24static const struct OAMBlock _oamBlockDMG[] = {
 25	{ 0xA000, 0xFE00 },
 26	{ 0xA000, 0xFE00 },
 27	{ 0xA000, 0xFE00 },
 28	{ 0xA000, 0xFE00 },
 29	{ 0x8000, 0xA000 },
 30	{ 0xA000, 0xFE00 },
 31	{ 0xA000, 0xFE00 },
 32	{ 0xA000, 0xFE00 },
 33};
 34
 35static const struct OAMBlock _oamBlockCGB[] = {
 36	{ 0xA000, 0xC000 },
 37	{ 0xA000, 0xC000 },
 38	{ 0xA000, 0xC000 },
 39	{ 0xA000, 0xC000 },
 40	{ 0x8000, 0xA000 },
 41	{ 0xA000, 0xC000 },
 42	{ 0xC000, 0xFE00 },
 43	{ 0xA000, 0xC000 },
 44};
 45
 46static void _pristineCow(struct GB* gba);
 47
 48static uint8_t GBFastLoad8(struct LR35902Core* cpu, uint16_t address) {
 49	if (UNLIKELY(address >= cpu->memory.activeRegionEnd)) {
 50		cpu->memory.setActiveRegion(cpu, address);
 51		return cpu->memory.cpuLoad8(cpu, address);
 52	}
 53	return cpu->memory.activeRegion[address & cpu->memory.activeMask];
 54}
 55
 56static void GBSetActiveRegion(struct LR35902Core* cpu, uint16_t address) {
 57	struct GB* gb = (struct GB*) cpu->master;
 58	struct GBMemory* memory = &gb->memory;
 59	switch (address >> 12) {
 60	case GB_REGION_CART_BANK0:
 61	case GB_REGION_CART_BANK0 + 1:
 62	case GB_REGION_CART_BANK0 + 2:
 63	case GB_REGION_CART_BANK0 + 3:
 64		cpu->memory.cpuLoad8 = GBFastLoad8;
 65		cpu->memory.activeRegion = memory->romBase;
 66		cpu->memory.activeRegionEnd = GB_BASE_CART_BANK1;
 67		cpu->memory.activeMask = GB_SIZE_CART_BANK0 - 1;
 68		break;
 69	case GB_REGION_CART_BANK1:
 70	case GB_REGION_CART_BANK1 + 1:
 71	case GB_REGION_CART_BANK1 + 2:
 72	case GB_REGION_CART_BANK1 + 3:
 73		cpu->memory.cpuLoad8 = GBFastLoad8;
 74		cpu->memory.activeRegion = memory->romBank;
 75		cpu->memory.activeRegionEnd = GB_BASE_VRAM;
 76		cpu->memory.activeMask = GB_SIZE_CART_BANK0 - 1;
 77		break;
 78	default:
 79		cpu->memory.cpuLoad8 = GBLoad8;
 80		break;
 81	}
 82}
 83
 84static void _GBMemoryDMAService(struct mTiming* timing, void* context, uint32_t cyclesLate);
 85static void _GBMemoryHDMAService(struct mTiming* timing, void* context, uint32_t cyclesLate);
 86
 87void GBMemoryInit(struct GB* gb) {
 88	struct LR35902Core* cpu = gb->cpu;
 89	cpu->memory.cpuLoad8 = GBLoad8;
 90	cpu->memory.load8 = GBLoad8;
 91	cpu->memory.store8 = GBStore8;
 92	cpu->memory.currentSegment = GBCurrentSegment;
 93	cpu->memory.setActiveRegion = GBSetActiveRegion;
 94
 95	gb->memory.wram = 0;
 96	gb->memory.wramBank = 0;
 97	gb->memory.rom = 0;
 98	gb->memory.romBank = 0;
 99	gb->memory.romSize = 0;
100	gb->memory.sram = 0;
101	gb->memory.mbcType = GB_MBC_AUTODETECT;
102	gb->memory.mbcRead = NULL;
103	gb->memory.mbcWrite = NULL;
104
105	gb->memory.rtc = NULL;
106	gb->memory.rotation = NULL;
107	gb->memory.rumble = NULL;
108	gb->memory.cam = NULL;
109
110	GBIOInit(gb);
111}
112
113void GBMemoryDeinit(struct GB* gb) {
114	mappedMemoryFree(gb->memory.wram, GB_SIZE_WORKING_RAM);
115	if (gb->memory.rom) {
116		mappedMemoryFree(gb->memory.rom, gb->memory.romSize);
117	}
118}
119
120void GBMemoryReset(struct GB* gb) {
121	if (gb->memory.wram) {
122		mappedMemoryFree(gb->memory.wram, GB_SIZE_WORKING_RAM);
123	}
124	gb->memory.wram = anonymousMemoryMap(GB_SIZE_WORKING_RAM);
125	if (gb->model >= GB_MODEL_CGB) {
126		uint32_t* base = (uint32_t*) gb->memory.wram;
127		size_t i;
128		uint32_t pattern = 0;
129		for (i = 0; i < GB_SIZE_WORKING_RAM / 4; i += 4) {
130			if ((i & 0x1FF) == 0) {
131				pattern = ~pattern;
132			}
133			base[i + 0] = pattern;
134			base[i + 1] = pattern;
135			base[i + 2] = ~pattern;
136			base[i + 3] = ~pattern;
137		}
138	}
139	GBMemorySwitchWramBank(&gb->memory, 1);
140	gb->memory.romBank = &gb->memory.rom[GB_SIZE_CART_BANK0];
141	gb->memory.currentBank = 1;
142	gb->memory.sramCurrentBank = 0;
143
144	gb->memory.ime = false;
145	gb->memory.ie = 0;
146
147	gb->memory.dmaRemaining = 0;
148	gb->memory.dmaSource = 0;
149	gb->memory.dmaDest = 0;
150	gb->memory.hdmaRemaining = 0;
151	gb->memory.hdmaSource = 0;
152	gb->memory.hdmaDest = 0;
153	gb->memory.isHdma = false;
154
155
156	gb->memory.dmaEvent.context = gb;
157	gb->memory.dmaEvent.name = "GB DMA";
158	gb->memory.dmaEvent.callback = _GBMemoryDMAService;
159	gb->memory.dmaEvent.priority = 0x40;
160	gb->memory.hdmaEvent.context = gb;
161	gb->memory.hdmaEvent.name = "GB HDMA";
162	gb->memory.hdmaEvent.callback = _GBMemoryHDMAService;
163	gb->memory.hdmaEvent.priority = 0x41;
164
165	memset(&gb->memory.hram, 0, sizeof(gb->memory.hram));
166	switch (gb->memory.mbcType) {
167	case GB_MBC1:
168		gb->memory.mbcState.mbc1.mode = 0;
169		break;
170	default:
171		memset(&gb->memory.mbcState, 0, sizeof(gb->memory.mbcState));
172	}
173
174	GBMBCInit(gb);
175	gb->memory.sramBank = gb->memory.sram;
176
177	if (!gb->memory.wram) {
178		GBMemoryDeinit(gb);
179	}
180}
181
182void GBMemorySwitchWramBank(struct GBMemory* memory, int bank) {
183	bank &= 7;
184	if (!bank) {
185		bank = 1;
186	}
187	memory->wramBank = &memory->wram[GB_SIZE_WORKING_RAM_BANK0 * bank];
188	memory->wramCurrentBank = bank;
189}
190
191uint8_t GBLoad8(struct LR35902Core* cpu, uint16_t address) {
192	struct GB* gb = (struct GB*) cpu->master;
193	struct GBMemory* memory = &gb->memory;
194	if (gb->memory.dmaRemaining) {
195		const struct OAMBlock* block = gb->model < GB_MODEL_CGB ? _oamBlockDMG : _oamBlockCGB;
196		block = &block[memory->dmaSource >> 13];
197		if (address >= block->low && address < block->high) {
198			return 0xFF;
199		}
200		if (address >= GB_BASE_OAM && address < GB_BASE_UNUSABLE) {
201			return 0xFF;
202		}
203	}
204	switch (address >> 12) {
205	case GB_REGION_CART_BANK0:
206	case GB_REGION_CART_BANK0 + 1:
207	case GB_REGION_CART_BANK0 + 2:
208	case GB_REGION_CART_BANK0 + 3:
209		return memory->romBase[address & (GB_SIZE_CART_BANK0 - 1)];
210	case GB_REGION_CART_BANK1 + 2:
211	case GB_REGION_CART_BANK1 + 3:
212		if (memory->mbcType == GB_MBC6) {
213			return memory->mbcState.mbc6.romBank1[address & (GB_SIZE_CART_HALFBANK - 1)];
214		}
215		// Fall through
216	case GB_REGION_CART_BANK1:
217	case GB_REGION_CART_BANK1 + 1:
218		return memory->romBank[address & (GB_SIZE_CART_BANK0 - 1)];
219	case GB_REGION_VRAM:
220	case GB_REGION_VRAM + 1:
221		return gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)];
222	case GB_REGION_EXTERNAL_RAM:
223	case GB_REGION_EXTERNAL_RAM + 1:
224		if (memory->rtcAccess) {
225			return memory->rtcRegs[memory->activeRtcReg];
226		} else if (memory->mbcRead) {
227			return memory->mbcRead(memory, address);
228		} else if (memory->sramAccess && memory->sram) {
229			return memory->sramBank[address & (GB_SIZE_EXTERNAL_RAM - 1)];
230		} else if (memory->mbcType == GB_HuC3) {
231			return 0x01; // TODO: Is this supposed to be the current SRAM bank?
232		}
233		return 0xFF;
234	case GB_REGION_WORKING_RAM_BANK0:
235	case GB_REGION_WORKING_RAM_BANK0 + 2:
236		return memory->wram[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
237	case GB_REGION_WORKING_RAM_BANK1:
238		return memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
239	default:
240		if (address < GB_BASE_OAM) {
241			return memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
242		}
243		if (address < GB_BASE_UNUSABLE) {
244			if (gb->video.mode < 2) {
245				return gb->video.oam.raw[address & 0xFF];
246			}
247			return 0xFF;
248		}
249		if (address < GB_BASE_IO) {
250			mLOG(GB_MEM, GAME_ERROR, "Attempt to read from unusable memory: %04X", address);
251			return 0xFF;
252		}
253		if (address < GB_BASE_HRAM) {
254			return GBIORead(gb, address & (GB_SIZE_IO - 1));
255		}
256		if (address < GB_BASE_IE) {
257			return memory->hram[address & GB_SIZE_HRAM];
258		}
259		return GBIORead(gb, REG_IE);
260	}
261}
262
263void GBStore8(struct LR35902Core* cpu, uint16_t address, int8_t value) {
264	struct GB* gb = (struct GB*) cpu->master;
265	struct GBMemory* memory = &gb->memory;
266	if (gb->memory.dmaRemaining) {
267		const struct OAMBlock* block = gb->model < GB_MODEL_CGB ? _oamBlockDMG : _oamBlockCGB;
268		block = &block[memory->dmaSource >> 13];
269		if (address >= block->low && address < block->high) {
270			return;
271		}
272		if (address >= GB_BASE_OAM && address < GB_BASE_UNUSABLE) {
273			return;
274		}
275	}
276	switch (address >> 12) {
277	case GB_REGION_CART_BANK0:
278	case GB_REGION_CART_BANK0 + 1:
279	case GB_REGION_CART_BANK0 + 2:
280	case GB_REGION_CART_BANK0 + 3:
281	case GB_REGION_CART_BANK1:
282	case GB_REGION_CART_BANK1 + 1:
283	case GB_REGION_CART_BANK1 + 2:
284	case GB_REGION_CART_BANK1 + 3:
285		memory->mbcWrite(gb, address, value);
286		cpu->memory.setActiveRegion(cpu, cpu->pc);
287		return;
288	case GB_REGION_VRAM:
289	case GB_REGION_VRAM + 1:
290		gb->video.renderer->writeVRAM(gb->video.renderer, (address & (GB_SIZE_VRAM_BANK0 - 1)) | (GB_SIZE_VRAM_BANK0 * gb->video.vramCurrentBank));
291		gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)] = value;
292		return;
293	case GB_REGION_EXTERNAL_RAM:
294	case GB_REGION_EXTERNAL_RAM + 1:
295		if (memory->rtcAccess) {
296			memory->rtcRegs[memory->activeRtcReg] = value;
297		} else if (memory->sramAccess && memory->sram) {
298			memory->sramBank[address & (GB_SIZE_EXTERNAL_RAM - 1)] = value;
299		} else {
300			memory->mbcWrite(gb, address, value);
301		}
302		gb->sramDirty |= GB_SRAM_DIRT_NEW;
303		return;
304	case GB_REGION_WORKING_RAM_BANK0:
305	case GB_REGION_WORKING_RAM_BANK0 + 2:
306		memory->wram[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value;
307		return;
308	case GB_REGION_WORKING_RAM_BANK1:
309		memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value;
310		return;
311	default:
312		if (address < GB_BASE_OAM) {
313			memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value;
314		} else if (address < GB_BASE_UNUSABLE) {
315			if (gb->video.mode < 2) {
316				gb->video.oam.raw[address & 0xFF] = value;
317				gb->video.renderer->writeOAM(gb->video.renderer, address & 0xFF);
318			}
319		} else if (address < GB_BASE_IO) {
320			mLOG(GB_MEM, GAME_ERROR, "Attempt to write to unusable memory: %04X:%02X", address, value);
321		} else if (address < GB_BASE_HRAM) {
322			GBIOWrite(gb, address & (GB_SIZE_IO - 1), value);
323		} else if (address < GB_BASE_IE) {
324			memory->hram[address & GB_SIZE_HRAM] = value;
325		} else {
326			GBIOWrite(gb, REG_IE, value);
327		}
328	}
329}
330
331int GBCurrentSegment(struct LR35902Core* cpu, uint16_t address) {
332	struct GB* gb = (struct GB*) cpu->master;
333	struct GBMemory* memory = &gb->memory;
334	switch (address >> 12) {
335	case GB_REGION_CART_BANK0:
336	case GB_REGION_CART_BANK0 + 1:
337	case GB_REGION_CART_BANK0 + 2:
338	case GB_REGION_CART_BANK0 + 3:
339		return 0;
340	case GB_REGION_CART_BANK1:
341	case GB_REGION_CART_BANK1 + 1:
342	case GB_REGION_CART_BANK1 + 2:
343	case GB_REGION_CART_BANK1 + 3:
344		return memory->currentBank;
345	case GB_REGION_VRAM:
346	case GB_REGION_VRAM + 1:
347		return gb->video.vramCurrentBank;
348	case GB_REGION_EXTERNAL_RAM:
349	case GB_REGION_EXTERNAL_RAM + 1:
350		return memory->sramCurrentBank;
351	case GB_REGION_WORKING_RAM_BANK0:
352	case GB_REGION_WORKING_RAM_BANK0 + 2:
353		return 0;
354	case GB_REGION_WORKING_RAM_BANK1:
355		return memory->wramCurrentBank;
356	default:
357		return 0;
358	}
359}
360
361uint8_t GBView8(struct LR35902Core* cpu, uint16_t address, int segment) {
362	struct GB* gb = (struct GB*) cpu->master;
363	struct GBMemory* memory = &gb->memory;
364	switch (address >> 12) {
365	case GB_REGION_CART_BANK0:
366	case GB_REGION_CART_BANK0 + 1:
367	case GB_REGION_CART_BANK0 + 2:
368	case GB_REGION_CART_BANK0 + 3:
369		return memory->romBase[address & (GB_SIZE_CART_BANK0 - 1)];
370	case GB_REGION_CART_BANK1:
371	case GB_REGION_CART_BANK1 + 1:
372	case GB_REGION_CART_BANK1 + 2:
373	case GB_REGION_CART_BANK1 + 3:
374		if (segment < 0) {
375			return memory->romBank[address & (GB_SIZE_CART_BANK0 - 1)];
376		} else if ((size_t) segment * GB_SIZE_CART_BANK0 < memory->romSize) {
377			return memory->rom[(address & (GB_SIZE_CART_BANK0 - 1)) + segment * GB_SIZE_CART_BANK0];
378		} else {
379			return 0xFF;
380		}
381	case GB_REGION_VRAM:
382	case GB_REGION_VRAM + 1:
383		if (segment < 0) {
384			return gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)];
385		} else if (segment < 2) {
386			return gb->video.vram[(address & (GB_SIZE_VRAM_BANK0 - 1)) + segment *GB_SIZE_VRAM_BANK0];
387		} else {
388			return 0xFF;
389		}
390	case GB_REGION_EXTERNAL_RAM:
391	case GB_REGION_EXTERNAL_RAM + 1:
392		if (memory->rtcAccess) {
393			return memory->rtcRegs[memory->activeRtcReg];
394		} else if (memory->sramAccess) {
395			if (segment < 0 && memory->sram) {
396				return memory->sramBank[address & (GB_SIZE_EXTERNAL_RAM - 1)];
397			} else if ((size_t) segment * GB_SIZE_EXTERNAL_RAM < gb->sramSize) {
398				return memory->sram[(address & (GB_SIZE_EXTERNAL_RAM - 1)) + segment *GB_SIZE_EXTERNAL_RAM];
399			} else {
400				return 0xFF;
401			}
402		} else if (memory->mbcRead) {
403			return memory->mbcRead(memory, address);
404		} else if (memory->mbcType == GB_HuC3) {
405			return 0x01; // TODO: Is this supposed to be the current SRAM bank?
406		}
407		return 0xFF;
408	case GB_REGION_WORKING_RAM_BANK0:
409	case GB_REGION_WORKING_RAM_BANK0 + 2:
410		return memory->wram[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
411	case GB_REGION_WORKING_RAM_BANK1:
412		if (segment < 0) {
413			return memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
414		} else if (segment < 8) {
415			return memory->wram[(address & (GB_SIZE_WORKING_RAM_BANK0 - 1)) + segment *GB_SIZE_WORKING_RAM_BANK0];
416		} else {
417			return 0xFF;
418		}
419	default:
420		if (address < GB_BASE_OAM) {
421			return memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
422		}
423		if (address < GB_BASE_UNUSABLE) {
424			if (gb->video.mode < 2) {
425				return gb->video.oam.raw[address & 0xFF];
426			}
427			return 0xFF;
428		}
429		if (address < GB_BASE_IO) {
430			mLOG(GB_MEM, GAME_ERROR, "Attempt to read from unusable memory: %04X", address);
431			return 0xFF;
432		}
433		if (address < GB_BASE_HRAM) {
434			return GBIORead(gb, address & (GB_SIZE_IO - 1));
435		}
436		if (address < GB_BASE_IE) {
437			return memory->hram[address & GB_SIZE_HRAM];
438		}
439		return GBIORead(gb, REG_IE);
440	}
441}
442
443void GBMemoryDMA(struct GB* gb, uint16_t base) {
444	if (base > 0xF100) {
445		return;
446	}
447	mTimingDeschedule(&gb->timing, &gb->memory.dmaEvent);
448	mTimingSchedule(&gb->timing, &gb->memory.dmaEvent, 8);
449	if (gb->cpu->cycles + 8 < gb->cpu->nextEvent) {
450		gb->cpu->nextEvent = gb->cpu->cycles + 8;
451	}
452	gb->memory.dmaSource = base;
453	gb->memory.dmaDest = 0;
454	gb->memory.dmaRemaining = 0xA0;
455}
456
457void GBMemoryWriteHDMA5(struct GB* gb, uint8_t value) {
458	gb->memory.hdmaSource = gb->memory.io[REG_HDMA1] << 8;
459	gb->memory.hdmaSource |= gb->memory.io[REG_HDMA2];
460	gb->memory.hdmaDest = gb->memory.io[REG_HDMA3] << 8;
461	gb->memory.hdmaDest |= gb->memory.io[REG_HDMA4];
462	gb->memory.hdmaSource &= 0xFFF0;
463	if (gb->memory.hdmaSource >= 0x8000 && gb->memory.hdmaSource < 0xA000) {
464		mLOG(GB_MEM, GAME_ERROR, "Invalid HDMA source: %04X", gb->memory.hdmaSource);
465		return;
466	}
467	gb->memory.hdmaDest &= 0x1FF0;
468	gb->memory.hdmaDest |= 0x8000;
469	bool wasHdma = gb->memory.isHdma;
470	gb->memory.isHdma = value & 0x80;
471	if ((!wasHdma && !gb->memory.isHdma) || gb->video.mode == 0) {
472		gb->memory.hdmaRemaining = ((value & 0x7F) + 1) * 0x10;
473		gb->cpuBlocked = true;
474		mTimingSchedule(&gb->timing, &gb->memory.hdmaEvent, 0);
475		gb->cpu->nextEvent = gb->cpu->cycles;
476	}
477}
478
479void _GBMemoryDMAService(struct mTiming* timing, void* context, uint32_t cyclesLate) {
480	struct GB* gb = context;
481	int dmaRemaining = gb->memory.dmaRemaining;
482	gb->memory.dmaRemaining = 0;
483	uint8_t b = GBLoad8(gb->cpu, gb->memory.dmaSource);
484	// TODO: Can DMA write OAM during modes 2-3?
485	gb->video.oam.raw[gb->memory.dmaDest] = b;
486	gb->video.renderer->writeOAM(gb->video.renderer, gb->memory.dmaDest);
487	++gb->memory.dmaSource;
488	++gb->memory.dmaDest;
489	gb->memory.dmaRemaining = dmaRemaining - 1;
490	if (gb->memory.dmaRemaining) {
491		mTimingSchedule(timing, &gb->memory.dmaEvent, 4 - cyclesLate);
492	}
493}
494
495void _GBMemoryHDMAService(struct mTiming* timing, void* context, uint32_t cyclesLate) {
496	struct GB* gb = context;
497	gb->cpuBlocked = true;
498	uint8_t b = gb->cpu->memory.load8(gb->cpu, gb->memory.hdmaSource);
499	gb->cpu->memory.store8(gb->cpu, gb->memory.hdmaDest, b);
500	++gb->memory.hdmaSource;
501	++gb->memory.hdmaDest;
502	--gb->memory.hdmaRemaining;
503	if (gb->memory.hdmaRemaining) {
504		mTimingDeschedule(timing, &gb->memory.hdmaEvent);
505		mTimingSchedule(timing, &gb->memory.hdmaEvent, 2 - cyclesLate);
506	} else {
507		gb->cpuBlocked = false;
508		gb->memory.io[REG_HDMA1] = gb->memory.hdmaSource >> 8;
509		gb->memory.io[REG_HDMA2] = gb->memory.hdmaSource;
510		gb->memory.io[REG_HDMA3] = gb->memory.hdmaDest >> 8;
511		gb->memory.io[REG_HDMA4] = gb->memory.hdmaDest;
512		if (gb->memory.isHdma) {
513			--gb->memory.io[REG_HDMA5];
514			if (gb->memory.io[REG_HDMA5] == 0xFF) {
515				gb->memory.isHdma = false;
516			}
517		} else {
518			gb->memory.io[REG_HDMA5] = 0xFF;
519		}
520	}
521}
522
523void GBPatch8(struct LR35902Core* cpu, uint16_t address, int8_t value, int8_t* old, int segment) {
524	struct GB* gb = (struct GB*) cpu->master;
525	struct GBMemory* memory = &gb->memory;
526	int8_t oldValue = -1;
527
528	switch (address >> 12) {
529	case GB_REGION_CART_BANK0:
530	case GB_REGION_CART_BANK0 + 1:
531	case GB_REGION_CART_BANK0 + 2:
532	case GB_REGION_CART_BANK0 + 3:
533		_pristineCow(gb);
534		oldValue = memory->romBase[address & (GB_SIZE_CART_BANK0 - 1)];
535		memory->romBase[address & (GB_SIZE_CART_BANK0 - 1)] =  value;
536		break;
537	case GB_REGION_CART_BANK1:
538	case GB_REGION_CART_BANK1 + 1:
539	case GB_REGION_CART_BANK1 + 2:
540	case GB_REGION_CART_BANK1 + 3:
541		_pristineCow(gb);
542		if (segment < 0) {
543			oldValue = memory->romBank[address & (GB_SIZE_CART_BANK0 - 1)];
544			memory->romBank[address & (GB_SIZE_CART_BANK0 - 1)] = value;
545		} else if ((size_t) segment * GB_SIZE_CART_BANK0 < memory->romSize) {
546			oldValue = memory->rom[(address & (GB_SIZE_CART_BANK0 - 1)) + segment * GB_SIZE_CART_BANK0];
547			memory->rom[(address & (GB_SIZE_CART_BANK0 - 1)) + segment * GB_SIZE_CART_BANK0] = value;
548		} else {
549			return;
550		}
551		break;
552	case GB_REGION_VRAM:
553	case GB_REGION_VRAM + 1:
554		if (segment < 0) {
555			oldValue = gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)];
556			gb->video.vramBank[address & (GB_SIZE_VRAM_BANK0 - 1)] = value;
557			gb->video.renderer->writeVRAM(gb->video.renderer, (address & (GB_SIZE_VRAM_BANK0 - 1)) + GB_SIZE_VRAM_BANK0 * gb->video.vramCurrentBank);
558		} else if (segment < 2) {
559			oldValue = gb->video.vram[(address & (GB_SIZE_VRAM_BANK0 - 1)) + segment * GB_SIZE_VRAM_BANK0];
560			gb->video.vramBank[(address & (GB_SIZE_VRAM_BANK0 - 1)) + segment * GB_SIZE_VRAM_BANK0] = value;
561			gb->video.renderer->writeVRAM(gb->video.renderer, (address & (GB_SIZE_VRAM_BANK0 - 1)) + segment * GB_SIZE_VRAM_BANK0);
562		} else {
563			return;
564		}
565		break;
566	case GB_REGION_EXTERNAL_RAM:
567	case GB_REGION_EXTERNAL_RAM + 1:
568		mLOG(GB_MEM, STUB, "Unimplemented memory Patch8: 0x%08X", address);
569		return;
570	case GB_REGION_WORKING_RAM_BANK0:
571	case GB_REGION_WORKING_RAM_BANK0 + 2:
572		oldValue = memory->wram[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
573		memory->wram[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value;
574		break;
575	case GB_REGION_WORKING_RAM_BANK1:
576		if (segment < 0) {
577			oldValue = memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
578			memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value;
579		} else if (segment < 8) {
580			oldValue = memory->wram[(address & (GB_SIZE_WORKING_RAM_BANK0 - 1)) + segment * GB_SIZE_WORKING_RAM_BANK0];
581			memory->wram[(address & (GB_SIZE_WORKING_RAM_BANK0 - 1)) + segment * GB_SIZE_WORKING_RAM_BANK0] = value;
582		} else {
583			return;
584		}
585		break;
586	default:
587		if (address < GB_BASE_OAM) {
588			oldValue = memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)];
589			memory->wramBank[address & (GB_SIZE_WORKING_RAM_BANK0 - 1)] = value;
590		} else if (address < GB_BASE_UNUSABLE) {
591			oldValue = gb->video.oam.raw[address & 0xFF];
592			gb->video.oam.raw[address & 0xFF] = value;
593			gb->video.renderer->writeOAM(gb->video.renderer, address & 0xFF);
594		} else if (address < GB_BASE_HRAM) {
595			mLOG(GB_MEM, STUB, "Unimplemented memory Patch8: 0x%08X", address);
596			return;
597		} else if (address < GB_BASE_IE) {
598			oldValue = memory->hram[address & GB_SIZE_HRAM];
599			memory->hram[address & GB_SIZE_HRAM] = value;
600		} else {
601			mLOG(GB_MEM, STUB, "Unimplemented memory Patch8: 0x%08X", address);
602			return;
603		}
604	}
605	if (old) {
606		*old = oldValue;
607	}
608}
609
610void GBMemorySerialize(const struct GB* gb, struct GBSerializedState* state) {
611	const struct GBMemory* memory = &gb->memory;
612	memcpy(state->wram, memory->wram, GB_SIZE_WORKING_RAM);
613	memcpy(state->hram, memory->hram, GB_SIZE_HRAM);
614	STORE_16LE(memory->currentBank, 0, &state->memory.currentBank);
615	state->memory.wramCurrentBank = memory->wramCurrentBank;
616	state->memory.sramCurrentBank = memory->sramCurrentBank;
617
618	STORE_16LE(memory->dmaSource, 0, &state->memory.dmaSource);
619	STORE_16LE(memory->dmaDest, 0, &state->memory.dmaDest);
620
621	STORE_16LE(memory->hdmaSource, 0, &state->memory.hdmaSource);
622	STORE_16LE(memory->hdmaDest, 0, &state->memory.hdmaDest);
623
624	STORE_16LE(memory->hdmaRemaining, 0, &state->memory.hdmaRemaining);
625	state->memory.dmaRemaining = memory->dmaRemaining;
626	memcpy(state->memory.rtcRegs, memory->rtcRegs, sizeof(state->memory.rtcRegs));
627
628	STORE_32LE(memory->dmaEvent.when - mTimingCurrentTime(&gb->timing), 0, &state->memory.dmaNext);
629	STORE_32LE(memory->hdmaEvent.when - mTimingCurrentTime(&gb->timing), 0, &state->memory.hdmaNext);
630
631	GBSerializedMemoryFlags flags = 0;
632	flags = GBSerializedMemoryFlagsSetSramAccess(flags, memory->sramAccess);
633	flags = GBSerializedMemoryFlagsSetRtcAccess(flags, memory->rtcAccess);
634	flags = GBSerializedMemoryFlagsSetRtcLatched(flags, memory->rtcLatched);
635	flags = GBSerializedMemoryFlagsSetIme(flags, memory->ime);
636	flags = GBSerializedMemoryFlagsSetIsHdma(flags, memory->isHdma);
637	flags = GBSerializedMemoryFlagsSetActiveRtcReg(flags, memory->activeRtcReg);
638	STORE_16LE(flags, 0, &state->memory.flags);
639
640	switch (memory->mbcType) {
641	case GB_MBC1:
642		state->memory.mbc1.mode = memory->mbcState.mbc1.mode;
643		state->memory.mbc1.multicartStride = memory->mbcState.mbc1.multicartStride;
644		break;
645	case GB_MBC3_RTC:
646		STORE_64LE(gb->memory.rtcLastLatch, 0, &state->memory.rtc.lastLatch);
647		break;
648	case GB_MBC7:
649		state->memory.mbc7.state = memory->mbcState.mbc7.state;
650		state->memory.mbc7.eeprom = memory->mbcState.mbc7.eeprom;
651		state->memory.mbc7.address = memory->mbcState.mbc7.address;
652		state->memory.mbc7.access = memory->mbcState.mbc7.access;
653		state->memory.mbc7.latch = memory->mbcState.mbc7.latch;
654		state->memory.mbc7.srBits = memory->mbcState.mbc7.srBits;
655		STORE_16LE(memory->mbcState.mbc7.sr, 0, &state->memory.mbc7.sr);
656		STORE_32LE(memory->mbcState.mbc7.writable, 0, &state->memory.mbc7.writable);
657		break;
658	default:
659		break;
660	}
661}
662
663void GBMemoryDeserialize(struct GB* gb, const struct GBSerializedState* state) {
664	struct GBMemory* memory = &gb->memory;
665	memcpy(memory->wram, state->wram, GB_SIZE_WORKING_RAM);
666	memcpy(memory->hram, state->hram, GB_SIZE_HRAM);
667	LOAD_16LE(memory->currentBank, 0, &state->memory.currentBank);
668	memory->wramCurrentBank = state->memory.wramCurrentBank;
669	memory->sramCurrentBank = state->memory.sramCurrentBank;
670
671	GBMBCSwitchBank(gb, memory->currentBank);
672	GBMemorySwitchWramBank(memory, memory->wramCurrentBank);
673	GBMBCSwitchSramBank(gb, memory->sramCurrentBank);
674
675	LOAD_16LE(memory->dmaSource, 0, &state->memory.dmaSource);
676	LOAD_16LE(memory->dmaDest, 0, &state->memory.dmaDest);
677
678	LOAD_16LE(memory->hdmaSource, 0, &state->memory.hdmaSource);
679	LOAD_16LE(memory->hdmaDest, 0, &state->memory.hdmaDest);
680
681	LOAD_16LE(memory->hdmaRemaining, 0, &state->memory.hdmaRemaining);
682	memory->dmaRemaining = state->memory.dmaRemaining;
683	memcpy(memory->rtcRegs, state->memory.rtcRegs, sizeof(state->memory.rtcRegs));
684
685	uint32_t when;
686	LOAD_32LE(when, 0, &state->memory.dmaNext);
687	if (memory->dmaRemaining) {
688		mTimingSchedule(&gb->timing, &memory->dmaEvent, when);
689	}
690	LOAD_32LE(when, 0, &state->memory.hdmaNext);
691	if (memory->hdmaRemaining) {
692		mTimingSchedule(&gb->timing, &memory->hdmaEvent, when);
693	}
694
695	GBSerializedMemoryFlags flags;
696	LOAD_16LE(flags, 0, &state->memory.flags);
697	memory->sramAccess = GBSerializedMemoryFlagsGetSramAccess(flags);
698	memory->rtcAccess = GBSerializedMemoryFlagsGetRtcAccess(flags);
699	memory->rtcLatched = GBSerializedMemoryFlagsGetRtcLatched(flags);
700	memory->ime = GBSerializedMemoryFlagsGetIme(flags);
701	memory->isHdma = GBSerializedMemoryFlagsGetIsHdma(flags);
702	memory->activeRtcReg = GBSerializedMemoryFlagsGetActiveRtcReg(flags);
703
704	switch (memory->mbcType) {
705	case GB_MBC1:
706		memory->mbcState.mbc1.mode = state->memory.mbc1.mode;
707		memory->mbcState.mbc1.multicartStride = state->memory.mbc1.multicartStride;
708		if (memory->mbcState.mbc1.mode) {
709			GBMBCSwitchBank0(gb, memory->currentBank >> memory->mbcState.mbc1.multicartStride);
710		}
711		break;
712	case GB_MBC3_RTC:
713		// TODO?
714		//LOAD_64LE(gb->memory.rtcLastLatch, 0, &state->memory.rtc.lastLatch);
715		break;
716	case GB_MBC7:
717		memory->mbcState.mbc7.state = state->memory.mbc7.state;
718		memory->mbcState.mbc7.eeprom = state->memory.mbc7.eeprom;
719		memory->mbcState.mbc7.address = state->memory.mbc7.address & 0x7F;
720		memory->mbcState.mbc7.access = state->memory.mbc7.access;
721		memory->mbcState.mbc7.latch = state->memory.mbc7.latch;
722		memory->mbcState.mbc7.srBits = state->memory.mbc7.srBits;
723		LOAD_16LE(memory->mbcState.mbc7.sr, 0, &state->memory.mbc7.sr);
724		LOAD_32LE(memory->mbcState.mbc7.writable, 0, &state->memory.mbc7.writable);
725		break;
726	default:
727		break;
728	}
729}
730
731void _pristineCow(struct GB* gb) {
732	if (!gb->isPristine) {
733		return;
734	}
735	void* newRom = anonymousMemoryMap(GB_SIZE_CART_MAX);
736	memcpy(newRom, gb->memory.rom, gb->memory.romSize);
737	memset(((uint8_t*) newRom) + gb->memory.romSize, 0xFF, GB_SIZE_CART_MAX - gb->memory.romSize);
738	if (gb->memory.rom == gb->memory.romBase) {
739		gb->memory.romBase = newRom;
740	}
741	gb->memory.rom = newRom;
742	GBMBCSwitchBank(gb, gb->memory.currentBank);
743	gb->isPristine = false;
744}