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 GBASync* sync;
87
88 struct ARMDebugger* debugger;
89
90 uint32_t bus;
91 bool performingDMA;
92
93 int timersEnabled;
94 struct GBATimer timers[4];
95
96 int springIRQ;
97 uint32_t biosChecksum;
98 int* keySource;
99 struct GBARotationSource* rotationSource;
100 struct GBALuminanceSource* luminanceSource;
101 struct GBARTCSource* rtcSource;
102 struct GBARumble* 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 GBAKeyCallback* keyCallback;
118 struct GBAStopCallback* 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};
132
133struct GBACartridge {
134 uint32_t entry;
135 uint8_t logo[156];
136 char title[12];
137 uint32_t id;
138 uint16_t maker;
139 uint8_t type;
140 uint8_t unit;
141 uint8_t device;
142 uint8_t reserved[7];
143 uint8_t version;
144 uint8_t checksum;
145 // And ROM data...
146};
147
148void GBACreate(struct GBA* gba);
149void GBADestroy(struct GBA* gba);
150
151void GBAReset(struct ARMCore* cpu);
152void GBASkipBIOS(struct ARMCore* cpu);
153
154void GBATimerUpdateRegister(struct GBA* gba, int timer);
155void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
156void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
157
158void GBAWriteIE(struct GBA* gba, uint16_t value);
159void GBAWriteIME(struct GBA* gba, uint16_t value);
160void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
161void GBATestIRQ(struct ARMCore* cpu);
162void GBAHalt(struct GBA* gba);
163void GBAStop(struct GBA* gba);
164
165void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger);
166void GBADetachDebugger(struct GBA* gba);
167
168void GBASetBreakpoint(struct GBA* gba, struct ARMComponent* component, uint32_t address, enum ExecutionMode mode,
169 uint32_t* opcode);
170void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
171
172bool GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname);
173void GBAYankROM(struct GBA* gba);
174void GBAUnloadROM(struct GBA* gba);
175void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
176void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
177
178bool GBAIsROM(struct VFile* vf);
179bool GBAIsBIOS(struct VFile* vf);
180void GBAGetGameCode(struct GBA* gba, char* out);
181void GBAGetGameTitle(struct GBA* gba, char* out);
182
183void GBAFrameStarted(struct GBA* gba);
184void GBAFrameEnded(struct GBA* gba);
185
186ATTRIBUTE_FORMAT(printf, 3, 4)
187void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...);
188
189ATTRIBUTE_FORMAT(printf, 3, 4)
190void GBADebuggerLogShim(struct ARMDebugger* debugger, enum DebuggerLogLevel level, const char* format, ...);
191
192#endif