all repos — mgba @ 70afe23fe4b7609ca85ee080b8dca255ff6d325a

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