all repos — mgba @ 4c38f769565e8ddd7d3a8eef1a41975206c129a0

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 _GBCoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
125	struct GBCore* gbcore = (struct GBCore*) core;
126	*buffer = gbcore->renderer.outputBuffer;
127	*stride = gbcore->renderer.outputBufferStride;
128}
129
130static struct blip_t* _GBCoreGetAudioChannel(struct mCore* core, int ch) {
131	struct GB* gb = core->board;
132	switch (ch) {
133	case 0:
134		return gb->audio.left;
135	case 1:
136		return gb->audio.right;
137	default:
138		return NULL;
139	}
140}
141
142static void _GBCoreSetAudioBufferSize(struct mCore* core, size_t samples) {
143	struct GB* gb = core->board;
144	GBAudioResizeBuffer(&gb->audio, samples);
145}
146
147static size_t _GBCoreGetAudioBufferSize(struct mCore* core) {
148	struct GB* gb = core->board;
149	return gb->audio.samples;
150}
151
152static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
153	struct GB* gb = core->board;
154	gb->stream = stream;
155	if (stream && stream->videoDimensionsChanged) {
156		stream->videoDimensionsChanged(stream, GB_VIDEO_HORIZONTAL_PIXELS, GB_VIDEO_VERTICAL_PIXELS);
157	}
158}
159
160static bool _GBCoreLoadROM(struct mCore* core, struct VFile* vf) {
161	return GBLoadROM(core->board, vf);
162}
163
164static bool _GBCoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
165	UNUSED(type);
166	GBLoadBIOS(core->board, vf);
167	return true;
168}
169
170static bool _GBCoreLoadSave(struct mCore* core, struct VFile* vf) {
171	return GBLoadSave(core->board, vf);
172}
173
174static bool _GBCoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
175	struct GB* gb = core->board;
176	GBSavedataMask(gb, vf);
177	return true; // TODO: Return a real value
178}
179
180static bool _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
181	if (!vf) {
182		return false;
183	}
184	struct Patch patch;
185	if (!loadPatch(vf, &patch)) {
186		return false;
187	}
188	GBApplyPatch(core->board, &patch);
189	return true;
190}
191
192static void _GBCoreUnloadROM(struct mCore* core) {
193	return GBUnloadROM(core->board);
194}
195
196static void _GBCoreReset(struct mCore* core) {
197	struct GBCore* gbcore = (struct GBCore*) core;
198	struct GB* gb = (struct GB*) core->board;
199	if (gbcore->renderer.outputBuffer) {
200		GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
201	}
202
203	struct GBCartridgeOverride override;
204	const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100];
205	if (cart) {
206		override.headerCrc32 = doCrc32(cart, sizeof(*cart));
207		if (GBOverrideFind(gbcore->overrides, &override)) {
208			GBOverrideApply(gb, &override);
209		}
210	}
211
212#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
213	struct VFile* bios = 0;
214	if (core->opts.useBios) {
215		if (!core->opts.bios) {
216			char path[PATH_MAX];
217			GBDetectModel(gb);
218			mCoreConfigDirectory(path, PATH_MAX);
219			switch (gb->model) {
220			case GB_MODEL_DMG:
221			case GB_MODEL_SGB: // TODO
222				strncat(path, PATH_SEP "gb_bios.bin", PATH_MAX - strlen(path));
223				break;
224			case GB_MODEL_CGB:
225			case GB_MODEL_AGB:
226				strncat(path, PATH_SEP "gbc_bios.bin", PATH_MAX - strlen(path));
227				break;
228			default:
229				break;
230			};
231			bios = VFileOpen(path, O_RDONLY);
232		} else {
233			bios = VFileOpen(core->opts.bios, O_RDONLY);
234		}
235	}
236	if (bios) {
237		GBLoadBIOS(gb, bios);
238	}
239#endif
240
241	LR35902Reset(core->cpu);
242}
243
244static void _GBCoreRunFrame(struct mCore* core) {
245	struct GB* gb = core->board;
246	int32_t frameCounter = gb->video.frameCounter;
247	while (gb->video.frameCounter == frameCounter) {
248		LR35902Run(core->cpu);
249	}
250}
251
252static void _GBCoreRunLoop(struct mCore* core) {
253	LR35902Run(core->cpu);
254}
255
256static void _GBCoreStep(struct mCore* core) {
257	struct LR35902Core* cpu = core->cpu;
258	do {
259		LR35902Tick(cpu);
260	} while (cpu->executionState != LR35902_CORE_FETCH);
261}
262
263static size_t _GBCoreStateSize(struct mCore* core) {
264	UNUSED(core);
265	return sizeof(struct GBSerializedState);
266}
267
268static bool _GBCoreLoadState(struct mCore* core, const void* state) {
269	return GBDeserialize(core->board, state);
270}
271
272static bool _GBCoreSaveState(struct mCore* core, void* state) {
273	struct LR35902Core* cpu = core->cpu;
274	while (cpu->executionState != LR35902_CORE_FETCH) {
275		LR35902Tick(cpu);
276	}
277	GBSerialize(core->board, state);
278	return true;
279}
280
281static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
282	struct GBCore* gbcore = (struct GBCore*) core;
283	gbcore->keys = keys;
284}
285
286static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
287	struct GBCore* gbcore = (struct GBCore*) core;
288	gbcore->keys |= keys;
289}
290
291static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
292	struct GBCore* gbcore = (struct GBCore*) core;
293	gbcore->keys &= ~keys;
294}
295
296static int32_t _GBCoreFrameCounter(struct mCore* core) {
297	struct GB* gb = core->board;
298	return gb->video.frameCounter;
299}
300
301static int32_t _GBCoreFrameCycles(struct mCore* core) {
302	UNUSED(core);
303	return GB_VIDEO_TOTAL_LENGTH;
304}
305
306static int32_t _GBCoreFrequency(struct mCore* core) {
307	UNUSED(core);
308	// TODO: GB differences
309	return DMG_LR35902_FREQUENCY;
310}
311
312static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
313	GBGetGameTitle(core->board, title);
314}
315
316static void _GBCoreGetGameCode(struct mCore* core, char* title) {
317	GBGetGameCode(core->board, title);
318}
319
320static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
321	struct GB* gb = core->board;
322	gb->memory.rtc = rtc;
323}
324
325static void _GBCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
326	struct GB* gb = core->board;
327	gb->memory.rotation = rotation;
328}
329
330static void _GBCoreSetRumble(struct mCore* core, struct mRumble* rumble) {
331	struct GB* gb = core->board;
332	gb->memory.rumble = rumble;
333}
334
335static uint32_t _GBCoreBusRead8(struct mCore* core, uint32_t address) {
336	struct LR35902Core* cpu = core->cpu;
337	return cpu->memory.load8(cpu, address);
338}
339
340static uint32_t _GBCoreBusRead16(struct mCore* core, uint32_t address) {
341	struct LR35902Core* cpu = core->cpu;
342	return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8);
343}
344
345static uint32_t _GBCoreBusRead32(struct mCore* core, uint32_t address) {
346	struct LR35902Core* cpu = core->cpu;
347	return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8) |
348	       (cpu->memory.load8(cpu, address + 2) << 16) | (cpu->memory.load8(cpu, address + 3) << 24);
349}
350
351static void _GBCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
352	struct LR35902Core* cpu = core->cpu;
353	cpu->memory.store8(cpu, address, value);
354}
355
356static void _GBCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
357	struct LR35902Core* cpu = core->cpu;
358	cpu->memory.store8(cpu, address, value);
359	cpu->memory.store8(cpu, address + 1, value >> 8);
360}
361
362static void _GBCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
363	struct LR35902Core* cpu = core->cpu;
364	cpu->memory.store8(cpu, address, value);
365	cpu->memory.store8(cpu, address + 1, value >> 8);
366	cpu->memory.store8(cpu, address + 2, value >> 16);
367	cpu->memory.store8(cpu, address + 3, value >> 24);
368}
369
370static uint32_t _GBCoreRawRead8(struct mCore* core, uint32_t address, int segment) {
371	struct LR35902Core* cpu = core->cpu;
372	return GBView8(cpu, address, segment);
373}
374
375static uint32_t _GBCoreRawRead16(struct mCore* core, uint32_t address, int segment) {
376	struct LR35902Core* cpu = core->cpu;
377	return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8);
378}
379
380static uint32_t _GBCoreRawRead32(struct mCore* core, uint32_t address, int segment) {
381	struct LR35902Core* cpu = core->cpu;
382	return GBView8(cpu, address, segment) | (GBView8(cpu, address + 1, segment) << 8) |
383	       (GBView8(cpu, address + 2, segment) << 16) | (GBView8(cpu, address + 3, segment) << 24);
384}
385
386static void _GBCoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
387	struct LR35902Core* cpu = core->cpu;
388	GBPatch8(cpu, address, value, NULL);
389}
390
391static void _GBCoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
392	struct LR35902Core* cpu = core->cpu;
393	GBPatch8(cpu, address, value, NULL);
394	GBPatch8(cpu, address + 1, value >> 8, NULL);
395}
396
397static void _GBCoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
398	struct LR35902Core* cpu = core->cpu;
399	GBPatch8(cpu, address, value, NULL);
400	GBPatch8(cpu, address + 1, value >> 8, NULL);
401	GBPatch8(cpu, address + 2, value >> 16, NULL);
402	GBPatch8(cpu, address + 3, value >> 24, NULL);
403}
404
405static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
406	UNUSED(core);
407	switch (type) {
408#ifdef USE_CLI_DEBUGGER
409	case DEBUGGER_CLI:
410		return true;
411#endif
412	default:
413		return false;
414	}
415}
416
417static struct mDebuggerPlatform* _GBCoreDebuggerPlatform(struct mCore* core) {
418	struct GBCore* gbcore = (struct GBCore*) core;
419	if (!gbcore->debuggerPlatform) {
420		gbcore->debuggerPlatform = LR35902DebuggerPlatformCreate();
421	}
422	return gbcore->debuggerPlatform;
423}
424
425static struct CLIDebuggerSystem* _GBCoreCliDebuggerSystem(struct mCore* core) {
426#ifdef USE_CLI_DEBUGGER
427	return GBCLIDebuggerCreate(core);
428#else
429	UNUSED(core);
430	return NULL;
431#endif
432}
433
434static void _GBCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
435	struct LR35902Core* cpu = core->cpu;
436	if (core->debugger) {
437		LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
438	}
439	cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
440	LR35902HotplugAttach(cpu, CPU_COMPONENT_DEBUGGER);
441	core->debugger = debugger;
442}
443
444static void _GBCoreDetachDebugger(struct mCore* core) {
445	struct LR35902Core* cpu = core->cpu;
446	LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
447	cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
448	core->debugger = NULL;
449}
450
451static struct mCheatDevice* _GBCoreCheatDevice(struct mCore* core) {
452	struct GBCore* gbcore = (struct GBCore*) core;
453	if (!gbcore->cheatDevice) {
454		gbcore->cheatDevice = GBCheatDeviceCreate();
455		((struct LR35902Core*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbcore->cheatDevice->d;
456		LR35902HotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
457		gbcore->cheatDevice->p = core;
458	}
459	return gbcore->cheatDevice;
460}
461
462static size_t _GBCoreSavedataClone(struct mCore* core, void** sram) {
463	struct GB* gb = core->board;
464	struct VFile* vf = gb->sramVf;
465	if (vf) {
466		*sram = malloc(vf->size(vf));
467		vf->seek(vf, 0, SEEK_SET);
468		return vf->read(vf, *sram, vf->size(vf));
469	}
470	*sram = malloc(gb->sramSize);
471	memcpy(*sram, gb->memory.sram, gb->sramSize);
472	return gb->sramSize;
473}
474
475static bool _GBCoreSavedataLoad(struct mCore* core, const void* sram, size_t size) {
476	struct GB* gb = core->board;
477	struct VFile* vf = gb->sramVf;
478	if (vf) {
479		vf->seek(vf, 0, SEEK_SET);
480		return vf->write(vf, sram, size) > 0;
481	}
482	if (size > 0x20000) {
483		size = 0x20000;
484	}
485	GBResizeSram(gb, size);
486	memcpy(gb->memory.sram, sram, size);
487	return true;
488}
489
490struct mCore* GBCoreCreate(void) {
491	struct GBCore* gbcore = malloc(sizeof(*gbcore));
492	struct mCore* core = &gbcore->d;
493	memset(&core->opts, 0, sizeof(core->opts));
494	core->cpu = NULL;
495	core->board = NULL;
496	core->debugger = NULL;
497	core->init = _GBCoreInit;
498	core->deinit = _GBCoreDeinit;
499	core->platform = _GBCorePlatform;
500	core->setSync = _GBCoreSetSync;
501	core->loadConfig = _GBCoreLoadConfig;
502	core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
503	core->setVideoBuffer = _GBCoreSetVideoBuffer;
504	core->getVideoBuffer = _GBCoreGetVideoBuffer;
505	core->getAudioChannel = _GBCoreGetAudioChannel;
506	core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
507	core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
508	core->setAVStream = _GBCoreSetAVStream;
509	core->isROM = GBIsROM;
510	core->loadROM = _GBCoreLoadROM;
511	core->loadBIOS = _GBCoreLoadBIOS;
512	core->loadSave = _GBCoreLoadSave;
513	core->loadTemporarySave = _GBCoreLoadTemporarySave;
514	core->loadPatch = _GBCoreLoadPatch;
515	core->unloadROM = _GBCoreUnloadROM;
516	core->reset = _GBCoreReset;
517	core->runFrame = _GBCoreRunFrame;
518	core->runLoop = _GBCoreRunLoop;
519	core->step = _GBCoreStep;
520	core->stateSize = _GBCoreStateSize;
521	core->loadState = _GBCoreLoadState;
522	core->saveState = _GBCoreSaveState;
523	core->setKeys = _GBCoreSetKeys;
524	core->addKeys = _GBCoreAddKeys;
525	core->clearKeys = _GBCoreClearKeys;
526	core->frameCounter = _GBCoreFrameCounter;
527	core->frameCycles = _GBCoreFrameCycles;
528	core->frequency = _GBCoreFrequency;
529	core->getGameTitle = _GBCoreGetGameTitle;
530	core->getGameCode = _GBCoreGetGameCode;
531	core->setRTC = _GBCoreSetRTC;
532	core->setRotation = _GBCoreSetRotation;
533	core->setRumble = _GBCoreSetRumble;
534	core->busRead8 = _GBCoreBusRead8;
535	core->busRead16 = _GBCoreBusRead16;
536	core->busRead32 = _GBCoreBusRead32;
537	core->busWrite8 = _GBCoreBusWrite8;
538	core->busWrite16 = _GBCoreBusWrite16;
539	core->busWrite32 = _GBCoreBusWrite32;
540	core->rawRead8 = _GBCoreRawRead8;
541	core->rawRead16 = _GBCoreRawRead16;
542	core->rawRead32 = _GBCoreRawRead32;
543	core->rawWrite8 = _GBCoreRawWrite8;
544	core->rawWrite16 = _GBCoreRawWrite16;
545	core->rawWrite32 = _GBCoreRawWrite32;
546	core->supportsDebuggerType = _GBCoreSupportsDebuggerType;
547	core->debuggerPlatform = _GBCoreDebuggerPlatform;
548	core->cliDebuggerSystem = _GBCoreCliDebuggerSystem;
549	core->attachDebugger = _GBCoreAttachDebugger;
550	core->detachDebugger = _GBCoreDetachDebugger;
551	core->cheatDevice = _GBCoreCheatDevice;
552	core->savedataClone = _GBCoreSavedataClone;
553	core->savedataLoad = _GBCoreSavedataLoad;
554	return core;
555}