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
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
20#define GBA_ARM7TDMI_FREQUENCY 0x1000000U
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 GBAIdleLoopOptimization {
40 IDLE_LOOP_IGNORE = -1,
41 IDLE_LOOP_REMOVE = 0,
42 IDLE_LOOP_DETECT
43};
44
45enum {
46 SP_BASE_SYSTEM = 0x03007F00,
47 SP_BASE_IRQ = 0x03007FA0,
48 SP_BASE_SUPERVISOR = 0x03007FE0
49};
50
51struct GBA;
52struct Patch;
53struct VFile;
54
55mLOG_DECLARE_CATEGORY(GBA);
56
57DECL_BITFIELD(GBATimerFlags, uint32_t);
58DECL_BITS(GBATimerFlags, PrescaleBits, 0, 4);
59DECL_BIT(GBATimerFlags, CountUp, 4);
60DECL_BIT(GBATimerFlags, DoIrq, 5);
61DECL_BIT(GBATimerFlags, Enable, 6);
62
63struct GBATimer {
64 uint16_t reload;
65 uint16_t oldReload;
66 int32_t lastEvent;
67 int32_t nextEvent;
68 int32_t overflowInterval;
69 GBATimerFlags flags;
70};
71
72struct GBA {
73 struct mCPUComponent d;
74
75 struct ARMCore* cpu;
76 struct GBAMemory memory;
77 struct GBAVideo video;
78 struct GBAAudio audio;
79 struct GBASIO sio;
80
81 struct mCoreSync* sync;
82
83 struct ARMDebugger* debugger;
84
85 uint32_t bus;
86 int performingDMA;
87
88 int timersEnabled;
89 struct GBATimer timers[4];
90
91 int springIRQ;
92 uint32_t biosChecksum;
93 int* keySource;
94 struct mRotationSource* rotationSource;
95 struct GBALuminanceSource* luminanceSource;
96 struct mRTCSource* rtcSource;
97 struct mRumble* rumble;
98
99 struct GBARRContext* rr;
100 void* pristineRom;
101 size_t pristineRomSize;
102 size_t yankedRomSize;
103 uint32_t romCrc32;
104 struct VFile* romVf;
105 struct VFile* biosVf;
106
107 struct mAVStream* stream;
108 struct mKeyCallback* keyCallback;
109 struct mStopCallback* stopCallback;
110
111 enum GBAIdleLoopOptimization idleOptimization;
112 uint32_t idleLoop;
113 uint32_t lastJump;
114 bool haltPending;
115 int idleDetectionStep;
116 int idleDetectionFailures;
117 int32_t cachedRegisters[16];
118 bool taintedRegisters[16];
119
120 bool realisticTiming;
121 bool hardCrash;
122 bool allowOpposingDirections;
123};
124
125struct GBACartridge {
126 uint32_t entry;
127 uint8_t logo[156];
128 char title[12];
129 uint32_t id;
130 uint16_t maker;
131 uint8_t type;
132 uint8_t unit;
133 uint8_t device;
134 uint8_t reserved[7];
135 uint8_t version;
136 uint8_t checksum;
137 // And ROM data...
138};
139
140void GBACreate(struct GBA* gba);
141void GBADestroy(struct GBA* gba);
142
143void GBAReset(struct ARMCore* cpu);
144void GBASkipBIOS(struct GBA* gba);
145
146void GBATimerUpdateRegister(struct GBA* gba, int timer);
147void GBATimerWriteTMCNT_LO(struct GBA* gba, int timer, uint16_t value);
148void GBATimerWriteTMCNT_HI(struct GBA* gba, int timer, uint16_t value);
149
150void GBAWriteIE(struct GBA* gba, uint16_t value);
151void GBAWriteIME(struct GBA* gba, uint16_t value);
152void GBARaiseIRQ(struct GBA* gba, enum GBAIRQ irq);
153void GBATestIRQ(struct ARMCore* cpu);
154void GBAHalt(struct GBA* gba);
155void GBAStop(struct GBA* gba);
156
157void GBAAttachDebugger(struct GBA* gba, struct mDebugger* debugger);
158void GBADetachDebugger(struct GBA* gba);
159
160void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t address, enum ExecutionMode mode,
161 uint32_t* opcode);
162void GBAClearBreakpoint(struct GBA* gba, uint32_t address, enum ExecutionMode mode, uint32_t opcode);
163
164bool GBALoadROM(struct GBA* gba, struct VFile* vf);
165bool GBALoadSave(struct GBA* gba, struct VFile* sav);
166void GBAYankROM(struct GBA* gba);
167void GBAUnloadROM(struct GBA* gba);
168void GBALoadBIOS(struct GBA* gba, struct VFile* vf);
169void GBAApplyPatch(struct GBA* gba, struct Patch* patch);
170
171bool GBALoadMB(struct GBA* gba, struct VFile* vf);
172
173bool GBAIsROM(struct VFile* vf);
174bool GBAIsMB(struct VFile* vf);
175bool GBAIsBIOS(struct VFile* vf);
176void GBAGetGameCode(const struct GBA* gba, char* out);
177void GBAGetGameTitle(const struct GBA* gba, char* out);
178
179void GBAFrameStarted(struct GBA* gba);
180void GBAFrameEnded(struct GBA* gba);
181
182#endif