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