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/interface.h"
15#include "gba/memory.h"
16#include "gba/video.h"
17#include "gba/audio.h"
18#include "gba/sio.h"
19
20extern const uint32_t GBA_ARM7TDMI_FREQUENCY;
21
22enum GBAIRQ {
23 IRQ_VBLANK = 0x0,
24 IRQ_HBLANK = 0x1,
25 IRQ_VCOUNTER = 0x2,
26 IRQ_TIMER0 = 0x3,
27 IRQ_TIMER1 = 0x4,
28 IRQ_TIMER2 = 0x5,
29 IRQ_TIMER3 = 0x6,
30 IRQ_SIO = 0x7,
31 IRQ_DMA0 = 0x8,
32 IRQ_DMA1 = 0x9,
33 IRQ_DMA2 = 0xA,
34 IRQ_DMA3 = 0xB,
35 IRQ_KEYPAD = 0xC,
36 IRQ_GAMEPAK = 0xD
37};
38
39enum GBAComponent {
40 GBA_COMPONENT_DEBUGGER,
41 GBA_COMPONENT_CHEAT_DEVICE,
42 GBA_COMPONENT_MAX
43};
44
45enum GBAIdleLoopOptimization {
46 IDLE_LOOP_IGNORE = -1,
47 IDLE_LOOP_REMOVE = 0,
48 IDLE_LOOP_DETECT
49};
50
51enum {
52 SP_BASE_SYSTEM = 0x03007F00,
53 SP_BASE_IRQ = 0x03007FA0,
54 SP_BASE_SUPERVISOR = 0x03007FE0
55};
56
57struct GBA;
58struct GBAThread;
59struct Patch;
60struct VFile;
61
62DECL_BITFIELD(GBATimerFlags, uint32_t);
63DECL_BITS(GBATimerFlags, PrescaleBits, 0, 4);
64DECL_BIT(GBATimerFlags, CountUp, 4);
65DECL_BIT(GBATimerFlags, DoIrq, 5);
66DECL_BIT(GBATimerFlags, Enable, 6);
67
68struct GBATimer {
69 uint16_t reload;
70 uint16_t oldReload;
71 int32_t lastEvent;
72 int32_t nextEvent;
73 int32_t overflowInterval;
74 GBATimerFlags flags;
75};
76
77struct GBA {
78 struct ARMComponent d;
79
80 struct ARMCore* cpu;
81 struct GBAMemory memory;
82 struct GBAVideo video;
83 struct GBAAudio audio;
84 struct GBASIO sio;
85
86 struct mCoreSync* sync;
87
88 struct ARMDebugger* debugger;
89
90 uint32_t bus;
91 int performingDMA;
92
93 int timersEnabled;
94 struct GBATimer timers[4];
95
96 int springIRQ;
97 uint32_t biosChecksum;
98 int* keySource;
99 struct mRotationSource* rotationSource;
100 struct GBALuminanceSource* luminanceSource;
101 struct mRTCSource* rtcSource;
102 struct mRumble* rumble;
103
104 struct GBARRContext* rr;
105 void* pristineRom;
106 size_t pristineRomSize;
107 size_t yankedRomSize;
108 uint32_t romCrc32;
109 struct VFile* romVf;
110 struct VFile* biosVf;
111
112 const char* activeFile;
113
114 GBALogHandler logHandler;
115 enum GBALogLevel logLevel;
116 struct GBAAVStream* stream;
117 struct mKeyCallback* keyCallback;
118 struct mStopCallback* stopCallback;
119
120 enum GBAIdleLoopOptimization idleOptimization;
121 uint32_t idleLoop;
122 uint32_t lastJump;
123 bool haltPending;
124 int idleDetectionStep;
125 int idleDetectionFailures;
126 int32_t cachedRegisters[16];
127 bool taintedRegisters[16];
128
129 bool realisticTiming;
130 bool hardCrash;
131 bool allowOpposingDirections;
132};
133
134struct GBACartridge {
135 uint32_t entry;
136 uint8_t logo[156];
137 char title[12];
138 uint32_t id;
139 uint16_t maker;
140 uint8_t type;
141 uint8_t unit;
142 uint8_t device;
143 uint8_t reserved[7];
144 uint8_t version;
145 uint8_t checksum;
146 // And ROM data...
147};
148
149void GBACreate(struct GBA* gba);
150void GBADestroy(struct GBA* gba);
151
152void GBAReset(struct ARMCore* cpu);
153void GBASkipBIOS(struct GBA* gba);
154
155void GBATimerUpdateRegister(struct GBA* gba, int timer);
156void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
157void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
158
159void GBAWriteIE(struct GBA* gba, uint16_t value);
160void GBAWriteIME(struct GBA* gba, uint16_t value);
161void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
162void GBATestIRQ(struct ARMCore* cpu);
163void GBAHalt(struct GBA* gba);
164void GBAStop(struct GBA* gba);
165
166void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
167void GBADetachDebugger(struct GBA* gba);
168
169void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode,
170 uint32_t* opcode);
171void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
172
173bool GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname);
174void GBAYankROM(struct GBA* gba);
175void GBAUnloadROM(struct GBA* gba);
176void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
177void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
178
179bool GBALoadMB(struct GBA* gba, struct VFile* vf, const char* fname);
180
181bool GBAIsROM(struct VFile* vf);
182bool GBAIsMB(struct VFile* vf);
183bool GBAIsBIOS(struct VFile* vf);
184void GBAGetGameCode(struct GBA* gba, char* out);
185void GBAGetGameTitle(struct GBA* gba, char* out);
186
187void GBAFrameStarted(struct GBA* gba);
188void GBAFrameEnded(struct GBA* gba);
189
190ATTRIBUTE_FORMAT(printf, 3, 4)
191void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
192
193ATTRIBUTE_FORMAT(printf, 3, 4)
194void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
195
196#endif