all repos — mgba @ bdf8f73ba57d1d4738023bde436c38e9948f37ed

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