include/mgba/internal/gba/hardware.h (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#ifndef GBA_HARDWARE_H
7#define GBA_HARDWARE_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/core/log.h>
14#include <mgba/core/timing.h>
15#include <mgba/gba/interface.h>
16
17mLOG_DECLARE_CATEGORY(GBA_HW);
18
19#define EREADER_DOTCODE_STRIDE 1420
20#define EREADER_DOTCODE_SIZE (EREADER_DOTCODE_STRIDE * 40)
21#define EREADER_CARDS_MAX 16
22
23#define IS_GPIO_REGISTER(reg) ((reg) == GPIO_REG_DATA || (reg) == GPIO_REG_DIRECTION || (reg) == GPIO_REG_CONTROL)
24
25struct GBARTCGenericSource {
26 struct mRTCSource d;
27 struct GBA* p;
28 enum mRTCGenericType override;
29 int64_t value;
30};
31
32enum GBAHardwareDevice {
33 HW_NO_OVERRIDE = 0x8000,
34 HW_NONE = 0,
35 HW_RTC = 1,
36 HW_RUMBLE = 2,
37 HW_LIGHT_SENSOR = 4,
38 HW_GYRO = 8,
39 HW_TILT = 16,
40 HW_GB_PLAYER = 32,
41 HW_GB_PLAYER_DETECTION = 64,
42 HW_EREADER = 128
43};
44
45enum GPIORegister {
46 GPIO_REG_DATA = 0xC4,
47 GPIO_REG_DIRECTION = 0xC6,
48 GPIO_REG_CONTROL = 0xC8
49};
50
51enum GPIODirection {
52 GPIO_WRITE_ONLY = 0,
53 GPIO_READ_WRITE = 1
54};
55
56DECL_BITFIELD(RTCControl, uint32_t);
57DECL_BIT(RTCControl, MinIRQ, 3);
58DECL_BIT(RTCControl, Hour24, 6);
59DECL_BIT(RTCControl, Poweroff, 7);
60
61enum RTCCommand {
62 RTC_RESET = 0,
63 RTC_DATETIME = 2,
64 RTC_FORCE_IRQ = 3,
65 RTC_CONTROL = 4,
66 RTC_TIME = 6
67};
68
69DECL_BITFIELD(RTCCommandData, uint32_t);
70DECL_BITS(RTCCommandData, Magic, 0, 4);
71DECL_BITS(RTCCommandData, Command, 4, 3);
72DECL_BIT(RTCCommandData, Reading, 7);
73
74struct GBARTC {
75 int32_t bytesRemaining;
76 int32_t transferStep;
77 int32_t bitsRead;
78 int32_t bits;
79 int32_t commandActive;
80 RTCCommandData command;
81 RTCControl control;
82 uint8_t time[7];
83};
84
85struct GBAGBPKeyCallback {
86 struct mKeyCallback d;
87 struct GBACartridgeHardware* p;
88};
89
90struct GBAGBPSIODriver {
91 struct GBASIODriver d;
92 struct GBACartridgeHardware* p;
93};
94
95DECL_BITFIELD(GPIOPin, uint16_t);
96
97DECL_BITFIELD(EReaderControl0, uint8_t);
98DECL_BIT(EReaderControl0, Data, 0);
99DECL_BIT(EReaderControl0, Clock, 1);
100DECL_BIT(EReaderControl0, Direction, 2);
101DECL_BIT(EReaderControl0, LedEnable, 3);
102DECL_BIT(EReaderControl0, Scan, 4);
103DECL_BIT(EReaderControl0, Phi, 5);
104DECL_BIT(EReaderControl0, PowerEnable, 6);
105DECL_BITFIELD(EReaderControl1, uint8_t);
106DECL_BIT(EReaderControl1, Scanline, 1);
107DECL_BIT(EReaderControl1, Unk1, 4);
108DECL_BIT(EReaderControl1, Voltage, 5);
109
110enum EReaderStateMachine {
111 EREADER_SERIAL_INACTIVE = 0,
112 EREADER_SERIAL_STARTING,
113 EREADER_SERIAL_BIT_0,
114 EREADER_SERIAL_BIT_1,
115 EREADER_SERIAL_BIT_2,
116 EREADER_SERIAL_BIT_3,
117 EREADER_SERIAL_BIT_4,
118 EREADER_SERIAL_BIT_5,
119 EREADER_SERIAL_BIT_6,
120 EREADER_SERIAL_BIT_7,
121 EREADER_SERIAL_END_BIT,
122};
123
124enum EReaderCommand {
125 EREADER_COMMAND_IDLE = 0, // TODO: Verify on hardware
126 EREADER_COMMAND_WRITE_DATA = 1,
127 EREADER_COMMAND_SET_INDEX = 0x22,
128 EREADER_COMMAND_READ_DATA = 0x23,
129};
130
131struct EReaderCard {
132 void* data;
133 size_t size;
134};
135
136struct GBACartridgeHardware {
137 struct GBA* p;
138 uint32_t devices;
139 enum GPIODirection readWrite;
140 uint16_t* gpioBase;
141
142 uint16_t pinState;
143 uint16_t direction;
144
145 struct GBARTC rtc;
146
147 uint16_t gyroSample;
148 bool gyroEdge;
149
150 unsigned lightCounter : 12;
151 uint8_t lightSample;
152 bool lightEdge;
153
154 uint16_t tiltX;
155 uint16_t tiltY;
156 int tiltState;
157
158 unsigned gbpInputsPosted;
159 int gbpTxPosition;
160 struct mTimingEvent gbpNextEvent;
161 struct GBAGBPKeyCallback gbpCallback;
162 struct GBAGBPSIODriver gbpDriver;
163
164 uint16_t eReaderData[44];
165 uint8_t eReaderSerial[92];
166 uint16_t eReaderRegisterUnk;
167 uint16_t eReaderRegisterReset;
168 EReaderControl0 eReaderRegisterControl0;
169 EReaderControl1 eReaderRegisterControl1;
170 uint16_t eReaderRegisterLed;
171
172 // TODO: Serialize these
173 enum EReaderStateMachine eReaderState;
174 enum EReaderCommand eReaderCommand;
175 uint8_t eReaderActiveRegister;
176 uint8_t eReaderByte;
177 int eReaderX;
178 int eReaderY;
179 uint8_t* eReaderDots;
180 struct EReaderCard eReaderCards[EREADER_CARDS_MAX];
181};
182
183void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
184void GBAHardwareClear(struct GBACartridgeHardware* gpio);
185
186void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
187void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
188void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
189void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
190void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
191
192void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
193void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
194uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
195
196struct GBAVideo;
197void GBAHardwarePlayerUpdate(struct GBA* gba);
198bool GBAHardwarePlayerCheckScreen(const struct GBAVideo* video);
199
200void GBAHardwareInitEReader(struct GBACartridgeHardware* hw);
201void GBAHardwareEReaderWrite(struct GBACartridgeHardware* hw, uint32_t address, uint16_t value);
202void GBAHardwareEReaderWriteFlash(struct GBACartridgeHardware* hw, uint32_t address, uint8_t value);
203uint16_t GBAHardwareEReaderRead(struct GBACartridgeHardware* hw, uint32_t address);
204uint8_t GBAHardwareEReaderReadFlash(struct GBACartridgeHardware* hw, uint32_t address);
205void GBAHardwareEReaderScan(struct GBACartridgeHardware* hw, const void* data, size_t size);
206
207void GBARTCGenericSourceInit(struct GBARTCGenericSource* rtc, struct GBA* gba);
208
209struct GBASerializedState;
210void GBAHardwareSerialize(const struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
211void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, const struct GBASerializedState* state);
212
213CXX_GUARD_END
214
215#endif