all repos — mgba @ ee44e2ac89d90979f8451672c4e0298438a3715d

mGBA Game Boy Advance Emulator

src/gba/gba-memory.c (view raw)

  1#include "gba-memory.h"
  2
  3#include "gba-io.h"
  4#include "hle-bios.h"
  5
  6#include <limits.h>
  7#include <string.h>
  8#include <sys/mman.h>
  9
 10static const char* GBA_CANNOT_MMAP = "Could not map memory";
 11
 12static void GBASetActiveRegion(struct ARMMemory* memory, uint32_t region);
 13static int GBAWaitMultiple(struct ARMMemory* memory, uint32_t startAddress, int count);
 14
 15static const char GBA_BASE_WAITSTATES[16] = { 0, 0, 2, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4 };
 16static const char GBA_BASE_WAITSTATES_32[16] = { 0, 0, 5, 0, 0, 0, 0, 0, 7, 7, 9, 9, 13, 13, 9 };
 17static const char GBA_BASE_WAITSTATES_SEQ[16] = { 0, 0, 2, 0, 0, 0, 0, 0, 2, 2, 4, 4, 8, 8, 4 };
 18static const char GBA_BASE_WAITSTATES_SEQ_32[16] = { 0, 0, 5, 0, 0, 0, 0, 0, 5, 5, 9, 9, 17, 17, 9 };
 19static const char GBA_ROM_WAITSTATES[] = { 4, 3, 2, 8 };
 20static const char GBA_ROM_WAITSTATES_SEQ[] = { 2, 1, 4, 1, 8, 1 };
 21static const int DMA_OFFSET[] = { 1, -1, 0, 1 };
 22
 23void GBAMemoryInit(struct GBAMemory* memory) {
 24	memory->d.load32 = GBALoad32;
 25	memory->d.load16 = GBALoad16;
 26	memory->d.loadU16 = GBALoadU16;
 27	memory->d.load8 = GBALoad8;
 28	memory->d.loadU8 = GBALoadU8;
 29	memory->d.store32 = GBAStore32;
 30	memory->d.store16 = GBAStore16;
 31	memory->d.store8 = GBAStore8;
 32
 33	memory->bios = (uint32_t*) hleBios;
 34	memory->fullBios = 0;
 35	memory->wram = mmap(0, SIZE_WORKING_RAM, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
 36	memory->iwram = mmap(0, SIZE_WORKING_IRAM, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
 37	memory->rom = 0;
 38	memset(memory->io, 0, sizeof(memory->io));
 39	memset(memory->dma, 0, sizeof(memory->dma));
 40
 41	if (!memory->wram || !memory->iwram) {
 42		GBAMemoryDeinit(memory);
 43		memory->p->errno = GBA_OUT_OF_MEMORY;
 44		memory->p->errstr = GBA_CANNOT_MMAP;
 45	}
 46
 47	int i;
 48	for (i = 0; i < 16; ++i) {
 49		memory->waitstates16[i] = GBA_BASE_WAITSTATES[i];
 50		memory->waitstatesSeq16[i] = GBA_BASE_WAITSTATES_SEQ[i];
 51		memory->waitstatesPrefetch16[i] = GBA_BASE_WAITSTATES_SEQ[i];
 52		memory->waitstates32[i] = GBA_BASE_WAITSTATES_32[i];
 53		memory->waitstatesSeq32[i] = GBA_BASE_WAITSTATES_SEQ_32[i];
 54		memory->waitstatesPrefetch32[i] = GBA_BASE_WAITSTATES_SEQ_32[i];
 55	}
 56	for (; i < 256; ++i) {
 57		memory->waitstates16[i] = 0;
 58		memory->waitstatesSeq16[i] = 0;
 59		memory->waitstatesPrefetch16[i] = 0;
 60		memory->waitstates32[i] = 0;
 61		memory->waitstatesSeq32[i] = 0;
 62		memory->waitstatesPrefetch32[i] = 0;
 63	}
 64
 65	memory->activeRegion = 0;
 66	memory->d.activeRegion = 0;
 67	memory->d.activeMask = 0;
 68	memory->d.setActiveRegion = GBASetActiveRegion;
 69	memory->d.activePrefetchCycles32 = 0;
 70	memory->d.activePrefetchCycles16 = 0;
 71	memory->biosPrefetch = 0;
 72	memory->d.waitMultiple = GBAWaitMultiple;
 73}
 74
 75void GBAMemoryDeinit(struct GBAMemory* memory) {
 76	munmap(memory->wram, SIZE_WORKING_RAM);
 77	munmap(memory->iwram, SIZE_WORKING_IRAM);
 78	GBASavedataDeinit(&memory->savedata);
 79}
 80
 81static void GBASetActiveRegion(struct ARMMemory* memory, uint32_t address) {
 82	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
 83
 84	if (gbaMemory->activeRegion == REGION_BIOS) {
 85		gbaMemory->biosPrefetch = memory->load32(memory, gbaMemory->p->cpu.currentPC + WORD_SIZE_ARM * 2, 0);
 86	}
 87	gbaMemory->activeRegion = address >> BASE_OFFSET;
 88	memory->activePrefetchCycles32 = gbaMemory->waitstatesPrefetch32[gbaMemory->activeRegion];
 89	memory->activePrefetchCycles16 = gbaMemory->waitstatesPrefetch16[gbaMemory->activeRegion];
 90	memory->activeNonseqCycles32 = gbaMemory->waitstates32[gbaMemory->activeRegion];
 91	memory->activeNonseqCycles16 = gbaMemory->waitstates16[gbaMemory->activeRegion];
 92	switch (address & ~OFFSET_MASK) {
 93	case BASE_BIOS:
 94		memory->activeRegion = gbaMemory->bios;
 95		memory->activeMask = SIZE_BIOS - 1;
 96		break;
 97	case BASE_WORKING_RAM:
 98		memory->activeRegion = gbaMemory->wram;
 99		memory->activeMask = SIZE_WORKING_RAM - 1;
100		break;
101	case BASE_WORKING_IRAM:
102		memory->activeRegion = gbaMemory->iwram;
103		memory->activeMask = SIZE_WORKING_IRAM - 1;
104		break;
105	case BASE_CART0:
106	case BASE_CART0_EX:
107	case BASE_CART1:
108	case BASE_CART1_EX:
109	case BASE_CART2:
110	case BASE_CART2_EX:
111		memory->activeRegion = gbaMemory->rom;
112		memory->activeMask = SIZE_CART0 - 1;
113		break;
114	default:
115		memory->activeRegion = 0;
116		memory->activeMask = 0;
117		break;
118	}
119}
120
121int32_t GBALoad32(struct ARMMemory* memory, uint32_t address, int* cycleCounter) {
122	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
123	uint32_t value = 0;
124	int wait = 0;
125
126	switch (address & ~OFFSET_MASK) {
127	case BASE_BIOS:
128		if (gbaMemory->p->cpu.currentPC >> BASE_OFFSET == REGION_BIOS) {
129			if (address < SIZE_BIOS) {
130				value = gbaMemory->bios[address >> 2];
131			} else {
132				value = 0;
133			}
134		} else {
135			value = gbaMemory->biosPrefetch;
136		}
137		break;
138	case BASE_WORKING_RAM:
139		value = gbaMemory->wram[(address & (SIZE_WORKING_RAM - 1)) >> 2];
140		wait = gbaMemory->waitstates32[REGION_WORKING_RAM];
141		break;
142	case BASE_WORKING_IRAM:
143		value = gbaMemory->iwram[(address & (SIZE_WORKING_IRAM - 1)) >> 2];
144		break;
145	case BASE_IO:
146		value = GBAIORead(gbaMemory->p, (address & (SIZE_IO - 1)) & ~2) | (GBAIORead(gbaMemory->p, (address & (SIZE_IO - 1)) | 2) << 16);
147		break;
148	case BASE_PALETTE_RAM:
149		value = ((int32_t*) gbaMemory->p->video.palette)[(address & (SIZE_PALETTE_RAM - 1)) >> 2];
150		break;
151	case BASE_VRAM:
152		value = ((int32_t*) gbaMemory->p->video.renderer->vram)[(address & 0x0001FFFF) >> 2];
153		break;
154	case BASE_OAM:
155		value = ((int32_t*) gbaMemory->p->video.oam.raw)[(address & (SIZE_OAM - 1)) >> 2];
156		break;
157	case BASE_CART0:
158	case BASE_CART0_EX:
159	case BASE_CART1:
160	case BASE_CART1_EX:
161	case BASE_CART2:
162	case BASE_CART2_EX:
163		wait = gbaMemory->waitstates32[address >> BASE_OFFSET];
164		if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
165			value = gbaMemory->rom[(address & (SIZE_CART0 - 1)) >> 2];
166		}
167		break;
168	case BASE_CART_SRAM:
169		GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Load32: 0x%08X", address);
170		break;
171	default:
172		GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Load32: 0x%08X", address);
173		if (gbaMemory->p->cpu.executionMode == MODE_ARM) {
174			value = memory->load32(memory, gbaMemory->p->cpu.currentPC + WORD_SIZE_ARM * 2, 0);
175		} else {
176			value = memory->load16(memory, gbaMemory->p->cpu.currentPC + WORD_SIZE_THUMB * 2, 0);
177			value |= value << 16;
178		}
179		break;
180	}
181
182
183	if (cycleCounter) {
184		*cycleCounter += 2 + wait;
185	}
186	// Unaligned 32-bit loads are "rotated" so they make some semblance of sense
187	int rotate = (address & 3) << 3;
188	return (value >> rotate) | (value << (32 - rotate));
189}
190
191uint16_t GBALoadU16(struct ARMMemory* memory, uint32_t address, int* cycleCounter) {
192	return GBALoad16(memory, address, cycleCounter);
193}
194
195int16_t GBALoad16(struct ARMMemory* memory, uint32_t address, int* cycleCounter) {
196	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
197	int16_t value = 0;
198	int wait = 0;
199
200	switch (address & ~OFFSET_MASK) {
201	case BASE_BIOS:
202		if (gbaMemory->p->cpu.currentPC >> BASE_OFFSET == REGION_BIOS) {
203			if (address < SIZE_BIOS) {
204				value = ((int16_t*) gbaMemory->bios)[address >> 1];
205			} else {
206				value = 0;
207			}
208		} else {
209			value = gbaMemory->biosPrefetch;
210		}
211		break;
212	case BASE_WORKING_RAM:
213		value = ((int16_t*) gbaMemory->wram)[(address & (SIZE_WORKING_RAM - 1)) >> 1];
214		wait = gbaMemory->waitstates16[REGION_WORKING_RAM];
215		break;
216	case BASE_WORKING_IRAM:
217		value = ((int16_t*) gbaMemory->iwram)[(address & (SIZE_WORKING_IRAM - 1)) >> 1];
218		break;
219	case BASE_IO:
220		value = GBAIORead(gbaMemory->p, address & (SIZE_IO - 1));
221		break;
222	case BASE_PALETTE_RAM:
223		value = gbaMemory->p->video.palette[(address & (SIZE_PALETTE_RAM - 1)) >> 1];
224		break;
225	case BASE_VRAM:
226		value = gbaMemory->p->video.renderer->vram[(address & 0x0001FFFF) >> 1];
227		break;
228	case BASE_OAM:
229		value = gbaMemory->p->video.oam.raw[(address & (SIZE_OAM - 1)) >> 1];
230		break;
231	case BASE_CART0:
232	case BASE_CART0_EX:
233	case BASE_CART1:
234	case BASE_CART1_EX:
235	case BASE_CART2:
236		wait = gbaMemory->waitstates16[address >> BASE_OFFSET];
237		if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
238			value = ((int16_t*) gbaMemory->rom)[(address & (SIZE_CART0 - 1)) >> 1];
239		}
240		break;
241	case BASE_CART2_EX:
242		wait = gbaMemory->waitstates16[address >> BASE_OFFSET];
243		if (gbaMemory->savedata.type == SAVEDATA_EEPROM) {
244			value = GBASavedataReadEEPROM(&gbaMemory->savedata);
245		} else if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
246			value = ((uint16_t*) gbaMemory->rom)[(address & (SIZE_CART0 - 1)) >> 1];
247		}
248		break;
249	case BASE_CART_SRAM:
250		GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Load16: 0x%08X", address);
251		break;
252	default:
253		GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Load16: 0x%08X", address);
254		value = memory->load16(memory, gbaMemory->p->cpu.currentPC + (gbaMemory->p->cpu.executionMode == MODE_ARM ? WORD_SIZE_ARM : WORD_SIZE_THUMB) * 2, 0);
255		break;
256	}
257
258	if (cycleCounter) {
259		*cycleCounter += 2 + wait;
260	}
261	// Unaligned 16-bit loads are "unpredictable", but the GBA rotates them, so we have to, too.
262	int rotate = (address & 1) << 3;
263	return (value >> rotate) | ((value << rotate) & 0xFF00);
264}
265
266uint8_t GBALoadU8(struct ARMMemory* memory, uint32_t address, int* cycleCounter) {
267	return GBALoad8(memory, address, cycleCounter);
268}
269
270int8_t GBALoad8(struct ARMMemory* memory, uint32_t address, int* cycleCounter) {
271	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
272	int8_t value = 0;
273	int wait = 0;
274
275	switch (address & ~OFFSET_MASK) {
276	case BASE_BIOS:
277		if (gbaMemory->p->cpu.currentPC >> BASE_OFFSET == REGION_BIOS) {
278			if (address < SIZE_BIOS) {
279				value = ((int8_t*) gbaMemory->bios)[address];
280			} else {
281				value = 0;
282			}
283		} else {
284			value = gbaMemory->biosPrefetch;
285		}
286		break;
287	case BASE_WORKING_RAM:
288		value = ((int8_t*) gbaMemory->wram)[address & (SIZE_WORKING_RAM - 1)];
289		wait = gbaMemory->waitstates16[REGION_WORKING_RAM];
290		break;
291	case BASE_WORKING_IRAM:
292		value = ((int8_t*) gbaMemory->iwram)[address & (SIZE_WORKING_IRAM - 1)];
293		break;
294	case BASE_IO:
295		value = (GBAIORead(gbaMemory->p, address & 0xFFFE) >> ((address & 0x0001) << 3)) & 0xFF;
296		break;
297	case BASE_PALETTE_RAM:
298		value = ((int8_t*) gbaMemory->p->video.renderer->palette)[address & (SIZE_PALETTE_RAM - 1)];
299		break;
300	case BASE_VRAM:
301		value = ((int8_t*) gbaMemory->p->video.renderer->vram)[address & 0x0001FFFF];
302		break;
303	case BASE_OAM:
304		GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Load8: 0x%08X", address);
305		break;
306	case BASE_CART0:
307	case BASE_CART0_EX:
308	case BASE_CART1:
309	case BASE_CART1_EX:
310	case BASE_CART2:
311	case BASE_CART2_EX:
312		wait = gbaMemory->waitstates16[address >> BASE_OFFSET];
313		if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
314			value = ((int8_t*) gbaMemory->rom)[address & (SIZE_CART0 - 1)];
315		}
316		break;
317	case BASE_CART_SRAM:
318		wait = gbaMemory->waitstates16[address >> BASE_OFFSET];
319		if (gbaMemory->savedata.type == SAVEDATA_NONE) {
320			GBASavedataInit(&gbaMemory->savedata, gbaMemory->p->savefile);
321			GBASavedataInitSRAM(&gbaMemory->savedata);
322		} else if (gbaMemory->savedata.type == SAVEDATA_SRAM) {
323			value = gbaMemory->savedata.data[address & (SIZE_CART_SRAM - 1)];
324		} else if (gbaMemory->savedata.type == SAVEDATA_FLASH512 || gbaMemory->savedata.type == SAVEDATA_FLASH1M) {
325			value = GBASavedataReadFlash(&gbaMemory->savedata, address);
326		}
327		break;
328	default:
329		GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Load8: 0x%08x", address);
330		value = memory->load16(memory, gbaMemory->p->cpu.currentPC + (gbaMemory->p->cpu.executionMode == MODE_ARM ? WORD_SIZE_ARM : WORD_SIZE_THUMB) * 2, 0) >> ((address & 1) << 3);
331		break;
332	}
333
334	if (cycleCounter) {
335		*cycleCounter += 2 + wait;
336	}
337	return value;
338}
339
340void GBAStore32(struct ARMMemory* memory, uint32_t address, int32_t value, int* cycleCounter) {
341	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
342	int wait = 0;
343
344	switch (address & ~OFFSET_MASK) {
345	case BASE_WORKING_RAM:
346		gbaMemory->wram[(address & (SIZE_WORKING_RAM - 1)) >> 2] = value;
347		wait = gbaMemory->waitstates32[REGION_WORKING_RAM];
348		break;
349	case BASE_WORKING_IRAM:
350		gbaMemory->iwram[(address & (SIZE_WORKING_IRAM - 1)) >> 2] = value;
351		break;
352	case BASE_IO:
353		GBAIOWrite32(gbaMemory->p, address & (SIZE_IO - 1), value);
354		break;
355	case BASE_PALETTE_RAM:
356		((int32_t*) gbaMemory->p->video.palette)[(address & (SIZE_PALETTE_RAM - 1)) >> 2] = value;
357		gbaMemory->p->video.renderer->writePalette(gbaMemory->p->video.renderer, (address & (SIZE_PALETTE_RAM - 1)) + 2, value >> 16);
358		gbaMemory->p->video.renderer->writePalette(gbaMemory->p->video.renderer, address & (SIZE_PALETTE_RAM - 1), value);
359		break;
360	case BASE_VRAM:
361		if ((address & OFFSET_MASK) < SIZE_VRAM - 2) {
362			((int32_t*) gbaMemory->p->video.renderer->vram)[(address & 0x0001FFFF) >> 2] = value;
363		}
364		break;
365	case BASE_OAM:
366		((int32_t*) gbaMemory->p->video.oam.raw)[(address & (SIZE_OAM - 1)) >> 2] = value;
367		gbaMemory->p->video.renderer->writeOAM(gbaMemory->p->video.renderer, (address & (SIZE_OAM - 4)) >> 1);
368		gbaMemory->p->video.renderer->writeOAM(gbaMemory->p->video.renderer, ((address & (SIZE_OAM - 4)) >> 1) + 1);
369		break;
370	case BASE_CART0:
371		GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store32: 0x%08X", address);
372		break;
373	case BASE_CART_SRAM:
374		GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store32: 0x%08X", address);
375		break;
376	default:
377		GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Store32: 0x%08X", address);
378		break;
379	}
380
381	if (cycleCounter) {
382		*cycleCounter += 1 + wait;
383	}
384}
385
386void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value, int* cycleCounter) {
387	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
388	int wait = 0;
389
390	switch (address & ~OFFSET_MASK) {
391	case BASE_WORKING_RAM:
392		((int16_t*) gbaMemory->wram)[(address & (SIZE_WORKING_RAM - 1)) >> 1] = value;
393		wait = gbaMemory->waitstates16[REGION_WORKING_RAM];
394		break;
395	case BASE_WORKING_IRAM:
396		((int16_t*) gbaMemory->iwram)[(address & (SIZE_WORKING_IRAM - 1)) >> 1] = value;
397		break;
398	case BASE_IO:
399		GBAIOWrite(gbaMemory->p, address & (SIZE_IO - 1), value);
400		break;
401	case BASE_PALETTE_RAM:
402		gbaMemory->p->video.palette[(address & (SIZE_PALETTE_RAM - 1)) >> 1] = value;
403		gbaMemory->p->video.renderer->writePalette(gbaMemory->p->video.renderer, address & (SIZE_PALETTE_RAM - 1), value);
404		break;
405	case BASE_VRAM:
406		if ((address & OFFSET_MASK) < SIZE_VRAM) {
407			gbaMemory->p->video.renderer->vram[(address & 0x0001FFFF) >> 1] = value;
408		}
409		break;
410	case BASE_OAM:
411		gbaMemory->p->video.oam.raw[(address & (SIZE_OAM - 1)) >> 1] = value;
412		gbaMemory->p->video.renderer->writeOAM(gbaMemory->p->video.renderer, (address & (SIZE_OAM - 1)) >> 1);
413		break;
414	case BASE_CART0:
415		GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store16: 0x%08X", address);
416		break;
417	case BASE_CART2_EX:
418		if (gbaMemory->savedata.type == SAVEDATA_NONE) {
419			GBASavedataInit(&gbaMemory->savedata, gbaMemory->p->savefile);
420			GBASavedataInitEEPROM(&gbaMemory->savedata);
421		}
422		GBASavedataWriteEEPROM(&gbaMemory->savedata, value, 1);
423		break;
424	case BASE_CART_SRAM:
425		GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store16: 0x%08X", address);
426		break;
427	default:
428		GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Store16: 0x%08X", address);
429		break;
430	}
431
432	if (cycleCounter) {
433		*cycleCounter += 1 + wait;
434	}
435}
436
437void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value, int* cycleCounter) {
438	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
439	int wait = 0;
440
441	switch (address & ~OFFSET_MASK) {
442	case BASE_WORKING_RAM:
443		((int8_t*) gbaMemory->wram)[address & (SIZE_WORKING_RAM - 1)] = value;
444		wait = gbaMemory->waitstates16[REGION_WORKING_RAM];
445		break;
446	case BASE_WORKING_IRAM:
447		((int8_t*) gbaMemory->iwram)[address & (SIZE_WORKING_IRAM - 1)] = value;
448		break;
449	case BASE_IO:
450		GBAIOWrite8(gbaMemory->p, address & (SIZE_IO - 1), value);
451		break;
452	case BASE_PALETTE_RAM:
453		GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store8: 0x%08X", address);
454		break;
455	case BASE_VRAM:
456		if (address >= 0x06018000) {
457			// TODO: check BG mode
458			GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Cannot Store8 to OBJ: 0x%08X", address);
459			break;
460		}
461		gbaMemory->p->video.renderer->vram[(address >> 1) & 0xFFFF] = (value) | (value << 8);
462		break;
463	case BASE_OAM:
464		GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Cannot Store8 to OAM: 0x%08X", address);
465		break;
466	case BASE_CART0:
467		GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store8: 0x%08X", address);
468		break;
469	case BASE_CART_SRAM:
470		if (gbaMemory->savedata.type == SAVEDATA_NONE) {
471			GBASavedataInit(&gbaMemory->savedata, gbaMemory->p->savefile);
472			if (address == SAVEDATA_FLASH_BASE) {
473				GBASavedataInitFlash(&gbaMemory->savedata);
474			} else {
475				GBASavedataInitSRAM(&gbaMemory->savedata);
476			}
477		}
478		if (gbaMemory->savedata.type == SAVEDATA_FLASH512 || gbaMemory->savedata.type == SAVEDATA_FLASH1M) {
479			GBASavedataWriteFlash(&gbaMemory->savedata, address, value);
480		} else if (gbaMemory->savedata.type == SAVEDATA_SRAM) {
481			gbaMemory->savedata.data[address & (SIZE_CART_SRAM - 1)] = value;
482		}
483		wait = gbaMemory->waitstates16[REGION_CART_SRAM];
484		break;
485	default:
486		GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Store8: 0x%08X", address);
487		break;
488	}
489
490	if (cycleCounter) {
491		*cycleCounter += 1 + wait;
492	}
493}
494
495static int GBAWaitMultiple(struct ARMMemory* memory, uint32_t startAddress, int count) {
496	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
497	int wait = 1 + gbaMemory->waitstates32[startAddress >> BASE_OFFSET];
498	wait += (1 + gbaMemory->waitstatesSeq32[startAddress >> BASE_OFFSET]) * (count - 1);
499	return wait;
500}
501
502void GBAAdjustWaitstates(struct GBAMemory* memory, uint16_t parameters) {
503	int sram = parameters & 0x0003;
504	int ws0 = (parameters & 0x000C) >> 2;
505	int ws0seq = (parameters & 0x0010) >> 4;
506	int ws1 = (parameters & 0x0060) >> 5;
507	int ws1seq = (parameters & 0x0080) >> 7;
508	int ws2 = (parameters & 0x0300) >> 8;
509	int ws2seq = (parameters & 0x0400) >> 10;
510	int prefetch = parameters & 0x4000;
511
512	memory->waitstates16[REGION_CART_SRAM] =  GBA_ROM_WAITSTATES[sram];
513	memory->waitstatesSeq16[REGION_CART_SRAM] = GBA_ROM_WAITSTATES[sram];
514	memory->waitstates32[REGION_CART_SRAM] = 2 * GBA_ROM_WAITSTATES[sram] + 1;
515	memory->waitstatesSeq32[REGION_CART_SRAM] = 2 * GBA_ROM_WAITSTATES[sram] + 1;
516
517	memory->waitstates16[REGION_CART0] = memory->waitstates16[REGION_CART0_EX] = GBA_ROM_WAITSTATES[ws0];
518	memory->waitstates16[REGION_CART1] = memory->waitstates16[REGION_CART1_EX] = GBA_ROM_WAITSTATES[ws1];
519	memory->waitstates16[REGION_CART2] = memory->waitstates16[REGION_CART2_EX] = GBA_ROM_WAITSTATES[ws2];
520
521	memory->waitstatesSeq16[REGION_CART0] = memory->waitstatesSeq16[REGION_CART0_EX] = GBA_ROM_WAITSTATES_SEQ[ws0seq];
522	memory->waitstatesSeq16[REGION_CART1] = memory->waitstatesSeq16[REGION_CART1_EX] = GBA_ROM_WAITSTATES_SEQ[ws1seq + 2];
523	memory->waitstatesSeq16[REGION_CART2] = memory->waitstatesSeq16[REGION_CART2_EX] = GBA_ROM_WAITSTATES_SEQ[ws2seq + 4];
524
525	memory->waitstates32[REGION_CART0] = memory->waitstates32[REGION_CART0_EX] = memory->waitstates16[REGION_CART0] + 1 + memory->waitstatesSeq16[REGION_CART0];
526	memory->waitstates32[REGION_CART1] = memory->waitstates32[REGION_CART1_EX] = memory->waitstates16[REGION_CART1] + 1 + memory->waitstatesSeq16[REGION_CART1];
527	memory->waitstates32[REGION_CART2] = memory->waitstates32[REGION_CART2_EX] = memory->waitstates16[REGION_CART2] + 1 + memory->waitstatesSeq16[REGION_CART2];
528
529	memory->waitstatesSeq32[REGION_CART0] = memory->waitstatesSeq32[REGION_CART0_EX] = 2 * memory->waitstatesSeq16[REGION_CART0] + 1;
530	memory->waitstatesSeq32[REGION_CART1] = memory->waitstatesSeq32[REGION_CART1_EX] = 2 * memory->waitstatesSeq16[REGION_CART1] + 1;
531	memory->waitstatesSeq32[REGION_CART2] = memory->waitstatesSeq32[REGION_CART2_EX] = 2 * memory->waitstatesSeq16[REGION_CART2] + 1;
532
533	if (!prefetch) {
534		memory->waitstatesPrefetch16[REGION_CART0] = memory->waitstatesPrefetch16[REGION_CART0_EX] = memory->waitstatesSeq16[REGION_CART0];
535		memory->waitstatesPrefetch16[REGION_CART1] = memory->waitstatesPrefetch16[REGION_CART1_EX] = memory->waitstatesSeq16[REGION_CART1];
536		memory->waitstatesPrefetch16[REGION_CART2] = memory->waitstatesPrefetch16[REGION_CART2_EX] = memory->waitstatesSeq16[REGION_CART2];
537
538		memory->waitstatesPrefetch32[REGION_CART0] = memory->waitstatesPrefetch32[REGION_CART0_EX] = memory->waitstatesSeq32[REGION_CART0];
539		memory->waitstatesPrefetch32[REGION_CART1] = memory->waitstatesPrefetch32[REGION_CART1_EX] = memory->waitstatesSeq32[REGION_CART1];
540		memory->waitstatesPrefetch32[REGION_CART2] = memory->waitstatesPrefetch32[REGION_CART2_EX] = memory->waitstatesSeq32[REGION_CART2];
541	} else {
542		memory->waitstatesPrefetch16[REGION_CART0] = memory->waitstatesPrefetch16[REGION_CART0_EX] = 0;
543		memory->waitstatesPrefetch16[REGION_CART1] = memory->waitstatesPrefetch16[REGION_CART1_EX] = 0;
544		memory->waitstatesPrefetch16[REGION_CART2] = memory->waitstatesPrefetch16[REGION_CART2_EX] = 0;
545
546		memory->waitstatesPrefetch32[REGION_CART0] = memory->waitstatesPrefetch32[REGION_CART0_EX] = 0;
547		memory->waitstatesPrefetch32[REGION_CART1] = memory->waitstatesPrefetch32[REGION_CART1_EX] = 0;
548		memory->waitstatesPrefetch32[REGION_CART2] = memory->waitstatesPrefetch32[REGION_CART2_EX] = 0;
549	}
550
551	memory->d.activePrefetchCycles32 = memory->waitstatesPrefetch32[memory->activeRegion];
552	memory->d.activePrefetchCycles16 = memory->waitstatesPrefetch16[memory->activeRegion];
553	memory->d.activeNonseqCycles32 = memory->waitstates32[memory->activeRegion];
554	memory->d.activeNonseqCycles16 = memory->waitstates16[memory->activeRegion];
555}
556
557int32_t GBAMemoryProcessEvents(struct GBAMemory* memory, int32_t cycles) {
558	struct GBADMA* dma;
559	int32_t test = INT_MAX;
560
561	dma = &memory->dma[0];
562	dma->nextIRQ -= cycles;
563	if (dma->enable && dma->doIrq && dma->nextIRQ) {
564		if (dma->nextIRQ <= 0) {
565			dma->nextIRQ = INT_MAX;
566			GBARaiseIRQ(memory->p, IRQ_DMA0);
567		} else if (dma->nextIRQ < test) {
568			test = dma->nextIRQ;
569		}
570	}
571
572	dma = &memory->dma[1];
573	dma->nextIRQ -= cycles;
574	if (dma->enable && dma->doIrq && dma->nextIRQ) {
575		if (dma->nextIRQ <= 0) {
576			dma->nextIRQ = INT_MAX;
577			GBARaiseIRQ(memory->p, IRQ_DMA1);
578		} else if (dma->nextIRQ < test) {
579			test = dma->nextIRQ;
580		}
581	}
582
583	dma = &memory->dma[2];
584	dma->nextIRQ -= cycles;
585	if (dma->enable && dma->doIrq && dma->nextIRQ) {
586		if (dma->nextIRQ <= 0) {
587			dma->nextIRQ = INT_MAX;
588			GBARaiseIRQ(memory->p, IRQ_DMA2);
589		} else if (dma->nextIRQ < test) {
590			test = dma->nextIRQ;
591		}
592	}
593
594	dma = &memory->dma[3];
595	dma->nextIRQ -= cycles;
596	if (dma->enable && dma->doIrq && dma->nextIRQ) {
597		if (dma->nextIRQ <= 0) {
598			dma->nextIRQ = INT_MAX;
599			GBARaiseIRQ(memory->p, IRQ_DMA3);
600		} else if (dma->nextIRQ < test) {
601			test = dma->nextIRQ;
602		}
603	}
604
605	return test;
606}
607
608void GBAMemoryWriteDMASAD(struct GBAMemory* memory, int dma, uint32_t address) {
609	memory->dma[dma].source = address & 0xFFFFFFFE;
610}
611
612void GBAMemoryWriteDMADAD(struct GBAMemory* memory, int dma, uint32_t address) {
613	memory->dma[dma].dest = address & 0xFFFFFFFE;
614}
615
616void GBAMemoryWriteDMACNT_LO(struct GBAMemory* memory, int dma, uint16_t count) {
617	memory->dma[dma].count = count ? count : (dma == 3 ? 0x10000 : 0x4000);
618}
619
620uint16_t GBAMemoryWriteDMACNT_HI(struct GBAMemory* memory, int dma, uint16_t control) {
621	struct GBADMA* currentDma = &memory->dma[dma];
622	int wasEnabled = currentDma->enable;
623	currentDma->packed = control;
624	currentDma->nextIRQ = 0;
625
626	if (currentDma->drq) {
627		GBALog(memory->p, GBA_LOG_STUB, "DRQ not implemented");
628	}
629
630	if (!wasEnabled && currentDma->enable) {
631		currentDma->nextSource = currentDma->source;
632		currentDma->nextDest = currentDma->dest;
633		currentDma->nextCount = currentDma->count;
634		GBAMemoryScheduleDMA(memory, dma, currentDma);
635	}
636	// If the DMA has already occurred, this value might have changed since the function started
637	return currentDma->packed;
638};
639
640void GBAMemoryScheduleDMA(struct GBAMemory* memory, int number, struct GBADMA* info) {
641	switch (info->timing) {
642	case DMA_TIMING_NOW:
643		GBAMemoryServiceDMA(memory, number, info);
644		break;
645	case DMA_TIMING_HBLANK:
646		// Handled implicitly
647		break;
648	case DMA_TIMING_VBLANK:
649		// Handled implicitly
650		break;
651	case DMA_TIMING_CUSTOM:
652		switch (number) {
653		case 0:
654			GBALog(memory->p, GBA_LOG_WARN, "Discarding invalid DMA0 scheduling");
655			break;
656		case 1:
657		case 2:
658			GBAAudioScheduleFifoDma(&memory->p->audio, number, info);
659			break;
660		case 3:
661			//this.cpu.irq.video.scheduleVCaptureDma(dma, info);
662			break;
663		}
664	}
665}
666
667void GBAMemoryRunHblankDMAs(struct GBAMemory* memory) {
668	struct GBADMA* dma;
669	int i;
670	for (i = 0; i < 4; ++i) {
671		dma = &memory->dma[i];
672		if (dma->enable && dma->timing == DMA_TIMING_HBLANK) {
673			GBAMemoryServiceDMA(memory, i, dma);
674		}
675	}
676}
677
678void GBAMemoryRunVblankDMAs(struct GBAMemory* memory) {
679	struct GBADMA* dma;
680	int i;
681	for (i = 0; i < 4; ++i) {
682		dma = &memory->dma[i];
683		if (dma->enable && dma->timing == DMA_TIMING_VBLANK) {
684			GBAMemoryServiceDMA(memory, i, dma);
685		}
686	}
687}
688
689void GBAMemoryServiceDMA(struct GBAMemory* memory, int number, struct GBADMA* info) {
690	if (!info->enable) {
691		// There was a DMA scheduled that got canceled
692		return;
693	}
694
695	uint32_t width = info->width ? 4 : 2;
696	int sourceOffset = DMA_OFFSET[info->srcControl] * width;
697	int destOffset = DMA_OFFSET[info->dstControl] * width;
698	int32_t wordsRemaining = info->nextCount;
699	uint32_t source = info->nextSource;
700	uint32_t dest = info->nextDest;
701	uint32_t sourceRegion = source >> BASE_OFFSET;
702	uint32_t destRegion = dest >> BASE_OFFSET;
703
704	if (width == 4) {
705		int32_t word;
706		source &= 0xFFFFFFFC;
707		dest &= 0xFFFFFFFC;
708		while (wordsRemaining--) {
709			word = memory->d.load32(&memory->d, source, 0);
710			memory->d.store32(&memory->d, dest, word, 0);
711			source += sourceOffset;
712			dest += destOffset;
713		}
714	} else {
715		uint16_t word;
716		if (sourceRegion == REGION_CART2_EX && memory->savedata.type == SAVEDATA_EEPROM) {
717			while (wordsRemaining--) {
718				word = GBASavedataReadEEPROM(&memory->savedata);
719				memory->d.store16(&memory->d, dest, word, 0);
720				source += sourceOffset;
721				dest += destOffset;
722			}
723		} else if (destRegion == REGION_CART2_EX) {
724			if (memory->savedata.type == SAVEDATA_NONE) {
725				GBASavedataInit(&memory->savedata, memory->p->savefile);
726				GBASavedataInitEEPROM(&memory->savedata);
727			}
728			while (wordsRemaining) {
729				word = memory->d.load16(&memory->d, source, 0);
730				GBASavedataWriteEEPROM(&memory->savedata, word, wordsRemaining);
731				source += sourceOffset;
732				dest += destOffset;
733				--wordsRemaining;
734			}
735		} else {
736			while (wordsRemaining--) {
737				word = memory->d.load16(&memory->d, source, 0);
738				memory->d.store16(&memory->d, dest, word, 0);
739				source += sourceOffset;
740				dest += destOffset;
741			}
742		}
743	}
744
745	if (info->doIrq) {
746		info->nextIRQ = memory->p->cpu.cycles + 2;
747		info->nextIRQ += (width == 4 ? memory->waitstates32[sourceRegion] + memory->waitstates32[destRegion]
748		                            : memory->waitstates16[sourceRegion] + memory->waitstates16[destRegion]);
749		info->nextIRQ += (info->count - 1) * (width == 4 ? memory->waitstatesSeq32[sourceRegion] + memory->waitstatesSeq32[destRegion]
750		                                               : memory->waitstatesSeq16[sourceRegion] + memory->waitstatesSeq16[destRegion]);
751	}
752
753	info->nextSource = source;
754	info->nextDest = dest;
755	info->nextCount = wordsRemaining;
756
757	if (!info->repeat) {
758		info->enable = 0;
759
760		// Clear the enable bit in memory
761		memory->io[(REG_DMA0CNT_HI + number * (REG_DMA1CNT_HI - REG_DMA0CNT_HI)) >> 1] &= 0x7FE0;
762	} else {
763		info->nextCount = info->count;
764		if (info->dstControl == DMA_INCREMENT_RELOAD) {
765			info->nextDest = info->dest;
766		}
767		GBAMemoryScheduleDMA(memory, number, info);
768	}
769}