all repos — mgba @ 6c5a1315ea314587e3387538ccbe2eb42157fb70

mGBA Game Boy Advance Emulator

src/platform/libretro/libretro.c (view raw)

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