include/mgba/internal/ds/ds.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 DS_H
7#define DS_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/core/log.h>
14#include <mgba/core/timing.h>
15#include <mgba-util/circle-buffer.h>
16
17#include <mgba/internal/ds/audio.h>
18#include <mgba/internal/ds/gx.h>
19#include <mgba/internal/ds/memory.h>
20#include <mgba/internal/ds/timer.h>
21#include <mgba/internal/ds/video.h>
22#include <mgba/internal/ds/wifi.h>
23#include <mgba/internal/gba/hardware.h>
24
25extern const uint32_t DS_ARM946ES_FREQUENCY;
26extern const uint32_t DS_ARM7TDMI_FREQUENCY;
27extern const uint8_t DS_CHIP_ID[4];
28
29enum DSIRQ {
30 DS_IRQ_VBLANK = 0x0,
31 DS_IRQ_HBLANK = 0x1,
32 DS_IRQ_VCOUNTER = 0x2,
33 DS_IRQ_TIMER0 = 0x3,
34 DS_IRQ_TIMER1 = 0x4,
35 DS_IRQ_TIMER2 = 0x5,
36 DS_IRQ_TIMER3 = 0x6,
37 DS_IRQ_SIO = 0x7,
38 DS_IRQ_DMA0 = 0x8,
39 DS_IRQ_DMA1 = 0x9,
40 DS_IRQ_DMA2 = 0xA,
41 DS_IRQ_DMA3 = 0xB,
42 DS_IRQ_KEYPAD = 0xC,
43 DS_IRQ_SLOT2 = 0xD,
44 DS_IRQ_IPC_SYNC = 0x10,
45 DS_IRQ_IPC_EMPTY = 0x11,
46 DS_IRQ_IPC_NOT_EMPTY = 0x12,
47 DS_IRQ_SLOT1_TRANS = 0x13,
48 DS_IRQ_SLOT1 = 0x14,
49 DS_IRQ_GEOM_FIFO = 0x15,
50 DS_IRQ_LID = 0x16,
51 DS_IRQ_SPI = 0x17,
52 DS_IRQ_WIFI = 0x18,
53};
54
55enum {
56 DS_CPU_BLOCK_DMA = 1,
57 DS_CPU_BLOCK_GX = 2
58};
59
60struct ARMCore;
61struct DS;
62struct Patch;
63struct VFile;
64struct mDebugger;
65
66mLOG_DECLARE_CATEGORY(DS);
67
68struct DSCommon {
69 struct DS* p;
70
71 struct ARMCore* cpu;
72 struct GBATimer timers[4];
73 struct mTiming timing;
74 int springIRQ;
75
76 struct DSCoreMemory memory;
77 struct DSCommon* ipc;
78
79 struct CircleBuffer fifo;
80};
81
82struct mCoreCallbacks;
83struct DS {
84 struct mCPUComponent d;
85
86 struct DSCommon ds7;
87 struct DSCommon ds9;
88 struct DSMemory memory;
89 struct DSVideo video;
90 struct DSAudio audio;
91 struct DSGX gx;
92 struct DSWifi wifi;
93 struct GBARTC rtc;
94
95 struct mCoreSync* sync;
96 struct mTimingEvent slice;
97 struct ARMCore* activeCpu;
98 uint32_t sliceStart;
99 int32_t cycleDrift;
100
101 struct ARMDebugger* debugger;
102
103 int cpuBlocked;
104 bool earlyExit;
105
106 uint32_t bios7Checksum;
107 uint32_t bios9Checksum;
108 int* keySource;
109 int* cursorSourceX;
110 int* cursorSourceY;
111 bool* touchSource;
112 struct mRTCSource* rtcSource;
113 struct mRumble* rumble;
114
115 struct VFile* romVf;
116 struct VFile* bios7Vf;
117 struct VFile* bios9Vf;
118 struct VFile* firmwareVf;
119
120 struct mAVStream* stream;
121 struct mKeyCallback* keyCallback;
122 struct mCoreCallbacksList coreCallbacks;
123
124 struct mTimingEvent divEvent;
125 struct mTimingEvent sqrtEvent;
126};
127
128struct DSCartridge {
129 char title[12];
130 uint32_t id;
131
132 uint16_t maker;
133 uint8_t type;
134 uint8_t encryptionSeed;
135 uint8_t size;
136 uint8_t reserved[8];
137 uint8_t region;
138 uint8_t version;
139 uint8_t autostart;
140 uint32_t arm9Offset;
141 uint32_t arm9Entry;
142 uint32_t arm9Base;
143 uint32_t arm9Size;
144 uint32_t arm7Offset;
145 uint32_t arm7Entry;
146 uint32_t arm7Base;
147 uint32_t arm7Size;
148 uint32_t fntOffset;
149 uint32_t fntSize;
150 uint32_t fatOffset;
151 uint32_t fatSize;
152 uint32_t arm9FileOverlayOffset;
153 uint32_t arm9FileOverlaySize;
154 uint32_t arm7FileOverlayOffset;
155 uint32_t arm7FileOverlaySize;
156 uint32_t busTiming;
157 uint32_t busKEY1Timing;
158 uint32_t iconOffset;
159 uint16_t secureAreaCrc16;
160 uint16_t secureAreaDelay;
161 // TODO: Fill in more
162 // And ROM data...
163};
164
165void DSCreate(struct DS* ds);
166void DSDestroy(struct DS* ds);
167
168void DSRunLoop(struct DS* ds);
169void DS7Step(struct DS* ds);
170void DS9Step(struct DS* ds);
171
172void DSAttachDebugger(struct DS* ds, struct mDebugger* debugger);
173void DSDetachDebugger(struct DS* ds);
174
175bool DSLoadROM(struct DS* ds, struct VFile* vf);
176bool DSLoadSave(struct DS* ds, struct VFile* vf);
177void DSUnloadROM(struct DS* ds);
178void DSApplyPatch(struct DS* ds, struct Patch* patch);
179
180bool DSIsBIOS7(struct VFile* vf);
181bool DSIsBIOS9(struct VFile* vf);
182bool DSLoadBIOS(struct DS* ds, struct VFile* vf);
183
184bool DSIsFirmware(struct VFile* vf);
185bool DSLoadFirmware(struct DS* ds, struct VFile* vf);
186
187bool DSIsROM(struct VFile* vf);
188void DSGetGameCode(struct DS* ds, char* out);
189void DSGetGameTitle(struct DS* ds, char* out);
190
191void DSWriteIME(struct ARMCore* cpu, uint16_t* io, uint16_t value);
192void DSWriteIE(struct ARMCore* cpu, uint16_t* io, uint32_t value);
193void DSRaiseIRQ(struct ARMCore* cpu, uint16_t* io, enum DSIRQ irq);
194
195void DSFrameStarted(struct DS* ds);
196void DSFrameEnded(struct DS* ds);
197
198uint16_t DSWriteRTC(struct DS* ds, DSRegisterRTC value);
199
200CXX_GUARD_END
201
202#endif