all repos — mgba @ b691c93416d44b89e16df3f700c00198deb7cf1f

mGBA Game Boy Advance Emulator

src/gba/gba.h (view raw)

  1#ifndef GBA_H
  2#define GBA_H
  3
  4#include "arm.h"
  5#include "debugger.h"
  6
  7#include "gba-memory.h"
  8#include "gba-video.h"
  9#include "gba-audio.h"
 10
 11#include <stdarg.h>
 12
 13extern const uint32_t GBA_ARM7TDMI_FREQUENCY;
 14
 15enum GBAIRQ {
 16	IRQ_VBLANK = 0x0,
 17	IRQ_HBLANK = 0x1,
 18	IRQ_VCOUNTER = 0x2,
 19	IRQ_TIMER0 = 0x3,
 20	IRQ_TIMER1 = 0x4,
 21	IRQ_TIMER2 = 0x5,
 22	IRQ_TIMER3 = 0x6,
 23	IRQ_SIO = 0x7,
 24	IRQ_DMA0 = 0x8,
 25	IRQ_DMA1 = 0x9,
 26	IRQ_DMA2 = 0xA,
 27	IRQ_DMA3 = 0xB,
 28	IRQ_KEYPAD = 0xC,
 29	IRQ_GAMEPAK = 0xD
 30};
 31
 32enum GBAError {
 33	GBA_NO_ERROR = 0,
 34	GBA_OUT_OF_MEMORY = -1
 35};
 36
 37enum GBALogLevel {
 38	GBA_LOG_STUB = 0x01,
 39	GBA_LOG_DEBUG = 0x02,
 40	GBA_LOG_INFO = 0x04,
 41	GBA_LOG_WARN = 0x08,
 42	GBA_LOG_ERROR = 0x10,
 43	GBA_LOG_FATAL = 0x20,
 44
 45	GBA_LOG_GAME_ERROR = 0x100
 46};
 47
 48enum GBAKey {
 49	GBA_KEY_A = 0,
 50	GBA_KEY_B = 1,
 51	GBA_KEY_SELECT = 2,
 52	GBA_KEY_START = 3,
 53	GBA_KEY_RIGHT = 4,
 54	GBA_KEY_LEFT = 5,
 55	GBA_KEY_UP = 6,
 56	GBA_KEY_DOWN = 7,
 57	GBA_KEY_R = 8,
 58	GBA_KEY_L = 9,
 59	GBA_KEY_NONE = -1
 60};
 61
 62struct GBARotationSource;
 63struct GBA;
 64typedef void (*GBALogHandler)(struct GBA*, enum GBALogLevel, const char* format, va_list args);
 65
 66struct GBABoard {
 67	struct ARMBoard d;
 68	struct GBA* p;
 69};
 70
 71struct GBA {
 72	struct ARMCore cpu;
 73	struct GBABoard board;
 74	struct GBAMemory memory;
 75	struct GBAVideo video;
 76	struct GBAAudio audio;
 77
 78	struct GBASync* sync;
 79
 80	struct ARMDebugger* debugger;
 81
 82	int timersEnabled;
 83	struct 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	} timers[4];
 94
 95	int springIRQ;
 96	uint32_t biosChecksum;
 97	int* keySource;
 98	struct GBARotationSource* rotationSource;
 99	struct GBARumble* rumble;
100
101	const char* activeFile;
102	const char* savefile;
103
104	int logLevel;
105	GBALogHandler logHandler;
106};
107
108struct GBACartridge {
109	uint32_t entry;
110	uint8_t logo[156];
111	char title[12];
112	uint32_t id;
113	uint16_t maker;
114	uint8_t type;
115	uint8_t unit;
116	uint8_t device;
117	uint8_t reserved[7];
118	uint8_t version;
119	uint8_t checksum;
120	// And ROM data...
121};
122
123void GBAInit(struct GBA* gba);
124void GBADeinit(struct GBA* gba);
125
126void GBAMemoryInit(struct GBAMemory* memory);
127void GBAMemoryDeinit(struct GBAMemory* memory);
128
129void GBABoardInit(struct GBABoard* board);
130void GBABoardReset(struct ARMBoard* board);
131
132void GBATimerUpdateRegister(struct GBA* gba, int timer);
133void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
134void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
135
136void GBAWriteIE(struct GBA* gba, uint16_t value);
137void GBAWriteIME(struct GBA* gba, uint16_t value);
138void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
139void GBATestIRQ(struct ARMBoard* board);
140int GBAWaitForIRQ(struct GBA* gba);
141int GBAHalt(struct GBA* gba);
142
143void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
144void GBADetachDebugger(struct GBA* gba);
145
146void GBALoadROM(struct GBA* gba, int fd, const char* fname);
147void GBALoadBIOS(struct GBA* gba, int fd);
148
149__attribute__((format (printf, 3, 4)))
150void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
151
152__attribute__((format (printf, 3, 4)))
153void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
154
155#endif