all repos — mgba @ 7ff5c3a905b0a4e42691a61b8059648c1bd5ec8b

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 "arm/debugger.h"
 13#include "core/log.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 GBAIdleLoopOptimization {
 41	IDLE_LOOP_IGNORE = -1,
 42	IDLE_LOOP_REMOVE = 0,
 43	IDLE_LOOP_DETECT
 44};
 45
 46enum {
 47	SP_BASE_SYSTEM = 0x03007F00,
 48	SP_BASE_IRQ = 0x03007FA0,
 49	SP_BASE_SUPERVISOR = 0x03007FE0
 50};
 51
 52struct GBA;
 53struct Patch;
 54struct VFile;
 55
 56mLOG_DECLARE_CATEGORY(GBA);
 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
 64struct GBATimer {
 65	uint16_t reload;
 66	uint16_t oldReload;
 67	int32_t lastEvent;
 68	int32_t nextEvent;
 69	int32_t overflowInterval;
 70	GBATimerFlags flags;
 71};
 72
 73struct GBA {
 74	struct mCPUComponent d;
 75
 76	struct ARMCore* cpu;
 77	struct GBAMemory memory;
 78	struct GBAVideo video;
 79	struct GBAAudio audio;
 80	struct GBASIO sio;
 81
 82	struct mCoreSync* sync;
 83
 84	struct ARMDebugger* debugger;
 85
 86	uint32_t bus;
 87	int performingDMA;
 88
 89	int timersEnabled;
 90	struct GBATimer timers[4];
 91
 92	int springIRQ;
 93	uint32_t biosChecksum;
 94	int* keySource;
 95	struct mRotationSource* rotationSource;
 96	struct GBALuminanceSource* luminanceSource;
 97	struct mRTCSource* rtcSource;
 98	struct mRumble* rumble;
 99
100	struct GBARRContext* rr;
101	void* pristineRom;
102	size_t pristineRomSize;
103	size_t yankedRomSize;
104	uint32_t romCrc32;
105	struct VFile* romVf;
106	struct VFile* biosVf;
107
108	struct mAVStream* stream;
109	struct mKeyCallback* keyCallback;
110	struct mStopCallback* stopCallback;
111
112	enum GBAIdleLoopOptimization idleOptimization;
113	uint32_t idleLoop;
114	uint32_t lastJump;
115	bool haltPending;
116	int idleDetectionStep;
117	int idleDetectionFailures;
118	int32_t cachedRegisters[16];
119	bool taintedRegisters[16];
120
121	bool realisticTiming;
122	bool hardCrash;
123	bool allowOpposingDirections;
124};
125
126struct GBACartridge {
127	uint32_t entry;
128	uint8_t logo[156];
129	char title[12];
130	uint32_t id;
131	uint16_t maker;
132	uint8_t type;
133	uint8_t unit;
134	uint8_t device;
135	uint8_t reserved[7];
136	uint8_t version;
137	uint8_t checksum;
138	// And ROM data...
139};
140
141void GBACreate(struct GBA* gba);
142void GBADestroy(struct GBA* gba);
143
144void GBAReset(struct ARMCore* cpu);
145void GBASkipBIOS(struct GBA* gba);
146
147void GBATimerUpdateRegister(struct GBA* gba, int timer);
148void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
149void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
150
151void GBAWriteIE(struct GBA* gba, uint16_t value);
152void GBAWriteIME(struct GBA* gba, uint16_t value);
153void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
154void GBATestIRQ(struct ARMCore* cpu);
155void GBAHalt(struct GBA* gba);
156void GBAStop(struct GBA* gba);
157
158void GBAAttachDebugger(struct GBA* gba, struct mDebugger* debugger);
159void GBADetachDebugger(struct GBA* gba);
160
161void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode,
162                      uint32_t* opcode);
163void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
164
165bool GBALoadROM(struct GBA* gba, struct VFile* vf);
166bool GBALoadSave(struct GBA* gba, struct VFile* sav);
167void GBAYankROM(struct GBA* gba);
168void GBAUnloadROM(struct GBA* gba);
169void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
170void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
171
172bool GBALoadMB(struct GBA* gba, struct VFile* vf);
173
174bool GBAIsROM(struct VFile* vf);
175bool GBAIsMB(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
183#endif