(NEON ARM) Add NEON ARM core option (Enhanced resolution - slow)
[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 #ifdef __ARM_NEON__
236    static const struct retro_variable vars[] = {
237       { "neon_enhancement_enable", "Enhanced resolution (slow); disabled|enabled" },
238       { NULL, NULL },
239    };
240
241    environ_cb = cb;
242
243    cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)vars);
244 #else
245    environ_cb = cb;
246 #endif
247 }
248
249 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
250 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
251 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
252 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
253 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
254
255 unsigned retro_api_version(void)
256 {
257         return RETRO_API_VERSION;
258 }
259
260 void retro_set_controller_port_device(unsigned port, unsigned device)
261 {
262 }
263
264 void retro_get_system_info(struct retro_system_info *info)
265 {
266         memset(info, 0, sizeof(*info));
267         info->library_name = "PCSX-ReARMed";
268         info->library_version = "r19";
269         info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u";
270         info->need_fullpath = true;
271 }
272
273 void retro_get_system_av_info(struct retro_system_av_info *info)
274 {
275         memset(info, 0, sizeof(*info));
276         info->timing.fps            = is_pal_mode ? 50 : 60;
277         info->timing.sample_rate    = 44100;
278         info->geometry.base_width   = 320;
279         info->geometry.base_height  = 240;
280         info->geometry.max_width    = 640;
281         info->geometry.max_height   = 512;
282         info->geometry.aspect_ratio = 4.0 / 3.0;
283 }
284
285 /* savestates */
286 size_t retro_serialize_size(void) 
287
288         // it's currently 4380651 bytes, but have some reserved for future
289         return 0x430000;
290 }
291
292 struct save_fp {
293         char *buf;
294         size_t pos;
295         int is_write;
296 };
297
298 static void *save_open(const char *name, const char *mode)
299 {
300         struct save_fp *fp;
301
302         if (name == NULL || mode == NULL)
303                 return NULL;
304
305         fp = malloc(sizeof(*fp));
306         if (fp == NULL)
307                 return NULL;
308
309         fp->buf = (char *)name;
310         fp->pos = 0;
311         fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
312
313         return fp;
314 }
315
316 static int save_read(void *file, void *buf, u32 len)
317 {
318         struct save_fp *fp = file;
319         if (fp == NULL || buf == NULL)
320                 return -1;
321
322         memcpy(buf, fp->buf + fp->pos, len);
323         fp->pos += len;
324         return len;
325 }
326
327 static int save_write(void *file, const void *buf, u32 len)
328 {
329         struct save_fp *fp = file;
330         if (fp == NULL || buf == NULL)
331                 return -1;
332
333         memcpy(fp->buf + fp->pos, buf, len);
334         fp->pos += len;
335         return len;
336 }
337
338 static long save_seek(void *file, long offs, int whence)
339 {
340         struct save_fp *fp = file;
341         if (fp == NULL)
342                 return -1;
343
344         switch (whence) {
345         case SEEK_CUR:
346                 fp->pos += offs;
347                 return fp->pos;
348         case SEEK_SET:
349                 fp->pos = offs;
350                 return fp->pos;
351         default:
352                 return -1;
353         }
354 }
355
356 static void save_close(void *file)
357 {
358         struct save_fp *fp = file;
359         size_t r_size = retro_serialize_size();
360         if (fp == NULL)
361                 return;
362
363         if (fp->pos > r_size)
364                 SysPrintf("ERROR: save buffer overflow detected\n");
365         else if (fp->is_write && fp->pos < r_size)
366                 // make sure we don't save trash in leftover space
367                 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
368         free(fp);
369 }
370
371 bool retro_serialize(void *data, size_t size)
372
373         int ret = SaveState(data);
374         return ret == 0 ? true : false;
375 }
376
377 bool retro_unserialize(const void *data, size_t size)
378 {
379         int ret = LoadState(data);
380         return ret == 0 ? true : false;
381 }
382
383 /* cheats */
384 void retro_cheat_reset(void)
385 {
386         ClearAllCheats();
387 }
388
389 void retro_cheat_set(unsigned index, bool enabled, const char *code)
390 {
391         char buf[256];
392         int ret;
393
394         // cheat funcs are destructive, need a copy..
395         strncpy(buf, code, sizeof(buf));
396         buf[sizeof(buf) - 1] = 0;
397
398         if (index < NumCheats)
399                 ret = EditCheat(index, "", buf);
400         else
401                 ret = AddCheat("", buf);
402
403         if (ret != 0)
404                 SysPrintf("Failed to set cheat %#u\n", index);
405         else if (index < NumCheats)
406                 Cheats[index].Enabled = enabled;
407 }
408
409 /* multidisk support */
410 static bool disk_ejected;
411 static unsigned int disk_current_index;
412 static unsigned int disk_count;
413 static struct disks_state {
414         char *fname;
415         int internal_index; // for multidisk eboots
416 } disks[8];
417
418 static bool disk_set_eject_state(bool ejected)
419 {
420         // weird PCSX API..
421         SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
422         LidInterrupt();
423
424         disk_ejected = ejected;
425         return true;
426 }
427
428 static bool disk_get_eject_state(void)
429 {
430         /* can't be controlled by emulated software */
431         return disk_ejected;
432 }
433
434 static unsigned int disk_get_image_index(void)
435 {
436         return disk_current_index;
437 }
438
439 static bool disk_set_image_index(unsigned int index)
440 {
441         if (index >= sizeof(disks) / sizeof(disks[0]))
442                 return false;
443
444         CdromId[0] = '\0';
445         CdromLabel[0] = '\0';
446
447         if (disks[index].fname == NULL) {
448                 SysPrintf("missing disk #%u\n", index);
449                 CDR_shutdown();
450
451                 // RetroArch specifies "no disk" with index == count,
452                 // so don't fail here..
453                 disk_current_index = index;
454                 return true;
455         }
456
457         SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
458                 disks[index].fname, disks[index].internal_index);
459
460         cdrIsoMultidiskSelect = disks[index].internal_index;
461         set_cd_image(disks[index].fname);
462         if (ReloadCdromPlugin() < 0) {
463                 SysPrintf("failed to load cdr plugin\n");
464                 return false;
465         }
466         if (CDR_open() < 0) {
467                 SysPrintf("failed to open cdr plugin\n");
468                 return false;
469         }
470
471         if (!disk_ejected) {
472                 SetCdOpenCaseTime(time(NULL) + 2);
473                 LidInterrupt();
474         }
475
476         disk_current_index = index;
477         return true;
478 }
479
480 static unsigned int disk_get_num_images(void)
481 {
482         return disk_count;
483 }
484
485 static bool disk_replace_image_index(unsigned index,
486         const struct retro_game_info *info)
487 {
488         char *old_fname;
489         bool ret = true;
490
491         if (index >= sizeof(disks) / sizeof(disks[0]))
492                 return false;
493
494         old_fname = disks[index].fname;
495         disks[index].fname = NULL;
496         disks[index].internal_index = 0;
497
498         if (info != NULL) {
499                 disks[index].fname = strdup(info->path);
500                 if (index == disk_current_index)
501                         ret = disk_set_image_index(index);
502         }
503
504         if (old_fname != NULL)
505                 free(old_fname);
506
507         return ret;
508 }
509
510 static bool disk_add_image_index(void)
511 {
512         if (disk_count >= 8)
513                 return false;
514
515         disk_count++;
516         return true;
517 }
518
519 static struct retro_disk_control_callback disk_control = {
520         .set_eject_state = disk_set_eject_state,
521         .get_eject_state = disk_get_eject_state,
522         .get_image_index = disk_get_image_index,
523         .set_image_index = disk_set_image_index,
524         .get_num_images = disk_get_num_images,
525         .replace_image_index = disk_replace_image_index,
526         .add_image_index = disk_add_image_index,
527 };
528
529 // just in case, maybe a win-rt port in the future?
530 #ifdef _WIN32
531 #define SLASH '\\'
532 #else
533 #define SLASH '/'
534 #endif
535
536 static char base_dir[PATH_MAX];
537
538 static bool read_m3u(const char *file)
539 {
540         char line[PATH_MAX];
541         char name[PATH_MAX];
542         FILE *f = fopen(file, "r");
543         if (!f)
544                 return false;
545
546         while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) {
547                 if (line[0] == '#')
548                         continue;
549                 char *carrige_return = strchr(line, '\r');
550                 if (carrige_return)
551                         *carrige_return = '\0';
552                 char *newline = strchr(line, '\n');
553                 if (newline)
554                         *newline = '\0';
555
556                 if (line[0] != '\0')
557                 {
558                         snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
559                         disks[disk_count++].fname = strdup(name);
560                 }
561         }
562
563         fclose(f);
564         return (disk_count != 0);
565 }
566
567 static void extract_directory(char *buf, const char *path, size_t size)
568 {
569    char *base;
570    strncpy(buf, path, size - 1);
571    buf[size - 1] = '\0';
572
573    base = strrchr(buf, '/');
574    if (!base)
575       base = strrchr(buf, '\\');
576
577    if (base)
578       *base = '\0';
579    else
580    {
581       buf[0] = '.';
582       buf[1] = '\0';
583    }
584 }
585
586 bool retro_load_game(const struct retro_game_info *info)
587 {
588         size_t i;
589         bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
590
591 #ifdef FRONTEND_SUPPORTS_RGB565
592         enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
593         if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
594                 SysPrintf("RGB565 supported, using it\n");
595         }
596 #endif
597
598         if (info == NULL || info->path == NULL) {
599                 SysPrintf("info->path required\n");
600                 return false;
601         }
602
603         if (plugins_opened) {
604                 ClosePlugins();
605                 plugins_opened = 0;
606         }
607
608         for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
609                 if (disks[i].fname != NULL) {
610                         free(disks[i].fname);
611                         disks[i].fname = NULL;
612                 }
613                 disks[i].internal_index = 0;
614         }
615
616         disk_current_index = 0;
617         extract_directory(base_dir, info->path, sizeof(base_dir));
618
619         if (is_m3u) {
620                 if (!read_m3u(info->path)) {
621                         SysPrintf("failed to read m3u file\n");
622                         return false;
623                 }
624         } else {
625                 disk_count = 1;
626                 disks[0].fname = strdup(info->path);
627         }
628
629         set_cd_image(disks[0].fname);
630
631         /* have to reload after set_cd_image for correct cdr plugin */
632         if (LoadPlugins() == -1) {
633                 SysPrintf("failed to load plugins\n");
634                 return false;
635         }
636
637         plugins_opened = 1;
638         NetOpened = 0;
639
640         if (OpenPlugins() == -1) {
641                 SysPrintf("failed to open plugins\n");
642                 return false;
643         }
644
645         plugin_call_rearmed_cbs();
646
647         Config.PsxAuto = 1;
648         if (CheckCdrom() == -1) {
649                 SysPrintf("unsupported/invalid CD image: %s\n", info->path);
650                 return false;
651         }
652
653         SysReset();
654
655         if (LoadCdrom() == -1) {
656                 SysPrintf("could not load CD-ROM!\n");
657                 return false;
658         }
659         emu_on_new_cd(0);
660
661         // multidisk images
662         if (!is_m3u) {
663                 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
664                 for (i = 1; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++) {
665                         disks[i].fname = strdup(info->path);
666                         disks[i].internal_index = i;
667                 }
668         }
669
670         return true;
671 }
672
673 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
674 {
675         return false;
676 }
677
678 void retro_unload_game(void) 
679 {
680 }
681
682 unsigned retro_get_region(void)
683 {
684         return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
685 }
686
687 void *retro_get_memory_data(unsigned id)
688 {
689         if (id == RETRO_MEMORY_SAVE_RAM)
690                 return Mcd1Data;
691         else
692                 return NULL;
693 }
694
695 size_t retro_get_memory_size(unsigned id)
696 {
697         if (id == RETRO_MEMORY_SAVE_RAM)
698                 return MCD_SIZE;
699         else
700                 return 0;
701 }
702
703 void retro_reset(void)
704 {
705         SysReset();
706 }
707
708 static const unsigned short retro_psx_map[] = {
709         [RETRO_DEVICE_ID_JOYPAD_B]      = 1 << DKEY_CROSS,
710         [RETRO_DEVICE_ID_JOYPAD_Y]      = 1 << DKEY_SQUARE,
711         [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
712         [RETRO_DEVICE_ID_JOYPAD_START]  = 1 << DKEY_START,
713         [RETRO_DEVICE_ID_JOYPAD_UP]     = 1 << DKEY_UP,
714         [RETRO_DEVICE_ID_JOYPAD_DOWN]   = 1 << DKEY_DOWN,
715         [RETRO_DEVICE_ID_JOYPAD_LEFT]   = 1 << DKEY_LEFT,
716         [RETRO_DEVICE_ID_JOYPAD_RIGHT]  = 1 << DKEY_RIGHT,
717         [RETRO_DEVICE_ID_JOYPAD_A]      = 1 << DKEY_CIRCLE,
718         [RETRO_DEVICE_ID_JOYPAD_X]      = 1 << DKEY_TRIANGLE,
719         [RETRO_DEVICE_ID_JOYPAD_L]      = 1 << DKEY_L1,
720         [RETRO_DEVICE_ID_JOYPAD_R]      = 1 << DKEY_R1,
721         [RETRO_DEVICE_ID_JOYPAD_L2]     = 1 << DKEY_L2,
722         [RETRO_DEVICE_ID_JOYPAD_R2]     = 1 << DKEY_R2,
723         [RETRO_DEVICE_ID_JOYPAD_L3]     = 1 << DKEY_L3,
724         [RETRO_DEVICE_ID_JOYPAD_R3]     = 1 << DKEY_R3,
725 };
726 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
727
728 static void update_variables(void)
729 {
730 #ifdef __ARM_NEON__
731    struct retro_variable var;
732    var.value = NULL;
733    var.key = "neon_enhancement_enable";
734
735    if (!environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || !var.value)
736       return;
737
738    if (strcmp(var.value, "disabled") == 0)
739       pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
740    else if (strcmp(var.value, "enabled") == 0)
741       pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
742 #endif
743 }
744
745 void retro_run(void) 
746 {
747         int i;
748
749         input_poll_cb();
750
751    bool updated = false;
752    if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
753       update_variables();
754
755         in_keystate = 0;
756         for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
757                 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
758                         in_keystate |= retro_psx_map[i];
759         in_keystate <<= 16;
760         for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
761                 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
762                         in_keystate |= retro_psx_map[i];
763
764         stop = 0;
765         psxCpu->Execute();
766
767         samples_to_send += is_pal_mode ? 44100 / 50 : 44100 / 60;
768
769         video_cb((vout_fb_dirty || !vout_can_dupe) ? vout_buf : NULL,
770                 vout_width, vout_height, vout_width * 2);
771         vout_fb_dirty = 0;
772 }
773
774 void retro_init(void)
775 {
776         const char *bios[] = { "scph1001", "scph5501", "scph7001" };
777         const char *dir;
778         char path[256];
779         FILE *f = NULL;
780         int i, ret, level;
781
782         ret = emu_core_preinit();
783         ret |= emu_core_init();
784         if (ret != 0) {
785                 SysPrintf("PCSX init failed.\n");
786                 exit(1);
787         }
788
789         vout_buf = malloc(640 * 512 * 2);
790
791         if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
792         {
793                 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
794
795                 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
796                         snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
797                         f = fopen(path, "r");
798                         if (f != NULL) {
799                                 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
800                                 break;
801                         }
802                 }
803         }
804         if (f != NULL) {
805                 SysPrintf("found BIOS file: %s\n", Config.Bios);
806                 fclose(f);
807         }
808         else
809                 SysPrintf("no BIOS files found.\n");
810
811         level = 1;
812         environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
813
814         environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
815         environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
816
817         /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
818          * we have to do this because cache misses and some IO penalties
819          * are not emulated. Warning: changing this may break compatibility. */
820 #ifdef __ARM_ARCH_7A__
821         cycle_multiplier = 175;
822 #else
823         cycle_multiplier = 200;
824 #endif
825
826         McdDisable[0] = 0;
827         McdDisable[1] = 1;
828         init_memcard(Mcd1Data);
829
830         SaveFuncs.open = save_open;
831         SaveFuncs.read = save_read;
832         SaveFuncs.write = save_write;
833         SaveFuncs.seek = save_seek;
834         SaveFuncs.close = save_close;
835
836    update_variables();
837 }
838
839 void retro_deinit(void)
840 {
841         SysClose();
842         free(vout_buf);
843         vout_buf = NULL;
844 }