mgba/src/gba/hardware.h

153 lines
3.5 KiB
C
Raw Normal View History

/* Copyright (c) 2013-2015 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef GBA_HARDWARE_H
#define GBA_HARDWARE_H
2013-10-21 01:08:18 +00:00
2014-10-12 01:18:47 +00:00
#include "util/common.h"
2013-10-21 01:08:18 +00:00
#include "macros.h"
#include <time.h>
2013-10-21 01:08:18 +00:00
#define IS_GPIO_REGISTER(reg) ((reg) == GPIO_REG_DATA || (reg) == GPIO_REG_DIRECTION || (reg) == GPIO_REG_CONTROL)
struct GBARotationSource {
void (*sample)(struct GBARotationSource*);
int32_t (*readTiltX)(struct GBARotationSource*);
int32_t (*readTiltY)(struct GBARotationSource*);
int32_t (*readGyroZ)(struct GBARotationSource*);
};
struct GBALuminanceSource {
void (*sample)(struct GBALuminanceSource*);
uint8_t (*readLuminance)(struct GBALuminanceSource*);
};
struct GBARTCSource {
void (*sample)(struct GBARTCSource*);
time_t (*unixTime)(struct GBARTCSource*);
};
struct GBARTCGenericSource {
struct GBARTCSource d;
struct GBA* p;
enum {
RTC_NO_OVERRIDE,
RTC_FIXED,
RTC_FAKE_EPOCH
} override;
int64_t value;
};
enum GBAHardwareDevice {
HW_NO_OVERRIDE = 0x8000,
HW_NONE = 0,
HW_RTC = 1,
HW_RUMBLE = 2,
HW_LIGHT_SENSOR = 4,
HW_GYRO = 8,
HW_TILT = 16
2013-10-21 01:08:18 +00:00
};
enum GPIORegister {
GPIO_REG_DATA = 0xC4,
GPIO_REG_DIRECTION = 0xC6,
GPIO_REG_CONTROL = 0xC8
};
enum GPIODirection {
GPIO_WRITE_ONLY = 0,
GPIO_READ_WRITE = 1
};
2015-03-21 08:05:07 +00:00
DECL_BITFIELD(RTCControl, uint32_t);
DECL_BIT(RTCControl, MinIRQ, 3);
DECL_BIT(RTCControl, Hour24, 6);
DECL_BIT(RTCControl, Poweroff, 7);
2013-10-21 04:39:47 +00:00
enum RTCCommand {
RTC_RESET = 0,
RTC_DATETIME = 2,
RTC_FORCE_IRQ = 3,
RTC_CONTROL = 4,
RTC_TIME = 6
};
2015-03-21 08:05:07 +00:00
DECL_BITFIELD(RTCCommandData, uint32_t);
DECL_BITS(RTCCommandData, Magic, 0, 4);
DECL_BITS(RTCCommandData, Command, 4, 3);
DECL_BIT(RTCCommandData, Reading, 7);
2013-10-21 04:39:47 +00:00
2015-06-08 08:25:36 +00:00
#pragma pack(push, 1)
2013-10-21 01:08:18 +00:00
struct GBARTC {
2015-06-08 08:25:36 +00:00
int32_t bytesRemaining;
int32_t transferStep;
int32_t bitsRead;
int32_t bits;
int32_t commandActive;
RTCCommandData command;
RTCControl control;
2013-10-21 04:39:47 +00:00
uint8_t time[7];
2015-06-08 08:25:36 +00:00
};
2015-06-07 04:08:13 +00:00
#pragma pack(pop)
2013-10-21 01:08:18 +00:00
2013-10-22 04:50:29 +00:00
struct GBARumble {
void (*setRumble)(struct GBARumble*, int enable);
};
DECL_BITFIELD(GPIOPin, uint16_t);
struct GBACartridgeHardware {
2013-10-21 09:54:52 +00:00
struct GBA* p;
int devices;
2013-10-21 04:39:47 +00:00
enum GPIODirection readWrite;
2013-10-21 01:08:18 +00:00
uint16_t* gpioBase;
uint16_t pinState;
uint16_t direction;
2013-10-21 01:08:18 +00:00
struct GBARTC rtc;
2013-10-21 09:54:52 +00:00
uint16_t gyroSample;
2014-07-20 22:53:15 +00:00
bool gyroEdge;
2014-12-25 10:53:43 +00:00
unsigned lightCounter : 12;
uint8_t lightSample;
bool lightEdge;
2014-12-31 22:43:02 +00:00
uint16_t tiltX;
uint16_t tiltY;
int tiltState;
2013-10-21 01:08:18 +00:00
};
void GBAHardwareInit(struct GBACartridgeHardware* gpio, uint16_t* gpioBase);
void GBAHardwareClear(struct GBACartridgeHardware* gpio);
2013-10-21 01:08:18 +00:00
void GBAHardwareInitRTC(struct GBACartridgeHardware* gpio);
void GBAHardwareInitGyro(struct GBACartridgeHardware* gpio);
void GBAHardwareInitRumble(struct GBACartridgeHardware* gpio);
void GBAHardwareInitLight(struct GBACartridgeHardware* gpio);
void GBAHardwareInitTilt(struct GBACartridgeHardware* gpio);
void GBAHardwareGPIOWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint16_t value);
void GBAHardwareTiltWrite(struct GBACartridgeHardware* gpio, uint32_t address, uint8_t value);
uint8_t GBAHardwareTiltRead(struct GBACartridgeHardware* gpio, uint32_t address);
2014-12-31 22:43:02 +00:00
struct GBAVideo;
bool GBAHardwarePlayerCheckScreen(const struct GBAVideo* video);
void GBARTCGenericSourceInit(struct GBARTCGenericSource* rtc, struct GBA* gba);
2014-07-20 22:53:15 +00:00
struct GBASerializedState;
void GBAHardwareSerialize(const struct GBACartridgeHardware* gpio, struct GBASerializedState* state);
void GBAHardwareDeserialize(struct GBACartridgeHardware* gpio, const struct GBASerializedState* state);
2014-07-20 22:53:15 +00:00
2013-10-21 01:08:18 +00:00
#endif