all repos — mgba @ 5122a236e0cb6ed73a274584b94d0639bf0157f3

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