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};
48
49enum GBAKey {
50 GBA_KEY_A = 0,
51 GBA_KEY_B = 1,
52 GBA_KEY_SELECT = 2,
53 GBA_KEY_START = 3,
54 GBA_KEY_RIGHT = 4,
55 GBA_KEY_LEFT = 5,
56 GBA_KEY_UP = 6,
57 GBA_KEY_DOWN = 7,
58 GBA_KEY_R = 8,
59 GBA_KEY_L = 9,
60 GBA_KEY_MAX,
61 GBA_KEY_NONE = -1
62};
63
64struct GBA;
65struct GBARotationSource;
66struct Patch;
67struct VFile;
68
69struct GBATimer {
70 uint16_t reload;
71 uint16_t oldReload;
72 int32_t lastEvent;
73 int32_t nextEvent;
74 int32_t overflowInterval;
75 unsigned prescaleBits : 4;
76 unsigned countUp : 1;
77 unsigned doIrq : 1;
78 unsigned enable : 1;
79};
80
81struct GBA {
82 struct ARMComponent d;
83
84 struct ARMCore* cpu;
85 struct GBAMemory memory;
86 struct GBAVideo video;
87 struct GBAAudio audio;
88 struct GBASIO sio;
89
90 struct GBASync* sync;
91
92 struct ARMDebugger* debugger;
93
94 int timersEnabled;
95 struct GBATimer timers[4];
96
97 int springIRQ;
98 uint32_t biosChecksum;
99 int* keySource;
100 uint32_t busyLoop;
101 struct GBARotationSource* rotationSource;
102 struct GBARumble* rumble;
103 struct GBARRContext* rr;
104 void* pristineRom;
105 size_t pristineRomSize;
106 uint32_t romCrc32;
107 struct VFile* romVf;
108 struct VFile* biosVf;
109
110 const char* activeFile;
111
112 int logLevel;
113};
114
115struct GBACartridge {
116 uint32_t entry;
117 uint8_t logo[156];
118 char title[12];
119 uint32_t id;
120 uint16_t maker;
121 uint8_t type;
122 uint8_t unit;
123 uint8_t device;
124 uint8_t reserved[7];
125 uint8_t version;
126 uint8_t checksum;
127 // And ROM data...
128};
129
130void GBACreate(struct GBA* gba);
131void GBADestroy(struct GBA* gba);
132
133void GBAReset(struct ARMCore* cpu);
134
135void GBATimerUpdateRegister(struct GBA* gba, int timer);
136void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
137void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
138
139void GBAWriteIE(struct GBA* gba, uint16_t value);
140void GBAWriteIME(struct GBA* gba, uint16_t value);
141void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
142void GBATestIRQ(struct ARMCore* cpu);
143void GBAHalt(struct GBA* gba);
144
145void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
146void GBADetachDebugger(struct GBA* gba);
147
148void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname);
149void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
150void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
151
152bool GBAIsROM(struct VFile* vf);
153void GBAGetGameCode(struct GBA* gba, char* out);
154void GBAGetGameTitle(struct GBA* gba, char* out);
155
156__attribute__((format (printf, 3, 4)))
157void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
158
159__attribute__((format (printf, 3, 4)))
160void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
161
162#endif