all repos — mgba @ ca51098ca0ac23d88f5b9235c0cc0244521175b8

mGBA Game Boy Advance Emulator

src/gba/gba.h (view raw)

  1/* Copyright (c) 2013-2014 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.h"
 12#include "debugger/debugger.h"
 13
 14#include "gba-memory.h"
 15#include "gba-video.h"
 16#include "gba-audio.h"
 17#include "gba-sio.h"
 18
 19extern const uint32_t GBA_ARM7TDMI_FREQUENCY;
 20
 21enum GBAIRQ {
 22	IRQ_VBLANK = 0x0,
 23	IRQ_HBLANK = 0x1,
 24	IRQ_VCOUNTER = 0x2,
 25	IRQ_TIMER0 = 0x3,
 26	IRQ_TIMER1 = 0x4,
 27	IRQ_TIMER2 = 0x5,
 28	IRQ_TIMER3 = 0x6,
 29	IRQ_SIO = 0x7,
 30	IRQ_DMA0 = 0x8,
 31	IRQ_DMA1 = 0x9,
 32	IRQ_DMA2 = 0xA,
 33	IRQ_DMA3 = 0xB,
 34	IRQ_KEYPAD = 0xC,
 35	IRQ_GAMEPAK = 0xD
 36};
 37
 38enum GBAError {
 39	GBA_NO_ERROR = 0,
 40	GBA_OUT_OF_MEMORY = -1
 41};
 42
 43enum GBALogLevel {
 44	GBA_LOG_FATAL = 0x01,
 45	GBA_LOG_ERROR = 0x02,
 46	GBA_LOG_WARN = 0x04,
 47	GBA_LOG_INFO = 0x08,
 48	GBA_LOG_DEBUG = 0x10,
 49	GBA_LOG_STUB = 0x20,
 50
 51	GBA_LOG_GAME_ERROR = 0x100,
 52	GBA_LOG_SWI = 0x200,
 53
 54	GBA_LOG_ALL = 0x33F,
 55
 56#ifdef NDEBUG
 57	GBA_LOG_DANGER = GBA_LOG_ERROR
 58#else
 59	GBA_LOG_DANGER = GBA_LOG_FATAL
 60#endif
 61};
 62
 63enum GBAKey {
 64	GBA_KEY_A = 0,
 65	GBA_KEY_B = 1,
 66	GBA_KEY_SELECT = 2,
 67	GBA_KEY_START = 3,
 68	GBA_KEY_RIGHT = 4,
 69	GBA_KEY_LEFT = 5,
 70	GBA_KEY_UP = 6,
 71	GBA_KEY_DOWN = 7,
 72	GBA_KEY_R = 8,
 73	GBA_KEY_L = 9,
 74	GBA_KEY_MAX,
 75	GBA_KEY_NONE = -1
 76};
 77
 78struct GBA;
 79struct GBARotationSource;
 80struct Patch;
 81struct VFile;
 82
 83struct GBATimer {
 84	uint16_t reload;
 85	uint16_t oldReload;
 86	int32_t lastEvent;
 87	int32_t nextEvent;
 88	int32_t overflowInterval;
 89	unsigned prescaleBits : 4;
 90	unsigned countUp : 1;
 91	unsigned doIrq : 1;
 92	unsigned enable : 1;
 93};
 94
 95struct GBA {
 96	struct ARMComponent d;
 97
 98	struct ARMCore* cpu;
 99	struct GBAMemory memory;
100	struct GBAVideo video;
101	struct GBAAudio audio;
102	struct GBASIO sio;
103
104	struct GBASync* sync;
105
106	struct ARMDebugger* debugger;
107
108	uint32_t bus;
109
110	int timersEnabled;
111	struct GBATimer timers[4];
112
113	int springIRQ;
114	uint32_t biosChecksum;
115	int* keySource;
116	uint32_t busyLoop;
117	struct GBARotationSource* rotationSource;
118	struct GBALuminanceSource* luminanceSource;
119	struct GBARumble* rumble;
120
121	struct GBARRContext* rr;
122	void* pristineRom;
123	size_t pristineRomSize;
124	uint32_t romCrc32;
125	struct VFile* romVf;
126	struct VFile* biosVf;
127
128	const char* activeFile;
129
130	int logLevel;
131};
132
133struct GBACartridge {
134	uint32_t entry;
135	uint8_t logo[156];
136	char title[12];
137	uint32_t id;
138	uint16_t maker;
139	uint8_t type;
140	uint8_t unit;
141	uint8_t device;
142	uint8_t reserved[7];
143	uint8_t version;
144	uint8_t checksum;
145	// And ROM data...
146};
147
148void GBACreate(struct GBA* gba);
149void GBADestroy(struct GBA* gba);
150
151void GBAReset(struct ARMCore* cpu);
152void GBASkipBIOS(struct ARMCore* cpu);
153
154void GBATimerUpdateRegister(struct GBA* gba, int timer);
155void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
156void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
157
158void GBAWriteIE(struct GBA* gba, uint16_t value);
159void GBAWriteIME(struct GBA* gba, uint16_t value);
160void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
161void GBATestIRQ(struct ARMCore* cpu);
162void GBAHalt(struct GBA* gba);
163
164void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
165void GBADetachDebugger(struct GBA* gba);
166
167void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname);
168void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
169void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
170
171bool GBAIsROM(struct VFile* vf);
172void GBAGetGameCode(struct GBA* gba, char* out);
173void GBAGetGameTitle(struct GBA* gba, char* out);
174
175__attribute__((format (printf, 3, 4)))
176void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
177
178__attribute__((format (printf, 3, 4)))
179void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
180
181#endif