all repos — mgba @ ea204bbfed6c468861a62ecd36114e974966848d

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 GBARTCSource d;
 20	struct GBA* p;
 21	enum {
 22		RTC_NO_OVERRIDE,
 23		RTC_FIXED,
 24		RTC_FAKE_EPOCH
 25	} override;
 26	int64_t value;
 27};
 28
 29enum GBAHardwareDevice {
 30	HW_NO_OVERRIDE = 0x8000,
 31	HW_NONE = 0,
 32	HW_RTC = 1,
 33	HW_RUMBLE = 2,
 34	HW_LIGHT_SENSOR = 4,
 35	HW_GYRO = 8,
 36	HW_TILT = 16,
 37	HW_GB_PLAYER = 32,
 38	HW_GB_PLAYER_DETECTION = 64
 39};
 40
 41enum GPIORegister {
 42	GPIO_REG_DATA = 0xC4,
 43	GPIO_REG_DIRECTION = 0xC6,
 44	GPIO_REG_CONTROL = 0xC8
 45};
 46
 47enum GPIODirection {
 48	GPIO_WRITE_ONLY = 0,
 49	GPIO_READ_WRITE = 1
 50};
 51
 52DECL_BITFIELD(RTCControl, uint32_t);
 53DECL_BIT(RTCControl, MinIRQ, 3);
 54DECL_BIT(RTCControl, Hour24, 6);
 55DECL_BIT(RTCControl, Poweroff, 7);
 56
 57enum RTCCommand {
 58	RTC_RESET = 0,
 59	RTC_DATETIME = 2,
 60	RTC_FORCE_IRQ = 3,
 61	RTC_CONTROL = 4,
 62	RTC_TIME = 6
 63};
 64
 65DECL_BITFIELD(RTCCommandData, uint32_t);
 66DECL_BITS(RTCCommandData, Magic, 0, 4);
 67DECL_BITS(RTCCommandData, Command, 4, 3);
 68DECL_BIT(RTCCommandData, Reading, 7);
 69
 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
 83struct GBARumble {
 84	void (*setRumble)(struct GBARumble*, int enable);
 85};
 86
 87struct GBAGBPKeyCallback {
 88	struct GBAKeyCallback d;
 89	struct GBACartridgeHardware* p;
 90};
 91
 92struct GBAGBPSIODriver {
 93	struct GBASIODriver d;
 94	struct GBACartridgeHardware* p;
 95};
 96
 97DECL_BITFIELD(GPIOPin, uint16_t);
 98
 99struct GBACartridgeHardware {
100	struct GBA* p;
101	uint32_t devices;
102	enum GPIODirection readWrite;
103	uint16_t* gpioBase;
104
105	uint16_t pinState;
106	uint16_t direction;
107
108	struct GBARTC rtc;
109
110	uint16_t gyroSample;
111	bool gyroEdge;
112
113	unsigned lightCounter : 12;
114	uint8_t lightSample;
115	bool lightEdge;
116
117	uint16_t tiltX;
118	uint16_t tiltY;
119	int tiltState;
120
121	unsigned gbpInputsPosted;
122	int gbpTxPosition;
123	int32_t gbpNextEvent;
124	struct GBAGBPKeyCallback gbpCallback;
125	struct GBAGBPSIODriver gbpDriver;
126};
127
128void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
129void GBAHardwareClear(struct GBACartridgeHardware* gpio);
130
131void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
132void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
133void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
134void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
135void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
136
137void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
138void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
139uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
140
141struct GBAVideo;
142void GBAHardwarePlayerUpdate(struct GBA* gba);
143bool GBAHardwarePlayerCheckScreen(const struct GBAVideo* video);
144
145void GBARTCGenericSourceInit(struct GBARTCGenericSource* rtc, struct GBA* gba);
146
147struct GBASerializedState;
148void GBAHardwareSerialize(const struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
149void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, const struct GBASerializedState* state);
150
151#endif