all repos — mgba @ f8933f23c4978cab76be1aed4efda8fb4fb1e978

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 GBAAVStream {
100	void (*postVideoFrame)(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
101	void (*postAudioFrame)(struct GBAAVStream*, int16_t left, int16_t right);
102};
103
104struct GBATimer {
105	uint16_t reload;
106	uint16_t oldReload;
107	int32_t lastEvent;
108	int32_t nextEvent;
109	int32_t overflowInterval;
110	unsigned prescaleBits : 4;
111	unsigned countUp : 1;
112	unsigned doIrq : 1;
113	unsigned enable : 1;
114};
115
116struct GBA {
117	struct ARMComponent d;
118
119	struct ARMCore* cpu;
120	struct GBAMemory memory;
121	struct GBAVideo video;
122	struct GBAAudio audio;
123	struct GBASIO sio;
124
125	struct GBASync* sync;
126
127	struct ARMDebugger* debugger;
128
129	uint32_t bus;
130	bool performingDMA;
131
132	int timersEnabled;
133	struct GBATimer timers[4];
134
135	int springIRQ;
136	uint32_t biosChecksum;
137	int* keySource;
138	struct GBARotationSource* rotationSource;
139	struct GBALuminanceSource* luminanceSource;
140	struct GBARTCSource* rtcSource;
141	struct GBARumble* rumble;
142
143	struct GBARRContext* rr;
144	void* pristineRom;
145	size_t pristineRomSize;
146	uint32_t romCrc32;
147	struct VFile* romVf;
148	struct VFile* biosVf;
149
150	const char* activeFile;
151
152	GBALogHandler logHandler;
153	enum GBALogLevel logLevel;
154	struct GBAAVStream* stream;
155
156	enum GBAIdleLoopOptimization idleOptimization;
157	uint32_t idleLoop;
158	uint32_t lastJump;
159	int idleDetectionStep;
160	int idleDetectionFailures;
161	int32_t cachedRegisters[16];
162	bool taintedRegisters[16];
163};
164
165struct GBACartridge {
166	uint32_t entry;
167	uint8_t logo[156];
168	char title[12];
169	uint32_t id;
170	uint16_t maker;
171	uint8_t type;
172	uint8_t unit;
173	uint8_t device;
174	uint8_t reserved[7];
175	uint8_t version;
176	uint8_t checksum;
177	// And ROM data...
178};
179
180void GBACreate(struct GBA* gba);
181void GBADestroy(struct GBA* gba);
182
183void GBAReset(struct ARMCore* cpu);
184void GBASkipBIOS(struct ARMCore* cpu);
185
186void GBATimerUpdateRegister(struct GBA* gba, int timer);
187void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
188void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
189
190void GBAWriteIE(struct GBA* gba, uint16_t value);
191void GBAWriteIME(struct GBA* gba, uint16_t value);
192void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
193void GBATestIRQ(struct ARMCore* cpu);
194void GBAHalt(struct GBA* gba);
195
196void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
197void GBADetachDebugger(struct GBA* gba);
198
199void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode);
200void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
201
202void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname);
203void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
204void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
205
206bool GBAIsROM(struct VFile* vf);
207bool GBAIsBIOS(struct VFile* vf);
208void GBAGetGameCode(struct GBA* gba, char* out);
209void GBAGetGameTitle(struct GBA* gba, char* out);
210
211void GBAFrameStarted(struct GBA* gba);
212void GBAFrameEnded(struct GBA* gba);
213
214__attribute__((format (printf, 3, 4)))
215void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
216
217__attribute__((format (printf, 3, 4)))
218void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
219
220#endif