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 
11 CXX_GUARD_START
12 
13 #include <mgba/core/log.h>
14 #include <mgba/core/timing.h>
15 #include <mgba/gba/interface.h>
16 
17 mLOG_DECLARE_CATEGORY(GBA_HW);
18 
19 #define IS_GPIO_REGISTER(reg) ((reg) == GPIO_REG_DATA || (reg) == GPIO_REG_DIRECTION || (reg) == GPIO_REG_CONTROL)
20 
21 struct GBARTCGenericSource {
22 	struct mRTCSource d;
23 	struct GBA* p;
24 	enum mRTCGenericType override;
25 	int64_t value;
26 };
27 
28 enum 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 };
39 
40 enum GPIORegister {
41 	GPIO_REG_DATA = 0xC4,
42 	GPIO_REG_DIRECTION = 0xC6,
43 	GPIO_REG_CONTROL = 0xC8
44 };
45 
46 enum GPIODirection {
47 	GPIO_WRITE_ONLY = 0,
48 	GPIO_READ_WRITE = 1
49 };
50 
51 DECL_BITFIELD(RTCControl, uint32_t);
52 DECL_BIT(RTCControl, MinIRQ, 3);
53 DECL_BIT(RTCControl, Hour24, 6);
54 DECL_BIT(RTCControl, Poweroff, 7);
55 
56 enum RTCCommand {
57 	RTC_RESET = 0,
58 	RTC_DATETIME = 2,
59 	RTC_FORCE_IRQ = 3,
60 	RTC_CONTROL = 4,
61 	RTC_TIME = 6
62 };
63 
64 DECL_BITFIELD(RTCCommandData, uint32_t);
65 DECL_BITS(RTCCommandData, Magic, 0, 4);
66 DECL_BITS(RTCCommandData, Command, 4, 3);
67 DECL_BIT(RTCCommandData, Reading, 7);
68 
69 #ifndef PYCPARSE
70 #pragma pack(push, 1)
71 struct GBARTC {
72 	int32_t bytesRemaining;
73 	int32_t transferStep;
74 	int32_t bitsRead;
75 	int32_t bits;
76 	int32_t commandActive;
77 	RTCCommandData command;
78 	RTCControl control;
79 	uint8_t time[7];
80 };
81 #pragma pack(pop)
82 #else
83 struct GBATRC;
84 #endif
85 
86 struct GBAGBPKeyCallback {
87 	struct mKeyCallback d;
88 	struct GBACartridgeHardware* p;
89 };
90 
91 struct GBAGBPSIODriver {
92 	struct GBASIODriver d;
93 	struct GBACartridgeHardware* p;
94 };
95 
96 DECL_BITFIELD(GPIOPin, uint16_t);
97 
98 struct GBACartridgeHardware {
99 	struct GBA* p;
100 	uint32_t devices;
101 	enum GPIODirection readWrite;
102 	uint16_t* gpioBase;
103 
104 	uint16_t pinState;
105 	uint16_t direction;
106 
107 	struct GBARTC rtc;
108 
109 	uint16_t gyroSample;
110 	bool gyroEdge;
111 
112 	unsigned lightCounter : 12;
113 	uint8_t lightSample;
114 	bool lightEdge;
115 
116 	uint16_t tiltX;
117 	uint16_t tiltY;
118 	int tiltState;
119 
120 	unsigned gbpInputsPosted;
121 	int gbpTxPosition;
122 	struct mTimingEvent gbpNextEvent;
123 	struct GBAGBPKeyCallback gbpCallback;
124 	struct GBAGBPSIODriver gbpDriver;
125 };
126 
127 void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
128 void GBAHardwareClear(struct GBACartridgeHardware* gpio);
129 
130 void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
131 void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
132 void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
133 void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
134 void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
135 
136 void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
137 void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
138 uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
139 
140 struct GBAVideo;
141 void GBAHardwarePlayerUpdate(struct GBA* gba);
142 bool GBAHardwarePlayerCheckScreen(const struct GBAVideo* video);
143 
144 void GBARTCGenericSourceInit(struct GBARTCGenericSource* rtc, struct GBA* gba);
145 
146 struct GBASerializedState;
147 void GBAHardwareSerialize(const struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
148 void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, const struct GBASerializedState* state);
149 
150 CXX_GUARD_END
151 
152 #endif
153