all repos — mgba @ e423cd45e5805f0d45fceb032eee8bee6d00f1c6

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
 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
 20#define GBA_ARM7TDMI_FREQUENCY 0x1000000U
 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 GBAIdleLoopOptimization {
 40	IDLE_LOOP_IGNORE = -1,
 41	IDLE_LOOP_REMOVE = 0,
 42	IDLE_LOOP_DETECT
 43};
 44
 45enum {
 46	SP_BASE_SYSTEM = 0x03007F00,
 47	SP_BASE_IRQ = 0x03007FA0,
 48	SP_BASE_SUPERVISOR = 0x03007FE0
 49};
 50
 51struct GBA;
 52struct Patch;
 53struct VFile;
 54
 55mLOG_DECLARE_CATEGORY(GBA);
 56mLOG_DECLARE_CATEGORY(GBA_DEBUG);
 57
 58DECL_BITFIELD(GBATimerFlags, uint32_t);
 59DECL_BITS(GBATimerFlags, PrescaleBits, 0, 4);
 60DECL_BIT(GBATimerFlags, CountUp, 4);
 61DECL_BIT(GBATimerFlags, DoIrq, 5);
 62DECL_BIT(GBATimerFlags, Enable, 6);
 63
 64DECL_BITFIELD(GBADebugFlags, uint16_t);
 65DECL_BITS(GBADebugFlags, Level, 0, 3);
 66DECL_BIT(GBADebugFlags, Send, 8);
 67
 68struct GBATimer {
 69	uint16_t reload;
 70	uint16_t oldReload;
 71	int32_t lastEvent;
 72	int32_t nextEvent;
 73	int32_t overflowInterval;
 74	GBATimerFlags flags;
 75};
 76
 77struct GBA {
 78	struct mCPUComponent d;
 79
 80	struct ARMCore* cpu;
 81	struct GBAMemory memory;
 82	struct GBAVideo video;
 83	struct GBAAudio audio;
 84	struct GBASIO sio;
 85
 86	struct mCoreSync* sync;
 87
 88	struct ARMDebugger* debugger;
 89
 90	uint32_t bus;
 91	int performingDMA;
 92
 93	int timersEnabled;
 94	struct GBATimer timers[4];
 95
 96	int springIRQ;
 97	uint32_t biosChecksum;
 98	int* keySource;
 99	struct mRotationSource* rotationSource;
100	struct GBALuminanceSource* luminanceSource;
101	struct mRTCSource* rtcSource;
102	struct mRumble* rumble;
103
104	struct GBARRContext* rr;
105	void* pristineRom;
106	size_t pristineRomSize;
107	size_t yankedRomSize;
108	uint32_t romCrc32;
109	struct VFile* romVf;
110	struct VFile* biosVf;
111
112	struct mAVStream* stream;
113	struct mKeyCallback* keyCallback;
114	struct mStopCallback* stopCallback;
115	struct mCoreCallbacks* coreCallbacks;
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	bool allowOpposingDirections;
129
130	bool debug;
131	char debugString[0x100];
132	GBADebugFlags debugFlags;
133};
134
135struct GBACartridge {
136	uint32_t entry;
137	uint8_t logo[156];
138	char title[12];
139	uint32_t id;
140	uint16_t maker;
141	uint8_t type;
142	uint8_t unit;
143	uint8_t device;
144	uint8_t reserved[7];
145	uint8_t version;
146	uint8_t checksum;
147	// And ROM data...
148};
149
150void GBACreate(struct GBA* gba);
151void GBADestroy(struct GBA* gba);
152
153void GBAReset(struct ARMCore* cpu);
154void GBASkipBIOS(struct GBA* gba);
155
156void GBATimerUpdateRegister(struct GBA* gba, int timer);
157void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
158void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
159
160void GBAWriteIE(struct GBA* gba, uint16_t value);
161void GBAWriteIME(struct GBA* gba, uint16_t value);
162void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
163void GBATestIRQ(struct ARMCore* cpu);
164void GBAHalt(struct GBA* gba);
165void GBAStop(struct GBA* gba);
166void GBADebug(struct GBA* gba, uint16_t value);
167
168void GBAAttachDebugger(struct GBA* gba, struct mDebugger* debugger);
169void GBADetachDebugger(struct GBA* gba);
170
171void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode,
172                      uint32_t* opcode);
173void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
174
175bool GBALoadROM(struct GBA* gba, struct VFile* vf);
176bool GBALoadSave(struct GBA* gba, struct VFile* sav);
177void GBAYankROM(struct GBA* gba);
178void GBAUnloadROM(struct GBA* gba);
179void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
180void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
181
182bool GBALoadMB(struct GBA* gba, struct VFile* vf);
183
184bool GBAIsROM(struct VFile* vf);
185bool GBAIsMB(struct VFile* vf);
186bool GBAIsBIOS(struct VFile* vf);
187void GBAGetGameCode(const struct GBA* gba, char* out);
188void GBAGetGameTitle(const struct GBA* gba, char* out);
189
190void GBAFrameStarted(struct GBA* gba);
191void GBAFrameEnded(struct GBA* gba);
192
193#endif