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