src/gba/gba-gpio.c (view raw)
1/* Copyright (c) 2013-2014 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 "gba.h"
7
8#include "gba-gpio.h"
9#include "gba-sensors.h"
10#include "gba-serialize.h"
11
12#include <time.h>
13
14static void _readPins(struct GBACartridgeGPIO* gpio);
15static void _outputPins(struct GBACartridgeGPIO* gpio, unsigned pins);
16
17static void _rtcReadPins(struct GBACartridgeGPIO* gpio);
18static unsigned _rtcOutput(struct GBACartridgeGPIO* gpio);
19static void _rtcProcessByte(struct GBACartridgeGPIO* gpio);
20static void _rtcUpdateClock(struct GBACartridgeGPIO* gpio);
21static unsigned _rtcBCD(unsigned value);
22
23static void _gyroReadPins(struct GBACartridgeGPIO* gpio);
24
25static void _rumbleReadPins(struct GBACartridgeGPIO* gpio);
26
27static void _lightReadPins(struct GBACartridgeGPIO* gpio);
28
29static const int RTC_BYTES[8] = {
30 0, // Force reset
31 0, // Empty
32 7, // Date/Time
33 0, // Force IRQ
34 1, // Control register
35 0, // Empty
36 3, // Time
37 0 // Empty
38};
39
40void GBAGPIOInit(struct GBACartridgeGPIO* gpio, uint16_t* base) {
41 gpio->gpioDevices = GPIO_NONE;
42 gpio->direction = GPIO_WRITE_ONLY;
43 gpio->gpioBase = base;
44 gpio->pinState = 0;
45 gpio->direction = 0;
46}
47
48void GBAGPIOWrite(struct GBACartridgeGPIO* gpio, uint32_t address, uint16_t value) {
49 switch (address) {
50 case GPIO_REG_DATA:
51 gpio->pinState &= ~gpio->direction;
52 gpio->pinState |= value;
53 _readPins(gpio);
54 break;
55 case GPIO_REG_DIRECTION:
56 gpio->direction = value;
57 break;
58 case GPIO_REG_CONTROL:
59 gpio->readWrite = value;
60 break;
61 default:
62 GBALog(gpio->p, GBA_LOG_WARN, "Invalid GPIO address");
63 }
64 if (gpio->readWrite) {
65 uint16_t old = gpio->gpioBase[0];
66 old &= ~gpio->direction;
67 gpio->gpioBase[0] = old | gpio->pinState;
68 } else {
69 gpio->gpioBase[0] = 0;
70 }
71}
72
73void GBAGPIOInitRTC(struct GBACartridgeGPIO* gpio) {
74 gpio->gpioDevices |= GPIO_RTC;
75 gpio->rtc.bytesRemaining = 0;
76
77 gpio->rtc.transferStep = 0;
78
79 gpio->rtc.bitsRead = 0;
80 gpio->rtc.bits = 0;
81 gpio->rtc.commandActive = 0;
82 gpio->rtc.command.packed = 0;
83 gpio->rtc.control.packed = 0x40;
84 memset(gpio->rtc.time, 0, sizeof(gpio->rtc.time));
85}
86
87void _readPins(struct GBACartridgeGPIO* gpio) {
88 if (gpio->gpioDevices & GPIO_RTC) {
89 _rtcReadPins(gpio);
90 }
91
92 if (gpio->gpioDevices & GPIO_GYRO) {
93 _gyroReadPins(gpio);
94 }
95
96 if (gpio->gpioDevices & GPIO_RUMBLE) {
97 _rumbleReadPins(gpio);
98 }
99
100 if (gpio->gpioDevices & GPIO_LIGHT_SENSOR) {
101 _lightReadPins(gpio);
102 }
103}
104
105void _outputPins(struct GBACartridgeGPIO* gpio, unsigned pins) {
106 if (gpio->readWrite) {
107 uint16_t old = gpio->gpioBase[0];
108 old &= gpio->direction;
109 gpio->pinState = old | (pins & ~gpio->direction & 0xF);
110 gpio->gpioBase[0] = gpio->pinState;
111 }
112}
113
114// == RTC
115
116void _rtcReadPins(struct GBACartridgeGPIO* gpio) {
117 // Transfer sequence:
118 // P: 0 | 1 | 2 | 3
119 // == Initiate
120 // > HI | - | LO | -
121 // > HI | - | HI | -
122 // == Transfer bit (x8)
123 // > LO | x | HI | -
124 // > HI | - | HI | -
125 // < ?? | x | ?? | -
126 // == Terminate
127 // > - | - | LO | -
128 switch (gpio->rtc.transferStep) {
129 case 0:
130 if ((gpio->pinState & 5) == 1) {
131 gpio->rtc.transferStep = 1;
132 }
133 break;
134 case 1:
135 if ((gpio->pinState & 5) == 5) {
136 gpio->rtc.transferStep = 2;
137 }
138 break;
139 case 2:
140 if (!gpio->p0) {
141 gpio->rtc.bits &= ~(1 << gpio->rtc.bitsRead);
142 gpio->rtc.bits |= gpio->p1 << gpio->rtc.bitsRead;
143 } else {
144 if (gpio->p2) {
145 // GPIO direction should always != reading
146 if (gpio->dir1) {
147 if (gpio->rtc.command.reading) {
148 GBALog(gpio->p, GBA_LOG_GAME_ERROR, "Attempting to write to RTC while in read mode");
149 }
150 ++gpio->rtc.bitsRead;
151 if (gpio->rtc.bitsRead == 8) {
152 _rtcProcessByte(gpio);
153 }
154 } else {
155 _outputPins(gpio, 5 | (_rtcOutput(gpio) << 1));
156 ++gpio->rtc.bitsRead;
157 if (gpio->rtc.bitsRead == 8) {
158 --gpio->rtc.bytesRemaining;
159 if (gpio->rtc.bytesRemaining <= 0) {
160 gpio->rtc.commandActive = 0;
161 gpio->rtc.command.reading = 0;
162 }
163 gpio->rtc.bitsRead = 0;
164 }
165 }
166 } else {
167 gpio->rtc.bitsRead = 0;
168 gpio->rtc.bytesRemaining = 0;
169 gpio->rtc.commandActive = 0;
170 gpio->rtc.command.reading = 0;
171 gpio->rtc.transferStep = 0;
172 }
173 }
174 break;
175 }
176}
177
178void _rtcProcessByte(struct GBACartridgeGPIO* gpio) {
179 --gpio->rtc.bytesRemaining;
180 if (!gpio->rtc.commandActive) {
181 union RTCCommandData command;
182 command.packed = gpio->rtc.bits;
183 if (command.magic == 0x06) {
184 gpio->rtc.command = command;
185
186 gpio->rtc.bytesRemaining = RTC_BYTES[gpio->rtc.command.command];
187 gpio->rtc.commandActive = gpio->rtc.bytesRemaining > 0;
188 switch (command.command) {
189 case RTC_RESET:
190 gpio->rtc.control.packed = 0;
191 break;
192 case RTC_DATETIME:
193 case RTC_TIME:
194 _rtcUpdateClock(gpio);
195 break;
196 case RTC_FORCE_IRQ:
197 case RTC_CONTROL:
198 break;
199 }
200 } else {
201 GBALog(gpio->p, GBA_LOG_WARN, "Invalid RTC command byte: %02X", gpio->rtc.bits);
202 }
203 } else {
204 switch (gpio->rtc.command.command) {
205 case RTC_CONTROL:
206 gpio->rtc.control.packed = gpio->rtc.bits;
207 break;
208 case RTC_FORCE_IRQ:
209 GBALog(gpio->p, GBA_LOG_STUB, "Unimplemented RTC command %u", gpio->rtc.command.command);
210 break;
211 case RTC_RESET:
212 case RTC_DATETIME:
213 case RTC_TIME:
214 break;
215 }
216 }
217
218 gpio->rtc.bits = 0;
219 gpio->rtc.bitsRead = 0;
220 if (!gpio->rtc.bytesRemaining) {
221 gpio->rtc.commandActive = 0;
222 gpio->rtc.command.reading = 0;
223 }
224}
225
226unsigned _rtcOutput(struct GBACartridgeGPIO* gpio) {
227 uint8_t outputByte = 0;
228 switch (gpio->rtc.command.command) {
229 case RTC_CONTROL:
230 outputByte = gpio->rtc.control.packed;
231 break;
232 case RTC_DATETIME:
233 case RTC_TIME:
234 outputByte = gpio->rtc.time[7 - gpio->rtc.bytesRemaining];
235 break;
236 case RTC_FORCE_IRQ:
237 case RTC_RESET:
238 break;
239 }
240 unsigned output = (outputByte >> gpio->rtc.bitsRead) & 1;
241 return output;
242}
243
244void _rtcUpdateClock(struct GBACartridgeGPIO* gpio) {
245 time_t t;
246 struct GBARTCSource* rtc = gpio->p->rtcSource;
247 if (rtc) {
248 rtc->sample(rtc);
249 t = rtc->unixTime(rtc);
250 } else {
251 t = time(0);
252 }
253 struct tm date;
254#ifdef _WIN32
255 date = *localtime(&t);
256#else
257 localtime_r(&t, &date);
258#endif
259 gpio->rtc.time[0] = _rtcBCD(date.tm_year - 100);
260 gpio->rtc.time[1] = _rtcBCD(date.tm_mon + 1);
261 gpio->rtc.time[2] = _rtcBCD(date.tm_mday);
262 gpio->rtc.time[3] = _rtcBCD(date.tm_wday);
263 if (gpio->rtc.control.hour24) {
264 gpio->rtc.time[4] = _rtcBCD(date.tm_hour);
265 } else {
266 gpio->rtc.time[4] = _rtcBCD(date.tm_hour % 12);
267 }
268 gpio->rtc.time[5] = _rtcBCD(date.tm_min);
269 gpio->rtc.time[6] = _rtcBCD(date.tm_sec);
270}
271
272unsigned _rtcBCD(unsigned value) {
273 int counter = value % 10;
274 value /= 10;
275 counter += (value % 10) << 4;
276 return counter;
277}
278
279// == Gyro
280
281void GBAGPIOInitGyro(struct GBACartridgeGPIO* gpio) {
282 gpio->gpioDevices |= GPIO_GYRO;
283 gpio->gyroSample = 0;
284 gpio->gyroEdge = 0;
285}
286
287void _gyroReadPins(struct GBACartridgeGPIO* gpio) {
288 struct GBARotationSource* gyro = gpio->p->rotationSource;
289 if (!gyro) {
290 return;
291 }
292
293 if (gpio->p0) {
294 if (gyro->sample) {
295 gyro->sample(gyro);
296 }
297 int32_t sample = gyro->readGyroZ(gyro);
298
299 // Normalize to ~12 bits, focused on 0x6C0
300 gpio->gyroSample = (sample >> 21) + 0x6C0; // Crop off an extra bit so that we can't go negative
301 }
302
303 if (gpio->gyroEdge && !gpio->p1) {
304 // Write bit on falling edge
305 unsigned bit = gpio->gyroSample >> 15;
306 gpio->gyroSample <<= 1;
307 _outputPins(gpio, bit << 2);
308 }
309
310 gpio->gyroEdge = gpio->p1;
311}
312
313// == Rumble
314
315void GBAGPIOInitRumble(struct GBACartridgeGPIO* gpio) {
316 gpio->gpioDevices |= GPIO_RUMBLE;
317}
318
319void _rumbleReadPins(struct GBACartridgeGPIO* gpio) {
320 struct GBARumble* rumble = gpio->p->rumble;
321 if (!rumble) {
322 return;
323 }
324
325 rumble->setRumble(rumble, gpio->p3);
326}
327
328// == Light sensor
329
330void GBAGPIOInitLightSensor(struct GBACartridgeGPIO* gpio) {
331 gpio->gpioDevices |= GPIO_LIGHT_SENSOR;
332 gpio->lightCounter = 0;
333 gpio->lightEdge = false;
334 gpio->lightSample = 0xFF;
335}
336
337void _lightReadPins(struct GBACartridgeGPIO* gpio) {
338 if (gpio->p2) {
339 // Boktai chip select
340 return;
341 }
342 if (gpio->p1) {
343 struct GBALuminanceSource* lux = gpio->p->luminanceSource;
344 GBALog(gpio->p, GBA_LOG_DEBUG, "[SOLAR] Got reset");
345 gpio->lightCounter = 0;
346 if (lux) {
347 lux->sample(lux);
348 gpio->lightSample = lux->readLuminance(lux);
349 } else {
350 gpio->lightSample = 0xFF;
351 }
352 }
353 if (gpio->p0 && gpio->lightEdge) {
354 ++gpio->lightCounter;
355 }
356 gpio->lightEdge = !gpio->p0;
357
358 bool sendBit = gpio->lightCounter >= gpio->lightSample;
359 _outputPins(gpio, sendBit << 3);
360 GBALog(gpio->p, GBA_LOG_DEBUG, "[SOLAR] Output %u with pins %u", gpio->lightCounter, gpio->pinState);
361}
362
363// == Tilt (not technically GPIO)
364
365void GBAGPIOInitTilt(struct GBACartridgeGPIO* gpio) {
366 gpio->gpioDevices |= GPIO_TILT;
367 gpio->tiltX = 0xFFF;
368 gpio->tiltY = 0xFFF;
369 gpio->tiltState = 0;
370}
371
372void GBAGPIOTiltWrite(struct GBACartridgeGPIO* gpio, uint32_t address, uint8_t value) {
373 switch (address) {
374 case 0x8000:
375 if (value == 0x55) {
376 gpio->tiltState = 1;
377 } else {
378 GBALog(gpio->p, GBA_LOG_GAME_ERROR, "Tilt sensor wrote wrong byte to %04x: %02x", address, value);
379 }
380 break;
381 case 0x8100:
382 if (value == 0xAA && gpio->tiltState == 1) {
383 gpio->tiltState = 0;
384 struct GBARotationSource* rotationSource = gpio->p->rotationSource;
385 if (!rotationSource || !rotationSource->readTiltX || !rotationSource->readTiltY) {
386 return;
387 }
388 if (rotationSource->sample) {
389 rotationSource->sample(rotationSource);
390 }
391 int32_t x = rotationSource->readTiltX(rotationSource);
392 int32_t y = rotationSource->readTiltY(rotationSource);
393 // Normalize to ~12 bits, focused on 0x3A0
394 gpio->tiltX = (x >> 21) + 0x3A0; // Crop off an extra bit so that we can't go negative
395 gpio->tiltY = (y >> 21) + 0x3A0;
396 } else {
397 GBALog(gpio->p, GBA_LOG_GAME_ERROR, "Tilt sensor wrote wrong byte to %04x: %02x", address, value);
398 }
399 break;
400 default:
401 GBALog(gpio->p, GBA_LOG_GAME_ERROR, "Invalid tilt sensor write to %04x: %02x", address, value);
402 break;
403 }
404}
405
406uint8_t GBAGPIOTiltRead(struct GBACartridgeGPIO* gpio, uint32_t address) {
407 switch (address) {
408 case 0x8200:
409 return gpio->tiltX & 0xFF;
410 case 0x8300:
411 return ((gpio->tiltX >> 8) & 0xF) | 0x80;
412 case 0x8400:
413 return gpio->tiltY & 0xFF;
414 case 0x8500:
415 return (gpio->tiltY >> 8) & 0xF;
416 default:
417 GBALog(gpio->p, GBA_LOG_GAME_ERROR, "Invalid tilt sensor read from %04x", address);
418 break;
419 }
420 return 0xFF;
421}
422
423// == Serialization
424
425void GBAGPIOSerialize(struct GBACartridgeGPIO* gpio, struct GBASerializedState* state) {
426 state->gpio.readWrite = gpio->readWrite;
427 state->gpio.pinState = gpio->pinState;
428 state->gpio.pinDirection = gpio->direction;
429 state->gpio.devices = gpio->gpioDevices;
430 state->gpio.rtc = gpio->rtc;
431 state->gpio.gyroSample = gpio->gyroSample;
432 state->gpio.gyroEdge = gpio->gyroEdge;
433 state->gpio.tiltSampleX = gpio->tiltX;
434 state->gpio.tiltSampleY = gpio->tiltY;
435 state->gpio.tiltState = gpio->tiltState;
436 state->gpio.lightCounter = gpio->lightCounter;
437 state->gpio.lightSample = gpio->lightSample;
438 state->gpio.lightEdge = gpio->lightEdge;
439}
440
441void GBAGPIODeserialize(struct GBACartridgeGPIO* gpio, struct GBASerializedState* state) {
442 gpio->readWrite = state->gpio.readWrite;
443 gpio->pinState = state->gpio.pinState;
444 gpio->direction = state->gpio.pinDirection;
445 // TODO: Deterministic RTC
446 gpio->rtc = state->gpio.rtc;
447 gpio->gyroSample = state->gpio.gyroSample;
448 gpio->gyroEdge = state->gpio.gyroEdge;
449 gpio->tiltX = state->gpio.tiltSampleX;
450 gpio->tiltY = state->gpio.tiltSampleY;
451 gpio->tiltState = state->gpio.tiltState;
452 gpio->lightCounter = state->gpio.lightCounter;
453 gpio->lightSample = state->gpio.lightSample;
454 gpio->lightEdge = state->gpio.lightEdge;
455}