all repos — mgba @ 5e30a7bfd8d5da214a1230803a23ac549c8c13ee

mGBA Game Boy Advance Emulator

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