all repos — mgba @ 71986b0477a960833c87113d7dda36f02132e93b

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		break;
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		break;
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		break;
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		gbaMemory->p->video.palette[(address & (SIZE_PALETTE_RAM - 1)) >> 1] = value;
292		gbaMemory->p->video.palette[((address & (SIZE_PALETTE_RAM - 1)) >> 1) + 1] = value >> 16;
293		break;
294	case BASE_VRAM:
295		break;
296	case BASE_OAM:
297		break;
298	case BASE_CART0:
299		break;
300	case BASE_CART2_EX:
301		break;
302	case BASE_CART_SRAM:
303		break;
304	default:
305		break;
306	}
307}
308
309void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value) {
310	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
311
312	switch (address & ~OFFSET_MASK) {
313	case BASE_WORKING_RAM:
314		((int16_t*) gbaMemory->wram)[(address & (SIZE_WORKING_RAM - 1)) >> 1] = value;
315		break;
316	case BASE_WORKING_IRAM:
317		((int16_t*) gbaMemory->iwram)[(address & (SIZE_WORKING_IRAM - 1)) >> 1] = value;
318		break;
319	case BASE_IO:
320		GBAIOWrite(gbaMemory->p, address & (SIZE_IO - 1), value);
321		break;
322	case BASE_PALETTE_RAM:
323		gbaMemory->p->video.palette[(address & (SIZE_PALETTE_RAM - 1)) >> 1] = value;
324		break;
325	case BASE_VRAM:
326		break;
327	case BASE_OAM:
328		break;
329	case BASE_CART0:
330		break;
331	case BASE_CART2_EX:
332		break;
333	case BASE_CART_SRAM:
334		break;
335	default:
336		break;
337	}
338}
339
340void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value) {
341	struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
342
343	switch (address & ~OFFSET_MASK) {
344	case BASE_WORKING_RAM:
345		((int8_t*) gbaMemory->wram)[address & (SIZE_WORKING_RAM - 1)] = value;
346		break;
347	case BASE_WORKING_IRAM:
348		((int8_t*) gbaMemory->iwram)[address & (SIZE_WORKING_IRAM - 1)] = value;
349		break;
350	case BASE_IO:
351		break;
352	case BASE_PALETTE_RAM:
353		break;
354	case BASE_VRAM:
355		break;
356	case BASE_OAM:
357		break;
358	case BASE_CART0:
359		break;
360	case BASE_CART2_EX:
361		break;
362	case BASE_CART_SRAM:
363		break;
364	default:
365		break;
366	}
367}
368
369void GBAAdjustWaitstates(struct GBAMemory* memory, uint16_t parameters) {
370	int sram = parameters & 0x0003;
371	int ws0 = (parameters & 0x000C) >> 2;
372	int ws0seq = (parameters & 0x0010) >> 4;
373	int ws1 = (parameters & 0x0060) >> 5;
374	int ws1seq = (parameters & 0x0080) >> 7;
375	int ws2 = (parameters & 0x0300) >> 8;
376	int ws2seq = (parameters & 0x0400) >> 10;
377	int prefetch = parameters & 0x4000;
378
379	memory->waitstates16[REGION_CART_SRAM] =  GBA_ROM_WAITSTATES[sram];
380	memory->waitstatesSeq16[REGION_CART_SRAM] = GBA_ROM_WAITSTATES[sram];
381	memory->waitstates32[REGION_CART_SRAM] = 2 * GBA_ROM_WAITSTATES[sram] + 1;
382	memory->waitstatesSeq32[REGION_CART_SRAM] = 2 * GBA_ROM_WAITSTATES[sram] + 1;
383
384	memory->waitstates16[REGION_CART0] = memory->waitstates16[REGION_CART0_EX] = GBA_ROM_WAITSTATES[ws0];
385	memory->waitstates16[REGION_CART1] = memory->waitstates16[REGION_CART1_EX] = GBA_ROM_WAITSTATES[ws1];
386	memory->waitstates16[REGION_CART2] = memory->waitstates16[REGION_CART2_EX] = GBA_ROM_WAITSTATES[ws2];
387
388	memory->waitstatesSeq16[REGION_CART0] = memory->waitstatesSeq16[REGION_CART0_EX] = GBA_ROM_WAITSTATES_SEQ[ws0seq];
389	memory->waitstatesSeq16[REGION_CART1] = memory->waitstatesSeq16[REGION_CART1_EX] = GBA_ROM_WAITSTATES_SEQ[ws1seq + 2];
390	memory->waitstatesSeq16[REGION_CART2] = memory->waitstatesSeq16[REGION_CART2_EX] = GBA_ROM_WAITSTATES_SEQ[ws2seq + 4];
391
392	memory->waitstates32[REGION_CART0] = memory->waitstates32[REGION_CART0_EX] = memory->waitstates16[REGION_CART0] + 1 + memory->waitstatesSeq16[REGION_CART0];
393	memory->waitstates32[REGION_CART1] = memory->waitstates32[REGION_CART1_EX] = memory->waitstates16[REGION_CART1] + 1 + memory->waitstatesSeq16[REGION_CART1];
394	memory->waitstates32[REGION_CART2] = memory->waitstates32[REGION_CART2_EX] = memory->waitstates16[REGION_CART2] + 1 + memory->waitstatesSeq16[REGION_CART2];
395
396	memory->waitstatesSeq32[REGION_CART0] = memory->waitstatesSeq32[REGION_CART0 + 1] = 2 * memory->waitstatesSeq16[REGION_CART0] + 1;
397	memory->waitstatesSeq32[REGION_CART1] = memory->waitstatesSeq32[REGION_CART1 + 1] = 2 * memory->waitstatesSeq16[REGION_CART1] + 1;
398	memory->waitstatesSeq32[REGION_CART2] = memory->waitstatesSeq32[REGION_CART2 + 1] = 2 * memory->waitstatesSeq16[REGION_CART2] + 1;
399
400	memory->d.activePrefetchCycles32 = memory->waitstates32[memory->activeRegion];
401	memory->d.activePrefetchCycles16 = memory->waitstates16[memory->activeRegion];
402}
403
404int32_t GBAMemoryProcessEvents(struct GBAMemory* memory, int32_t cycles) {
405	struct GBADMA* dma;
406	int32_t test = INT_MAX;
407
408	dma = &memory->dma[0];
409	dma->nextIRQ -= cycles;
410	if (dma->enable && dma->doIrq && dma->nextIRQ) {
411		if (dma->nextIRQ <= 0) {
412			dma->nextIRQ = INT_MAX;
413			GBARaiseIRQ(memory->p, IRQ_DMA0);
414		} else if (dma->nextIRQ < test) {
415			test = dma->nextIRQ;
416		}
417	}
418
419	dma = &memory->dma[1];
420	dma->nextIRQ -= cycles;
421	if (dma->enable && dma->doIrq && dma->nextIRQ) {
422		if (dma->nextIRQ <= 0) {
423			dma->nextIRQ = INT_MAX;
424			GBARaiseIRQ(memory->p, IRQ_DMA1);
425		} else if (dma->nextIRQ < test) {
426			test = dma->nextIRQ;
427		}
428	}
429
430	dma = &memory->dma[2];
431	dma->nextIRQ -= cycles;
432	if (dma->enable && dma->doIrq && dma->nextIRQ) {
433		if (dma->nextIRQ <= 0) {
434			dma->nextIRQ = INT_MAX;
435			GBARaiseIRQ(memory->p, IRQ_DMA2);
436		} else if (dma->nextIRQ < test) {
437			test = dma->nextIRQ;
438		}
439	}
440
441	dma = &memory->dma[3];
442	dma->nextIRQ -= cycles;
443	if (dma->enable && dma->doIrq && dma->nextIRQ) {
444		if (dma->nextIRQ <= 0) {
445			dma->nextIRQ = INT_MAX;
446			GBARaiseIRQ(memory->p, IRQ_DMA3);
447		} else if (dma->nextIRQ < test) {
448			test = dma->nextIRQ;
449		}
450	}
451
452	return test;
453}
454
455void GBAMemoryWriteDMASAD(struct GBAMemory* memory, int dma, uint32_t address) {
456	memory->dma[dma].source = address & 0xFFFFFFFE;
457}
458
459void GBAMemoryWriteDMADAD(struct GBAMemory* memory, int dma, uint32_t address) {
460	memory->dma[dma].dest = address & 0xFFFFFFFE;
461}
462
463void GBAMemoryWriteDMACNT_LO(struct GBAMemory* memory, int dma, uint16_t count) {
464	memory->dma[dma].count = count ? count : (dma == 3 ? 0x10000 : 0x4000);
465}
466
467uint16_t GBAMemoryWriteDMACNT_HI(struct GBAMemory* memory, int dma, uint16_t control) {
468	struct GBADMA* currentDma = &memory->dma[dma];
469	int wasEnabled = currentDma->enable;
470	currentDma->packed = control;
471	currentDma->nextIRQ = 0;
472
473	if (currentDma->drq) {
474		GBALog(GBA_LOG_STUB, "DRQ not implemented");
475	}
476
477	if (!wasEnabled && currentDma->enable) {
478		currentDma->nextSource = currentDma->source;
479		currentDma->nextDest = currentDma->dest;
480		currentDma->nextCount = currentDma->count;
481		GBAMemoryScheduleDMA(memory, dma, currentDma);
482	}
483	// If the DMA has already occurred, this value might have changed since the function started
484	return currentDma->packed;
485};
486
487void GBAMemoryScheduleDMA(struct GBAMemory* memory, int number, struct GBADMA* info) {
488	switch (info->timing) {
489	case DMA_TIMING_NOW:
490		GBAMemoryServiceDMA(memory, number, info);
491		break;
492	case DMA_TIMING_HBLANK:
493		// Handled implicitly
494		break;
495	case DMA_TIMING_VBLANK:
496		// Handled implicitly
497		break;
498	case DMA_TIMING_CUSTOM:
499		switch (number) {
500		case 0:
501			GBALog(GBA_LOG_WARN, "Discarding invalid DMA0 scheduling");
502			break;
503		case 1:
504		case 2:
505			//this.cpu.irq.audio.scheduleFIFODma(number, info);
506			break;
507		case 3:
508			//this.cpu.irq.video.scheduleVCaptureDma(dma, info);
509			break;
510		}
511	}
512}
513
514void GBAMemoryRunHblankDMAs(struct GBAMemory* memory) {
515	struct GBADMA* dma;
516	int i;
517	for (i = 0; i < 4; ++i) {
518		dma = &memory->dma[i];
519		if (dma->enable && dma->timing == DMA_TIMING_HBLANK) {
520			GBAMemoryServiceDMA(memory, i, dma);
521		}
522	}
523}
524
525void GBAMemoryRunVblankDMAs(struct GBAMemory* memory) {
526	struct GBADMA* dma;
527	int i;
528	for (i = 0; i < 4; ++i) {
529		dma = &memory->dma[i];
530		if (dma->enable && dma->timing == DMA_TIMING_VBLANK) {
531			GBAMemoryServiceDMA(memory, i, dma);
532		}
533	}
534}
535
536void GBAMemoryServiceDMA(struct GBAMemory* memory, int number, struct GBADMA* info) {
537	if (!info->enable) {
538		// There was a DMA scheduled that got canceled
539		return;
540	}
541
542	uint32_t width = info->width ? 4 : 2;
543	int sourceOffset = DMA_OFFSET[info->srcControl] * width;
544	int destOffset = DMA_OFFSET[info->dstControl] * width;
545	int32_t wordsRemaining = info->nextCount;
546	uint32_t source = info->nextSource;
547	uint32_t dest = info->nextDest;
548	uint32_t sourceRegion = source >> BASE_OFFSET;
549	uint32_t destRegion = dest >> BASE_OFFSET;
550
551	if (width == 4) {
552		int32_t word;
553		source &= 0xFFFFFFFC;
554		dest &= 0xFFFFFFFC;
555		while (wordsRemaining--) {
556			word = GBALoad32(&memory->d, source);
557			GBAStore32(&memory->d, dest, word);
558			source += sourceOffset;
559			dest += destOffset;
560		}
561	} else {
562		uint16_t word;
563		while (wordsRemaining--) {
564			word = GBALoadU16(&memory->d, source);
565			GBAStore16(&memory->d, dest, word);
566			source += sourceOffset;
567			dest += destOffset;
568		}
569	}
570
571	if (info->doIrq) {
572		info->nextIRQ = memory->p->cpu.cycles + 2;
573		info->nextIRQ += (width == 4 ? memory->waitstates32[sourceRegion] + memory->waitstates32[destRegion]
574		                            : memory->waitstates16[sourceRegion] + memory->waitstates16[destRegion]);
575		info->nextIRQ += (info->count - 1) * (width == 4 ? memory->waitstatesSeq32[sourceRegion] + memory->waitstatesSeq32[destRegion]
576		                                               : memory->waitstatesSeq16[sourceRegion] + memory->waitstatesSeq16[destRegion]);
577	}
578
579	info->nextSource = source;
580	info->nextDest = dest;
581	info->nextCount = wordsRemaining;
582
583	if (!info->repeat) {
584		info->enable = 0;
585
586		// Clear the enable bit in memory
587		memory->io[(REG_DMA0CNT_HI + number * (REG_DMA1CNT_HI - REG_DMA0CNT_HI)) >> 1] &= 0x7FE0;
588	} else {
589		info->nextCount = info->count;
590		if (info->dstControl == DMA_INCREMENT_RELOAD) {
591			info->nextDest = info->dest;
592		}
593		GBAMemoryScheduleDMA(memory, number, info);
594	}
595}