all repos — mgba @ 357008ff409ae566a115c855ed4e6cf4654bd303

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 Patch;
 60struct VFile;
 61
 62mLOG_DECLARE_CATEGORY(GBA);
 63
 64DECL_BITFIELD(GBATimerFlags, uint32_t);
 65DECL_BITS(GBATimerFlags, PrescaleBits, 0, 4);
 66DECL_BIT(GBATimerFlags, CountUp, 4);
 67DECL_BIT(GBATimerFlags, DoIrq, 5);
 68DECL_BIT(GBATimerFlags, Enable, 6);
 69
 70struct GBATimer {
 71	uint16_t reload;
 72	uint16_t oldReload;
 73	int32_t lastEvent;
 74	int32_t nextEvent;
 75	int32_t overflowInterval;
 76	GBATimerFlags flags;
 77};
 78
 79struct GBA {
 80	struct mCPUComponent d;
 81
 82	struct ARMCore* cpu;
 83	struct GBAMemory memory;
 84	struct GBAVideo video;
 85	struct GBAAudio audio;
 86	struct GBASIO sio;
 87
 88	struct mCoreSync* sync;
 89
 90	struct Debugger* debugger;
 91
 92	uint32_t bus;
 93	int performingDMA;
 94
 95	int timersEnabled;
 96	struct GBATimer timers[4];
 97
 98	int springIRQ;
 99	uint32_t biosChecksum;
100	int* keySource;
101	struct mRotationSource* rotationSource;
102	struct GBALuminanceSource* luminanceSource;
103	struct mRTCSource* rtcSource;
104	struct mRumble* rumble;
105
106	struct GBARRContext* rr;
107	void* pristineRom;
108	size_t pristineRomSize;
109	size_t yankedRomSize;
110	uint32_t romCrc32;
111	struct VFile* romVf;
112	struct VFile* biosVf;
113
114	struct mAVStream* stream;
115	struct mKeyCallback* keyCallback;
116	struct mStopCallback* stopCallback;
117
118	enum GBAIdleLoopOptimization idleOptimization;
119	uint32_t idleLoop;
120	uint32_t lastJump;
121	bool haltPending;
122	int idleDetectionStep;
123	int idleDetectionFailures;
124	int32_t cachedRegisters[16];
125	bool taintedRegisters[16];
126
127	bool realisticTiming;
128	bool hardCrash;
129	bool allowOpposingDirections;
130};
131
132struct GBACartridge {
133	uint32_t entry;
134	uint8_t logo[156];
135	char title[12];
136	uint32_t id;
137	uint16_t maker;
138	uint8_t type;
139	uint8_t unit;
140	uint8_t device;
141	uint8_t reserved[7];
142	uint8_t version;
143	uint8_t checksum;
144	// And ROM data...
145};
146
147void GBACreate(struct GBA* gba);
148void GBADestroy(struct GBA* gba);
149
150void GBAReset(struct ARMCore* cpu);
151void GBASkipBIOS(struct GBA* gba);
152
153void GBATimerUpdateRegister(struct GBA* gba, int timer);
154void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
155void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
156
157void GBAWriteIE(struct GBA* gba, uint16_t value);
158void GBAWriteIME(struct GBA* gba, uint16_t value);
159void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
160void GBATestIRQ(struct ARMCore* cpu);
161void GBAHalt(struct GBA* gba);
162void GBAStop(struct GBA* gba);
163
164void GBAAttachDebugger(struct GBA* gba, struct Debugger* debugger);
165void GBADetachDebugger(struct GBA* gba);
166
167void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode,
168                      uint32_t* opcode);
169void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
170
171bool GBALoadROM(struct GBA* gba, struct VFile* vf);
172bool GBALoadSave(struct GBA* gba, struct VFile* sav);
173void GBAYankROM(struct GBA* gba);
174void GBAUnloadROM(struct GBA* gba);
175void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
176void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
177
178bool GBALoadMB(struct GBA* gba, struct VFile* vf);
179
180bool GBAIsROM(struct VFile* vf);
181bool GBAIsMB(struct VFile* vf);
182bool GBAIsBIOS(struct VFile* vf);
183void GBAGetGameCode(struct GBA* gba, char* out);
184void GBAGetGameTitle(struct GBA* gba, char* out);
185
186void GBAFrameStarted(struct GBA* gba);
187void GBAFrameEnded(struct GBA* gba);
188
189#endif