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