all repos — mgba @ d5cfcf219ec1502cc152b331e74e512c5c0af674

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