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/arm.h"
12#include "core/log.h"
13#include "debugger/debugger.h"
14
15#include "gba/interface.h"
16#include "gba/memory.h"
17#include "gba/video.h"
18#include "gba/audio.h"
19#include "gba/sio.h"
20
21extern const uint32_t GBA_ARM7TDMI_FREQUENCY;
22
23enum GBAIRQ {
24 IRQ_VBLANK = 0x0,
25 IRQ_HBLANK = 0x1,
26 IRQ_VCOUNTER = 0x2,
27 IRQ_TIMER0 = 0x3,
28 IRQ_TIMER1 = 0x4,
29 IRQ_TIMER2 = 0x5,
30 IRQ_TIMER3 = 0x6,
31 IRQ_SIO = 0x7,
32 IRQ_DMA0 = 0x8,
33 IRQ_DMA1 = 0x9,
34 IRQ_DMA2 = 0xA,
35 IRQ_DMA3 = 0xB,
36 IRQ_KEYPAD = 0xC,
37 IRQ_GAMEPAK = 0xD
38};
39
40enum GBAComponent {
41 GBA_COMPONENT_DEBUGGER,
42 GBA_COMPONENT_CHEAT_DEVICE,
43 GBA_COMPONENT_MAX
44};
45
46enum GBAIdleLoopOptimization {
47 IDLE_LOOP_IGNORE = -1,
48 IDLE_LOOP_REMOVE = 0,
49 IDLE_LOOP_DETECT
50};
51
52enum {
53 SP_BASE_SYSTEM = 0x03007F00,
54 SP_BASE_IRQ = 0x03007FA0,
55 SP_BASE_SUPERVISOR = 0x03007FE0
56};
57
58struct GBA;
59struct Patch;
60struct VFile;
61
62mLOG_DECLARE_CATEGORY(GBA);
63
64DECL_BITFIELD(GBATimerFlags, uint32_t);
65DECL_BITS(GBATimerFlags, PrescaleBits, 0, 4);
66DECL_BIT(GBATimerFlags, CountUp, 4);
67DECL_BIT(GBATimerFlags, DoIrq, 5);
68DECL_BIT(GBATimerFlags, Enable, 6);
69
70struct GBATimer {
71 uint16_t reload;
72 uint16_t oldReload;
73 int32_t lastEvent;
74 int32_t nextEvent;
75 int32_t overflowInterval;
76 GBATimerFlags flags;
77};
78
79struct GBA {
80 struct mCPUComponent d;
81
82 struct ARMCore* cpu;
83 struct GBAMemory memory;
84 struct GBAVideo video;
85 struct GBAAudio audio;
86 struct GBASIO sio;
87
88 struct mCoreSync* sync;
89
90 struct Debugger* debugger;
91
92 uint32_t bus;
93 int performingDMA;
94
95 int timersEnabled;
96 struct GBATimer timers[4];
97
98 int springIRQ;
99 uint32_t biosChecksum;
100 int* keySource;
101 struct mRotationSource* rotationSource;
102 struct GBALuminanceSource* luminanceSource;
103 struct mRTCSource* rtcSource;
104 struct mRumble* rumble;
105
106 struct GBARRContext* rr;
107 void* pristineRom;
108 size_t pristineRomSize;
109 size_t yankedRomSize;
110 uint32_t romCrc32;
111 struct VFile* romVf;
112 struct VFile* biosVf;
113
114 const char* activeFile;
115
116 struct mAVStream* 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 Debugger* debugger);
167void GBADetachDebugger(struct GBA* gba);
168
169void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* 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);
174bool GBALoadROM2(struct GBA* gba, struct VFile* vf);
175bool GBALoadSave(struct GBA* gba, struct VFile* sav);
176void GBAYankROM(struct GBA* gba);
177void GBAUnloadROM(struct GBA* gba);
178void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
179void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
180
181bool GBALoadMB(struct GBA* gba, struct VFile* vf, const char* fname);
182
183bool GBAIsROM(struct VFile* vf);
184bool GBAIsMB(struct VFile* vf);
185bool GBAIsBIOS(struct VFile* vf);
186void GBAGetGameCode(struct GBA* gba, char* out);
187void GBAGetGameTitle(struct GBA* gba, char* out);
188
189void GBAFrameStarted(struct GBA* gba);
190void GBAFrameEnded(struct GBA* gba);
191
192ATTRIBUTE_FORMAT(printf, 3, 4)
193void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
194
195ATTRIBUTE_FORMAT(printf, 3, 4)
196void GBADebuggerLogShim(struct Debugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
197
198#endif