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 "gba/cheats.h"
11#include "gba/gba.h"
12#include "gba/extra/cli.h"
13#include "gba/overrides.h"
14#include "gba/renderers/video-software.h"
15#include "gba/serialize.h"
16#include "util/memory.h"
17#include "util/patch.h"
18#include "util/vfs.h"
19
20struct GBACore {
21 struct mCore d;
22 struct GBAVideoSoftwareRenderer renderer;
23 int keys;
24 struct mCPUComponent* components[CPU_COMPONENT_MAX];
25 const struct Configuration* overrides;
26 struct mDebuggerPlatform* debuggerPlatform;
27 struct mCheatDevice* cheatDevice;
28};
29
30static bool _GBACoreInit(struct mCore* core) {
31 struct GBACore* gbacore = (struct GBACore*) core;
32
33 struct ARMCore* cpu = anonymousMemoryMap(sizeof(struct ARMCore));
34 struct GBA* gba = anonymousMemoryMap(sizeof(struct GBA));
35 if (!cpu || !gba) {
36 free(cpu);
37 free(gba);
38 return false;
39 }
40 core->cpu = cpu;
41 core->board = gba;
42 core->debugger = NULL;
43 gbacore->overrides = NULL;
44 gbacore->debuggerPlatform = NULL;
45 gbacore->cheatDevice = NULL;
46
47 GBACreate(gba);
48 // TODO: Restore cheats
49 memset(gbacore->components, 0, sizeof(gbacore->components));
50 ARMSetComponents(cpu, &gba->d, CPU_COMPONENT_MAX, gbacore->components);
51 ARMInit(cpu);
52
53 GBAVideoSoftwareRendererCreate(&gbacore->renderer);
54 gbacore->renderer.outputBuffer = NULL;
55
56 gbacore->keys = 0;
57 gba->keySource = &gbacore->keys;
58
59#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
60 mDirectorySetInit(&core->dirs);
61#endif
62
63 return true;
64}
65
66static void _GBACoreDeinit(struct mCore* core) {
67 ARMDeinit(core->cpu);
68 GBADestroy(core->board);
69 mappedMemoryFree(core->cpu, sizeof(struct ARMCore));
70 mappedMemoryFree(core->board, sizeof(struct GBA));
71#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
72 mDirectorySetDeinit(&core->dirs);
73#endif
74
75 struct GBACore* gbacore = (struct GBACore*) core;
76 free(gbacore->debuggerPlatform);
77 if (gbacore->cheatDevice) {
78 mCheatDeviceDestroy(gbacore->cheatDevice);
79 }
80 free(gbacore->cheatDevice);
81 free(core);
82}
83
84static enum mPlatform _GBACorePlatform(struct mCore* core) {
85 UNUSED(core);
86 return PLATFORM_GBA;
87}
88
89static void _GBACoreSetSync(struct mCore* core, struct mCoreSync* sync) {
90 struct GBA* gba = core->board;
91 gba->sync = sync;
92}
93
94static void _GBACoreLoadConfig(struct mCore* core, const struct mCoreConfig* config) {
95 struct GBA* gba = core->board;
96 if (core->opts.mute) {
97 gba->audio.masterVolume = 0;
98 } else {
99 gba->audio.masterVolume = core->opts.volume;
100 }
101 gba->video.frameskip = core->opts.frameskip;
102
103#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
104 struct GBACore* gbacore = (struct GBACore*) core;
105 gbacore->overrides = mCoreConfigGetOverridesConst(config);
106
107 struct VFile* bios = 0;
108 if (core->opts.useBios && core->opts.bios) {
109 bios = VFileOpen(core->opts.bios, O_RDONLY);
110 }
111 if (bios) {
112 GBALoadBIOS(gba, bios);
113 }
114#endif
115
116 const char* idleOptimization = mCoreConfigGetValue(config, "idleOptimization");
117 if (idleOptimization) {
118 if (strcasecmp(idleOptimization, "ignore") == 0) {
119 gba->idleOptimization = IDLE_LOOP_IGNORE;
120 } else if (strcasecmp(idleOptimization, "remove") == 0) {
121 gba->idleOptimization = IDLE_LOOP_REMOVE;
122 } else if (strcasecmp(idleOptimization, "detect") == 0) {
123 gba->idleOptimization = IDLE_LOOP_DETECT;
124 }
125 }
126}
127
128static void _GBACoreDesiredVideoDimensions(struct mCore* core, unsigned* width, unsigned* height) {
129 UNUSED(core);
130 *width = VIDEO_HORIZONTAL_PIXELS;
131 *height = VIDEO_VERTICAL_PIXELS;
132}
133
134static void _GBACoreSetVideoBuffer(struct mCore* core, color_t* buffer, size_t stride) {
135 struct GBACore* gbacore = (struct GBACore*) core;
136 gbacore->renderer.outputBuffer = buffer;
137 gbacore->renderer.outputBufferStride = stride;
138}
139
140static void _GBACoreGetVideoBuffer(struct mCore* core, color_t** buffer, size_t* stride) {
141 struct GBACore* gbacore = (struct GBACore*) core;
142 *buffer = gbacore->renderer.outputBuffer;
143 *stride = gbacore->renderer.outputBufferStride;
144}
145
146static struct blip_t* _GBACoreGetAudioChannel(struct mCore* core, int ch) {
147 struct GBA* gba = core->board;
148 switch (ch) {
149 case 0:
150 return gba->audio.psg.left;
151 case 1:
152 return gba->audio.psg.right;
153 default:
154 return NULL;
155 }
156}
157
158static void _GBACoreSetAudioBufferSize(struct mCore* core, size_t samples) {
159 struct GBA* gba = core->board;
160 GBAAudioResizeBuffer(&gba->audio, samples);
161}
162
163static size_t _GBACoreGetAudioBufferSize(struct mCore* core) {
164 struct GBA* gba = core->board;
165 return gba->audio.samples;
166}
167
168static void _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) {
169 struct GBA* gba = core->board;
170 gba->stream = stream;
171 if (stream && stream->videoDimensionsChanged) {
172 stream->videoDimensionsChanged(stream, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
173 }
174}
175
176static bool _GBACoreLoadROM(struct mCore* core, struct VFile* vf) {
177 return GBALoadROM(core->board, vf);
178}
179
180static bool _GBACoreLoadBIOS(struct mCore* core, struct VFile* vf, int type) {
181 UNUSED(type);
182 if (!GBAIsBIOS(vf)) {
183 return false;
184 }
185 GBALoadBIOS(core->board, vf);
186 return true;
187}
188
189static bool _GBACoreLoadSave(struct mCore* core, struct VFile* vf) {
190 return GBALoadSave(core->board, vf);
191}
192
193static bool _GBACoreLoadPatch(struct mCore* core, struct VFile* vf) {
194 if (!vf) {
195 return false;
196 }
197 struct Patch patch;
198 if (!loadPatch(vf, &patch)) {
199 return false;
200 }
201 GBAApplyPatch(core->board, &patch);
202 return true;
203}
204
205static void _GBACoreUnloadROM(struct mCore* core) {
206 return GBAUnloadROM(core->board);
207}
208
209static void _GBACoreReset(struct mCore* core) {
210 struct GBACore* gbacore = (struct GBACore*) core;
211 struct GBA* gba = (struct GBA*) core->board;
212 if (gbacore->renderer.outputBuffer) {
213 GBAVideoAssociateRenderer(&gba->video, &gbacore->renderer.d);
214 }
215 ARMReset(core->cpu);
216 if (core->opts.skipBios) {
217 GBASkipBIOS(core->board);
218 }
219
220 struct GBACartridgeOverride override;
221 const struct GBACartridge* cart = (const struct GBACartridge*) gba->memory.rom;
222 if (cart) {
223 memcpy(override.id, &cart->id, sizeof(override.id));
224 if (GBAOverrideFind(gbacore->overrides, &override)) {
225 GBAOverrideApply(gba, &override);
226 }
227 }
228}
229
230static void _GBACoreRunFrame(struct mCore* core) {
231 struct GBA* gba = core->board;
232 int32_t frameCounter = gba->video.frameCounter;
233 while (gba->video.frameCounter == frameCounter) {
234 ARMRunLoop(core->cpu);
235 }
236}
237
238static void _GBACoreRunLoop(struct mCore* core) {
239 ARMRunLoop(core->cpu);
240}
241
242static void _GBACoreStep(struct mCore* core) {
243 ARMRun(core->cpu);
244}
245
246static bool _GBACoreLoadState(struct mCore* core, struct VFile* vf, int flags) {
247 return GBALoadStateNamed(core->board, vf, flags);
248}
249
250static bool _GBACoreSaveState(struct mCore* core, struct VFile* vf, int flags) {
251 return GBASaveStateNamed(core->board, vf, flags);
252}
253
254static void _GBACoreSetKeys(struct mCore* core, uint32_t keys) {
255 struct GBACore* gbacore = (struct GBACore*) core;
256 gbacore->keys = keys;
257}
258
259static void _GBACoreAddKeys(struct mCore* core, uint32_t keys) {
260 struct GBACore* gbacore = (struct GBACore*) core;
261 gbacore->keys |= keys;
262}
263
264static void _GBACoreClearKeys(struct mCore* core, uint32_t keys) {
265 struct GBACore* gbacore = (struct GBACore*) core;
266 gbacore->keys &= ~keys;
267}
268
269static int32_t _GBACoreFrameCounter(struct mCore* core) {
270 struct GBA* gba = core->board;
271 return gba->video.frameCounter;
272}
273
274static int32_t _GBACoreFrameCycles(struct mCore* core) {
275 UNUSED(core);
276 return VIDEO_TOTAL_LENGTH;
277}
278
279static int32_t _GBACoreFrequency(struct mCore* core) {
280 UNUSED(core);
281 return GBA_ARM7TDMI_FREQUENCY;
282}
283
284static void _GBACoreGetGameTitle(struct mCore* core, char* title) {
285 GBAGetGameTitle(core->board, title);
286}
287
288static void _GBACoreGetGameCode(struct mCore* core, char* title) {
289 GBAGetGameCode(core->board, title);
290}
291
292static void _GBACoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
293 struct GBA* gba = core->board;
294 gba->rtcSource = rtc;
295}
296
297static void _GBACoreSetRotation(struct mCore* core, struct mRotationSource* rotation) {
298 struct GBA* gba = core->board;
299 gba->rotationSource = rotation;
300}
301
302static void _GBACoreSetRumble(struct mCore* core, struct mRumble* rumble) {
303 struct GBA* gba = core->board;
304 gba->rumble = rumble;
305}
306
307static uint32_t _GBACoreBusRead8(struct mCore* core, uint32_t address) {
308 struct ARMCore* cpu = core->cpu;
309 return cpu->memory.load8(cpu, address, 0);
310}
311
312static uint32_t _GBACoreBusRead16(struct mCore* core, uint32_t address) {
313 struct ARMCore* cpu = core->cpu;
314 return cpu->memory.load16(cpu, address, 0);
315
316}
317
318static uint32_t _GBACoreBusRead32(struct mCore* core, uint32_t address) {
319 struct ARMCore* cpu = core->cpu;
320 return cpu->memory.load32(cpu, address, 0);
321}
322
323static void _GBACoreBusWrite8(struct mCore* core, uint32_t address, uint8_t value) {
324 struct ARMCore* cpu = core->cpu;
325 cpu->memory.store8(cpu, address, value, 0);
326}
327
328static void _GBACoreBusWrite16(struct mCore* core, uint32_t address, uint16_t value) {
329 struct ARMCore* cpu = core->cpu;
330 cpu->memory.store16(cpu, address, value, 0);
331}
332
333static void _GBACoreBusWrite32(struct mCore* core, uint32_t address, uint32_t value) {
334 struct ARMCore* cpu = core->cpu;
335 cpu->memory.store32(cpu, address, value, 0);
336}
337
338static uint32_t _GBACoreRawRead8(struct mCore* core, uint32_t address) {
339 struct ARMCore* cpu = core->cpu;
340 return GBAView8(cpu, address);
341}
342
343static uint32_t _GBACoreRawRead16(struct mCore* core, uint32_t address) {
344 struct ARMCore* cpu = core->cpu;
345 return GBAView16(cpu, address);
346}
347
348static uint32_t _GBACoreRawRead32(struct mCore* core, uint32_t address) {
349 struct ARMCore* cpu = core->cpu;
350 return GBAView32(cpu, address);
351}
352
353static void _GBACoreRawWrite8(struct mCore* core, uint32_t address, uint8_t value) {
354 struct ARMCore* cpu = core->cpu;
355 GBAPatch8(cpu, address, value, NULL);
356}
357
358static void _GBACoreRawWrite16(struct mCore* core, uint32_t address, uint16_t value) {
359 struct ARMCore* cpu = core->cpu;
360 GBAPatch16(cpu, address, value, NULL);
361}
362
363static void _GBACoreRawWrite32(struct mCore* core, uint32_t address, uint32_t value) {
364 struct ARMCore* cpu = core->cpu;
365 GBAPatch32(cpu, address, value, NULL);
366}
367
368static bool _GBACoreSupportsDebuggerType(struct mCore* core, enum mDebuggerType type) {
369 UNUSED(core);
370 switch (type) {
371#ifdef USE_CLI_DEBUGGER
372 case DEBUGGER_CLI:
373 return true;
374#endif
375#ifdef USE_GDB_STUB
376 case DEBUGGER_GDB:
377 return true;
378#endif
379 default:
380 return false;
381 }
382}
383
384static struct mDebuggerPlatform* _GBACoreDebuggerPlatform(struct mCore* core) {
385 struct GBACore* gbacore = (struct GBACore*) core;
386 if (!gbacore->debuggerPlatform) {
387 gbacore->debuggerPlatform = ARMDebuggerPlatformCreate();
388 }
389 return gbacore->debuggerPlatform;
390}
391
392static struct CLIDebuggerSystem* _GBACoreCliDebuggerSystem(struct mCore* core) {
393#ifdef USE_CLI_DEBUGGER
394 return &GBACLIDebuggerCreate(core)->d;
395#else
396 UNUSED(core);
397 return NULL;
398#endif
399}
400
401static void _GBACoreAttachDebugger(struct mCore* core, struct mDebugger* debugger) {
402 if (core->debugger) {
403 GBADetachDebugger(core->board);
404 }
405 GBAAttachDebugger(core->board, debugger);
406 core->debugger = debugger;
407}
408
409static void _GBACoreDetachDebugger(struct mCore* core) {
410 GBADetachDebugger(core->board);
411 core->debugger = NULL;
412}
413
414static struct mCheatDevice* _GBACoreCheatDevice(struct mCore* core) {
415 struct GBACore* gbacore = (struct GBACore*) core;
416 if (!gbacore->cheatDevice) {
417 gbacore->cheatDevice = GBACheatDeviceCreate();
418 ((struct ARMCore*) core->cpu)->components[CPU_COMPONENT_CHEAT_DEVICE] = &gbacore->cheatDevice->d;
419 ARMHotplugAttach(core->cpu, CPU_COMPONENT_CHEAT_DEVICE);
420 gbacore->cheatDevice->p = core;
421 }
422 return gbacore->cheatDevice;
423}
424
425struct mCore* GBACoreCreate(void) {
426 struct GBACore* gbacore = malloc(sizeof(*gbacore));
427 struct mCore* core = &gbacore->d;
428 memset(&core->opts, 0, sizeof(core->opts));
429 core->cpu = NULL;
430 core->board = NULL;
431 core->debugger = NULL;
432 core->init = _GBACoreInit;
433 core->deinit = _GBACoreDeinit;
434 core->platform = _GBACorePlatform;
435 core->setSync = _GBACoreSetSync;
436 core->loadConfig = _GBACoreLoadConfig;
437 core->desiredVideoDimensions = _GBACoreDesiredVideoDimensions;
438 core->setVideoBuffer = _GBACoreSetVideoBuffer;
439 core->getVideoBuffer = _GBACoreGetVideoBuffer;
440 core->getAudioChannel = _GBACoreGetAudioChannel;
441 core->setAudioBufferSize = _GBACoreSetAudioBufferSize;
442 core->getAudioBufferSize = _GBACoreGetAudioBufferSize;
443 core->setAVStream = _GBACoreSetAVStream;
444 core->isROM = GBAIsROM;
445 core->loadROM = _GBACoreLoadROM;
446 core->loadBIOS = _GBACoreLoadBIOS;
447 core->loadSave = _GBACoreLoadSave;
448 core->loadPatch = _GBACoreLoadPatch;
449 core->unloadROM = _GBACoreUnloadROM;
450 core->reset = _GBACoreReset;
451 core->runFrame = _GBACoreRunFrame;
452 core->runLoop = _GBACoreRunLoop;
453 core->step = _GBACoreStep;
454 core->loadState = _GBACoreLoadState;
455 core->saveState = _GBACoreSaveState;
456 core->setKeys = _GBACoreSetKeys;
457 core->addKeys = _GBACoreAddKeys;
458 core->clearKeys = _GBACoreClearKeys;
459 core->frameCounter = _GBACoreFrameCounter;
460 core->frameCycles = _GBACoreFrameCycles;
461 core->frequency = _GBACoreFrequency;
462 core->getGameTitle = _GBACoreGetGameTitle;
463 core->getGameCode = _GBACoreGetGameCode;
464 core->setRTC = _GBACoreSetRTC;
465 core->setRotation = _GBACoreSetRotation;
466 core->setRumble = _GBACoreSetRumble;
467 core->busRead8 = _GBACoreBusRead8;
468 core->busRead16 = _GBACoreBusRead16;
469 core->busRead32 = _GBACoreBusRead32;
470 core->busWrite8 = _GBACoreBusWrite8;
471 core->busWrite16 = _GBACoreBusWrite16;
472 core->busWrite32 = _GBACoreBusWrite32;
473 core->rawRead8 = _GBACoreRawRead8;
474 core->rawRead16 = _GBACoreRawRead16;
475 core->rawRead32 = _GBACoreRawRead32;
476 core->rawWrite8 = _GBACoreRawWrite8;
477 core->rawWrite16 = _GBACoreRawWrite16;
478 core->rawWrite32 = _GBACoreRawWrite32;
479 core->supportsDebuggerType = _GBACoreSupportsDebuggerType;
480 core->debuggerPlatform = _GBACoreDebuggerPlatform;
481 core->cliDebuggerSystem = _GBACoreCliDebuggerSystem;
482 core->attachDebugger = _GBACoreAttachDebugger;
483 core->detachDebugger = _GBACoreDetachDebugger;
484 core->cheatDevice = _GBACoreCheatDevice;
485 return core;
486}