all repos — mgba @ 0de46a78679adf820eba08c3f72e64202540b692

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2014 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_GPIO_H
  7#define GBA_GPIO_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
 13enum GPIODevice {
 14	GPIO_NO_OVERRIDE = 0x8000,
 15	GPIO_NONE = 0,
 16	GPIO_RTC = 1,
 17	GPIO_RUMBLE = 2,
 18	GPIO_LIGHT_SENSOR = 4,
 19	GPIO_GYRO = 8,
 20	GPIO_TILT = 16
 21};
 22
 23enum GPIORegister {
 24	GPIO_REG_DATA = 0xC4,
 25	GPIO_REG_DIRECTION = 0xC6,
 26	GPIO_REG_CONTROL = 0xC8
 27};
 28
 29enum GPIODirection {
 30	GPIO_WRITE_ONLY = 0,
 31	GPIO_READ_WRITE = 1
 32};
 33
 34union RTCControl {
 35	struct {
 36		unsigned : 3;
 37		unsigned minIRQ : 1;
 38		unsigned : 2;
 39		unsigned hour24 : 1;
 40		unsigned poweroff : 1;
 41	};
 42	uint8_t packed;
 43};
 44
 45enum RTCCommand {
 46	RTC_RESET = 0,
 47	RTC_DATETIME = 2,
 48	RTC_FORCE_IRQ = 3,
 49	RTC_CONTROL = 4,
 50	RTC_TIME = 6
 51};
 52
 53union RTCCommandData {
 54	struct {
 55		unsigned magic : 4;
 56		enum RTCCommand command : 3;
 57		unsigned reading : 1;
 58	};
 59	uint8_t packed;
 60};
 61
 62struct GBARTC {
 63	int bytesRemaining;
 64	int transferStep;
 65	int bitsRead;
 66	int bits;
 67	int commandActive;
 68	union RTCCommandData command;
 69	union RTCControl control;
 70	uint8_t time[7];
 71} __attribute__((packed));
 72
 73struct GBARumble {
 74	void (*setRumble)(struct GBARumble*, int enable);
 75};
 76
 77struct GBACartridgeGPIO {
 78	struct GBA* p;
 79	int gpioDevices;
 80	enum GPIODirection readWrite;
 81	uint16_t* gpioBase;
 82
 83	union {
 84		struct {
 85			unsigned p0 : 1;
 86			unsigned p1 : 1;
 87			unsigned p2 : 1;
 88			unsigned p3 : 1;
 89		};
 90		uint16_t pinState;
 91	};
 92
 93	union {
 94		struct {
 95			unsigned dir0 : 1;
 96			unsigned dir1 : 1;
 97			unsigned dir2 : 1;
 98			unsigned dir3 : 1;			
 99		};
100		uint16_t direction;
101	};
102
103	struct GBARTC rtc;
104
105	uint16_t gyroSample;
106	bool gyroEdge;
107
108	unsigned lightCounter : 12;
109	uint8_t lightSample;
110	bool lightEdge;
111
112	uint16_t tiltX;
113	uint16_t tiltY;
114	int tiltState;
115};
116
117void GBAGPIOInit(struct GBACartridgeGPIO* gpio, uint16_t* gpioBase);
118void GBAGPIOClear(struct GBACartridgeGPIO* gpio);
119void GBAGPIOWrite(struct GBACartridgeGPIO* gpio, uint32_t address, uint16_t value);
120
121void GBAGPIOInitRTC(struct GBACartridgeGPIO* gpio);
122void GBAGPIOInitGyro(struct GBACartridgeGPIO* gpio);
123void GBAGPIOInitRumble(struct GBACartridgeGPIO* gpio);
124void GBAGPIOInitLightSensor(struct GBACartridgeGPIO* gpio);
125void GBAGPIOInitTilt(struct GBACartridgeGPIO* gpio);
126
127void GBAGPIOTiltWrite(struct GBACartridgeGPIO* gpio, uint32_t address, uint8_t value);
128uint8_t GBAGPIOTiltRead(struct GBACartridgeGPIO* gpio, uint32_t address);
129
130struct GBASerializedState;
131void GBAGPIOSerialize(struct GBACartridgeGPIO* gpio, struct GBASerializedState* state);
132void GBAGPIODeserialize(struct GBACartridgeGPIO* gpio, struct GBASerializedState* state);
133
134#endif