all repos — mgba @ 74fc29fc68e7ae84f0c27130d1d98299434ce6f2

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#include "gba/interface.h"
 11
 12#include "macros.h"
 13
 14#include <time.h>
 15
 16#define IS_GPIO_REGISTER(reg) ((reg) == GPIO_REG_DATA || (reg) == GPIO_REG_DIRECTION || (reg) == GPIO_REG_CONTROL)
 17
 18struct GBARTCGenericSource {
 19	struct mRTCSource d;
 20	struct GBA* p;
 21	enum mRTCGenericType override;
 22	int64_t value;
 23};
 24
 25enum GBAHardwareDevice {
 26	HW_NO_OVERRIDE = 0x8000,
 27	HW_NONE = 0,
 28	HW_RTC = 1,
 29	HW_RUMBLE = 2,
 30	HW_LIGHT_SENSOR = 4,
 31	HW_GYRO = 8,
 32	HW_TILT = 16,
 33	HW_GB_PLAYER = 32,
 34	HW_GB_PLAYER_DETECTION = 64
 35};
 36
 37enum GPIORegister {
 38	GPIO_REG_DATA = 0xC4,
 39	GPIO_REG_DIRECTION = 0xC6,
 40	GPIO_REG_CONTROL = 0xC8
 41};
 42
 43enum GPIODirection {
 44	GPIO_WRITE_ONLY = 0,
 45	GPIO_READ_WRITE = 1
 46};
 47
 48DECL_BITFIELD(RTCControl, uint32_t);
 49DECL_BIT(RTCControl, MinIRQ, 3);
 50DECL_BIT(RTCControl, Hour24, 6);
 51DECL_BIT(RTCControl, Poweroff, 7);
 52
 53enum RTCCommand {
 54	RTC_RESET = 0,
 55	RTC_DATETIME = 2,
 56	RTC_FORCE_IRQ = 3,
 57	RTC_CONTROL = 4,
 58	RTC_TIME = 6
 59};
 60
 61DECL_BITFIELD(RTCCommandData, uint32_t);
 62DECL_BITS(RTCCommandData, Magic, 0, 4);
 63DECL_BITS(RTCCommandData, Command, 4, 3);
 64DECL_BIT(RTCCommandData, Reading, 7);
 65
 66#pragma pack(push, 1)
 67struct GBARTC {
 68	int32_t bytesRemaining;
 69	int32_t transferStep;
 70	int32_t bitsRead;
 71	int32_t bits;
 72	int32_t commandActive;
 73	RTCCommandData command;
 74	RTCControl control;
 75	uint8_t time[7];
 76};
 77#pragma pack(pop)
 78
 79struct GBAGBPKeyCallback {
 80	struct mKeyCallback d;
 81	struct GBACartridgeHardware* p;
 82};
 83
 84struct GBAGBPSIODriver {
 85	struct GBASIODriver d;
 86	struct GBACartridgeHardware* p;
 87};
 88
 89DECL_BITFIELD(GPIOPin, uint16_t);
 90
 91struct GBACartridgeHardware {
 92	struct GBA* p;
 93	uint32_t devices;
 94	enum GPIODirection readWrite;
 95	uint16_t* gpioBase;
 96
 97	uint16_t pinState;
 98	uint16_t direction;
 99
100	struct GBARTC rtc;
101
102	uint16_t gyroSample;
103	bool gyroEdge;
104
105	unsigned lightCounter : 12;
106	uint8_t lightSample;
107	bool lightEdge;
108
109	uint16_t tiltX;
110	uint16_t tiltY;
111	int tiltState;
112
113	unsigned gbpInputsPosted;
114	int gbpTxPosition;
115	int32_t gbpNextEvent;
116	struct GBAGBPKeyCallback gbpCallback;
117	struct GBAGBPSIODriver gbpDriver;
118};
119
120void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
121void GBAHardwareClear(struct GBACartridgeHardware* gpio);
122
123void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
124void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
125void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
126void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
127void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
128
129void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
130void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
131uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
132
133struct GBAVideo;
134void GBAHardwarePlayerUpdate(struct GBA* gba);
135bool GBAHardwarePlayerCheckScreen(const struct GBAVideo* video);
136
137void GBARTCGenericSourceInit(struct GBARTCGenericSource* rtc, struct GBA* gba);
138
139struct GBASerializedState;
140void GBAHardwareSerialize(const struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
141void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, const struct GBASerializedState* state);
142
143#endif