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