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#define IS_GPIO_REGISTER(reg) ((reg) == GPIO_REG_DATA || (reg) == GPIO_REG_DIRECTION || (reg) == GPIO_REG_CONTROL)
12
13struct GBARotationSource {
14 void (*sample)(struct GBARotationSource*);
15
16 int32_t (*readTiltX)(struct GBARotationSource*);
17 int32_t (*readTiltY)(struct GBARotationSource*);
18
19 int32_t (*readGyroZ)(struct GBARotationSource*);
20};
21
22struct GBALuminanceSource {
23 void (*sample)(struct GBALuminanceSource*);
24
25 uint8_t (*readLuminance)(struct GBALuminanceSource*);
26};
27
28struct GBARTCSource {
29 void (*sample)(struct GBARTCSource*);
30
31 time_t (*unixTime)(struct GBARTCSource*);
32};
33
34enum GBAHardwareDevice {
35 HW_NO_OVERRIDE = 0x8000,
36 HW_NONE = 0,
37 HW_RTC = 1,
38 HW_RUMBLE = 2,
39 HW_LIGHT_SENSOR = 4,
40 HW_GYRO = 8,
41 HW_TILT = 16
42};
43
44enum GPIORegister {
45 GPIO_REG_DATA = 0xC4,
46 GPIO_REG_DIRECTION = 0xC6,
47 GPIO_REG_CONTROL = 0xC8
48};
49
50enum GPIODirection {
51 GPIO_WRITE_ONLY = 0,
52 GPIO_READ_WRITE = 1
53};
54
55union RTCControl {
56 struct {
57 unsigned : 3;
58 unsigned minIRQ : 1;
59 unsigned : 2;
60 unsigned hour24 : 1;
61 unsigned poweroff : 1;
62 };
63 uint8_t packed;
64};
65
66enum RTCCommand {
67 RTC_RESET = 0,
68 RTC_DATETIME = 2,
69 RTC_FORCE_IRQ = 3,
70 RTC_CONTROL = 4,
71 RTC_TIME = 6
72};
73
74union RTCCommandData {
75 struct {
76 unsigned magic : 4;
77 enum RTCCommand command : 3;
78 unsigned reading : 1;
79 };
80 uint8_t packed;
81};
82
83struct GBARTC {
84 int bytesRemaining;
85 int transferStep;
86 int bitsRead;
87 int bits;
88 int commandActive;
89 union RTCCommandData command;
90 union RTCControl control;
91 uint8_t time[7];
92} __attribute__((packed));
93
94struct GBARumble {
95 void (*setRumble)(struct GBARumble*, int enable);
96};
97
98struct GBACartridgeHardware {
99 struct GBA* p;
100 int devices;
101 enum GPIODirection readWrite;
102 uint16_t* gpioBase;
103
104 union {
105 struct {
106 unsigned p0 : 1;
107 unsigned p1 : 1;
108 unsigned p2 : 1;
109 unsigned p3 : 1;
110 };
111 uint16_t pinState;
112 };
113
114 union {
115 struct {
116 unsigned dir0 : 1;
117 unsigned dir1 : 1;
118 unsigned dir2 : 1;
119 unsigned dir3 : 1;
120 };
121 uint16_t direction;
122 };
123
124 struct GBARTC rtc;
125
126 uint16_t gyroSample;
127 bool gyroEdge;
128
129 unsigned lightCounter : 12;
130 uint8_t lightSample;
131 bool lightEdge;
132
133 uint16_t tiltX;
134 uint16_t tiltY;
135 int tiltState;
136};
137
138void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
139void GBAHardwareClear(struct GBACartridgeHardware* gpio);
140
141void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
142void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
143void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
144void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
145void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
146
147void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
148void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
149uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
150
151struct GBASerializedState;
152void GBAHardwareSerialize(struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
153void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
154
155#endif