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);
13static int GBAWaitMultiple(struct ARMMemory* memory, uint32_t startAddress, int count);
14
15static const char GBA_BASE_WAITSTATES[16] = { 0, 0, 2, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4 };
16static const char GBA_BASE_WAITSTATES_32[16] = { 0, 0, 5, 0, 0, 0, 0, 0, 7, 7, 9, 9, 13, 13, 9 };
17static const char GBA_BASE_WAITSTATES_SEQ[16] = { 0, 0, 2, 0, 0, 0, 0, 0, 2, 2, 4, 4, 8, 8, 4 };
18static const char GBA_BASE_WAITSTATES_SEQ_32[16] = { 0, 0, 5, 0, 0, 0, 0, 0, 5, 5, 9, 9, 17, 17, 9 };
19static const char GBA_ROM_WAITSTATES[] = { 4, 3, 2, 8 };
20static const char GBA_ROM_WAITSTATES_SEQ[] = { 2, 1, 4, 1, 8, 1 };
21static const int DMA_OFFSET[] = { 1, -1, 0, 1 };
22
23void GBAMemoryInit(struct GBAMemory* memory) {
24 memory->d.load32 = GBALoad32;
25 memory->d.load16 = GBALoad16;
26 memory->d.loadU16 = GBALoadU16;
27 memory->d.load8 = GBALoad8;
28 memory->d.loadU8 = GBALoadU8;
29 memory->d.store32 = GBAStore32;
30 memory->d.store16 = GBAStore16;
31 memory->d.store8 = GBAStore8;
32
33 memory->bios = (uint32_t*) hleBios;
34 memory->fullBios = 0;
35 memory->wram = mmap(0, SIZE_WORKING_RAM, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
36 memory->iwram = mmap(0, SIZE_WORKING_IRAM, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
37 memory->rom = 0;
38 memset(memory->io, 0, sizeof(memory->io));
39 memset(memory->dma, 0, sizeof(memory->dma));
40
41 if (!memory->wram || !memory->iwram) {
42 GBAMemoryDeinit(memory);
43 memory->p->errno = GBA_OUT_OF_MEMORY;
44 memory->p->errstr = GBA_CANNOT_MMAP;
45 }
46
47 int i;
48 for (i = 0; i < 16; ++i) {
49 memory->waitstates16[i] = GBA_BASE_WAITSTATES[i];
50 memory->waitstatesSeq16[i] = GBA_BASE_WAITSTATES_SEQ[i];
51 memory->waitstatesPrefetch16[i] = GBA_BASE_WAITSTATES_SEQ[i];
52 memory->waitstates32[i] = GBA_BASE_WAITSTATES_32[i];
53 memory->waitstatesSeq32[i] = GBA_BASE_WAITSTATES_SEQ_32[i];
54 memory->waitstatesPrefetch32[i] = GBA_BASE_WAITSTATES_SEQ_32[i];
55 }
56 for (; i < 256; ++i) {
57 memory->waitstates16[i] = 0;
58 memory->waitstatesSeq16[i] = 0;
59 memory->waitstatesPrefetch16[i] = 0;
60 memory->waitstates32[i] = 0;
61 memory->waitstatesSeq32[i] = 0;
62 memory->waitstatesPrefetch32[i] = 0;
63 }
64
65 memory->activeRegion = 0;
66 memory->d.activeRegion = 0;
67 memory->d.activeMask = 0;
68 memory->d.setActiveRegion = GBASetActiveRegion;
69 memory->d.activePrefetchCycles32 = 0;
70 memory->d.activePrefetchCycles16 = 0;
71 memory->biosPrefetch = 0;
72 memory->d.waitMultiple = GBAWaitMultiple;
73}
74
75void GBAMemoryDeinit(struct GBAMemory* memory) {
76 munmap(memory->wram, SIZE_WORKING_RAM);
77 munmap(memory->iwram, SIZE_WORKING_IRAM);
78 GBASavedataDeinit(&memory->savedata);
79}
80
81static void GBASetActiveRegion(struct ARMMemory* memory, uint32_t address) {
82 struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
83
84 if (gbaMemory->activeRegion == REGION_BIOS) {
85 gbaMemory->biosPrefetch = memory->load32(memory, gbaMemory->p->cpu.currentPC + WORD_SIZE_ARM * 2, 0);
86 }
87 gbaMemory->activeRegion = address >> BASE_OFFSET;
88 memory->activePrefetchCycles32 = gbaMemory->waitstatesPrefetch32[gbaMemory->activeRegion];
89 memory->activePrefetchCycles16 = gbaMemory->waitstatesPrefetch16[gbaMemory->activeRegion];
90 memory->activeNonseqCycles32 = gbaMemory->waitstates32[gbaMemory->activeRegion];
91 memory->activeNonseqCycles16 = gbaMemory->waitstates16[gbaMemory->activeRegion];
92 switch (address & ~OFFSET_MASK) {
93 case BASE_BIOS:
94 memory->activeRegion = gbaMemory->bios;
95 memory->activeMask = SIZE_BIOS - 1;
96 break;
97 case BASE_WORKING_RAM:
98 memory->activeRegion = gbaMemory->wram;
99 memory->activeMask = SIZE_WORKING_RAM - 1;
100 break;
101 case BASE_WORKING_IRAM:
102 memory->activeRegion = gbaMemory->iwram;
103 memory->activeMask = SIZE_WORKING_IRAM - 1;
104 break;
105 case BASE_CART0:
106 case BASE_CART0_EX:
107 case BASE_CART1:
108 case BASE_CART1_EX:
109 case BASE_CART2:
110 case BASE_CART2_EX:
111 memory->activeRegion = gbaMemory->rom;
112 memory->activeMask = SIZE_CART0 - 1;
113 break;
114 default:
115 memory->activeRegion = 0;
116 memory->activeMask = 0;
117 break;
118 }
119}
120
121int32_t GBALoad32(struct ARMMemory* memory, uint32_t address, int* cycleCounter) {
122 struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
123 uint32_t value = 0;
124 int wait = 0;
125
126 switch (address & ~OFFSET_MASK) {
127 case BASE_BIOS:
128 if (gbaMemory->p->cpu.currentPC >> BASE_OFFSET == REGION_BIOS) {
129 if (address < SIZE_BIOS) {
130 value = gbaMemory->bios[address >> 2];
131 } else {
132 value = 0;
133 }
134 } else {
135 value = gbaMemory->biosPrefetch;
136 }
137 break;
138 case BASE_WORKING_RAM:
139 value = gbaMemory->wram[(address & (SIZE_WORKING_RAM - 1)) >> 2];
140 wait = gbaMemory->waitstates32[REGION_WORKING_RAM];
141 break;
142 case BASE_WORKING_IRAM:
143 value = gbaMemory->iwram[(address & (SIZE_WORKING_IRAM - 1)) >> 2];
144 break;
145 case BASE_IO:
146 value = GBAIORead(gbaMemory->p, (address & (SIZE_IO - 1)) & ~2) | (GBAIORead(gbaMemory->p, (address & (SIZE_IO - 1)) | 2) << 16);
147 break;
148 case BASE_PALETTE_RAM:
149 value = ((int32_t*) gbaMemory->p->video.palette)[(address & (SIZE_PALETTE_RAM - 1)) >> 2];
150 break;
151 case BASE_VRAM:
152 value = ((int32_t*) gbaMemory->p->video.renderer->vram)[(address & 0x0001FFFF) >> 2];
153 break;
154 case BASE_OAM:
155 value = ((int32_t*) gbaMemory->p->video.oam.raw)[(address & (SIZE_OAM - 1)) >> 2];
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 wait = gbaMemory->waitstates32[address >> BASE_OFFSET];
164 if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
165 value = gbaMemory->rom[(address & (SIZE_CART0 - 1)) >> 2];
166 }
167 break;
168 case BASE_CART_SRAM:
169 GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Load32: 0x%08X", address);
170 break;
171 default:
172 GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Load32: 0x%08X", address);
173 if (gbaMemory->p->cpu.executionMode == MODE_ARM) {
174 value = memory->load32(memory, gbaMemory->p->cpu.currentPC + WORD_SIZE_ARM * 2, 0);
175 } else {
176 value = memory->load16(memory, gbaMemory->p->cpu.currentPC + WORD_SIZE_THUMB * 2, 0);
177 value |= value << 16;
178 }
179 break;
180 }
181
182
183 if (cycleCounter) {
184 *cycleCounter += 2 + wait;
185 }
186 // Unaligned 32-bit loads are "rotated" so they make some semblance of sense
187 int rotate = (address & 3) << 3;
188 return (value >> rotate) | (value << (32 - rotate));
189}
190
191uint16_t GBALoadU16(struct ARMMemory* memory, uint32_t address, int* cycleCounter) {
192 return GBALoad16(memory, address, cycleCounter);
193}
194
195int16_t GBALoad16(struct ARMMemory* memory, uint32_t address, int* cycleCounter) {
196 struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
197 int16_t value = 0;
198 int wait = 0;
199
200 switch (address & ~OFFSET_MASK) {
201 case BASE_BIOS:
202 if (gbaMemory->p->cpu.currentPC >> BASE_OFFSET == REGION_BIOS) {
203 if (address < SIZE_BIOS) {
204 value = ((int16_t*) gbaMemory->bios)[address >> 1];
205 } else {
206 value = 0;
207 }
208 } else {
209 value = gbaMemory->biosPrefetch;
210 }
211 break;
212 case BASE_WORKING_RAM:
213 value = ((int16_t*) gbaMemory->wram)[(address & (SIZE_WORKING_RAM - 1)) >> 1];
214 wait = gbaMemory->waitstates16[REGION_WORKING_RAM];
215 break;
216 case BASE_WORKING_IRAM:
217 value = ((int16_t*) gbaMemory->iwram)[(address & (SIZE_WORKING_IRAM - 1)) >> 1];
218 break;
219 case BASE_IO:
220 value = GBAIORead(gbaMemory->p, address & (SIZE_IO - 1));
221 break;
222 case BASE_PALETTE_RAM:
223 value = gbaMemory->p->video.palette[(address & (SIZE_PALETTE_RAM - 1)) >> 1];
224 break;
225 case BASE_VRAM:
226 value = gbaMemory->p->video.renderer->vram[(address & 0x0001FFFF) >> 1];
227 break;
228 case BASE_OAM:
229 value = gbaMemory->p->video.oam.raw[(address & (SIZE_OAM - 1)) >> 1];
230 break;
231 case BASE_CART0:
232 case BASE_CART0_EX:
233 case BASE_CART1:
234 case BASE_CART1_EX:
235 case BASE_CART2:
236 wait = gbaMemory->waitstates16[address >> BASE_OFFSET];
237 if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
238 value = ((int16_t*) gbaMemory->rom)[(address & (SIZE_CART0 - 1)) >> 1];
239 }
240 break;
241 case BASE_CART2_EX:
242 wait = gbaMemory->waitstates16[address >> BASE_OFFSET];
243 if (gbaMemory->savedata.type == SAVEDATA_EEPROM) {
244 value = GBASavedataReadEEPROM(&gbaMemory->savedata);
245 } else if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
246 value = ((uint16_t*) gbaMemory->rom)[(address & (SIZE_CART0 - 1)) >> 1];
247 }
248 break;
249 case BASE_CART_SRAM:
250 GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Load16: 0x%08X", address);
251 break;
252 default:
253 GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Load16: 0x%08X", address);
254 value = memory->load16(memory, gbaMemory->p->cpu.currentPC + (gbaMemory->p->cpu.executionMode == MODE_ARM ? WORD_SIZE_ARM : WORD_SIZE_THUMB) * 2, 0);
255 break;
256 }
257
258 if (cycleCounter) {
259 *cycleCounter += 2 + wait;
260 }
261 // Unaligned 16-bit loads are "unpredictable", but the GBA rotates them, so we have to, too.
262 int rotate = (address & 1) << 3;
263 return (value >> rotate) | ((value << rotate) & 0xFF00);
264}
265
266uint8_t GBALoadU8(struct ARMMemory* memory, uint32_t address, int* cycleCounter) {
267 return GBALoad8(memory, address, cycleCounter);
268}
269
270int8_t GBALoad8(struct ARMMemory* memory, uint32_t address, int* cycleCounter) {
271 struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
272 int8_t value = 0;
273 int wait = 0;
274
275 switch (address & ~OFFSET_MASK) {
276 case BASE_BIOS:
277 if (gbaMemory->p->cpu.currentPC >> BASE_OFFSET == REGION_BIOS) {
278 if (address < SIZE_BIOS) {
279 value = ((int8_t*) gbaMemory->bios)[address];
280 } else {
281 value = 0;
282 }
283 } else {
284 value = gbaMemory->biosPrefetch;
285 }
286 break;
287 case BASE_WORKING_RAM:
288 value = ((int8_t*) gbaMemory->wram)[address & (SIZE_WORKING_RAM - 1)];
289 wait = gbaMemory->waitstates16[REGION_WORKING_RAM];
290 break;
291 case BASE_WORKING_IRAM:
292 value = ((int8_t*) gbaMemory->iwram)[address & (SIZE_WORKING_IRAM - 1)];
293 break;
294 case BASE_IO:
295 value = (GBAIORead(gbaMemory->p, address & 0xFFFE) >> ((address & 0x0001) << 3)) & 0xFF;
296 break;
297 case BASE_PALETTE_RAM:
298 value = ((int8_t*) gbaMemory->p->video.renderer->palette)[address & (SIZE_PALETTE_RAM - 1)];
299 break;
300 case BASE_VRAM:
301 value = ((int8_t*) gbaMemory->p->video.renderer->vram)[address & 0x0001FFFF];
302 break;
303 case BASE_OAM:
304 GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Load8: 0x%08X", address);
305 break;
306 case BASE_CART0:
307 case BASE_CART0_EX:
308 case BASE_CART1:
309 case BASE_CART1_EX:
310 case BASE_CART2:
311 case BASE_CART2_EX:
312 wait = gbaMemory->waitstates16[address >> BASE_OFFSET];
313 if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) {
314 value = ((int8_t*) gbaMemory->rom)[address & (SIZE_CART0 - 1)];
315 }
316 break;
317 case BASE_CART_SRAM:
318 wait = gbaMemory->waitstates16[address >> BASE_OFFSET];
319 if (gbaMemory->savedata.type == SAVEDATA_NONE) {
320 GBASavedataInit(&gbaMemory->savedata, gbaMemory->p->savefile);
321 GBASavedataInitSRAM(&gbaMemory->savedata);
322 } else if (gbaMemory->savedata.type == SAVEDATA_SRAM) {
323 value = gbaMemory->savedata.data[address & (SIZE_CART_SRAM - 1)];
324 } else if (gbaMemory->savedata.type == SAVEDATA_FLASH512 || gbaMemory->savedata.type == SAVEDATA_FLASH1M) {
325 value = GBASavedataReadFlash(&gbaMemory->savedata, address);
326 }
327 break;
328 default:
329 GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Load8: 0x%08x", address);
330 value = memory->load16(memory, gbaMemory->p->cpu.currentPC + (gbaMemory->p->cpu.executionMode == MODE_ARM ? WORD_SIZE_ARM : WORD_SIZE_THUMB) * 2, 0) >> ((address & 1) << 3);
331 break;
332 }
333
334 if (cycleCounter) {
335 *cycleCounter += 2 + wait;
336 }
337 return value;
338}
339
340void GBAStore32(struct ARMMemory* memory, uint32_t address, int32_t value, int* cycleCounter) {
341 struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
342 int wait = 0;
343
344 switch (address & ~OFFSET_MASK) {
345 case BASE_WORKING_RAM:
346 gbaMemory->wram[(address & (SIZE_WORKING_RAM - 1)) >> 2] = value;
347 wait = gbaMemory->waitstates32[REGION_WORKING_RAM];
348 break;
349 case BASE_WORKING_IRAM:
350 gbaMemory->iwram[(address & (SIZE_WORKING_IRAM - 1)) >> 2] = value;
351 break;
352 case BASE_IO:
353 GBAIOWrite32(gbaMemory->p, address & (SIZE_IO - 1), value);
354 break;
355 case BASE_PALETTE_RAM:
356 ((int32_t*) gbaMemory->p->video.palette)[(address & (SIZE_PALETTE_RAM - 1)) >> 2] = value;
357 gbaMemory->p->video.renderer->writePalette(gbaMemory->p->video.renderer, (address & (SIZE_PALETTE_RAM - 1)) + 2, value >> 16);
358 gbaMemory->p->video.renderer->writePalette(gbaMemory->p->video.renderer, address & (SIZE_PALETTE_RAM - 1), value);
359 break;
360 case BASE_VRAM:
361 if ((address & OFFSET_MASK) < SIZE_VRAM - 2) {
362 ((int32_t*) gbaMemory->p->video.renderer->vram)[(address & 0x0001FFFF) >> 2] = value;
363 }
364 break;
365 case BASE_OAM:
366 ((int32_t*) gbaMemory->p->video.oam.raw)[(address & (SIZE_OAM - 1)) >> 2] = value;
367 gbaMemory->p->video.renderer->writeOAM(gbaMemory->p->video.renderer, (address & (SIZE_OAM - 4)) >> 1);
368 gbaMemory->p->video.renderer->writeOAM(gbaMemory->p->video.renderer, ((address & (SIZE_OAM - 4)) >> 1) + 1);
369 break;
370 case BASE_CART0:
371 GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store32: 0x%08X", address);
372 break;
373 case BASE_CART_SRAM:
374 GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store32: 0x%08X", address);
375 break;
376 default:
377 GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Store32: 0x%08X", address);
378 break;
379 }
380
381 if (cycleCounter) {
382 *cycleCounter += 1 + wait;
383 }
384}
385
386void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value, int* cycleCounter) {
387 struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
388 int wait = 0;
389
390 switch (address & ~OFFSET_MASK) {
391 case BASE_WORKING_RAM:
392 ((int16_t*) gbaMemory->wram)[(address & (SIZE_WORKING_RAM - 1)) >> 1] = value;
393 wait = gbaMemory->waitstates16[REGION_WORKING_RAM];
394 break;
395 case BASE_WORKING_IRAM:
396 ((int16_t*) gbaMemory->iwram)[(address & (SIZE_WORKING_IRAM - 1)) >> 1] = value;
397 break;
398 case BASE_IO:
399 GBAIOWrite(gbaMemory->p, address & (SIZE_IO - 1), value);
400 break;
401 case BASE_PALETTE_RAM:
402 gbaMemory->p->video.palette[(address & (SIZE_PALETTE_RAM - 1)) >> 1] = value;
403 gbaMemory->p->video.renderer->writePalette(gbaMemory->p->video.renderer, address & (SIZE_PALETTE_RAM - 1), value);
404 break;
405 case BASE_VRAM:
406 if ((address & OFFSET_MASK) < SIZE_VRAM) {
407 gbaMemory->p->video.renderer->vram[(address & 0x0001FFFF) >> 1] = value;
408 }
409 break;
410 case BASE_OAM:
411 gbaMemory->p->video.oam.raw[(address & (SIZE_OAM - 1)) >> 1] = value;
412 gbaMemory->p->video.renderer->writeOAM(gbaMemory->p->video.renderer, (address & (SIZE_OAM - 1)) >> 1);
413 break;
414 case BASE_CART0:
415 GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store16: 0x%08X", address);
416 break;
417 case BASE_CART2_EX:
418 if (gbaMemory->savedata.type == SAVEDATA_NONE) {
419 GBASavedataInit(&gbaMemory->savedata, gbaMemory->p->savefile);
420 GBASavedataInitEEPROM(&gbaMemory->savedata);
421 }
422 GBASavedataWriteEEPROM(&gbaMemory->savedata, value, 1);
423 break;
424 case BASE_CART_SRAM:
425 GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store16: 0x%08X", address);
426 break;
427 default:
428 GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Store16: 0x%08X", address);
429 break;
430 }
431
432 if (cycleCounter) {
433 *cycleCounter += 1 + wait;
434 }
435}
436
437void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value, int* cycleCounter) {
438 struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
439 int wait = 0;
440
441 switch (address & ~OFFSET_MASK) {
442 case BASE_WORKING_RAM:
443 ((int8_t*) gbaMemory->wram)[address & (SIZE_WORKING_RAM - 1)] = value;
444 wait = gbaMemory->waitstates16[REGION_WORKING_RAM];
445 break;
446 case BASE_WORKING_IRAM:
447 ((int8_t*) gbaMemory->iwram)[address & (SIZE_WORKING_IRAM - 1)] = value;
448 break;
449 case BASE_IO:
450 GBAIOWrite8(gbaMemory->p, address & (SIZE_IO - 1), value);
451 break;
452 case BASE_PALETTE_RAM:
453 GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store8: 0x%08X", address);
454 break;
455 case BASE_VRAM:
456 if (address >= 0x06018000) {
457 // TODO: check BG mode
458 GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Cannot Store8 to OBJ: 0x%08X", address);
459 break;
460 }
461 ((int8_t*) gbaMemory->p->video.renderer->vram)[address & 0x1FFFE] = value;
462 ((int8_t*) gbaMemory->p->video.renderer->vram)[(address & 0x1FFFE) | 1] = value;
463 break;
464 case BASE_OAM:
465 GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Cannot Store8 to OAM: 0x%08X", address);
466 break;
467 case BASE_CART0:
468 GBALog(gbaMemory->p, GBA_LOG_STUB, "Unimplemented memory Store8: 0x%08X", address);
469 break;
470 case BASE_CART_SRAM:
471 if (gbaMemory->savedata.type == SAVEDATA_NONE) {
472 GBASavedataInit(&gbaMemory->savedata, gbaMemory->p->savefile);
473 if (address == SAVEDATA_FLASH_BASE) {
474 GBASavedataInitFlash(&gbaMemory->savedata);
475 } else {
476 GBASavedataInitSRAM(&gbaMemory->savedata);
477 }
478 }
479 if (gbaMemory->savedata.type == SAVEDATA_FLASH512 || gbaMemory->savedata.type == SAVEDATA_FLASH1M) {
480 GBASavedataWriteFlash(&gbaMemory->savedata, address, value);
481 } else if (gbaMemory->savedata.type == SAVEDATA_SRAM) {
482 gbaMemory->savedata.data[address & (SIZE_CART_SRAM - 1)] = value;
483 }
484 wait = gbaMemory->waitstates16[REGION_CART_SRAM];
485 break;
486 default:
487 GBALog(gbaMemory->p, GBA_LOG_GAME_ERROR, "Bad memory Store8: 0x%08X", address);
488 break;
489 }
490
491 if (cycleCounter) {
492 *cycleCounter += 1 + wait;
493 }
494}
495
496static int GBAWaitMultiple(struct ARMMemory* memory, uint32_t startAddress, int count) {
497 struct GBAMemory* gbaMemory = (struct GBAMemory*) memory;
498 int wait = 1 + gbaMemory->waitstates32[startAddress >> BASE_OFFSET];
499 wait += (1 + gbaMemory->waitstatesSeq32[startAddress >> BASE_OFFSET]) * (count - 1);
500 return wait;
501}
502
503void GBAAdjustWaitstates(struct GBAMemory* memory, uint16_t parameters) {
504 int sram = parameters & 0x0003;
505 int ws0 = (parameters & 0x000C) >> 2;
506 int ws0seq = (parameters & 0x0010) >> 4;
507 int ws1 = (parameters & 0x0060) >> 5;
508 int ws1seq = (parameters & 0x0080) >> 7;
509 int ws2 = (parameters & 0x0300) >> 8;
510 int ws2seq = (parameters & 0x0400) >> 10;
511 int prefetch = parameters & 0x4000;
512
513 memory->waitstates16[REGION_CART_SRAM] = GBA_ROM_WAITSTATES[sram];
514 memory->waitstatesSeq16[REGION_CART_SRAM] = GBA_ROM_WAITSTATES[sram];
515 memory->waitstates32[REGION_CART_SRAM] = 2 * GBA_ROM_WAITSTATES[sram] + 1;
516 memory->waitstatesSeq32[REGION_CART_SRAM] = 2 * GBA_ROM_WAITSTATES[sram] + 1;
517
518 memory->waitstates16[REGION_CART0] = memory->waitstates16[REGION_CART0_EX] = GBA_ROM_WAITSTATES[ws0];
519 memory->waitstates16[REGION_CART1] = memory->waitstates16[REGION_CART1_EX] = GBA_ROM_WAITSTATES[ws1];
520 memory->waitstates16[REGION_CART2] = memory->waitstates16[REGION_CART2_EX] = GBA_ROM_WAITSTATES[ws2];
521
522 memory->waitstatesSeq16[REGION_CART0] = memory->waitstatesSeq16[REGION_CART0_EX] = GBA_ROM_WAITSTATES_SEQ[ws0seq];
523 memory->waitstatesSeq16[REGION_CART1] = memory->waitstatesSeq16[REGION_CART1_EX] = GBA_ROM_WAITSTATES_SEQ[ws1seq + 2];
524 memory->waitstatesSeq16[REGION_CART2] = memory->waitstatesSeq16[REGION_CART2_EX] = GBA_ROM_WAITSTATES_SEQ[ws2seq + 4];
525
526 memory->waitstates32[REGION_CART0] = memory->waitstates32[REGION_CART0_EX] = memory->waitstates16[REGION_CART0] + 1 + memory->waitstatesSeq16[REGION_CART0];
527 memory->waitstates32[REGION_CART1] = memory->waitstates32[REGION_CART1_EX] = memory->waitstates16[REGION_CART1] + 1 + memory->waitstatesSeq16[REGION_CART1];
528 memory->waitstates32[REGION_CART2] = memory->waitstates32[REGION_CART2_EX] = memory->waitstates16[REGION_CART2] + 1 + memory->waitstatesSeq16[REGION_CART2];
529
530 memory->waitstatesSeq32[REGION_CART0] = memory->waitstatesSeq32[REGION_CART0_EX] = 2 * memory->waitstatesSeq16[REGION_CART0] + 1;
531 memory->waitstatesSeq32[REGION_CART1] = memory->waitstatesSeq32[REGION_CART1_EX] = 2 * memory->waitstatesSeq16[REGION_CART1] + 1;
532 memory->waitstatesSeq32[REGION_CART2] = memory->waitstatesSeq32[REGION_CART2_EX] = 2 * memory->waitstatesSeq16[REGION_CART2] + 1;
533
534 if (!prefetch) {
535 memory->waitstatesPrefetch16[REGION_CART0] = memory->waitstatesPrefetch16[REGION_CART0_EX] = memory->waitstatesSeq16[REGION_CART0];
536 memory->waitstatesPrefetch16[REGION_CART1] = memory->waitstatesPrefetch16[REGION_CART1_EX] = memory->waitstatesSeq16[REGION_CART1];
537 memory->waitstatesPrefetch16[REGION_CART2] = memory->waitstatesPrefetch16[REGION_CART2_EX] = memory->waitstatesSeq16[REGION_CART2];
538
539 memory->waitstatesPrefetch32[REGION_CART0] = memory->waitstatesPrefetch32[REGION_CART0_EX] = memory->waitstatesSeq32[REGION_CART0];
540 memory->waitstatesPrefetch32[REGION_CART1] = memory->waitstatesPrefetch32[REGION_CART1_EX] = memory->waitstatesSeq32[REGION_CART1];
541 memory->waitstatesPrefetch32[REGION_CART2] = memory->waitstatesPrefetch32[REGION_CART2_EX] = memory->waitstatesSeq32[REGION_CART2];
542 } else {
543 memory->waitstatesPrefetch16[REGION_CART0] = memory->waitstatesPrefetch16[REGION_CART0_EX] = 0;
544 memory->waitstatesPrefetch16[REGION_CART1] = memory->waitstatesPrefetch16[REGION_CART1_EX] = 0;
545 memory->waitstatesPrefetch16[REGION_CART2] = memory->waitstatesPrefetch16[REGION_CART2_EX] = 0;
546
547 memory->waitstatesPrefetch32[REGION_CART0] = memory->waitstatesPrefetch32[REGION_CART0_EX] = 0;
548 memory->waitstatesPrefetch32[REGION_CART1] = memory->waitstatesPrefetch32[REGION_CART1_EX] = 0;
549 memory->waitstatesPrefetch32[REGION_CART2] = memory->waitstatesPrefetch32[REGION_CART2_EX] = 0;
550 }
551
552 memory->d.activePrefetchCycles32 = memory->waitstatesPrefetch32[memory->activeRegion];
553 memory->d.activePrefetchCycles16 = memory->waitstatesPrefetch16[memory->activeRegion];
554 memory->d.activeNonseqCycles32 = memory->waitstates32[memory->activeRegion];
555 memory->d.activeNonseqCycles16 = memory->waitstates16[memory->activeRegion];
556}
557
558int32_t GBAMemoryProcessEvents(struct GBAMemory* memory, int32_t cycles) {
559 struct GBADMA* dma;
560 int32_t test = INT_MAX;
561
562 dma = &memory->dma[0];
563 dma->nextIRQ -= cycles;
564 if (dma->enable && dma->doIrq && dma->nextIRQ) {
565 if (dma->nextIRQ <= 0) {
566 dma->nextIRQ = INT_MAX;
567 GBARaiseIRQ(memory->p, IRQ_DMA0);
568 } else if (dma->nextIRQ < test) {
569 test = dma->nextIRQ;
570 }
571 }
572
573 dma = &memory->dma[1];
574 dma->nextIRQ -= cycles;
575 if (dma->enable && dma->doIrq && dma->nextIRQ) {
576 if (dma->nextIRQ <= 0) {
577 dma->nextIRQ = INT_MAX;
578 GBARaiseIRQ(memory->p, IRQ_DMA1);
579 } else if (dma->nextIRQ < test) {
580 test = dma->nextIRQ;
581 }
582 }
583
584 dma = &memory->dma[2];
585 dma->nextIRQ -= cycles;
586 if (dma->enable && dma->doIrq && dma->nextIRQ) {
587 if (dma->nextIRQ <= 0) {
588 dma->nextIRQ = INT_MAX;
589 GBARaiseIRQ(memory->p, IRQ_DMA2);
590 } else if (dma->nextIRQ < test) {
591 test = dma->nextIRQ;
592 }
593 }
594
595 dma = &memory->dma[3];
596 dma->nextIRQ -= cycles;
597 if (dma->enable && dma->doIrq && dma->nextIRQ) {
598 if (dma->nextIRQ <= 0) {
599 dma->nextIRQ = INT_MAX;
600 GBARaiseIRQ(memory->p, IRQ_DMA3);
601 } else if (dma->nextIRQ < test) {
602 test = dma->nextIRQ;
603 }
604 }
605
606 return test;
607}
608
609void GBAMemoryWriteDMASAD(struct GBAMemory* memory, int dma, uint32_t address) {
610 memory->dma[dma].source = address & 0xFFFFFFFE;
611}
612
613void GBAMemoryWriteDMADAD(struct GBAMemory* memory, int dma, uint32_t address) {
614 memory->dma[dma].dest = address & 0xFFFFFFFE;
615}
616
617void GBAMemoryWriteDMACNT_LO(struct GBAMemory* memory, int dma, uint16_t count) {
618 memory->dma[dma].count = count ? count : (dma == 3 ? 0x10000 : 0x4000);
619}
620
621uint16_t GBAMemoryWriteDMACNT_HI(struct GBAMemory* memory, int dma, uint16_t control) {
622 struct GBADMA* currentDma = &memory->dma[dma];
623 int wasEnabled = currentDma->enable;
624 currentDma->packed = control;
625 currentDma->nextIRQ = 0;
626
627 if (currentDma->drq) {
628 GBALog(memory->p, GBA_LOG_STUB, "DRQ not implemented");
629 }
630
631 if (!wasEnabled && currentDma->enable) {
632 currentDma->nextSource = currentDma->source;
633 currentDma->nextDest = currentDma->dest;
634 currentDma->nextCount = currentDma->count;
635 GBAMemoryScheduleDMA(memory, dma, currentDma);
636 }
637 // If the DMA has already occurred, this value might have changed since the function started
638 return currentDma->packed;
639};
640
641void GBAMemoryScheduleDMA(struct GBAMemory* memory, int number, struct GBADMA* info) {
642 switch (info->timing) {
643 case DMA_TIMING_NOW:
644 GBAMemoryServiceDMA(memory, number, info);
645 break;
646 case DMA_TIMING_HBLANK:
647 // Handled implicitly
648 break;
649 case DMA_TIMING_VBLANK:
650 // Handled implicitly
651 break;
652 case DMA_TIMING_CUSTOM:
653 switch (number) {
654 case 0:
655 GBALog(memory->p, GBA_LOG_WARN, "Discarding invalid DMA0 scheduling");
656 break;
657 case 1:
658 case 2:
659 GBAAudioScheduleFifoDma(&memory->p->audio, number, info);
660 break;
661 case 3:
662 //this.cpu.irq.video.scheduleVCaptureDma(dma, info);
663 break;
664 }
665 }
666}
667
668void GBAMemoryRunHblankDMAs(struct GBAMemory* memory) {
669 struct GBADMA* dma;
670 int i;
671 for (i = 0; i < 4; ++i) {
672 dma = &memory->dma[i];
673 if (dma->enable && dma->timing == DMA_TIMING_HBLANK) {
674 GBAMemoryServiceDMA(memory, i, dma);
675 }
676 }
677}
678
679void GBAMemoryRunVblankDMAs(struct GBAMemory* memory) {
680 struct GBADMA* dma;
681 int i;
682 for (i = 0; i < 4; ++i) {
683 dma = &memory->dma[i];
684 if (dma->enable && dma->timing == DMA_TIMING_VBLANK) {
685 GBAMemoryServiceDMA(memory, i, dma);
686 }
687 }
688}
689
690void GBAMemoryServiceDMA(struct GBAMemory* memory, int number, struct GBADMA* info) {
691 if (!info->enable) {
692 // There was a DMA scheduled that got canceled
693 return;
694 }
695
696 uint32_t width = info->width ? 4 : 2;
697 int sourceOffset = DMA_OFFSET[info->srcControl] * width;
698 int destOffset = DMA_OFFSET[info->dstControl] * width;
699 int32_t wordsRemaining = info->nextCount;
700 uint32_t source = info->nextSource;
701 uint32_t dest = info->nextDest;
702 uint32_t sourceRegion = source >> BASE_OFFSET;
703 uint32_t destRegion = dest >> BASE_OFFSET;
704
705 if (width == 4) {
706 int32_t word;
707 source &= 0xFFFFFFFC;
708 dest &= 0xFFFFFFFC;
709 while (wordsRemaining--) {
710 word = memory->d.load32(&memory->d, source, 0);
711 memory->d.store32(&memory->d, dest, word, 0);
712 source += sourceOffset;
713 dest += destOffset;
714 }
715 } else {
716 uint16_t word;
717 if (sourceRegion == REGION_CART2_EX && memory->savedata.type == SAVEDATA_EEPROM) {
718 while (wordsRemaining--) {
719 word = GBASavedataReadEEPROM(&memory->savedata);
720 memory->d.store16(&memory->d, dest, word, 0);
721 source += sourceOffset;
722 dest += destOffset;
723 }
724 } else if (destRegion == REGION_CART2_EX) {
725 if (memory->savedata.type == SAVEDATA_NONE) {
726 GBASavedataInit(&memory->savedata, memory->p->savefile);
727 GBASavedataInitEEPROM(&memory->savedata);
728 }
729 while (wordsRemaining) {
730 word = memory->d.load16(&memory->d, source, 0);
731 GBASavedataWriteEEPROM(&memory->savedata, word, wordsRemaining);
732 source += sourceOffset;
733 dest += destOffset;
734 --wordsRemaining;
735 }
736 } else {
737 while (wordsRemaining--) {
738 word = memory->d.load16(&memory->d, source, 0);
739 memory->d.store16(&memory->d, dest, word, 0);
740 source += sourceOffset;
741 dest += destOffset;
742 }
743 }
744 }
745
746 if (info->doIrq) {
747 info->nextIRQ = memory->p->cpu.cycles + 2;
748 info->nextIRQ += (width == 4 ? memory->waitstates32[sourceRegion] + memory->waitstates32[destRegion]
749 : memory->waitstates16[sourceRegion] + memory->waitstates16[destRegion]);
750 info->nextIRQ += (info->count - 1) * (width == 4 ? memory->waitstatesSeq32[sourceRegion] + memory->waitstatesSeq32[destRegion]
751 : memory->waitstatesSeq16[sourceRegion] + memory->waitstatesSeq16[destRegion]);
752 }
753
754 info->nextSource = source;
755 info->nextDest = dest;
756 info->nextCount = wordsRemaining;
757
758 if (!info->repeat) {
759 info->enable = 0;
760
761 // Clear the enable bit in memory
762 memory->io[(REG_DMA0CNT_HI + number * (REG_DMA1CNT_HI - REG_DMA0CNT_HI)) >> 1] &= 0x7FE0;
763 } else {
764 info->nextCount = info->count;
765 if (info->dstControl == DMA_INCREMENT_RELOAD) {
766 info->nextDest = info->dest;
767 }
768 GBAMemoryScheduleDMA(memory, number, info);
769 }
770}