all repos — mgba @ c9b7f450aa3ce437bdd3f20b001488b05d919bc5

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