all repos — mgba @ ea91c48d3e75f72577ef8cd00276819e4c3aa07e

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