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
17#include <time.h>
18
19mLOG_DECLARE_CATEGORY(GBA_HW);
20
21#define IS_GPIO_REGISTER(reg) ((reg) == GPIO_REG_DATA || (reg) == GPIO_REG_DIRECTION || (reg) == GPIO_REG_CONTROL)
22
23struct GBARTCGenericSource {
24 struct mRTCSource d;
25 struct GBA* p;
26 enum mRTCGenericType override;
27 int64_t value;
28};
29
30enum GBAHardwareDevice {
31 HW_NO_OVERRIDE = 0x8000,
32 HW_NONE = 0,
33 HW_RTC = 1,
34 HW_RUMBLE = 2,
35 HW_LIGHT_SENSOR = 4,
36 HW_GYRO = 8,
37 HW_TILT = 16,
38 HW_GB_PLAYER = 32,
39 HW_GB_PLAYER_DETECTION = 64
40};
41
42enum GPIORegister {
43 GPIO_REG_DATA = 0xC4,
44 GPIO_REG_DIRECTION = 0xC6,
45 GPIO_REG_CONTROL = 0xC8
46};
47
48enum GPIODirection {
49 GPIO_WRITE_ONLY = 0,
50 GPIO_READ_WRITE = 1
51};
52
53DECL_BITFIELD(RTCControl, uint32_t);
54DECL_BIT(RTCControl, MinIRQ, 3);
55DECL_BIT(RTCControl, Hour24, 6);
56DECL_BIT(RTCControl, Poweroff, 7);
57
58enum RTCCommand {
59 RTC_RESET = 0,
60 RTC_DATETIME = 2,
61 RTC_FORCE_IRQ = 3,
62 RTC_CONTROL = 4,
63 RTC_TIME = 6
64};
65
66DECL_BITFIELD(RTCCommandData, uint32_t);
67DECL_BITS(RTCCommandData, Magic, 0, 4);
68DECL_BITS(RTCCommandData, Command, 4, 3);
69DECL_BIT(RTCCommandData, Reading, 7);
70
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
84struct GBAGBPKeyCallback {
85 struct mKeyCallback d;
86 struct GBACartridgeHardware* p;
87};
88
89struct GBAGBPSIODriver {
90 struct GBASIODriver d;
91 struct GBACartridgeHardware* p;
92};
93
94DECL_BITFIELD(GPIOPin, uint16_t);
95
96struct GBACartridgeHardware {
97 struct GBA* p;
98 uint32_t devices;
99 enum GPIODirection readWrite;
100 uint16_t* gpioBase;
101
102 uint16_t pinState;
103 uint16_t direction;
104
105 struct GBARTC rtc;
106
107 uint16_t gyroSample;
108 bool gyroEdge;
109
110 unsigned lightCounter : 12;
111 uint8_t lightSample;
112 bool lightEdge;
113
114 uint16_t tiltX;
115 uint16_t tiltY;
116 int tiltState;
117
118 unsigned gbpInputsPosted;
119 int gbpTxPosition;
120 struct mTimingEvent gbpNextEvent;
121 struct GBAGBPKeyCallback gbpCallback;
122 struct GBAGBPSIODriver gbpDriver;
123};
124
125void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
126void GBAHardwareClear(struct GBACartridgeHardware* gpio);
127
128void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
129void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
130void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
131void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
132void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
133
134void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
135void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
136uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
137
138struct GBAVideo;
139void GBAHardwarePlayerUpdate(struct GBA* gba);
140bool GBAHardwarePlayerCheckScreen(const struct GBAVideo* video);
141
142void GBARTCGenericSourceInit(struct GBARTCGenericSource* rtc, struct GBA* gba);
143
144struct GBASerializedState;
145void GBAHardwareSerialize(const struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
146void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, const struct GBASerializedState* state);
147
148CXX_GUARD_END
149
150#endif