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};
39
40enum GPIORegister {
41 GPIO_REG_DATA = 0xC4,
42 GPIO_REG_DIRECTION = 0xC6,
43 GPIO_REG_CONTROL = 0xC8
44};
45
46enum GPIODirection {
47 GPIO_WRITE_ONLY = 0,
48 GPIO_READ_WRITE = 1
49};
50
51DECL_BITFIELD(RTCControl, uint8_t);
52DECL_BIT(RTCControl, Reset, 0);
53DECL_BIT(RTCControl, Hour24, 1);
54DECL_BIT(RTCControl, IRQ1, 4);
55DECL_BIT(RTCControl, IRQ2, 5);
56
57enum RTCCommand {
58 RTC_STATUS1 = 0,
59 RTC_ALARM1 = 1,
60 RTC_DATETIME = 2,
61 RTC_FORCE_IRQ = 3,
62 RTC_STATUS2 = 4,
63 RTC_ALARM2 = 5,
64 RTC_TIME = 6,
65 RTC_FREE_REG = 7
66};
67
68DECL_BITFIELD(RTCCommandData, uint8_t);
69DECL_BITS(RTCCommandData, Magic, 0, 4);
70DECL_BITS(RTCCommandData, Command, 4, 3);
71DECL_BIT(RTCCommandData, Reading, 7);
72
73DECL_BITFIELD(RTCStatus2, uint8_t);
74
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 RTCStatus2 status2;
84 uint8_t freeReg;
85 RTCControl control;
86 uint8_t reserved1;
87 uint8_t reserved2;
88 uint8_t reserved3;
89 uint8_t time[7];
90};
91#pragma pack(pop)
92
93struct GBAGBPKeyCallback {
94 struct mKeyCallback d;
95 struct GBACartridgeHardware* p;
96};
97
98struct GBAGBPSIODriver {
99 struct GBASIODriver d;
100 struct GBACartridgeHardware* p;
101};
102
103DECL_BITFIELD(GPIOPin, uint16_t);
104
105struct GBACartridgeHardware {
106 struct GBA* p;
107 uint32_t devices;
108 enum GPIODirection readWrite;
109 uint16_t* gpioBase;
110
111 uint16_t pinState;
112 uint16_t direction;
113
114 struct GBARTC rtc;
115
116 uint16_t gyroSample;
117 bool gyroEdge;
118
119 unsigned lightCounter : 12;
120 uint8_t lightSample;
121 bool lightEdge;
122
123 uint16_t tiltX;
124 uint16_t tiltY;
125 int tiltState;
126
127 unsigned gbpInputsPosted;
128 int gbpTxPosition;
129 struct mTimingEvent gbpNextEvent;
130 struct GBAGBPKeyCallback gbpCallback;
131 struct GBAGBPSIODriver gbpDriver;
132};
133
134void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
135void GBAHardwareClear(struct GBACartridgeHardware* gpio);
136
137void GBARTCProcessByte(struct GBARTC* rtc, struct mRTCSource* source);
138unsigned GBARTCOutput(struct GBARTC* rtc);
139
140void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
141void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
142void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
143void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
144void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
145
146void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
147void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
148uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
149
150struct GBAVideo;
151void GBAHardwarePlayerUpdate(struct GBA* gba);
152bool GBAHardwarePlayerCheckScreen(const struct GBAVideo* video);
153
154struct GBASerializedState;
155void GBAHardwareSerialize(const struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
156void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, const struct GBASerializedState* state);
157
158CXX_GUARD_END
159
160#endif