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 struct mRTCSource* rtcSource;
99 struct mRumble* rumble;
100
101 struct VFile* romVf;
102 struct VFile* bios7Vf;
103 struct VFile* bios9Vf;
104 struct VFile* firmwareVf;
105
106 struct mKeyCallback* keyCallback;
107 struct mCoreCallbacks* coreCallbacks;
108
109 struct mTimingEvent divEvent;
110 struct mTimingEvent sqrtEvent;
111};
112
113struct DSCartridge {
114 char title[12];
115 uint32_t id;
116
117 uint16_t maker;
118 uint8_t type;
119 uint8_t encryptionSeed;
120 uint8_t size;
121 uint8_t reserved[8];
122 uint8_t region;
123 uint8_t version;
124 uint8_t autostart;
125 uint32_t arm9Offset;
126 uint32_t arm9Entry;
127 uint32_t arm9Base;
128 uint32_t arm9Size;
129 uint32_t arm7Offset;
130 uint32_t arm7Entry;
131 uint32_t arm7Base;
132 uint32_t arm7Size;
133 uint32_t fntOffset;
134 uint32_t fntSize;
135 uint32_t fatOffset;
136 uint32_t fatSize;
137 uint32_t arm9FileOverlayOffset;
138 uint32_t arm9FileOverlaySize;
139 uint32_t arm7FileOverlayOffset;
140 uint32_t arm7FileOverlaySize;
141 uint32_t busTiming;
142 uint32_t busKEY1Timing;
143 uint32_t iconOffset;
144 uint16_t secureAreaCrc16;
145 uint16_t secureAreaDelay;
146 // TODO: Fill in more
147 // And ROM data...
148};
149
150void DSCreate(struct DS* ds);
151void DSDestroy(struct DS* ds);
152
153void DSRunLoop(struct DS* ds);
154void DS7Step(struct DS* ds);
155void DS9Step(struct DS* ds);
156
157void DSAttachDebugger(struct DS* ds, struct mDebugger* debugger);
158void DSDetachDebugger(struct DS* ds);
159
160bool DSLoadROM(struct DS* ds, struct VFile* vf);
161bool DSLoadSave(struct DS* ds, struct VFile* vf);
162void DSUnloadROM(struct DS* ds);
163void DSApplyPatch(struct DS* ds, struct Patch* patch);
164
165bool DSIsBIOS7(struct VFile* vf);
166bool DSIsBIOS9(struct VFile* vf);
167bool DSLoadBIOS(struct DS* ds, struct VFile* vf);
168
169bool DSIsFirmware(struct VFile* vf);
170bool DSLoadFirmware(struct DS* ds, struct VFile* vf);
171
172bool DSIsROM(struct VFile* vf);
173void DSGetGameCode(struct DS* ds, char* out);
174void DSGetGameTitle(struct DS* ds, char* out);
175
176void DSWriteIME(struct ARMCore* cpu, uint16_t* io, uint16_t value);
177void DSWriteIE(struct ARMCore* cpu, uint16_t* io, uint32_t value);
178void DSRaiseIRQ(struct ARMCore* cpu, uint16_t* io, enum DSIRQ irq);
179
180void DSFrameStarted(struct DS* ds);
181void DSFrameEnded(struct DS* ds);
182
183CXX_GUARD_END
184
185#endif