all repos — mgba @ cf55fcbb52f91059f59b59d5e8419fffdee679a2

mGBA Game Boy Advance Emulator

src/gba/gba.h (view raw)

  1#ifndef GBA_H
  2#define GBA_H
  3
  4#include "util/common.h"
  5
  6#include "arm.h"
  7#include "debugger/debugger.h"
  8
  9#include "gba-memory.h"
 10#include "gba-video.h"
 11#include "gba-audio.h"
 12#include "gba-sio.h"
 13
 14extern const uint32_t GBA_ARM7TDMI_FREQUENCY;
 15
 16enum GBAIRQ {
 17	IRQ_VBLANK = 0x0,
 18	IRQ_HBLANK = 0x1,
 19	IRQ_VCOUNTER = 0x2,
 20	IRQ_TIMER0 = 0x3,
 21	IRQ_TIMER1 = 0x4,
 22	IRQ_TIMER2 = 0x5,
 23	IRQ_TIMER3 = 0x6,
 24	IRQ_SIO = 0x7,
 25	IRQ_DMA0 = 0x8,
 26	IRQ_DMA1 = 0x9,
 27	IRQ_DMA2 = 0xA,
 28	IRQ_DMA3 = 0xB,
 29	IRQ_KEYPAD = 0xC,
 30	IRQ_GAMEPAK = 0xD
 31};
 32
 33enum GBAError {
 34	GBA_NO_ERROR = 0,
 35	GBA_OUT_OF_MEMORY = -1
 36};
 37
 38enum GBALogLevel {
 39	GBA_LOG_FATAL = 0x01,
 40	GBA_LOG_ERROR = 0x02,
 41	GBA_LOG_WARN = 0x04,
 42	GBA_LOG_INFO = 0x08,
 43	GBA_LOG_DEBUG = 0x10,
 44	GBA_LOG_STUB = 0x20,
 45
 46	GBA_LOG_GAME_ERROR = 0x100,
 47	GBA_LOG_SWI = 0x200,
 48
 49	GBA_LOG_ALL = 0x33F,
 50
 51#ifdef NDEBUG
 52	GBA_LOG_DANGER = GBA_LOG_ERROR
 53#else
 54	GBA_LOG_DANGER = GBA_LOG_FATAL
 55#endif
 56};
 57
 58enum GBAKey {
 59	GBA_KEY_A = 0,
 60	GBA_KEY_B = 1,
 61	GBA_KEY_SELECT = 2,
 62	GBA_KEY_START = 3,
 63	GBA_KEY_RIGHT = 4,
 64	GBA_KEY_LEFT = 5,
 65	GBA_KEY_UP = 6,
 66	GBA_KEY_DOWN = 7,
 67	GBA_KEY_R = 8,
 68	GBA_KEY_L = 9,
 69	GBA_KEY_MAX,
 70	GBA_KEY_NONE = -1
 71};
 72
 73struct GBA;
 74struct GBARotationSource;
 75struct Patch;
 76struct VFile;
 77
 78struct GBATimer {
 79	uint16_t reload;
 80	uint16_t oldReload;
 81	int32_t lastEvent;
 82	int32_t nextEvent;
 83	int32_t overflowInterval;
 84	unsigned prescaleBits : 4;
 85	unsigned countUp : 1;
 86	unsigned doIrq : 1;
 87	unsigned enable : 1;
 88};
 89
 90struct GBA {
 91	struct ARMComponent d;
 92
 93	struct ARMCore* cpu;
 94	struct GBAMemory memory;
 95	struct GBAVideo video;
 96	struct GBAAudio audio;
 97	struct GBASIO sio;
 98
 99	struct GBASync* sync;
100
101	struct ARMDebugger* debugger;
102
103	uint32_t bus;
104
105	int timersEnabled;
106	struct GBATimer timers[4];
107
108	int springIRQ;
109	uint32_t biosChecksum;
110	int* keySource;
111	uint32_t busyLoop;
112	struct GBARotationSource* rotationSource;
113	struct GBARumble* rumble;
114	struct GBARRContext* rr;
115	void* pristineRom;
116	size_t pristineRomSize;
117	uint32_t romCrc32;
118	struct VFile* romVf;
119	struct VFile* biosVf;
120
121	const char* activeFile;
122
123	int logLevel;
124};
125
126struct GBACartridge {
127	uint32_t entry;
128	uint8_t logo[156];
129	char title[12];
130	uint32_t id;
131	uint16_t maker;
132	uint8_t type;
133	uint8_t unit;
134	uint8_t device;
135	uint8_t reserved[7];
136	uint8_t version;
137	uint8_t checksum;
138	// And ROM data...
139};
140
141void GBACreate(struct GBA* gba);
142void GBADestroy(struct GBA* gba);
143
144void GBAReset(struct ARMCore* cpu);
145
146void GBATimerUpdateRegister(struct GBA* gba, int timer);
147void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
148void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
149
150void GBAWriteIE(struct GBA* gba, uint16_t value);
151void GBAWriteIME(struct GBA* gba, uint16_t value);
152void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
153void GBATestIRQ(struct ARMCore* cpu);
154void GBAHalt(struct GBA* gba);
155
156void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
157void GBADetachDebugger(struct GBA* gba);
158
159void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname);
160void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
161void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
162
163bool GBAIsROM(struct VFile* vf);
164void GBAGetGameCode(struct GBA* gba, char* out);
165void GBAGetGameTitle(struct GBA* gba, char* out);
166
167__attribute__((format (printf, 3, 4)))
168void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
169
170__attribute__((format (printf, 3, 4)))
171void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
172
173#endif