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