all repos — mgba @ 207219b25e8a770e2cb58d72ceefee4f9b388245

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