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