all repos — mgba @ 9739f177c76de09233544144d68130337280deb3

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	uint32_t romCrc32;
150	struct VFile* romVf;
151	struct VFile* biosVf;
152
153	const char* activeFile;
154
155	GBALogHandler logHandler;
156	enum GBALogLevel logLevel;
157	struct GBAAVStream* stream;
158
159	enum GBAIdleLoopOptimization idleOptimization;
160	uint32_t idleLoop;
161	uint32_t lastJump;
162	bool haltPending;
163	int idleDetectionStep;
164	int idleDetectionFailures;
165	int32_t cachedRegisters[16];
166	bool taintedRegisters[16];
167
168	bool realisticTiming;
169};
170
171struct GBACartridge {
172	uint32_t entry;
173	uint8_t logo[156];
174	char title[12];
175	uint32_t id;
176	uint16_t maker;
177	uint8_t type;
178	uint8_t unit;
179	uint8_t device;
180	uint8_t reserved[7];
181	uint8_t version;
182	uint8_t checksum;
183	// And ROM data...
184};
185
186void GBACreate(struct GBA* gba);
187void GBADestroy(struct GBA* gba);
188
189void GBAReset(struct ARMCore* cpu);
190void GBASkipBIOS(struct ARMCore* cpu);
191
192void GBATimerUpdateRegister(struct GBA* gba, int timer);
193void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
194void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
195
196void GBAWriteIE(struct GBA* gba, uint16_t value);
197void GBAWriteIME(struct GBA* gba, uint16_t value);
198void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
199void GBATestIRQ(struct ARMCore* cpu);
200void GBAHalt(struct GBA* gba);
201
202void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
203void GBADetachDebugger(struct GBA* gba);
204
205void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode, uint32_t* opcode);
206void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
207
208void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname);
209void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
210void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
211
212bool GBAIsROM(struct VFile* vf);
213bool GBAIsBIOS(struct VFile* vf);
214void GBAGetGameCode(struct GBA* gba, char* out);
215void GBAGetGameTitle(struct GBA* gba, char* out);
216
217void GBAFrameStarted(struct GBA* gba);
218void GBAFrameEnded(struct GBA* gba);
219
220ATTRIBUTE_FORMAT(printf,3,4)
221void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
222
223ATTRIBUTE_FORMAT(printf, 3, 4)
224void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
225
226#endif