all repos — mgba @ 662a6cdd588172118b64ed11c059e9bfce2b270b

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_SOUND0CNT_LO:
277	case DS7_REG_SOUND1CNT_LO:
278	case DS7_REG_SOUND2CNT_LO:
279	case DS7_REG_SOUND3CNT_LO:
280	case DS7_REG_SOUND4CNT_LO:
281	case DS7_REG_SOUND5CNT_LO:
282	case DS7_REG_SOUND6CNT_LO:
283	case DS7_REG_SOUND7CNT_LO:
284	case DS7_REG_SOUND8CNT_LO:
285	case DS7_REG_SOUND9CNT_LO:
286	case DS7_REG_SOUNDACNT_LO:
287	case DS7_REG_SOUNDBCNT_LO:
288	case DS7_REG_SOUNDCCNT_LO:
289	case DS7_REG_SOUNDDCNT_LO:
290	case DS7_REG_SOUNDECNT_LO:
291	case DS7_REG_SOUNDFCNT_LO:
292		value &= 0x837F;
293		DSAudioWriteSOUNDCNT_LO(&ds->audio, (address - DS7_REG_SOUND0CNT_LO) >> 4, value);
294		break;
295	case DS7_REG_SOUND0CNT_HI:
296	case DS7_REG_SOUND1CNT_HI:
297	case DS7_REG_SOUND2CNT_HI:
298	case DS7_REG_SOUND3CNT_HI:
299	case DS7_REG_SOUND4CNT_HI:
300	case DS7_REG_SOUND5CNT_HI:
301	case DS7_REG_SOUND6CNT_HI:
302	case DS7_REG_SOUND7CNT_HI:
303	case DS7_REG_SOUND8CNT_HI:
304	case DS7_REG_SOUND9CNT_HI:
305	case DS7_REG_SOUNDACNT_HI:
306	case DS7_REG_SOUNDBCNT_HI:
307	case DS7_REG_SOUNDCCNT_HI:
308	case DS7_REG_SOUNDDCNT_HI:
309	case DS7_REG_SOUNDECNT_HI:
310	case DS7_REG_SOUNDFCNT_HI:
311		value &= 0xFF7F;
312		DSAudioWriteSOUNDCNT_HI(&ds->audio, (address - DS7_REG_SOUND0CNT_HI) >> 4, value);
313		break;
314	case DS7_REG_SOUND0TMR:
315	case DS7_REG_SOUND1TMR:
316	case DS7_REG_SOUND2TMR:
317	case DS7_REG_SOUND3TMR:
318	case DS7_REG_SOUND4TMR:
319	case DS7_REG_SOUND5TMR:
320	case DS7_REG_SOUND6TMR:
321	case DS7_REG_SOUND7TMR:
322	case DS7_REG_SOUND8TMR:
323	case DS7_REG_SOUND9TMR:
324	case DS7_REG_SOUNDATMR:
325	case DS7_REG_SOUNDBTMR:
326	case DS7_REG_SOUNDCTMR:
327	case DS7_REG_SOUNDDTMR:
328	case DS7_REG_SOUNDETMR:
329	case DS7_REG_SOUNDFTMR:
330		DSAudioWriteSOUNDTMR(&ds->audio, (address - DS7_REG_SOUND0TMR) >> 4, value);
331		break;
332	case DS7_REG_SOUND0PNT:
333	case DS7_REG_SOUND1PNT:
334	case DS7_REG_SOUND2PNT:
335	case DS7_REG_SOUND3PNT:
336	case DS7_REG_SOUND4PNT:
337	case DS7_REG_SOUND5PNT:
338	case DS7_REG_SOUND6PNT:
339	case DS7_REG_SOUND7PNT:
340	case DS7_REG_SOUND8PNT:
341	case DS7_REG_SOUND9PNT:
342	case DS7_REG_SOUNDAPNT:
343	case DS7_REG_SOUNDBPNT:
344	case DS7_REG_SOUNDCPNT:
345	case DS7_REG_SOUNDDPNT:
346	case DS7_REG_SOUNDEPNT:
347	case DS7_REG_SOUNDFPNT:
348		DSAudioWriteSOUNDPNT(&ds->audio, (address - DS7_REG_SOUND0PNT) >> 4, value);
349		break;
350	default:
351		{
352			uint32_t v2 = DSIOWrite(&ds->ds7, address, value);
353			if (v2 & 0x10000) {
354				value = v2;
355				break;
356			} else if (v2 & 0x20000) {
357				return;
358			}
359		}
360		if (address >= DS7_IO_BASE_WIFI && address < DS7_IO_END_WIFI) {
361			DSWifiWriteIO(ds, address & 0x7FFF, value);
362			return;
363		}
364		mLOG(DS_IO, STUB, "Stub DS7 I/O register write: %06X:%04X", address, value);
365		if (address >= DS7_REG_MAX) {
366			mLOG(DS_IO, GAME_ERROR, "Write to unused DS7 I/O register: %06X:%04X", address, value);
367			return;
368		}
369		break;
370	}
371	ds->memory.io7[address >> 1] = value;
372}
373
374void DS7IOWrite8(struct DS* ds, uint32_t address, uint8_t value) {
375	if (address == DS7_REG_HALTCNT) {
376		_DSHaltCNT(&ds->ds7, value);
377		return;
378	}
379	if (address < DS7_REG_MAX) {
380		uint16_t value16 = value << (8 * (address & 1));
381		value16 |= (ds->ds7.memory.io[(address & 0xFFF) >> 1]) & ~(0xFF << (8 * (address & 1)));
382		DS7IOWrite(ds, address & 0xFFFFFFFE, value16);
383	} else {
384		mLOG(DS, STUB, "Writing to unknown DS7 register: %08X:%02X", address, value);
385	}
386}
387
388void DS7IOWrite32(struct DS* ds, uint32_t address, uint32_t value) {
389	switch (address) {
390	case DS_REG_DMA0SAD_LO:
391	case DS_REG_DMA1SAD_LO:
392	case DS_REG_DMA2SAD_LO:
393	case DS_REG_DMA3SAD_LO:
394	case DS_REG_DMA0DAD_LO:
395	case DS_REG_DMA1DAD_LO:
396	case DS_REG_DMA2DAD_LO:
397	case DS_REG_DMA3DAD_LO:
398	case DS_REG_IPCFIFOSEND_LO:
399	case DS_REG_IE_LO:
400		value = DSIOWrite32(&ds->ds7, address, value);
401		break;
402
403	case DS_REG_DMA0CNT_LO:
404		DS7DMAWriteCNT(&ds->ds7, 0, value);
405		break;
406	case DS_REG_DMA1CNT_LO:
407		DS7DMAWriteCNT(&ds->ds7, 1, value);
408		break;
409	case DS_REG_DMA2CNT_LO:
410		DS7DMAWriteCNT(&ds->ds7, 2, value);
411		break;
412	case DS_REG_DMA3CNT_LO:
413		DS7DMAWriteCNT(&ds->ds7, 3, value);
414		break;
415
416	case DS7_REG_SOUND0SAD_LO:
417	case DS7_REG_SOUND1SAD_LO:
418	case DS7_REG_SOUND2SAD_LO:
419	case DS7_REG_SOUND3SAD_LO:
420	case DS7_REG_SOUND4SAD_LO:
421	case DS7_REG_SOUND5SAD_LO:
422	case DS7_REG_SOUND6SAD_LO:
423	case DS7_REG_SOUND7SAD_LO:
424	case DS7_REG_SOUND8SAD_LO:
425	case DS7_REG_SOUND9SAD_LO:
426	case DS7_REG_SOUNDASAD_LO:
427	case DS7_REG_SOUNDBSAD_LO:
428	case DS7_REG_SOUNDCSAD_LO:
429	case DS7_REG_SOUNDDSAD_LO:
430	case DS7_REG_SOUNDESAD_LO:
431	case DS7_REG_SOUNDFSAD_LO:
432		DSAudioWriteSOUNDSAD(&ds->audio, (address - DS7_REG_SOUND0SAD_LO) >> 4, value);
433		break;
434
435	case DS7_REG_SOUND0LEN_LO:
436	case DS7_REG_SOUND1LEN_LO:
437	case DS7_REG_SOUND2LEN_LO:
438	case DS7_REG_SOUND3LEN_LO:
439	case DS7_REG_SOUND4LEN_LO:
440	case DS7_REG_SOUND5LEN_LO:
441	case DS7_REG_SOUND6LEN_LO:
442	case DS7_REG_SOUND7LEN_LO:
443	case DS7_REG_SOUND8LEN_LO:
444	case DS7_REG_SOUND9LEN_LO:
445	case DS7_REG_SOUNDALEN_LO:
446	case DS7_REG_SOUNDBLEN_LO:
447	case DS7_REG_SOUNDCLEN_LO:
448	case DS7_REG_SOUNDDLEN_LO:
449	case DS7_REG_SOUNDELEN_LO:
450	case DS7_REG_SOUNDFLEN_LO:
451		value &= 0x3FFFFF;
452		DSAudioWriteSOUNDLEN(&ds->audio, (address - DS7_REG_SOUND0LEN_LO) >> 4, value);
453		break;
454
455	default:
456		DS7IOWrite(ds, address, value & 0xFFFF);
457		DS7IOWrite(ds, address | 2, value >> 16);
458		return;
459	}
460	ds->ds7.memory.io[address >> 1] = value;
461	ds->ds7.memory.io[(address >> 1) + 1] = value >> 16;
462}
463
464uint16_t DS7IORead(struct DS* ds, uint32_t address) {
465	switch (address) {
466	case DS_REG_TM0CNT_LO:
467	case DS_REG_TM1CNT_LO:
468	case DS_REG_TM2CNT_LO:
469	case DS_REG_TM3CNT_LO:
470		DSIOUpdateTimer(&ds->ds7, address);
471		break;
472	case DS_REG_KEYINPUT:
473		return DSIOReadKeyInput(ds);
474	case DS7_REG_EXTKEYIN:
475		return DSIOReadExKeyInput(ds);
476	case DS_REG_VCOUNT:
477	case DS_REG_DMA0FILL_LO:
478	case DS_REG_DMA0FILL_HI:
479	case DS_REG_DMA1FILL_LO:
480	case DS_REG_DMA1FILL_HI:
481	case DS_REG_DMA2FILL_LO:
482	case DS_REG_DMA2FILL_HI:
483	case DS_REG_DMA3FILL_LO:
484	case DS_REG_DMA3FILL_HI:
485	case DS_REG_TM0CNT_HI:
486	case DS_REG_TM1CNT_HI:
487	case DS_REG_TM2CNT_HI:
488	case DS_REG_TM3CNT_HI:
489	case DS7_REG_SPICNT:
490	case DS7_REG_SPIDATA:
491	case DS_REG_IPCSYNC:
492	case DS_REG_IPCFIFOCNT:
493	case DS_REG_ROMCNT_LO:
494	case DS_REG_ROMCNT_HI:
495	case DS_REG_IME:
496	case 0x20A:
497	case DS_REG_IE_LO:
498	case DS_REG_IE_HI:
499	case DS_REG_IF_LO:
500	case DS_REG_IF_HI:
501	case DS_REG_POSTFLG:
502		// Handled transparently by the registers
503		break;
504	case DS_REG_AUXSPICNT:
505	case DS_REG_AUXSPIDATA:
506		if (ds->ds7.memory.slot1Access) {
507			break;
508		} else {
509			mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
510			return 0;
511		}
512	default:
513		if (address >= DS7_IO_BASE_WIFI && address < DS7_IO_END_WIFI) {
514			return DSWifiReadIO(ds, address & 0x7FFF);
515		}
516		mLOG(DS_IO, STUB, "Stub DS7 I/O register read: %06X", address);
517	}
518	if (address < DS7_REG_MAX) {
519		return ds->memory.io7[address >> 1];
520	}
521
522	return 0;
523}
524
525uint32_t DS7IORead32(struct DS* ds, uint32_t address) {
526	switch (address) {
527	case DS_REG_IPCFIFORECV_LO:
528		return DSIPCReadFIFO(&ds->ds7);
529	case DS_REG_ROMDATA_0:
530		if (ds->ds7.memory.slot1Access) {
531			return DSSlot1Read(ds);
532		} else {
533			mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
534			return 0;
535		}
536	default:
537		return DS7IORead(ds, address & 0x00FFFFFC) | (DS7IORead(ds, (address & 0x00FFFFFC) | 2) << 16);
538	}
539}
540
541void DS9IOInit(struct DS* ds) {
542	memset(ds->memory.io9, 0, sizeof(ds->memory.io9));
543	ds->memory.io9[DS_REG_IPCFIFOCNT >> 1] = 0x0101;
544	ds->memory.io9[DS_REG_POSTFLG >> 1] = 0x0001;
545	ds->memory.io9[DS9_REG_GXSTAT_HI >> 1] = 0x0600;
546	DS9IOWrite(ds, DS9_REG_VRAMCNT_G, 0x0300);
547}
548
549void DS9IOWrite(struct DS* ds, uint32_t address, uint16_t value) {
550	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) {
551		value = ds->video.renderer->writeVideoRegister(ds->video.renderer, address, value);
552	} else if ((address >= DS9_REG_B_DISPCNT_LO && address <= DS9_REG_B_BLDY) || address == DS9_REG_B_MASTER_BRIGHT) {
553		value = ds->video.renderer->writeVideoRegister(ds->video.renderer, address, value);
554	} else if ((address >= DS9_REG_RDLINES_COUNT && address <= DS9_REG_VECMTX_RESULT_12) || address == DS9_REG_DISP3DCNT) {
555		value = DSGXWriteRegister(&ds->gx, address, value);
556	} else {
557		uint16_t oldValue;
558		switch (address) {
559		// Other video
560		case DS9_REG_DISPCAPCNT_LO:
561			value &= 0x1F1F;
562			break;
563		case DS9_REG_DISPCAPCNT_HI:
564			value &= 0xEF3F;
565			break;
566
567		// VRAM control
568		case DS9_REG_VRAMCNT_A:
569		case DS9_REG_VRAMCNT_C:
570		case DS9_REG_VRAMCNT_E:
571			oldValue = ds->memory.io9[address >> 1];
572			value &= 0x9F9F;
573			DSVideoConfigureVRAM(ds, address - DS9_REG_VRAMCNT_A, value & 0xFF, oldValue & 0xFF);
574			DSVideoConfigureVRAM(ds, address - DS9_REG_VRAMCNT_A + 1, value >> 8, oldValue >> 8);
575			break;
576		case DS9_REG_VRAMCNT_G:
577			oldValue = ds->memory.io9[address >> 1];
578			value &= 0x039F;
579			DSVideoConfigureVRAM(ds, 6, value & 0xFF, oldValue & 0xFF);
580			DSConfigureWRAM(&ds->memory, value >> 8);
581			break;
582		case DS9_REG_VRAMCNT_H:
583			oldValue = ds->memory.io9[address >> 1];
584			value &= 0x9F9F;
585			DSVideoConfigureVRAM(ds, 7, value & 0xFF, oldValue & 0xFF);
586			DSVideoConfigureVRAM(ds, 8, value >> 8, oldValue >> 8);
587			break;
588
589		case DS9_REG_EXMEMCNT:
590			value &= 0xE8FF;
591			DSConfigureExternalMemory(ds, value);
592			break;
593
594		// Math
595		case DS9_REG_DIVCNT:
596			value = _scheduleDiv(ds, value);
597			break;
598		case DS9_REG_DIV_NUMER_0:
599		case DS9_REG_DIV_NUMER_1:
600		case DS9_REG_DIV_NUMER_2:
601		case DS9_REG_DIV_NUMER_3:
602		case DS9_REG_DIV_DENOM_0:
603		case DS9_REG_DIV_DENOM_1:
604		case DS9_REG_DIV_DENOM_2:
605		case DS9_REG_DIV_DENOM_3:
606			ds->memory.io9[DS9_REG_DIVCNT >> 1] = _scheduleDiv(ds, ds->memory.io9[DS9_REG_DIVCNT >> 1]);
607			break;
608		case DS9_REG_SQRTCNT:
609			value = _scheduleSqrt(ds, value);
610			break;
611		case DS9_REG_SQRT_PARAM_0:
612		case DS9_REG_SQRT_PARAM_1:
613		case DS9_REG_SQRT_PARAM_2:
614		case DS9_REG_SQRT_PARAM_3:
615			ds->memory.io9[DS9_REG_SQRTCNT >> 1] = _scheduleSqrt(ds, ds->memory.io9[DS9_REG_SQRTCNT >> 1]);
616			break;
617
618		// High Video
619		case DS9_REG_POWCNT1:
620			value = ds->video.renderer->writeVideoRegister(ds->video.renderer, address, value);
621			break;
622
623		default:
624			{
625				uint32_t v2 = DSIOWrite(&ds->ds9, address, value);
626				if (v2 & 0x10000) {
627					value = v2;
628					break;
629				} else if (v2 & 0x20000) {
630					return;
631				}
632			}
633			mLOG(DS_IO, STUB, "Stub DS9 I/O register write: %06X:%04X", address, value);
634			if (address >= DS7_REG_MAX) {
635				mLOG(DS_IO, GAME_ERROR, "Write to unused DS9 I/O register: %06X:%04X", address, value);
636				return;
637			}
638			break;
639		}
640	}
641	ds->memory.io9[address >> 1] = value;
642}
643
644void DS9IOWrite8(struct DS* ds, uint32_t address, uint8_t value) {
645	if (address < DS9_REG_MAX) {
646		uint16_t value16 = value << (8 * (address & 1));
647		value16 |= (ds->memory.io9[(address & 0x1FFF) >> 1]) & ~(0xFF << (8 * (address & 1)));
648		DS9IOWrite(ds, address & 0xFFFFFFFE, value16);
649	} else {
650		mLOG(DS, STUB, "Writing to unknown DS9 register: %08X:%02X", address, value);
651	}
652}
653
654void DS9IOWrite32(struct DS* ds, uint32_t address, uint32_t value) {
655	if ((address >= DS9_REG_RDLINES_COUNT && address <= DS9_REG_VECMTX_RESULT_12) || address == DS9_REG_DISP3DCNT) {
656		value = DSGXWriteRegister32(&ds->gx, address, value);
657	} else {
658		switch (address) {
659		case DS_REG_DMA0SAD_LO:
660		case DS_REG_DMA1SAD_LO:
661		case DS_REG_DMA2SAD_LO:
662		case DS_REG_DMA3SAD_LO:
663		case DS_REG_DMA0DAD_LO:
664		case DS_REG_DMA1DAD_LO:
665		case DS_REG_DMA2DAD_LO:
666		case DS_REG_DMA3DAD_LO:
667		case DS_REG_IPCFIFOSEND_LO:
668		case DS_REG_IE_LO:
669			value = DSIOWrite32(&ds->ds9, address, value);
670			break;
671
672		case DS_REG_DMA0CNT_LO:
673			DS9DMAWriteCNT(&ds->ds9, 0, value);
674			break;
675		case DS_REG_DMA1CNT_LO:
676			DS9DMAWriteCNT(&ds->ds9, 1, value);
677			break;
678		case DS_REG_DMA2CNT_LO:
679			DS9DMAWriteCNT(&ds->ds9, 2, value);
680			break;
681		case DS_REG_DMA3CNT_LO:
682			DS9DMAWriteCNT(&ds->ds9, 3, value);
683			break;
684
685		default:
686			DS9IOWrite(ds, address, value & 0xFFFF);
687			DS9IOWrite(ds, address | 2, value >> 16);
688			return;
689		}
690	}
691	ds->ds9.memory.io[address >> 1] = value;
692	ds->ds9.memory.io[(address >> 1) + 1] = value >> 16;
693}
694
695uint16_t DS9IORead(struct DS* ds, uint32_t address) {
696	switch (address) {
697	case DS_REG_TM0CNT_LO:
698	case DS_REG_TM1CNT_LO:
699	case DS_REG_TM2CNT_LO:
700	case DS_REG_TM3CNT_LO:
701		DSIOUpdateTimer(&ds->ds9, address);
702		break;
703	case DS_REG_KEYINPUT:
704		return DSIOReadKeyInput(ds);
705	case DS_REG_VCOUNT:
706	case DS_REG_DMA0FILL_LO:
707	case DS_REG_DMA0FILL_HI:
708	case DS_REG_DMA1FILL_LO:
709	case DS_REG_DMA1FILL_HI:
710	case DS_REG_DMA2FILL_LO:
711	case DS_REG_DMA2FILL_HI:
712	case DS_REG_DMA3FILL_LO:
713	case DS_REG_DMA3FILL_HI:
714	case DS_REG_TM0CNT_HI:
715	case DS_REG_TM1CNT_HI:
716	case DS_REG_TM2CNT_HI:
717	case DS_REG_TM3CNT_HI:
718	case DS_REG_IPCSYNC:
719	case DS_REG_IPCFIFOCNT:
720	case DS_REG_ROMCNT_LO:
721	case DS_REG_ROMCNT_HI:
722	case DS_REG_IME:
723	case 0x20A:
724	case DS_REG_IE_LO:
725	case DS_REG_IE_HI:
726	case DS_REG_IF_LO:
727	case DS_REG_IF_HI:
728	case DS9_REG_DIVCNT:
729	case DS9_REG_DIV_NUMER_0:
730	case DS9_REG_DIV_NUMER_1:
731	case DS9_REG_DIV_NUMER_2:
732	case DS9_REG_DIV_NUMER_3:
733	case DS9_REG_DIV_DENOM_0:
734	case DS9_REG_DIV_DENOM_1:
735	case DS9_REG_DIV_DENOM_2:
736	case DS9_REG_DIV_DENOM_3:
737	case DS9_REG_DIV_RESULT_0:
738	case DS9_REG_DIV_RESULT_1:
739	case DS9_REG_DIV_RESULT_2:
740	case DS9_REG_DIV_RESULT_3:
741	case DS9_REG_DIVREM_RESULT_0:
742	case DS9_REG_DIVREM_RESULT_1:
743	case DS9_REG_DIVREM_RESULT_2:
744	case DS9_REG_DIVREM_RESULT_3:
745	case DS9_REG_SQRTCNT:
746	case DS9_REG_SQRT_PARAM_0:
747	case DS9_REG_SQRT_PARAM_1:
748	case DS9_REG_SQRT_PARAM_2:
749	case DS9_REG_SQRT_PARAM_3:
750	case DS9_REG_SQRT_RESULT_LO:
751	case DS9_REG_SQRT_RESULT_HI:
752	case DS_REG_POSTFLG:
753	case DS9_REG_GXSTAT_LO:
754	case DS9_REG_GXSTAT_HI:
755	case DS9_REG_CLIPMTX_RESULT_00:
756	case DS9_REG_CLIPMTX_RESULT_01:
757	case DS9_REG_CLIPMTX_RESULT_02:
758	case DS9_REG_CLIPMTX_RESULT_03:
759	case DS9_REG_CLIPMTX_RESULT_04:
760	case DS9_REG_CLIPMTX_RESULT_05:
761	case DS9_REG_CLIPMTX_RESULT_06:
762	case DS9_REG_CLIPMTX_RESULT_07:
763	case DS9_REG_CLIPMTX_RESULT_08:
764	case DS9_REG_CLIPMTX_RESULT_09:
765	case DS9_REG_CLIPMTX_RESULT_0A:
766	case DS9_REG_CLIPMTX_RESULT_0B:
767	case DS9_REG_CLIPMTX_RESULT_0C:
768	case DS9_REG_CLIPMTX_RESULT_0D:
769	case DS9_REG_CLIPMTX_RESULT_0E:
770	case DS9_REG_CLIPMTX_RESULT_0F:
771	case DS9_REG_CLIPMTX_RESULT_10:
772	case DS9_REG_CLIPMTX_RESULT_11:
773	case DS9_REG_CLIPMTX_RESULT_12:
774	case DS9_REG_CLIPMTX_RESULT_13:
775	case DS9_REG_CLIPMTX_RESULT_14:
776	case DS9_REG_CLIPMTX_RESULT_15:
777	case DS9_REG_CLIPMTX_RESULT_16:
778	case DS9_REG_CLIPMTX_RESULT_17:
779	case DS9_REG_CLIPMTX_RESULT_18:
780	case DS9_REG_CLIPMTX_RESULT_19:
781	case DS9_REG_CLIPMTX_RESULT_1A:
782	case DS9_REG_CLIPMTX_RESULT_1B:
783	case DS9_REG_CLIPMTX_RESULT_1C:
784	case DS9_REG_CLIPMTX_RESULT_1D:
785	case DS9_REG_CLIPMTX_RESULT_1E:
786	case DS9_REG_CLIPMTX_RESULT_1F:
787		// Handled transparently by the registers
788		break;
789	case DS_REG_AUXSPICNT:
790	case DS_REG_AUXSPIDATA:
791		if (ds->ds9.memory.slot1Access) {
792			break;
793		} else {
794			mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
795			return 0;
796		}
797	default:
798		mLOG(DS_IO, STUB, "Stub DS9 I/O register read: %06X", address);
799	}
800	if (address < DS9_REG_MAX) {
801		return ds->ds9.memory.io[address >> 1];
802	}
803	return 0;
804}
805
806uint32_t DS9IORead32(struct DS* ds, uint32_t address) {
807	switch (address) {
808	case DS_REG_IPCFIFORECV_LO:
809		return DSIPCReadFIFO(&ds->ds9);
810	case DS_REG_ROMDATA_0:
811		if (ds->ds9.memory.slot1Access) {
812			return DSSlot1Read(ds);
813		} else {
814			mLOG(DS_IO, GAME_ERROR, "Invalid cart access");
815			return 0;
816		}
817	default:
818		return DS9IORead(ds, address & 0x00FFFFFC) | (DS9IORead(ds, (address & 0x00FFFFFC) | 2) << 16);
819	}
820}