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 fmt = RETRO_PIXEL_FORMAT_0RGB1555;
81#endif
82#else
83 fmt = RETRO_PIXEL_FORMAT_XRGB8888;
84#endif
85 environCallback(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt);
86
87 struct retro_input_descriptor inputDescriptors[] = {
88 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
89 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
90 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
91 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
92 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
93 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
94 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
95 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
96 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R" },
97 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L" }
98 };
99 environCallback(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, &inputDescriptors);
100
101 // TODO: RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME when BIOS booting is supported
102 // TODO: RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE
103
104 struct retro_log_callback log;
105 if (environCallback(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log)) {
106 logCallback = log.log;
107 } else {
108 logCallback = 0;
109 }
110
111 stream.postAudioFrame = _postAudioFrame;
112 stream.postVideoFrame = _postVideoFrame;
113
114 GBACreate(&gba);
115 ARMSetComponents(&cpu, &gba.d, 0, 0);
116 ARMInit(&cpu);
117 gba.logLevel = 0; // TODO: Settings
118 gba.logHandler = GBARetroLog;
119 gba.stream = &stream;
120 gba.idleOptimization = IDLE_LOOP_REMOVE; // TODO: Settings
121 rom = 0;
122
123 GBAVideoSoftwareRendererCreate(&renderer);
124 renderer.outputBuffer = malloc(256 * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL);
125 renderer.outputBufferStride = 256;
126 GBAVideoAssociateRenderer(&gba.video, &renderer.d);
127}
128
129void retro_deinit(void) {
130 GBADestroy(&gba);
131}
132
133void retro_run(void) {
134 int keys;
135 gba.keySource = &keys;
136 inputPollCallback();
137
138 keys = 0;
139 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A)) << 0;
140 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B)) << 1;
141 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT)) << 2;
142 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START)) << 3;
143 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT)) << 4;
144 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT)) << 5;
145 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP)) << 6;
146 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN)) << 7;
147 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R)) << 8;
148 keys |= (!!inputCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L)) << 9;
149
150 int frameCount = gba.video.frameCounter;
151 while (gba.video.frameCounter == frameCount) {
152 ARMRunLoop(&cpu);
153 }
154 videoCallback(renderer.outputBuffer, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, BYTES_PER_PIXEL * 256);
155}
156
157void retro_reset(void) {
158 ARMReset(&cpu);
159}
160
161bool retro_load_game(const struct retro_game_info* game) {
162 if (game->data) {
163 rom = VFileFromMemory((void*) game->data, game->size); // TODO: readonly VFileMem
164 } else {
165 rom = VFileOpen(game->path, O_RDONLY);
166 }
167 if (!rom) {
168 return false;
169 }
170 if (!GBAIsROM(rom)) {
171 return false;
172 }
173
174 savedata = malloc(SIZE_CART_FLASH1M);
175 save = VFileFromMemory(savedata, SIZE_CART_FLASH1M);
176
177 GBALoadROM(&gba, rom, save, game->path);
178 ARMReset(&cpu);
179 return true;
180}
181
182void retro_unload_game(void) {
183 // TODO
184}
185
186size_t retro_serialize_size(void) {
187 return sizeof(struct GBASerializedState);
188}
189
190bool retro_serialize(void* data, size_t size) {
191 if (size != retro_serialize_size()) {
192 return false;
193 }
194 GBASerialize(&gba, data);
195 return true;
196}
197
198bool retro_unserialize(const void* data, size_t size) {
199 if (size != retro_serialize_size()) {
200 return false;
201 }
202 GBADeserialize(&gba, data);
203 return true;
204}
205
206void retro_cheat_reset(void) {
207 // TODO: Cheats
208}
209
210void retro_cheat_set(unsigned index, bool enabled, const char* code) {
211 // TODO: Cheats
212 UNUSED(index);
213 UNUSED(enabled);
214 UNUSED(code);
215}
216
217unsigned retro_get_region(void) {
218 return RETRO_REGION_NTSC; // TODO: This isn't strictly true
219}
220
221void retro_set_controller_port_device(unsigned port, unsigned device) {
222 UNUSED(port);
223 UNUSED(device);
224}
225
226bool retro_load_game_special(unsigned game_type, const struct retro_game_info* info, size_t num_info) {
227 UNUSED(game_type);
228 UNUSED(info);
229 UNUSED(num_info);
230 return false;
231}
232
233void* retro_get_memory_data(unsigned id) {
234 if (id != RETRO_MEMORY_SAVE_RAM) {
235 return 0;
236 }
237 return savedata;
238}
239
240size_t retro_get_memory_size(unsigned id) {
241 if (id != RETRO_MEMORY_SAVE_RAM) {
242 return 0;
243 }
244 switch (gba.memory.savedata.type) {
245 case SAVEDATA_AUTODETECT:
246 case SAVEDATA_FLASH1M:
247 return SIZE_CART_FLASH1M;
248 case SAVEDATA_FLASH512:
249 return SIZE_CART_FLASH512;
250 case SAVEDATA_EEPROM:
251 return SIZE_CART_EEPROM;
252 case SAVEDATA_SRAM:
253 return SIZE_CART_SRAM;
254 case SAVEDATA_FORCE_NONE:
255 return 0;
256 }
257}
258
259void GBARetroLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
260 UNUSED(thread);
261 if (!logCallback) {
262 return;
263 }
264
265 char message[128];
266 vsnprintf(message, sizeof(message), format, args);
267
268 enum retro_log_level retroLevel = RETRO_LOG_INFO;
269 switch (level) {
270 case GBA_LOG_ALL:
271 case GBA_LOG_ERROR:
272 case GBA_LOG_FATAL:
273 retroLevel = RETRO_LOG_ERROR;
274 break;
275 case GBA_LOG_WARN:
276 retroLevel = RETRO_LOG_WARN;
277 break;
278 case GBA_LOG_INFO:
279 case GBA_LOG_GAME_ERROR:
280 case GBA_LOG_SWI:
281 retroLevel = RETRO_LOG_INFO;
282 break;
283 case GBA_LOG_DEBUG:
284 case GBA_LOG_STUB:
285 retroLevel = RETRO_LOG_DEBUG;
286 break;
287 }
288 logCallback(retroLevel, "%s\n", message);
289}
290
291static void _postAudioFrame(struct GBAAVStream* stream, int16_t left, int16_t right) {
292 UNUSED(stream);
293 audioCallback(left, right);
294}
295
296static void _postVideoFrame(struct GBAAVStream* stream, struct GBAVideoRenderer* renderer) {
297 UNUSED(stream);
298 void* pixels;
299 unsigned stride;
300 renderer->getPixels(renderer, &stride, &pixels);
301 videoCallback(pixels, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, BYTES_PER_PIXEL * stride);
302}