all repos — mgba @ 55679df8fc49fca3d5f8584d16b90e9bb734a922

mGBA Game Boy Advance Emulator

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