all repos — mgba @ f6581773d8e33d1e600a5d2fb615f36023711126

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	GBA_LOG_STATUS = 0x400,
 49	GBA_LOG_SIO = 0x800,
 50
 51	GBA_LOG_ALL = 0xF3F,
 52
 53#ifdef NDEBUG
 54	GBA_LOG_DANGER = GBA_LOG_ERROR
 55#else
 56	GBA_LOG_DANGER = GBA_LOG_FATAL
 57#endif
 58};
 59
 60enum GBAKey {
 61	GBA_KEY_A = 0,
 62	GBA_KEY_B = 1,
 63	GBA_KEY_SELECT = 2,
 64	GBA_KEY_START = 3,
 65	GBA_KEY_RIGHT = 4,
 66	GBA_KEY_LEFT = 5,
 67	GBA_KEY_UP = 6,
 68	GBA_KEY_DOWN = 7,
 69	GBA_KEY_R = 8,
 70	GBA_KEY_L = 9,
 71	GBA_KEY_MAX,
 72	GBA_KEY_NONE = -1
 73};
 74
 75enum GBAComponent {
 76	GBA_COMPONENT_DEBUGGER,
 77	GBA_COMPONENT_CHEAT_DEVICE,
 78	GBA_COMPONENT_MAX
 79};
 80
 81enum GBAIdleLoopOptimization {
 82	IDLE_LOOP_IGNORE = -1,
 83	IDLE_LOOP_REMOVE = 0,
 84	IDLE_LOOP_DETECT
 85};
 86
 87enum {
 88	SP_BASE_SYSTEM = 0x03007F00,
 89	SP_BASE_IRQ = 0x03007FA0,
 90	SP_BASE_SUPERVISOR = 0x03007FE0
 91};
 92
 93struct GBA;
 94struct GBARotationSource;
 95struct GBAThread;
 96struct Patch;
 97struct VFile;
 98
 99typedef void (*GBALogHandler)(struct GBAThread*, enum GBALogLevel, const char* format, va_list args);
100
101struct GBAAVStream {
102	void (*postVideoFrame)(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
103	void (*postAudioFrame)(struct GBAAVStream*, int16_t left, int16_t right);
104	void (*postAudioBuffer)(struct GBAAVStream*, struct GBAAudio*);
105};
106
107struct GBATimer {
108	uint16_t reload;
109	uint16_t oldReload;
110	int32_t lastEvent;
111	int32_t nextEvent;
112	int32_t overflowInterval;
113	unsigned prescaleBits : 4;
114	unsigned countUp : 1;
115	unsigned doIrq : 1;
116	unsigned enable : 1;
117};
118
119struct GBA {
120	struct ARMComponent d;
121
122	struct ARMCore* cpu;
123	struct GBAMemory memory;
124	struct GBAVideo video;
125	struct GBAAudio audio;
126	struct GBASIO sio;
127
128	struct GBASync* sync;
129
130	struct ARMDebugger* debugger;
131
132	uint32_t bus;
133	bool performingDMA;
134
135	int timersEnabled;
136	struct GBATimer timers[4];
137
138	int springIRQ;
139	uint32_t biosChecksum;
140	int* keySource;
141	struct GBARotationSource* rotationSource;
142	struct GBALuminanceSource* luminanceSource;
143	struct GBARTCSource* rtcSource;
144	struct GBARumble* rumble;
145
146	struct GBARRContext* rr;
147	void* pristineRom;
148	size_t pristineRomSize;
149	size_t yankedRomSize;
150	uint32_t romCrc32;
151	struct VFile* romVf;
152	struct VFile* biosVf;
153
154	const char* activeFile;
155
156	GBALogHandler logHandler;
157	enum GBALogLevel logLevel;
158	struct GBAAVStream* stream;
159
160	enum GBAIdleLoopOptimization idleOptimization;
161	uint32_t idleLoop;
162	uint32_t lastJump;
163	bool haltPending;
164	int idleDetectionStep;
165	int idleDetectionFailures;
166	int32_t cachedRegisters[16];
167	bool taintedRegisters[16];
168
169	bool realisticTiming;
170};
171
172struct GBACartridge {
173	uint32_t entry;
174	uint8_t logo[156];
175	char title[12];
176	uint32_t id;
177	uint16_t maker;
178	uint8_t type;
179	uint8_t unit;
180	uint8_t device;
181	uint8_t reserved[7];
182	uint8_t version;
183	uint8_t checksum;
184	// And ROM data...
185};
186
187void GBACreate(struct GBA* gba);
188void GBADestroy(struct GBA* gba);
189
190void GBAReset(struct ARMCore* cpu);
191void GBASkipBIOS(struct ARMCore* cpu);
192
193void GBATimerUpdateRegister(struct GBA* gba, int timer);
194void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
195void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
196
197void GBAWriteIE(struct GBA* gba, uint16_t value);
198void GBAWriteIME(struct GBA* gba, uint16_t value);
199void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
200void GBATestIRQ(struct ARMCore* cpu);
201void GBAHalt(struct GBA* gba);
202
203void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
204void GBADetachDebugger(struct GBA* gba);
205
206void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode,
207                      uint32_t* opcode);
208void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
209
210void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname);
211void GBAYankROM(struct GBA* gba);
212void GBAUnloadROM(struct GBA* gba);
213void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
214void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
215
216bool GBAIsROM(struct VFile* vf);
217bool GBAIsBIOS(struct VFile* vf);
218void GBAGetGameCode(struct GBA* gba, char* out);
219void GBAGetGameTitle(struct GBA* gba, char* out);
220
221void GBAFrameStarted(struct GBA* gba);
222void GBAFrameEnded(struct GBA* gba);
223
224ATTRIBUTE_FORMAT(printf, 3, 4)
225void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
226
227ATTRIBUTE_FORMAT(printf, 3, 4)
228void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
229
230#endif