all repos — mgba @ 2fde9738bef25e0cd097dba87a82096040ab2c6c

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