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}
285
286void GBARetroLog(struct GBAThread* thread, enum GBALogLevel level, const char* format, va_list args) {
287 UNUSED(thread);
288 if (!logCallback) {
289 return;
290 }
291
292 char message[128];
293 vsnprintf(message, sizeof(message), format, args);
294
295 enum retro_log_level retroLevel = RETRO_LOG_INFO;
296 switch (level) {
297 case GBA_LOG_ALL:
298 case GBA_LOG_ERROR:
299 case GBA_LOG_FATAL:
300 retroLevel = RETRO_LOG_ERROR;
301 break;
302 case GBA_LOG_WARN:
303 retroLevel = RETRO_LOG_WARN;
304 break;
305 case GBA_LOG_INFO:
306 case GBA_LOG_GAME_ERROR:
307 case GBA_LOG_SWI:
308 retroLevel = RETRO_LOG_INFO;
309 break;
310 case GBA_LOG_DEBUG:
311 case GBA_LOG_STUB:
312 retroLevel = RETRO_LOG_DEBUG;
313 break;
314 }
315 logCallback(retroLevel, "%s\n", message);
316}
317
318static void _postAudioFrame(struct GBAAVStream* stream, int16_t left, int16_t right) {
319 UNUSED(stream);
320 audioCallback(left, right);
321}
322
323static void _postVideoFrame(struct GBAAVStream* stream, struct GBAVideoRenderer* renderer) {
324 UNUSED(stream);
325 void* pixels;
326 unsigned stride;
327 renderer->getPixels(renderer, &stride, &pixels);
328 videoCallback(pixels, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, BYTES_PER_PIXEL * stride);
329}