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