all repos — mgba @ 19b164d560f6382b2e8ee55cc8a6eec22951ae29

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 "util/common.h"
  9
 10#include "core/core.h"
 11#include "core/version.h"
 12#ifdef M_CORE_GB
 13#include "gb/core.h"
 14#include "gb/gb.h"
 15#endif
 16#ifdef M_CORE_GBA
 17#include "gba/bios.h"
 18#include "gba/core.h"
 19#include "gba/cheats.h"
 20#include "gba/core.h"
 21#include "gba/serialize.h"
 22#endif
 23#include "util/circle-buffer.h"
 24#include "util/memory.h"
 25#include "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 = GBA_ARM7TDMI_FREQUENCY / (float) VIDEO_TOTAL_LENGTH;
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->setRumble(core, &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		struct GBA* gba = core->board;
413		gba->luminanceSource = &lux;
414
415		const char* sysDir = 0;
416		if (core->opts.useBios && environCallback(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &sysDir)) {
417			char biosPath[PATH_MAX];
418			snprintf(biosPath, sizeof(biosPath), "%s%s%s", sysDir, PATH_SEP, "gba_bios.bin");
419			struct VFile* bios = VFileOpen(biosPath, O_RDONLY);
420			if (bios) {
421				core->loadBIOS(core, bios, 0);
422			}
423		}
424	}
425#endif
426
427	core->reset(core);
428	_setupMaps(core);
429
430	return true;
431}
432
433void retro_unload_game(void) {
434	if (!core) {
435		return;
436	}
437	core->deinit(core);
438	mappedMemoryFree(data, dataSize);
439	data = 0;
440	mappedMemoryFree(savedata, SIZE_CART_FLASH1M);
441	savedata = 0;
442	CircleBufferDeinit(&rumbleHistory);
443}
444
445size_t retro_serialize_size(void) {
446	return sizeof(struct GBASerializedState);
447}
448
449bool retro_serialize(void* data, size_t size) {
450	if (size != retro_serialize_size()) {
451		return false;
452	}
453	GBASerialize(core->board, data);
454	return true;
455}
456
457bool retro_unserialize(const void* data, size_t size) {
458	if (size != retro_serialize_size()) {
459		return false;
460	}
461	GBADeserialize(core->board, data);
462	return true;
463}
464
465void retro_cheat_reset(void) {
466	mCheatDeviceClear(core->cheatDevice(core));
467}
468
469void retro_cheat_set(unsigned index, bool enabled, const char* code) {
470	UNUSED(index);
471	UNUSED(enabled);
472	struct mCheatDevice* device = core->cheatDevice(core);
473	struct mCheatSet* cheatSet = NULL;
474	if (mCheatSetsSize(&device->cheats)) {
475		cheatSet = *mCheatSetsGetPointer(&device->cheats, 0);
476	} else {
477		cheatSet = device->createSet(device, NULL);
478		mCheatAddSet(device, cheatSet);
479	}
480	// Convert the super wonky unportable libretro format to something normal
481	char realCode[] = "XXXXXXXX XXXXXXXX";
482	size_t len = strlen(code) + 1; // Include null terminator
483	size_t i, pos;
484	for (i = 0, pos = 0; i < len; ++i) {
485		if (isspace((int) code[i]) || code[i] == '+') {
486			realCode[pos] = ' ';
487		} else {
488			realCode[pos] = code[i];
489		}
490		if ((pos == 13 && (realCode[pos] == ' ' || !realCode[pos])) || pos == 17) {
491			realCode[pos] = '\0';
492			mCheatAddLine(cheatSet, realCode, 0);
493			pos = 0;
494			continue;
495		}
496		++pos;
497	}
498}
499
500unsigned retro_get_region(void) {
501	return RETRO_REGION_NTSC; // TODO: This isn't strictly true
502}
503
504void retro_set_controller_port_device(unsigned port, unsigned device) {
505	UNUSED(port);
506	UNUSED(device);
507}
508
509bool retro_load_game_special(unsigned game_type, const struct retro_game_info* info, size_t num_info) {
510	UNUSED(game_type);
511	UNUSED(info);
512	UNUSED(num_info);
513	return false;
514}
515
516void* retro_get_memory_data(unsigned id) {
517	if (id != RETRO_MEMORY_SAVE_RAM) {
518		return 0;
519	}
520	return savedata;
521}
522
523size_t retro_get_memory_size(unsigned id) {
524	if (id != RETRO_MEMORY_SAVE_RAM) {
525		return 0;
526	}
527	switch (((struct GBA*) core->board)->memory.savedata.type) {
528	case SAVEDATA_AUTODETECT:
529	case SAVEDATA_FLASH1M:
530		return SIZE_CART_FLASH1M;
531	case SAVEDATA_FLASH512:
532		return SIZE_CART_FLASH512;
533	case SAVEDATA_EEPROM:
534		return SIZE_CART_EEPROM;
535	case SAVEDATA_SRAM:
536		return SIZE_CART_SRAM;
537	case SAVEDATA_FORCE_NONE:
538		return 0;
539	}
540	return 0;
541}
542
543void GBARetroLog(struct mLogger* logger, int category, enum mLogLevel level, const char* format, va_list args) {
544	UNUSED(logger);
545	if (!logCallback) {
546		return;
547	}
548
549	char message[128];
550	vsnprintf(message, sizeof(message), format, args);
551
552	enum retro_log_level retroLevel = RETRO_LOG_INFO;
553	switch (level) {
554	case mLOG_ERROR:
555	case mLOG_FATAL:
556		retroLevel = RETRO_LOG_ERROR;
557		break;
558	case mLOG_WARN:
559		retroLevel = RETRO_LOG_WARN;
560		break;
561	case mLOG_INFO:
562		retroLevel = RETRO_LOG_INFO;
563		break;
564	case mLOG_GAME_ERROR:
565	case mLOG_STUB:
566#ifdef NDEBUG
567		return;
568#else
569		retroLevel = RETRO_LOG_DEBUG;
570		break;
571#endif
572	case mLOG_DEBUG:
573		retroLevel = RETRO_LOG_DEBUG;
574		break;
575	}
576#ifdef NDEBUG
577	if (category == _mLOG_CAT_GBA_BIOS()) {
578		return;
579	}
580#endif
581	logCallback(retroLevel, "%s: %s\n", mLogCategoryName(category), message);
582}
583
584static void _postAudioBuffer(struct mAVStream* stream, blip_t* left, blip_t* right) {
585	UNUSED(stream);
586	int16_t samples[SAMPLES * 2];
587	blip_read_samples(left, samples, SAMPLES, true);
588	blip_read_samples(right, samples + 1, SAMPLES, true);
589	audioCallback(samples, SAMPLES);
590}
591
592static void _setRumble(struct mRumble* rumble, int enable) {
593	UNUSED(rumble);
594	if (!rumbleCallback) {
595		return;
596	}
597	rumbleLevel += enable;
598	if (CircleBufferSize(&rumbleHistory) == RUMBLE_PWM) {
599		int8_t oldLevel;
600		CircleBufferRead8(&rumbleHistory, &oldLevel);
601		rumbleLevel -= oldLevel;
602	}
603	CircleBufferWrite8(&rumbleHistory, enable);
604	rumbleCallback(0, RETRO_RUMBLE_STRONG, rumbleLevel * 0xFFFF / RUMBLE_PWM);
605	rumbleCallback(0, RETRO_RUMBLE_WEAK, rumbleLevel * 0xFFFF / RUMBLE_PWM);
606}
607
608static void _updateLux(struct GBALuminanceSource* lux) {
609	UNUSED(lux);
610	struct retro_variable var = {
611		.key = "mgba_solar_sensor_level",
612		.value = 0
613	};
614
615	bool updated = false;
616	if (!environCallback(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) || !updated) {
617		return;
618	}
619	if (!environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || !var.value) {
620		return;
621	}
622
623	char* end;
624	int newLuxLevel = strtol(var.value, &end, 10);
625	if (!*end) {
626		if (newLuxLevel > 10) {
627			luxLevel = 10;
628		} else if (newLuxLevel < 0) {
629			luxLevel = 0;
630		} else {
631			luxLevel = newLuxLevel;
632		}
633	}
634}
635
636static uint8_t _readLux(struct GBALuminanceSource* lux) {
637	UNUSED(lux);
638	int value = 0x16;
639	if (luxLevel > 0) {
640		value += GBA_LUX_LEVELS[luxLevel - 1];
641	}
642	return 0xFF - value;
643}