all repos — mgba @ 1c6fc26bf704a8a23ed9df2b92be0babb24887bf

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