all repos — mgba @ c62d913e233e7ea3bb23a3f52fcb7b481f2faed5

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