all repos — mgba @ f80406efb02fc1470c532e01611d946e5852632c

mGBA Game Boy Advance Emulator

src/gba/hardware.h (view raw)

  1/* Copyright (c) 2013-2015 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_HARDWARE_H
  7#define GBA_HARDWARE_H
  8
  9#include "util/common.h"
 10
 11#include "macros.h"
 12
 13#define IS_GPIO_REGISTER(reg) ((reg) == GPIO_REG_DATA || (reg) == GPIO_REG_DIRECTION || (reg) == GPIO_REG_CONTROL)
 14
 15struct GBARotationSource {
 16	void (*sample)(struct GBARotationSource*);
 17
 18	int32_t (*readTiltX)(struct GBARotationSource*);
 19	int32_t (*readTiltY)(struct GBARotationSource*);
 20
 21	int32_t (*readGyroZ)(struct GBARotationSource*);
 22};
 23
 24struct GBALuminanceSource {
 25	void (*sample)(struct GBALuminanceSource*);
 26
 27	uint8_t (*readLuminance)(struct GBALuminanceSource*);
 28};
 29
 30struct GBARTCSource {
 31	void (*sample)(struct GBARTCSource*);
 32
 33	time_t (*unixTime)(struct GBARTCSource*);
 34};
 35
 36enum GBAHardwareDevice {
 37	HW_NO_OVERRIDE = 0x8000,
 38	HW_NONE = 0,
 39	HW_RTC = 1,
 40	HW_RUMBLE = 2,
 41	HW_LIGHT_SENSOR = 4,
 42	HW_GYRO = 8,
 43	HW_TILT = 16
 44};
 45
 46enum GPIORegister {
 47	GPIO_REG_DATA = 0xC4,
 48	GPIO_REG_DIRECTION = 0xC6,
 49	GPIO_REG_CONTROL = 0xC8
 50};
 51
 52enum GPIODirection {
 53	GPIO_WRITE_ONLY = 0,
 54	GPIO_READ_WRITE = 1
 55};
 56
 57DECL_BITFIELD(RTCControl, uint8_t);
 58DECL_BIT(RTCControl, MinIRQ, 3);
 59DECL_BIT(RTCControl, Hour24, 6);
 60DECL_BIT(RTCControl, Poweroff, 7);
 61
 62enum RTCCommand {
 63	RTC_RESET = 0,
 64	RTC_DATETIME = 2,
 65	RTC_FORCE_IRQ = 3,
 66	RTC_CONTROL = 4,
 67	RTC_TIME = 6
 68};
 69
 70DECL_BITFIELD(RTCCommandData, uint8_t);
 71DECL_BITS(RTCCommandData, Magic, 0, 4);
 72DECL_BITS(RTCCommandData, Command, 4, 3);
 73DECL_BIT(RTCCommandData, Reading, 7);
 74
 75struct GBARTC {
 76	int bytesRemaining;
 77	int transferStep;
 78	int bitsRead;
 79	int bits;
 80	int commandActive;
 81	RTCCommandData command;
 82	RTCControl control;
 83	uint8_t time[7];
 84} __attribute__((packed));
 85
 86struct GBARumble {
 87	void (*setRumble)(struct GBARumble*, int enable);
 88};
 89
 90struct GBACartridgeHardware {
 91	struct GBA* p;
 92	int devices;
 93	enum GPIODirection readWrite;
 94	uint16_t* gpioBase;
 95
 96	union {
 97		struct {
 98			unsigned p0 : 1;
 99			unsigned p1 : 1;
100			unsigned p2 : 1;
101			unsigned p3 : 1;
102		};
103		uint16_t pinState;
104	};
105
106	union {
107		struct {
108			unsigned dir0 : 1;
109			unsigned dir1 : 1;
110			unsigned dir2 : 1;
111			unsigned dir3 : 1;			
112		};
113		uint16_t direction;
114	};
115
116	struct GBARTC rtc;
117
118	uint16_t gyroSample;
119	bool gyroEdge;
120
121	unsigned lightCounter : 12;
122	uint8_t lightSample;
123	bool lightEdge;
124
125	uint16_t tiltX;
126	uint16_t tiltY;
127	int tiltState;
128};
129
130void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
131void GBAHardwareClear(struct GBACartridgeHardware* gpio);
132
133void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
134void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
135void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
136void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
137void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
138
139void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
140void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
141uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
142
143struct GBASerializedState;
144void GBAHardwareSerialize(const struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
145void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, const struct GBASerializedState* state);
146
147#endif