all repos — mgba @ 7c597d5205f7ccedbff372927aa5f061e04f4bbc

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		break;
331	case BASE_VRAM:
332		if ((address & OFFSET_MASK) < SIZE_VRAM - 2) {
333			((int32_t*) gbaMemory->p->video.vram)[(address & 0x0001FFFF) >> 2] = value;
334		}
335		break;
336	case BASE_OAM:
337		((int32_t*) gbaMemory->p->video.oam.raw)[(address & (SIZE_OAM - 1)) >> 2] = value;
338		break;
339	case BASE_CART0:
340		break;
341	case BASE_CART_SRAM:
342		break;
343	default:
344		break;
345	}
346}
347
348void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value) {
349	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
350
351	switch (address & ~OFFSET_MASK) {
352	case BASE_WORKING_RAM:
353		((int16_t*) gbaMemory->wram)[(address & (SIZE_WORKING_RAM - 1)) >> 1] = value;
354		break;
355	case BASE_WORKING_IRAM:
356		((int16_t*) gbaMemory->iwram)[(address & (SIZE_WORKING_IRAM - 1)) >> 1] = value;
357		break;
358	case BASE_IO:
359		GBAIOWrite(gbaMemory->p, address & (SIZE_IO - 1), value);
360		break;
361	case BASE_PALETTE_RAM:
362		gbaMemory->p->video.palette[(address & (SIZE_PALETTE_RAM - 1)) >> 1] = value;
363		break;
364	case BASE_VRAM:
365		if ((address & OFFSET_MASK) < SIZE_VRAM) {
366			gbaMemory->p->video.vram[(address & 0x0001FFFF) >> 1] = value;
367		}
368		break;
369	case BASE_OAM:
370		gbaMemory->p->video.oam.raw[(address & (SIZE_OAM - 1)) >> 1] = value;
371		break;
372	case BASE_CART0:
373		break;
374	case BASE_CART2_EX:
375		if (gbaMemory->savedata.type == SAVEDATA_NONE) {
376			GBASavedataInitEEPROM(&gbaMemory->savedata);
377		}
378		GBASavedataWriteEEPROM(&gbaMemory->savedata, value, 1);
379		break;
380	case BASE_CART_SRAM:
381		break;
382	default:
383		break;
384	}
385}
386
387void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value) {
388	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
389
390	switch (address & ~OFFSET_MASK) {
391	case BASE_WORKING_RAM:
392		((int8_t*) gbaMemory->wram)[address & (SIZE_WORKING_RAM - 1)] = value;
393		break;
394	case BASE_WORKING_IRAM:
395		((int8_t*) gbaMemory->iwram)[address & (SIZE_WORKING_IRAM - 1)] = value;
396		break;
397	case BASE_IO:
398		break;
399	case BASE_PALETTE_RAM:
400		break;
401	case BASE_VRAM:
402		break;
403	case BASE_OAM:
404		break;
405	case BASE_CART0:
406		break;
407	case BASE_CART_SRAM:
408		if (gbaMemory->savedata.type == SAVEDATA_NONE) {
409			if (address == SAVEDATA_FLASH_BASE) {
410				GBASavedataInitFlash(&gbaMemory->savedata);
411			} else {
412				GBASavedataInitSRAM(&gbaMemory->savedata);
413			}
414		}
415		if (gbaMemory->savedata.type == SAVEDATA_FLASH512 || gbaMemory->savedata.type == SAVEDATA_FLASH1M) {
416			GBASavedataWriteFlash(&gbaMemory->savedata, value);
417		} else if (gbaMemory->savedata.type == SAVEDATA_SRAM) {
418			gbaMemory->savedata.data[address & (SIZE_CART_SRAM - 1)] = value;
419		}
420		break;
421	default:
422		break;
423	}
424}
425
426void GBAAdjustWaitstates(struct GBAMemory* memory, uint16_t parameters) {
427	int sram = parameters & 0x0003;
428	int ws0 = (parameters & 0x000C) >> 2;
429	int ws0seq = (parameters & 0x0010) >> 4;
430	int ws1 = (parameters & 0x0060) >> 5;
431	int ws1seq = (parameters & 0x0080) >> 7;
432	int ws2 = (parameters & 0x0300) >> 8;
433	int ws2seq = (parameters & 0x0400) >> 10;
434	int prefetch = parameters & 0x4000;
435
436	memory->waitstates16[REGION_CART_SRAM] =  GBA_ROM_WAITSTATES[sram];
437	memory->waitstatesSeq16[REGION_CART_SRAM] = GBA_ROM_WAITSTATES[sram];
438	memory->waitstates32[REGION_CART_SRAM] = 2 * GBA_ROM_WAITSTATES[sram] + 1;
439	memory->waitstatesSeq32[REGION_CART_SRAM] = 2 * GBA_ROM_WAITSTATES[sram] + 1;
440
441	memory->waitstates16[REGION_CART0] = memory->waitstates16[REGION_CART0_EX] = GBA_ROM_WAITSTATES[ws0];
442	memory->waitstates16[REGION_CART1] = memory->waitstates16[REGION_CART1_EX] = GBA_ROM_WAITSTATES[ws1];
443	memory->waitstates16[REGION_CART2] = memory->waitstates16[REGION_CART2_EX] = GBA_ROM_WAITSTATES[ws2];
444
445	memory->waitstatesSeq16[REGION_CART0] = memory->waitstatesSeq16[REGION_CART0_EX] = GBA_ROM_WAITSTATES_SEQ[ws0seq];
446	memory->waitstatesSeq16[REGION_CART1] = memory->waitstatesSeq16[REGION_CART1_EX] = GBA_ROM_WAITSTATES_SEQ[ws1seq + 2];
447	memory->waitstatesSeq16[REGION_CART2] = memory->waitstatesSeq16[REGION_CART2_EX] = GBA_ROM_WAITSTATES_SEQ[ws2seq + 4];
448
449	memory->waitstates32[REGION_CART0] = memory->waitstates32[REGION_CART0_EX] = memory->waitstates16[REGION_CART0] + 1 + memory->waitstatesSeq16[REGION_CART0];
450	memory->waitstates32[REGION_CART1] = memory->waitstates32[REGION_CART1_EX] = memory->waitstates16[REGION_CART1] + 1 + memory->waitstatesSeq16[REGION_CART1];
451	memory->waitstates32[REGION_CART2] = memory->waitstates32[REGION_CART2_EX] = memory->waitstates16[REGION_CART2] + 1 + memory->waitstatesSeq16[REGION_CART2];
452
453	memory->waitstatesSeq32[REGION_CART0] = memory->waitstatesSeq32[REGION_CART0 + 1] = 2 * memory->waitstatesSeq16[REGION_CART0] + 1;
454	memory->waitstatesSeq32[REGION_CART1] = memory->waitstatesSeq32[REGION_CART1 + 1] = 2 * memory->waitstatesSeq16[REGION_CART1] + 1;
455	memory->waitstatesSeq32[REGION_CART2] = memory->waitstatesSeq32[REGION_CART2 + 1] = 2 * memory->waitstatesSeq16[REGION_CART2] + 1;
456
457	memory->d.activePrefetchCycles32 = memory->waitstates32[memory->activeRegion];
458	memory->d.activePrefetchCycles16 = memory->waitstates16[memory->activeRegion];
459}
460
461int32_t GBAMemoryProcessEvents(struct GBAMemory* memory, int32_t cycles) {
462	struct GBADMA* dma;
463	int32_t test = INT_MAX;
464
465	dma = &memory->dma[0];
466	dma->nextIRQ -= cycles;
467	if (dma->enable && dma->doIrq && dma->nextIRQ) {
468		if (dma->nextIRQ <= 0) {
469			dma->nextIRQ = INT_MAX;
470			GBARaiseIRQ(memory->p, IRQ_DMA0);
471		} else if (dma->nextIRQ < test) {
472			test = dma->nextIRQ;
473		}
474	}
475
476	dma = &memory->dma[1];
477	dma->nextIRQ -= cycles;
478	if (dma->enable && dma->doIrq && dma->nextIRQ) {
479		if (dma->nextIRQ <= 0) {
480			dma->nextIRQ = INT_MAX;
481			GBARaiseIRQ(memory->p, IRQ_DMA1);
482		} else if (dma->nextIRQ < test) {
483			test = dma->nextIRQ;
484		}
485	}
486
487	dma = &memory->dma[2];
488	dma->nextIRQ -= cycles;
489	if (dma->enable && dma->doIrq && dma->nextIRQ) {
490		if (dma->nextIRQ <= 0) {
491			dma->nextIRQ = INT_MAX;
492			GBARaiseIRQ(memory->p, IRQ_DMA2);
493		} else if (dma->nextIRQ < test) {
494			test = dma->nextIRQ;
495		}
496	}
497
498	dma = &memory->dma[3];
499	dma->nextIRQ -= cycles;
500	if (dma->enable && dma->doIrq && dma->nextIRQ) {
501		if (dma->nextIRQ <= 0) {
502			dma->nextIRQ = INT_MAX;
503			GBARaiseIRQ(memory->p, IRQ_DMA3);
504		} else if (dma->nextIRQ < test) {
505			test = dma->nextIRQ;
506		}
507	}
508
509	return test;
510}
511
512void GBAMemoryWriteDMASAD(struct GBAMemory* memory, int dma, uint32_t address) {
513	memory->dma[dma].source = address & 0xFFFFFFFE;
514}
515
516void GBAMemoryWriteDMADAD(struct GBAMemory* memory, int dma, uint32_t address) {
517	memory->dma[dma].dest = address & 0xFFFFFFFE;
518}
519
520void GBAMemoryWriteDMACNT_LO(struct GBAMemory* memory, int dma, uint16_t count) {
521	memory->dma[dma].count = count ? count : (dma == 3 ? 0x10000 : 0x4000);
522}
523
524uint16_t GBAMemoryWriteDMACNT_HI(struct GBAMemory* memory, int dma, uint16_t control) {
525	struct GBADMA* currentDma = &memory->dma[dma];
526	int wasEnabled = currentDma->enable;
527	currentDma->packed = control;
528	currentDma->nextIRQ = 0;
529
530	if (currentDma->drq) {
531		GBALog(GBA_LOG_STUB, "DRQ not implemented");
532	}
533
534	if (!wasEnabled && currentDma->enable) {
535		currentDma->nextSource = currentDma->source;
536		currentDma->nextDest = currentDma->dest;
537		currentDma->nextCount = currentDma->count;
538		GBAMemoryScheduleDMA(memory, dma, currentDma);
539	}
540	// If the DMA has already occurred, this value might have changed since the function started
541	return currentDma->packed;
542};
543
544void GBAMemoryScheduleDMA(struct GBAMemory* memory, int number, struct GBADMA* info) {
545	switch (info->timing) {
546	case DMA_TIMING_NOW:
547		GBAMemoryServiceDMA(memory, number, info);
548		break;
549	case DMA_TIMING_HBLANK:
550		// Handled implicitly
551		break;
552	case DMA_TIMING_VBLANK:
553		// Handled implicitly
554		break;
555	case DMA_TIMING_CUSTOM:
556		switch (number) {
557		case 0:
558			GBALog(GBA_LOG_WARN, "Discarding invalid DMA0 scheduling");
559			break;
560		case 1:
561		case 2:
562			//this.cpu.irq.audio.scheduleFIFODma(number, info);
563			break;
564		case 3:
565			//this.cpu.irq.video.scheduleVCaptureDma(dma, info);
566			break;
567		}
568	}
569}
570
571void GBAMemoryRunHblankDMAs(struct GBAMemory* memory) {
572	struct GBADMA* dma;
573	int i;
574	for (i = 0; i < 4; ++i) {
575		dma = &memory->dma[i];
576		if (dma->enable && dma->timing == DMA_TIMING_HBLANK) {
577			GBAMemoryServiceDMA(memory, i, dma);
578		}
579	}
580}
581
582void GBAMemoryRunVblankDMAs(struct GBAMemory* memory) {
583	struct GBADMA* dma;
584	int i;
585	for (i = 0; i < 4; ++i) {
586		dma = &memory->dma[i];
587		if (dma->enable && dma->timing == DMA_TIMING_VBLANK) {
588			GBAMemoryServiceDMA(memory, i, dma);
589		}
590	}
591}
592
593void GBAMemoryServiceDMA(struct GBAMemory* memory, int number, struct GBADMA* info) {
594	if (!info->enable) {
595		// There was a DMA scheduled that got canceled
596		return;
597	}
598
599	uint32_t width = info->width ? 4 : 2;
600	int sourceOffset = DMA_OFFSET[info->srcControl] * width;
601	int destOffset = DMA_OFFSET[info->dstControl] * width;
602	int32_t wordsRemaining = info->nextCount;
603	uint32_t source = info->nextSource;
604	uint32_t dest = info->nextDest;
605	uint32_t sourceRegion = source >> BASE_OFFSET;
606	uint32_t destRegion = dest >> BASE_OFFSET;
607
608	if (width == 4) {
609		int32_t word;
610		source &= 0xFFFFFFFC;
611		dest &= 0xFFFFFFFC;
612		while (wordsRemaining--) {
613			word = GBALoad32(&memory->d, source);
614			GBAStore32(&memory->d, dest, word);
615			source += sourceOffset;
616			dest += destOffset;
617		}
618	} else {
619		uint16_t word;
620		if (sourceRegion == REGION_CART2_EX && memory->savedata.type == SAVEDATA_EEPROM) {
621			while (wordsRemaining--) {
622				word = GBASavedataReadEEPROM(&memory->savedata);
623				GBAStore16(&memory->d, dest, word);
624				source += sourceOffset;
625				dest += destOffset;
626			}
627		} else if (destRegion == REGION_CART2_EX) {
628			if (memory->savedata.type != SAVEDATA_EEPROM) {
629				GBASavedataInitEEPROM(&memory->savedata);
630			}
631			while (wordsRemaining) {
632				word = GBALoadU16(&memory->d, source);
633				GBASavedataWriteEEPROM(&memory->savedata, word, wordsRemaining);
634				source += sourceOffset;
635				dest += destOffset;
636				--wordsRemaining;
637			}
638		} else {
639			while (wordsRemaining--) {
640				word = GBALoadU16(&memory->d, source);
641				GBAStore16(&memory->d, dest, word);
642				source += sourceOffset;
643				dest += destOffset;
644			}
645		}
646	}
647
648	if (info->doIrq) {
649		info->nextIRQ = memory->p->cpu.cycles + 2;
650		info->nextIRQ += (width == 4 ? memory->waitstates32[sourceRegion] + memory->waitstates32[destRegion]
651		                            : memory->waitstates16[sourceRegion] + memory->waitstates16[destRegion]);
652		info->nextIRQ += (info->count - 1) * (width == 4 ? memory->waitstatesSeq32[sourceRegion] + memory->waitstatesSeq32[destRegion]
653		                                               : memory->waitstatesSeq16[sourceRegion] + memory->waitstatesSeq16[destRegion]);
654	}
655
656	info->nextSource = source;
657	info->nextDest = dest;
658	info->nextCount = wordsRemaining;
659
660	if (!info->repeat) {
661		info->enable = 0;
662
663		// Clear the enable bit in memory
664		memory->io[(REG_DMA0CNT_HI + number * (REG_DMA1CNT_HI - REG_DMA0CNT_HI)) >> 1] &= 0x7FE0;
665	} else {
666		info->nextCount = info->count;
667		if (info->dstControl == DMA_INCREMENT_RELOAD) {
668			info->nextDest = info->dest;
669		}
670		GBAMemoryScheduleDMA(memory, number, info);
671	}
672}