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