all repos — mgba @ 7a9e97ca52de3d7785f7caa4bdd03996c87c5f41

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
116	enum GBAIdleLoopOptimization idleOptimization;
117	uint32_t idleLoop;
118	uint32_t lastJump;
119	bool haltPending;
120	int idleDetectionStep;
121	int idleDetectionFailures;
122	int32_t cachedRegisters[16];
123	bool taintedRegisters[16];
124
125	bool realisticTiming;
126};
127
128struct GBACartridge {
129	uint32_t entry;
130	uint8_t logo[156];
131	char title[12];
132	uint32_t id;
133	uint16_t maker;
134	uint8_t type;
135	uint8_t unit;
136	uint8_t device;
137	uint8_t reserved[7];
138	uint8_t version;
139	uint8_t checksum;
140	// And ROM data...
141};
142
143void GBACreate(struct GBA* gba);
144void GBADestroy(struct GBA* gba);
145
146void GBAReset(struct ARMCore* cpu);
147void GBASkipBIOS(struct ARMCore* cpu);
148
149void GBATimerUpdateRegister(struct GBA* gba, int timer);
150void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
151void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
152
153void GBAWriteIE(struct GBA* gba, uint16_t value);
154void GBAWriteIME(struct GBA* gba, uint16_t value);
155void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
156void GBATestIRQ(struct ARMCore* cpu);
157void GBAHalt(struct GBA* gba);
158
159void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
160void GBADetachDebugger(struct GBA* gba);
161
162void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode,
163                      uint32_t* opcode);
164void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
165
166void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname);
167void GBAYankROM(struct GBA* gba);
168void GBAUnloadROM(struct GBA* gba);
169void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
170void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
171
172bool GBAIsROM(struct VFile* vf);
173bool GBAIsBIOS(struct VFile* vf);
174void GBAGetGameCode(struct GBA* gba, char* out);
175void GBAGetGameTitle(struct GBA* gba, char* out);
176
177void GBAFrameStarted(struct GBA* gba);
178void GBAFrameEnded(struct GBA* gba);
179
180ATTRIBUTE_FORMAT(printf, 3, 4)
181void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
182
183ATTRIBUTE_FORMAT(printf, 3, 4)
184void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
185
186#endif