all repos — mgba @ cd448324c5f837ff3eab5b87bda66bbaa44a7be6

mGBA Game Boy Advance Emulator

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

  1#include "gba-memory.h"
  2
  3#include "gba-gpio.h"
  4#include "gba-io.h"
  5#include "hle-bios.h"
  6
  7#include <limits.h>
  8#include <string.h>
  9#include <sys/mman.h>
 10
 11static void GBASetActiveRegion(struct ARMMemory* memory, uint32_t region);
 12static int GBAWaitMultiple(struct ARMMemory* memory, uint32_t startAddress, int count);
 13
 14static const char GBA_BASE_WAITSTATES[16] = { 0, 0, 2, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4 };
 15static const char GBA_BASE_WAITSTATES_32[16] = { 0, 0, 5, 0, 0, 0, 0, 0, 7, 7, 9, 9, 13, 13, 9 };
 16static const char GBA_BASE_WAITSTATES_SEQ[16] = { 0, 0, 2, 0, 0, 0, 0, 0, 2, 2, 4, 4, 8, 8, 4 };
 17static const char GBA_BASE_WAITSTATES_SEQ_32[16] = { 0, 0, 5, 0, 0, 0, 0, 0, 5, 5, 9, 9, 17, 17, 9 };
 18static const char GBA_ROM_WAITSTATES[] = { 4, 3, 2, 8 };
 19static const char GBA_ROM_WAITSTATES_SEQ[] = { 2, 1, 4, 1, 8, 1 };
 20static const int DMA_OFFSET[] = { 1, -1, 0, 1 };
 21
 22void GBAMemoryInit(struct GBAMemory* memory) {
 23	memory->d.load32 = GBALoad32;
 24	memory->d.load16 = GBALoad16;
 25	memory->d.loadU16 = GBALoadU16;
 26	memory->d.load8 = GBALoad8;
 27	memory->d.loadU8 = GBALoadU8;
 28	memory->d.store32 = GBAStore32;
 29	memory->d.store16 = GBAStore16;
 30	memory->d.store8 = GBAStore8;
 31
 32	memory->bios = (uint32_t*) hleBios;
 33	memory->fullBios = 0;
 34	memory->wram = mmap(0, SIZE_WORKING_RAM, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
 35	memory->iwram = mmap(0, SIZE_WORKING_IRAM, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
 36	memory->rom = 0;
 37	memory->gpio.p = memory->p;
 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		GBALog(memory->p, GBA_LOG_ERROR, "Could not map memory");
 44		return;
 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				LOAD_32(value, address, gbaMemory->bios);
131			} else {
132				value = 0;
133			}
134		} else {
135			value = gbaMemory->biosPrefetch;
136		}
137		break;
138	case BASE_WORKING_RAM:
139		LOAD_32(value, address & (SIZE_WORKING_RAM - 1), gbaMemory->wram);
140		wait = gbaMemory->waitstates32[REGION_WORKING_RAM];
141		break;
142	case BASE_WORKING_IRAM:
143		LOAD_32(value, address & (SIZE_WORKING_IRAM - 1), gbaMemory->iwram);
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		LOAD_32(value, address & (SIZE_PALETTE_RAM - 1), gbaMemory->p->video.palette);
150		break;
151	case BASE_VRAM:
152		LOAD_32(value, address & 0x0001FFFF, gbaMemory->p->video.renderer->vram);
153		break;
154	case BASE_OAM:
155		LOAD_32(value, address & (SIZE_OAM - 1), gbaMemory->p->video.oam.raw);
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			LOAD_32(value, address & (SIZE_CART0 - 1), gbaMemory->rom);
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				LOAD_16(value, address, gbaMemory->bios);
205			} else {
206				value = 0;
207			}
208		} else {
209			value = gbaMemory->biosPrefetch;
210		}
211		break;
212	case BASE_WORKING_RAM:
213		LOAD_16(value, address & (SIZE_WORKING_RAM - 1), gbaMemory->wram);
214		wait = gbaMemory->waitstates16[REGION_WORKING_RAM];
215		break;
216	case BASE_WORKING_IRAM:
217		LOAD_16(value, address & (SIZE_WORKING_IRAM - 1), gbaMemory->iwram);
218		break;
219	case BASE_IO:
220		value = GBAIORead(gbaMemory->p, address & (SIZE_IO - 1));
221		break;
222	case BASE_PALETTE_RAM:
223		LOAD_16(value, address & (SIZE_PALETTE_RAM - 1), gbaMemory->p->video.palette);
224		break;
225	case BASE_VRAM:
226		LOAD_16(value, address & 0x0001FFFF, gbaMemory->p->video.renderer->vram);
227		break;
228	case BASE_OAM:
229		LOAD_16(value, address & (SIZE_OAM - 1), gbaMemory->p->video.oam.raw);
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			LOAD_16(value, address & (SIZE_CART0 - 1), gbaMemory->rom);
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			LOAD_16(value, address & (SIZE_CART0 - 1), gbaMemory->rom);
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			GBASavedataInitSRAM(&gbaMemory->savedata);
321		}
322		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		STORE_32(value, address & (SIZE_WORKING_RAM - 1), gbaMemory->wram);
347		wait = gbaMemory->waitstates32[REGION_WORKING_RAM];
348		break;
349	case BASE_WORKING_IRAM:
350		STORE_32(value, address & (SIZE_WORKING_IRAM - 1), gbaMemory->iwram);
351		break;
352	case BASE_IO:
353		GBAIOWrite32(gbaMemory->p, address & (SIZE_IO - 1), value);
354		break;
355	case BASE_PALETTE_RAM:
356		STORE_32(value, address & (SIZE_PALETTE_RAM - 1), gbaMemory->p->video.palette);
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			STORE_32(value, address & 0x0001FFFF, gbaMemory->p->video.renderer->vram);
363		}
364		break;
365	case BASE_OAM:
366		STORE_32(value, address & (SIZE_OAM - 1), gbaMemory->p->video.oam.raw);
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		STORE_16(value, address & (SIZE_WORKING_RAM - 1), gbaMemory->wram);
393		wait = gbaMemory->waitstates16[REGION_WORKING_RAM];
394		break;
395	case BASE_WORKING_IRAM:
396		STORE_16(value, address & (SIZE_WORKING_IRAM - 1), gbaMemory->iwram);
397		break;
398	case BASE_IO:
399		GBAIOWrite(gbaMemory->p, address & (SIZE_IO - 1), value);
400		break;
401	case BASE_PALETTE_RAM:
402		STORE_16(value, address & (SIZE_PALETTE_RAM - 1), gbaMemory->p->video.palette);
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			STORE_16(value, address & 0x0001FFFF, gbaMemory->p->video.renderer->vram);
408		}
409		break;
410	case BASE_OAM:
411		STORE_16(value, address & (SIZE_OAM - 1), gbaMemory->p->video.oam.raw);
412		gbaMemory->p->video.renderer->writeOAM(gbaMemory->p->video.renderer, (address & (SIZE_OAM - 1)) >> 1);
413		break;
414	case BASE_CART0:
415		if (IS_GPIO_REGISTER(address & 0xFFFFFF)) {
416			uint32_t reg = address & 0xFFFFFF;
417			GBAGPIOWrite(&gbaMemory->gpio, reg, value);
418		} else {
419			GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad cartridge Store16: 0x%08X", address);
420		}
421		break;
422	case BASE_CART2_EX:
423		if (gbaMemory->savedata.type == SAVEDATA_NONE) {
424			GBASavedataInitEEPROM(&gbaMemory->savedata);
425		}
426		GBASavedataWriteEEPROM(&gbaMemory->savedata, value, 1);
427		break;
428	case BASE_CART_SRAM:
429		GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store16: 0x%08X", address);
430		break;
431	default:
432		GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Store16: 0x%08X", address);
433		break;
434	}
435
436	if (cycleCounter) {
437		*cycleCounter += 1 + wait;
438	}
439}
440
441void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value, int* cycleCounter) {
442	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
443	int wait = 0;
444
445	switch (address & ~OFFSET_MASK) {
446	case BASE_WORKING_RAM:
447		((int8_t*) gbaMemory->wram)[address & (SIZE_WORKING_RAM - 1)] = value;
448		wait = gbaMemory->waitstates16[REGION_WORKING_RAM];
449		break;
450	case BASE_WORKING_IRAM:
451		((int8_t*) gbaMemory->iwram)[address & (SIZE_WORKING_IRAM - 1)] = value;
452		break;
453	case BASE_IO:
454		GBAIOWrite8(gbaMemory->p, address & (SIZE_IO - 1), value);
455		break;
456	case BASE_PALETTE_RAM:
457		GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store8: 0x%08X", address);
458		break;
459	case BASE_VRAM:
460		if (address >= 0x06018000) {
461			// TODO: check BG mode
462			GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Cannot Store8 to OBJ: 0x%08X", address);
463			break;
464		}
465		((int8_t*) gbaMemory->p->video.renderer->vram)[address & 0x1FFFE] = value;
466		((int8_t*) gbaMemory->p->video.renderer->vram)[(address & 0x1FFFE) | 1] = value;
467		break;
468	case BASE_OAM:
469		GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Cannot Store8 to OAM: 0x%08X", address);
470		break;
471	case BASE_CART0:
472		GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store8: 0x%08X", address);
473		break;
474	case BASE_CART_SRAM:
475		if (gbaMemory->savedata.type == SAVEDATA_NONE) {
476			if (address == SAVEDATA_FLASH_BASE) {
477				GBASavedataInitFlash(&gbaMemory->savedata);
478			} else {
479				GBASavedataInitSRAM(&gbaMemory->savedata);
480			}
481		}
482		if (gbaMemory->savedata.type == SAVEDATA_FLASH512 || gbaMemory->savedata.type == SAVEDATA_FLASH1M) {
483			GBASavedataWriteFlash(&gbaMemory->savedata, address, value);
484		} else if (gbaMemory->savedata.type == SAVEDATA_SRAM) {
485			gbaMemory->savedata.data[address & (SIZE_CART_SRAM - 1)] = value;
486		}
487		wait = gbaMemory->waitstates16[REGION_CART_SRAM];
488		break;
489	default:
490		GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Store8: 0x%08X", address);
491		break;
492	}
493
494	if (cycleCounter) {
495		*cycleCounter += 1 + wait;
496	}
497}
498
499static int GBAWaitMultiple(struct ARMMemory* memory, uint32_t startAddress, int count) {
500	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
501	int wait = 1 + gbaMemory->waitstates32[startAddress >> BASE_OFFSET];
502	wait += (1 + gbaMemory->waitstatesSeq32[startAddress >> BASE_OFFSET]) * (count - 1);
503	return wait;
504}
505
506void GBAAdjustWaitstates(struct GBAMemory* memory, uint16_t parameters) {
507	int sram = parameters & 0x0003;
508	int ws0 = (parameters & 0x000C) >> 2;
509	int ws0seq = (parameters & 0x0010) >> 4;
510	int ws1 = (parameters & 0x0060) >> 5;
511	int ws1seq = (parameters & 0x0080) >> 7;
512	int ws2 = (parameters & 0x0300) >> 8;
513	int ws2seq = (parameters & 0x0400) >> 10;
514	int prefetch = parameters & 0x4000;
515
516	memory->waitstates16[REGION_CART_SRAM] =  GBA_ROM_WAITSTATES[sram];
517	memory->waitstatesSeq16[REGION_CART_SRAM] = GBA_ROM_WAITSTATES[sram];
518	memory->waitstates32[REGION_CART_SRAM] = 2 * GBA_ROM_WAITSTATES[sram] + 1;
519	memory->waitstatesSeq32[REGION_CART_SRAM] = 2 * GBA_ROM_WAITSTATES[sram] + 1;
520
521	memory->waitstates16[REGION_CART0] = memory->waitstates16[REGION_CART0_EX] = GBA_ROM_WAITSTATES[ws0];
522	memory->waitstates16[REGION_CART1] = memory->waitstates16[REGION_CART1_EX] = GBA_ROM_WAITSTATES[ws1];
523	memory->waitstates16[REGION_CART2] = memory->waitstates16[REGION_CART2_EX] = GBA_ROM_WAITSTATES[ws2];
524
525	memory->waitstatesSeq16[REGION_CART0] = memory->waitstatesSeq16[REGION_CART0_EX] = GBA_ROM_WAITSTATES_SEQ[ws0seq];
526	memory->waitstatesSeq16[REGION_CART1] = memory->waitstatesSeq16[REGION_CART1_EX] = GBA_ROM_WAITSTATES_SEQ[ws1seq + 2];
527	memory->waitstatesSeq16[REGION_CART2] = memory->waitstatesSeq16[REGION_CART2_EX] = GBA_ROM_WAITSTATES_SEQ[ws2seq + 4];
528
529	memory->waitstates32[REGION_CART0] = memory->waitstates32[REGION_CART0_EX] = memory->waitstates16[REGION_CART0] + 1 + memory->waitstatesSeq16[REGION_CART0];
530	memory->waitstates32[REGION_CART1] = memory->waitstates32[REGION_CART1_EX] = memory->waitstates16[REGION_CART1] + 1 + memory->waitstatesSeq16[REGION_CART1];
531	memory->waitstates32[REGION_CART2] = memory->waitstates32[REGION_CART2_EX] = memory->waitstates16[REGION_CART2] + 1 + memory->waitstatesSeq16[REGION_CART2];
532
533	memory->waitstatesSeq32[REGION_CART0] = memory->waitstatesSeq32[REGION_CART0_EX] = 2 * memory->waitstatesSeq16[REGION_CART0] + 1;
534	memory->waitstatesSeq32[REGION_CART1] = memory->waitstatesSeq32[REGION_CART1_EX] = 2 * memory->waitstatesSeq16[REGION_CART1] + 1;
535	memory->waitstatesSeq32[REGION_CART2] = memory->waitstatesSeq32[REGION_CART2_EX] = 2 * memory->waitstatesSeq16[REGION_CART2] + 1;
536
537	if (!prefetch) {
538		memory->waitstatesPrefetch16[REGION_CART0] = memory->waitstatesPrefetch16[REGION_CART0_EX] = memory->waitstatesSeq16[REGION_CART0];
539		memory->waitstatesPrefetch16[REGION_CART1] = memory->waitstatesPrefetch16[REGION_CART1_EX] = memory->waitstatesSeq16[REGION_CART1];
540		memory->waitstatesPrefetch16[REGION_CART2] = memory->waitstatesPrefetch16[REGION_CART2_EX] = memory->waitstatesSeq16[REGION_CART2];
541
542		memory->waitstatesPrefetch32[REGION_CART0] = memory->waitstatesPrefetch32[REGION_CART0_EX] = memory->waitstatesSeq32[REGION_CART0];
543		memory->waitstatesPrefetch32[REGION_CART1] = memory->waitstatesPrefetch32[REGION_CART1_EX] = memory->waitstatesSeq32[REGION_CART1];
544		memory->waitstatesPrefetch32[REGION_CART2] = memory->waitstatesPrefetch32[REGION_CART2_EX] = memory->waitstatesSeq32[REGION_CART2];
545	} else {
546		memory->waitstatesPrefetch16[REGION_CART0] = memory->waitstatesPrefetch16[REGION_CART0_EX] = 0;
547		memory->waitstatesPrefetch16[REGION_CART1] = memory->waitstatesPrefetch16[REGION_CART1_EX] = 0;
548		memory->waitstatesPrefetch16[REGION_CART2] = memory->waitstatesPrefetch16[REGION_CART2_EX] = 0;
549
550		memory->waitstatesPrefetch32[REGION_CART0] = memory->waitstatesPrefetch32[REGION_CART0_EX] = 0;
551		memory->waitstatesPrefetch32[REGION_CART1] = memory->waitstatesPrefetch32[REGION_CART1_EX] = 0;
552		memory->waitstatesPrefetch32[REGION_CART2] = memory->waitstatesPrefetch32[REGION_CART2_EX] = 0;
553	}
554
555	memory->d.activePrefetchCycles32 = memory->waitstatesPrefetch32[memory->activeRegion];
556	memory->d.activePrefetchCycles16 = memory->waitstatesPrefetch16[memory->activeRegion];
557	memory->d.activeNonseqCycles32 = memory->waitstates32[memory->activeRegion];
558	memory->d.activeNonseqCycles16 = memory->waitstates16[memory->activeRegion];
559}
560
561int32_t GBAMemoryProcessEvents(struct GBAMemory* memory, int32_t cycles) {
562	struct GBADMA* dma;
563	int32_t test = INT_MAX;
564
565	dma = &memory->dma[0];
566	dma->nextIRQ -= cycles;
567	if (dma->enable && dma->doIrq && dma->nextIRQ) {
568		if (dma->nextIRQ <= 0) {
569			dma->nextIRQ = INT_MAX;
570			GBARaiseIRQ(memory->p, IRQ_DMA0);
571		} else if (dma->nextIRQ < test) {
572			test = dma->nextIRQ;
573		}
574	}
575
576	dma = &memory->dma[1];
577	dma->nextIRQ -= cycles;
578	if (dma->enable && dma->doIrq && dma->nextIRQ) {
579		if (dma->nextIRQ <= 0) {
580			dma->nextIRQ = INT_MAX;
581			GBARaiseIRQ(memory->p, IRQ_DMA1);
582		} else if (dma->nextIRQ < test) {
583			test = dma->nextIRQ;
584		}
585	}
586
587	dma = &memory->dma[2];
588	dma->nextIRQ -= cycles;
589	if (dma->enable && dma->doIrq && dma->nextIRQ) {
590		if (dma->nextIRQ <= 0) {
591			dma->nextIRQ = INT_MAX;
592			GBARaiseIRQ(memory->p, IRQ_DMA2);
593		} else if (dma->nextIRQ < test) {
594			test = dma->nextIRQ;
595		}
596	}
597
598	dma = &memory->dma[3];
599	dma->nextIRQ -= cycles;
600	if (dma->enable && dma->doIrq && dma->nextIRQ) {
601		if (dma->nextIRQ <= 0) {
602			dma->nextIRQ = INT_MAX;
603			GBARaiseIRQ(memory->p, IRQ_DMA3);
604		} else if (dma->nextIRQ < test) {
605			test = dma->nextIRQ;
606		}
607	}
608
609	return test;
610}
611
612void GBAMemoryWriteDMASAD(struct GBAMemory* memory, int dma, uint32_t address) {
613	memory->dma[dma].source = address & 0xFFFFFFFE;
614}
615
616void GBAMemoryWriteDMADAD(struct GBAMemory* memory, int dma, uint32_t address) {
617	memory->dma[dma].dest = address & 0xFFFFFFFE;
618}
619
620void GBAMemoryWriteDMACNT_LO(struct GBAMemory* memory, int dma, uint16_t count) {
621	memory->dma[dma].count = count ? count : (dma == 3 ? 0x10000 : 0x4000);
622}
623
624uint16_t GBAMemoryWriteDMACNT_HI(struct GBAMemory* memory, int dma, uint16_t control) {
625	struct GBADMA* currentDma = &memory->dma[dma];
626	int wasEnabled = currentDma->enable;
627	currentDma->packed = control;
628	currentDma->nextIRQ = 0;
629
630	if (currentDma->drq) {
631		GBALog(memory->p, GBA_LOG_STUB, "DRQ not implemented");
632	}
633
634	if (!wasEnabled && currentDma->enable) {
635		currentDma->nextSource = currentDma->source;
636		currentDma->nextDest = currentDma->dest;
637		currentDma->nextCount = currentDma->count;
638		GBAMemoryScheduleDMA(memory, dma, currentDma);
639	}
640	// If the DMA has already occurred, this value might have changed since the function started
641	return currentDma->packed;
642};
643
644void GBAMemoryScheduleDMA(struct GBAMemory* memory, int number, struct GBADMA* info) {
645	switch (info->timing) {
646	case DMA_TIMING_NOW:
647		GBAMemoryServiceDMA(memory, number, info);
648		break;
649	case DMA_TIMING_HBLANK:
650		// Handled implicitly
651		break;
652	case DMA_TIMING_VBLANK:
653		// Handled implicitly
654		break;
655	case DMA_TIMING_CUSTOM:
656		switch (number) {
657		case 0:
658			GBALog(memory->p, GBA_LOG_WARN, "Discarding invalid DMA0 scheduling");
659			break;
660		case 1:
661		case 2:
662			GBAAudioScheduleFifoDma(&memory->p->audio, number, info);
663			break;
664		case 3:
665			//this.cpu.irq.video.scheduleVCaptureDma(dma, info);
666			break;
667		}
668	}
669}
670
671void GBAMemoryRunHblankDMAs(struct GBAMemory* memory) {
672	struct GBADMA* dma;
673	int i;
674	for (i = 0; i < 4; ++i) {
675		dma = &memory->dma[i];
676		if (dma->enable && dma->timing == DMA_TIMING_HBLANK) {
677			GBAMemoryServiceDMA(memory, i, dma);
678		}
679	}
680}
681
682void GBAMemoryRunVblankDMAs(struct GBAMemory* memory) {
683	struct GBADMA* dma;
684	int i;
685	for (i = 0; i < 4; ++i) {
686		dma = &memory->dma[i];
687		if (dma->enable && dma->timing == DMA_TIMING_VBLANK) {
688			GBAMemoryServiceDMA(memory, i, dma);
689		}
690	}
691}
692
693void GBAMemoryServiceDMA(struct GBAMemory* memory, int number, struct GBADMA* info) {
694	if (!info->enable) {
695		// There was a DMA scheduled that got canceled
696		return;
697	}
698
699	uint32_t width = info->width ? 4 : 2;
700	int sourceOffset = DMA_OFFSET[info->srcControl] * width;
701	int destOffset = DMA_OFFSET[info->dstControl] * width;
702	int32_t wordsRemaining = info->nextCount;
703	uint32_t source = info->nextSource;
704	uint32_t dest = info->nextDest;
705	uint32_t sourceRegion = source >> BASE_OFFSET;
706	uint32_t destRegion = dest >> BASE_OFFSET;
707
708	if (width == 4) {
709		int32_t word;
710		source &= 0xFFFFFFFC;
711		dest &= 0xFFFFFFFC;
712		while (wordsRemaining--) {
713			word = memory->d.load32(&memory->d, source, 0);
714			memory->d.store32(&memory->d, dest, word, 0);
715			source += sourceOffset;
716			dest += destOffset;
717		}
718	} else {
719		uint16_t word;
720		if (sourceRegion == REGION_CART2_EX && memory->savedata.type == SAVEDATA_EEPROM) {
721			while (wordsRemaining--) {
722				word = GBASavedataReadEEPROM(&memory->savedata);
723				memory->d.store16(&memory->d, dest, word, 0);
724				source += sourceOffset;
725				dest += destOffset;
726			}
727		} else if (destRegion == REGION_CART2_EX) {
728			if (memory->savedata.type == SAVEDATA_NONE) {
729				GBASavedataInitEEPROM(&memory->savedata);
730			}
731			while (wordsRemaining) {
732				word = memory->d.load16(&memory->d, source, 0);
733				GBASavedataWriteEEPROM(&memory->savedata, word, wordsRemaining);
734				source += sourceOffset;
735				dest += destOffset;
736				--wordsRemaining;
737			}
738		} else {
739			while (wordsRemaining--) {
740				word = memory->d.load16(&memory->d, source, 0);
741				memory->d.store16(&memory->d, dest, word, 0);
742				source += sourceOffset;
743				dest += destOffset;
744			}
745		}
746	}
747
748	if (info->doIrq) {
749		info->nextIRQ = memory->p->cpu.cycles + 2;
750		info->nextIRQ += (width == 4 ? memory->waitstates32[sourceRegion] + memory->waitstates32[destRegion]
751		                            : memory->waitstates16[sourceRegion] + memory->waitstates16[destRegion]);
752		info->nextIRQ += (info->count - 1) * (width == 4 ? memory->waitstatesSeq32[sourceRegion] + memory->waitstatesSeq32[destRegion]
753		                                               : memory->waitstatesSeq16[sourceRegion] + memory->waitstatesSeq16[destRegion]);
754	}
755
756	info->nextSource = source;
757	info->nextDest = dest;
758	info->nextCount = wordsRemaining;
759
760	if (!info->repeat) {
761		info->enable = 0;
762
763		// Clear the enable bit in memory
764		memory->io[(REG_DMA0CNT_HI + number * (REG_DMA1CNT_HI - REG_DMA0CNT_HI)) >> 1] &= 0x7FE0;
765	} else {
766		info->nextCount = info->count;
767		if (info->dstControl == DMA_INCREMENT_RELOAD) {
768			info->nextDest = info->dest;
769		}
770		GBAMemoryScheduleDMA(memory, number, info);
771	}
772}