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