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