all repos — mgba @ cd0a352a33d1613196ad5c3fa7e2af055e0fbecc

mGBA Game Boy Advance Emulator

src/gb/core.c (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#include "core.h"
  7
  8#include "core/core.h"
  9#include "gb/gb.h"
 10#include "gb/renderers/software.h"
 11#include "util/memory.h"
 12#include "util/patch.h"
 13
 14struct GBCore {
 15	struct mCore d;
 16	struct GBVideoSoftwareRenderer renderer;
 17	uint8_t keys;
 18};
 19
 20static bool _GBCoreInit(struct mCore* core) {
 21	struct GBCore* gbcore = (struct GBCore*) core;
 22
 23	struct LR35902Core* cpu = anonymousMemoryMap(sizeof(struct LR35902Core));
 24	struct GB* gb = anonymousMemoryMap(sizeof(struct GB));
 25	if (!cpu || !gb) {
 26		free(cpu);
 27		free(gb);
 28		return false;
 29	}
 30	core->cpu = cpu;
 31	core->board = gb;
 32
 33	GBCreate(gb);
 34	LR35902SetComponents(cpu, &gb->d, 0, 0);
 35	LR35902Init(cpu);
 36
 37	GBVideoSoftwareRendererCreate(&gbcore->renderer);
 38
 39	gbcore->keys = 0;
 40	gb->keySource = &gbcore->keys;
 41
 42#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 43	mDirectorySetInit(&core->dirs);
 44#endif
 45	
 46	return true;
 47}
 48
 49static void _GBCoreDeinit(struct mCore* core) {
 50	LR35902Deinit(core->cpu);
 51	GBDestroy(core->board);
 52	mappedMemoryFree(core->cpu, sizeof(struct LR35902Core));
 53	mappedMemoryFree(core->board, sizeof(struct GB));
 54#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 55	mDirectorySetDeinit(&core->dirs);
 56#endif
 57}
 58
 59static enum mPlatform _GBCorePlatform(struct mCore* core) {
 60	UNUSED(core);
 61	return PLATFORM_GB;
 62}
 63
 64static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
 65	struct GB* gb = core->board;
 66	gb->sync = sync;
 67}
 68
 69static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
 70	UNUSED(config);
 71
 72	struct GB* gb = core->board;
 73	gb->audio.masterVolume = core->opts.volume;
 74	gb->video.frameskip = core->opts.frameskip;
 75}
 76
 77static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
 78	UNUSED(core);
 79	*width = GB_VIDEO_HORIZONTAL_PIXELS;
 80	*height = GB_VIDEO_VERTICAL_PIXELS;
 81}
 82
 83static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
 84	struct GBCore* gbcore = (struct GBCore*) core;
 85	gbcore->renderer.outputBuffer = buffer;
 86	gbcore->renderer.outputBufferStride = stride;
 87}
 88
 89static void _GBCoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
 90	struct GBCore* gbcore = (struct GBCore*) core;
 91	*buffer = gbcore->renderer.outputBuffer;
 92	*stride = gbcore->renderer.outputBufferStride;
 93}
 94
 95static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
 96	struct GB* gb = core->board;
 97	switch (ch) {
 98	case 0:
 99		return gb->audio.left;
100	case 1:
101		return gb->audio.right;
102	default:
103		return NULL;
104	}
105}
106
107static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
108	struct GB* gb = core->board;
109	GBAudioResizeBuffer(&gb->audio, samples);
110}
111
112static size_t _GBCoreGetAudioBufferSize(struct mCore* core) {
113	struct GB* gb = core->board;
114	return gb->audio.samples;
115}
116
117static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
118	// TODO
119}
120
121static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
122	return GBLoadROM(core->board, vf);
123}
124
125static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
126	UNUSED(core);
127	UNUSED(vf);
128	UNUSED(type);
129	// TODO
130	return false;
131}
132
133static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
134	return GBLoadSave(core->board, vf);
135}
136
137static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
138	if (!vf) {
139		return false;
140	}
141	struct Patch patch;
142	if (!loadPatch(vf, &patch)) {
143		return false;
144	}
145	GBApplyPatch(core->board, &patch);
146	return true;
147}
148
149static void _GBCoreUnloadROM(struct mCore* core) {
150	return GBUnloadROM(core->board);
151}
152
153static void _GBCoreReset(struct mCore* core) {
154	struct GBCore* gbcore = (struct GBCore*) core;
155	struct GB* gb = (struct GB*) core->board;
156	if (gbcore->renderer.outputBuffer) {
157		GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
158	}
159	LR35902Reset(core->cpu);
160}
161
162static void _GBCoreRunFrame(struct mCore* core) {
163	struct GB* gb = core->board;
164	int32_t frameCounter = gb->video.frameCounter;
165	while (gb->video.frameCounter == frameCounter) {
166		LR35902Run(core->cpu);
167	}
168}
169
170static void _GBCoreRunLoop(struct mCore* core) {
171	LR35902Run(core->cpu);
172}
173
174static void _GBCoreStep(struct mCore* core) {
175	LR35902Tick(core->cpu);
176}
177
178static bool _GBCoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
179	UNUSED(core);
180	UNUSED(vf);
181	UNUSED(flags);
182	// TODO
183	return false;
184}
185
186static bool _GBCoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
187	UNUSED(core);
188	UNUSED(vf);
189	UNUSED(flags);
190	// TODO
191	return false;
192}
193
194static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
195	struct GBCore* gbcore = (struct GBCore*) core;
196	gbcore->keys = keys;
197}
198
199static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
200	struct GBCore* gbcore = (struct GBCore*) core;
201	gbcore->keys |= keys;
202}
203
204static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
205	struct GBCore* gbcore = (struct GBCore*) core;
206	gbcore->keys &= ~keys;
207}
208
209static int32_t _GBCoreFrameCounter(struct mCore* core) {
210	struct GB* gb = core->board;
211	return gb->video.frameCounter;
212}
213
214static int32_t _GBCoreFrameCycles(struct mCore* core) {
215	UNUSED(core);
216	return GB_VIDEO_TOTAL_LENGTH;
217}
218
219static int32_t _GBCoreFrequency(struct mCore* core) {
220	UNUSED(core);
221	// TODO: GB differences
222	return DMG_LR35902_FREQUENCY;
223}
224
225static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
226	GBGetGameTitle(core->board, title);
227}
228
229static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
230	struct GB* gb = core->board;
231	gb->memory.rtc = rtc;
232}
233
234struct mCore* GBCoreCreate(void) {
235	struct GBCore* gbcore = malloc(sizeof(*gbcore));
236	struct mCore* core = &gbcore->d;
237	memset(&core->opts, 0, sizeof(core->opts));
238	core->cpu = 0;
239	core->board = 0;
240	core->init = _GBCoreInit;
241	core->deinit = _GBCoreDeinit;
242	core->platform = _GBCorePlatform;
243	core->setSync = _GBCoreSetSync;
244	core->loadConfig = _GBCoreLoadConfig;
245	core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
246	core->setVideoBuffer = _GBCoreSetVideoBuffer;
247	core->getVideoBuffer = _GBCoreGetVideoBuffer;
248	core->getAudioChannel = _GBCoreGetAudioChannel;
249	core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
250	core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
251	core->setAVStream = _GBCoreSetAVStream;
252	core->isROM = GBIsROM;
253	core->loadROM = _GBCoreLoadROM;
254	core->loadBIOS = _GBCoreLoadBIOS;
255	core->loadSave = _GBCoreLoadSave;
256	core->loadPatch = _GBCoreLoadPatch;
257	core->unloadROM = _GBCoreUnloadROM;
258	core->reset = _GBCoreReset;
259	core->runFrame = _GBCoreRunFrame;
260	core->runLoop = _GBCoreRunLoop;
261	core->step = _GBCoreStep;
262	core->loadState = _GBCoreLoadState;
263	core->saveState = _GBCoreSaveState;
264	core->setKeys = _GBCoreSetKeys;
265	core->addKeys = _GBCoreAddKeys;
266	core->clearKeys = _GBCoreClearKeys;
267	core->frameCounter = _GBCoreFrameCounter;
268	core->frameCycles = _GBCoreFrameCycles;
269	core->frequency = _GBCoreFrequency;
270	core->getGameTitle = _GBCoreGetGameTitle;
271	core->setRTC = _GBCoreSetRTC;
272	return core;
273}