all repos — mgba @ 1975fc77062eb05c3482723556914803bc400066

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 "gba/gba.h"
 11#include "gba/renderers/video-software.h"
 12#include "gba/serialize.h"
 13#include "gba/supervisor/overrides.h"
 14#include "gba/video.h"
 15#include "util/circle-buffer.h"
 16#include "util/vfs.h"
 17
 18#define SAMPLES 1024
 19#define RUMBLE_PWM 35
 20
 21static retro_environment_t environCallback;
 22static retro_video_refresh_t videoCallback;
 23static retro_audio_sample_batch_t audioCallback;
 24static retro_input_poll_t inputPollCallback;
 25static retro_input_state_t inputCallback;
 26static retro_log_printf_t logCallback;
 27static retro_set_rumble_state_t rumbleCallback;
 28
 29static void GBARetroLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args);
 30
 31static void _postAudioBuffer(struct GBAAVStream*, struct GBAAudio* audio);
 32static void _postVideoFrame(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
 33static void _setRumble(struct GBARumble* rumble, int enable);
 34
 35static struct GBA gba;
 36static struct ARMCore cpu;
 37static struct GBAVideoSoftwareRenderer renderer;
 38static struct VFile* rom;
 39static void* data;
 40static struct VFile* save;
 41static void* savedata;
 42static struct GBAAVStream stream;
 43static int rumbleLevel;
 44static struct CircleBuffer rumbleHistory;
 45static struct GBARumble rumble;
 46
 47unsigned retro_api_version(void) {
 48   return RETRO_API_VERSION;
 49}
 50
 51void retro_set_environment(retro_environment_t env) {
 52	environCallback = env;
 53}
 54
 55void retro_set_video_refresh(retro_video_refresh_t video) {
 56	videoCallback = video;
 57}
 58
 59void retro_set_audio_sample(retro_audio_sample_t audio) {
 60	UNUSED(audio);
 61}
 62
 63void retro_set_audio_sample_batch(retro_audio_sample_batch_t audioBatch) {
 64	audioCallback = audioBatch;
 65}
 66
 67void retro_set_input_poll(retro_input_poll_t inputPoll) {
 68	inputPollCallback = inputPoll;
 69}
 70
 71void retro_set_input_state(retro_input_state_t input) {
 72	inputCallback = input;
 73}
 74
 75void retro_get_system_info(struct retro_system_info* info) {
 76   info->need_fullpath = false;
 77   info->valid_extensions = "gba";
 78   info->library_version = projectVersion;
 79   info->library_name = projectName;
 80   info->block_extract = false;
 81}
 82
 83void retro_get_system_av_info(struct retro_system_av_info* info) {
 84   info->geometry.base_width = VIDEO_HORIZONTAL_PIXELS;
 85   info->geometry.base_height = VIDEO_VERTICAL_PIXELS;
 86   info->geometry.max_width = VIDEO_HORIZONTAL_PIXELS;
 87   info->geometry.max_height = VIDEO_VERTICAL_PIXELS;
 88   info->timing.fps =  GBA_ARM7TDMI_FREQUENCY / (float) VIDEO_TOTAL_LENGTH;
 89   info->timing.sample_rate = 32768;
 90}
 91
 92void retro_init(void) {
 93	enum retro_pixel_format fmt;
 94#ifdef COLOR_16_BIT
 95#ifdef COLOR_5_6_5
 96	fmt = RETRO_PIXEL_FORMAT_RGB565;
 97#else
 98#warning This pixel format is unsupported. Please use -DCOLOR_16-BIT -DCOLOR_5_6_5
 99	fmt = RETRO_PIXEL_FORMAT_0RGB1555;
100#endif
101#else
102#warning This pixel format is unsupported. Please use -DCOLOR_16-BIT -DCOLOR_5_6_5
103	fmt = RETRO_PIXEL_FORMAT_XRGB8888;
104#endif
105	environCallback(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt);
106
107	struct retro_input_descriptor inputDescriptors[] = {
108		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
109		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
110		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
111		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
112		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
113		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
114		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
115		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
116		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R" },
117		{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L" }
118	};
119	environCallback(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, &inputDescriptors);
120
121	// TODO: RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME when BIOS booting is supported
122
123	struct retro_rumble_interface rumbleInterface;
124	if (environCallback(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumbleInterface)) {
125		rumbleCallback = rumbleInterface.set_rumble_state;
126		CircleBufferInit(&rumbleHistory, RUMBLE_PWM);
127		rumble.setRumble = _setRumble;
128	} else {
129		rumbleCallback = 0;
130	}
131
132	struct retro_log_callback log;
133	if (environCallback(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log)) {
134		logCallback = log.log;
135	} else {
136		logCallback = 0;
137	}
138
139	stream.postAudioFrame = 0;
140	stream.postAudioBuffer = _postAudioBuffer;
141	stream.postVideoFrame = _postVideoFrame;
142
143	GBACreate(&gba);
144	ARMSetComponents(&cpu, &gba.d, 0, 0);
145	ARMInit(&cpu);
146	gba.logLevel = 0; // TODO: Settings
147	gba.logHandler = GBARetroLog;
148	gba.stream = &stream;
149	gba.idleOptimization = IDLE_LOOP_REMOVE; // TODO: Settings
150	if (rumbleCallback) {
151		gba.rumble = &rumble;
152	}
153	rom = 0;
154
155	GBAVideoSoftwareRendererCreate(&renderer);
156	renderer.outputBuffer = malloc(256 * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL);
157	renderer.outputBufferStride = 256;
158	GBAVideoAssociateRenderer(&gba.video, &renderer.d);
159
160	GBAAudioResizeBuffer(&gba.audio, SAMPLES);
161
162#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
163	blip_set_rates(gba.audio.left,  GBA_ARM7TDMI_FREQUENCY, 32768);
164	blip_set_rates(gba.audio.right, GBA_ARM7TDMI_FREQUENCY, 32768);
165#endif
166}
167
168void retro_deinit(void) {
169	GBADestroy(&gba);
170}
171
172void retro_run(void) {
173	int keys;
174	gba.keySource = &keys;
175	inputPollCallback();
176
177	keys = 0;
178	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A)) << 0;
179	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B)) << 1;
180	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT)) << 2;
181	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START)) << 3;
182	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT)) << 4;
183	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT)) << 5;
184	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP)) << 6;
185	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN)) << 7;
186	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R)) << 8;
187	keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L)) << 9;
188
189	int frameCount = gba.video.frameCounter;
190	while (gba.video.frameCounter == frameCount) {
191		ARMRunLoop(&cpu);
192	}
193}
194
195void retro_reset(void) {
196	ARMReset(&cpu);
197
198	if (rumbleCallback) {
199		CircleBufferClear(&rumbleHistory);
200	}
201}
202
203bool retro_load_game(const struct retro_game_info* game) {
204	if (game->data) {
205		data = malloc(game->size);
206		memcpy(data, game->data, game->size);
207		rom = VFileFromMemory(data, game->size);
208	} else {
209		data = 0;
210		rom = VFileOpen(game->path, O_RDONLY);
211	}
212	if (!rom) {
213		return false;
214	}
215	if (!GBAIsROM(rom)) {
216		return false;
217	}
218
219	savedata = malloc(SIZE_CART_FLASH1M);
220	save = VFileFromMemory(savedata, SIZE_CART_FLASH1M);
221
222	GBALoadROM(&gba, rom, save, game->path);
223
224	struct GBACartridgeOverride override;
225	const struct GBACartridge* cart = (const struct GBACartridge*) gba.memory.rom;
226	memcpy(override.id, &cart->id, sizeof(override.id));
227	if (GBAOverrideFind(0, &override)) {
228		GBAOverrideApply(&gba, &override);
229	}
230
231	ARMReset(&cpu);
232	return true;
233}
234
235void retro_unload_game(void) {
236	rom->close(rom);
237	rom = 0;
238	free(data);
239	data = 0;
240	save->close(save);
241	save = 0;
242	free(savedata);
243	savedata = 0;
244	CircleBufferDeinit(&rumbleHistory);
245}
246
247size_t retro_serialize_size(void) {
248	return sizeof(struct GBASerializedState);
249}
250
251bool retro_serialize(void* data, size_t size) {
252	if (size != retro_serialize_size()) {
253		return false;
254	}
255	GBASerialize(&gba, data);
256	return true;
257}
258
259bool retro_unserialize(const void* data, size_t size) {
260	if (size != retro_serialize_size()) {
261		return false;
262	}
263	GBADeserialize(&gba, data);
264	return true;
265}
266
267void retro_cheat_reset(void) {
268	// TODO: Cheats
269}
270
271void retro_cheat_set(unsigned index, bool enabled, const char* code) {
272	// TODO: Cheats
273	UNUSED(index);
274	UNUSED(enabled);
275	UNUSED(code);
276}
277
278unsigned retro_get_region(void) {
279	return RETRO_REGION_NTSC; // TODO: This isn't strictly true
280}
281
282void retro_set_controller_port_device(unsigned port, unsigned device) {
283	UNUSED(port);
284	UNUSED(device);
285}
286
287bool retro_load_game_special(unsigned game_type, const struct retro_game_info* info, size_t num_info) {
288	UNUSED(game_type);
289	UNUSED(info);
290	UNUSED(num_info);
291	return false;
292}
293
294void* retro_get_memory_data(unsigned id) {
295	if (id != RETRO_MEMORY_SAVE_RAM) {
296		return 0;
297	}
298	return savedata;
299}
300
301size_t retro_get_memory_size(unsigned id) {
302	if (id != RETRO_MEMORY_SAVE_RAM) {
303		return 0;
304	}
305	switch (gba.memory.savedata.type) {
306	case SAVEDATA_AUTODETECT:
307	case SAVEDATA_FLASH1M:
308		return SIZE_CART_FLASH1M;
309	case SAVEDATA_FLASH512:
310		return SIZE_CART_FLASH512;
311	case SAVEDATA_EEPROM:
312		return SIZE_CART_EEPROM;
313	case SAVEDATA_SRAM:
314		return SIZE_CART_SRAM;
315	case SAVEDATA_FORCE_NONE:
316		return 0;
317	}
318	return 0;
319}
320
321void GBARetroLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
322	UNUSED(thread);
323	if (!logCallback) {
324		return;
325	}
326
327	char message[128];
328	vsnprintf(message, sizeof(message), format, args);
329
330	enum retro_log_level retroLevel = RETRO_LOG_INFO;
331	switch (level) {
332	case GBA_LOG_ALL:
333	case GBA_LOG_ERROR:
334	case GBA_LOG_FATAL:
335		retroLevel = RETRO_LOG_ERROR;
336		break;
337	case GBA_LOG_WARN:
338		retroLevel = RETRO_LOG_WARN;
339		break;
340	case GBA_LOG_INFO:
341	case GBA_LOG_GAME_ERROR:
342	case GBA_LOG_SWI:
343	case GBA_LOG_STATUS:
344		retroLevel = RETRO_LOG_INFO;
345		break;
346	case GBA_LOG_DEBUG:
347	case GBA_LOG_STUB:
348	case GBA_LOG_SIO:
349		retroLevel = RETRO_LOG_DEBUG;
350		break;
351	}
352	logCallback(retroLevel, "%s\n", message);
353}
354
355static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio) {
356	UNUSED(stream);
357	int16_t samples[SAMPLES * 2];
358#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
359	blip_read_samples(audio->left, samples, SAMPLES, true);
360	blip_read_samples(audio->right, samples + 1, SAMPLES, true);
361#else
362	int16_t samplesR[SAMPLES];
363	GBAAudioCopy(audio, &samples[SAMPLES], samplesR, SAMPLES);
364	size_t i;
365	for (i = 0; i < SAMPLES; ++i) {
366		samples[i * 2] = samples[SAMPLES + i];
367		samples[i * 2 + 1] = samplesR[i];
368	}
369#endif
370	audioCallback(samples, SAMPLES);
371}
372
373static void _postVideoFrame(struct GBAAVStream* stream, struct GBAVideoRenderer* renderer) {
374	UNUSED(stream);
375	void* pixels;
376	unsigned stride;
377	renderer->getPixels(renderer, &stride, &pixels);
378	videoCallback(pixels, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, BYTES_PER_PIXEL * stride);
379}
380
381static void _setRumble(struct GBARumble* rumble, int enable) {
382	UNUSED(rumble);
383	if (!rumbleCallback) {
384		return;
385	}
386	rumbleLevel += enable;
387	if (CircleBufferSize(&rumbleHistory) == RUMBLE_PWM) {
388		int8_t oldLevel;
389		CircleBufferRead8(&rumbleHistory, &oldLevel);
390		rumbleLevel -= oldLevel;
391	}
392	CircleBufferWrite8(&rumbleHistory, enable);
393	rumbleCallback(0, RETRO_RUMBLE_STRONG, rumbleLevel * 0xFFFF / RUMBLE_PWM);
394}