all repos — mgba @ 9756f79f044767052280bcc7d7d1225b1f326698

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