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