all repos — mgba @ ebac3c65ea1c9da9c76bb0acd1b9468cc76d03c1

mGBA Game Boy Advance Emulator

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