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 rom->close(rom);
186 rom = 0;
187 save->close(save);
188 save = 0;
189 free(savedata);
190 savedata = 0;
191}
192
193size_t retro_serialize_size(void) {
194 return sizeof(struct GBASerializedState);
195}
196
197bool retro_serialize(void* data, size_t size) {
198 if (size != retro_serialize_size()) {
199 return false;
200 }
201 GBASerialize(&gba, data);
202 return true;
203}
204
205bool retro_unserialize(const void* data, size_t size) {
206 if (size != retro_serialize_size()) {
207 return false;
208 }
209 GBADeserialize(&gba, data);
210 return true;
211}
212
213void retro_cheat_reset(void) {
214 // TODO: Cheats
215}
216
217void retro_cheat_set(unsigned index, bool enabled, const char* code) {
218 // TODO: Cheats
219 UNUSED(index);
220 UNUSED(enabled);
221 UNUSED(code);
222}
223
224unsigned retro_get_region(void) {
225 return RETRO_REGION_NTSC; // TODO: This isn't strictly true
226}
227
228void retro_set_controller_port_device(unsigned port, unsigned device) {
229 UNUSED(port);
230 UNUSED(device);
231}
232
233bool retro_load_game_special(unsigned game_type, const struct retro_game_info* info, size_t num_info) {
234 UNUSED(game_type);
235 UNUSED(info);
236 UNUSED(num_info);
237 return false;
238}
239
240void* retro_get_memory_data(unsigned id) {
241 if (id != RETRO_MEMORY_SAVE_RAM) {
242 return 0;
243 }
244 return savedata;
245}
246
247size_t retro_get_memory_size(unsigned id) {
248 if (id != RETRO_MEMORY_SAVE_RAM) {
249 return 0;
250 }
251 switch (gba.memory.savedata.type) {
252 case SAVEDATA_AUTODETECT:
253 case SAVEDATA_FLASH1M:
254 return SIZE_CART_FLASH1M;
255 case SAVEDATA_FLASH512:
256 return SIZE_CART_FLASH512;
257 case SAVEDATA_EEPROM:
258 return SIZE_CART_EEPROM;
259 case SAVEDATA_SRAM:
260 return SIZE_CART_SRAM;
261 case SAVEDATA_FORCE_NONE:
262 return 0;
263 }
264}
265
266void GBARetroLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
267 UNUSED(thread);
268 if (!logCallback) {
269 return;
270 }
271
272 char message[128];
273 vsnprintf(message, sizeof(message), format, args);
274
275 enum retro_log_level retroLevel = RETRO_LOG_INFO;
276 switch (level) {
277 case GBA_LOG_ALL:
278 case GBA_LOG_ERROR:
279 case GBA_LOG_FATAL:
280 retroLevel = RETRO_LOG_ERROR;
281 break;
282 case GBA_LOG_WARN:
283 retroLevel = RETRO_LOG_WARN;
284 break;
285 case GBA_LOG_INFO:
286 case GBA_LOG_GAME_ERROR:
287 case GBA_LOG_SWI:
288 retroLevel = RETRO_LOG_INFO;
289 break;
290 case GBA_LOG_DEBUG:
291 case GBA_LOG_STUB:
292 retroLevel = RETRO_LOG_DEBUG;
293 break;
294 }
295 logCallback(retroLevel, "%s\n", message);
296}
297
298static void _postAudioFrame(struct GBAAVStream* stream, int16_t left, int16_t right) {
299 UNUSED(stream);
300 audioCallback(left, right);
301}
302
303static void _postVideoFrame(struct GBAAVStream* stream, struct GBAVideoRenderer* renderer) {
304 UNUSED(stream);
305 void* pixels;
306 unsigned stride;
307 renderer->getPixels(renderer, &stride, &pixels);
308 videoCallback(pixels, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, BYTES_PER_PIXEL * stride);
309}