all repos — mgba @ 33a4c45f3ff72c83a3f68b5f4e5730764018a743

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