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