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