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 _GBACoreChecksum(const struct mCore* core, void* data, enum mCoreChecksumType type) {
251 struct GBA* gba = (struct GBA*) core->board;
252 switch (type) {
253 case CHECKSUM_CRC32:
254 memcpy(data, &gba->romCrc32, sizeof(gba->romCrc32));
255 break;
256 }
257 return;
258}
259
260static void _GBACoreReset(struct mCore* core) {
261 struct GBACore* gbacore = (struct GBACore*) core;
262 struct GBA* gba = (struct GBA*) core->board;
263 if (gbacore->renderer.outputBuffer) {
264 struct GBAVideoRenderer* renderer = &gbacore->renderer.d;
265#ifndef DISABLE_THREADING
266 if (gbacore->threadedVideo) {
267 renderer = &gbacore->threadProxy.d;
268 }
269#endif
270 GBAVideoAssociateRenderer(&gba->video, renderer);
271 }
272
273 struct GBACartridgeOverride override;
274 const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
275 if (cart) {
276 memcpy(override.id, &cart->id, sizeof(override.id));
277 if (GBAOverrideFind(gbacore->overrides, &override)) {
278 GBAOverrideApply(gba, &override);
279 }
280 }
281
282#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
283 struct VFile* bios = 0;
284 if (core->opts.useBios) {
285 if (!core->opts.bios) {
286 char path[PATH_MAX];
287 mCoreConfigDirectory(path, PATH_MAX);
288 strncat(path, PATH_SEP "gba_bios.bin", PATH_MAX - strlen(path));
289 bios = VFileOpen(path, O_RDONLY);
290 } else {
291 bios = VFileOpen(core->opts.bios, O_RDONLY);
292 }
293 }
294 if (bios) {
295 GBALoadBIOS(gba, bios);
296 }
297#endif
298
299 ARMReset(core->cpu);
300 if (core->opts.skipBios && gba->pristineRom) {
301 GBASkipBIOS(core->board);
302 }
303}
304
305static void _GBACoreRunFrame(struct mCore* core) {
306 struct GBA* gba = core->board;
307 int32_t frameCounter = gba->video.frameCounter;
308 while (gba->video.frameCounter == frameCounter) {
309 ARMRunLoop(core->cpu);
310 }
311}
312
313static void _GBACoreRunLoop(struct mCore* core) {
314 ARMRunLoop(core->cpu);
315}
316
317static void _GBACoreStep(struct mCore* core) {
318 ARMRun(core->cpu);
319}
320
321static size_t _GBACoreStateSize(struct mCore* core) {
322 UNUSED(core);
323 return sizeof(struct GBASerializedState);
324}
325
326static bool _GBACoreLoadState(struct mCore* core, const void* state) {
327 return GBADeserialize(core->board, state);
328}
329
330static bool _GBACoreSaveState(struct mCore* core, void* state) {
331 GBASerialize(core->board, state);
332 return true;
333}
334
335static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
336 struct GBACore* gbacore = (struct GBACore*) core;
337 gbacore->keys = keys;
338}
339
340static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
341 struct GBACore* gbacore = (struct GBACore*) core;
342 gbacore->keys |= keys;
343}
344
345static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
346 struct GBACore* gbacore = (struct GBACore*) core;
347 gbacore->keys &= ~keys;
348}
349
350static int32_t _GBACoreFrameCounter(const struct mCore* core) {
351 const struct GBA* gba = core->board;
352 return gba->video.frameCounter;
353}
354
355static int32_t _GBACoreFrameCycles(const struct mCore* core) {
356 UNUSED(core);
357 return VIDEO_TOTAL_LENGTH;
358}
359
360static int32_t _GBACoreFrequency(const struct mCore* core) {
361 UNUSED(core);
362 return GBA_ARM7TDMI_FREQUENCY;
363}
364
365static void _GBACoreGetGameTitle(const struct mCore* core, char* title) {
366 GBAGetGameTitle(core->board, title);
367}
368
369static void _GBACoreGetGameCode(const struct mCore* core, char* title) {
370 GBAGetGameCode(core->board, title);
371}
372
373static void _GBACoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
374 struct GBA* gba = core->board;
375 gba->rtcSource = rtc;
376}
377
378static void _GBACoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
379 struct GBA* gba = core->board;
380 gba->rotationSource = rotation;
381}
382
383static void _GBACoreSetRumble(struct mCore* core, struct mRumble* rumble) {
384 struct GBA* gba = core->board;
385 gba->rumble = rumble;
386}
387
388static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) {
389 struct ARMCore* cpu = core->cpu;
390 return cpu->memory.load8(cpu, address, 0);
391}
392
393static uint32_t _GBACoreBusRead16(struct mCore* core, uint32_t address) {
394 struct ARMCore* cpu = core->cpu;
395 return cpu->memory.load16(cpu, address, 0);
396
397}
398
399static uint32_t _GBACoreBusRead32(struct mCore* core, uint32_t address) {
400 struct ARMCore* cpu = core->cpu;
401 return cpu->memory.load32(cpu, address, 0);
402}
403
404static void _GBACoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
405 struct ARMCore* cpu = core->cpu;
406 cpu->memory.store8(cpu, address, value, 0);
407}
408
409static void _GBACoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
410 struct ARMCore* cpu = core->cpu;
411 cpu->memory.store16(cpu, address, value, 0);
412}
413
414static void _GBACoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
415 struct ARMCore* cpu = core->cpu;
416 cpu->memory.store32(cpu, address, value, 0);
417}
418
419static uint32_t _GBACoreRawRead8(struct mCore* core, uint32_t address, int segment) {
420 UNUSED(segment);
421 struct ARMCore* cpu = core->cpu;
422 return GBAView8(cpu, address);
423}
424
425static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address, int segment) {
426 UNUSED(segment);
427 struct ARMCore* cpu = core->cpu;
428 return GBAView16(cpu, address);
429}
430
431static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address, int segment) {
432 UNUSED(segment);
433 struct ARMCore* cpu = core->cpu;
434 return GBAView32(cpu, address);
435}
436
437static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, int segment, uint8_t value) {
438 UNUSED(segment);
439 struct ARMCore* cpu = core->cpu;
440 GBAPatch8(cpu, address, value, NULL);
441}
442
443static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, int segment, uint16_t value) {
444 UNUSED(segment);
445 struct ARMCore* cpu = core->cpu;
446 GBAPatch16(cpu, address, value, NULL);
447}
448
449static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, int segment, uint32_t value) {
450 UNUSED(segment);
451 struct ARMCore* cpu = core->cpu;
452 GBAPatch32(cpu, address, value, NULL);
453}
454
455#ifdef USE_DEBUGGERS
456static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
457 UNUSED(core);
458 switch (type) {
459 case DEBUGGER_CLI:
460 return true;
461#ifdef USE_GDB_STUB
462 case DEBUGGER_GDB:
463 return true;
464#endif
465 default:
466 return false;
467 }
468}
469
470static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
471 struct GBACore* gbacore = (struct GBACore*) core;
472 if (!gbacore->debuggerPlatform) {
473 gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
474 }
475 return gbacore->debuggerPlatform;
476}
477
478static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
479 return &GBACLIDebuggerCreate(core)->d;
480}
481
482static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
483 if (core->debugger) {
484 GBADetachDebugger(core->board);
485 }
486 GBAAttachDebugger(core->board, debugger);
487 core->debugger = debugger;
488}
489
490static void _GBACoreDetachDebugger(struct mCore* core) {
491 GBADetachDebugger(core->board);
492 core->debugger = NULL;
493}
494#endif
495
496static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
497 struct GBACore* gbacore = (struct GBACore*) core;
498 if (!gbacore->cheatDevice) {
499 gbacore->cheatDevice = GBACheatDeviceCreate();
500 ((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
501 ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
502 gbacore->cheatDevice->p = core;
503 }
504 return gbacore->cheatDevice;
505}
506
507static size_t _GBACoreSavedataClone(struct mCore* core, void** sram) {
508 struct GBA* gba = core->board;
509 size_t size = GBASavedataSize(&gba->memory.savedata);
510 if (!size) {
511 *sram = NULL;
512 return 0;
513 }
514 *sram = malloc(size);
515 struct VFile* vf = VFileFromMemory(*sram, size);
516 if (!vf) {
517 free(*sram);
518 *sram = NULL;
519 return 0;
520 }
521 bool success = GBASavedataClone(&gba->memory.savedata, vf);
522 vf->close(vf);
523 if (!success) {
524 free(*sram);
525 *sram = NULL;
526 return 0;
527 }
528 return size;
529}
530
531static bool _GBACoreSavedataRestore(struct mCore* core, const void* sram, size_t size, bool writeback) {
532 struct VFile* vf = VFileMemChunk(sram, size);
533 if (!vf) {
534 return false;
535 }
536 struct GBA* gba = core->board;
537 bool success = true;
538 if (writeback) {
539 success = GBASavedataLoad(&gba->memory.savedata, vf);
540 vf->close(vf);
541 } else {
542 GBASavedataMask(&gba->memory.savedata, vf, true);
543 }
544 return success;
545}
546
547struct mCore* GBACoreCreate(void) {
548 struct GBACore* gbacore = malloc(sizeof(*gbacore));
549 struct mCore* core = &gbacore->d;
550 memset(&core->opts, 0, sizeof(core->opts));
551 core->cpu = NULL;
552 core->board = NULL;
553 core->debugger = NULL;
554 core->init = _GBACoreInit;
555 core->deinit = _GBACoreDeinit;
556 core->platform = _GBACorePlatform;
557 core->setSync = _GBACoreSetSync;
558 core->loadConfig = _GBACoreLoadConfig;
559 core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
560 core->setVideoBuffer = _GBACoreSetVideoBuffer;
561 core->getPixels = _GBACoreGetPixels;
562 core->putPixels = _GBACorePutPixels;
563 core->getAudioChannel = _GBACoreGetAudioChannel;
564 core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
565 core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
566 core->setCoreCallbacks = _GBACoreSetCoreCallbacks;
567 core->setAVStream = _GBACoreSetAVStream;
568 core->isROM = GBAIsROM;
569 core->loadROM = _GBACoreLoadROM;
570 core->loadBIOS = _GBACoreLoadBIOS;
571 core->loadSave = _GBACoreLoadSave;
572 core->loadTemporarySave = _GBACoreLoadTemporarySave;
573 core->loadPatch = _GBACoreLoadPatch;
574 core->unloadROM = _GBACoreUnloadROM;
575 core->checksum = _GBACoreChecksum;
576 core->reset = _GBACoreReset;
577 core->runFrame = _GBACoreRunFrame;
578 core->runLoop = _GBACoreRunLoop;
579 core->step = _GBACoreStep;
580 core->stateSize = _GBACoreStateSize;
581 core->loadState = _GBACoreLoadState;
582 core->saveState = _GBACoreSaveState;
583 core->setKeys = _GBACoreSetKeys;
584 core->addKeys = _GBACoreAddKeys;
585 core->clearKeys = _GBACoreClearKeys;
586 core->frameCounter = _GBACoreFrameCounter;
587 core->frameCycles = _GBACoreFrameCycles;
588 core->frequency = _GBACoreFrequency;
589 core->getGameTitle = _GBACoreGetGameTitle;
590 core->getGameCode = _GBACoreGetGameCode;
591 core->setRTC = _GBACoreSetRTC;
592 core->setRotation = _GBACoreSetRotation;
593 core->setRumble = _GBACoreSetRumble;
594 core->busRead8 = _GBACoreBusRead8;
595 core->busRead16 = _GBACoreBusRead16;
596 core->busRead32 = _GBACoreBusRead32;
597 core->busWrite8 = _GBACoreBusWrite8;
598 core->busWrite16 = _GBACoreBusWrite16;
599 core->busWrite32 = _GBACoreBusWrite32;
600 core->rawRead8 = _GBACoreRawRead8;
601 core->rawRead16 = _GBACoreRawRead16;
602 core->rawRead32 = _GBACoreRawRead32;
603 core->rawWrite8 = _GBACoreRawWrite8;
604 core->rawWrite16 = _GBACoreRawWrite16;
605 core->rawWrite32 = _GBACoreRawWrite32;
606#ifdef USE_DEBUGGERS
607 core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
608 core->debuggerPlatform = _GBACoreDebuggerPlatform;
609 core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
610 core->attachDebugger = _GBACoreAttachDebugger;
611 core->detachDebugger = _GBACoreDetachDebugger;
612#endif
613 core->cheatDevice = _GBACoreCheatDevice;
614 core->savedataClone = _GBACoreSavedataClone;
615 core->savedataRestore = _GBACoreSavedataRestore;
616 return core;
617}