src/platform/libretro/libretro.c (view raw)
1/* Copyright (c) 2013-2015 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 "libretro.h"
7
8#include <mgba-util/common.h>
9
10#include <mgba/core/blip_buf.h>
11#include <mgba/core/cheats.h>
12#include <mgba/core/core.h>
13#include <mgba/core/log.h>
14#include <mgba/core/version.h>
15#ifdef M_CORE_GB
16#include <mgba/gb/core.h>
17#include <mgba/internal/gb/gb.h>
18#endif
19#ifdef M_CORE_GBA
20#include <mgba/gba/core.h>
21#include <mgba/gba/interface.h>
22#include <mgba/internal/gba/gba.h>
23#endif
24#include <mgba-util/circle-buffer.h>
25#include <mgba-util/memory.h>
26#include <mgba-util/vfs.h>
27
28#define SAMPLES 1024
29#define RUMBLE_PWM 35
30
31static retro_environment_t environCallback;
32static retro_video_refresh_t videoCallback;
33static retro_audio_sample_batch_t audioCallback;
34static retro_input_poll_t inputPollCallback;
35static retro_input_state_t inputCallback;
36static retro_log_printf_t logCallback;
37static retro_set_rumble_state_t rumbleCallback;
38
39static void GBARetroLog(struct mLogger* logger, int category, enum mLogLevel level, const char* format, va_list args);
40
41static void _postAudioBuffer(struct mAVStream*, blip_t* left, blip_t* right);
42static void _setRumble(struct mRumble* rumble, int enable);
43static uint8_t _readLux(struct GBALuminanceSource* lux);
44static void _updateLux(struct GBALuminanceSource* lux);
45
46static struct mCore* core;
47static void* outputBuffer;
48static void* data;
49static size_t dataSize;
50static void* savedata;
51static struct mAVStream stream;
52static int rumbleLevel;
53static struct CircleBuffer rumbleHistory;
54static struct mRumble rumble;
55static struct GBALuminanceSource lux;
56static int luxLevel;
57static struct mLogger logger;
58
59static void _reloadSettings(void) {
60 struct mCoreOptions opts = {
61 .useBios = true,
62 .volume = 0x100,
63 };
64
65 struct retro_variable var;
66 enum GBModel model;
67 const char* modelName;
68
69 var.key = "mgba_gb_model";
70 var.value = 0;
71 if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
72 if (strcmp(var.value, "Game Boy") == 0) {
73 model = GB_MODEL_DMG;
74 } else if (strcmp(var.value, "Super Game Boy") == 0) {
75 model = GB_MODEL_SGB;
76 } else if (strcmp(var.value, "Game Boy Color") == 0) {
77 model = GB_MODEL_CGB;
78 } else if (strcmp(var.value, "Game Boy Advance") == 0) {
79 model = GB_MODEL_AGB;
80 } else {
81 model = GB_MODEL_AUTODETECT;
82 }
83
84 modelName = GBModelToName(model);
85 mCoreConfigSetDefaultValue(&core->config, "gb.model", modelName);
86 mCoreConfigSetDefaultValue(&core->config, "sgb.model", modelName);
87 mCoreConfigSetDefaultValue(&core->config, "cgb.model", modelName);
88 }
89
90 var.key = "mgba_use_bios";
91 var.value = 0;
92 if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
93 opts.useBios = strcmp(var.value, "ON") == 0;
94 }
95
96 var.key = "mgba_skip_bios";
97 var.value = 0;
98 if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
99 opts.skipBios = strcmp(var.value, "ON") == 0;
100 }
101
102 var.key = "mgba_sgb_borders";
103 var.value = 0;
104 if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
105 if (strcmp(var.value, "ON") == 0) {
106 mCoreConfigSetDefaultIntValue(&core->config, "sgb.borders", true);
107 } else {
108 mCoreConfigSetDefaultIntValue(&core->config, "sgb.borders", false);
109 }
110 }
111
112 var.key = "mgba_idle_optimization";
113 var.value = 0;
114 if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
115 if (strcmp(var.value, "Don't Remove") == 0) {
116 mCoreConfigSetDefaultValue(&core->config, "idleOptimization", "ignore");
117 } else if (strcmp(var.value, "Remove Known") == 0) {
118 mCoreConfigSetDefaultValue(&core->config, "idleOptimization", "remove");
119 } else if (strcmp(var.value, "Detect and Remove") == 0) {
120 mCoreConfigSetDefaultValue(&core->config, "idleOptimization", "detect");
121 }
122 }
123
124 var.key = "mgba_frameskip";
125 var.value = 0;
126 if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
127 opts.frameskip = strtol(var.value, NULL, 10);
128
129 }
130
131 mCoreConfigLoadDefaults(&core->config, &opts);
132 mCoreLoadConfig(core);
133}
134
135unsigned retro_api_version(void) {
136 return RETRO_API_VERSION;
137}
138
139void retro_set_environment(retro_environment_t env) {
140 environCallback = env;
141
142 struct retro_variable vars[] = {
143 { "mgba_solar_sensor_level", "Solar sensor level; 0|1|2|3|4|5|6|7|8|9|10" },
144 { "mgba_allow_opposing_directions", "Allow opposing directional input; OFF|ON" },
145 { "mgba_gb_model", "Game Boy model (requires restart); Autodetect|Game Boy|Super Game Boy|Game Boy Color|Game Boy Advance" },
146 { "mgba_use_bios", "Use BIOS file if found (requires restart); ON|OFF" },
147 { "mgba_skip_bios", "Skip BIOS intro (requires restart); OFF|ON" },
148 { "mgba_sgb_borders", "Use Super Game Boy borders (requires restart); ON|OFF" },
149 { "mgba_idle_optimization", "Idle loop removal; Remove Known|Detect and Remove|Don't Remove" },
150 { "mgba_frameskip", "Frameskip; 0|1|2|3|4|5|6|7|8|9|10" },
151 { 0, 0 }
152 };
153
154 environCallback(RETRO_ENVIRONMENT_SET_VARIABLES, vars);
155}
156
157void retro_set_video_refresh(retro_video_refresh_t video) {
158 videoCallback = video;
159}
160
161void retro_set_audio_sample(retro_audio_sample_t audio) {
162 UNUSED(audio);
163}
164
165void retro_set_audio_sample_batch(retro_audio_sample_batch_t audioBatch) {
166 audioCallback = audioBatch;
167}
168
169void retro_set_input_poll(retro_input_poll_t inputPoll) {
170 inputPollCallback = inputPoll;
171}
172
173void retro_set_input_state(retro_input_state_t input) {
174 inputCallback = input;
175}
176
177void retro_get_system_info(struct retro_system_info* info) {
178 info->need_fullpath = false;
179 info->valid_extensions = "gba|gb|gbc";
180 info->library_version = projectVersion;
181 info->library_name = projectName;
182 info->block_extract = false;
183}
184
185void retro_get_system_av_info(struct retro_system_av_info* info) {
186 unsigned width, height;
187 core->desiredVideoDimensions(core, &width, &height);
188 info->geometry.base_width = width;
189 info->geometry.base_height = height;
190 info->geometry.max_width = width;
191 info->geometry.max_height = height;
192 info->geometry.aspect_ratio = width / (double) height;
193 info->timing.fps = core->frequency(core) / (float) core->frameCycles(core);
194 info->timing.sample_rate = 32768;
195}
196
197void retro_init(void) {
198 enum retro_pixel_format fmt;
199#ifdef COLOR_16_BIT
200#ifdef COLOR_5_6_5
201 fmt = RETRO_PIXEL_FORMAT_RGB565;
202#else
203#warning This pixel format is unsupported. Please use -DCOLOR_16-BIT -DCOLOR_5_6_5
204 fmt = RETRO_PIXEL_FORMAT_0RGB1555;
205#endif
206#else
207#warning This pixel format is unsupported. Please use -DCOLOR_16-BIT -DCOLOR_5_6_5
208 fmt = RETRO_PIXEL_FORMAT_XRGB8888;
209#endif
210 environCallback(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt);
211
212 struct retro_input_descriptor inputDescriptors[] = {
213 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
214 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
215 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
216 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
217 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
218 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
219 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
220 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
221 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R" },
222 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L" },
223 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "Brighten Solar Sensor" },
224 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "Darken Solar Sensor" },
225 { 0 }
226 };
227 environCallback(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, &inputDescriptors);
228
229 // TODO: RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME when BIOS booting is supported
230
231 struct retro_rumble_interface rumbleInterface;
232 if (environCallback(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumbleInterface)) {
233 rumbleCallback = rumbleInterface.set_rumble_state;
234 CircleBufferInit(&rumbleHistory, RUMBLE_PWM);
235 rumble.setRumble = _setRumble;
236 } else {
237 rumbleCallback = 0;
238 }
239
240 luxLevel = 0;
241 lux.readLuminance = _readLux;
242 lux.sample = _updateLux;
243 _updateLux(&lux);
244
245 struct retro_log_callback log;
246 if (environCallback(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log)) {
247 logCallback = log.log;
248 } else {
249 logCallback = 0;
250 }
251 logger.log = GBARetroLog;
252 mLogSetDefaultLogger(&logger);
253
254 stream.videoDimensionsChanged = 0;
255 stream.postAudioFrame = 0;
256 stream.postAudioBuffer = _postAudioBuffer;
257 stream.postVideoFrame = 0;
258}
259
260void retro_deinit(void) {
261 free(outputBuffer);
262}
263
264void retro_run(void) {
265 uint16_t keys;
266 inputPollCallback();
267
268 bool updated = false;
269 if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) {
270 struct retro_variable var = {
271 .key = "mgba_allow_opposing_directions",
272 .value = 0
273 };
274 if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
275 ((struct GBA*) core->board)->allowOpposingDirections = strcmp(var.value, "yes") == 0;
276 }
277
278 var.key = "mgba_frameskip";
279 var.value = 0;
280 if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
281 mCoreConfigSetUIntValue(&core->config, "frameskip", strtol(var.value, NULL, 10));
282 mCoreLoadConfig(core);
283 }
284 }
285
286 keys = 0;
287 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A)) << 0;
288 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B)) << 1;
289 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT)) << 2;
290 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START)) << 3;
291 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT)) << 4;
292 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT)) << 5;
293 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP)) << 6;
294 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN)) << 7;
295 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R)) << 8;
296 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L)) << 9;
297 core->setKeys(core, keys);
298
299 static bool wasAdjustingLux = false;
300 if (wasAdjustingLux) {
301 wasAdjustingLux = inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3) ||
302 inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3);
303 } else {
304 if (inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3)) {
305 ++luxLevel;
306 if (luxLevel > 10) {
307 luxLevel = 10;
308 }
309 wasAdjustingLux = true;
310 } else if (inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3)) {
311 --luxLevel;
312 if (luxLevel < 0) {
313 luxLevel = 0;
314 }
315 wasAdjustingLux = true;
316 }
317 }
318
319 core->runFrame(core);
320 unsigned width, height;
321 core->desiredVideoDimensions(core, &width, &height);
322 videoCallback(outputBuffer, width, height, BYTES_PER_PIXEL * 256);
323}
324
325static void _setupMaps(struct mCore* core) {
326#ifdef M_CORE_GBA
327 if (core->platform(core) == PLATFORM_GBA) {
328 struct GBA* gba = core->board;
329 struct retro_memory_descriptor descs[11];
330 struct retro_memory_map mmaps;
331 size_t romSize = gba->memory.romSize + (gba->memory.romSize & 1);
332
333 memset(descs, 0, sizeof(descs));
334 size_t savedataSize = retro_get_memory_size(RETRO_MEMORY_SAVE_RAM);
335
336 /* Map internal working RAM */
337 descs[0].ptr = gba->memory.iwram;
338 descs[0].start = BASE_WORKING_IRAM;
339 descs[0].len = SIZE_WORKING_IRAM;
340 descs[0].select = 0xFF000000;
341
342 /* Map working RAM */
343 descs[1].ptr = gba->memory.wram;
344 descs[1].start = BASE_WORKING_RAM;
345 descs[1].len = SIZE_WORKING_RAM;
346 descs[1].select = 0xFF000000;
347
348 /* Map save RAM */
349 /* TODO: if SRAM is flash, use start=0 addrspace="S" instead */
350 descs[2].ptr = savedataSize ? savedata : NULL;
351 descs[2].start = BASE_CART_SRAM;
352 descs[2].len = savedataSize;
353
354 /* Map ROM */
355 descs[3].ptr = gba->memory.rom;
356 descs[3].start = BASE_CART0;
357 descs[3].len = romSize;
358 descs[3].flags = RETRO_MEMDESC_CONST;
359
360 descs[4].ptr = gba->memory.rom;
361 descs[4].start = BASE_CART1;
362 descs[4].len = romSize;
363 descs[4].flags = RETRO_MEMDESC_CONST;
364
365 descs[5].ptr = gba->memory.rom;
366 descs[5].start = BASE_CART2;
367 descs[5].len = romSize;
368 descs[5].flags = RETRO_MEMDESC_CONST;
369
370 /* Map BIOS */
371 descs[6].ptr = gba->memory.bios;
372 descs[6].start = BASE_BIOS;
373 descs[6].len = SIZE_BIOS;
374 descs[6].flags = RETRO_MEMDESC_CONST;
375
376 /* Map VRAM */
377 descs[7].ptr = gba->video.vram;
378 descs[7].start = BASE_VRAM;
379 descs[7].len = SIZE_VRAM;
380 descs[7].select = 0xFF000000;
381
382 /* Map palette RAM */
383 descs[8].ptr = gba->video.palette;
384 descs[8].start = BASE_PALETTE_RAM;
385 descs[8].len = SIZE_PALETTE_RAM;
386 descs[8].select = 0xFF000000;
387
388 /* Map OAM */
389 descs[9].ptr = &gba->video.oam; /* video.oam is a structure */
390 descs[9].start = BASE_OAM;
391 descs[9].len = SIZE_OAM;
392 descs[9].select = 0xFF000000;
393
394 /* Map mmapped I/O */
395 descs[10].ptr = gba->memory.io;
396 descs[10].start = BASE_IO;
397 descs[10].len = SIZE_IO;
398
399 mmaps.descriptors = descs;
400 mmaps.num_descriptors = sizeof(descs) / sizeof(descs[0]);
401
402 bool yes = true;
403 environCallback(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps);
404 environCallback(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &yes);
405 }
406#endif
407}
408
409void retro_reset(void) {
410 core->reset(core);
411 _setupMaps(core);
412
413 if (rumbleCallback) {
414 CircleBufferClear(&rumbleHistory);
415 }
416}
417
418bool retro_load_game(const struct retro_game_info* game) {
419 struct VFile* rom;
420 if (game->data) {
421 data = anonymousMemoryMap(game->size);
422 dataSize = game->size;
423 memcpy(data, game->data, game->size);
424 rom = VFileFromMemory(data, game->size);
425 } else {
426 data = 0;
427 rom = VFileOpen(game->path, O_RDONLY);
428 }
429 if (!rom) {
430 return false;
431 }
432
433 core = mCoreFindVF(rom);
434 if (!core) {
435 rom->close(rom);
436 mappedMemoryFree(data, game->size);
437 return false;
438 }
439 mCoreInitConfig(core, NULL);
440 core->init(core);
441 core->setAVStream(core, &stream);
442
443 size_t size = 256 * 224 * BYTES_PER_PIXEL;
444 outputBuffer = malloc(size);
445 memset(outputBuffer, 0xFF, size);
446 core->setVideoBuffer(core, outputBuffer, 256);
447
448 core->setAudioBufferSize(core, SAMPLES);
449
450 blip_set_rates(core->getAudioChannel(core, 0), core->frequency(core), 32768);
451 blip_set_rates(core->getAudioChannel(core, 1), core->frequency(core), 32768);
452
453 core->setPeripheral(core, mPERIPH_RUMBLE, &rumble);
454
455 savedata = anonymousMemoryMap(SIZE_CART_FLASH1M);
456 struct VFile* save = VFileFromMemory(savedata, SIZE_CART_FLASH1M);
457
458 _reloadSettings();
459 core->loadROM(core, rom);
460 core->loadSave(core, save);
461
462 const char* sysDir = 0;
463 const char* biosName = 0;
464 char biosPath[PATH_MAX];
465 environCallback(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &sysDir);
466
467#ifdef M_CORE_GBA
468 if (core->platform(core) == PLATFORM_GBA) {
469 core->setPeripheral(core, mPERIPH_GBA_LUMINANCE, &lux);
470 biosName = "gba_bios.bin";
471
472 }
473#endif
474
475#ifdef M_CORE_GB
476 if (core->platform(core) == PLATFORM_GB) {
477 const char* modelName = mCoreConfigGetValue(&core->config, "gb.model");
478 struct GB* gb = core->board;
479
480 if (modelName) {
481 gb->model = GBNameToModel(modelName);
482 } else {
483 GBDetectModel(gb);
484 }
485
486 switch (gb->model) {
487 case GB_MODEL_AGB:
488 case GB_MODEL_CGB:
489 biosName = "gbc_bios.bin";
490 break;
491 case GB_MODEL_SGB:
492 biosName = "sgb_bios.bin";
493 break;
494 case GB_MODEL_DMG:
495 default:
496 biosName = "gb_bios.bin";
497 break;
498 };
499 }
500#endif
501
502 if (core->opts.useBios && sysDir && biosName) {
503 snprintf(biosPath, sizeof(biosPath), "%s%s%s", sysDir, PATH_SEP, biosName);
504 struct VFile* bios = VFileOpen(biosPath, O_RDONLY);
505 if (bios) {
506 core->loadBIOS(core, bios, 0);
507 }
508 }
509
510 core->reset(core);
511 _setupMaps(core);
512
513 return true;
514}
515
516void retro_unload_game(void) {
517 if (!core) {
518 return;
519 }
520 core->deinit(core);
521 mappedMemoryFree(data, dataSize);
522 data = 0;
523 mappedMemoryFree(savedata, SIZE_CART_FLASH1M);
524 savedata = 0;
525 CircleBufferDeinit(&rumbleHistory);
526}
527
528size_t retro_serialize_size(void) {
529 return core->stateSize(core);
530}
531
532bool retro_serialize(void* data, size_t size) {
533 if (size != retro_serialize_size()) {
534 return false;
535 }
536 core->saveState(core, data);
537 return true;
538}
539
540bool retro_unserialize(const void* data, size_t size) {
541 if (size != retro_serialize_size()) {
542 return false;
543 }
544 core->loadState(core, data);
545 return true;
546}
547
548void retro_cheat_reset(void) {
549 mCheatDeviceClear(core->cheatDevice(core));
550}
551
552void retro_cheat_set(unsigned index, bool enabled, const char* code) {
553 UNUSED(index);
554 UNUSED(enabled);
555 struct mCheatDevice* device = core->cheatDevice(core);
556 struct mCheatSet* cheatSet = NULL;
557 if (mCheatSetsSize(&device->cheats)) {
558 cheatSet = *mCheatSetsGetPointer(&device->cheats, 0);
559 } else {
560 cheatSet = device->createSet(device, NULL);
561 mCheatAddSet(device, cheatSet);
562 }
563 // Convert the super wonky unportable libretro format to something normal
564 char realCode[] = "XXXXXXXX XXXXXXXX";
565 size_t len = strlen(code) + 1; // Include null terminator
566 size_t i, pos;
567 for (i = 0, pos = 0; i < len; ++i) {
568 if (isspace((int) code[i]) || code[i] == '+') {
569 realCode[pos] = ' ';
570 } else {
571 realCode[pos] = code[i];
572 }
573 if ((pos == 13 && (realCode[pos] == ' ' || !realCode[pos])) || pos == 17) {
574 realCode[pos] = '\0';
575 mCheatAddLine(cheatSet, realCode, 0);
576 pos = 0;
577 continue;
578 }
579 ++pos;
580 }
581}
582
583unsigned retro_get_region(void) {
584 return RETRO_REGION_NTSC; // TODO: This isn't strictly true
585}
586
587void retro_set_controller_port_device(unsigned port, unsigned device) {
588 UNUSED(port);
589 UNUSED(device);
590}
591
592bool retro_load_game_special(unsigned game_type, const struct retro_game_info* info, size_t num_info) {
593 UNUSED(game_type);
594 UNUSED(info);
595 UNUSED(num_info);
596 return false;
597}
598
599void* retro_get_memory_data(unsigned id) {
600 if (id != RETRO_MEMORY_SAVE_RAM) {
601 return 0;
602 }
603 return savedata;
604}
605
606size_t retro_get_memory_size(unsigned id) {
607 if (id != RETRO_MEMORY_SAVE_RAM) {
608 return 0;
609 }
610#ifdef M_CORE_GBA
611 if (core->platform(core) == PLATFORM_GBA) {
612 switch (((struct GBA*) core->board)->memory.savedata.type) {
613 case SAVEDATA_AUTODETECT:
614 return SIZE_CART_FLASH1M;
615 default:
616 return GBASavedataSize(&((struct GBA*) core->board)->memory.savedata);
617 }
618 }
619#endif
620#ifdef M_CORE_GB
621 if (core->platform(core) == PLATFORM_GB) {
622 return ((struct GB*) core->board)->sramSize;
623 }
624#endif
625 return 0;
626}
627
628void GBARetroLog(struct mLogger* logger, int category, enum mLogLevel level, const char* format, va_list args) {
629 UNUSED(logger);
630 if (!logCallback) {
631 return;
632 }
633
634 char message[128];
635 vsnprintf(message, sizeof(message), format, args);
636
637 enum retro_log_level retroLevel = RETRO_LOG_INFO;
638 switch (level) {
639 case mLOG_ERROR:
640 case mLOG_FATAL:
641 retroLevel = RETRO_LOG_ERROR;
642 break;
643 case mLOG_WARN:
644 retroLevel = RETRO_LOG_WARN;
645 break;
646 case mLOG_INFO:
647 retroLevel = RETRO_LOG_INFO;
648 break;
649 case mLOG_GAME_ERROR:
650 case mLOG_STUB:
651#ifdef NDEBUG
652 return;
653#else
654 retroLevel = RETRO_LOG_DEBUG;
655 break;
656#endif
657 case mLOG_DEBUG:
658 retroLevel = RETRO_LOG_DEBUG;
659 break;
660 }
661#ifdef NDEBUG
662 static int biosCat = -1;
663 if (biosCat < 0) {
664 biosCat = mLogCategoryById("gba.bios");
665 }
666
667 if (category == biosCat) {
668 return;
669 }
670#endif
671 logCallback(retroLevel, "%s: %s\n", mLogCategoryName(category), message);
672}
673
674static void _postAudioBuffer(struct mAVStream* stream, blip_t* left, blip_t* right) {
675 UNUSED(stream);
676 int16_t samples[SAMPLES * 2];
677 blip_read_samples(left, samples, SAMPLES, true);
678 blip_read_samples(right, samples + 1, SAMPLES, true);
679 audioCallback(samples, SAMPLES);
680}
681
682static void _setRumble(struct mRumble* rumble, int enable) {
683 UNUSED(rumble);
684 if (!rumbleCallback) {
685 return;
686 }
687 rumbleLevel += enable;
688 if (CircleBufferSize(&rumbleHistory) == RUMBLE_PWM) {
689 int8_t oldLevel;
690 CircleBufferRead8(&rumbleHistory, &oldLevel);
691 rumbleLevel -= oldLevel;
692 }
693 CircleBufferWrite8(&rumbleHistory, enable);
694 rumbleCallback(0, RETRO_RUMBLE_STRONG, rumbleLevel * 0xFFFF / RUMBLE_PWM);
695 rumbleCallback(0, RETRO_RUMBLE_WEAK, rumbleLevel * 0xFFFF / RUMBLE_PWM);
696}
697
698static void _updateLux(struct GBALuminanceSource* lux) {
699 UNUSED(lux);
700 struct retro_variable var = {
701 .key = "mgba_solar_sensor_level",
702 .value = 0
703 };
704
705 bool updated = false;
706 if (!environCallback(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) || !updated) {
707 return;
708 }
709 if (!environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || !var.value) {
710 return;
711 }
712
713 char* end;
714 int newLuxLevel = strtol(var.value, &end, 10);
715 if (!*end) {
716 if (newLuxLevel > 10) {
717 luxLevel = 10;
718 } else if (newLuxLevel < 0) {
719 luxLevel = 0;
720 } else {
721 luxLevel = newLuxLevel;
722 }
723 }
724}
725
726static uint8_t _readLux(struct GBALuminanceSource* lux) {
727 UNUSED(lux);
728 int value = 0x16;
729 if (luxLevel > 0) {
730 value += GBA_LUX_LEVELS[luxLevel - 1];
731 }
732 return 0xFF - value;
733}