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