all repos — mgba @ b3c7bd8227dcb77222bd596c0cff474919bd5b99

mGBA Game Boy Advance Emulator

src/gba/sio.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "sio.h"
  7
  8#include "gba/io.h"
  9
 10mLOG_DEFINE_CATEGORY(GBA_SIO, "GBA Serial I/O");
 11
 12const int GBASIOCyclesPerTransfer[4][MAX_GBAS] = {
 13	{ 38326, 73003, 107680, 142356 },
 14	{ 9582, 18251, 26920, 35589 },
 15	{ 6388, 12167, 17947, 23726 },
 16	{ 3194, 6075, 8973, 11863 }
 17};
 18
 19static struct GBASIODriver* _lookupDriver(struct GBASIO* sio, enum GBASIOMode mode) {
 20	switch (mode) {
 21	case SIO_NORMAL_8:
 22	case SIO_NORMAL_32:
 23		return sio->drivers.normal;
 24	case SIO_MULTI:
 25		return sio->drivers.multiplayer;
 26	case SIO_JOYBUS:
 27		return sio->drivers.joybus;
 28	default:
 29		return 0;
 30	}
 31}
 32
 33static void _switchMode(struct GBASIO* sio) {
 34	unsigned mode = ((sio->rcnt & 0xC000) | (sio->siocnt & 0x3000)) >> 12;
 35	enum GBASIOMode oldMode = sio->mode;
 36	if (mode < 8) {
 37		sio->mode = (enum GBASIOMode) (mode & 0x3);
 38	} else {
 39		sio->mode = (enum GBASIOMode) (mode & 0xC);
 40	}
 41	if (oldMode != sio->mode) {
 42		if (sio->activeDriver && sio->activeDriver->unload) {
 43			sio->activeDriver->unload(sio->activeDriver);
 44		}
 45		sio->activeDriver = _lookupDriver(sio, sio->mode);
 46		if (sio->activeDriver && sio->activeDriver->load) {
 47			sio->activeDriver->load(sio->activeDriver);
 48		}
 49	}
 50}
 51
 52void GBASIOInit(struct GBASIO* sio) {
 53	sio->drivers.normal = 0;
 54	sio->drivers.multiplayer = 0;
 55	sio->drivers.joybus = 0;
 56	sio->activeDriver = 0;
 57	GBASIOReset(sio);
 58}
 59
 60void GBASIODeinit(struct GBASIO* sio) {
 61	if (sio->activeDriver && sio->activeDriver->unload) {
 62		sio->activeDriver->unload(sio->activeDriver);
 63	}
 64	if (sio->drivers.multiplayer && sio->drivers.multiplayer->deinit) {
 65		sio->drivers.multiplayer->deinit(sio->drivers.multiplayer);
 66	}
 67	if (sio->drivers.joybus && sio->drivers.joybus->deinit) {
 68		sio->drivers.joybus->deinit(sio->drivers.joybus);
 69	}
 70	if (sio->drivers.normal && sio->drivers.normal->deinit) {
 71		sio->drivers.normal->deinit(sio->drivers.normal);
 72	}
 73}
 74
 75void GBASIOReset(struct GBASIO* sio) {
 76	GBASIODeinit(sio);
 77	sio->rcnt = RCNT_INITIAL;
 78	sio->siocnt = 0;
 79	sio->mode = -1;
 80	_switchMode(sio);
 81}
 82
 83void GBASIOSetDriverSet(struct GBASIO* sio, struct GBASIODriverSet* drivers) {
 84	GBASIOSetDriver(sio, drivers->normal, SIO_NORMAL_8);
 85	GBASIOSetDriver(sio, drivers->multiplayer, SIO_MULTI);
 86	GBASIOSetDriver(sio, drivers->joybus, SIO_JOYBUS);
 87}
 88
 89void GBASIOSetDriver(struct GBASIO* sio, struct GBASIODriver* driver, enum GBASIOMode mode) {
 90	struct GBASIODriver** driverLoc;
 91	switch (mode) {
 92	case SIO_NORMAL_8:
 93	case SIO_NORMAL_32:
 94		driverLoc = &sio->drivers.normal;
 95		break;
 96	case SIO_MULTI:
 97		driverLoc = &sio->drivers.multiplayer;
 98		break;
 99	case SIO_JOYBUS:
100		driverLoc = &sio->drivers.joybus;
101		break;
102	default:
103		mLOG(GBA_SIO, ERROR, "Setting an unsupported SIO driver: %x", mode);
104		return;
105	}
106	if (*driverLoc) {
107		if ((*driverLoc)->unload) {
108			(*driverLoc)->unload(*driverLoc);
109		}
110		if ((*driverLoc)->deinit) {
111			(*driverLoc)->deinit(*driverLoc);
112		}
113	}
114	if (driver) {
115		driver->p = sio;
116
117		if (driver->init) {
118			if (!driver->init(driver)) {
119				driver->deinit(driver);
120				mLOG(GBA_SIO, ERROR, "Could not initialize SIO driver");
121				return;
122			}
123		}
124		if (sio->mode == mode) {
125			sio->activeDriver = driver;
126			if (driver->load) {
127				driver->load(driver);
128			}
129		}
130	}
131	*driverLoc = driver;
132}
133
134void GBASIOWriteRCNT(struct GBASIO* sio, uint16_t value) {
135	sio->rcnt &= 0xF;
136	sio->rcnt |= value & ~0xF;
137	_switchMode(sio);
138	if (sio->activeDriver && sio->activeDriver->writeRegister) {
139		sio->activeDriver->writeRegister(sio->activeDriver, REG_RCNT, value);
140	}
141}
142
143void GBASIOWriteSIOCNT(struct GBASIO* sio, uint16_t value) {
144	if ((value ^ sio->siocnt) & 0x3000) {
145		sio->siocnt = value & 0x3000;
146		_switchMode(sio);
147	}
148	if (sio->activeDriver && sio->activeDriver->writeRegister) {
149		value = sio->activeDriver->writeRegister(sio->activeDriver, REG_SIOCNT, value);
150	} else {
151		// Dummy drivers
152		switch (sio->mode) {
153		case SIO_NORMAL_8:
154		case SIO_NORMAL_32:
155			value |= 0x0004;
156			if ((value & 0x4080) == 0x4080) {
157				// TODO: Test this on hardware to see if this is correct
158				GBARaiseIRQ(sio->p, IRQ_SIO);
159			}
160			value &= ~0x0080;
161			break;
162		default:
163			// TODO
164			break;
165		}
166	}
167	sio->siocnt = value;
168}
169
170uint16_t GBASIOWriteRegister(struct GBASIO* sio, uint32_t address, uint16_t value) {
171	if (sio->activeDriver && sio->activeDriver->writeRegister) {
172		return sio->activeDriver->writeRegister(sio->activeDriver, address, value);
173	}
174	return value;
175}
176
177int32_t GBASIOProcessEvents(struct GBASIO* sio, int32_t cycles) {
178	if (sio->activeDriver && sio->activeDriver->processEvents) {
179		return sio->activeDriver->processEvents(sio->activeDriver, cycles);
180	}
181	return INT_MAX;
182}