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
52struct ARMCore;
53struct DS;
54struct Patch;
55struct VFile;
56struct mDebugger;
57
58mLOG_DECLARE_CATEGORY(DS);
59
60struct DSCommon {
61 struct DS* p;
62
63 struct ARMCore* cpu;
64 struct GBATimer timers[4];
65 struct mTiming timing;
66 int springIRQ;
67
68 struct DSCoreMemory memory;
69 struct DSCommon* ipc;
70
71 struct CircleBuffer fifo;
72};
73
74struct mCoreCallbacks;
75struct DS {
76 struct mCPUComponent d;
77
78 struct DSCommon ds7;
79 struct DSCommon ds9;
80 struct DSMemory memory;
81 struct DSVideo video;
82 struct DSGX gx;
83
84 struct mCoreSync* sync;
85 struct mTimingEvent slice;
86 struct ARMCore* activeCpu;
87 uint32_t sliceStart;
88 int32_t cycleDrift;
89
90 struct ARMDebugger* debugger;
91
92 bool cpuBlocked;
93 bool earlyExit;
94
95 uint32_t bios7Checksum;
96 uint32_t bios9Checksum;
97 int* keySource;
98 int* cursorSourceX;
99 int* cursorSourceY;
100 bool* touchSource;
101 struct mRTCSource* rtcSource;
102 struct mRumble* rumble;
103
104 struct VFile* romVf;
105 struct VFile* bios7Vf;
106 struct VFile* bios9Vf;
107 struct VFile* firmwareVf;
108
109 struct mKeyCallback* keyCallback;
110 struct mCoreCallbacks* coreCallbacks;
111
112 struct mTimingEvent divEvent;
113 struct mTimingEvent sqrtEvent;
114};
115
116struct DSCartridge {
117 char title[12];
118 uint32_t id;
119
120 uint16_t maker;
121 uint8_t type;
122 uint8_t encryptionSeed;
123 uint8_t size;
124 uint8_t reserved[8];
125 uint8_t region;
126 uint8_t version;
127 uint8_t autostart;
128 uint32_t arm9Offset;
129 uint32_t arm9Entry;
130 uint32_t arm9Base;
131 uint32_t arm9Size;
132 uint32_t arm7Offset;
133 uint32_t arm7Entry;
134 uint32_t arm7Base;
135 uint32_t arm7Size;
136 uint32_t fntOffset;
137 uint32_t fntSize;
138 uint32_t fatOffset;
139 uint32_t fatSize;
140 uint32_t arm9FileOverlayOffset;
141 uint32_t arm9FileOverlaySize;
142 uint32_t arm7FileOverlayOffset;
143 uint32_t arm7FileOverlaySize;
144 uint32_t busTiming;
145 uint32_t busKEY1Timing;
146 uint32_t iconOffset;
147 uint16_t secureAreaCrc16;
148 uint16_t secureAreaDelay;
149 // TODO: Fill in more
150 // And ROM data...
151};
152
153void DSCreate(struct DS* ds);
154void DSDestroy(struct DS* ds);
155
156void DSRunLoop(struct DS* ds);
157void DS7Step(struct DS* ds);
158void DS9Step(struct DS* ds);
159
160void DSAttachDebugger(struct DS* ds, struct mDebugger* debugger);
161void DSDetachDebugger(struct DS* ds);
162
163bool DSLoadROM(struct DS* ds, struct VFile* vf);
164bool DSLoadSave(struct DS* ds, struct VFile* vf);
165void DSUnloadROM(struct DS* ds);
166void DSApplyPatch(struct DS* ds, struct Patch* patch);
167
168bool DSIsBIOS7(struct VFile* vf);
169bool DSIsBIOS9(struct VFile* vf);
170bool DSLoadBIOS(struct DS* ds, struct VFile* vf);
171
172bool DSIsFirmware(struct VFile* vf);
173bool DSLoadFirmware(struct DS* ds, struct VFile* vf);
174
175bool DSIsROM(struct VFile* vf);
176void DSGetGameCode(struct DS* ds, char* out);
177void DSGetGameTitle(struct DS* ds, char* out);
178
179void DSWriteIME(struct ARMCore* cpu, uint16_t* io, uint16_t value);
180void DSWriteIE(struct ARMCore* cpu, uint16_t* io, uint32_t value);
181void DSRaiseIRQ(struct ARMCore* cpu, uint16_t* io, enum DSIRQ irq);
182
183void DSFrameStarted(struct DS* ds);
184void DSFrameEnded(struct DS* ds);
185
186CXX_GUARD_END
187
188#endif