all repos — mgba @ 12687faad214b6424c216cc911ebafe239678e7c

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