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 GBACartridgeGPIO {
67 int gpioDevices;
68 enum GPIODirection readWrite;
69 uint16_t* gpioBase;
70
71 union {
72 struct {
73 unsigned p0 : 1;
74 unsigned p1 : 1;
75 unsigned p2 : 1;
76 unsigned p3 : 1;
77 };
78 uint16_t pinState;
79 };
80
81 union {
82 struct {
83 unsigned dir0 : 1;
84 unsigned dir1 : 1;
85 unsigned dir2 : 1;
86 unsigned dir3 : 1;
87 };
88 uint16_t direction;
89 };
90
91 struct GBARTC rtc;
92};
93
94void GBAGPIOInit(struct GBACartridgeGPIO* gpio, uint16_t* gpioBase);
95void GBAGPIOWrite(struct GBACartridgeGPIO* gpio, uint32_t address, uint16_t value);
96
97void GBAGPIOInitRTC(struct GBACartridgeGPIO* gpio);
98
99#endif