all repos — mgba @ e2807b39158b867d1226b211dc592d5d9a24dbba

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/cheats.h"
 10#include "gb/cli.h"
 11#include "gb/gb.h"
 12#include "gb/mbc.h"
 13#include "gb/overrides.h"
 14#include "gb/renderers/software.h"
 15#include "gb/serialize.h"
 16#include "lr35902/debugger/debugger.h"
 17#include "util/crc32.h"
 18#include "util/memory.h"
 19#include "util/patch.h"
 20#include "util/vfs.h"
 21
 22struct GBCore {
 23	struct mCore d;
 24	struct GBVideoSoftwareRenderer renderer;
 25	uint8_t keys;
 26	struct mCPUComponent* components[CPU_COMPONENT_MAX];
 27	const struct Configuration* overrides;
 28	struct mDebuggerPlatform* debuggerPlatform;
 29	struct mCheatDevice* cheatDevice;
 30};
 31
 32static bool _GBCoreInit(struct mCore* core) {
 33	struct GBCore* gbcore = (struct GBCore*) core;
 34
 35	struct LR35902Core* cpu = anonymousMemoryMap(sizeof(struct LR35902Core));
 36	struct GB* gb = anonymousMemoryMap(sizeof(struct GB));
 37	if (!cpu || !gb) {
 38		free(cpu);
 39		free(gb);
 40		return false;
 41	}
 42	core->cpu = cpu;
 43	core->board = gb;
 44	gbcore->overrides = NULL;
 45	gbcore->debuggerPlatform = NULL;
 46	gbcore->cheatDevice = NULL;
 47
 48	GBCreate(gb);
 49	memset(gbcore->components, 0, sizeof(gbcore->components));
 50	LR35902SetComponents(cpu, &gb->d, CPU_COMPONENT_MAX, gbcore->components);
 51	LR35902Init(cpu);
 52
 53	GBVideoSoftwareRendererCreate(&gbcore->renderer);
 54	gbcore->renderer.outputBuffer = NULL;
 55
 56	gbcore->keys = 0;
 57	gb->keySource = &gbcore->keys;
 58
 59#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 60	mDirectorySetInit(&core->dirs);
 61#endif
 62	
 63	return true;
 64}
 65
 66static void _GBCoreDeinit(struct mCore* core) {
 67	LR35902Deinit(core->cpu);
 68	GBDestroy(core->board);
 69	mappedMemoryFree(core->cpu, sizeof(struct LR35902Core));
 70	mappedMemoryFree(core->board, sizeof(struct GB));
 71#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 72	mDirectorySetDeinit(&core->dirs);
 73#endif
 74
 75	struct GBCore* gbcore = (struct GBCore*) core;
 76	free(gbcore->debuggerPlatform);
 77	if (gbcore->cheatDevice) {
 78		mCheatDeviceDestroy(gbcore->cheatDevice);
 79	}
 80	free(gbcore->cheatDevice);
 81	mCoreConfigFreeOpts(&core->opts);
 82	free(core);
 83}
 84
 85static enum mPlatform _GBCorePlatform(struct mCore* core) {
 86	UNUSED(core);
 87	return PLATFORM_GB;
 88}
 89
 90static void _GBCoreSetSync(struct mCore* core, struct mCoreSync* sync) {
 91	struct GB* gb = core->board;
 92	gb->sync = sync;
 93}
 94
 95static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
 96	UNUSED(config);
 97
 98	struct GB* gb = core->board;
 99	if (core->opts.mute) {
100		gb->audio.masterVolume = 0;
101	} else {
102		gb->audio.masterVolume = core->opts.volume;
103	}
104	gb->video.frameskip = core->opts.frameskip;
105
106#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
107	struct GBCore* gbcore = (struct GBCore*) core;
108	gbcore->overrides = mCoreConfigGetOverridesConst(config);
109#endif
110}
111
112static void _GBCoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
113	UNUSED(core);
114	*width = GB_VIDEO_HORIZONTAL_PIXELS;
115	*height = GB_VIDEO_VERTICAL_PIXELS;
116}
117
118static void _GBCoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
119	struct GBCore* gbcore = (struct GBCore*) core;
120	gbcore->renderer.outputBuffer = buffer;
121	gbcore->renderer.outputBufferStride = stride;
122}
123
124static void _GBCoreGetPixels(struct mCore* core, const void** buffer, size_t* stride) {
125	struct GBCore* gbcore = (struct GBCore*) core;
126	gbcore->renderer.d.getPixels(&gbcore->renderer.d, stride, buffer);
127}
128
129static void _GBCorePutPixels(struct mCore* core, const void* buffer, size_t stride) {
130	struct GBCore* gbcore = (struct GBCore*) core;
131	gbcore->renderer.d.putPixels(&gbcore->renderer.d, stride, buffer);
132}
133
134static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
135	struct GB* gb = core->board;
136	switch (ch) {
137	case 0:
138		return gb->audio.left;
139	case 1:
140		return gb->audio.right;
141	default:
142		return NULL;
143	}
144}
145
146static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
147	struct GB* gb = core->board;
148	GBAudioResizeBuffer(&gb->audio, samples);
149}
150
151static size_t _GBCoreGetAudioBufferSize(struct mCore* core) {
152	struct GB* gb = core->board;
153	return gb->audio.samples;
154}
155
156static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
157	struct GB* gb = core->board;
158	gb->stream = stream;
159	if (stream && stream->videoDimensionsChanged) {
160		stream->videoDimensionsChanged(stream, GB_VIDEO_HORIZONTAL_PIXELS, GB_VIDEO_VERTICAL_PIXELS);
161	}
162}
163
164static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
165	return GBLoadROM(core->board, vf);
166}
167
168static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
169	UNUSED(type);
170	GBLoadBIOS(core->board, vf);
171	return true;
172}
173
174static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
175	return GBLoadSave(core->board, vf);
176}
177
178static bool _GBCoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
179	struct GB* gb = core->board;
180	GBSavedataMask(gb, vf, false);
181	return true; // TODO: Return a real value
182}
183
184static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
185	if (!vf) {
186		return false;
187	}
188	struct Patch patch;
189	if (!loadPatch(vf, &patch)) {
190		return false;
191	}
192	GBApplyPatch(core->board, &patch);
193	return true;
194}
195
196static void _GBCoreUnloadROM(struct mCore* core) {
197	return GBUnloadROM(core->board);
198}
199
200static void _GBCoreReset(struct mCore* core) {
201	struct GBCore* gbcore = (struct GBCore*) core;
202	struct GB* gb = (struct GB*) core->board;
203	if (gbcore->renderer.outputBuffer) {
204		GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
205	}
206
207	struct GBCartridgeOverride override;
208	const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
209	if (cart) {
210		override.headerCrc32 = doCrc32(cart, sizeof(*cart));
211		if (GBOverrideFind(gbcore->overrides, &override)) {
212			GBOverrideApply(gb, &override);
213		}
214	}
215
216#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
217	struct VFile* bios = NULL;
218	if (core->opts.useBios) {
219		bool found = false;
220		if (core->opts.bios) {
221			bios = VFileOpen(core->opts.bios, O_RDONLY);
222			if (bios && GBIsBIOS(bios)) {
223				found = true;
224			} else if (bios) {
225				bios->close(bios);
226				bios = NULL;
227			}
228		}
229		if (!found) {
230			char path[PATH_MAX];
231			GBDetectModel(gb);
232			mCoreConfigDirectory(path, PATH_MAX);
233			switch (gb->model) {
234			case GB_MODEL_DMG:
235			case GB_MODEL_SGB: // TODO
236				strncat(path, PATH_SEP "gb_bios.bin", PATH_MAX - strlen(path));
237				break;
238			case GB_MODEL_CGB:
239			case GB_MODEL_AGB:
240				strncat(path, PATH_SEP "gbc_bios.bin", PATH_MAX - strlen(path));
241				break;
242			default:
243				break;
244			};
245			bios = VFileOpen(path, O_RDONLY);
246		}
247	}
248	if (bios) {
249		GBLoadBIOS(gb, bios);
250	}
251#endif
252
253	LR35902Reset(core->cpu);
254}
255
256static void _GBCoreRunFrame(struct mCore* core) {
257	struct GB* gb = core->board;
258	int32_t frameCounter = gb->video.frameCounter;
259	while (gb->video.frameCounter == frameCounter) {
260		LR35902Run(core->cpu);
261	}
262}
263
264static void _GBCoreRunLoop(struct mCore* core) {
265	LR35902Run(core->cpu);
266}
267
268static void _GBCoreStep(struct mCore* core) {
269	struct LR35902Core* cpu = core->cpu;
270	do {
271		LR35902Tick(cpu);
272	} while (cpu->executionState != LR35902_CORE_FETCH);
273}
274
275static size_t _GBCoreStateSize(struct mCore* core) {
276	UNUSED(core);
277	return sizeof(struct GBSerializedState);
278}
279
280static bool _GBCoreLoadState(struct mCore* core, const void* state) {
281	return GBDeserialize(core->board, state);
282}
283
284static bool _GBCoreSaveState(struct mCore* core, void* state) {
285	struct LR35902Core* cpu = core->cpu;
286	while (cpu->executionState != LR35902_CORE_FETCH) {
287		LR35902Tick(cpu);
288	}
289	GBSerialize(core->board, state);
290	return true;
291}
292
293static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
294	struct GBCore* gbcore = (struct GBCore*) core;
295	gbcore->keys = keys;
296}
297
298static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
299	struct GBCore* gbcore = (struct GBCore*) core;
300	gbcore->keys |= keys;
301}
302
303static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
304	struct GBCore* gbcore = (struct GBCore*) core;
305	gbcore->keys &= ~keys;
306}
307
308static int32_t _GBCoreFrameCounter(struct mCore* core) {
309	struct GB* gb = core->board;
310	return gb->video.frameCounter;
311}
312
313static int32_t _GBCoreFrameCycles(struct mCore* core) {
314	UNUSED(core);
315	return GB_VIDEO_TOTAL_LENGTH;
316}
317
318static int32_t _GBCoreFrequency(struct mCore* core) {
319	UNUSED(core);
320	// TODO: GB differences
321	return DMG_LR35902_FREQUENCY;
322}
323
324static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
325	GBGetGameTitle(core->board, title);
326}
327
328static void _GBCoreGetGameCode(struct mCore* core, char* title) {
329	GBGetGameCode(core->board, title);
330}
331
332static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
333	struct GB* gb = core->board;
334	gb->memory.rtc = rtc;
335}
336
337static void _GBCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
338	struct GB* gb = core->board;
339	gb->memory.rotation = rotation;
340}
341
342static void _GBCoreSetRumble(struct mCore* core, struct mRumble* rumble) {
343	struct GB* gb = core->board;
344	gb->memory.rumble = rumble;
345}
346
347static uint32_t _GBCoreBusRead8(struct mCore* core, uint32_t address) {
348	struct LR35902Core* cpu = core->cpu;
349	return cpu->memory.load8(cpu, address);
350}
351
352static uint32_t _GBCoreBusRead16(struct mCore* core, uint32_t address) {
353	struct LR35902Core* cpu = core->cpu;
354	return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8);
355}
356
357static uint32_t _GBCoreBusRead32(struct mCore* core, uint32_t address) {
358	struct LR35902Core* cpu = core->cpu;
359	return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8) |
360	       (cpu->memory.load8(cpu, address + 2) << 16) | (cpu->memory.load8(cpu, address + 3) << 24);
361}
362
363static void _GBCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
364	struct LR35902Core* cpu = core->cpu;
365	cpu->memory.store8(cpu, address, value);
366}
367
368static void _GBCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
369	struct LR35902Core* cpu = core->cpu;
370	cpu->memory.store8(cpu, address, value);
371	cpu->memory.store8(cpu, address + 1, value >> 8);
372}
373
374static void _GBCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
375	struct LR35902Core* cpu = core->cpu;
376	cpu->memory.store8(cpu, address, value);
377	cpu->memory.store8(cpu, address + 1, value >> 8);
378	cpu->memory.store8(cpu, address + 2, value >> 16);
379	cpu->memory.store8(cpu, address + 3, value >> 24);
380}
381
382static uint32_t _GBCoreRawRead8(struct mCore* core, uint32_t address, int segment) {
383	struct LR35902Core* cpu = core->cpu;
384	return GBView8(cpu, address, segment);
385}
386
387static uint32_t _GBCoreRawRead16(struct mCore* core, uint32_t address, int segment) {
388	struct LR35902Core* cpu = core->cpu;
389	return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8);
390}
391
392static uint32_t _GBCoreRawRead32(struct mCore* core, uint32_t address, int segment) {
393	struct LR35902Core* cpu = core->cpu;
394	return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8) |
395	       (GBView8(cpu, address + 2, segment) << 16) | (GBView8(cpu, address + 3, segment) << 24);
396}
397
398static void _GBCoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
399	struct LR35902Core* cpu = core->cpu;
400	GBPatch8(cpu, address, value, NULL, segment);
401}
402
403static void _GBCoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
404	struct LR35902Core* cpu = core->cpu;
405	GBPatch8(cpu, address, value, NULL, segment);
406	GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
407}
408
409static void _GBCoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
410	struct LR35902Core* cpu = core->cpu;
411	GBPatch8(cpu, address, value, NULL, segment);
412	GBPatch8(cpu, address + 1, value >> 8, NULL, segment);
413	GBPatch8(cpu, address + 2, value >> 16, NULL, segment);
414	GBPatch8(cpu, address + 3, value >> 24, NULL, segment);
415}
416
417static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
418	UNUSED(core);
419	switch (type) {
420#ifdef USE_CLI_DEBUGGER
421	case DEBUGGER_CLI:
422		return true;
423#endif
424	default:
425		return false;
426	}
427}
428
429static struct mDebuggerPlatform* _GBCoreDebuggerPlatform(struct mCore* core) {
430	struct GBCore* gbcore = (struct GBCore*) core;
431	if (!gbcore->debuggerPlatform) {
432		gbcore->debuggerPlatform = LR35902DebuggerPlatformCreate();
433	}
434	return gbcore->debuggerPlatform;
435}
436
437static struct CLIDebuggerSystem* _GBCoreCliDebuggerSystem(struct mCore* core) {
438#ifdef USE_CLI_DEBUGGER
439	return GBCLIDebuggerCreate(core);
440#else
441	UNUSED(core);
442	return NULL;
443#endif
444}
445
446static void _GBCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
447	struct LR35902Core* cpu = core->cpu;
448	if (core->debugger) {
449		LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
450	}
451	cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
452	LR35902HotplugAttach(cpu, CPU_COMPONENT_DEBUGGER);
453	core->debugger = debugger;
454}
455
456static void _GBCoreDetachDebugger(struct mCore* core) {
457	struct LR35902Core* cpu = core->cpu;
458	LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
459	cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
460	core->debugger = NULL;
461}
462
463static struct mCheatDevice* _GBCoreCheatDevice(struct mCore* core) {
464	struct GBCore* gbcore = (struct GBCore*) core;
465	if (!gbcore->cheatDevice) {
466		gbcore->cheatDevice = GBCheatDeviceCreate();
467		((struct LR35902Core*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbcore->cheatDevice->d;
468		LR35902HotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
469		gbcore->cheatDevice->p = core;
470	}
471	return gbcore->cheatDevice;
472}
473
474static size_t _GBCoreSavedataClone(struct mCore* core, void** sram) {
475	struct GB* gb = core->board;
476	struct VFile* vf = gb->sramVf;
477	if (vf) {
478		*sram = malloc(vf->size(vf));
479		vf->seek(vf, 0, SEEK_SET);
480		return vf->read(vf, *sram, vf->size(vf));
481	}
482	*sram = malloc(gb->sramSize);
483	memcpy(*sram, gb->memory.sram, gb->sramSize);
484	return gb->sramSize;
485}
486
487static bool _GBCoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
488	struct GB* gb = core->board;
489	if (!writeback) {
490		struct VFile* vf = VFileFromConstMemory(sram, size);
491		GBSavedataMask(gb, vf, true);
492		return true;
493	}
494	struct VFile* vf = gb->sramVf;
495	if (vf) {
496		vf->seek(vf, 0, SEEK_SET);
497		return vf->write(vf, sram, size) > 0;
498	}
499	if (size > 0x20000) {
500		size = 0x20000;
501	}
502	GBResizeSram(gb, size);
503	memcpy(gb->memory.sram, sram, size);
504	return true;
505}
506
507struct mCore* GBCoreCreate(void) {
508	struct GBCore* gbcore = malloc(sizeof(*gbcore));
509	struct mCore* core = &gbcore->d;
510	memset(&core->opts, 0, sizeof(core->opts));
511	core->cpu = NULL;
512	core->board = NULL;
513	core->debugger = NULL;
514	core->init = _GBCoreInit;
515	core->deinit = _GBCoreDeinit;
516	core->platform = _GBCorePlatform;
517	core->setSync = _GBCoreSetSync;
518	core->loadConfig = _GBCoreLoadConfig;
519	core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
520	core->setVideoBuffer = _GBCoreSetVideoBuffer;
521	core->getPixels = _GBCoreGetPixels;
522	core->putPixels = _GBCorePutPixels;
523	core->getAudioChannel = _GBCoreGetAudioChannel;
524	core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
525	core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
526	core->setAVStream = _GBCoreSetAVStream;
527	core->isROM = GBIsROM;
528	core->loadROM = _GBCoreLoadROM;
529	core->loadBIOS = _GBCoreLoadBIOS;
530	core->loadSave = _GBCoreLoadSave;
531	core->loadTemporarySave = _GBCoreLoadTemporarySave;
532	core->loadPatch = _GBCoreLoadPatch;
533	core->unloadROM = _GBCoreUnloadROM;
534	core->reset = _GBCoreReset;
535	core->runFrame = _GBCoreRunFrame;
536	core->runLoop = _GBCoreRunLoop;
537	core->step = _GBCoreStep;
538	core->stateSize = _GBCoreStateSize;
539	core->loadState = _GBCoreLoadState;
540	core->saveState = _GBCoreSaveState;
541	core->setKeys = _GBCoreSetKeys;
542	core->addKeys = _GBCoreAddKeys;
543	core->clearKeys = _GBCoreClearKeys;
544	core->frameCounter = _GBCoreFrameCounter;
545	core->frameCycles = _GBCoreFrameCycles;
546	core->frequency = _GBCoreFrequency;
547	core->getGameTitle = _GBCoreGetGameTitle;
548	core->getGameCode = _GBCoreGetGameCode;
549	core->setRTC = _GBCoreSetRTC;
550	core->setRotation = _GBCoreSetRotation;
551	core->setRumble = _GBCoreSetRumble;
552	core->busRead8 = _GBCoreBusRead8;
553	core->busRead16 = _GBCoreBusRead16;
554	core->busRead32 = _GBCoreBusRead32;
555	core->busWrite8 = _GBCoreBusWrite8;
556	core->busWrite16 = _GBCoreBusWrite16;
557	core->busWrite32 = _GBCoreBusWrite32;
558	core->rawRead8 = _GBCoreRawRead8;
559	core->rawRead16 = _GBCoreRawRead16;
560	core->rawRead32 = _GBCoreRawRead32;
561	core->rawWrite8 = _GBCoreRawWrite8;
562	core->rawWrite16 = _GBCoreRawWrite16;
563	core->rawWrite32 = _GBCoreRawWrite32;
564	core->supportsDebuggerType = _GBCoreSupportsDebuggerType;
565	core->debuggerPlatform = _GBCoreDebuggerPlatform;
566	core->cliDebuggerSystem = _GBCoreCliDebuggerSystem;
567	core->attachDebugger = _GBCoreAttachDebugger;
568	core->detachDebugger = _GBCoreDetachDebugger;
569	core->cheatDevice = _GBCoreCheatDevice;
570	core->savedataClone = _GBCoreSavedataClone;
571	core->savedataRestore = _GBCoreSavedataRestore;
572	return core;
573}