all repos — mgba @ 8a6254cc73053ee75d60e99d01daa7c8c4e5d6a9

mGBA Game Boy Advance Emulator

src/gba/gba-gpio.h (view raw)

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