all repos — mgba @ 10a5432df72a68bf178163954e2dafdc92f7a742

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/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	mCoreConfigLoadDefaults(&core->config, &opts);
 92	mCoreLoadConfig(core);
 93}
 94
 95unsigned retro_api_version(void) {
 96	return RETRO_API_VERSION;
 97}
 98
 99void retro_set_environment(retro_environment_t env) {
100	environCallback = env;
101
102	struct retro_variable vars[] = {
103		{ "mgba_solar_sensor_level", "Solar sensor level; 0|1|2|3|4|5|6|7|8|9|10" },
104		{ "mgba_allow_opposing_directions", "Allow opposing directional input; OFF|ON" },
105		{ "mgba_use_bios", "Use BIOS file if found; ON|OFF" },
106		{ "mgba_skip_bios", "Skip BIOS intro; OFF|ON" },
107		{ "mgba_idle_optimization", "Idle loop removal; Remove Known|Detect and Remove|Don't Remove" },
108		{ 0, 0 }
109	};
110
111	environCallback(RETRO_ENVIRONMENT_SET_VARIABLES, vars);
112}
113
114void retro_set_video_refresh(retro_video_refresh_t video) {
115	videoCallback = video;
116}
117
118void retro_set_audio_sample(retro_audio_sample_t audio) {
119	UNUSED(audio);
120}
121
122void retro_set_audio_sample_batch(retro_audio_sample_batch_t audioBatch) {
123	audioCallback = audioBatch;
124}
125
126void retro_set_input_poll(retro_input_poll_t inputPoll) {
127	inputPollCallback = inputPoll;
128}
129
130void retro_set_input_state(retro_input_state_t input) {
131	inputCallback = input;
132}
133
134void retro_get_system_info(struct retro_system_info* info) {
135	info->need_fullpath = false;
136	info->valid_extensions = "gba|gb|gbc";
137	info->library_version = projectVersion;
138	info->library_name = projectName;
139	info->block_extract = false;
140}
141
142void retro_get_system_av_info(struct retro_system_av_info* info) {
143	unsigned width, height;
144	core->desiredVideoDimensions(core, &width, &height);
145	info->geometry.base_width = width;
146	info->geometry.base_height = height;
147	info->geometry.max_width = width;
148	info->geometry.max_height = height;
149	info->geometry.aspect_ratio = width / (double) height;
150	info->timing.fps = core->frequency(core) / (float) core->frameCycles(core);
151	info->timing.sample_rate = 32768;
152}
153
154void retro_init(void) {
155	enum retro_pixel_format fmt;
156#ifdef COLOR_16_BIT
157#ifdef COLOR_5_6_5
158	fmt = RETRO_PIXEL_FORMAT_RGB565;
159#else
160#warning This pixel format is unsupported. Please use -DCOLOR_16-BIT -DCOLOR_5_6_5
161	fmt = RETRO_PIXEL_FORMAT_0RGB1555;
162#endif
163#else
164#warning This pixel format is unsupported. Please use -DCOLOR_16-BIT -DCOLOR_5_6_5
165	fmt = RETRO_PIXEL_FORMAT_XRGB8888;
166#endif
167	environCallback(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt);
168
169	struct retro_input_descriptor inputDescriptors[] = {
170		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
171		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
172		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
173		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
174		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
175		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
176		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
177		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
178		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R" },
179		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L" },
180		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "Brighten Solar Sensor" },
181		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "Darken Solar Sensor" },
182		{ 0 }
183	};
184	environCallback(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, &inputDescriptors);
185
186	// TODO: RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME when BIOS booting is supported
187
188	struct retro_rumble_interface rumbleInterface;
189	if (environCallback(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumbleInterface)) {
190		rumbleCallback = rumbleInterface.set_rumble_state;
191		CircleBufferInit(&rumbleHistory, RUMBLE_PWM);
192		rumble.setRumble = _setRumble;
193	} else {
194		rumbleCallback = 0;
195	}
196
197	luxLevel = 0;
198	lux.readLuminance = _readLux;
199	lux.sample = _updateLux;
200	_updateLux(&lux);
201
202	struct retro_log_callback log;
203	if (environCallback(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log)) {
204		logCallback = log.log;
205	} else {
206		logCallback = 0;
207	}
208	logger.log = GBARetroLog;
209	mLogSetDefaultLogger(&logger);
210
211	stream.videoDimensionsChanged = 0;
212	stream.postAudioFrame = 0;
213	stream.postAudioBuffer = _postAudioBuffer;
214	stream.postVideoFrame = 0;
215}
216
217void retro_deinit(void) {
218	free(outputBuffer);
219}
220
221void retro_run(void) {
222	uint16_t keys;
223	inputPollCallback();
224
225	struct retro_variable var = {
226		.key = "mgba_allow_opposing_directions",
227		.value = 0
228	};
229
230	bool updated = false;
231	if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) {
232		if (environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
233			((struct GBA*) core->board)->allowOpposingDirections = strcmp(var.value, "yes") == 0;
234		}
235	}
236
237	keys = 0;
238	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A)) << 0;
239	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B)) << 1;
240	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT)) << 2;
241	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START)) << 3;
242	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT)) << 4;
243	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT)) << 5;
244	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP)) << 6;
245	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN)) << 7;
246	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R)) << 8;
247	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L)) << 9;
248	core->setKeys(core, keys);
249
250	static bool wasAdjustingLux = false;
251	if (wasAdjustingLux) {
252		wasAdjustingLux = inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3) ||
253		                  inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3);
254	} else {
255		if (inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3)) {
256			++luxLevel;
257			if (luxLevel > 10) {
258				luxLevel = 10;
259			}
260			wasAdjustingLux = true;
261		} else if (inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3)) {
262			--luxLevel;
263			if (luxLevel < 0) {
264				luxLevel = 0;
265			}
266			wasAdjustingLux = true;
267		}
268	}
269
270	core->runFrame(core);
271	unsigned width, height;
272	core->desiredVideoDimensions(core, &width, &height);
273	videoCallback(outputBuffer, width, height, BYTES_PER_PIXEL * 256);
274}
275
276void static _setupMaps(struct mCore* core) {
277#ifdef M_CORE_GBA
278	if (core->platform(core) == PLATFORM_GBA) {
279		struct GBA* gba = core->board;
280		struct retro_memory_descriptor descs[11];
281		struct retro_memory_map mmaps;
282		size_t romSize = gba->memory.romSize + (gba->memory.romSize & 1);
283
284		memset(descs, 0, sizeof(descs));
285		size_t savedataSize = retro_get_memory_size(RETRO_MEMORY_SAVE_RAM);
286
287		/* Map internal working RAM */
288		descs[0].ptr    = gba->memory.iwram;
289		descs[0].start  = BASE_WORKING_IRAM;
290		descs[0].len    = SIZE_WORKING_IRAM;
291		descs[0].select = 0xFF000000;
292
293		/* Map working RAM */
294		descs[1].ptr    = gba->memory.wram;
295		descs[1].start  = BASE_WORKING_RAM;
296		descs[1].len    = SIZE_WORKING_RAM;
297		descs[1].select = 0xFF000000;
298
299		/* Map save RAM */
300		/* TODO: if SRAM is flash, use start=0 addrspace="S" instead */
301		descs[2].ptr    = savedataSize ? savedata : NULL;
302		descs[2].start  = BASE_CART_SRAM;
303		descs[2].len    = savedataSize;
304
305		/* Map ROM */
306		descs[3].ptr    = gba->memory.rom;
307		descs[3].start  = BASE_CART0;
308		descs[3].len    = romSize;
309		descs[3].flags  = RETRO_MEMDESC_CONST;
310
311		descs[4].ptr    = gba->memory.rom;
312		descs[4].start  = BASE_CART1;
313		descs[4].len    = romSize;
314		descs[4].flags  = RETRO_MEMDESC_CONST;
315
316		descs[5].ptr    = gba->memory.rom;
317		descs[5].start  = BASE_CART2;
318		descs[5].len    = romSize;
319		descs[5].flags  = RETRO_MEMDESC_CONST;
320
321		/* Map BIOS */
322		descs[6].ptr    = gba->memory.bios;
323		descs[6].start  = BASE_BIOS;
324		descs[6].len    = SIZE_BIOS;
325		descs[6].flags  = RETRO_MEMDESC_CONST;
326
327		/* Map VRAM */
328		descs[7].ptr    = gba->video.vram;
329		descs[7].start  = BASE_VRAM;
330		descs[7].len    = SIZE_VRAM;
331		descs[7].select = 0xFF000000;
332
333		/* Map palette RAM */
334		descs[8].ptr    = gba->video.palette;
335		descs[8].start  = BASE_PALETTE_RAM;
336		descs[8].len    = SIZE_PALETTE_RAM;
337		descs[8].select = 0xFF000000;
338
339		/* Map OAM */
340		descs[9].ptr    = &gba->video.oam; /* video.oam is a structure */
341		descs[9].start  = BASE_OAM;
342		descs[9].len    = SIZE_OAM;
343		descs[9].select = 0xFF000000;
344
345		/* Map mmapped I/O */
346		descs[10].ptr    = gba->memory.io;
347		descs[10].start  = BASE_IO;
348		descs[10].len    = SIZE_IO;
349
350		mmaps.descriptors = descs;
351		mmaps.num_descriptors = sizeof(descs) / sizeof(descs[0]);
352
353		bool yes = true;
354		environCallback(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps);
355		environCallback(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &yes);
356	}
357#endif
358}
359
360void retro_reset(void) {
361	core->reset(core);
362	_setupMaps(core);
363
364	if (rumbleCallback) {
365		CircleBufferClear(&rumbleHistory);
366	}
367}
368
369bool retro_load_game(const struct retro_game_info* game) {
370	struct VFile* rom;
371	if (game->data) {
372		data = anonymousMemoryMap(game->size);
373		dataSize = game->size;
374		memcpy(data, game->data, game->size);
375		rom = VFileFromMemory(data, game->size);
376	} else {
377		data = 0;
378		rom = VFileOpen(game->path, O_RDONLY);
379	}
380	if (!rom) {
381		return false;
382	}
383
384	core = mCoreFindVF(rom);
385	if (!core) {
386		rom->close(rom);
387		mappedMemoryFree(data, game->size);
388		return false;
389	}
390	mCoreInitConfig(core, NULL);
391	core->init(core);
392	core->setAVStream(core, &stream);
393
394	outputBuffer = malloc(256 * 224 * BYTES_PER_PIXEL);
395	core->setVideoBuffer(core, outputBuffer, 256);
396
397	core->setAudioBufferSize(core, SAMPLES);
398
399	blip_set_rates(core->getAudioChannel(core, 0), core->frequency(core), 32768);
400	blip_set_rates(core->getAudioChannel(core, 1), core->frequency(core), 32768);
401
402	core->setPeripheral(core, mPERIPH_RUMBLE, &rumble);
403
404	savedata = anonymousMemoryMap(SIZE_CART_FLASH1M);
405	struct VFile* save = VFileFromMemory(savedata, SIZE_CART_FLASH1M);
406
407	_reloadSettings();
408	core->loadROM(core, rom);
409	core->loadSave(core, save);
410
411#ifdef M_CORE_GBA
412	if (core->platform(core) == PLATFORM_GBA) {
413		core->setPeripheral(core, mPERIPH_GBA_LUMINANCE, &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 core->stateSize(core);
447}
448
449bool retro_serialize(void* data, size_t size) {
450	if (size != retro_serialize_size()) {
451		return false;
452	}
453	core->saveState(core, 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	core->loadState(core, 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#ifdef M_CORE_GBA
528	if (core->platform(core) == PLATFORM_GBA) {
529		switch (((struct GBA*) core->board)->memory.savedata.type) {
530		case SAVEDATA_AUTODETECT:
531		case SAVEDATA_FLASH1M:
532			return SIZE_CART_FLASH1M;
533		case SAVEDATA_FLASH512:
534			return SIZE_CART_FLASH512;
535		case SAVEDATA_EEPROM:
536			return SIZE_CART_EEPROM;
537		case SAVEDATA_SRAM:
538			return SIZE_CART_SRAM;
539		case SAVEDATA_FORCE_NONE:
540			return 0;
541		}
542	}
543#endif
544#ifdef M_CORE_GB
545	if (core->platform(core) == PLATFORM_GB) {
546		return ((struct GB*) core->board)->sramSize;
547	}
548#endif
549	return 0;
550}
551
552void GBARetroLog(struct mLogger* logger, int category, enum mLogLevel level, const char* format, va_list args) {
553	UNUSED(logger);
554	if (!logCallback) {
555		return;
556	}
557
558	char message[128];
559	vsnprintf(message, sizeof(message), format, args);
560
561	enum retro_log_level retroLevel = RETRO_LOG_INFO;
562	switch (level) {
563	case mLOG_ERROR:
564	case mLOG_FATAL:
565		retroLevel = RETRO_LOG_ERROR;
566		break;
567	case mLOG_WARN:
568		retroLevel = RETRO_LOG_WARN;
569		break;
570	case mLOG_INFO:
571		retroLevel = RETRO_LOG_INFO;
572		break;
573	case mLOG_GAME_ERROR:
574	case mLOG_STUB:
575#ifdef NDEBUG
576		return;
577#else
578		retroLevel = RETRO_LOG_DEBUG;
579		break;
580#endif
581	case mLOG_DEBUG:
582		retroLevel = RETRO_LOG_DEBUG;
583		break;
584	}
585#ifdef NDEBUG
586	static int biosCat = -1;
587	if (biosCat < 0) {
588		biosCat = mLogCategoryById("gba.bios");
589	}
590
591	if (category == biosCat) {
592		return;
593	}
594#endif
595	logCallback(retroLevel, "%s: %s\n", mLogCategoryName(category), message);
596}
597
598static void _postAudioBuffer(struct mAVStream* stream, blip_t* left, blip_t* right) {
599	UNUSED(stream);
600	int16_t samples[SAMPLES * 2];
601	blip_read_samples(left, samples, SAMPLES, true);
602	blip_read_samples(right, samples + 1, SAMPLES, true);
603	audioCallback(samples, SAMPLES);
604}
605
606static void _setRumble(struct mRumble* rumble, int enable) {
607	UNUSED(rumble);
608	if (!rumbleCallback) {
609		return;
610	}
611	rumbleLevel += enable;
612	if (CircleBufferSize(&rumbleHistory) == RUMBLE_PWM) {
613		int8_t oldLevel;
614		CircleBufferRead8(&rumbleHistory, &oldLevel);
615		rumbleLevel -= oldLevel;
616	}
617	CircleBufferWrite8(&rumbleHistory, enable);
618	rumbleCallback(0, RETRO_RUMBLE_STRONG, rumbleLevel * 0xFFFF / RUMBLE_PWM);
619	rumbleCallback(0, RETRO_RUMBLE_WEAK, rumbleLevel * 0xFFFF / RUMBLE_PWM);
620}
621
622static void _updateLux(struct GBALuminanceSource* lux) {
623	UNUSED(lux);
624	struct retro_variable var = {
625		.key = "mgba_solar_sensor_level",
626		.value = 0
627	};
628
629	bool updated = false;
630	if (!environCallback(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) || !updated) {
631		return;
632	}
633	if (!environCallback(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || !var.value) {
634		return;
635	}
636
637	char* end;
638	int newLuxLevel = strtol(var.value, &end, 10);
639	if (!*end) {
640		if (newLuxLevel > 10) {
641			luxLevel = 10;
642		} else if (newLuxLevel < 0) {
643			luxLevel = 0;
644		} else {
645			luxLevel = newLuxLevel;
646		}
647	}
648}
649
650static uint8_t _readLux(struct GBALuminanceSource* lux) {
651	UNUSED(lux);
652	int value = 0x16;
653	if (luxLevel > 0) {
654		value += GBA_LUX_LEVELS[luxLevel - 1];
655	}
656	return 0xFF - value;
657}