all repos — mgba @ d957736ed95751328f6913b1954cdeac0bf7e80d

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}
176
177void retro_reset(void) {
178	ARMReset(&cpu);
179}
180
181bool retro_load_game(const struct retro_game_info* game) {
182	if (game->data) {
183		data = malloc(game->size);
184		memcpy(data, game->data, game->size);
185		rom = VFileFromMemory(data, game->size);
186	} else {
187		data = 0;
188		rom = VFileOpen(game->path, O_RDONLY);
189	}
190	if (!rom) {
191		return false;
192	}
193	if (!GBAIsROM(rom)) {
194		return false;
195	}
196
197	savedata = malloc(SIZE_CART_FLASH1M);
198	save = VFileFromMemory(savedata, SIZE_CART_FLASH1M);
199
200	GBALoadROM(&gba, rom, save, game->path);
201	GBAOverrideApplyDefaults(&gba);
202
203	ARMReset(&cpu);
204	return true;
205}
206
207void retro_unload_game(void) {
208	rom->close(rom);
209	rom = 0;
210	free(data);
211	data = 0;
212	save->close(save);
213	save = 0;
214	free(savedata);
215	savedata = 0;
216}
217
218size_t retro_serialize_size(void) {
219	return sizeof(struct GBASerializedState);
220}
221
222bool retro_serialize(void* data, size_t size) {
223	if (size != retro_serialize_size()) {
224		return false;
225	}
226	GBASerialize(&gba, data);
227	return true;
228}
229
230bool retro_unserialize(const void* data, size_t size) {
231	if (size != retro_serialize_size()) {
232		return false;
233	}
234	GBADeserialize(&gba, data);
235	return true;
236}
237
238void retro_cheat_reset(void) {
239	// TODO: Cheats
240}
241
242void retro_cheat_set(unsigned index, bool enabled, const char* code) {
243	// TODO: Cheats
244	UNUSED(index);
245	UNUSED(enabled);
246	UNUSED(code);
247}
248
249unsigned retro_get_region(void) {
250	return RETRO_REGION_NTSC; // TODO: This isn't strictly true
251}
252
253void retro_set_controller_port_device(unsigned port, unsigned device) {
254	UNUSED(port);
255	UNUSED(device);
256}
257
258bool retro_load_game_special(unsigned game_type, const struct retro_game_info* info, size_t num_info) {
259	UNUSED(game_type);
260	UNUSED(info);
261	UNUSED(num_info);
262	return false;
263}
264
265void* retro_get_memory_data(unsigned id) {
266	if (id != RETRO_MEMORY_SAVE_RAM) {
267		return 0;
268	}
269	return savedata;
270}
271
272size_t retro_get_memory_size(unsigned id) {
273	if (id != RETRO_MEMORY_SAVE_RAM) {
274		return 0;
275	}
276	switch (gba.memory.savedata.type) {
277	case SAVEDATA_AUTODETECT:
278	case SAVEDATA_FLASH1M:
279		return SIZE_CART_FLASH1M;
280	case SAVEDATA_FLASH512:
281		return SIZE_CART_FLASH512;
282	case SAVEDATA_EEPROM:
283		return SIZE_CART_EEPROM;
284	case SAVEDATA_SRAM:
285		return SIZE_CART_SRAM;
286	case SAVEDATA_FORCE_NONE:
287		return 0;
288	}
289	return 0;
290}
291
292void GBARetroLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
293	UNUSED(thread);
294	if (!logCallback) {
295		return;
296	}
297
298	char message[128];
299	vsnprintf(message, sizeof(message), format, args);
300
301	enum retro_log_level retroLevel = RETRO_LOG_INFO;
302	switch (level) {
303	case GBA_LOG_ALL:
304	case GBA_LOG_ERROR:
305	case GBA_LOG_FATAL:
306		retroLevel = RETRO_LOG_ERROR;
307		break;
308	case GBA_LOG_WARN:
309		retroLevel = RETRO_LOG_WARN;
310		break;
311	case GBA_LOG_INFO:
312	case GBA_LOG_GAME_ERROR:
313	case GBA_LOG_SWI:
314		retroLevel = RETRO_LOG_INFO;
315		break;
316	case GBA_LOG_DEBUG:
317	case GBA_LOG_STUB:
318		retroLevel = RETRO_LOG_DEBUG;
319		break;
320	}
321	logCallback(retroLevel, "%s\n", message);
322}
323
324static void _postAudioBuffer(struct GBAAVStream* stream, struct GBAAudio* audio) {
325	UNUSED(stream);
326	int16_t samples[SAMPLES * 2];
327#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
328	blip_read_samples(audio->left, samples, SAMPLES, true);
329	blip_read_samples(audio->right, samples + 1, SAMPLES, true);
330#else
331	int16_t samplesR[SAMPLES];
332	GBAAudioCopy(audio, &samples[SAMPLES], samplesR, SAMPLES);
333	size_t i;
334	for (i = 0; i < SAMPLES; ++i) {
335		samples[i * 2] = samples[SAMPLES + i];
336		samples[i * 2 + 1] = samplesR[i];
337	}
338#endif
339	audioCallback(samples, SAMPLES);
340}
341
342static void _postVideoFrame(struct GBAAVStream* stream, struct GBAVideoRenderer* renderer) {
343	UNUSED(stream);
344	void* pixels;
345	unsigned stride;
346	renderer->getPixels(renderer, &stride, &pixels);
347	videoCallback(pixels, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, BYTES_PER_PIXEL * stride);
348}