(Libretro) Add NEON interlace enable core option and region core option
[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/psxmem_map.h"
15 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
16 #include "../libpcsxcore/cdrom.h"
17 #include "../libpcsxcore/cdriso.h"
18 #include "../libpcsxcore/cheat.h"
19 #include "../plugins/dfsound/out.h"
20 #include "cspace.h"
21 #include "main.h"
22 #include "plugin.h"
23 #include "plugin_lib.h"
24 #include "revision.h"
25 #include "libretro.h"
26
27 static retro_video_refresh_t video_cb;
28 static retro_input_poll_t input_poll_cb;
29 static retro_input_state_t input_state_cb;
30 static retro_environment_t environ_cb;
31 static retro_audio_sample_batch_t audio_batch_cb;
32
33 static void *vout_buf;
34 static int vout_width, vout_height;
35 static int vout_doffs_old, vout_fb_dirty;
36 static bool vout_can_dupe;
37
38 static int samples_sent, samples_to_send;
39 static int plugins_opened;
40 static int is_pal_mode;
41
42 /* memory card data */
43 extern char Mcd1Data[MCD_SIZE];
44 extern char McdDisable[2];
45
46 /* PCSX ReARMed core calls and stuff */
47 int in_type1, in_type2;
48 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
49 int in_keystate;
50 int in_enable_vibration;
51
52 static void init_memcard(char *mcd_data)
53 {
54         unsigned off = 0;
55         unsigned i;
56
57         memset(mcd_data, 0, MCD_SIZE);
58
59         mcd_data[off++] = 'M';
60         mcd_data[off++] = 'C';
61         off += 0x7d;
62         mcd_data[off++] = 0x0e;
63
64         for (i = 0; i < 15; i++) {
65                 mcd_data[off++] = 0xa0;
66                 off += 0x07;
67                 mcd_data[off++] = 0xff;
68                 mcd_data[off++] = 0xff;
69                 off += 0x75;
70                 mcd_data[off++] = 0xa0;
71         }
72
73         for (i = 0; i < 20; i++) {
74                 mcd_data[off++] = 0xff;
75                 mcd_data[off++] = 0xff;
76                 mcd_data[off++] = 0xff;
77                 mcd_data[off++] = 0xff;
78                 off += 0x04;
79                 mcd_data[off++] = 0xff;
80                 mcd_data[off++] = 0xff;
81                 off += 0x76;
82         }
83 }
84
85 static int vout_open(void)
86 {
87         return 0;
88 }
89
90 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
91 {
92         vout_width = w;
93         vout_height = h;
94 }
95
96 #ifndef FRONTEND_SUPPORTS_RGB565
97 static void convert(void *buf, size_t bytes)
98 {
99         unsigned int i, v, *p = buf;
100
101         for (i = 0; i < bytes / 4; i++) {
102                 v = p[i];
103                 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
104         }
105 }
106 #endif
107
108 static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
109 {
110         unsigned short *dest = vout_buf;
111         const unsigned short *src = vram;
112         int dstride = vout_width, h1 = h;
113         int doffs;
114
115         if (vram == NULL) {
116                 // blanking
117                 memset(vout_buf, 0, dstride * h * 2);
118                 goto out;
119         }
120
121         doffs = (vout_height - h) * dstride;
122         doffs += (dstride - w) / 2 & ~1;
123         if (doffs != vout_doffs_old) {
124                 // clear borders
125                 memset(vout_buf, 0, dstride * h * 2);
126                 vout_doffs_old = doffs;
127         }
128         dest += doffs;
129
130         if (bgr24)
131         {
132                 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
133                 for (; h1-- > 0; dest += dstride, src += stride)
134                 {
135                         bgr888_to_rgb565(dest, src, w * 3);
136                 }
137         }
138         else
139         {
140                 for (; h1-- > 0; dest += dstride, src += stride)
141                 {
142                         bgr555_to_rgb565(dest, src, w * 2);
143                 }
144         }
145
146 out:
147 #ifndef FRONTEND_SUPPORTS_RGB565
148         convert(vout_buf, vout_width * vout_height * 2);
149 #endif
150         vout_fb_dirty = 1;
151         pl_rearmed_cbs.flip_cnt++;
152 }
153
154 static void vout_close(void)
155 {
156 }
157
158 static void *pl_mmap(unsigned int size)
159 {
160         return psxMap(0, size, 0, MAP_TAG_VRAM);
161 }
162
163 static void pl_munmap(void *ptr, unsigned int size)
164 {
165         psxUnmap(ptr, size, MAP_TAG_VRAM);
166 }
167
168 struct rearmed_cbs pl_rearmed_cbs = {
169         .pl_vout_open = vout_open,
170         .pl_vout_set_mode = vout_set_mode,
171         .pl_vout_flip = vout_flip,
172         .pl_vout_close = vout_close,
173         .mmap = pl_mmap,
174         .munmap = pl_munmap,
175         /* from psxcounters */
176         .gpu_hcnt = &hSyncCount,
177         .gpu_frame_count = &frame_counter,
178 };
179
180 void pl_frame_limit(void)
181 {
182         /* called once per frame, make psxCpu->Execute() above return */
183         stop = 1;
184 }
185
186 void pl_timing_prepare(int is_pal)
187 {
188         is_pal_mode = is_pal;
189 }
190
191 void plat_trigger_vibrate(int is_strong)
192 {
193 }
194
195 void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
196 {
197 }
198
199 /* sound calls */
200 static int snd_init(void)
201 {
202         return 0;
203 }
204
205 static void snd_finish(void)
206 {
207 }
208
209 static int snd_busy(void)
210 {
211         if (samples_to_send > samples_sent)
212                 return 0; /* give more samples */
213         else
214                 return 1;
215 }
216
217 static void snd_feed(void *buf, int bytes)
218 {
219         audio_batch_cb(buf, bytes / 4);
220         samples_sent += bytes / 4;
221 }
222
223 void out_register_libretro(struct out_driver *drv)
224 {
225         drv->name = "libretro";
226         drv->init = snd_init;
227         drv->finish = snd_finish;
228         drv->busy = snd_busy;
229         drv->feed = snd_feed;
230 }
231
232 /* libretro */
233 void retro_set_environment(retro_environment_t cb)
234 {
235    static const struct retro_variable vars[] = {
236       { "frameskip", "Frameskip; 0|1|2|3" },
237       { "region", "Region; Auto|NTSC|PAL" },
238 #ifdef __ARM_NEON__
239       { "neon_interlace_enable", "Enable interlacing mode(s); disabled|enabled" },
240       { "neon_enhancement_enable", "Enhanced resolution (slow); disabled|enabled" },
241 #endif
242       { NULL, NULL },
243    };
244
245    environ_cb = cb;
246
247    cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)vars);
248 }
249
250 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
251 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
252 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
253 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
254 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
255
256 unsigned retro_api_version(void)
257 {
258         return RETRO_API_VERSION;
259 }
260
261 void retro_set_controller_port_device(unsigned port, unsigned device)
262 {
263 }
264
265 void retro_get_system_info(struct retro_system_info *info)
266 {
267         memset(info, 0, sizeof(*info));
268         info->library_name = "PCSX-ReARMed";
269         info->library_version = "r19";
270         info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u";
271         info->need_fullpath = true;
272 }
273
274 void retro_get_system_av_info(struct retro_system_av_info *info)
275 {
276         memset(info, 0, sizeof(*info));
277         info->timing.fps            = is_pal_mode ? 50 : 60;
278         info->timing.sample_rate    = 44100;
279         info->geometry.base_width   = 320;
280         info->geometry.base_height  = 240;
281         info->geometry.max_width    = 640;
282         info->geometry.max_height   = 512;
283         info->geometry.aspect_ratio = 4.0 / 3.0;
284 }
285
286 /* savestates */
287 size_t retro_serialize_size(void) 
288
289         // it's currently 4380651 bytes, but have some reserved for future
290         return 0x430000;
291 }
292
293 struct save_fp {
294         char *buf;
295         size_t pos;
296         int is_write;
297 };
298
299 static void *save_open(const char *name, const char *mode)
300 {
301         struct save_fp *fp;
302
303         if (name == NULL || mode == NULL)
304                 return NULL;
305
306         fp = malloc(sizeof(*fp));
307         if (fp == NULL)
308                 return NULL;
309
310         fp->buf = (char *)name;
311         fp->pos = 0;
312         fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
313
314         return fp;
315 }
316
317 static int save_read(void *file, void *buf, u32 len)
318 {
319         struct save_fp *fp = file;
320         if (fp == NULL || buf == NULL)
321                 return -1;
322
323         memcpy(buf, fp->buf + fp->pos, len);
324         fp->pos += len;
325         return len;
326 }
327
328 static int save_write(void *file, const void *buf, u32 len)
329 {
330         struct save_fp *fp = file;
331         if (fp == NULL || buf == NULL)
332                 return -1;
333
334         memcpy(fp->buf + fp->pos, buf, len);
335         fp->pos += len;
336         return len;
337 }
338
339 static long save_seek(void *file, long offs, int whence)
340 {
341         struct save_fp *fp = file;
342         if (fp == NULL)
343                 return -1;
344
345         switch (whence) {
346         case SEEK_CUR:
347                 fp->pos += offs;
348                 return fp->pos;
349         case SEEK_SET:
350                 fp->pos = offs;
351                 return fp->pos;
352         default:
353                 return -1;
354         }
355 }
356
357 static void save_close(void *file)
358 {
359         struct save_fp *fp = file;
360         size_t r_size = retro_serialize_size();
361         if (fp == NULL)
362                 return;
363
364         if (fp->pos > r_size)
365                 SysPrintf("ERROR: save buffer overflow detected\n");
366         else if (fp->is_write && fp->pos < r_size)
367                 // make sure we don't save trash in leftover space
368                 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
369         free(fp);
370 }
371
372 bool retro_serialize(void *data, size_t size)
373
374         int ret = SaveState(data);
375         return ret == 0 ? true : false;
376 }
377
378 bool retro_unserialize(const void *data, size_t size)
379 {
380         int ret = LoadState(data);
381         return ret == 0 ? true : false;
382 }
383
384 /* cheats */
385 void retro_cheat_reset(void)
386 {
387         ClearAllCheats();
388 }
389
390 void retro_cheat_set(unsigned index, bool enabled, const char *code)
391 {
392         char buf[256];
393         int ret;
394
395         // cheat funcs are destructive, need a copy..
396         strncpy(buf, code, sizeof(buf));
397         buf[sizeof(buf) - 1] = 0;
398
399         if (index < NumCheats)
400                 ret = EditCheat(index, "", buf);
401         else
402                 ret = AddCheat("", buf);
403
404         if (ret != 0)
405                 SysPrintf("Failed to set cheat %#u\n", index);
406         else if (index < NumCheats)
407                 Cheats[index].Enabled = enabled;
408 }
409
410 /* multidisk support */
411 static bool disk_ejected;
412 static unsigned int disk_current_index;
413 static unsigned int disk_count;
414 static struct disks_state {
415         char *fname;
416         int internal_index; // for multidisk eboots
417 } disks[8];
418
419 static bool disk_set_eject_state(bool ejected)
420 {
421         // weird PCSX API..
422         SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
423         LidInterrupt();
424
425         disk_ejected = ejected;
426         return true;
427 }
428
429 static bool disk_get_eject_state(void)
430 {
431         /* can't be controlled by emulated software */
432         return disk_ejected;
433 }
434
435 static unsigned int disk_get_image_index(void)
436 {
437         return disk_current_index;
438 }
439
440 static bool disk_set_image_index(unsigned int index)
441 {
442         if (index >= sizeof(disks) / sizeof(disks[0]))
443                 return false;
444
445         CdromId[0] = '\0';
446         CdromLabel[0] = '\0';
447
448         if (disks[index].fname == NULL) {
449                 SysPrintf("missing disk #%u\n", index);
450                 CDR_shutdown();
451
452                 // RetroArch specifies "no disk" with index == count,
453                 // so don't fail here..
454                 disk_current_index = index;
455                 return true;
456         }
457
458         SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
459                 disks[index].fname, disks[index].internal_index);
460
461         cdrIsoMultidiskSelect = disks[index].internal_index;
462         set_cd_image(disks[index].fname);
463         if (ReloadCdromPlugin() < 0) {
464                 SysPrintf("failed to load cdr plugin\n");
465                 return false;
466         }
467         if (CDR_open() < 0) {
468                 SysPrintf("failed to open cdr plugin\n");
469                 return false;
470         }
471
472         if (!disk_ejected) {
473                 SetCdOpenCaseTime(time(NULL) + 2);
474                 LidInterrupt();
475         }
476
477         disk_current_index = index;
478         return true;
479 }
480
481 static unsigned int disk_get_num_images(void)
482 {
483         return disk_count;
484 }
485
486 static bool disk_replace_image_index(unsigned index,
487         const struct retro_game_info *info)
488 {
489         char *old_fname;
490         bool ret = true;
491
492         if (index >= sizeof(disks) / sizeof(disks[0]))
493                 return false;
494
495         old_fname = disks[index].fname;
496         disks[index].fname = NULL;
497         disks[index].internal_index = 0;
498
499         if (info != NULL) {
500                 disks[index].fname = strdup(info->path);
501                 if (index == disk_current_index)
502                         ret = disk_set_image_index(index);
503         }
504
505         if (old_fname != NULL)
506                 free(old_fname);
507
508         return ret;
509 }
510
511 static bool disk_add_image_index(void)
512 {
513         if (disk_count >= 8)
514                 return false;
515
516         disk_count++;
517         return true;
518 }
519
520 static struct retro_disk_control_callback disk_control = {
521         .set_eject_state = disk_set_eject_state,
522         .get_eject_state = disk_get_eject_state,
523         .get_image_index = disk_get_image_index,
524         .set_image_index = disk_set_image_index,
525         .get_num_images = disk_get_num_images,
526         .replace_image_index = disk_replace_image_index,
527         .add_image_index = disk_add_image_index,
528 };
529
530 // just in case, maybe a win-rt port in the future?
531 #ifdef _WIN32
532 #define SLASH '\\'
533 #else
534 #define SLASH '/'
535 #endif
536
537 static char base_dir[PATH_MAX];
538
539 static bool read_m3u(const char *file)
540 {
541         char line[PATH_MAX];
542         char name[PATH_MAX];
543         FILE *f = fopen(file, "r");
544         if (!f)
545                 return false;
546
547         while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) {
548                 if (line[0] == '#')
549                         continue;
550                 char *carrige_return = strchr(line, '\r');
551                 if (carrige_return)
552                         *carrige_return = '\0';
553                 char *newline = strchr(line, '\n');
554                 if (newline)
555                         *newline = '\0';
556
557                 if (line[0] != '\0')
558                 {
559                         snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
560                         disks[disk_count++].fname = strdup(name);
561                 }
562         }
563
564         fclose(f);
565         return (disk_count != 0);
566 }
567
568 static void extract_directory(char *buf, const char *path, size_t size)
569 {
570    char *base;
571    strncpy(buf, path, size - 1);
572    buf[size - 1] = '\0';
573
574    base = strrchr(buf, '/');
575    if (!base)
576       base = strrchr(buf, '\\');
577
578    if (base)
579       *base = '\0';
580    else
581    {
582       buf[0] = '.';
583       buf[1] = '\0';
584    }
585 }
586
587 bool retro_load_game(const struct retro_game_info *info)
588 {
589         size_t i;
590         bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
591
592 #ifdef FRONTEND_SUPPORTS_RGB565
593         enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
594         if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
595                 SysPrintf("RGB565 supported, using it\n");
596         }
597 #endif
598
599         if (info == NULL || info->path == NULL) {
600                 SysPrintf("info->path required\n");
601                 return false;
602         }
603
604         if (plugins_opened) {
605                 ClosePlugins();
606                 plugins_opened = 0;
607         }
608
609         for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
610                 if (disks[i].fname != NULL) {
611                         free(disks[i].fname);
612                         disks[i].fname = NULL;
613                 }
614                 disks[i].internal_index = 0;
615         }
616
617         disk_current_index = 0;
618         extract_directory(base_dir, info->path, sizeof(base_dir));
619
620         if (is_m3u) {
621                 if (!read_m3u(info->path)) {
622                         SysPrintf("failed to read m3u file\n");
623                         return false;
624                 }
625         } else {
626                 disk_count = 1;
627                 disks[0].fname = strdup(info->path);
628         }
629
630         set_cd_image(disks[0].fname);
631
632         /* have to reload after set_cd_image for correct cdr plugin */
633         if (LoadPlugins() == -1) {
634                 SysPrintf("failed to load plugins\n");
635                 return false;
636         }
637
638         plugins_opened = 1;
639         NetOpened = 0;
640
641         if (OpenPlugins() == -1) {
642                 SysPrintf("failed to open plugins\n");
643                 return false;
644         }
645
646         plugin_call_rearmed_cbs();
647
648         Config.PsxAuto = 1;
649         if (CheckCdrom() == -1) {
650                 SysPrintf("unsupported/invalid CD image: %s\n", info->path);
651                 return false;
652         }
653
654         SysReset();
655
656         if (LoadCdrom() == -1) {
657                 SysPrintf("could not load CD-ROM!\n");
658                 return false;
659         }
660         emu_on_new_cd(0);
661
662         // multidisk images
663         if (!is_m3u) {
664                 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
665                 for (i = 1; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++) {
666                         disks[i].fname = strdup(info->path);
667                         disks[i].internal_index = i;
668                 }
669         }
670
671         return true;
672 }
673
674 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
675 {
676         return false;
677 }
678
679 void retro_unload_game(void) 
680 {
681 }
682
683 unsigned retro_get_region(void)
684 {
685         return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
686 }
687
688 void *retro_get_memory_data(unsigned id)
689 {
690         if (id == RETRO_MEMORY_SAVE_RAM)
691                 return Mcd1Data;
692         else
693                 return NULL;
694 }
695
696 size_t retro_get_memory_size(unsigned id)
697 {
698         if (id == RETRO_MEMORY_SAVE_RAM)
699                 return MCD_SIZE;
700         else
701                 return 0;
702 }
703
704 void retro_reset(void)
705 {
706         SysReset();
707 }
708
709 static const unsigned short retro_psx_map[] = {
710         [RETRO_DEVICE_ID_JOYPAD_B]      = 1 << DKEY_CROSS,
711         [RETRO_DEVICE_ID_JOYPAD_Y]      = 1 << DKEY_SQUARE,
712         [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
713         [RETRO_DEVICE_ID_JOYPAD_START]  = 1 << DKEY_START,
714         [RETRO_DEVICE_ID_JOYPAD_UP]     = 1 << DKEY_UP,
715         [RETRO_DEVICE_ID_JOYPAD_DOWN]   = 1 << DKEY_DOWN,
716         [RETRO_DEVICE_ID_JOYPAD_LEFT]   = 1 << DKEY_LEFT,
717         [RETRO_DEVICE_ID_JOYPAD_RIGHT]  = 1 << DKEY_RIGHT,
718         [RETRO_DEVICE_ID_JOYPAD_A]      = 1 << DKEY_CIRCLE,
719         [RETRO_DEVICE_ID_JOYPAD_X]      = 1 << DKEY_TRIANGLE,
720         [RETRO_DEVICE_ID_JOYPAD_L]      = 1 << DKEY_L1,
721         [RETRO_DEVICE_ID_JOYPAD_R]      = 1 << DKEY_R1,
722         [RETRO_DEVICE_ID_JOYPAD_L2]     = 1 << DKEY_L2,
723         [RETRO_DEVICE_ID_JOYPAD_R2]     = 1 << DKEY_R2,
724         [RETRO_DEVICE_ID_JOYPAD_L3]     = 1 << DKEY_L3,
725         [RETRO_DEVICE_ID_JOYPAD_R3]     = 1 << DKEY_R3,
726 };
727 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
728
729 static void update_variables(void)
730 {
731    struct retro_variable var;
732    
733    var.value = NULL;
734    var.key = "frameskip";
735
736    if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
737       pl_rearmed_cbs.frameskip = atoi(var.value);
738
739    var.value = NULL;
740    var.key = "region";
741
742    if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
743    {
744       Config.PsxAuto = 0;
745       if (strcmp(var.value, "Automatic") == 0)
746          Config.PsxAuto = 1;
747       else if (strcmp(var.value, "NTSC") == 0)
748          Config.PsxType = 0;
749       else if (strcmp(var.value, "PAL") == 0)
750          Config.PsxType = 1;
751    }
752 #ifdef __ARM_NEON__
753    var.value = "NULL";
754    var.key = "neon_interlace_enable";
755
756    if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
757    {
758       if (strcmp(var.value, "disabled") == 0)
759          pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
760       else if (strcmp(var.value, "enabled") == 0)
761          pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
762    }
763
764
765    var.value = NULL;
766    var.key = "neon_enhancement_enable";
767
768    if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
769    {
770       if (strcmp(var.value, "disabled") == 0)
771          pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
772       else if (strcmp(var.value, "enabled") == 0)
773          pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
774    }
775 #endif
776 }
777
778 void retro_run(void) 
779 {
780         int i;
781
782         input_poll_cb();
783
784    bool updated = false;
785    if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
786       update_variables();
787
788         in_keystate = 0;
789         for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
790                 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
791                         in_keystate |= retro_psx_map[i];
792         in_keystate <<= 16;
793         for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
794                 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
795                         in_keystate |= retro_psx_map[i];
796
797         stop = 0;
798         psxCpu->Execute();
799
800         samples_to_send += is_pal_mode ? 44100 / 50 : 44100 / 60;
801
802         video_cb((vout_fb_dirty || !vout_can_dupe) ? vout_buf : NULL,
803                 vout_width, vout_height, vout_width * 2);
804         vout_fb_dirty = 0;
805 }
806
807 void retro_init(void)
808 {
809         const char *bios[] = { "scph1001", "scph5501", "scph7001" };
810         const char *dir;
811         char path[256];
812         FILE *f = NULL;
813         int i, ret, level;
814
815         ret = emu_core_preinit();
816         ret |= emu_core_init();
817         if (ret != 0) {
818                 SysPrintf("PCSX init failed.\n");
819                 exit(1);
820         }
821
822         vout_buf = malloc(640 * 512 * 2);
823
824         if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
825         {
826                 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
827
828                 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
829                         snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
830                         f = fopen(path, "r");
831                         if (f != NULL) {
832                                 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
833                                 break;
834                         }
835                 }
836         }
837         if (f != NULL) {
838                 SysPrintf("found BIOS file: %s\n", Config.Bios);
839                 fclose(f);
840         }
841         else
842                 SysPrintf("no BIOS files found.\n");
843
844         level = 1;
845         environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
846
847         environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
848         environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
849
850         /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
851          * we have to do this because cache misses and some IO penalties
852          * are not emulated. Warning: changing this may break compatibility. */
853 #ifdef __ARM_ARCH_7A__
854         cycle_multiplier = 175;
855 #else
856         cycle_multiplier = 200;
857 #endif
858
859         McdDisable[0] = 0;
860         McdDisable[1] = 1;
861         init_memcard(Mcd1Data);
862
863         SaveFuncs.open = save_open;
864         SaveFuncs.read = save_read;
865         SaveFuncs.write = save_write;
866         SaveFuncs.seek = save_seek;
867         SaveFuncs.close = save_close;
868
869    update_variables();
870 }
871
872 void retro_deinit(void)
873 {
874         SysClose();
875         free(vout_buf);
876         vout_buf = NULL;
877 }