all repos — mgba @ b6e274c3d46784309ceee0306d9973f31902228f

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