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 1200
20#define EREADER_DOTCODE_SIZE (EREADER_DOTCODE_STRIDE * 40 + 200)
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
74#ifndef PYCPARSE
75#pragma pack(push, 1)
76struct GBARTC {
77 int32_t bytesRemaining;
78 int32_t transferStep;
79 int32_t bitsRead;
80 int32_t bits;
81 int32_t commandActive;
82 RTCCommandData command;
83 RTCControl control;
84 uint8_t time[7];
85};
86#pragma pack(pop)
87#else
88struct GBATRC;
89#endif
90
91struct GBAGBPKeyCallback {
92 struct mKeyCallback d;
93 struct GBACartridgeHardware* p;
94};
95
96struct GBAGBPSIODriver {
97 struct GBASIODriver d;
98 struct GBACartridgeHardware* p;
99};
100
101DECL_BITFIELD(GPIOPin, uint16_t);
102
103DECL_BITFIELD(EReaderControl0, uint8_t);
104DECL_BIT(EReaderControl0, Data, 0);
105DECL_BIT(EReaderControl0, Clock, 1);
106DECL_BIT(EReaderControl0, Direction, 2);
107DECL_BIT(EReaderControl0, LedEnable, 3);
108DECL_BIT(EReaderControl0, Scan, 4);
109DECL_BIT(EReaderControl0, Phi, 5);
110DECL_BIT(EReaderControl0, PowerEnable, 6);
111DECL_BITFIELD(EReaderControl1, uint8_t);
112DECL_BIT(EReaderControl1, Scanline, 1);
113DECL_BIT(EReaderControl1, Unk1, 4);
114DECL_BIT(EReaderControl1, Voltage, 5);
115
116enum EReaderStateMachine {
117 EREADER_SERIAL_INACTIVE = 0,
118 EREADER_SERIAL_STARTING,
119 EREADER_SERIAL_BIT_0,
120 EREADER_SERIAL_BIT_1,
121 EREADER_SERIAL_BIT_2,
122 EREADER_SERIAL_BIT_3,
123 EREADER_SERIAL_BIT_4,
124 EREADER_SERIAL_BIT_5,
125 EREADER_SERIAL_BIT_6,
126 EREADER_SERIAL_BIT_7,
127 EREADER_SERIAL_END_BIT,
128};
129
130enum EReaderCommand {
131 EREADER_COMMAND_IDLE = 0, // TODO: Verify on hardware
132 EREADER_COMMAND_WRITE_DATA = 1,
133 EREADER_COMMAND_SET_INDEX = 0x22,
134 EREADER_COMMAND_READ_DATA = 0x23,
135};
136
137struct EReaderCard {
138 void* data;
139 size_t size;
140};
141
142struct GBACartridgeHardware {
143 struct GBA* p;
144 uint32_t devices;
145 enum GPIODirection readWrite;
146 uint16_t* gpioBase;
147
148 uint16_t pinState;
149 uint16_t direction;
150
151 struct GBARTC rtc;
152
153 uint16_t gyroSample;
154 bool gyroEdge;
155
156 unsigned lightCounter : 12;
157 uint8_t lightSample;
158 bool lightEdge;
159
160 uint16_t tiltX;
161 uint16_t tiltY;
162 int tiltState;
163
164 unsigned gbpInputsPosted;
165 int gbpTxPosition;
166 struct mTimingEvent gbpNextEvent;
167 struct GBAGBPKeyCallback gbpCallback;
168 struct GBAGBPSIODriver gbpDriver;
169
170 uint16_t eReaderData[44];
171 uint8_t eReaderSerial[92];
172 uint16_t eReaderRegisterUnk;
173 uint16_t eReaderRegisterReset;
174 EReaderControl0 eReaderRegisterControl0;
175 EReaderControl1 eReaderRegisterControl1;
176 uint16_t eReaderRegisterLed;
177
178 // TODO: Serialize these
179 enum EReaderStateMachine eReaderState;
180 enum EReaderCommand eReaderCommand;
181 uint8_t eReaderActiveRegister;
182 uint8_t eReaderByte;
183 int eReaderX;
184 int eReaderY;
185 uint8_t* eReaderDots;
186 struct EReaderCard eReaderCards[EREADER_CARDS_MAX];
187};
188
189void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
190void GBAHardwareClear(struct GBACartridgeHardware* gpio);
191
192void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
193void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
194void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
195void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
196void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
197
198void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
199void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
200uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
201
202struct GBAVideo;
203void GBAHardwarePlayerUpdate(struct GBA* gba);
204bool GBAHardwarePlayerCheckScreen(const struct GBAVideo* video);
205
206void GBAHardwareInitEReader(struct GBACartridgeHardware* hw);
207void GBAHardwareEReaderWrite(struct GBACartridgeHardware* hw, uint32_t address, uint16_t value);
208void GBAHardwareEReaderWriteFlash(struct GBACartridgeHardware* hw, uint32_t address, uint8_t value);
209uint16_t GBAHardwareEReaderRead(struct GBACartridgeHardware* hw, uint32_t address);
210uint8_t GBAHardwareEReaderReadFlash(struct GBACartridgeHardware* hw, uint32_t address);
211void GBAHardwareEReaderScan(struct GBACartridgeHardware* hw, const void* data, size_t size);
212
213void GBARTCGenericSourceInit(struct GBARTCGenericSource* rtc, struct GBA* gba);
214
215struct GBASerializedState;
216void GBAHardwareSerialize(const struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
217void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, const struct GBASerializedState* state);
218
219CXX_GUARD_END
220
221#endif