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