all repos — mgba @ 4ea859af01d6bf244698cddc2fa7529a10a25da8

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 static _setupMaps(struct mCore* core) {
275#ifdef M_CORE_GBA
276	if (core->platform(core) == PLATFORM_GBA) {
277		struct GBA* gba = core->board;
278		struct retro_memory_descriptor descs[11];
279		struct retro_memory_map mmaps;
280		size_t romSize = gba->memory.romSize + (gba->memory.romSize & 1);
281
282		memset(descs, 0, sizeof(descs));
283		size_t savedataSize = retro_get_memory_size(RETRO_MEMORY_SAVE_RAM);
284
285		/* Map internal working RAM */
286		descs[0].ptr    = gba->memory.iwram;
287		descs[0].start  = BASE_WORKING_IRAM;
288		descs[0].len    = SIZE_WORKING_IRAM;
289		descs[0].select = 0xFF000000;
290
291		/* Map working RAM */
292		descs[1].ptr    = gba->memory.wram;
293		descs[1].start  = BASE_WORKING_RAM;
294		descs[1].len    = SIZE_WORKING_RAM;
295		descs[1].select = 0xFF000000;
296
297		/* Map save RAM */
298		/* TODO: if SRAM is flash, use start=0 addrspace="S" instead */
299		descs[2].ptr    = savedataSize ? savedata : NULL;
300		descs[2].start  = BASE_CART_SRAM;
301		descs[2].len    = savedataSize;
302
303		/* Map ROM */
304		descs[3].ptr    = gba->memory.rom;
305		descs[3].start  = BASE_CART0;
306		descs[3].len    = romSize;
307		descs[3].flags  = RETRO_MEMDESC_CONST;
308
309		descs[4].ptr    = gba->memory.rom;
310		descs[4].start  = BASE_CART1;
311		descs[4].len    = romSize;
312		descs[4].flags  = RETRO_MEMDESC_CONST;
313
314		descs[5].ptr    = gba->memory.rom;
315		descs[5].start  = BASE_CART2;
316		descs[5].len    = romSize;
317		descs[5].flags  = RETRO_MEMDESC_CONST;
318
319		/* Map BIOS */
320		descs[6].ptr    = gba->memory.bios;
321		descs[6].start  = BASE_BIOS;
322		descs[6].len    = SIZE_BIOS;
323		descs[6].flags  = RETRO_MEMDESC_CONST;
324
325		/* Map VRAM */
326		descs[7].ptr    = gba->video.vram;
327		descs[7].start  = BASE_VRAM;
328		descs[7].len    = SIZE_VRAM;
329		descs[7].select = 0xFF000000;
330
331		/* Map palette RAM */
332		descs[8].ptr    = gba->video.palette;
333		descs[8].start  = BASE_PALETTE_RAM;
334		descs[8].len    = SIZE_PALETTE_RAM;
335		descs[8].select = 0xFF000000;
336
337		/* Map OAM */
338		descs[9].ptr    = &gba->video.oam; /* video.oam is a structure */
339		descs[9].start  = BASE_OAM;
340		descs[9].len    = SIZE_OAM;
341		descs[9].select = 0xFF000000;
342
343		/* Map mmapped I/O */
344		descs[10].ptr    = gba->memory.io;
345		descs[10].start  = BASE_IO;
346		descs[10].len    = SIZE_IO;
347
348		mmaps.descriptors = descs;
349		mmaps.num_descriptors = sizeof(descs) / sizeof(descs[0]);
350
351		bool yes = true;
352		environCallback(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps);
353		environCallback(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &yes);
354	}
355#endif
356}
357
358void retro_reset(void) {
359	core->reset(core);
360	_setupMaps(core);
361
362	if (rumbleCallback) {
363		CircleBufferClear(&rumbleHistory);
364	}
365}
366
367bool retro_load_game(const struct retro_game_info* game) {
368	struct VFile* rom;
369	if (game->data) {
370		data = anonymousMemoryMap(game->size);
371		dataSize = game->size;
372		memcpy(data, game->data, game->size);
373		rom = VFileFromMemory(data, game->size);
374	} else {
375		data = 0;
376		rom = VFileOpen(game->path, O_RDONLY);
377	}
378	if (!rom) {
379		return false;
380	}
381
382	core = mCoreFindVF(rom);
383	if (!core) {
384		rom->close(rom);
385		mappedMemoryFree(data, game->size);
386		return false;
387	}
388	mCoreInitConfig(core, NULL);
389	core->init(core);
390	core->setAVStream(core, &stream);
391
392	outputBuffer = malloc(256 * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL);
393	core->setVideoBuffer(core, outputBuffer, 256);
394
395	core->setAudioBufferSize(core, SAMPLES);
396
397	blip_set_rates(core->getAudioChannel(core, 0), core->frequency(core), 32768);
398	blip_set_rates(core->getAudioChannel(core, 1), core->frequency(core), 32768);
399
400	core->setRumble(core, &rumble);
401
402	savedata = anonymousMemoryMap(SIZE_CART_FLASH1M);
403	struct VFile* save = VFileFromMemory(savedata, SIZE_CART_FLASH1M);
404
405	_reloadSettings();
406	core->loadROM(core, rom);
407	core->loadSave(core, save);
408
409#ifdef M_CORE_GBA
410	if (core->platform(core) == PLATFORM_GBA) {
411		struct GBA* gba = core->board;
412		gba->luminanceSource = &lux;
413
414		const char* sysDir = 0;
415		if (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 sizeof(struct GBASerializedState);
446}
447
448bool retro_serialize(void* data, size_t size) {
449	if (size != retro_serialize_size()) {
450		return false;
451	}
452	GBASerialize(core->board, 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	GBADeserialize(core->board, 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	switch (((struct GBA*) core->board)->memory.savedata.type) {
527	case SAVEDATA_AUTODETECT:
528	case SAVEDATA_FLASH1M:
529		return SIZE_CART_FLASH1M;
530	case SAVEDATA_FLASH512:
531		return SIZE_CART_FLASH512;
532	case SAVEDATA_EEPROM:
533		return SIZE_CART_EEPROM;
534	case SAVEDATA_SRAM:
535		return SIZE_CART_SRAM;
536	case SAVEDATA_FORCE_NONE:
537		return 0;
538	}
539	return 0;
540}
541
542void GBARetroLog(struct mLogger* logger, int category, enum mLogLevel level, const char* format, va_list args) {
543	UNUSED(logger);
544	if (!logCallback) {
545		return;
546	}
547
548	char message[128];
549	vsnprintf(message, sizeof(message), format, args);
550
551	enum retro_log_level retroLevel = RETRO_LOG_INFO;
552	switch (level) {
553	case mLOG_ERROR:
554	case mLOG_FATAL:
555		retroLevel = RETRO_LOG_ERROR;
556		break;
557	case mLOG_WARN:
558		retroLevel = RETRO_LOG_WARN;
559		break;
560	case mLOG_INFO:
561		retroLevel = RETRO_LOG_INFO;
562		break;
563	case mLOG_GAME_ERROR:
564	case mLOG_STUB:
565#ifdef NDEBUG
566		return;
567#else
568		retroLevel = RETRO_LOG_DEBUG;
569		break;
570#endif
571	case mLOG_DEBUG:
572		retroLevel = RETRO_LOG_DEBUG;
573		break;
574	}
575#ifdef NDEBUG
576	if (category == _mLOG_CAT_GBA_BIOS()) {
577		return;
578	}
579#endif
580	logCallback(retroLevel, "%s: %s\n", mLogCategoryName(category), message);
581}
582
583static void _postAudioBuffer(struct mAVStream* stream, blip_t* left, blip_t* right) {
584	UNUSED(stream);
585	int16_t samples[SAMPLES * 2];
586	blip_read_samples(left, samples, SAMPLES, true);
587	blip_read_samples(right, samples + 1, SAMPLES, true);
588	audioCallback(samples, SAMPLES);
589}
590
591static void _setRumble(struct mRumble* rumble, int enable) {
592	UNUSED(rumble);
593	if (!rumbleCallback) {
594		return;
595	}
596	rumbleLevel += enable;
597	if (CircleBufferSize(&rumbleHistory) == RUMBLE_PWM) {
598		int8_t oldLevel;
599		CircleBufferRead8(&rumbleHistory, &oldLevel);
600		rumbleLevel -= oldLevel;
601	}
602	CircleBufferWrite8(&rumbleHistory, enable);
603	rumbleCallback(0, RETRO_RUMBLE_STRONG, rumbleLevel * 0xFFFF / RUMBLE_PWM);
604	rumbleCallback(0, RETRO_RUMBLE_WEAK, rumbleLevel * 0xFFFF / RUMBLE_PWM);
605}
606
607static void _updateLux(struct GBALuminanceSource* lux) {
608	UNUSED(lux);
609	struct retro_variable var = {
610		.key = "mgba_solar_sensor_level",
611		.value = 0
612	};
613
614	bool updated = false;
615	if (!environCallback(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) || !updated) {
616		return;
617	}
618	if (!environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || !var.value) {
619		return;
620	}
621
622	char* end;
623	int newLuxLevel = strtol(var.value, &end, 10);
624	if (!*end) {
625		if (newLuxLevel > 10) {
626			luxLevel = 10;
627		} else if (newLuxLevel < 0) {
628			luxLevel = 0;
629		} else {
630			luxLevel = newLuxLevel;
631		}
632	}
633}
634
635static uint8_t _readLux(struct GBALuminanceSource* lux) {
636	UNUSED(lux);
637	int value = 0x16;
638	if (luxLevel > 0) {
639		value += GBA_LUX_LEVELS[luxLevel - 1];
640	}
641	return 0xFF - value;
642}