all repos — mgba @ 4787eb29c5d953bac921281edae17920e149cb62

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#pragma pack(push, 1)
 70struct GBARTC {
 71	int32_t bytesRemaining;
 72	int32_t transferStep;
 73	int32_t bitsRead;
 74	int32_t bits;
 75	int32_t commandActive;
 76	RTCCommandData command;
 77	RTCControl control;
 78	uint8_t time[7];
 79};
 80#pragma pack(pop)
 81
 82struct GBAGBPKeyCallback {
 83	struct mKeyCallback d;
 84	struct GBACartridgeHardware* p;
 85};
 86
 87struct GBAGBPSIODriver {
 88	struct GBASIODriver d;
 89	struct GBACartridgeHardware* p;
 90};
 91
 92DECL_BITFIELD(GPIOPin, uint16_t);
 93
 94struct GBACartridgeHardware {
 95	struct GBA* p;
 96	uint32_t devices;
 97	enum GPIODirection readWrite;
 98	uint16_t* gpioBase;
 99
100	uint16_t pinState;
101	uint16_t direction;
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	unsigned gbpInputsPosted;
117	int gbpTxPosition;
118	struct mTimingEvent gbpNextEvent;
119	struct GBAGBPKeyCallback gbpCallback;
120	struct GBAGBPSIODriver gbpDriver;
121};
122
123void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
124void GBAHardwareClear(struct GBACartridgeHardware* gpio);
125
126void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
127void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
128void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
129void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
130void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
131
132void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
133void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
134uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
135
136struct GBAVideo;
137void GBAHardwarePlayerUpdate(struct GBA* gba);
138bool GBAHardwarePlayerCheckScreen(const struct GBAVideo* video);
139
140void GBARTCGenericSourceInit(struct GBARTCGenericSource* rtc, struct GBA* gba);
141
142struct GBASerializedState;
143void GBAHardwareSerialize(const struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
144void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, const struct GBASerializedState* state);
145
146CXX_GUARD_END
147
148#endif