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