all repos — mgba @ 44175d9381936c0445f30a611e034a9c23103649

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 EREADER_DOTCODE_STRIDE 1200
 20#define EREADER_DOTCODE_SIZE (EREADER_DOTCODE_STRIDE * 40 + 200)
 21
 22#define IS_GPIO_REGISTER(reg) ((reg) == GPIO_REG_DATA || (reg) == GPIO_REG_DIRECTION || (reg) == GPIO_REG_CONTROL)
 23
 24struct GBARTCGenericSource {
 25	struct mRTCSource d;
 26	struct GBA* p;
 27	enum mRTCGenericType override;
 28	int64_t value;
 29};
 30
 31enum GBAHardwareDevice {
 32	HW_NO_OVERRIDE = 0x8000,
 33	HW_NONE = 0,
 34	HW_RTC = 1,
 35	HW_RUMBLE = 2,
 36	HW_LIGHT_SENSOR = 4,
 37	HW_GYRO = 8,
 38	HW_TILT = 16,
 39	HW_GB_PLAYER = 32,
 40	HW_GB_PLAYER_DETECTION = 64,
 41	HW_EREADER = 128
 42};
 43
 44enum GPIORegister {
 45	GPIO_REG_DATA = 0xC4,
 46	GPIO_REG_DIRECTION = 0xC6,
 47	GPIO_REG_CONTROL = 0xC8
 48};
 49
 50enum GPIODirection {
 51	GPIO_WRITE_ONLY = 0,
 52	GPIO_READ_WRITE = 1
 53};
 54
 55DECL_BITFIELD(RTCControl, uint32_t);
 56DECL_BIT(RTCControl, MinIRQ, 3);
 57DECL_BIT(RTCControl, Hour24, 6);
 58DECL_BIT(RTCControl, Poweroff, 7);
 59
 60enum RTCCommand {
 61	RTC_RESET = 0,
 62	RTC_DATETIME = 2,
 63	RTC_FORCE_IRQ = 3,
 64	RTC_CONTROL = 4,
 65	RTC_TIME = 6
 66};
 67
 68DECL_BITFIELD(RTCCommandData, uint32_t);
 69DECL_BITS(RTCCommandData, Magic, 0, 4);
 70DECL_BITS(RTCCommandData, Command, 4, 3);
 71DECL_BIT(RTCCommandData, Reading, 7);
 72
 73#ifndef PYCPARSE
 74#pragma pack(push, 1)
 75struct GBARTC {
 76	int32_t bytesRemaining;
 77	int32_t transferStep;
 78	int32_t bitsRead;
 79	int32_t bits;
 80	int32_t commandActive;
 81	RTCCommandData command;
 82	RTCControl control;
 83	uint8_t time[7];
 84};
 85#pragma pack(pop)
 86#else
 87struct GBATRC;
 88#endif
 89
 90struct GBAGBPKeyCallback {
 91	struct mKeyCallback d;
 92	struct GBACartridgeHardware* p;
 93};
 94
 95struct GBAGBPSIODriver {
 96	struct GBASIODriver d;
 97	struct GBACartridgeHardware* p;
 98};
 99
100DECL_BITFIELD(GPIOPin, uint16_t);
101
102DECL_BITFIELD(EReaderControl0, uint8_t);
103DECL_BIT(EReaderControl0, Data, 0);
104DECL_BIT(EReaderControl0, Clock, 1);
105DECL_BIT(EReaderControl0, Direction, 2);
106DECL_BIT(EReaderControl0, LedEnable, 3);
107DECL_BIT(EReaderControl0, Scan, 4);
108DECL_BIT(EReaderControl0, Phi, 5);
109DECL_BIT(EReaderControl0, PowerEnable, 6);
110DECL_BITFIELD(EReaderControl1, uint8_t);
111DECL_BIT(EReaderControl1, Scanline, 1);
112DECL_BIT(EReaderControl1, Unk1, 4);
113DECL_BIT(EReaderControl1, Voltage, 5);
114
115enum EReaderStateMachine {
116	EREADER_SERIAL_INACTIVE = 0,
117	EREADER_SERIAL_STARTING,
118	EREADER_SERIAL_BIT_0,
119	EREADER_SERIAL_BIT_1,
120	EREADER_SERIAL_BIT_2,
121	EREADER_SERIAL_BIT_3,
122	EREADER_SERIAL_BIT_4,
123	EREADER_SERIAL_BIT_5,
124	EREADER_SERIAL_BIT_6,
125	EREADER_SERIAL_BIT_7,
126	EREADER_SERIAL_END_BIT,
127};
128
129enum EReaderCommand {
130	EREADER_COMMAND_IDLE = 0, // TODO: Verify on hardware
131	EREADER_COMMAND_WRITE_DATA = 1,
132	EREADER_COMMAND_SET_INDEX = 0x22,
133	EREADER_COMMAND_READ_DATA = 0x23,
134};
135
136struct GBACartridgeHardware {
137	struct GBA* p;
138	uint32_t devices;
139	enum GPIODirection readWrite;
140	uint16_t* gpioBase;
141
142	uint16_t pinState;
143	uint16_t direction;
144
145	struct GBARTC rtc;
146
147	uint16_t gyroSample;
148	bool gyroEdge;
149
150	unsigned lightCounter : 12;
151	uint8_t lightSample;
152	bool lightEdge;
153
154	uint16_t tiltX;
155	uint16_t tiltY;
156	int tiltState;
157
158	unsigned gbpInputsPosted;
159	int gbpTxPosition;
160	struct mTimingEvent gbpNextEvent;
161	struct GBAGBPKeyCallback gbpCallback;
162	struct GBAGBPSIODriver gbpDriver;
163
164	uint16_t eReaderData[44];
165	uint8_t eReaderSerial[92];
166	uint16_t eReaderRegisterUnk;
167	uint16_t eReaderRegisterReset;
168	EReaderControl0 eReaderRegisterControl0;
169	EReaderControl1 eReaderRegisterControl1;
170	uint16_t eReaderRegisterLed;
171
172	// TODO: Serialize these
173	enum EReaderStateMachine eReaderState;
174	enum EReaderCommand eReaderCommand;
175	uint8_t eReaderActiveRegister;
176	uint8_t eReaderByte;
177	int eReaderX;
178	int eReaderY;
179	uint8_t* eReaderDots;
180};
181
182void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
183void GBAHardwareClear(struct GBACartridgeHardware* gpio);
184
185void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
186void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
187void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
188void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
189void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
190
191void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
192void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
193uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
194
195struct GBAVideo;
196void GBAHardwarePlayerUpdate(struct GBA* gba);
197bool GBAHardwarePlayerCheckScreen(const struct GBAVideo* video);
198
199void GBAHardwareInitEReader(struct GBACartridgeHardware* hw);
200void GBAHardwareEReaderWrite(struct GBACartridgeHardware* hw, uint32_t address, uint16_t value);
201void GBAHardwareEReaderWriteFlash(struct GBACartridgeHardware* hw, uint32_t address, uint8_t value);
202uint16_t GBAHardwareEReaderRead(struct GBACartridgeHardware* hw, uint32_t address);
203uint8_t GBAHardwareEReaderReadFlash(struct GBACartridgeHardware* hw, uint32_t address);
204void GBAHardwareEReaderScan(struct GBACartridgeHardware* hw, const void* data, size_t size);
205
206void GBARTCGenericSourceInit(struct GBARTCGenericSource* rtc, struct GBA* gba);
207
208struct GBASerializedState;
209void GBAHardwareSerialize(const struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
210void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, const struct GBASerializedState* state);
211
212CXX_GUARD_END
213
214#endif