src/gba/dma.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include <mgba/internal/gba/dma.h>
7
8#include <mgba/internal/gba/gba.h>
9#include <mgba/internal/gba/io.h>
10
11static void _dmaEvent(struct mTiming* timing, void* context, uint32_t cyclesLate);
12
13static void GBADMAService(struct GBA* gba, int number, struct GBADMA* info);
14
15static const int DMA_OFFSET[] = { 1, -1, 0, 1 };
16
17void GBADMAInit(struct GBA* gba) {
18 gba->memory.dmaEvent.name = "GBA DMA";
19 gba->memory.dmaEvent.callback = _dmaEvent;
20 gba->memory.dmaEvent.context = gba;
21 gba->memory.dmaEvent.priority = 0x40;
22}
23
24void GBADMAReset(struct GBA* gba) {
25 memset(gba->memory.dma, 0, sizeof(gba->memory.dma));
26 int i;
27 for (i = 0; i < 4; ++i) {
28 gba->memory.dma[i].count = 0x4000;
29 }
30 gba->memory.dma[3].count = 0x10000;
31 gba->memory.activeDMA = -1;
32}
33static bool _isValidDMASAD(int dma, uint32_t address) {
34 if (dma == 0 && address >= BASE_CART0 && address < BASE_CART_SRAM) {
35 return false;
36 }
37 return address >= BASE_WORKING_RAM;
38}
39
40static bool _isValidDMADAD(int dma, uint32_t address) {
41 return dma == 3 || address < BASE_CART0;
42}
43
44uint32_t GBADMAWriteSAD(struct GBA* gba, int dma, uint32_t address) {
45 struct GBAMemory* memory = &gba->memory;
46 address &= 0x0FFFFFFE;
47 if (_isValidDMASAD(dma, address)) {
48 memory->dma[dma].source = address;
49 } else {
50 memory->dma[dma].source = 0;
51 }
52 return memory->dma[dma].source;
53}
54
55uint32_t GBADMAWriteDAD(struct GBA* gba, int dma, uint32_t address) {
56 struct GBAMemory* memory = &gba->memory;
57 address &= 0x0FFFFFFE;
58 if (_isValidDMADAD(dma, address)) {
59 memory->dma[dma].dest = address;
60 }
61 return memory->dma[dma].dest;
62}
63
64void GBADMAWriteCNT_LO(struct GBA* gba, int dma, uint16_t count) {
65 struct GBAMemory* memory = &gba->memory;
66 memory->dma[dma].count = count ? count : (dma == 3 ? 0x10000 : 0x4000);
67}
68
69uint16_t GBADMAWriteCNT_HI(struct GBA* gba, int dma, uint16_t control) {
70 struct GBAMemory* memory = &gba->memory;
71 struct GBADMA* currentDma = &memory->dma[dma];
72 int wasEnabled = GBADMARegisterIsEnable(currentDma->reg);
73 if (dma < 3) {
74 control &= 0xF7E0;
75 } else {
76 control &= 0xFFE0;
77 }
78 currentDma->reg = control;
79
80 if (GBADMARegisterIsDRQ(currentDma->reg)) {
81 mLOG(GBA_MEM, STUB, "DRQ not implemented");
82 }
83
84 if (!wasEnabled && GBADMARegisterIsEnable(currentDma->reg)) {
85 currentDma->nextSource = currentDma->source;
86 if (currentDma->nextSource >= BASE_CART0 && currentDma->nextSource < BASE_CART_SRAM && GBADMARegisterGetSrcControl(currentDma->reg) < 3) {
87 currentDma->reg = GBADMARegisterClearSrcControl(currentDma->reg);
88 }
89 currentDma->nextDest = currentDma->dest;
90
91 uint32_t width = 2 << GBADMARegisterGetWidth(currentDma->reg);
92 if (currentDma->nextSource & (width - 1)) {
93 mLOG(GBA_MEM, GAME_ERROR, "Misaligned DMA source address: 0x%08X", currentDma->nextSource);
94 }
95 if (currentDma->nextDest & (width - 1)) {
96 mLOG(GBA_MEM, GAME_ERROR, "Misaligned DMA destination address: 0x%08X", currentDma->nextDest);
97 }
98
99 GBADMASchedule(gba, dma, currentDma);
100 }
101 // If the DMA has already occurred, this value might have changed since the function started
102 return currentDma->reg;
103};
104
105void GBADMASchedule(struct GBA* gba, int number, struct GBADMA* info) {
106 switch (GBADMARegisterGetTiming(info->reg)) {
107 case GBA_DMA_TIMING_NOW:
108 info->when = mTimingCurrentTime(&gba->timing) + 3; // DMAs take 3 cycles to start
109 info->nextCount = info->count;
110 break;
111 case GBA_DMA_TIMING_HBLANK:
112 case GBA_DMA_TIMING_VBLANK:
113 // Handled implicitly
114 return;
115 case GBA_DMA_TIMING_CUSTOM:
116 switch (number) {
117 case 0:
118 mLOG(GBA_MEM, WARN, "Discarding invalid DMA0 scheduling");
119 return;
120 case 1:
121 case 2:
122 GBAAudioScheduleFifoDma(&gba->audio, number, info);
123 break;
124 case 3:
125 // Handled implicitly
126 break;
127 }
128 }
129 GBADMAUpdate(gba);
130}
131
132void GBADMARunHblank(struct GBA* gba, int32_t cycles) {
133 struct GBAMemory* memory = &gba->memory;
134 struct GBADMA* dma;
135 int i;
136 for (i = 0; i < 4; ++i) {
137 dma = &memory->dma[i];
138 if (GBADMARegisterIsEnable(dma->reg) && GBADMARegisterGetTiming(dma->reg) == GBA_DMA_TIMING_HBLANK && !dma->nextCount) {
139 dma->when = mTimingCurrentTime(&gba->timing) + 3 + cycles;
140 dma->nextCount = dma->count;
141 }
142 }
143 GBADMAUpdate(gba);
144}
145
146void GBADMARunVblank(struct GBA* gba, int32_t cycles) {
147 struct GBAMemory* memory = &gba->memory;
148 struct GBADMA* dma;
149 int i;
150 for (i = 0; i < 4; ++i) {
151 dma = &memory->dma[i];
152 if (GBADMARegisterIsEnable(dma->reg) && GBADMARegisterGetTiming(dma->reg) == GBA_DMA_TIMING_VBLANK && !dma->nextCount) {
153 dma->when = mTimingCurrentTime(&gba->timing) + 3 + cycles;
154 dma->nextCount = dma->count;
155 }
156 }
157 GBADMAUpdate(gba);
158}
159
160void GBADMARunDisplayStart(struct GBA* gba, int32_t cycles) {
161 struct GBAMemory* memory = &gba->memory;
162 struct GBADMA* dma = &memory->dma[3];
163 if (GBADMARegisterIsEnable(dma->reg) && GBADMARegisterGetTiming(dma->reg) == GBA_DMA_TIMING_CUSTOM && !dma->nextCount) {
164 dma->when = mTimingCurrentTime(&gba->timing) + 3 + cycles;
165 dma->nextCount = dma->count;
166 GBADMAUpdate(gba);
167 }
168}
169
170void _dmaEvent(struct mTiming* timing, void* context, uint32_t cyclesLate) {
171 UNUSED(timing);
172 UNUSED(cyclesLate);
173 struct GBA* gba = context;
174 struct GBAMemory* memory = &gba->memory;
175 struct GBADMA* dma = &memory->dma[memory->activeDMA];
176 if (dma->nextCount == dma->count) {
177 dma->when = mTimingCurrentTime(&gba->timing);
178 }
179 if (dma->nextCount & 0xFFFFF) {
180 GBADMAService(gba, memory->activeDMA, dma);
181 } else {
182 dma->nextCount = 0;
183 bool noRepeat = !GBADMARegisterIsRepeat(dma->reg);
184 noRepeat |= GBADMARegisterGetTiming(dma->reg) == GBA_DMA_TIMING_NOW;
185 noRepeat |= memory->activeDMA == 3 && GBADMARegisterGetTiming(dma->reg) == GBA_DMA_TIMING_CUSTOM && gba->video.vcount == GBA_VIDEO_VERTICAL_PIXELS + 1;
186 if (noRepeat) {
187 dma->reg = GBADMARegisterClearEnable(dma->reg);
188
189 // Clear the enable bit in memory
190 memory->io[(REG_DMA0CNT_HI + memory->activeDMA * (REG_DMA1CNT_HI - REG_DMA0CNT_HI)) >> 1] &= 0x7FE0;
191 }
192 if (GBADMARegisterGetDestControl(dma->reg) == GBA_DMA_INCREMENT_RELOAD) {
193 dma->nextDest = dma->dest;
194 }
195 if (GBADMARegisterIsDoIRQ(dma->reg)) {
196 GBARaiseIRQ(gba, IRQ_DMA0 + memory->activeDMA, cyclesLate);
197 }
198 GBADMAUpdate(gba);
199 }
200}
201
202void GBADMAUpdate(struct GBA* gba) {
203 int i;
204 struct GBAMemory* memory = &gba->memory;
205 uint32_t currentTime = mTimingCurrentTime(&gba->timing);
206 int32_t leastTime = INT_MAX;
207 memory->activeDMA = -1;
208 for (i = 0; i < 4; ++i) {
209 struct GBADMA* dma = &memory->dma[i];
210 if (GBADMARegisterIsEnable(dma->reg) && dma->nextCount) {
211 int32_t time = dma->when - currentTime;
212 if (memory->activeDMA == -1 || (dma->count == dma->nextCount && time < leastTime)) {
213 leastTime = time;
214 memory->activeDMA = i;
215 }
216 }
217 }
218
219 if (memory->activeDMA >= 0) {
220 mTimingDeschedule(&gba->timing, &memory->dmaEvent);
221 mTimingSchedule(&gba->timing, &memory->dmaEvent, memory->dma[memory->activeDMA].when - currentTime);
222 } else {
223 gba->cpuBlocked = false;
224 }
225}
226
227void GBADMAService(struct GBA* gba, int number, struct GBADMA* info) {
228 struct GBAMemory* memory = &gba->memory;
229 struct ARMCore* cpu = gba->cpu;
230 uint32_t width = 2 << GBADMARegisterGetWidth(info->reg);
231 int32_t wordsRemaining = info->nextCount;
232 uint32_t source = info->nextSource;
233 uint32_t dest = info->nextDest;
234 uint32_t sourceRegion = source >> BASE_OFFSET;
235 uint32_t destRegion = dest >> BASE_OFFSET;
236 int32_t cycles = 2;
237
238 gba->cpuBlocked = true;
239 if (info->count == info->nextCount) {
240 if (width == 4) {
241 cycles += memory->waitstatesNonseq32[sourceRegion] + memory->waitstatesNonseq32[destRegion];
242 } else {
243 cycles += memory->waitstatesNonseq16[sourceRegion] + memory->waitstatesNonseq16[destRegion];
244 }
245 source &= -width;
246 dest &= -width;
247 } else {
248 if (width == 4) {
249 cycles += memory->waitstatesSeq32[sourceRegion] + memory->waitstatesSeq32[destRegion];
250 } else {
251 cycles += memory->waitstatesSeq16[sourceRegion] + memory->waitstatesSeq16[destRegion];
252 }
253 }
254 info->when += cycles;
255
256 gba->performingDMA = 1 | (number << 1);
257 if (width == 4) {
258 if (source) {
259 memory->dmaTransferRegister = cpu->memory.load32(cpu, source, 0);
260 }
261 gba->bus = memory->dmaTransferRegister;
262 cpu->memory.store32(cpu, dest, memory->dmaTransferRegister, 0);
263 } else {
264 if (sourceRegion == REGION_CART2_EX && (memory->savedata.type == SAVEDATA_EEPROM || memory->savedata.type == SAVEDATA_EEPROM512)) {
265 memory->dmaTransferRegister = GBASavedataReadEEPROM(&memory->savedata);
266 memory->dmaTransferRegister |= memory->dmaTransferRegister << 16;
267 } else if (source) {
268 memory->dmaTransferRegister = cpu->memory.load16(cpu, source, 0);
269 memory->dmaTransferRegister |= memory->dmaTransferRegister << 16;
270 }
271 if (destRegion == REGION_CART2_EX) {
272 if (memory->savedata.type == SAVEDATA_AUTODETECT) {
273 mLOG(GBA_MEM, INFO, "Detected EEPROM savegame");
274 GBASavedataInitEEPROM(&memory->savedata);
275 }
276 if (memory->savedata.type == SAVEDATA_EEPROM512 || memory->savedata.type == SAVEDATA_EEPROM) {
277 GBASavedataWriteEEPROM(&memory->savedata, memory->dmaTransferRegister, wordsRemaining);
278 }
279 } else {
280 cpu->memory.store16(cpu, dest, memory->dmaTransferRegister, 0);
281
282 }
283 gba->bus = memory->dmaTransferRegister;
284 }
285 int sourceOffset = DMA_OFFSET[GBADMARegisterGetSrcControl(info->reg)] * width;
286 int destOffset = DMA_OFFSET[GBADMARegisterGetDestControl(info->reg)] * width;
287 if (source) {
288 source += sourceOffset;
289 }
290 dest += destOffset;
291 --wordsRemaining;
292 gba->performingDMA = 0;
293
294 info->nextCount = wordsRemaining;
295 info->nextSource = source;
296 info->nextDest = dest;
297 if (!wordsRemaining) {
298 info->nextCount |= 0x80000000;
299 if (sourceRegion < REGION_CART0 || destRegion < REGION_CART0) {
300 info->when += 2;
301 }
302 }
303 GBADMAUpdate(gba);
304}