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