src/gba/gba.h (view raw)
1/* Copyright (c) 2013-2015 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 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 GBA_LOG_STATUS = 0x400,
49 GBA_LOG_SIO = 0x800,
50
51 GBA_LOG_ALL = 0xF3F,
52};
53
54enum GBAKey {
55 GBA_KEY_A = 0,
56 GBA_KEY_B = 1,
57 GBA_KEY_SELECT = 2,
58 GBA_KEY_START = 3,
59 GBA_KEY_RIGHT = 4,
60 GBA_KEY_LEFT = 5,
61 GBA_KEY_UP = 6,
62 GBA_KEY_DOWN = 7,
63 GBA_KEY_R = 8,
64 GBA_KEY_L = 9,
65 GBA_KEY_MAX,
66 GBA_KEY_NONE = -1
67};
68
69enum GBAComponent {
70 GBA_COMPONENT_DEBUGGER,
71 GBA_COMPONENT_CHEAT_DEVICE,
72 GBA_COMPONENT_MAX
73};
74
75enum GBAIdleLoopOptimization {
76 IDLE_LOOP_IGNORE = -1,
77 IDLE_LOOP_REMOVE = 0,
78 IDLE_LOOP_DETECT
79};
80
81enum {
82 SP_BASE_SYSTEM = 0x03007F00,
83 SP_BASE_IRQ = 0x03007FA0,
84 SP_BASE_SUPERVISOR = 0x03007FE0
85};
86
87struct GBA;
88struct GBARotationSource;
89struct GBAThread;
90struct Patch;
91struct VFile;
92
93typedef void (*GBALogHandler)(struct GBAThread*, enum GBALogLevel, const char* format, va_list args);
94
95struct GBAAVStream {
96 void (*postVideoFrame)(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
97 void (*postAudioFrame)(struct GBAAVStream*, int16_t left, int16_t right);
98 void (*postAudioBuffer)(struct GBAAVStream*, struct GBAAudio*);
99};
100
101struct GBATimer {
102 uint16_t reload;
103 uint16_t oldReload;
104 int32_t lastEvent;
105 int32_t nextEvent;
106 int32_t overflowInterval;
107 unsigned prescaleBits : 4;
108 unsigned countUp : 1;
109 unsigned doIrq : 1;
110 unsigned enable : 1;
111};
112
113struct GBA {
114 struct ARMComponent d;
115
116 struct ARMCore* cpu;
117 struct GBAMemory memory;
118 struct GBAVideo video;
119 struct GBAAudio audio;
120 struct GBASIO sio;
121
122 struct GBASync* sync;
123
124 struct ARMDebugger* debugger;
125
126 uint32_t bus;
127 bool performingDMA;
128
129 int timersEnabled;
130 struct GBATimer timers[4];
131
132 int springIRQ;
133 uint32_t biosChecksum;
134 int* keySource;
135 struct GBARotationSource* rotationSource;
136 struct GBALuminanceSource* luminanceSource;
137 struct GBARTCSource* rtcSource;
138 struct GBARumble* rumble;
139
140 struct GBARRContext* rr;
141 void* pristineRom;
142 size_t pristineRomSize;
143 size_t yankedRomSize;
144 uint32_t romCrc32;
145 struct VFile* romVf;
146 struct VFile* biosVf;
147
148 const char* activeFile;
149
150 GBALogHandler logHandler;
151 enum GBALogLevel logLevel;
152 struct GBAAVStream* stream;
153
154 enum GBAIdleLoopOptimization idleOptimization;
155 uint32_t idleLoop;
156 uint32_t lastJump;
157 bool haltPending;
158 int idleDetectionStep;
159 int idleDetectionFailures;
160 int32_t cachedRegisters[16];
161 bool taintedRegisters[16];
162
163 bool realisticTiming;
164};
165
166struct GBACartridge {
167 uint32_t entry;
168 uint8_t logo[156];
169 char title[12];
170 uint32_t id;
171 uint16_t maker;
172 uint8_t type;
173 uint8_t unit;
174 uint8_t device;
175 uint8_t reserved[7];
176 uint8_t version;
177 uint8_t checksum;
178 // And ROM data...
179};
180
181void GBACreate(struct GBA* gba);
182void GBADestroy(struct GBA* gba);
183
184void GBAReset(struct ARMCore* cpu);
185void GBASkipBIOS(struct ARMCore* cpu);
186
187void GBATimerUpdateRegister(struct GBA* gba, int timer);
188void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
189void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
190
191void GBAWriteIE(struct GBA* gba, uint16_t value);
192void GBAWriteIME(struct GBA* gba, uint16_t value);
193void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
194void GBATestIRQ(struct ARMCore* cpu);
195void GBAHalt(struct GBA* gba);
196
197void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
198void GBADetachDebugger(struct GBA* gba);
199
200void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode,
201 uint32_t* opcode);
202void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
203
204void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname);
205void GBAYankROM(struct GBA* gba);
206void GBAUnloadROM(struct GBA* gba);
207void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
208void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
209
210bool GBAIsROM(struct VFile* vf);
211bool GBAIsBIOS(struct VFile* vf);
212void GBAGetGameCode(struct GBA* gba, char* out);
213void GBAGetGameTitle(struct GBA* gba, char* out);
214
215void GBAFrameStarted(struct GBA* gba);
216void GBAFrameEnded(struct GBA* gba);
217
218ATTRIBUTE_FORMAT(printf, 3, 4)
219void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
220
221ATTRIBUTE_FORMAT(printf, 3, 4)
222void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
223
224#endif