all repos — mgba @ eb21dd722f3660e0f832e11225048304a15b1d07

mGBA Game Boy Advance Emulator

src/gba/gba.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_H
  7#define GBA_H
  8
  9#include "util/common.h"
 10
 11#include "arm.h"
 12#include "debugger/debugger.h"
 13
 14#include "gba/memory.h"
 15#include "gba/video.h"
 16#include "gba/audio.h"
 17#include "gba/sio.h"
 18
 19extern const uint32_t GBA_ARM7TDMI_FREQUENCY;
 20
 21enum GBAIRQ {
 22	IRQ_VBLANK = 0x0,
 23	IRQ_HBLANK = 0x1,
 24	IRQ_VCOUNTER = 0x2,
 25	IRQ_TIMER0 = 0x3,
 26	IRQ_TIMER1 = 0x4,
 27	IRQ_TIMER2 = 0x5,
 28	IRQ_TIMER3 = 0x6,
 29	IRQ_SIO = 0x7,
 30	IRQ_DMA0 = 0x8,
 31	IRQ_DMA1 = 0x9,
 32	IRQ_DMA2 = 0xA,
 33	IRQ_DMA3 = 0xB,
 34	IRQ_KEYPAD = 0xC,
 35	IRQ_GAMEPAK = 0xD
 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
 73enum GBAComponent {
 74	GBA_COMPONENT_DEBUGGER,
 75	GBA_COMPONENT_CHEAT_DEVICE,
 76	GBA_COMPONENT_MAX
 77};
 78
 79enum GBAIdleLoopOptimization {
 80	IDLE_LOOP_IGNORE = -1,
 81	IDLE_LOOP_REMOVE = 0,
 82	IDLE_LOOP_DETECT
 83};
 84
 85enum {
 86	SP_BASE_SYSTEM = 0x03007F00,
 87	SP_BASE_IRQ = 0x03007FA0,
 88	SP_BASE_SUPERVISOR = 0x03007FE0
 89};
 90
 91struct GBA;
 92struct GBARotationSource;
 93struct GBAThread;
 94struct Patch;
 95struct VFile;
 96
 97typedef void (*GBALogHandler)(struct GBAThread*, enum GBALogLevel, const char* format, va_list args);
 98
 99struct GBATimer {
100	uint16_t reload;
101	uint16_t oldReload;
102	int32_t lastEvent;
103	int32_t nextEvent;
104	int32_t overflowInterval;
105	unsigned prescaleBits : 4;
106	unsigned countUp : 1;
107	unsigned doIrq : 1;
108	unsigned enable : 1;
109};
110
111struct GBA {
112	struct ARMComponent d;
113
114	struct ARMCore* cpu;
115	struct GBAMemory memory;
116	struct GBAVideo video;
117	struct GBAAudio audio;
118	struct GBASIO sio;
119
120	struct GBASync* sync;
121
122	struct ARMDebugger* debugger;
123
124	uint32_t bus;
125	bool performingDMA;
126
127	int timersEnabled;
128	struct GBATimer timers[4];
129
130	int springIRQ;
131	uint32_t biosChecksum;
132	int* keySource;
133	struct GBARotationSource* rotationSource;
134	struct GBALuminanceSource* luminanceSource;
135	struct GBARTCSource* rtcSource;
136	struct GBARumble* rumble;
137
138	struct GBARRContext* rr;
139	void* pristineRom;
140	size_t pristineRomSize;
141	uint32_t romCrc32;
142	struct VFile* romVf;
143	struct VFile* biosVf;
144
145	const char* activeFile;
146
147	GBALogHandler logHandler;
148	enum GBALogLevel logLevel;
149
150	enum GBAIdleLoopOptimization idleOptimization;
151	uint32_t idleLoop;
152	uint32_t lastJump;
153	int idleDetectionStep;
154	int idleDetectionFailures;
155	int32_t cachedRegisters[16];
156	bool taintedRegisters[16];
157};
158
159struct GBACartridge {
160	uint32_t entry;
161	uint8_t logo[156];
162	char title[12];
163	uint32_t id;
164	uint16_t maker;
165	uint8_t type;
166	uint8_t unit;
167	uint8_t device;
168	uint8_t reserved[7];
169	uint8_t version;
170	uint8_t checksum;
171	// And ROM data...
172};
173
174void GBACreate(struct GBA* gba);
175void GBADestroy(struct GBA* gba);
176
177void GBAReset(struct ARMCore* cpu);
178void GBASkipBIOS(struct ARMCore* cpu);
179
180void GBATimerUpdateRegister(struct GBA* gba, int timer);
181void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
182void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
183
184void GBAWriteIE(struct GBA* gba, uint16_t value);
185void GBAWriteIME(struct GBA* gba, uint16_t value);
186void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
187void GBATestIRQ(struct ARMCore* cpu);
188void GBAHalt(struct GBA* gba);
189
190void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
191void GBADetachDebugger(struct GBA* gba);
192
193void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode);
194void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
195
196void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname);
197void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
198void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
199
200bool GBAIsROM(struct VFile* vf);
201bool GBAIsBIOS(struct VFile* vf);
202void GBAGetGameCode(struct GBA* gba, char* out);
203void GBAGetGameTitle(struct GBA* gba, char* out);
204
205void GBAFrameStarted(struct GBA* gba);
206void GBAFrameEnded(struct GBA* gba);
207
208__attribute__((format (printf, 3, 4)))
209void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
210
211__attribute__((format (printf, 3, 4)))
212void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
213
214#endif