Add RGB565 support
[pcsx_rearmed.git] / frontend / libretro.c
1 /*
2  * (C) notaz, 2012
3  *
4  * This work is licensed under the terms of the GNU GPLv2 or later.
5  * See the COPYING file in the top-level directory.
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "../libpcsxcore/misc.h"
13 #include "../libpcsxcore/psxcounters.h"
14 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
15 #include "../plugins/dfsound/out.h"
16 #include "main.h"
17 #include "plugin.h"
18 #include "plugin_lib.h"
19 #include "revision.h"
20 #include "libretro.h"
21
22 static retro_video_refresh_t video_cb;
23 static retro_input_poll_t input_poll_cb;
24 static retro_input_state_t input_state_cb;
25 static retro_environment_t environ_cb;
26 static retro_audio_sample_batch_t audio_batch_cb;
27
28 static void *vout_buf;
29 static int vout_width, vout_height;
30 static int samples_sent, samples_to_send;
31 static int plugins_opened;
32 static int native_rgb565;
33
34 /* PCSX ReARMed core calls and stuff */
35 int in_type1, in_type2;
36 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
37 int in_keystate;
38 int in_enable_vibration;
39
40 static int vout_open(void)
41 {
42         return 0;
43 }
44
45 static void *vout_set_mode(int w, int h, int bpp)
46 {
47         vout_width = w;
48         vout_height = h;
49         return vout_buf;
50 }
51
52 /* FIXME: either teach PCSX to blit to RGB1555 or RetroArch to support RGB565 */
53 static void convert(void *buf, size_t bytes)
54 {
55         unsigned int i, v, *p = buf;
56
57         for (i = 0; i < bytes / 4; i++) {
58                 v = p[i];
59                 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
60         }
61 }
62
63 static void *vout_flip(void)
64 {
65         pl_rearmed_cbs.flip_cnt++;
66         if (!native_rgb565)
67                 convert(vout_buf,  vout_width * vout_height * 2);
68         video_cb(vout_buf, vout_width, vout_height, vout_width * 2);
69
70         return vout_buf;
71 }
72
73 static void vout_close(void)
74 {
75 }
76
77 struct rearmed_cbs pl_rearmed_cbs = {
78         .pl_vout_open = vout_open,
79         .pl_vout_set_mode = vout_set_mode,
80         .pl_vout_flip = vout_flip,
81         .pl_vout_close = vout_close,
82         /* from psxcounters */
83         .gpu_hcnt = &hSyncCount,
84         .gpu_frame_count = &frame_counter,
85 };
86
87 void pl_frame_limit(void)
88 {
89         /* called once per frame, make psxCpu->Execute() above return */
90         stop = 1;
91 }
92
93 void pl_timing_prepare(int is_pal)
94 {
95 }
96
97 void plat_trigger_vibrate(int is_strong)
98 {
99 }
100
101 void pl_update_gun(int *xn, int *xres, int *y, int *in)
102 {
103 }
104
105 /* sound calls */
106 static int snd_init(void)
107 {
108         return 0;
109 }
110
111 static void snd_finish(void)
112 {
113 }
114
115 static int snd_busy(void)
116 {
117         if (samples_to_send > samples_sent)
118                 return 0; /* give more samples */
119         else
120                 return 1;
121 }
122
123 static void snd_feed(void *buf, int bytes)
124 {
125         audio_batch_cb(buf, bytes / 4);
126         samples_sent += bytes / 4;
127 }
128
129 void out_register_libretro(struct out_driver *drv)
130 {
131         drv->name = "libretro";
132         drv->init = snd_init;
133         drv->finish = snd_finish;
134         drv->busy = snd_busy;
135         drv->feed = snd_feed;
136 }
137
138 /* libretro */
139 void retro_set_environment(retro_environment_t cb) { environ_cb = cb; }
140 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
141 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
142 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
143 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
144 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
145
146 unsigned retro_api_version(void)
147 {
148         return RETRO_API_VERSION;
149 }
150
151 void retro_set_controller_port_device(unsigned port, unsigned device)
152 {
153 }
154
155 void retro_get_system_info(struct retro_system_info *info)
156 {
157         memset(info, 0, sizeof(*info));
158         info->library_name = "PCSX-ReARMed";
159         info->library_version = REV;
160         info->valid_extensions = "bin|cue|img|mdf|pbp|cbn";
161         info->need_fullpath = true;
162 }
163
164 void retro_get_system_av_info(struct retro_system_av_info *info)
165 {
166         memset(info, 0, sizeof(*info));
167         info->timing.fps            = 60;
168         info->timing.sample_rate    = 44100;
169         info->geometry.base_width   = 320;
170         info->geometry.base_height  = 240;
171         info->geometry.max_width    = 640;
172         info->geometry.max_height   = 512;
173         info->geometry.aspect_ratio = 4.0 / 3.0;
174 }
175
176 /* TODO */
177 size_t retro_serialize_size(void) 
178
179         return 0;
180 }
181
182 bool retro_serialize(void *data, size_t size)
183
184         return false;
185 }
186
187 bool retro_unserialize(const void *data, size_t size)
188 {
189         return false;
190 }
191
192 void retro_cheat_reset(void)
193 {
194 }
195
196 void retro_cheat_set(unsigned index, bool enabled, const char *code)
197 {
198 }
199
200 bool retro_load_game(const struct retro_game_info *info)
201 {
202         enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
203         if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
204                 native_rgb565 = 1;
205                 fprintf(stderr, "RGB565 supported, using it\n");
206         }
207
208         if (plugins_opened) {
209                 ClosePlugins();
210                 plugins_opened = 0;
211         }
212
213         set_cd_image(info->path);
214
215         /* have to reload after set_cd_image for correct cdr plugin */
216         if (LoadPlugins() == -1) {
217                 printf("faled to load plugins\n");
218                 return false;
219         }
220
221         plugins_opened = 1;
222         NetOpened = 0;
223
224         if (OpenPlugins() == -1) {
225                 printf("faled to open plugins\n");
226                 return false;
227         }
228
229         plugin_call_rearmed_cbs();
230
231         Config.PsxAuto = 1;
232         if (CheckCdrom() == -1) {
233                 printf("unsupported/invalid CD image: %s\n", info->path);
234                 return false;
235         }
236
237         SysReset();
238
239         if (LoadCdrom() == -1) {
240                 printf("could not load CD-ROM!\n");
241                 return false;
242         }
243         emu_on_new_cd();
244
245         return true;
246 }
247
248 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
249 {
250         return false;
251 }
252
253 void retro_unload_game(void) 
254 {
255 }
256
257 unsigned retro_get_region(void)
258 {
259         return RETRO_REGION_NTSC;
260 }
261
262 void *retro_get_memory_data(unsigned id)
263 {
264         return NULL;
265 }
266
267 size_t retro_get_memory_size(unsigned id)
268 {
269         return 0;
270 }
271
272 void retro_reset(void)
273 {
274         SysReset();
275 }
276
277 static const unsigned short retro_psx_map[] = {
278         [RETRO_DEVICE_ID_JOYPAD_B]      = 1 << DKEY_CROSS,
279         [RETRO_DEVICE_ID_JOYPAD_Y]      = 1 << DKEY_SQUARE,
280         [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
281         [RETRO_DEVICE_ID_JOYPAD_START]  = 1 << DKEY_START,
282         [RETRO_DEVICE_ID_JOYPAD_UP]     = 1 << DKEY_UP,
283         [RETRO_DEVICE_ID_JOYPAD_DOWN]   = 1 << DKEY_DOWN,
284         [RETRO_DEVICE_ID_JOYPAD_LEFT]   = 1 << DKEY_LEFT,
285         [RETRO_DEVICE_ID_JOYPAD_RIGHT]  = 1 << DKEY_RIGHT,
286         [RETRO_DEVICE_ID_JOYPAD_A]      = 1 << DKEY_CIRCLE,
287         [RETRO_DEVICE_ID_JOYPAD_X]      = 1 << DKEY_TRIANGLE,
288         [RETRO_DEVICE_ID_JOYPAD_L]      = 1 << DKEY_L1,
289         [RETRO_DEVICE_ID_JOYPAD_R]      = 1 << DKEY_R1,
290         [RETRO_DEVICE_ID_JOYPAD_L2]     = 1 << DKEY_L2,
291         [RETRO_DEVICE_ID_JOYPAD_R2]     = 1 << DKEY_R2,
292         [RETRO_DEVICE_ID_JOYPAD_L3]     = 1 << DKEY_L3,
293         [RETRO_DEVICE_ID_JOYPAD_R3]     = 1 << DKEY_R3,
294 };
295 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
296
297 void retro_run(void) 
298 {
299         int i;
300
301         input_poll_cb();
302         in_keystate = 0;
303         for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
304                 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
305                         in_keystate |= retro_psx_map[i];
306         in_keystate <<= 16;
307         for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
308                 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
309                         in_keystate |= retro_psx_map[i];
310
311         stop = 0;
312         psxCpu->Execute();
313
314         samples_to_send += 44100 / 60;
315 }
316
317 void retro_init(void)
318 {
319         const char *bios[] = { "scph1001", "scph5501", "scph7001" };
320         const char *dir;
321         char path[256];
322         FILE *f = NULL;
323         int i, ret, level;
324
325         ret = emu_core_preinit();
326         ret |= emu_core_init();
327         if (ret != 0) {
328                 printf("PCSX init failed, sorry\n");
329                 exit(1);
330         }
331
332         vout_buf = malloc(640 * 512 * 2);
333
334         if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
335         {
336                 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
337
338                 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
339                         snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
340                         f = fopen(path, "r");
341                         if (f != NULL) {
342                                 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
343                                 break;
344                         }
345                 }
346         }
347         if (f != NULL) {
348                 printf("found BIOS file: %s\n", Config.Bios);
349                 fclose(f);
350         }
351         else
352                 printf("no BIOS files found.\n");
353
354         level = 1;
355         environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
356 }
357
358 void retro_deinit(void)
359 {
360         SysClose();
361         free(vout_buf);
362         vout_buf = NULL;
363 }
364