all repos — mgba @ 47d945bf75c4a43307698393a57b015d7373e716

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