src/ds/io.c (view raw)
1/* Copyright (c) 2013-2017 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/ds/io.h>
7
8#include <mgba/core/interface.h>
9#include <mgba/internal/ds/audio.h>
10#include <mgba/internal/ds/ds.h>
11#include <mgba/internal/ds/gx.h>
12#include <mgba/internal/ds/ipc.h>
13#include <mgba/internal/ds/slot1.h>
14#include <mgba/internal/ds/spi.h>
15
16mLOG_DEFINE_CATEGORY(DS_IO, "DS I/O", "ds.io");
17
18static void _DSHaltCNT(struct DSCommon* dscore, uint8_t value) {
19 switch (value >> 6) {
20 case 0:
21 default:
22 break;
23 case 1:
24 mLOG(DS_IO, STUB, "Enter GBA mode not supported");
25 break;
26 case 2:
27 ARMHalt(dscore->cpu);
28 break;
29 case 3:
30 mLOG(DS_IO, STUB, "Enter sleep mode not supported");
31 break;
32 }
33}
34
35static uint16_t _scheduleDiv(struct DS* ds, uint16_t control) {
36 mTimingDeschedule(&ds->ds9.timing, &ds->divEvent);
37 mTimingSchedule(&ds->ds9.timing, &ds->divEvent, (control & 3) ? 36 : 68);
38 return control | 0x8000;
39}
40
41static uint16_t _scheduleSqrt(struct DS* ds, uint16_t control) {
42 mTimingDeschedule(&ds->ds9.timing, &ds->sqrtEvent);
43 mTimingSchedule(&ds->ds9.timing, &ds->sqrtEvent, 26);
44 return control | 0x8000;
45}
46
47static uint32_t DSIOWrite(struct DSCommon* dscore, uint32_t address, uint16_t value) {
48 switch (address) {
49 // Video
50 case DS_REG_DISPSTAT:
51 DSVideoWriteDISPSTAT(dscore, value);
52 break;
53
54 // DMA Fill
55 case DS_REG_DMA0FILL_LO:
56 case DS_REG_DMA0FILL_HI:
57 case DS_REG_DMA1FILL_LO:
58 case DS_REG_DMA1FILL_HI:
59 case DS_REG_DMA2FILL_LO:
60 case DS_REG_DMA2FILL_HI:
61 case DS_REG_DMA3FILL_LO:
62 case DS_REG_DMA3FILL_HI:
63 break;
64
65 // Timers
66 case DS_REG_TM0CNT_LO:
67 GBATimerWriteTMCNT_LO(&dscore->timers[0], value);
68 return 0x20000;
69 case DS_REG_TM1CNT_LO:
70 GBATimerWriteTMCNT_LO(&dscore->timers[1], value);
71 return 0x20000;
72 case DS_REG_TM2CNT_LO:
73 GBATimerWriteTMCNT_LO(&dscore->timers[2], value);
74 return 0x20000;
75 case DS_REG_TM3CNT_LO:
76 GBATimerWriteTMCNT_LO(&dscore->timers[3], value);
77 return 0x20000;
78
79 case DS_REG_TM0CNT_HI:
80 value &= 0x00C7;
81 DSTimerWriteTMCNT_HI(&dscore->timers[0], &dscore->timing, dscore->cpu, &dscore->memory.io[DS_REG_TM0CNT_LO >> 1], value);
82 break;
83 case DS_REG_TM1CNT_HI:
84 value &= 0x00C7;
85 DSTimerWriteTMCNT_HI(&dscore->timers[1], &dscore->timing, dscore->cpu, &dscore->memory.io[DS_REG_TM1CNT_LO >> 1], value);
86 break;
87 case DS_REG_TM2CNT_HI:
88 value &= 0x00C7;
89 DSTimerWriteTMCNT_HI(&dscore->timers[2], &dscore->timing, dscore->cpu, &dscore->memory.io[DS_REG_TM2CNT_LO >> 1], value);
90 break;
91 case DS_REG_TM3CNT_HI:
92 value &= 0x00C7;
93 DSTimerWriteTMCNT_HI(&dscore->timers[3], &dscore->timing, dscore->cpu, &dscore->memory.io[DS_REG_TM3CNT_LO >> 1], value);
94 break;
95
96 // IPC
97 case DS_REG_IPCSYNC:
98 value &= 0x6F00;
99 value |= dscore->memory.io[address >> 1] & 0x000F;
100 DSIPCWriteSYNC(dscore->ipc->cpu, dscore->ipc->memory.io, value);
101 break;
102 case DS_REG_IPCFIFOCNT:
103 value = DSIPCWriteFIFOCNT(dscore, value);
104 break;
105
106 // Cart bus
107 case DS_REG_AUXSPICNT:
108 if (dscore->memory.slot1Access) {
109 value = DSSlot1Configure(dscore->p, value);
110 dscore->ipc->memory.io[address >> 1] = value;
111 } else {
112 mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
113 return 0;
114 }
115 break;
116 case DS_REG_AUXSPIDATA:
117 if (dscore->memory.slot1Access) {
118 DSSlot1WriteSPI(dscore, value);
119 dscore->ipc->memory.io[address >> 1] = value;
120 } else {
121 mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
122 return 0;
123 }
124 break;
125 case DS_REG_ROMCNT_HI:
126 if (dscore->memory.slot1Access) {
127 DSSlot1ROMCNT cnt = value << 16;
128 cnt |= dscore->memory.io[(address - 2) >> 1];
129 cnt = DSSlot1Control(dscore->p, cnt);
130 value = cnt >> 16;
131 dscore->ipc->memory.io[address >> 1] = value;
132 } else {
133 mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
134 return 0;
135 }
136 break;
137 case DS_REG_ROMCNT_LO:
138 case DS_REG_ROMCMD_0:
139 case DS_REG_ROMCMD_2:
140 case DS_REG_ROMCMD_4:
141 case DS_REG_ROMCMD_6:
142 if (dscore->memory.slot1Access) {
143 dscore->ipc->memory.io[address >> 1] = value;
144 } else {
145 mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
146 return 0;
147 }
148 break;
149
150 // Interrupts
151 case DS_REG_IME:
152 DSWriteIME(dscore->cpu, dscore->memory.io, value);
153 break;
154 case 0x20A:
155 value = 0;
156 // Some bad interrupt libraries will write to this
157 break;
158 case DS_REG_IF_LO:
159 case DS_REG_IF_HI:
160 value = dscore->memory.io[address >> 1] & ~value;
161 DSGXUpdateGXSTAT(&dscore->p->gx);
162 break;
163 default:
164 return 0;
165 }
166 return value | 0x10000;
167}
168
169uint32_t DSIOWrite32(struct DSCommon* dscore, uint32_t address, uint32_t value) {
170 switch (address) {
171 case DS_REG_DMA0SAD_LO:
172 value = DSDMAWriteSAD(dscore, 0, value);
173 break;
174 case DS_REG_DMA1SAD_LO:
175 value = DSDMAWriteSAD(dscore, 1, value);
176 break;
177 case DS_REG_DMA2SAD_LO:
178 value = DSDMAWriteSAD(dscore, 2, value);
179 break;
180 case DS_REG_DMA3SAD_LO:
181 value = DSDMAWriteSAD(dscore, 3, value);
182 break;
183
184 case DS_REG_DMA0DAD_LO:
185 value = DSDMAWriteDAD(dscore, 0, value);
186 break;
187 case DS_REG_DMA1DAD_LO:
188 value = DSDMAWriteDAD(dscore, 1, value);
189 break;
190 case DS_REG_DMA2DAD_LO:
191 value = DSDMAWriteDAD(dscore, 2, value);
192 break;
193 case DS_REG_DMA3DAD_LO:
194 value = DSDMAWriteDAD(dscore, 3, value);
195 break;
196
197 case DS_REG_IPCFIFOSEND_LO:
198 DSIPCWriteFIFO(dscore, value);
199 break;
200 case DS_REG_IE_LO:
201 DSWriteIE(dscore->cpu, dscore->memory.io, value);
202 break;
203 }
204
205 return value;
206}
207
208static uint16_t DSIOReadExKeyInput(struct DS* ds) {
209 uint16_t input = 0;
210 if (ds->keyCallback) {
211 input = ds->keyCallback->readKeys(ds->keyCallback);
212 } else if (ds->keySource) {
213 input = *ds->keySource;
214 }
215 input = ~(input >> 10) & 0x3;
216 input |= 0x3C;
217 input |= ds->memory.io7[DS7_REG_EXTKEYIN >> 1] & 0xC0;
218 return input;
219}
220
221static uint16_t DSIOReadKeyInput(struct DS* ds) {
222 uint16_t input = 0;
223 if (ds->keyCallback) {
224 input = ds->keyCallback->readKeys(ds->keyCallback);
225 } else if (ds->keySource) {
226 input = *ds->keySource;
227 }
228 // TODO: Put back
229 /*if (!dscore->p->allowOpposingDirections) {
230 unsigned rl = input & 0x030;
231 unsigned ud = input & 0x0C0;
232 input &= 0x30F;
233 if (rl != 0x030) {
234 input |= rl;
235 }
236 if (ud != 0x0C0) {
237 input |= ud;
238 }
239 }*/
240 return ~input & 0x3FF;
241}
242
243static void DSIOUpdateTimer(struct DSCommon* dscore, uint32_t address) {
244 switch (address) {
245 case DS_REG_TM0CNT_LO:
246 GBATimerUpdateRegisterInternal(&dscore->timers[0], &dscore->timing, dscore->cpu, &dscore->memory.io[address >> 1], 0);
247 break;
248 case DS_REG_TM1CNT_LO:
249 GBATimerUpdateRegisterInternal(&dscore->timers[1], &dscore->timing, dscore->cpu, &dscore->memory.io[address >> 1], 0);
250 break;
251 case DS_REG_TM2CNT_LO:
252 GBATimerUpdateRegisterInternal(&dscore->timers[2], &dscore->timing, dscore->cpu, &dscore->memory.io[address >> 1], 0);
253 break;
254 case DS_REG_TM3CNT_LO:
255 GBATimerUpdateRegisterInternal(&dscore->timers[3], &dscore->timing, dscore->cpu, &dscore->memory.io[address >> 1], 0);
256 break;
257 }
258}
259
260void DS7IOInit(struct DS* ds) {
261 memset(ds->memory.io7, 0, sizeof(ds->memory.io7));
262 ds->memory.io7[DS_REG_IPCFIFOCNT >> 1] = 0x0101;
263 ds->memory.io7[DS_REG_POSTFLG >> 1] = 0x0001;
264 ds->memory.io7[DS7_REG_EXTKEYIN >> 1] = 0x007F;
265}
266
267void DS7IOWrite(struct DS* ds, uint32_t address, uint16_t value) {
268 switch (address) {
269 case DS7_REG_SPICNT:
270 value &= 0xCF83;
271 value = DSSPIWriteControl(ds, value);
272 break;
273 case DS7_REG_SPIDATA:
274 DSSPIWrite(ds, value);
275 break;
276 case DS7_REG_RTC:
277 value = DSWriteRTC(ds, value);
278 break;
279 case DS7_REG_SOUND0CNT_LO:
280 case DS7_REG_SOUND1CNT_LO:
281 case DS7_REG_SOUND2CNT_LO:
282 case DS7_REG_SOUND3CNT_LO:
283 case DS7_REG_SOUND4CNT_LO:
284 case DS7_REG_SOUND5CNT_LO:
285 case DS7_REG_SOUND6CNT_LO:
286 case DS7_REG_SOUND7CNT_LO:
287 case DS7_REG_SOUND8CNT_LO:
288 case DS7_REG_SOUND9CNT_LO:
289 case DS7_REG_SOUNDACNT_LO:
290 case DS7_REG_SOUNDBCNT_LO:
291 case DS7_REG_SOUNDCCNT_LO:
292 case DS7_REG_SOUNDDCNT_LO:
293 case DS7_REG_SOUNDECNT_LO:
294 case DS7_REG_SOUNDFCNT_LO:
295 value &= 0x837F;
296 DSAudioWriteSOUNDCNT_LO(&ds->audio, (address - DS7_REG_SOUND0CNT_LO) >> 4, value);
297 break;
298 case DS7_REG_SOUND0CNT_HI:
299 case DS7_REG_SOUND1CNT_HI:
300 case DS7_REG_SOUND2CNT_HI:
301 case DS7_REG_SOUND3CNT_HI:
302 case DS7_REG_SOUND4CNT_HI:
303 case DS7_REG_SOUND5CNT_HI:
304 case DS7_REG_SOUND6CNT_HI:
305 case DS7_REG_SOUND7CNT_HI:
306 case DS7_REG_SOUND8CNT_HI:
307 case DS7_REG_SOUND9CNT_HI:
308 case DS7_REG_SOUNDACNT_HI:
309 case DS7_REG_SOUNDBCNT_HI:
310 case DS7_REG_SOUNDCCNT_HI:
311 case DS7_REG_SOUNDDCNT_HI:
312 case DS7_REG_SOUNDECNT_HI:
313 case DS7_REG_SOUNDFCNT_HI:
314 value &= 0xFF7F;
315 DSAudioWriteSOUNDCNT_HI(&ds->audio, (address - DS7_REG_SOUND0CNT_HI) >> 4, value);
316 break;
317 case DS7_REG_SOUND0TMR:
318 case DS7_REG_SOUND1TMR:
319 case DS7_REG_SOUND2TMR:
320 case DS7_REG_SOUND3TMR:
321 case DS7_REG_SOUND4TMR:
322 case DS7_REG_SOUND5TMR:
323 case DS7_REG_SOUND6TMR:
324 case DS7_REG_SOUND7TMR:
325 case DS7_REG_SOUND8TMR:
326 case DS7_REG_SOUND9TMR:
327 case DS7_REG_SOUNDATMR:
328 case DS7_REG_SOUNDBTMR:
329 case DS7_REG_SOUNDCTMR:
330 case DS7_REG_SOUNDDTMR:
331 case DS7_REG_SOUNDETMR:
332 case DS7_REG_SOUNDFTMR:
333 DSAudioWriteSOUNDTMR(&ds->audio, (address - DS7_REG_SOUND0TMR) >> 4, value);
334 break;
335 case DS7_REG_SOUND0PNT:
336 case DS7_REG_SOUND1PNT:
337 case DS7_REG_SOUND2PNT:
338 case DS7_REG_SOUND3PNT:
339 case DS7_REG_SOUND4PNT:
340 case DS7_REG_SOUND5PNT:
341 case DS7_REG_SOUND6PNT:
342 case DS7_REG_SOUND7PNT:
343 case DS7_REG_SOUND8PNT:
344 case DS7_REG_SOUND9PNT:
345 case DS7_REG_SOUNDAPNT:
346 case DS7_REG_SOUNDBPNT:
347 case DS7_REG_SOUNDCPNT:
348 case DS7_REG_SOUNDDPNT:
349 case DS7_REG_SOUNDEPNT:
350 case DS7_REG_SOUNDFPNT:
351 DSAudioWriteSOUNDPNT(&ds->audio, (address - DS7_REG_SOUND0PNT) >> 4, value);
352 break;
353 default:
354 {
355 uint32_t v2 = DSIOWrite(&ds->ds7, address, value);
356 if (v2 & 0x10000) {
357 value = v2;
358 break;
359 } else if (v2 & 0x20000) {
360 return;
361 }
362 }
363 if (address >= DS7_IO_BASE_WIFI && address < DS7_IO_END_WIFI) {
364 DSWifiWriteIO(ds, address & 0x7FFF, value);
365 return;
366 }
367 mLOG(DS_IO, STUB, "Stub DS7 I/O register write: %06X:%04X", address, value);
368 if (address >= DS7_REG_MAX) {
369 mLOG(DS_IO, GAME_ERROR, "Write to unused DS7 I/O register: %06X:%04X", address, value);
370 return;
371 }
372 break;
373 }
374 ds->memory.io7[address >> 1] = value;
375}
376
377void DS7IOWrite8(struct DS* ds, uint32_t address, uint8_t value) {
378 if (address == DS7_REG_HALTCNT) {
379 _DSHaltCNT(&ds->ds7, value);
380 return;
381 }
382 if (address < DS7_REG_MAX) {
383 uint16_t value16 = value << (8 * (address & 1));
384 value16 |= (ds->ds7.memory.io[(address & 0xFFF) >> 1]) & ~(0xFF << (8 * (address & 1)));
385 DS7IOWrite(ds, address & 0xFFFFFFFE, value16);
386 } else {
387 mLOG(DS, STUB, "Writing to unknown DS7 register: %08X:%02X", address, value);
388 }
389}
390
391void DS7IOWrite32(struct DS* ds, uint32_t address, uint32_t value) {
392 switch (address) {
393 case DS_REG_DMA0SAD_LO:
394 case DS_REG_DMA1SAD_LO:
395 case DS_REG_DMA2SAD_LO:
396 case DS_REG_DMA3SAD_LO:
397 case DS_REG_DMA0DAD_LO:
398 case DS_REG_DMA1DAD_LO:
399 case DS_REG_DMA2DAD_LO:
400 case DS_REG_DMA3DAD_LO:
401 case DS_REG_IPCFIFOSEND_LO:
402 case DS_REG_IE_LO:
403 value = DSIOWrite32(&ds->ds7, address, value);
404 break;
405
406 case DS_REG_DMA0CNT_LO:
407 DS7DMAWriteCNT(&ds->ds7, 0, value);
408 break;
409 case DS_REG_DMA1CNT_LO:
410 DS7DMAWriteCNT(&ds->ds7, 1, value);
411 break;
412 case DS_REG_DMA2CNT_LO:
413 DS7DMAWriteCNT(&ds->ds7, 2, value);
414 break;
415 case DS_REG_DMA3CNT_LO:
416 DS7DMAWriteCNT(&ds->ds7, 3, value);
417 break;
418
419 case DS7_REG_SOUND0SAD_LO:
420 case DS7_REG_SOUND1SAD_LO:
421 case DS7_REG_SOUND2SAD_LO:
422 case DS7_REG_SOUND3SAD_LO:
423 case DS7_REG_SOUND4SAD_LO:
424 case DS7_REG_SOUND5SAD_LO:
425 case DS7_REG_SOUND6SAD_LO:
426 case DS7_REG_SOUND7SAD_LO:
427 case DS7_REG_SOUND8SAD_LO:
428 case DS7_REG_SOUND9SAD_LO:
429 case DS7_REG_SOUNDASAD_LO:
430 case DS7_REG_SOUNDBSAD_LO:
431 case DS7_REG_SOUNDCSAD_LO:
432 case DS7_REG_SOUNDDSAD_LO:
433 case DS7_REG_SOUNDESAD_LO:
434 case DS7_REG_SOUNDFSAD_LO:
435 DSAudioWriteSOUNDSAD(&ds->audio, (address - DS7_REG_SOUND0SAD_LO) >> 4, value);
436 break;
437
438 case DS7_REG_SOUND0LEN_LO:
439 case DS7_REG_SOUND1LEN_LO:
440 case DS7_REG_SOUND2LEN_LO:
441 case DS7_REG_SOUND3LEN_LO:
442 case DS7_REG_SOUND4LEN_LO:
443 case DS7_REG_SOUND5LEN_LO:
444 case DS7_REG_SOUND6LEN_LO:
445 case DS7_REG_SOUND7LEN_LO:
446 case DS7_REG_SOUND8LEN_LO:
447 case DS7_REG_SOUND9LEN_LO:
448 case DS7_REG_SOUNDALEN_LO:
449 case DS7_REG_SOUNDBLEN_LO:
450 case DS7_REG_SOUNDCLEN_LO:
451 case DS7_REG_SOUNDDLEN_LO:
452 case DS7_REG_SOUNDELEN_LO:
453 case DS7_REG_SOUNDFLEN_LO:
454 value &= 0x3FFFFF;
455 DSAudioWriteSOUNDLEN(&ds->audio, (address - DS7_REG_SOUND0LEN_LO) >> 4, value);
456 break;
457
458 default:
459 DS7IOWrite(ds, address, value & 0xFFFF);
460 DS7IOWrite(ds, address | 2, value >> 16);
461 return;
462 }
463 ds->ds7.memory.io[address >> 1] = value;
464 ds->ds7.memory.io[(address >> 1) + 1] = value >> 16;
465}
466
467uint16_t DS7IORead(struct DS* ds, uint32_t address) {
468 switch (address) {
469 case DS_REG_TM0CNT_LO:
470 case DS_REG_TM1CNT_LO:
471 case DS_REG_TM2CNT_LO:
472 case DS_REG_TM3CNT_LO:
473 DSIOUpdateTimer(&ds->ds7, address);
474 break;
475 case DS_REG_KEYINPUT:
476 return DSIOReadKeyInput(ds);
477 case DS7_REG_EXTKEYIN:
478 return DSIOReadExKeyInput(ds);
479 case DS_REG_VCOUNT:
480 case DS_REG_DMA0CNT_HI:
481 case DS_REG_DMA1CNT_HI:
482 case DS_REG_DMA2CNT_HI:
483 case DS_REG_DMA3CNT_HI:
484 case DS_REG_DMA0FILL_LO:
485 case DS_REG_DMA0FILL_HI:
486 case DS_REG_DMA1FILL_LO:
487 case DS_REG_DMA1FILL_HI:
488 case DS_REG_DMA2FILL_LO:
489 case DS_REG_DMA2FILL_HI:
490 case DS_REG_DMA3FILL_LO:
491 case DS_REG_DMA3FILL_HI:
492 case DS_REG_TM0CNT_HI:
493 case DS_REG_TM1CNT_HI:
494 case DS_REG_TM2CNT_HI:
495 case DS_REG_TM3CNT_HI:
496 case DS7_REG_SPICNT:
497 case DS7_REG_SPIDATA:
498 case DS_REG_IPCSYNC:
499 case DS_REG_IPCFIFOCNT:
500 case DS_REG_ROMCNT_LO:
501 case DS_REG_ROMCNT_HI:
502 case DS_REG_IME:
503 case 0x20A:
504 case DS_REG_IE_LO:
505 case DS_REG_IE_HI:
506 case DS_REG_IF_LO:
507 case DS_REG_IF_HI:
508 case DS_REG_POSTFLG:
509 case DS7_REG_SOUND0CNT_LO:
510 case DS7_REG_SOUND1CNT_LO:
511 case DS7_REG_SOUND2CNT_LO:
512 case DS7_REG_SOUND3CNT_LO:
513 case DS7_REG_SOUND4CNT_LO:
514 case DS7_REG_SOUND5CNT_LO:
515 case DS7_REG_SOUND6CNT_LO:
516 case DS7_REG_SOUND7CNT_LO:
517 case DS7_REG_SOUND8CNT_LO:
518 case DS7_REG_SOUND9CNT_LO:
519 case DS7_REG_SOUNDACNT_LO:
520 case DS7_REG_SOUNDBCNT_LO:
521 case DS7_REG_SOUNDCCNT_LO:
522 case DS7_REG_SOUNDDCNT_LO:
523 case DS7_REG_SOUNDECNT_LO:
524 case DS7_REG_SOUNDFCNT_LO:
525 case DS7_REG_SOUND0CNT_HI:
526 case DS7_REG_SOUND1CNT_HI:
527 case DS7_REG_SOUND2CNT_HI:
528 case DS7_REG_SOUND3CNT_HI:
529 case DS7_REG_SOUND4CNT_HI:
530 case DS7_REG_SOUND5CNT_HI:
531 case DS7_REG_SOUND6CNT_HI:
532 case DS7_REG_SOUND7CNT_HI:
533 case DS7_REG_SOUND8CNT_HI:
534 case DS7_REG_SOUND9CNT_HI:
535 case DS7_REG_SOUNDACNT_HI:
536 case DS7_REG_SOUNDBCNT_HI:
537 case DS7_REG_SOUNDCCNT_HI:
538 case DS7_REG_SOUNDDCNT_HI:
539 case DS7_REG_SOUNDECNT_HI:
540 case DS7_REG_SOUNDFCNT_HI:
541 // Handled transparently by the registers
542 break;
543 case DS_REG_AUXSPICNT:
544 case DS_REG_AUXSPIDATA:
545 if (ds->ds7.memory.slot1Access) {
546 break;
547 } else {
548 mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
549 return 0;
550 }
551 default:
552 if (address >= DS7_IO_BASE_WIFI && address < DS7_IO_END_WIFI) {
553 return DSWifiReadIO(ds, address & 0x7FFF);
554 }
555 mLOG(DS_IO, STUB, "Stub DS7 I/O register read: %06X", address);
556 }
557 if (address < DS7_REG_MAX) {
558 return ds->memory.io7[address >> 1];
559 }
560
561 return 0;
562}
563
564uint32_t DS7IORead32(struct DS* ds, uint32_t address) {
565 switch (address) {
566 case DS_REG_IPCFIFORECV_LO:
567 return DSIPCReadFIFO(&ds->ds7);
568 case DS_REG_ROMDATA_0:
569 if (ds->ds7.memory.slot1Access) {
570 return DSSlot1Read(ds);
571 } else {
572 mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
573 return 0;
574 }
575 default:
576 return DS7IORead(ds, address & 0x00FFFFFC) | (DS7IORead(ds, (address & 0x00FFFFFC) | 2) << 16);
577 }
578}
579
580void DS9IOInit(struct DS* ds) {
581 memset(ds->memory.io9, 0, sizeof(ds->memory.io9));
582 ds->memory.io9[DS_REG_IPCFIFOCNT >> 1] = 0x0101;
583 ds->memory.io9[DS_REG_POSTFLG >> 1] = 0x0001;
584 ds->memory.io9[DS9_REG_GXSTAT_HI >> 1] = 0x0600;
585 DS9IOWrite(ds, DS9_REG_VRAMCNT_G, 0x0300);
586}
587
588void DS9IOWrite(struct DS* ds, uint32_t address, uint16_t value) {
589 if ((address <= DS9_REG_A_BLDY && address > DS_REG_VCOUNT) || address == DS9_REG_A_DISPCNT_LO || address == DS9_REG_A_DISPCNT_HI || address == DS9_REG_A_MASTER_BRIGHT) {
590 value = ds->video.renderer->writeVideoRegister(ds->video.renderer, address, value);
591 } else if ((address >= DS9_REG_B_DISPCNT_LO && address <= DS9_REG_B_BLDY) || address == DS9_REG_B_MASTER_BRIGHT) {
592 value = ds->video.renderer->writeVideoRegister(ds->video.renderer, address, value);
593 } else if ((address >= DS9_REG_RDLINES_COUNT && address <= DS9_REG_VECMTX_RESULT_12) || address == DS9_REG_DISP3DCNT) {
594 value = DSGXWriteRegister(&ds->gx, address, value);
595 } else {
596 uint16_t oldValue;
597 switch (address) {
598 // Other video
599 case DS9_REG_DISPCAPCNT_LO:
600 value &= 0x1F1F;
601 break;
602 case DS9_REG_DISPCAPCNT_HI:
603 value &= 0xEF3F;
604 break;
605
606 // VRAM control
607 case DS9_REG_VRAMCNT_A:
608 case DS9_REG_VRAMCNT_C:
609 case DS9_REG_VRAMCNT_E:
610 oldValue = ds->memory.io9[address >> 1];
611 value &= 0x9F9F;
612 DSVideoConfigureVRAM(ds, address - DS9_REG_VRAMCNT_A, value & 0xFF, oldValue & 0xFF);
613 DSVideoConfigureVRAM(ds, address - DS9_REG_VRAMCNT_A + 1, value >> 8, oldValue >> 8);
614 break;
615 case DS9_REG_VRAMCNT_G:
616 oldValue = ds->memory.io9[address >> 1];
617 value &= 0x039F;
618 DSVideoConfigureVRAM(ds, 6, value & 0xFF, oldValue & 0xFF);
619 DSConfigureWRAM(&ds->memory, value >> 8);
620 break;
621 case DS9_REG_VRAMCNT_H:
622 oldValue = ds->memory.io9[address >> 1];
623 value &= 0x9F9F;
624 DSVideoConfigureVRAM(ds, 7, value & 0xFF, oldValue & 0xFF);
625 DSVideoConfigureVRAM(ds, 8, value >> 8, oldValue >> 8);
626 break;
627
628 case DS9_REG_EXMEMCNT:
629 value &= 0xE8FF;
630 DSConfigureExternalMemory(ds, value);
631 break;
632
633 // Math
634 case DS9_REG_DIVCNT:
635 value = _scheduleDiv(ds, value);
636 break;
637 case DS9_REG_DIV_NUMER_0:
638 case DS9_REG_DIV_NUMER_1:
639 case DS9_REG_DIV_NUMER_2:
640 case DS9_REG_DIV_NUMER_3:
641 case DS9_REG_DIV_DENOM_0:
642 case DS9_REG_DIV_DENOM_1:
643 case DS9_REG_DIV_DENOM_2:
644 case DS9_REG_DIV_DENOM_3:
645 ds->memory.io9[DS9_REG_DIVCNT >> 1] = _scheduleDiv(ds, ds->memory.io9[DS9_REG_DIVCNT >> 1]);
646 break;
647 case DS9_REG_SQRTCNT:
648 value = _scheduleSqrt(ds, value);
649 break;
650 case DS9_REG_SQRT_PARAM_0:
651 case DS9_REG_SQRT_PARAM_1:
652 case DS9_REG_SQRT_PARAM_2:
653 case DS9_REG_SQRT_PARAM_3:
654 ds->memory.io9[DS9_REG_SQRTCNT >> 1] = _scheduleSqrt(ds, ds->memory.io9[DS9_REG_SQRTCNT >> 1]);
655 break;
656
657 // High Video
658 case DS9_REG_POWCNT1:
659 value = ds->video.renderer->writeVideoRegister(ds->video.renderer, address, value);
660 break;
661
662 default:
663 {
664 uint32_t v2 = DSIOWrite(&ds->ds9, address, value);
665 if (v2 & 0x10000) {
666 value = v2;
667 break;
668 } else if (v2 & 0x20000) {
669 return;
670 }
671 }
672 mLOG(DS_IO, STUB, "Stub DS9 I/O register write: %06X:%04X", address, value);
673 if (address >= DS7_REG_MAX) {
674 mLOG(DS_IO, GAME_ERROR, "Write to unused DS9 I/O register: %06X:%04X", address, value);
675 return;
676 }
677 break;
678 }
679 }
680 ds->memory.io9[address >> 1] = value;
681}
682
683void DS9IOWrite8(struct DS* ds, uint32_t address, uint8_t value) {
684 if (address < DS9_REG_MAX) {
685 uint16_t value16 = value << (8 * (address & 1));
686 value16 |= (ds->memory.io9[(address & 0x1FFF) >> 1]) & ~(0xFF << (8 * (address & 1)));
687 DS9IOWrite(ds, address & 0xFFFFFFFE, value16);
688 } else {
689 mLOG(DS, STUB, "Writing to unknown DS9 register: %08X:%02X", address, value);
690 }
691}
692
693void DS9IOWrite32(struct DS* ds, uint32_t address, uint32_t value) {
694 if ((address >= DS9_REG_RDLINES_COUNT && address <= DS9_REG_VECMTX_RESULT_12) || address == DS9_REG_DISP3DCNT) {
695 value = DSGXWriteRegister32(&ds->gx, address, value);
696 } else {
697 switch (address) {
698 case DS_REG_DMA0SAD_LO:
699 case DS_REG_DMA1SAD_LO:
700 case DS_REG_DMA2SAD_LO:
701 case DS_REG_DMA3SAD_LO:
702 case DS_REG_DMA0DAD_LO:
703 case DS_REG_DMA1DAD_LO:
704 case DS_REG_DMA2DAD_LO:
705 case DS_REG_DMA3DAD_LO:
706 case DS_REG_IPCFIFOSEND_LO:
707 case DS_REG_IE_LO:
708 value = DSIOWrite32(&ds->ds9, address, value);
709 break;
710
711 case DS_REG_DMA0CNT_LO:
712 DS9DMAWriteCNT(&ds->ds9, 0, value);
713 break;
714 case DS_REG_DMA1CNT_LO:
715 DS9DMAWriteCNT(&ds->ds9, 1, value);
716 break;
717 case DS_REG_DMA2CNT_LO:
718 DS9DMAWriteCNT(&ds->ds9, 2, value);
719 break;
720 case DS_REG_DMA3CNT_LO:
721 DS9DMAWriteCNT(&ds->ds9, 3, value);
722 break;
723
724 default:
725 DS9IOWrite(ds, address, value & 0xFFFF);
726 DS9IOWrite(ds, address | 2, value >> 16);
727 return;
728 }
729 }
730 ds->ds9.memory.io[address >> 1] = value;
731 ds->ds9.memory.io[(address >> 1) + 1] = value >> 16;
732}
733
734uint16_t DS9IORead(struct DS* ds, uint32_t address) {
735 switch (address) {
736 case DS_REG_TM0CNT_LO:
737 case DS_REG_TM1CNT_LO:
738 case DS_REG_TM2CNT_LO:
739 case DS_REG_TM3CNT_LO:
740 DSIOUpdateTimer(&ds->ds9, address);
741 break;
742 case DS_REG_KEYINPUT:
743 return DSIOReadKeyInput(ds);
744 case DS_REG_VCOUNT:
745 case DS_REG_DMA0CNT_HI:
746 case DS_REG_DMA1CNT_HI:
747 case DS_REG_DMA2CNT_HI:
748 case DS_REG_DMA3CNT_HI:
749 case DS_REG_DMA0FILL_LO:
750 case DS_REG_DMA0FILL_HI:
751 case DS_REG_DMA1FILL_LO:
752 case DS_REG_DMA1FILL_HI:
753 case DS_REG_DMA2FILL_LO:
754 case DS_REG_DMA2FILL_HI:
755 case DS_REG_DMA3FILL_LO:
756 case DS_REG_DMA3FILL_HI:
757 case DS_REG_TM0CNT_HI:
758 case DS_REG_TM1CNT_HI:
759 case DS_REG_TM2CNT_HI:
760 case DS_REG_TM3CNT_HI:
761 case DS_REG_IPCSYNC:
762 case DS_REG_IPCFIFOCNT:
763 case DS_REG_ROMCNT_LO:
764 case DS_REG_ROMCNT_HI:
765 case DS_REG_IME:
766 case 0x20A:
767 case DS_REG_IE_LO:
768 case DS_REG_IE_HI:
769 case DS_REG_IF_LO:
770 case DS_REG_IF_HI:
771 case DS9_REG_DIVCNT:
772 case DS9_REG_DIV_NUMER_0:
773 case DS9_REG_DIV_NUMER_1:
774 case DS9_REG_DIV_NUMER_2:
775 case DS9_REG_DIV_NUMER_3:
776 case DS9_REG_DIV_DENOM_0:
777 case DS9_REG_DIV_DENOM_1:
778 case DS9_REG_DIV_DENOM_2:
779 case DS9_REG_DIV_DENOM_3:
780 case DS9_REG_DIV_RESULT_0:
781 case DS9_REG_DIV_RESULT_1:
782 case DS9_REG_DIV_RESULT_2:
783 case DS9_REG_DIV_RESULT_3:
784 case DS9_REG_DIVREM_RESULT_0:
785 case DS9_REG_DIVREM_RESULT_1:
786 case DS9_REG_DIVREM_RESULT_2:
787 case DS9_REG_DIVREM_RESULT_3:
788 case DS9_REG_SQRTCNT:
789 case DS9_REG_SQRT_PARAM_0:
790 case DS9_REG_SQRT_PARAM_1:
791 case DS9_REG_SQRT_PARAM_2:
792 case DS9_REG_SQRT_PARAM_3:
793 case DS9_REG_SQRT_RESULT_LO:
794 case DS9_REG_SQRT_RESULT_HI:
795 case DS_REG_POSTFLG:
796 case DS9_REG_GXSTAT_LO:
797 case DS9_REG_GXSTAT_HI:
798 case DS9_REG_CLIPMTX_RESULT_00:
799 case DS9_REG_CLIPMTX_RESULT_01:
800 case DS9_REG_CLIPMTX_RESULT_02:
801 case DS9_REG_CLIPMTX_RESULT_03:
802 case DS9_REG_CLIPMTX_RESULT_04:
803 case DS9_REG_CLIPMTX_RESULT_05:
804 case DS9_REG_CLIPMTX_RESULT_06:
805 case DS9_REG_CLIPMTX_RESULT_07:
806 case DS9_REG_CLIPMTX_RESULT_08:
807 case DS9_REG_CLIPMTX_RESULT_09:
808 case DS9_REG_CLIPMTX_RESULT_0A:
809 case DS9_REG_CLIPMTX_RESULT_0B:
810 case DS9_REG_CLIPMTX_RESULT_0C:
811 case DS9_REG_CLIPMTX_RESULT_0D:
812 case DS9_REG_CLIPMTX_RESULT_0E:
813 case DS9_REG_CLIPMTX_RESULT_0F:
814 case DS9_REG_CLIPMTX_RESULT_10:
815 case DS9_REG_CLIPMTX_RESULT_11:
816 case DS9_REG_CLIPMTX_RESULT_12:
817 case DS9_REG_CLIPMTX_RESULT_13:
818 case DS9_REG_CLIPMTX_RESULT_14:
819 case DS9_REG_CLIPMTX_RESULT_15:
820 case DS9_REG_CLIPMTX_RESULT_16:
821 case DS9_REG_CLIPMTX_RESULT_17:
822 case DS9_REG_CLIPMTX_RESULT_18:
823 case DS9_REG_CLIPMTX_RESULT_19:
824 case DS9_REG_CLIPMTX_RESULT_1A:
825 case DS9_REG_CLIPMTX_RESULT_1B:
826 case DS9_REG_CLIPMTX_RESULT_1C:
827 case DS9_REG_CLIPMTX_RESULT_1D:
828 case DS9_REG_CLIPMTX_RESULT_1E:
829 case DS9_REG_CLIPMTX_RESULT_1F:
830 // Handled transparently by the registers
831 break;
832 case DS_REG_AUXSPICNT:
833 case DS_REG_AUXSPIDATA:
834 if (ds->ds9.memory.slot1Access) {
835 break;
836 } else {
837 mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
838 return 0;
839 }
840 default:
841 mLOG(DS_IO, STUB, "Stub DS9 I/O register read: %06X", address);
842 }
843 if (address < DS9_REG_MAX) {
844 return ds->ds9.memory.io[address >> 1];
845 }
846 return 0;
847}
848
849uint32_t DS9IORead32(struct DS* ds, uint32_t address) {
850 switch (address) {
851 case DS_REG_IPCFIFORECV_LO:
852 return DSIPCReadFIFO(&ds->ds9);
853 case DS_REG_ROMDATA_0:
854 if (ds->ds9.memory.slot1Access) {
855 return DSSlot1Read(ds);
856 } else {
857 mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
858 return 0;
859 }
860 default:
861 return DS9IORead(ds, address & 0x00FFFFFC) | (DS9IORead(ds, (address & 0x00FFFFFC) | 2) << 16);
862 }
863}