all repos — mgba @ 8072ff7d2cb38aa60488ce3db932568ff8d29fc2

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	if (!rotate) {
151		return value;
152	}
153	return (value >> rotate) | (value << (32 - rotate));
154}
155
156int16_t GBALoad16(struct ARMMemory* memory, uint32_t address) {
157	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
158
159	switch (address & ~OFFSET_MASK) {
160	case BASE_BIOS:
161		break;
162	case BASE_WORKING_RAM:
163		return ((int16_t*) gbaMemory->wram)[(address & (SIZE_WORKING_RAM - 1)) >> 1];
164	case BASE_WORKING_IRAM:
165		return ((int16_t*) gbaMemory->iwram)[(address & (SIZE_WORKING_IRAM - 1)) >> 1];
166	case BASE_IO:
167		return GBAIORead(gbaMemory->p, address & (SIZE_IO - 1));
168	case BASE_PALETTE_RAM:
169		return gbaMemory->p->video.palette[(address & (SIZE_PALETTE_RAM - 1)) >> 1];
170	case BASE_VRAM:
171		return gbaMemory->p->video.vram[(address & 0x0001FFFF) >> 1];
172	case BASE_OAM:
173		break;
174	case BASE_CART0:
175	case BASE_CART0_EX:
176	case BASE_CART1:
177	case BASE_CART1_EX:
178	case BASE_CART2:
179		if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
180			return ((int16_t*) gbaMemory->rom)[(address & (SIZE_CART0 - 1)) >> 1];
181		}
182	case BASE_CART2_EX:
183		if (gbaMemory->savedata.type == SAVEDATA_EEPROM) {
184			return GBASavedataReadEEPROM(&gbaMemory->savedata);
185		} else if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
186			return ((uint16_t*) gbaMemory->rom)[(address & (SIZE_CART0 - 1)) >> 1];
187		}
188	case BASE_CART_SRAM:
189		break;
190	default:
191		break;
192	}
193
194	return 0;
195}
196
197uint16_t GBALoadU16(struct ARMMemory* memory, uint32_t address) {
198	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
199
200	switch (address & ~OFFSET_MASK) {
201	case BASE_BIOS:
202		break;
203	case BASE_WORKING_RAM:
204		return ((uint16_t*) gbaMemory->wram)[(address & (SIZE_WORKING_RAM - 1)) >> 1];
205	case BASE_WORKING_IRAM:
206		return ((uint16_t*) gbaMemory->iwram)[(address & (SIZE_WORKING_IRAM - 1)) >> 1];
207	case BASE_IO:
208		return GBAIORead(gbaMemory->p, address & (SIZE_IO - 1));
209	case BASE_PALETTE_RAM:
210		return gbaMemory->p->video.palette[(address & (SIZE_PALETTE_RAM - 1)) >> 1];
211	case BASE_VRAM:
212		return gbaMemory->p->video.vram[(address & 0x0001FFFF) >> 1];
213	case BASE_OAM:
214		break;
215	case BASE_CART0:
216	case BASE_CART0_EX:
217	case BASE_CART1:
218	case BASE_CART1_EX:
219	case BASE_CART2:
220		if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
221			return ((uint16_t*) gbaMemory->rom)[(address & (SIZE_CART0 - 1)) >> 1];
222		}
223	case BASE_CART2_EX:
224		if (gbaMemory->savedata.type == SAVEDATA_EEPROM) {
225			return GBASavedataReadEEPROM(&gbaMemory->savedata);
226		} else if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
227			return ((uint16_t*) gbaMemory->rom)[(address & (SIZE_CART0 - 1)) >> 1];
228		}
229	case BASE_CART_SRAM:
230		break;
231	default:
232		break;
233	}
234
235	return 0;
236}
237
238int8_t GBALoad8(struct ARMMemory* memory, uint32_t address) {
239	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
240
241	switch (address & ~OFFSET_MASK) {
242	case BASE_BIOS:
243		break;
244	case BASE_WORKING_RAM:
245		return ((int8_t*) gbaMemory->wram)[address & (SIZE_WORKING_RAM - 1)];
246	case BASE_WORKING_IRAM:
247		return ((int8_t*) gbaMemory->iwram)[address & (SIZE_WORKING_IRAM - 1)];
248	case BASE_IO:
249		break;
250	case BASE_PALETTE_RAM:
251		break;
252	case BASE_VRAM:
253		break;
254	case BASE_OAM:
255		break;
256	case BASE_CART0:
257	case BASE_CART0_EX:
258	case BASE_CART1:
259	case BASE_CART1_EX:
260	case BASE_CART2:
261	case BASE_CART2_EX:
262		if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
263			return ((int8_t*) gbaMemory->rom)[address & (SIZE_CART0 - 1)];
264		}
265	case BASE_CART_SRAM:
266		if (gbaMemory->savedata.type == SAVEDATA_NONE) {
267			GBASavedataInitSRAM(&gbaMemory->savedata);
268		}
269		return gbaMemory->savedata.data[address & (SIZE_CART_SRAM - 1)];
270	default:
271		break;
272	}
273
274	return 0;
275}
276
277uint8_t GBALoadU8(struct ARMMemory* memory, uint32_t address) {
278	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
279
280	switch (address & ~OFFSET_MASK) {
281	case BASE_BIOS:
282		break;
283	case BASE_WORKING_RAM:
284		return ((uint8_t*) gbaMemory->wram)[address & (SIZE_WORKING_RAM - 1)];
285		break;
286	case BASE_WORKING_IRAM:
287		return ((uint8_t*) gbaMemory->iwram)[address & (SIZE_WORKING_IRAM - 1)];
288		break;
289	case BASE_IO:
290		return (GBAIORead(gbaMemory->p, address & 0xFFFE) >> ((address & 0x0001) << 3)) & 0xFF;
291	case BASE_PALETTE_RAM:
292		break;
293	case BASE_VRAM:
294		break;
295	case BASE_OAM:
296		break;
297	case BASE_CART0:
298	case BASE_CART0_EX:
299	case BASE_CART1:
300	case BASE_CART1_EX:
301	case BASE_CART2:
302	case BASE_CART2_EX:
303		if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
304			return ((uint8_t*) gbaMemory->rom)[address & (SIZE_CART0 - 1)];
305		}
306	case BASE_CART_SRAM:
307		if (gbaMemory->savedata.type == SAVEDATA_NONE) {
308			GBASavedataInitSRAM(&gbaMemory->savedata);
309		}
310		return gbaMemory->savedata.data[address & (SIZE_CART_SRAM - 1)];
311	default:
312		break;
313	}
314
315	return 0;
316}
317
318void GBAStore32(struct ARMMemory* memory, uint32_t address, int32_t value) {
319	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
320
321	switch (address & ~OFFSET_MASK) {
322	case BASE_WORKING_RAM:
323		gbaMemory->wram[(address & (SIZE_WORKING_RAM - 1)) >> 2] = value;
324		break;
325	case BASE_WORKING_IRAM:
326		gbaMemory->iwram[(address & (SIZE_WORKING_IRAM - 1)) >> 2] = value;
327		break;
328	case BASE_IO:
329		GBAIOWrite32(gbaMemory->p, address & (SIZE_IO - 1), value);
330		break;
331	case BASE_PALETTE_RAM:
332		((int32_t*) gbaMemory->p->video.palette)[(address & (SIZE_PALETTE_RAM - 1)) >> 2] = value;
333		break;
334	case BASE_VRAM:
335		if ((address & OFFSET_MASK) < SIZE_VRAM - 2) {
336			((int32_t*) gbaMemory->p->video.vram)[(address & 0x0001FFFF) >> 2] = value;
337		}
338		break;
339	case BASE_OAM:
340		((int32_t*) gbaMemory->p->video.oam.raw)[(address & (SIZE_OAM - 1)) >> 2] = value;
341		break;
342	case BASE_CART0:
343		break;
344	case BASE_CART_SRAM:
345		break;
346	default:
347		break;
348	}
349}
350
351void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value) {
352	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
353
354	switch (address & ~OFFSET_MASK) {
355	case BASE_WORKING_RAM:
356		((int16_t*) gbaMemory->wram)[(address & (SIZE_WORKING_RAM - 1)) >> 1] = value;
357		break;
358	case BASE_WORKING_IRAM:
359		((int16_t*) gbaMemory->iwram)[(address & (SIZE_WORKING_IRAM - 1)) >> 1] = value;
360		break;
361	case BASE_IO:
362		GBAIOWrite(gbaMemory->p, address & (SIZE_IO - 1), value);
363		break;
364	case BASE_PALETTE_RAM:
365		gbaMemory->p->video.palette[(address & (SIZE_PALETTE_RAM - 1)) >> 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
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 + 1] = 2 * memory->waitstatesSeq16[REGION_CART0] + 1;
457	memory->waitstatesSeq32[REGION_CART1] = memory->waitstatesSeq32[REGION_CART1 + 1] = 2 * memory->waitstatesSeq16[REGION_CART1] + 1;
458	memory->waitstatesSeq32[REGION_CART2] = memory->waitstatesSeq32[REGION_CART2 + 1] = 2 * memory->waitstatesSeq16[REGION_CART2] + 1;
459
460	memory->d.activePrefetchCycles32 = memory->waitstates32[memory->activeRegion];
461	memory->d.activePrefetchCycles16 = memory->waitstates16[memory->activeRegion];
462}
463
464int32_t GBAMemoryProcessEvents(struct GBAMemory* memory, int32_t cycles) {
465	struct GBADMA* dma;
466	int32_t test = INT_MAX;
467
468	dma = &memory->dma[0];
469	dma->nextIRQ -= cycles;
470	if (dma->enable && dma->doIrq && dma->nextIRQ) {
471		if (dma->nextIRQ <= 0) {
472			dma->nextIRQ = INT_MAX;
473			GBARaiseIRQ(memory->p, IRQ_DMA0);
474		} else if (dma->nextIRQ < test) {
475			test = dma->nextIRQ;
476		}
477	}
478
479	dma = &memory->dma[1];
480	dma->nextIRQ -= cycles;
481	if (dma->enable && dma->doIrq && dma->nextIRQ) {
482		if (dma->nextIRQ <= 0) {
483			dma->nextIRQ = INT_MAX;
484			GBARaiseIRQ(memory->p, IRQ_DMA1);
485		} else if (dma->nextIRQ < test) {
486			test = dma->nextIRQ;
487		}
488	}
489
490	dma = &memory->dma[2];
491	dma->nextIRQ -= cycles;
492	if (dma->enable && dma->doIrq && dma->nextIRQ) {
493		if (dma->nextIRQ <= 0) {
494			dma->nextIRQ = INT_MAX;
495			GBARaiseIRQ(memory->p, IRQ_DMA2);
496		} else if (dma->nextIRQ < test) {
497			test = dma->nextIRQ;
498		}
499	}
500
501	dma = &memory->dma[3];
502	dma->nextIRQ -= cycles;
503	if (dma->enable && dma->doIrq && dma->nextIRQ) {
504		if (dma->nextIRQ <= 0) {
505			dma->nextIRQ = INT_MAX;
506			GBARaiseIRQ(memory->p, IRQ_DMA3);
507		} else if (dma->nextIRQ < test) {
508			test = dma->nextIRQ;
509		}
510	}
511
512	return test;
513}
514
515void GBAMemoryWriteDMASAD(struct GBAMemory* memory, int dma, uint32_t address) {
516	memory->dma[dma].source = address & 0xFFFFFFFE;
517}
518
519void GBAMemoryWriteDMADAD(struct GBAMemory* memory, int dma, uint32_t address) {
520	memory->dma[dma].dest = address & 0xFFFFFFFE;
521}
522
523void GBAMemoryWriteDMACNT_LO(struct GBAMemory* memory, int dma, uint16_t count) {
524	memory->dma[dma].count = count ? count : (dma == 3 ? 0x10000 : 0x4000);
525}
526
527uint16_t GBAMemoryWriteDMACNT_HI(struct GBAMemory* memory, int dma, uint16_t control) {
528	struct GBADMA* currentDma = &memory->dma[dma];
529	int wasEnabled = currentDma->enable;
530	currentDma->packed = control;
531	currentDma->nextIRQ = 0;
532
533	if (currentDma->drq) {
534		GBALog(GBA_LOG_STUB, "DRQ not implemented");
535	}
536
537	if (!wasEnabled && currentDma->enable) {
538		currentDma->nextSource = currentDma->source;
539		currentDma->nextDest = currentDma->dest;
540		currentDma->nextCount = currentDma->count;
541		GBAMemoryScheduleDMA(memory, dma, currentDma);
542	}
543	// If the DMA has already occurred, this value might have changed since the function started
544	return currentDma->packed;
545};
546
547void GBAMemoryScheduleDMA(struct GBAMemory* memory, int number, struct GBADMA* info) {
548	switch (info->timing) {
549	case DMA_TIMING_NOW:
550		GBAMemoryServiceDMA(memory, number, info);
551		break;
552	case DMA_TIMING_HBLANK:
553		// Handled implicitly
554		break;
555	case DMA_TIMING_VBLANK:
556		// Handled implicitly
557		break;
558	case DMA_TIMING_CUSTOM:
559		switch (number) {
560		case 0:
561			GBALog(GBA_LOG_WARN, "Discarding invalid DMA0 scheduling");
562			break;
563		case 1:
564		case 2:
565			//this.cpu.irq.audio.scheduleFIFODma(number, info);
566			break;
567		case 3:
568			//this.cpu.irq.video.scheduleVCaptureDma(dma, info);
569			break;
570		}
571	}
572}
573
574void GBAMemoryRunHblankDMAs(struct GBAMemory* memory) {
575	struct GBADMA* dma;
576	int i;
577	for (i = 0; i < 4; ++i) {
578		dma = &memory->dma[i];
579		if (dma->enable && dma->timing == DMA_TIMING_HBLANK) {
580			GBAMemoryServiceDMA(memory, i, dma);
581		}
582	}
583}
584
585void GBAMemoryRunVblankDMAs(struct GBAMemory* memory) {
586	struct GBADMA* dma;
587	int i;
588	for (i = 0; i < 4; ++i) {
589		dma = &memory->dma[i];
590		if (dma->enable && dma->timing == DMA_TIMING_VBLANK) {
591			GBAMemoryServiceDMA(memory, i, dma);
592		}
593	}
594}
595
596void GBAMemoryServiceDMA(struct GBAMemory* memory, int number, struct GBADMA* info) {
597	if (!info->enable) {
598		// There was a DMA scheduled that got canceled
599		return;
600	}
601
602	uint32_t width = info->width ? 4 : 2;
603	int sourceOffset = DMA_OFFSET[info->srcControl] * width;
604	int destOffset = DMA_OFFSET[info->dstControl] * width;
605	int32_t wordsRemaining = info->nextCount;
606	uint32_t source = info->nextSource;
607	uint32_t dest = info->nextDest;
608	uint32_t sourceRegion = source >> BASE_OFFSET;
609	uint32_t destRegion = dest >> BASE_OFFSET;
610
611	if (width == 4) {
612		int32_t word;
613		source &= 0xFFFFFFFC;
614		dest &= 0xFFFFFFFC;
615		while (wordsRemaining--) {
616			word = GBALoad32(&memory->d, source);
617			GBAStore32(&memory->d, dest, word);
618			source += sourceOffset;
619			dest += destOffset;
620		}
621	} else {
622		uint16_t word;
623		if (sourceRegion == REGION_CART2_EX && memory->savedata.type == SAVEDATA_EEPROM) {
624			while (wordsRemaining--) {
625				word = GBASavedataReadEEPROM(&memory->savedata);
626				GBAStore16(&memory->d, dest, word);
627				source += sourceOffset;
628				dest += destOffset;
629			}
630		} else if (destRegion == REGION_CART2_EX) {
631			if (memory->savedata.type != SAVEDATA_EEPROM) {
632				GBASavedataInitEEPROM(&memory->savedata);
633			}
634			while (wordsRemaining) {
635				word = GBALoadU16(&memory->d, source);
636				GBASavedataWriteEEPROM(&memory->savedata, word, wordsRemaining);
637				source += sourceOffset;
638				dest += destOffset;
639				--wordsRemaining;
640			}
641		} else {
642			while (wordsRemaining--) {
643				word = GBALoadU16(&memory->d, source);
644				GBAStore16(&memory->d, dest, word);
645				source += sourceOffset;
646				dest += destOffset;
647			}
648		}
649	}
650
651	if (info->doIrq) {
652		info->nextIRQ = memory->p->cpu.cycles + 2;
653		info->nextIRQ += (width == 4 ? memory->waitstates32[sourceRegion] + memory->waitstates32[destRegion]
654		                            : memory->waitstates16[sourceRegion] + memory->waitstates16[destRegion]);
655		info->nextIRQ += (info->count - 1) * (width == 4 ? memory->waitstatesSeq32[sourceRegion] + memory->waitstatesSeq32[destRegion]
656		                                               : memory->waitstatesSeq16[sourceRegion] + memory->waitstatesSeq16[destRegion]);
657	}
658
659	info->nextSource = source;
660	info->nextDest = dest;
661	info->nextCount = wordsRemaining;
662
663	if (!info->repeat) {
664		info->enable = 0;
665
666		// Clear the enable bit in memory
667		memory->io[(REG_DMA0CNT_HI + number * (REG_DMA1CNT_HI - REG_DMA0CNT_HI)) >> 1] &= 0x7FE0;
668	} else {
669		info->nextCount = info->count;
670		if (info->dstControl == DMA_INCREMENT_RELOAD) {
671			info->nextDest = info->dest;
672		}
673		GBAMemoryScheduleDMA(memory, number, info);
674	}
675}