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
11#include "macros.h"
12
13#include <time.h>
14
15#define IS_GPIO_REGISTER(reg) ((reg) == GPIO_REG_DATA || (reg) == GPIO_REG_DIRECTION || (reg) == GPIO_REG_CONTROL)
16
17struct GBARotationSource {
18 void (*sample)(struct GBARotationSource*);
19
20 int32_t (*readTiltX)(struct GBARotationSource*);
21 int32_t (*readTiltY)(struct GBARotationSource*);
22
23 int32_t (*readGyroZ)(struct GBARotationSource*);
24};
25
26struct GBALuminanceSource {
27 void (*sample)(struct GBALuminanceSource*);
28
29 uint8_t (*readLuminance)(struct GBALuminanceSource*);
30};
31
32struct GBARTCSource {
33 void (*sample)(struct GBARTCSource*);
34
35 time_t (*unixTime)(struct GBARTCSource*);
36};
37
38enum GBAHardwareDevice {
39 HW_NO_OVERRIDE = 0x8000,
40 HW_NONE = 0,
41 HW_RTC = 1,
42 HW_RUMBLE = 2,
43 HW_LIGHT_SENSOR = 4,
44 HW_GYRO = 8,
45 HW_TILT = 16
46};
47
48enum GPIORegister {
49 GPIO_REG_DATA = 0xC4,
50 GPIO_REG_DIRECTION = 0xC6,
51 GPIO_REG_CONTROL = 0xC8
52};
53
54enum GPIODirection {
55 GPIO_WRITE_ONLY = 0,
56 GPIO_READ_WRITE = 1
57};
58
59DECL_BITFIELD(RTCControl, uint32_t);
60DECL_BIT(RTCControl, MinIRQ, 3);
61DECL_BIT(RTCControl, Hour24, 6);
62DECL_BIT(RTCControl, Poweroff, 7);
63
64enum RTCCommand {
65 RTC_RESET = 0,
66 RTC_DATETIME = 2,
67 RTC_FORCE_IRQ = 3,
68 RTC_CONTROL = 4,
69 RTC_TIME = 6
70};
71
72DECL_BITFIELD(RTCCommandData, uint32_t);
73DECL_BITS(RTCCommandData, Magic, 0, 4);
74DECL_BITS(RTCCommandData, Command, 4, 3);
75DECL_BIT(RTCCommandData, Reading, 7);
76
77#pragma pack(push, 1)
78struct GBARTC {
79 int32_t bytesRemaining;
80 int32_t transferStep;
81 int32_t bitsRead;
82 int32_t bits;
83 int32_t commandActive;
84 RTCCommandData command;
85 RTCControl control;
86 uint8_t time[7];
87};
88#pragma pack(pop)
89
90struct GBARumble {
91 void (*setRumble)(struct GBARumble*, int enable);
92};
93
94DECL_BITFIELD(GPIOPin, uint16_t);
95
96struct GBACartridgeHardware {
97 struct GBA* p;
98 int 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
119void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
120void GBAHardwareClear(struct GBACartridgeHardware* gpio);
121
122void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
123void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
124void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
125void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
126void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
127
128void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
129void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
130uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
131
132struct GBASerializedState;
133void GBAHardwareSerialize(const struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
134void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, const struct GBASerializedState* state);
135
136#endif