all repos — mgba @ 5ea104844d58095e3dc002d93074d3302976cae6

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 _GBCoreLoadPatch(struct mCore* core, struct VFile* vf) {
174	if (!vf) {
175		return false;
176	}
177	struct Patch patch;
178	if (!loadPatch(vf, &patch)) {
179		return false;
180	}
181	GBApplyPatch(core->board, &patch);
182	return true;
183}
184
185static void _GBCoreUnloadROM(struct mCore* core) {
186	return GBUnloadROM(core->board);
187}
188
189static void _GBCoreReset(struct mCore* core) {
190	struct GBCore* gbcore = (struct GBCore*) core;
191	struct GB* gb = (struct GB*) core->board;
192	if (gbcore->renderer.outputBuffer) {
193		GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
194	}
195	LR35902Reset(core->cpu);
196}
197
198static void _GBCoreRunFrame(struct mCore* core) {
199	struct GB* gb = core->board;
200	int32_t frameCounter = gb->video.frameCounter;
201	while (gb->video.frameCounter == frameCounter) {
202		LR35902Run(core->cpu);
203	}
204}
205
206static void _GBCoreRunLoop(struct mCore* core) {
207	LR35902Run(core->cpu);
208}
209
210static void _GBCoreStep(struct mCore* core) {
211	struct LR35902Core* cpu = core->cpu;
212	do {
213		LR35902Tick(cpu);
214	} while (cpu->executionState != LR35902_CORE_FETCH);
215}
216
217static size_t _GBCoreStateSize(struct mCore* core) {
218	UNUSED(core);
219	return sizeof(struct GBSerializedState);
220}
221
222static bool _GBCoreLoadState(struct mCore* core, const void* state) {
223	return GBDeserialize(core->board, state);
224}
225
226static bool _GBCoreSaveState(struct mCore* core, void* state) {
227	GBSerialize(core->board, state);
228	return true;
229}
230
231static void _GBCoreSetKeys(struct mCore* core, uint32_t keys) {
232	struct GBCore* gbcore = (struct GBCore*) core;
233	gbcore->keys = keys;
234}
235
236static void _GBCoreAddKeys(struct mCore* core, uint32_t keys) {
237	struct GBCore* gbcore = (struct GBCore*) core;
238	gbcore->keys |= keys;
239}
240
241static void _GBCoreClearKeys(struct mCore* core, uint32_t keys) {
242	struct GBCore* gbcore = (struct GBCore*) core;
243	gbcore->keys &= ~keys;
244}
245
246static int32_t _GBCoreFrameCounter(struct mCore* core) {
247	struct GB* gb = core->board;
248	return gb->video.frameCounter;
249}
250
251static int32_t _GBCoreFrameCycles(struct mCore* core) {
252	UNUSED(core);
253	return GB_VIDEO_TOTAL_LENGTH;
254}
255
256static int32_t _GBCoreFrequency(struct mCore* core) {
257	UNUSED(core);
258	// TODO: GB differences
259	return DMG_LR35902_FREQUENCY;
260}
261
262static void _GBCoreGetGameTitle(struct mCore* core, char* title) {
263	GBGetGameTitle(core->board, title);
264}
265
266static void _GBCoreGetGameCode(struct mCore* core, char* title) {
267	GBGetGameCode(core->board, title);
268}
269
270static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
271	struct GB* gb = core->board;
272	gb->memory.rtc = rtc;
273}
274
275static void _GBCoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
276	struct GB* gb = core->board;
277	gb->memory.rotation = rotation;
278}
279
280static void _GBCoreSetRumble(struct mCore* core, struct mRumble* rumble) {
281	struct GB* gb = core->board;
282	gb->memory.rumble = rumble;
283}
284
285static uint32_t _GBCoreBusRead8(struct mCore* core, uint32_t address) {
286	struct LR35902Core* cpu = core->cpu;
287	return cpu->memory.load8(cpu, address);
288}
289
290static uint32_t _GBCoreBusRead16(struct mCore* core, uint32_t address) {
291	struct LR35902Core* cpu = core->cpu;
292	return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8);
293}
294
295static uint32_t _GBCoreBusRead32(struct mCore* core, uint32_t address) {
296	struct LR35902Core* cpu = core->cpu;
297	return cpu->memory.load8(cpu, address) | (cpu->memory.load8(cpu, address + 1) << 8) |
298	       (cpu->memory.load8(cpu, address + 2) << 16) | (cpu->memory.load8(cpu, address + 3) << 24);
299}
300
301static void _GBCoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
302	struct LR35902Core* cpu = core->cpu;
303	cpu->memory.store8(cpu, address, value);
304}
305
306static void _GBCoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
307	struct LR35902Core* cpu = core->cpu;
308	cpu->memory.store8(cpu, address, value);
309	cpu->memory.store8(cpu, address + 1, value >> 8);
310}
311
312static void _GBCoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
313	struct LR35902Core* cpu = core->cpu;
314	cpu->memory.store8(cpu, address, value);
315	cpu->memory.store8(cpu, address + 1, value >> 8);
316	cpu->memory.store8(cpu, address + 2, value >> 16);
317	cpu->memory.store8(cpu, address + 3, value >> 24);
318}
319
320static uint32_t _GBCoreRawRead8(struct mCore* core, uint32_t address) {
321	struct LR35902Core* cpu = core->cpu;
322	return GBLoad8(cpu, address);
323}
324
325static uint32_t _GBCoreRawRead16(struct mCore* core, uint32_t address) {
326	struct LR35902Core* cpu = core->cpu;
327	return GBLoad8(cpu, address) | (GBLoad8(cpu, address + 1) << 8);
328}
329
330static uint32_t _GBCoreRawRead32(struct mCore* core, uint32_t address) {
331	struct LR35902Core* cpu = core->cpu;
332	return GBLoad8(cpu, address) | (GBLoad8(cpu, address + 1) << 8) |
333	       (GBLoad8(cpu, address + 2) << 16) | (GBLoad8(cpu, address + 3) << 24);
334}
335
336static void _GBCoreRawWrite8(struct mCore* core, uint32_t address, uint8_t value) {
337	struct LR35902Core* cpu = core->cpu;
338	GBPatch8(cpu, address, value, NULL);
339}
340
341static void _GBCoreRawWrite16(struct mCore* core, uint32_t address, uint16_t value) {
342	struct LR35902Core* cpu = core->cpu;
343	GBPatch8(cpu, address, value, NULL);
344	GBPatch8(cpu, address + 1, value >> 8, NULL);
345}
346
347static void _GBCoreRawWrite32(struct mCore* core, uint32_t address, uint32_t value) {
348	struct LR35902Core* cpu = core->cpu;
349	GBPatch8(cpu, address, value, NULL);
350	GBPatch8(cpu, address + 1, value >> 8, NULL);
351	GBPatch8(cpu, address + 2, value >> 16, NULL);
352	GBPatch8(cpu, address + 3, value >> 24, NULL);
353}
354
355static bool _GBCoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
356	UNUSED(core);
357	switch (type) {
358#ifdef USE_CLI_DEBUGGER
359	case DEBUGGER_CLI:
360		return true;
361#endif
362	default:
363		return false;
364	}
365}
366
367static struct mDebuggerPlatform* _GBCoreDebuggerPlatform(struct mCore* core) {
368	struct GBCore* gbcore = (struct GBCore*) core;
369	if (!gbcore->debuggerPlatform) {
370		gbcore->debuggerPlatform = LR35902DebuggerPlatformCreate();
371	}
372	return gbcore->debuggerPlatform;
373}
374
375static struct CLIDebuggerSystem* _GBCoreCliDebuggerSystem(struct mCore* core) {
376#ifdef USE_CLI_DEBUGGER
377	return GBCLIDebuggerCreate(core);
378#else
379	UNUSED(core);
380	return NULL;
381#endif
382}
383
384static void _GBCoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
385	struct LR35902Core* cpu = core->cpu;
386	if (core->debugger) {
387		LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
388	}
389	cpu->components[CPU_COMPONENT_DEBUGGER] = &debugger->d;
390	LR35902HotplugAttach(cpu, CPU_COMPONENT_DEBUGGER);
391	core->debugger = debugger;
392}
393
394static void _GBCoreDetachDebugger(struct mCore* core) {
395	struct LR35902Core* cpu = core->cpu;
396	LR35902HotplugDetach(cpu, CPU_COMPONENT_DEBUGGER);
397	cpu->components[CPU_COMPONENT_DEBUGGER] = NULL;
398	core->debugger = NULL;
399}
400
401static struct mCheatDevice* _GBCoreCheatDevice(struct mCore* core) {
402	struct GBCore* gbcore = (struct GBCore*) core;
403	if (!gbcore->cheatDevice) {
404		gbcore->cheatDevice = GBCheatDeviceCreate();
405		((struct LR35902Core*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbcore->cheatDevice->d;
406		LR35902HotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
407		gbcore->cheatDevice->p = core;
408	}
409	return gbcore->cheatDevice;
410}
411
412static size_t _GBCoreSavedataClone(struct mCore* core, void** sram) {
413	struct GB* gb = core->board;
414	struct VFile* vf = gb->sramVf;
415	if (vf) {
416		*sram = malloc(vf->size(vf));
417		vf->seek(vf, 0, SEEK_SET);
418		return vf->read(vf, *sram, vf->size(vf));
419	}
420	*sram = malloc(0x20000);
421	memcpy(*sram, gb->memory.sram, 0x20000);
422	return 0x20000;
423}
424
425static bool _GBCoreSavedataLoad(struct mCore* core, const void* sram, size_t size) {
426	struct GB* gb = core->board;
427	struct VFile* vf = gb->sramVf;
428	if (vf) {
429		vf->seek(vf, 0, SEEK_SET);
430		return vf->write(vf, sram, size) > 0;
431	}
432	if (size > 0x20000) {
433		size = 0x20000;
434	}
435	memcpy(gb->memory.sram, sram, 0x20000);
436	return true;
437}
438
439struct mCore* GBCoreCreate(void) {
440	struct GBCore* gbcore = malloc(sizeof(*gbcore));
441	struct mCore* core = &gbcore->d;
442	memset(&core->opts, 0, sizeof(core->opts));
443	core->cpu = NULL;
444	core->board = NULL;
445	core->debugger = NULL;
446	core->init = _GBCoreInit;
447	core->deinit = _GBCoreDeinit;
448	core->platform = _GBCorePlatform;
449	core->setSync = _GBCoreSetSync;
450	core->loadConfig = _GBCoreLoadConfig;
451	core->desiredVideoDimensions = _GBCoreDesiredVideoDimensions;
452	core->setVideoBuffer = _GBCoreSetVideoBuffer;
453	core->getVideoBuffer = _GBCoreGetVideoBuffer;
454	core->getAudioChannel = _GBCoreGetAudioChannel;
455	core->setAudioBufferSize = _GBCoreSetAudioBufferSize;
456	core->getAudioBufferSize = _GBCoreGetAudioBufferSize;
457	core->setAVStream = _GBCoreSetAVStream;
458	core->isROM = GBIsROM;
459	core->loadROM = _GBCoreLoadROM;
460	core->loadBIOS = _GBCoreLoadBIOS;
461	core->loadSave = _GBCoreLoadSave;
462	core->loadPatch = _GBCoreLoadPatch;
463	core->unloadROM = _GBCoreUnloadROM;
464	core->reset = _GBCoreReset;
465	core->runFrame = _GBCoreRunFrame;
466	core->runLoop = _GBCoreRunLoop;
467	core->step = _GBCoreStep;
468	core->stateSize = _GBCoreStateSize;
469	core->loadState = _GBCoreLoadState;
470	core->saveState = _GBCoreSaveState;
471	core->setKeys = _GBCoreSetKeys;
472	core->addKeys = _GBCoreAddKeys;
473	core->clearKeys = _GBCoreClearKeys;
474	core->frameCounter = _GBCoreFrameCounter;
475	core->frameCycles = _GBCoreFrameCycles;
476	core->frequency = _GBCoreFrequency;
477	core->getGameTitle = _GBCoreGetGameTitle;
478	core->getGameCode = _GBCoreGetGameCode;
479	core->setRTC = _GBCoreSetRTC;
480	core->setRotation = _GBCoreSetRotation;
481	core->setRumble = _GBCoreSetRumble;
482	core->busRead8 = _GBCoreBusRead8;
483	core->busRead16 = _GBCoreBusRead16;
484	core->busRead32 = _GBCoreBusRead32;
485	core->busWrite8 = _GBCoreBusWrite8;
486	core->busWrite16 = _GBCoreBusWrite16;
487	core->busWrite32 = _GBCoreBusWrite32;
488	core->rawRead8 = _GBCoreRawRead8;
489	core->rawRead16 = _GBCoreRawRead16;
490	core->rawRead32 = _GBCoreRawRead32;
491	core->rawWrite8 = _GBCoreRawWrite8;
492	core->rawWrite16 = _GBCoreRawWrite16;
493	core->rawWrite32 = _GBCoreRawWrite32;
494	core->supportsDebuggerType = _GBCoreSupportsDebuggerType;
495	core->debuggerPlatform = _GBCoreDebuggerPlatform;
496	core->cliDebuggerSystem = _GBCoreCliDebuggerSystem;
497	core->attachDebugger = _GBCoreAttachDebugger;
498	core->detachDebugger = _GBCoreDetachDebugger;
499	core->cheatDevice = _GBCoreCheatDevice;
500	core->savedataClone = _GBCoreSavedataClone;
501	core->savedataLoad = _GBCoreSavedataLoad;
502	return core;
503}