all repos — mgba @ a0af67842add0d49b3339d5f1b3b91945ee0a47c

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	};
182	environCallback(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, &inputDescriptors);
183
184	// TODO: RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME when BIOS booting is supported
185
186	struct retro_rumble_interface rumbleInterface;
187	if (environCallback(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumbleInterface)) {
188		rumbleCallback = rumbleInterface.set_rumble_state;
189		CircleBufferInit(&rumbleHistory, RUMBLE_PWM);
190		rumble.setRumble = _setRumble;
191	} else {
192		rumbleCallback = 0;
193	}
194
195	luxLevel = 0;
196	lux.readLuminance = _readLux;
197	lux.sample = _updateLux;
198	_updateLux(&lux);
199
200	struct retro_log_callback log;
201	if (environCallback(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log)) {
202		logCallback = log.log;
203	} else {
204		logCallback = 0;
205	}
206	logger.log = GBARetroLog;
207	mLogSetDefaultLogger(&logger);
208
209	stream.videoDimensionsChanged = 0;
210	stream.postAudioFrame = 0;
211	stream.postAudioBuffer = _postAudioBuffer;
212	stream.postVideoFrame = 0;
213}
214
215void retro_deinit(void) {
216	free(outputBuffer);
217}
218
219void retro_run(void) {
220	uint16_t keys;
221	inputPollCallback();
222
223	struct retro_variable var = {
224		.key = "mgba_allow_opposing_directions",
225		.value = 0
226	};
227
228	bool updated = false;
229	if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) {
230		if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
231			((struct GBA*) core->board)->allowOpposingDirections = strcmp(var.value, "yes") == 0;
232		}
233	}
234
235	keys = 0;
236	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A)) << 0;
237	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B)) << 1;
238	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT)) << 2;
239	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START)) << 3;
240	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT)) << 4;
241	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT)) << 5;
242	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP)) << 6;
243	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN)) << 7;
244	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R)) << 8;
245	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L)) << 9;
246	core->setKeys(core, keys);
247
248	static bool wasAdjustingLux = false;
249	if (wasAdjustingLux) {
250		wasAdjustingLux = inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3) ||
251		                  inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3);
252	} else {
253		if (inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3)) {
254			++luxLevel;
255			if (luxLevel > 10) {
256				luxLevel = 10;
257			}
258			wasAdjustingLux = true;
259		} else if (inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3)) {
260			--luxLevel;
261			if (luxLevel < 0) {
262				luxLevel = 0;
263			}
264			wasAdjustingLux = true;
265		}
266	}
267
268	core->runFrame(core);
269	unsigned width, height;
270	core->desiredVideoDimensions(core, &width, &height);
271	videoCallback(outputBuffer, width, height, BYTES_PER_PIXEL * 256);
272}
273
274void retro_reset(void) {
275	core->reset(core);
276
277	if (rumbleCallback) {
278		CircleBufferClear(&rumbleHistory);
279	}
280}
281
282bool retro_load_game(const struct retro_game_info* game) {
283	struct VFile* rom;
284	if (game->data) {
285		data = anonymousMemoryMap(game->size);
286		dataSize = game->size;
287		memcpy(data, game->data, game->size);
288		rom = VFileFromMemory(data, game->size);
289	} else {
290		data = 0;
291		rom = VFileOpen(game->path, O_RDONLY);
292	}
293	if (!rom) {
294		return false;
295	}
296
297	core = mCoreFindVF(rom);
298	if (!core) {
299		rom->close(rom);
300		mappedMemoryFree(data, game->size);
301		return false;
302	}
303	mCoreInitConfig(core, NULL);
304	core->init(core);
305	core->setAVStream(core, &stream);
306
307	outputBuffer = malloc(256 * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL);
308	core->setVideoBuffer(core, outputBuffer, 256);
309
310	core->setAudioBufferSize(core, SAMPLES);
311
312	blip_set_rates(core->getAudioChannel(core, 0), core->frequency(core), 32768);
313	blip_set_rates(core->getAudioChannel(core, 1), core->frequency(core), 32768);
314
315	core->setRumble(core, &rumble);
316
317#ifdef M_CORE_GBA
318	if (core->platform(core) == PLATFORM_GBA) {
319		struct GBA* gba = core->board;
320		gba->luminanceSource = &lux;
321
322		const char* sysDir = 0;
323		if (environCallback(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &sysDir)) {
324			char biosPath[PATH_MAX];
325			snprintf(biosPath, sizeof(biosPath), "%s%s%s", sysDir, PATH_SEP, "gba_bios.bin");
326			struct VFile* bios = VFileOpen(biosPath, O_RDONLY);
327			if (bios) {
328				core->loadBIOS(core, bios, 0);
329			}
330		}
331
332		struct retro_memory_descriptor descs[11];
333		struct retro_memory_map mmaps;
334		size_t romSize = game->size + (game->size & 1);
335
336		memset(descs, 0, sizeof(descs));
337		size_t savedataSize = retro_get_memory_size(RETRO_MEMORY_SAVE_RAM);
338
339		/* Map internal working RAM */
340		descs[0].ptr    = gba->memory.iwram;
341		descs[0].start  = BASE_WORKING_IRAM;
342		descs[0].len    = SIZE_WORKING_IRAM;
343		descs[0].select = 0xFF000000;
344
345		/* Map working RAM */
346		descs[1].ptr    = gba->memory.wram;
347		descs[1].start  = BASE_WORKING_RAM;
348		descs[1].len    = SIZE_WORKING_RAM;
349		descs[1].select = 0xFF000000;
350
351		/* Map save RAM */
352		/* TODO: if SRAM is flash, use start=0 addrspace="S" instead */
353		descs[2].ptr    = savedataSize ? savedata : NULL;
354		descs[2].start  = BASE_CART_SRAM;
355		descs[2].len    = savedataSize;
356
357		/* Map ROM */
358		descs[3].ptr    = gba->memory.rom;
359		descs[3].start  = BASE_CART0;
360		descs[3].len    = romSize;
361		descs[3].flags  = RETRO_MEMDESC_CONST;
362
363		descs[4].ptr    = gba->memory.rom;
364		descs[4].start  = BASE_CART1;
365		descs[4].len    = romSize;
366		descs[4].flags  = RETRO_MEMDESC_CONST;
367
368		descs[5].ptr    = gba->memory.rom;
369		descs[5].start  = BASE_CART2;
370		descs[5].len    = romSize;
371		descs[5].flags  = RETRO_MEMDESC_CONST;
372
373		/* Map BIOS */
374		descs[6].ptr    = gba->memory.bios;
375		descs[6].start  = BASE_BIOS;
376		descs[6].len    = SIZE_BIOS;
377		descs[6].flags  = RETRO_MEMDESC_CONST;
378
379		/* Map VRAM */
380		descs[7].ptr    = gba->video.vram;
381		descs[7].start  = BASE_VRAM;
382		descs[7].len    = SIZE_VRAM;
383		descs[7].select = 0xFF000000;
384
385		/* Map palette RAM */
386		descs[8].ptr    = gba->video.palette;
387		descs[8].start  = BASE_PALETTE_RAM;
388		descs[8].len    = SIZE_PALETTE_RAM;
389		descs[8].select = 0xFF000000;
390
391		/* Map OAM */
392		descs[9].ptr    = &gba->video.oam; /* video.oam is a structure */
393		descs[9].start  = BASE_OAM;
394		descs[9].len    = SIZE_OAM;
395		descs[9].select = 0xFF000000;
396
397		/* Map mmapped I/O */
398		descs[10].ptr    = gba->memory.io;
399		descs[10].start  = BASE_IO;
400		descs[10].len    = SIZE_IO;
401
402		mmaps.descriptors = descs;
403		mmaps.num_descriptors = sizeof(descs) / sizeof(descs[0]);
404
405		bool yes = true;
406		environCallback(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps);
407		environCallback(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &yes);
408	}
409#endif
410
411	savedata = anonymousMemoryMap(SIZE_CART_FLASH1M);
412	struct VFile* save = VFileFromMemory(savedata, SIZE_CART_FLASH1M);
413
414	_reloadSettings();
415	core->loadROM(core, rom);
416	core->loadSave(core, save);
417	core->reset(core);
418	return true;
419}
420
421void retro_unload_game(void) {
422	if (!core) {
423		return;
424	}
425	core->deinit(core);
426	mappedMemoryFree(data, dataSize);
427	data = 0;
428	mappedMemoryFree(savedata, SIZE_CART_FLASH1M);
429	savedata = 0;
430	CircleBufferDeinit(&rumbleHistory);
431}
432
433size_t retro_serialize_size(void) {
434	return sizeof(struct GBASerializedState);
435}
436
437bool retro_serialize(void* data, size_t size) {
438	if (size != retro_serialize_size()) {
439		return false;
440	}
441	GBASerialize(core->board, data);
442	return true;
443}
444
445bool retro_unserialize(const void* data, size_t size) {
446	if (size != retro_serialize_size()) {
447		return false;
448	}
449	GBADeserialize(core->board, data);
450	return true;
451}
452
453void retro_cheat_reset(void) {
454	mCheatDeviceClear(core->cheatDevice(core));
455}
456
457void retro_cheat_set(unsigned index, bool enabled, const char* code) {
458	UNUSED(index);
459	UNUSED(enabled);
460	struct mCheatDevice* device = core->cheatDevice(core);
461	struct mCheatSet* cheatSet = NULL;
462	if (mCheatSetsSize(&device->cheats)) {
463		cheatSet = *mCheatSetsGetPointer(&device->cheats, 0);
464	} else {
465		cheatSet = device->createSet(device, NULL);
466		mCheatAddSet(device, cheatSet);
467	}
468	// Convert the super wonky unportable libretro format to something normal
469	char realCode[] = "XXXXXXXX XXXXXXXX";
470	size_t len = strlen(code) + 1; // Include null terminator
471	size_t i, pos;
472	for (i = 0, pos = 0; i < len; ++i) {
473		if (isspace((int) code[i]) || code[i] == '+') {
474			realCode[pos] = ' ';
475		} else {
476			realCode[pos] = code[i];
477		}
478		if ((pos == 13 && (realCode[pos] == ' ' || !realCode[pos])) || pos == 17) {
479			realCode[pos] = '\0';
480			mCheatAddLine(cheatSet, realCode, 0);
481			pos = 0;
482			continue;
483		}
484		++pos;
485	}
486}
487
488unsigned retro_get_region(void) {
489	return RETRO_REGION_NTSC; // TODO: This isn't strictly true
490}
491
492void retro_set_controller_port_device(unsigned port, unsigned device) {
493	UNUSED(port);
494	UNUSED(device);
495}
496
497bool retro_load_game_special(unsigned game_type, const struct retro_game_info* info, size_t num_info) {
498	UNUSED(game_type);
499	UNUSED(info);
500	UNUSED(num_info);
501	return false;
502}
503
504void* retro_get_memory_data(unsigned id) {
505	if (id != RETRO_MEMORY_SAVE_RAM) {
506		return 0;
507	}
508	return savedata;
509}
510
511size_t retro_get_memory_size(unsigned id) {
512	if (id != RETRO_MEMORY_SAVE_RAM) {
513		return 0;
514	}
515	switch (((struct GBA*) core->board)->memory.savedata.type) {
516	case SAVEDATA_AUTODETECT:
517	case SAVEDATA_FLASH1M:
518		return SIZE_CART_FLASH1M;
519	case SAVEDATA_FLASH512:
520		return SIZE_CART_FLASH512;
521	case SAVEDATA_EEPROM:
522		return SIZE_CART_EEPROM;
523	case SAVEDATA_SRAM:
524		return SIZE_CART_SRAM;
525	case SAVEDATA_FORCE_NONE:
526		return 0;
527	}
528	return 0;
529}
530
531void GBARetroLog(struct mLogger* logger, int category, enum mLogLevel level, const char* format, va_list args) {
532	UNUSED(logger);
533	if (!logCallback) {
534		return;
535	}
536
537	char message[128];
538	vsnprintf(message, sizeof(message), format, args);
539
540	enum retro_log_level retroLevel = RETRO_LOG_INFO;
541	switch (level) {
542	case mLOG_ERROR:
543	case mLOG_FATAL:
544		retroLevel = RETRO_LOG_ERROR;
545		break;
546	case mLOG_WARN:
547		retroLevel = RETRO_LOG_WARN;
548		break;
549	case mLOG_INFO:
550		retroLevel = RETRO_LOG_INFO;
551		break;
552	case mLOG_GAME_ERROR:
553	case mLOG_STUB:
554#ifdef NDEBUG
555		return;
556#else
557		retroLevel = RETRO_LOG_DEBUG;
558		break;
559#endif
560	case mLOG_DEBUG:
561		retroLevel = RETRO_LOG_DEBUG;
562		break;
563	}
564#ifdef NDEBUG
565	if (category == _mLOG_CAT_GBA_BIOS()) {
566		return;
567	}
568#endif
569	logCallback(retroLevel, "%s: %s\n", mLogCategoryName(category), message);
570}
571
572static void _postAudioBuffer(struct mAVStream* stream, blip_t* left, blip_t* right) {
573	UNUSED(stream);
574	int16_t samples[SAMPLES * 2];
575	blip_read_samples(left, samples, SAMPLES, true);
576	blip_read_samples(right, samples + 1, SAMPLES, true);
577	audioCallback(samples, SAMPLES);
578}
579
580static void _setRumble(struct mRumble* rumble, int enable) {
581	UNUSED(rumble);
582	if (!rumbleCallback) {
583		return;
584	}
585	rumbleLevel += enable;
586	if (CircleBufferSize(&rumbleHistory) == RUMBLE_PWM) {
587		int8_t oldLevel;
588		CircleBufferRead8(&rumbleHistory, &oldLevel);
589		rumbleLevel -= oldLevel;
590	}
591	CircleBufferWrite8(&rumbleHistory, enable);
592	rumbleCallback(0, RETRO_RUMBLE_STRONG, rumbleLevel * 0xFFFF / RUMBLE_PWM);
593	rumbleCallback(0, RETRO_RUMBLE_WEAK, rumbleLevel * 0xFFFF / RUMBLE_PWM);
594}
595
596static void _updateLux(struct GBALuminanceSource* lux) {
597	UNUSED(lux);
598	struct retro_variable var = {
599		.key = "mgba_solar_sensor_level",
600		.value = 0
601	};
602
603	bool updated = false;
604	if (!environCallback(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) || !updated) {
605		return;
606	}
607	if (!environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || !var.value) {
608		return;
609	}
610
611	char* end;
612	int newLuxLevel = strtol(var.value, &end, 10);
613	if (!*end) {
614		if (newLuxLevel > 10) {
615			luxLevel = 10;
616		} else if (newLuxLevel < 0) {
617			luxLevel = 0;
618		} else {
619			luxLevel = newLuxLevel;
620		}
621	}
622}
623
624static uint8_t _readLux(struct GBALuminanceSource* lux) {
625	UNUSED(lux);
626	int value = 0x16;
627	if (luxLevel > 0) {
628		value += GBA_LUX_LEVELS[luxLevel - 1];
629	}
630	return 0xFF - value;
631}