all repos — mgba @ 6a1afbda50aa57118c752a953f1e3e741991be4a

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