all repos — mgba @ 2222010107fa367af94450dffd71d350d1fde96f

mGBA Game Boy Advance Emulator

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_DMA0FILL_LO:
481	case DS_REG_DMA0FILL_HI:
482	case DS_REG_DMA1FILL_LO:
483	case DS_REG_DMA1FILL_HI:
484	case DS_REG_DMA2FILL_LO:
485	case DS_REG_DMA2FILL_HI:
486	case DS_REG_DMA3FILL_LO:
487	case DS_REG_DMA3FILL_HI:
488	case DS_REG_TM0CNT_HI:
489	case DS_REG_TM1CNT_HI:
490	case DS_REG_TM2CNT_HI:
491	case DS_REG_TM3CNT_HI:
492	case DS7_REG_SPICNT:
493	case DS7_REG_SPIDATA:
494	case DS_REG_IPCSYNC:
495	case DS_REG_IPCFIFOCNT:
496	case DS_REG_ROMCNT_LO:
497	case DS_REG_ROMCNT_HI:
498	case DS_REG_IME:
499	case 0x20A:
500	case DS_REG_IE_LO:
501	case DS_REG_IE_HI:
502	case DS_REG_IF_LO:
503	case DS_REG_IF_HI:
504	case DS_REG_POSTFLG:
505		// Handled transparently by the registers
506		break;
507	case DS_REG_AUXSPICNT:
508	case DS_REG_AUXSPIDATA:
509		if (ds->ds7.memory.slot1Access) {
510			break;
511		} else {
512			mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
513			return 0;
514		}
515	default:
516		if (address >= DS7_IO_BASE_WIFI && address < DS7_IO_END_WIFI) {
517			return DSWifiReadIO(ds, address & 0x7FFF);
518		}
519		mLOG(DS_IO, STUB, "Stub DS7 I/O register read: %06X", address);
520	}
521	if (address < DS7_REG_MAX) {
522		return ds->memory.io7[address >> 1];
523	}
524
525	return 0;
526}
527
528uint32_t DS7IORead32(struct DS* ds, uint32_t address) {
529	switch (address) {
530	case DS_REG_IPCFIFORECV_LO:
531		return DSIPCReadFIFO(&ds->ds7);
532	case DS_REG_ROMDATA_0:
533		if (ds->ds7.memory.slot1Access) {
534			return DSSlot1Read(ds);
535		} else {
536			mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
537			return 0;
538		}
539	default:
540		return DS7IORead(ds, address & 0x00FFFFFC) | (DS7IORead(ds, (address & 0x00FFFFFC) | 2) << 16);
541	}
542}
543
544void DS9IOInit(struct DS* ds) {
545	memset(ds->memory.io9, 0, sizeof(ds->memory.io9));
546	ds->memory.io9[DS_REG_IPCFIFOCNT >> 1] = 0x0101;
547	ds->memory.io9[DS_REG_POSTFLG >> 1] = 0x0001;
548	ds->memory.io9[DS9_REG_GXSTAT_HI >> 1] = 0x0600;
549	DS9IOWrite(ds, DS9_REG_VRAMCNT_G, 0x0300);
550}
551
552void DS9IOWrite(struct DS* ds, uint32_t address, uint16_t value) {
553	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) {
554		value = ds->video.renderer->writeVideoRegister(ds->video.renderer, address, value);
555	} else if ((address >= DS9_REG_B_DISPCNT_LO && address <= DS9_REG_B_BLDY) || address == DS9_REG_B_MASTER_BRIGHT) {
556		value = ds->video.renderer->writeVideoRegister(ds->video.renderer, address, value);
557	} else if ((address >= DS9_REG_RDLINES_COUNT && address <= DS9_REG_VECMTX_RESULT_12) || address == DS9_REG_DISP3DCNT) {
558		value = DSGXWriteRegister(&ds->gx, address, value);
559	} else {
560		uint16_t oldValue;
561		switch (address) {
562		// Other video
563		case DS9_REG_DISPCAPCNT_LO:
564			value &= 0x1F1F;
565			break;
566		case DS9_REG_DISPCAPCNT_HI:
567			value &= 0xEF3F;
568			break;
569
570		// VRAM control
571		case DS9_REG_VRAMCNT_A:
572		case DS9_REG_VRAMCNT_C:
573		case DS9_REG_VRAMCNT_E:
574			oldValue = ds->memory.io9[address >> 1];
575			value &= 0x9F9F;
576			DSVideoConfigureVRAM(ds, address - DS9_REG_VRAMCNT_A, value & 0xFF, oldValue & 0xFF);
577			DSVideoConfigureVRAM(ds, address - DS9_REG_VRAMCNT_A + 1, value >> 8, oldValue >> 8);
578			break;
579		case DS9_REG_VRAMCNT_G:
580			oldValue = ds->memory.io9[address >> 1];
581			value &= 0x039F;
582			DSVideoConfigureVRAM(ds, 6, value & 0xFF, oldValue & 0xFF);
583			DSConfigureWRAM(&ds->memory, value >> 8);
584			break;
585		case DS9_REG_VRAMCNT_H:
586			oldValue = ds->memory.io9[address >> 1];
587			value &= 0x9F9F;
588			DSVideoConfigureVRAM(ds, 7, value & 0xFF, oldValue & 0xFF);
589			DSVideoConfigureVRAM(ds, 8, value >> 8, oldValue >> 8);
590			break;
591
592		case DS9_REG_EXMEMCNT:
593			value &= 0xE8FF;
594			DSConfigureExternalMemory(ds, value);
595			break;
596
597		// Math
598		case DS9_REG_DIVCNT:
599			value = _scheduleDiv(ds, value);
600			break;
601		case DS9_REG_DIV_NUMER_0:
602		case DS9_REG_DIV_NUMER_1:
603		case DS9_REG_DIV_NUMER_2:
604		case DS9_REG_DIV_NUMER_3:
605		case DS9_REG_DIV_DENOM_0:
606		case DS9_REG_DIV_DENOM_1:
607		case DS9_REG_DIV_DENOM_2:
608		case DS9_REG_DIV_DENOM_3:
609			ds->memory.io9[DS9_REG_DIVCNT >> 1] = _scheduleDiv(ds, ds->memory.io9[DS9_REG_DIVCNT >> 1]);
610			break;
611		case DS9_REG_SQRTCNT:
612			value = _scheduleSqrt(ds, value);
613			break;
614		case DS9_REG_SQRT_PARAM_0:
615		case DS9_REG_SQRT_PARAM_1:
616		case DS9_REG_SQRT_PARAM_2:
617		case DS9_REG_SQRT_PARAM_3:
618			ds->memory.io9[DS9_REG_SQRTCNT >> 1] = _scheduleSqrt(ds, ds->memory.io9[DS9_REG_SQRTCNT >> 1]);
619			break;
620
621		// High Video
622		case DS9_REG_POWCNT1:
623			value = ds->video.renderer->writeVideoRegister(ds->video.renderer, address, value);
624			break;
625
626		default:
627			{
628				uint32_t v2 = DSIOWrite(&ds->ds9, address, value);
629				if (v2 & 0x10000) {
630					value = v2;
631					break;
632				} else if (v2 & 0x20000) {
633					return;
634				}
635			}
636			mLOG(DS_IO, STUB, "Stub DS9 I/O register write: %06X:%04X", address, value);
637			if (address >= DS7_REG_MAX) {
638				mLOG(DS_IO, GAME_ERROR, "Write to unused DS9 I/O register: %06X:%04X", address, value);
639				return;
640			}
641			break;
642		}
643	}
644	ds->memory.io9[address >> 1] = value;
645}
646
647void DS9IOWrite8(struct DS* ds, uint32_t address, uint8_t value) {
648	if (address < DS9_REG_MAX) {
649		uint16_t value16 = value << (8 * (address & 1));
650		value16 |= (ds->memory.io9[(address & 0x1FFF) >> 1]) & ~(0xFF << (8 * (address & 1)));
651		DS9IOWrite(ds, address & 0xFFFFFFFE, value16);
652	} else {
653		mLOG(DS, STUB, "Writing to unknown DS9 register: %08X:%02X", address, value);
654	}
655}
656
657void DS9IOWrite32(struct DS* ds, uint32_t address, uint32_t value) {
658	if ((address >= DS9_REG_RDLINES_COUNT && address <= DS9_REG_VECMTX_RESULT_12) || address == DS9_REG_DISP3DCNT) {
659		value = DSGXWriteRegister32(&ds->gx, address, value);
660	} else {
661		switch (address) {
662		case DS_REG_DMA0SAD_LO:
663		case DS_REG_DMA1SAD_LO:
664		case DS_REG_DMA2SAD_LO:
665		case DS_REG_DMA3SAD_LO:
666		case DS_REG_DMA0DAD_LO:
667		case DS_REG_DMA1DAD_LO:
668		case DS_REG_DMA2DAD_LO:
669		case DS_REG_DMA3DAD_LO:
670		case DS_REG_IPCFIFOSEND_LO:
671		case DS_REG_IE_LO:
672			value = DSIOWrite32(&ds->ds9, address, value);
673			break;
674
675		case DS_REG_DMA0CNT_LO:
676			DS9DMAWriteCNT(&ds->ds9, 0, value);
677			break;
678		case DS_REG_DMA1CNT_LO:
679			DS9DMAWriteCNT(&ds->ds9, 1, value);
680			break;
681		case DS_REG_DMA2CNT_LO:
682			DS9DMAWriteCNT(&ds->ds9, 2, value);
683			break;
684		case DS_REG_DMA3CNT_LO:
685			DS9DMAWriteCNT(&ds->ds9, 3, value);
686			break;
687
688		default:
689			DS9IOWrite(ds, address, value & 0xFFFF);
690			DS9IOWrite(ds, address | 2, value >> 16);
691			return;
692		}
693	}
694	ds->ds9.memory.io[address >> 1] = value;
695	ds->ds9.memory.io[(address >> 1) + 1] = value >> 16;
696}
697
698uint16_t DS9IORead(struct DS* ds, uint32_t address) {
699	switch (address) {
700	case DS_REG_TM0CNT_LO:
701	case DS_REG_TM1CNT_LO:
702	case DS_REG_TM2CNT_LO:
703	case DS_REG_TM3CNT_LO:
704		DSIOUpdateTimer(&ds->ds9, address);
705		break;
706	case DS_REG_KEYINPUT:
707		return DSIOReadKeyInput(ds);
708	case DS_REG_VCOUNT:
709	case DS_REG_DMA0FILL_LO:
710	case DS_REG_DMA0FILL_HI:
711	case DS_REG_DMA1FILL_LO:
712	case DS_REG_DMA1FILL_HI:
713	case DS_REG_DMA2FILL_LO:
714	case DS_REG_DMA2FILL_HI:
715	case DS_REG_DMA3FILL_LO:
716	case DS_REG_DMA3FILL_HI:
717	case DS_REG_TM0CNT_HI:
718	case DS_REG_TM1CNT_HI:
719	case DS_REG_TM2CNT_HI:
720	case DS_REG_TM3CNT_HI:
721	case DS_REG_IPCSYNC:
722	case DS_REG_IPCFIFOCNT:
723	case DS_REG_ROMCNT_LO:
724	case DS_REG_ROMCNT_HI:
725	case DS_REG_IME:
726	case 0x20A:
727	case DS_REG_IE_LO:
728	case DS_REG_IE_HI:
729	case DS_REG_IF_LO:
730	case DS_REG_IF_HI:
731	case DS9_REG_DIVCNT:
732	case DS9_REG_DIV_NUMER_0:
733	case DS9_REG_DIV_NUMER_1:
734	case DS9_REG_DIV_NUMER_2:
735	case DS9_REG_DIV_NUMER_3:
736	case DS9_REG_DIV_DENOM_0:
737	case DS9_REG_DIV_DENOM_1:
738	case DS9_REG_DIV_DENOM_2:
739	case DS9_REG_DIV_DENOM_3:
740	case DS9_REG_DIV_RESULT_0:
741	case DS9_REG_DIV_RESULT_1:
742	case DS9_REG_DIV_RESULT_2:
743	case DS9_REG_DIV_RESULT_3:
744	case DS9_REG_DIVREM_RESULT_0:
745	case DS9_REG_DIVREM_RESULT_1:
746	case DS9_REG_DIVREM_RESULT_2:
747	case DS9_REG_DIVREM_RESULT_3:
748	case DS9_REG_SQRTCNT:
749	case DS9_REG_SQRT_PARAM_0:
750	case DS9_REG_SQRT_PARAM_1:
751	case DS9_REG_SQRT_PARAM_2:
752	case DS9_REG_SQRT_PARAM_3:
753	case DS9_REG_SQRT_RESULT_LO:
754	case DS9_REG_SQRT_RESULT_HI:
755	case DS_REG_POSTFLG:
756	case DS9_REG_GXSTAT_LO:
757	case DS9_REG_GXSTAT_HI:
758	case DS9_REG_CLIPMTX_RESULT_00:
759	case DS9_REG_CLIPMTX_RESULT_01:
760	case DS9_REG_CLIPMTX_RESULT_02:
761	case DS9_REG_CLIPMTX_RESULT_03:
762	case DS9_REG_CLIPMTX_RESULT_04:
763	case DS9_REG_CLIPMTX_RESULT_05:
764	case DS9_REG_CLIPMTX_RESULT_06:
765	case DS9_REG_CLIPMTX_RESULT_07:
766	case DS9_REG_CLIPMTX_RESULT_08:
767	case DS9_REG_CLIPMTX_RESULT_09:
768	case DS9_REG_CLIPMTX_RESULT_0A:
769	case DS9_REG_CLIPMTX_RESULT_0B:
770	case DS9_REG_CLIPMTX_RESULT_0C:
771	case DS9_REG_CLIPMTX_RESULT_0D:
772	case DS9_REG_CLIPMTX_RESULT_0E:
773	case DS9_REG_CLIPMTX_RESULT_0F:
774	case DS9_REG_CLIPMTX_RESULT_10:
775	case DS9_REG_CLIPMTX_RESULT_11:
776	case DS9_REG_CLIPMTX_RESULT_12:
777	case DS9_REG_CLIPMTX_RESULT_13:
778	case DS9_REG_CLIPMTX_RESULT_14:
779	case DS9_REG_CLIPMTX_RESULT_15:
780	case DS9_REG_CLIPMTX_RESULT_16:
781	case DS9_REG_CLIPMTX_RESULT_17:
782	case DS9_REG_CLIPMTX_RESULT_18:
783	case DS9_REG_CLIPMTX_RESULT_19:
784	case DS9_REG_CLIPMTX_RESULT_1A:
785	case DS9_REG_CLIPMTX_RESULT_1B:
786	case DS9_REG_CLIPMTX_RESULT_1C:
787	case DS9_REG_CLIPMTX_RESULT_1D:
788	case DS9_REG_CLIPMTX_RESULT_1E:
789	case DS9_REG_CLIPMTX_RESULT_1F:
790		// Handled transparently by the registers
791		break;
792	case DS_REG_AUXSPICNT:
793	case DS_REG_AUXSPIDATA:
794		if (ds->ds9.memory.slot1Access) {
795			break;
796		} else {
797			mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
798			return 0;
799		}
800	default:
801		mLOG(DS_IO, STUB, "Stub DS9 I/O register read: %06X", address);
802	}
803	if (address < DS9_REG_MAX) {
804		return ds->ds9.memory.io[address >> 1];
805	}
806	return 0;
807}
808
809uint32_t DS9IORead32(struct DS* ds, uint32_t address) {
810	switch (address) {
811	case DS_REG_IPCFIFORECV_LO:
812		return DSIPCReadFIFO(&ds->ds9);
813	case DS_REG_ROMDATA_0:
814		if (ds->ds9.memory.slot1Access) {
815			return DSSlot1Read(ds);
816		} else {
817			mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
818			return 0;
819		}
820	default:
821		return DS9IORead(ds, address & 0x00FFFFFC) | (DS9IORead(ds, (address & 0x00FFFFFC) | 2) << 16);
822	}
823}