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 DS {
73 struct mCPUComponent d;
74
75 struct DSCommon ds7;
76 struct DSCommon ds9;
77 struct DSMemory memory;
78 struct DSVideo video;
79
80 struct mCoreSync* sync;
81 struct mTimingEvent slice;
82 struct ARMCore* activeCpu;
83 uint32_t sliceStart;
84 int32_t cycleDrift;
85
86 struct ARMDebugger* debugger;
87
88 bool cpuBlocked;
89 bool earlyExit;
90
91 uint32_t bios7Checksum;
92 uint32_t bios9Checksum;
93 int* keySource;
94 struct mRTCSource* rtcSource;
95 struct mRumble* rumble;
96
97 struct VFile* romVf;
98 struct VFile* bios7Vf;
99 struct VFile* bios9Vf;
100
101 struct mKeyCallback* keyCallback;
102};
103
104struct DSCartridge {
105 char title[12];
106 uint32_t id;
107
108 uint16_t maker;
109 uint8_t type;
110 uint8_t encryptionSeed;
111 uint8_t size;
112 uint8_t reserved[8];
113 uint8_t region;
114 uint8_t version;
115 uint8_t autostart;
116 uint32_t arm9Offset;
117 uint32_t arm9Entry;
118 uint32_t arm9Base;
119 uint32_t arm9Size;
120 uint32_t arm7Offset;
121 uint32_t arm7Entry;
122 uint32_t arm7Base;
123 uint32_t arm7Size;
124 uint32_t fntOffset;
125 uint32_t fntSize;
126 uint32_t fatOffset;
127 uint32_t fatSize;
128 uint32_t arm9FileOverlayOffset;
129 uint32_t arm9FileOverlaySize;
130 uint32_t arm7FileOverlayOffset;
131 uint32_t arm7FileOverlaySize;
132 uint32_t busTiming;
133 uint32_t busKEY1Timing;
134 uint32_t iconOffset;
135 uint16_t secureAreaCrc16;
136 uint16_t secureAreaDelay;
137 // TODO: Fill in more
138 // And ROM data...
139};
140
141void DSCreate(struct DS* ds);
142void DSDestroy(struct DS* ds);
143
144void DSRunLoop(struct DS* ds);
145void DS7Step(struct DS* ds);
146void DS9Step(struct DS* ds);
147
148void DSAttachDebugger(struct DS* ds, struct mDebugger* debugger);
149void DSDetachDebugger(struct DS* ds);
150
151bool DSLoadROM(struct DS* ds, struct VFile* vf);
152void DSUnloadROM(struct DS* ds);
153void DSApplyPatch(struct DS* ds, struct Patch* patch);
154
155bool DSLoadBIOS(struct DS* ds, struct VFile* vf);
156
157bool DSIsROM(struct VFile* vf);
158void DSGetGameCode(struct DS* ds, char* out);
159void DSGetGameTitle(struct DS* ds, char* out);
160
161void DSWriteIME(struct ARMCore* cpu, uint16_t* io, uint16_t value);
162void DSWriteIE(struct ARMCore* cpu, uint16_t* io, uint32_t value);
163void DSRaiseIRQ(struct ARMCore* cpu, uint16_t* io, enum DSIRQ irq);
164
165CXX_GUARD_END
166
167#endif