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