src/gba/gba-gpio.h (view raw)
1#ifndef GBA_GPIO_H
2#define GBA_GPIO_H
3
4#include <stdint.h>
5
6#define IS_GPIO_REGISTER(reg) ((reg) == GPIO_REG_DATA || (reg) == GPIO_REG_DIRECTION || (reg) == GPIO_REG_CONTROL)
7
8enum GPIODevice {
9 GPIO_NONE = 0,
10 GPIO_RTC = 1,
11 GPIO_RUMBLE = 2,
12 GPIO_LIGHT_SENSOR = 4,
13 GPIO_GYRO = 8
14};
15
16enum GPIORegister {
17 GPIO_REG_DATA = 0xC4,
18 GPIO_REG_DIRECTION = 0xC6,
19 GPIO_REG_CONTROL = 0xC8
20};
21
22enum GPIODirection {
23 GPIO_WRITE_ONLY = 0,
24 GPIO_READ_WRITE = 1
25};
26
27union RTCControl {
28 struct {
29 unsigned : 3;
30 unsigned minIRQ : 1;
31 unsigned : 2;
32 unsigned hour24 : 1;
33 unsigned poweroff : 1;
34 };
35 uint8_t packed;
36};
37
38enum RTCCommand {
39 RTC_RESET = 0,
40 RTC_DATETIME = 2,
41 RTC_FORCE_IRQ = 3,
42 RTC_CONTROL = 4,
43 RTC_TIME = 6
44};
45
46union RTCCommandData {
47 struct {
48 unsigned magic : 4;
49 enum RTCCommand command : 3;
50 unsigned reading : 1;
51 };
52 uint8_t packed;
53};
54
55struct GBARTC {
56 int bytesRemaining;
57 int transferStep;
58 int bitsRead;
59 int bits;
60 int commandActive;
61 union RTCCommandData command;
62 union RTCControl control;
63 uint8_t time[7];
64};
65
66struct GBARumble {
67 void (*setRumble)(struct GBARumble*, int enable);
68};
69
70struct GBACartridgeGPIO {
71 struct GBA* p;
72 int gpioDevices;
73 enum GPIODirection readWrite;
74 uint16_t* gpioBase;
75
76 union {
77 struct {
78 unsigned p0 : 1;
79 unsigned p1 : 1;
80 unsigned p2 : 1;
81 unsigned p3 : 1;
82 };
83 uint16_t pinState;
84 };
85
86 union {
87 struct {
88 unsigned dir0 : 1;
89 unsigned dir1 : 1;
90 unsigned dir2 : 1;
91 unsigned dir3 : 1;
92 };
93 uint16_t direction;
94 };
95
96 struct GBARTC rtc;
97
98 uint16_t gyroSample;
99 int gyroEdge;
100};
101
102void GBAGPIOInit(struct GBACartridgeGPIO* gpio, uint16_t* gpioBase);
103void GBAGPIOWrite(struct GBACartridgeGPIO* gpio, uint32_t address, uint16_t value);
104
105void GBAGPIOInitRTC(struct GBACartridgeGPIO* gpio);
106
107void GBAGPIOInitGyro(struct GBACartridgeGPIO* gpio);
108
109void GBAGPIOInitRumble(struct GBACartridgeGPIO* gpio);
110
111#endif