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