all repos — mgba @ 68e70b61f19b1e2b34e39be0c4dff35a84f2d313

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/interface.h"
 15#include "gba/memory.h"
 16#include "gba/video.h"
 17#include "gba/audio.h"
 18#include "gba/sio.h"
 19
 20extern const uint32_t GBA_ARM7TDMI_FREQUENCY;
 21
 22enum GBAIRQ {
 23	IRQ_VBLANK = 0x0,
 24	IRQ_HBLANK = 0x1,
 25	IRQ_VCOUNTER = 0x2,
 26	IRQ_TIMER0 = 0x3,
 27	IRQ_TIMER1 = 0x4,
 28	IRQ_TIMER2 = 0x5,
 29	IRQ_TIMER3 = 0x6,
 30	IRQ_SIO = 0x7,
 31	IRQ_DMA0 = 0x8,
 32	IRQ_DMA1 = 0x9,
 33	IRQ_DMA2 = 0xA,
 34	IRQ_DMA3 = 0xB,
 35	IRQ_KEYPAD = 0xC,
 36	IRQ_GAMEPAK = 0xD
 37};
 38
 39enum GBAComponent {
 40	GBA_COMPONENT_DEBUGGER,
 41	GBA_COMPONENT_CHEAT_DEVICE,
 42	GBA_COMPONENT_MAX
 43};
 44
 45enum GBAIdleLoopOptimization {
 46	IDLE_LOOP_IGNORE = -1,
 47	IDLE_LOOP_REMOVE = 0,
 48	IDLE_LOOP_DETECT
 49};
 50
 51enum {
 52	SP_BASE_SYSTEM = 0x03007F00,
 53	SP_BASE_IRQ = 0x03007FA0,
 54	SP_BASE_SUPERVISOR = 0x03007FE0
 55};
 56
 57struct GBA;
 58struct GBAThread;
 59struct Patch;
 60struct VFile;
 61
 62struct GBATimer {
 63	uint16_t reload;
 64	uint16_t oldReload;
 65	int32_t lastEvent;
 66	int32_t nextEvent;
 67	int32_t overflowInterval;
 68	unsigned prescaleBits : 4;
 69	unsigned countUp : 1;
 70	unsigned doIrq : 1;
 71	unsigned enable : 1;
 72};
 73
 74struct GBA {
 75	struct ARMComponent d;
 76
 77	struct ARMCore* cpu;
 78	struct GBAMemory memory;
 79	struct GBAVideo video;
 80	struct GBAAudio audio;
 81	struct GBASIO sio;
 82
 83	struct GBASync* sync;
 84
 85	struct ARMDebugger* debugger;
 86
 87	uint32_t bus;
 88	bool performingDMA;
 89
 90	int timersEnabled;
 91	struct GBATimer timers[4];
 92
 93	int springIRQ;
 94	uint32_t biosChecksum;
 95	int* keySource;
 96	struct GBARotationSource* rotationSource;
 97	struct GBALuminanceSource* luminanceSource;
 98	struct GBARTCSource* rtcSource;
 99	struct GBARumble* rumble;
100
101	struct GBARRContext* rr;
102	void* pristineRom;
103	size_t pristineRomSize;
104	size_t yankedRomSize;
105	uint32_t romCrc32;
106	struct VFile* romVf;
107	struct VFile* biosVf;
108
109	const char* activeFile;
110
111	GBALogHandler logHandler;
112	enum GBALogLevel logLevel;
113	struct GBAAVStream* stream;
114	struct GBAKeyCallback* keyCallback;
115	struct GBAStopCallback* stopCallback;
116
117	enum GBAIdleLoopOptimization idleOptimization;
118	uint32_t idleLoop;
119	uint32_t lastJump;
120	bool haltPending;
121	int idleDetectionStep;
122	int idleDetectionFailures;
123	int32_t cachedRegisters[16];
124	bool taintedRegisters[16];
125
126	bool realisticTiming;
127};
128
129struct GBACartridge {
130	uint32_t entry;
131	uint8_t logo[156];
132	char title[12];
133	uint32_t id;
134	uint16_t maker;
135	uint8_t type;
136	uint8_t unit;
137	uint8_t device;
138	uint8_t reserved[7];
139	uint8_t version;
140	uint8_t checksum;
141	// And ROM data...
142};
143
144void GBACreate(struct GBA* gba);
145void GBADestroy(struct GBA* gba);
146
147void GBAReset(struct ARMCore* cpu);
148void GBASkipBIOS(struct ARMCore* cpu);
149
150void GBATimerUpdateRegister(struct GBA* gba, int timer);
151void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
152void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
153
154void GBAWriteIE(struct GBA* gba, uint16_t value);
155void GBAWriteIME(struct GBA* gba, uint16_t value);
156void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
157void GBATestIRQ(struct ARMCore* cpu);
158void GBAHalt(struct GBA* gba);
159void GBAStop(struct GBA* gba);
160
161void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
162void GBADetachDebugger(struct GBA* gba);
163
164void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode,
165                      uint32_t* opcode);
166void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
167
168void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname);
169void GBAYankROM(struct GBA* gba);
170void GBAUnloadROM(struct GBA* gba);
171void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
172void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
173
174bool GBAIsROM(struct VFile* vf);
175bool GBAIsBIOS(struct VFile* vf);
176void GBAGetGameCode(struct GBA* gba, char* out);
177void GBAGetGameTitle(struct GBA* gba, char* out);
178
179void GBAFrameStarted(struct GBA* gba);
180void GBAFrameEnded(struct GBA* gba);
181
182ATTRIBUTE_FORMAT(printf, 3, 4)
183void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
184
185ATTRIBUTE_FORMAT(printf, 3, 4)
186void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
187
188#endif