src/gba/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 <mgba/gba/core.h>
7
8#include <mgba/core/core.h>
9#include <mgba/core/log.h>
10#include <mgba/internal/arm/debugger/debugger.h>
11#include <mgba/internal/gba/cheats.h>
12#include <mgba/internal/gba/gba.h>
13#include <mgba/internal/gba/extra/cli.h>
14#include <mgba/internal/gba/overrides.h>
15#ifndef DISABLE_THREADING
16#include <mgba/internal/gba/renderers/thread-proxy.h>
17#endif
18#include <mgba/internal/gba/renderers/video-software.h>
19#include <mgba/internal/gba/savedata.h>
20#include <mgba/internal/gba/serialize.h>
21#include <mgba-util/memory.h>
22#include <mgba-util/patch.h>
23#include <mgba-util/vfs.h>
24
25struct GBACore {
26 struct mCore d;
27 struct GBAVideoSoftwareRenderer renderer;
28#ifndef DISABLE_THREADING
29 struct GBAVideoThreadProxyRenderer threadProxy;
30 int threadedVideo;
31#endif
32 int keys;
33 struct mCPUComponent* components[CPU_COMPONENT_MAX];
34 const struct Configuration* overrides;
35 struct mDebuggerPlatform* debuggerPlatform;
36 struct mCheatDevice* cheatDevice;
37};
38
39static bool _GBACoreInit(struct mCore* core) {
40 struct GBACore* gbacore = (struct GBACore*) core;
41
42 struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
43 struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
44 if (!cpu || !gba) {
45 free(cpu);
46 free(gba);
47 return false;
48 }
49 core->cpu = cpu;
50 core->board = gba;
51 core->debugger = NULL;
52 gbacore->overrides = NULL;
53 gbacore->debuggerPlatform = NULL;
54 gbacore->cheatDevice = NULL;
55
56 GBACreate(gba);
57 // TODO: Restore cheats
58 memset(gbacore->components, 0, sizeof(gbacore->components));
59 ARMSetComponents(cpu, &gba->d, CPU_COMPONENT_MAX, gbacore->components);
60 ARMInit(cpu);
61
62 GBAVideoSoftwareRendererCreate(&gbacore->renderer);
63 gbacore->renderer.outputBuffer = NULL;
64
65#ifndef DISABLE_THREADING
66 gbacore->threadedVideo = false;
67 GBAVideoThreadProxyRendererCreate(&gbacore->threadProxy, &gbacore->renderer.d);
68#endif
69
70 gbacore->keys = 0;
71 gba->keySource = &gbacore->keys;
72
73#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
74 mDirectorySetInit(&core->dirs);
75#endif
76
77 return true;
78}
79
80static void _GBACoreDeinit(struct mCore* core) {
81 ARMDeinit(core->cpu);
82 GBADestroy(core->board);
83 mappedMemoryFree(core->cpu, sizeof(struct ARMCore));
84 mappedMemoryFree(core->board, sizeof(struct GBA));
85#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
86 mDirectorySetDeinit(&core->dirs);
87#endif
88
89 struct GBACore* gbacore = (struct GBACore*) core;
90 free(gbacore->debuggerPlatform);
91 if (gbacore->cheatDevice) {
92 mCheatDeviceDestroy(gbacore->cheatDevice);
93 }
94 free(gbacore->cheatDevice);
95 mCoreConfigFreeOpts(&core->opts);
96 free(core);
97}
98
99static enum mPlatform _GBACorePlatform(const struct mCore* core) {
100 UNUSED(core);
101 return PLATFORM_GBA;
102}
103
104static void _GBACoreSetSync(struct mCore* core, struct mCoreSync* sync) {
105 struct GBA* gba = core->board;
106 gba->sync = sync;
107}
108
109static void _GBACoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
110 struct GBA* gba = core->board;
111 if (core->opts.mute) {
112 gba->audio.masterVolume = 0;
113 } else {
114 gba->audio.masterVolume = core->opts.volume;
115 }
116 gba->video.frameskip = core->opts.frameskip;
117
118#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
119 struct GBACore* gbacore = (struct GBACore*) core;
120 gbacore->overrides = mCoreConfigGetOverridesConst(config);
121#endif
122
123 const char* idleOptimization = mCoreConfigGetValue(config, "idleOptimization");
124 if (idleOptimization) {
125 if (strcasecmp(idleOptimization, "ignore") == 0) {
126 gba->idleOptimization = IDLE_LOOP_IGNORE;
127 } else if (strcasecmp(idleOptimization, "remove") == 0) {
128 gba->idleOptimization = IDLE_LOOP_REMOVE;
129 } else if (strcasecmp(idleOptimization, "detect") == 0) {
130 if (gba->idleLoop == IDLE_LOOP_NONE) {
131 gba->idleOptimization = IDLE_LOOP_DETECT;
132 } else {
133 gba->idleOptimization = IDLE_LOOP_REMOVE;
134 }
135 }
136 }
137
138#ifndef DISABLE_THREADING
139 mCoreConfigGetIntValue(config, "threadedVideo", &gbacore->threadedVideo);
140#endif
141}
142
143static void _GBACoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
144 UNUSED(core);
145 *width = VIDEO_HORIZONTAL_PIXELS;
146 *height = VIDEO_VERTICAL_PIXELS;
147}
148
149static void _GBACoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
150 struct GBACore* gbacore = (struct GBACore*) core;
151 gbacore->renderer.outputBuffer = buffer;
152 gbacore->renderer.outputBufferStride = stride;
153}
154
155static void _GBACoreGetPixels(struct mCore* core, const void** buffer, size_t* stride) {
156 struct GBACore* gbacore = (struct GBACore*) core;
157 gbacore->renderer.d.getPixels(&gbacore->renderer.d, stride, buffer);
158}
159
160static void _GBACorePutPixels(struct mCore* core, const void* buffer, size_t stride) {
161 struct GBACore* gbacore = (struct GBACore*) core;
162 gbacore->renderer.d.putPixels(&gbacore->renderer.d, stride, buffer);
163}
164
165static struct blip_t* _GBACoreGetAudioChannel(struct mCore* core, int ch) {
166 struct GBA* gba = core->board;
167 switch (ch) {
168 case 0:
169 return gba->audio.psg.left;
170 case 1:
171 return gba->audio.psg.right;
172 default:
173 return NULL;
174 }
175}
176
177static void _GBACoreSetAudioBufferSize(struct mCore* core, size_t samples) {
178 struct GBA* gba = core->board;
179 GBAAudioResizeBuffer(&gba->audio, samples);
180}
181
182static size_t _GBACoreGetAudioBufferSize(struct mCore* core) {
183 struct GBA* gba = core->board;
184 return gba->audio.samples;
185}
186
187static void _GBACoreSetCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) {
188 struct GBA* gba = core->board;
189 gba->coreCallbacks = coreCallbacks;
190}
191
192static void _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
193 struct GBA* gba = core->board;
194 gba->stream = stream;
195 if (stream && stream->videoDimensionsChanged) {
196 stream->videoDimensionsChanged(stream, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
197 }
198}
199
200static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
201 if (GBAIsMB(vf)) {
202 return GBALoadMB(core->board, vf);
203 }
204 return GBALoadROM(core->board, vf);
205}
206
207static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
208 UNUSED(type);
209 if (!GBAIsBIOS(vf)) {
210 return false;
211 }
212 GBALoadBIOS(core->board, vf);
213 return true;
214}
215
216static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
217 return GBALoadSave(core->board, vf);
218}
219
220static bool _GBACoreLoadTemporarySave(struct mCore* core, struct VFile* vf) {
221 struct GBA* gba = core->board;
222 GBASavedataMask(&gba->memory.savedata, vf, false);
223 return true; // TODO: Return a real value
224}
225
226static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
227 if (!vf) {
228 return false;
229 }
230 struct Patch patch;
231 if (!loadPatch(vf, &patch)) {
232 return false;
233 }
234 GBAApplyPatch(core->board, &patch);
235 return true;
236}
237
238static void _GBACoreUnloadROM(struct mCore* core) {
239 struct GBACore* gbacore = (struct GBACore*) core;
240 struct ARMCore* cpu = core->cpu;
241 if (gbacore->cheatDevice) {
242 ARMHotplugDetach(cpu, CPU_COMPONENT_CHEAT_DEVICE);
243 cpu->components[CPU_COMPONENT_CHEAT_DEVICE] = NULL;
244 mCheatDeviceDestroy(gbacore->cheatDevice);
245 gbacore->cheatDevice = NULL;
246 }
247 return GBAUnloadROM(core->board);
248}
249
250static void _GBACoreReset(struct mCore* core) {
251 struct GBACore* gbacore = (struct GBACore*) core;
252 struct GBA* gba = (struct GBA*) core->board;
253 if (gbacore->renderer.outputBuffer) {
254 struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
255#ifndef DISABLE_THREADING
256 if (gbacore->threadedVideo) {
257 renderer = &gbacore->threadProxy.d;
258 }
259#endif
260 GBAVideoAssociateRenderer(&gba->video, renderer);
261 }
262
263 struct GBACartridgeOverride override;
264 const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
265 if (cart) {
266 memcpy(override.id, &cart->id, sizeof(override.id));
267 if (GBAOverrideFind(gbacore->overrides, &override)) {
268 GBAOverrideApply(gba, &override);
269 }
270 }
271
272#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
273 struct VFile* bios = 0;
274 if (core->opts.useBios) {
275 if (!core->opts.bios) {
276 char path[PATH_MAX];
277 mCoreConfigDirectory(path, PATH_MAX);
278 strncat(path, PATH_SEP "gba_bios.bin", PATH_MAX - strlen(path));
279 bios = VFileOpen(path, O_RDONLY);
280 } else {
281 bios = VFileOpen(core->opts.bios, O_RDONLY);
282 }
283 }
284 if (bios) {
285 GBALoadBIOS(gba, bios);
286 }
287#endif
288
289 ARMReset(core->cpu);
290 if (core->opts.skipBios && gba->pristineRom) {
291 GBASkipBIOS(core->board);
292 }
293}
294
295static void _GBACoreRunFrame(struct mCore* core) {
296 struct GBA* gba = core->board;
297 int32_t frameCounter = gba->video.frameCounter;
298 while (gba->video.frameCounter == frameCounter) {
299 ARMRunLoop(core->cpu);
300 }
301}
302
303static void _GBACoreRunLoop(struct mCore* core) {
304 ARMRunLoop(core->cpu);
305}
306
307static void _GBACoreStep(struct mCore* core) {
308 ARMRun(core->cpu);
309}
310
311static size_t _GBACoreStateSize(struct mCore* core) {
312 UNUSED(core);
313 return sizeof(struct GBASerializedState);
314}
315
316static bool _GBACoreLoadState(struct mCore* core, const void* state) {
317 return GBADeserialize(core->board, state);
318}
319
320static bool _GBACoreSaveState(struct mCore* core, void* state) {
321 GBASerialize(core->board, state);
322 return true;
323}
324
325static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
326 struct GBACore* gbacore = (struct GBACore*) core;
327 gbacore->keys = keys;
328}
329
330static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
331 struct GBACore* gbacore = (struct GBACore*) core;
332 gbacore->keys |= keys;
333}
334
335static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
336 struct GBACore* gbacore = (struct GBACore*) core;
337 gbacore->keys &= ~keys;
338}
339
340static int32_t _GBACoreFrameCounter(const struct mCore* core) {
341 const struct GBA* gba = core->board;
342 return gba->video.frameCounter;
343}
344
345static int32_t _GBACoreFrameCycles(const struct mCore* core) {
346 UNUSED(core);
347 return VIDEO_TOTAL_LENGTH;
348}
349
350static int32_t _GBACoreFrequency(const struct mCore* core) {
351 UNUSED(core);
352 return GBA_ARM7TDMI_FREQUENCY;
353}
354
355static void _GBACoreGetGameTitle(const struct mCore* core, char* title) {
356 GBAGetGameTitle(core->board, title);
357}
358
359static void _GBACoreGetGameCode(const struct mCore* core, char* title) {
360 GBAGetGameCode(core->board, title);
361}
362
363static void _GBACoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
364 struct GBA* gba = core->board;
365 gba->rtcSource = rtc;
366}
367
368static void _GBACoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
369 struct GBA* gba = core->board;
370 gba->rotationSource = rotation;
371}
372
373static void _GBACoreSetRumble(struct mCore* core, struct mRumble* rumble) {
374 struct GBA* gba = core->board;
375 gba->rumble = rumble;
376}
377
378static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) {
379 struct ARMCore* cpu = core->cpu;
380 return cpu->memory.load8(cpu, address, 0);
381}
382
383static uint32_t _GBACoreBusRead16(struct mCore* core, uint32_t address) {
384 struct ARMCore* cpu = core->cpu;
385 return cpu->memory.load16(cpu, address, 0);
386
387}
388
389static uint32_t _GBACoreBusRead32(struct mCore* core, uint32_t address) {
390 struct ARMCore* cpu = core->cpu;
391 return cpu->memory.load32(cpu, address, 0);
392}
393
394static void _GBACoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
395 struct ARMCore* cpu = core->cpu;
396 cpu->memory.store8(cpu, address, value, 0);
397}
398
399static void _GBACoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
400 struct ARMCore* cpu = core->cpu;
401 cpu->memory.store16(cpu, address, value, 0);
402}
403
404static void _GBACoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
405 struct ARMCore* cpu = core->cpu;
406 cpu->memory.store32(cpu, address, value, 0);
407}
408
409static uint32_t _GBACoreRawRead8(struct mCore* core, uint32_t address, int segment) {
410 UNUSED(segment);
411 struct ARMCore* cpu = core->cpu;
412 return GBAView8(cpu, address);
413}
414
415static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address, int segment) {
416 UNUSED(segment);
417 struct ARMCore* cpu = core->cpu;
418 return GBAView16(cpu, address);
419}
420
421static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address, int segment) {
422 UNUSED(segment);
423 struct ARMCore* cpu = core->cpu;
424 return GBAView32(cpu, address);
425}
426
427static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
428 UNUSED(segment);
429 struct ARMCore* cpu = core->cpu;
430 GBAPatch8(cpu, address, value, NULL);
431}
432
433static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
434 UNUSED(segment);
435 struct ARMCore* cpu = core->cpu;
436 GBAPatch16(cpu, address, value, NULL);
437}
438
439static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
440 UNUSED(segment);
441 struct ARMCore* cpu = core->cpu;
442 GBAPatch32(cpu, address, value, NULL);
443}
444
445#ifdef USE_DEBUGGERS
446static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
447 UNUSED(core);
448 switch (type) {
449 case DEBUGGER_CLI:
450 return true;
451#ifdef USE_GDB_STUB
452 case DEBUGGER_GDB:
453 return true;
454#endif
455 default:
456 return false;
457 }
458}
459
460static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
461 struct GBACore* gbacore = (struct GBACore*) core;
462 if (!gbacore->debuggerPlatform) {
463 gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
464 }
465 return gbacore->debuggerPlatform;
466}
467
468static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
469 return &GBACLIDebuggerCreate(core)->d;
470}
471
472static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
473 if (core->debugger) {
474 GBADetachDebugger(core->board);
475 }
476 GBAAttachDebugger(core->board, debugger);
477 core->debugger = debugger;
478}
479
480static void _GBACoreDetachDebugger(struct mCore* core) {
481 GBADetachDebugger(core->board);
482 core->debugger = NULL;
483}
484#endif
485
486static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
487 struct GBACore* gbacore = (struct GBACore*) core;
488 if (!gbacore->cheatDevice) {
489 gbacore->cheatDevice = GBACheatDeviceCreate();
490 ((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
491 ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
492 gbacore->cheatDevice->p = core;
493 }
494 return gbacore->cheatDevice;
495}
496
497static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
498 struct GBA* gba = core->board;
499 size_t size = GBASavedataSize(&gba->memory.savedata);
500 if (!size) {
501 *sram = NULL;
502 return 0;
503 }
504 *sram = malloc(size);
505 struct VFile* vf = VFileFromMemory(*sram, size);
506 if (!vf) {
507 free(*sram);
508 *sram = NULL;
509 return 0;
510 }
511 bool success = GBASavedataClone(&gba->memory.savedata, vf);
512 vf->close(vf);
513 if (!success) {
514 free(*sram);
515 *sram = NULL;
516 return 0;
517 }
518 return size;
519}
520
521static bool _GBACoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
522 struct VFile* vf = VFileMemChunk(sram, size);
523 if (!vf) {
524 return false;
525 }
526 struct GBA* gba = core->board;
527 bool success = true;
528 if (writeback) {
529 success = GBASavedataLoad(&gba->memory.savedata, vf);
530 vf->close(vf);
531 } else {
532 GBASavedataMask(&gba->memory.savedata, vf, true);
533 }
534 return success;
535}
536
537struct mCore* GBACoreCreate(void) {
538 struct GBACore* gbacore = malloc(sizeof(*gbacore));
539 struct mCore* core = &gbacore->d;
540 memset(&core->opts, 0, sizeof(core->opts));
541 core->cpu = NULL;
542 core->board = NULL;
543 core->debugger = NULL;
544 core->init = _GBACoreInit;
545 core->deinit = _GBACoreDeinit;
546 core->platform = _GBACorePlatform;
547 core->setSync = _GBACoreSetSync;
548 core->loadConfig = _GBACoreLoadConfig;
549 core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
550 core->setVideoBuffer = _GBACoreSetVideoBuffer;
551 core->getPixels = _GBACoreGetPixels;
552 core->putPixels = _GBACorePutPixels;
553 core->getAudioChannel = _GBACoreGetAudioChannel;
554 core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
555 core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
556 core->setCoreCallbacks = _GBACoreSetCoreCallbacks;
557 core->setAVStream = _GBACoreSetAVStream;
558 core->isROM = GBAIsROM;
559 core->loadROM = _GBACoreLoadROM;
560 core->loadBIOS = _GBACoreLoadBIOS;
561 core->loadSave = _GBACoreLoadSave;
562 core->loadTemporarySave = _GBACoreLoadTemporarySave;
563 core->loadPatch = _GBACoreLoadPatch;
564 core->unloadROM = _GBACoreUnloadROM;
565 core->reset = _GBACoreReset;
566 core->runFrame = _GBACoreRunFrame;
567 core->runLoop = _GBACoreRunLoop;
568 core->step = _GBACoreStep;
569 core->stateSize = _GBACoreStateSize;
570 core->loadState = _GBACoreLoadState;
571 core->saveState = _GBACoreSaveState;
572 core->setKeys = _GBACoreSetKeys;
573 core->addKeys = _GBACoreAddKeys;
574 core->clearKeys = _GBACoreClearKeys;
575 core->frameCounter = _GBACoreFrameCounter;
576 core->frameCycles = _GBACoreFrameCycles;
577 core->frequency = _GBACoreFrequency;
578 core->getGameTitle = _GBACoreGetGameTitle;
579 core->getGameCode = _GBACoreGetGameCode;
580 core->setRTC = _GBACoreSetRTC;
581 core->setRotation = _GBACoreSetRotation;
582 core->setRumble = _GBACoreSetRumble;
583 core->busRead8 = _GBACoreBusRead8;
584 core->busRead16 = _GBACoreBusRead16;
585 core->busRead32 = _GBACoreBusRead32;
586 core->busWrite8 = _GBACoreBusWrite8;
587 core->busWrite16 = _GBACoreBusWrite16;
588 core->busWrite32 = _GBACoreBusWrite32;
589 core->rawRead8 = _GBACoreRawRead8;
590 core->rawRead16 = _GBACoreRawRead16;
591 core->rawRead32 = _GBACoreRawRead32;
592 core->rawWrite8 = _GBACoreRawWrite8;
593 core->rawWrite16 = _GBACoreRawWrite16;
594 core->rawWrite32 = _GBACoreRawWrite32;
595#ifdef USE_DEBUGGERS
596 core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
597 core->debuggerPlatform = _GBACoreDebuggerPlatform;
598 core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
599 core->attachDebugger = _GBACoreAttachDebugger;
600 core->detachDebugger = _GBACoreDetachDebugger;
601#endif
602 core->cheatDevice = _GBACoreCheatDevice;
603 core->savedataClone = _GBACoreSavedataClone;
604 core->savedataRestore = _GBACoreSavedataRestore;
605 return core;
606}