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