2 * (C) notaz, 2012,2014,2015
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
8 #define _GNU_SOURCE 1 // strcasestr
15 #include <sys/syscall.h>
18 #include "../libpcsxcore/misc.h"
19 #include "../libpcsxcore/psxcounters.h"
20 #include "../libpcsxcore/psxmem_map.h"
21 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
22 #include "../libpcsxcore/cdrom.h"
23 #include "../libpcsxcore/cdriso.h"
24 #include "../libpcsxcore/cheat.h"
25 #include "../plugins/dfsound/out.h"
26 #include "../plugins/dfsound/spu_config.h"
27 #include "../plugins/dfinput/externals.h"
31 #include "plugin_lib.h"
32 #include "arm_features.h"
37 #include "3ds/3ds_utils.h"
40 #define PORTS_NUMBER 8
43 #define MIN(a, b) ((a) < (b) ? (a) : (b))
46 #define ISHEXDEC ((buf[cursor]>='0') && (buf[cursor]<='9')) || ((buf[cursor]>='a') && (buf[cursor]<='f')) || ((buf[cursor]>='A') && (buf[cursor]<='F'))
48 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
49 static int rebootemu = 0;
51 static retro_video_refresh_t video_cb;
52 static retro_input_poll_t input_poll_cb;
53 static retro_input_state_t input_state_cb;
54 static retro_environment_t environ_cb;
55 static retro_audio_sample_batch_t audio_batch_cb;
56 static struct retro_rumble_interface rumble;
57 static struct retro_log_callback logging;
58 static retro_log_printf_t log_cb;
60 static void *vout_buf;
61 static void * vout_buf_ptr;
62 static int vout_width, vout_height;
63 static int vout_doffs_old, vout_fb_dirty;
64 static bool vout_can_dupe;
65 static bool duping_enable;
66 static bool found_bios;
68 static int plugins_opened;
69 static int is_pal_mode;
71 /* memory card data */
72 extern char Mcd1Data[MCD_SIZE];
73 extern char Mcd2Data[MCD_SIZE];
74 extern char McdDisable[2];
76 /* PCSX ReARMed core calls and stuff */
77 int in_type[8] = { PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
78 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
79 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
80 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE };
81 int in_analog_left[8][2] = {{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 }};
82 int in_analog_right[8][2] = {{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 }};
83 unsigned short in_keystate[PORTS_NUMBER];
86 int in_enable_vibration = 1;
88 /* PSX max resolution is 640x512, but with enhancement it's 1024x512 */
89 #define VOUT_MAX_WIDTH 1024
90 #define VOUT_MAX_HEIGHT 512
93 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info){return false;}
94 void retro_unload_game(void){}
95 static int vout_open(void){return 0;}
96 static void vout_close(void){}
97 static int snd_init(void){return 0;}
98 static void snd_finish(void){}
99 static int snd_busy(void){return 0;}
101 static void init_memcard(char *mcd_data)
106 memset(mcd_data, 0, MCD_SIZE);
108 mcd_data[off++] = 'M';
109 mcd_data[off++] = 'C';
111 mcd_data[off++] = 0x0e;
113 for (i = 0; i < 15; i++) {
114 mcd_data[off++] = 0xa0;
116 mcd_data[off++] = 0xff;
117 mcd_data[off++] = 0xff;
119 mcd_data[off++] = 0xa0;
122 for (i = 0; i < 20; i++) {
123 mcd_data[off++] = 0xff;
124 mcd_data[off++] = 0xff;
125 mcd_data[off++] = 0xff;
126 mcd_data[off++] = 0xff;
128 mcd_data[off++] = 0xff;
129 mcd_data[off++] = 0xff;
134 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
139 struct retro_framebuffer fb = {0};
141 fb.width = vout_width;
142 fb.height = vout_height;
143 fb.access_flags = RETRO_MEMORY_ACCESS_WRITE;
145 vout_buf_ptr = vout_buf;
147 if (environ_cb(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &fb) && fb.format == RETRO_PIXEL_FORMAT_RGB565)
149 vout_buf_ptr = (uint16_t*)fb.data;
154 #ifndef FRONTEND_SUPPORTS_RGB565
155 static void convert(void *buf, size_t bytes)
157 unsigned int i, v, *p = buf;
159 for (i = 0; i < bytes / 4; i++) {
161 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
166 static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
168 unsigned short *dest = vout_buf_ptr;
169 const unsigned short *src = vram;
170 int dstride = vout_width, h1 = h;
175 memset(vout_buf_ptr, 0, dstride * h * 2);
179 doffs = (vout_height - h) * dstride;
180 doffs += (dstride - w) / 2 & ~1;
181 if (doffs != vout_doffs_old) {
183 memset(vout_buf_ptr, 0, dstride * h * 2);
184 vout_doffs_old = doffs;
190 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
191 for (; h1-- > 0; dest += dstride, src += stride)
193 bgr888_to_rgb565(dest, src, w * 3);
198 for (; h1-- > 0; dest += dstride, src += stride)
200 bgr555_to_rgb565(dest, src, w * 2);
205 #ifndef FRONTEND_SUPPORTS_RGB565
206 convert(vout_buf_ptr, vout_width * vout_height * 2);
209 pl_rearmed_cbs.flip_cnt++;
221 psx_map_t custom_psx_maps[] = {
222 {NULL, 0x13000000, 0x210000, MAP_TAG_RAM}, // 0x80000000
223 {NULL, 0x12800000, 0x010000, MAP_TAG_OTHER}, // 0x1f800000
224 {NULL, 0x12c00000, 0x080000, MAP_TAG_OTHER}, // 0x1fc00000
225 {NULL, 0x11000000, 0x800000, MAP_TAG_LUTS}, // 0x08000000
226 {NULL, 0x12000000, 0x200000, MAP_TAG_VRAM}, // 0x00000000
229 void* pl_3ds_mmap(unsigned long addr, size_t size, int is_fixed,
237 psx_map_t* custom_map = custom_psx_maps;
239 for (; custom_map->size; custom_map++)
241 if ((custom_map->size == size) && (custom_map->tag == tag))
243 uint32_t ptr_aligned, tmp;
245 custom_map->buffer = malloc(size + 0x1000);
246 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
248 if(svcControlMemory(&tmp, (void*)custom_map->target_map, (void*)ptr_aligned, size, MEMOP_MAP, 0x3) < 0)
250 SysPrintf("could not map memory @0x%08X\n", custom_map->target_map);
254 return (void*)custom_map->target_map;
262 void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
268 psx_map_t* custom_map = custom_psx_maps;
270 for (; custom_map->size; custom_map++)
272 if ((custom_map->target_map == (uint32_t)ptr))
274 uint32_t ptr_aligned, tmp;
276 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
278 svcControlMemory(&tmp, (void*)custom_map->target_map, (void*)ptr_aligned, size, MEMOP_UNMAP, 0x3);
280 free(custom_map->buffer);
281 custom_map->buffer = NULL;
302 psx_map_t custom_psx_maps[] = {
303 {NULL, NULL, 0x210000, MAP_TAG_RAM}, // 0x80000000
304 {NULL, NULL, 0x010000, MAP_TAG_OTHER}, // 0x1f800000
305 {NULL, NULL, 0x080000, MAP_TAG_OTHER}, // 0x1fc00000
306 {NULL, NULL, 0x800000, MAP_TAG_LUTS}, // 0x08000000
307 {NULL, NULL, 0x200000, MAP_TAG_VRAM}, // 0x00000000
310 int init_vita_mmap(){
313 addr = malloc(64*1024*1024);
316 tmpaddr = ((u32)(addr+0xFFFFFF))&~0xFFFFFF;
317 custom_psx_maps[0].buffer=tmpaddr+0x2000000;
318 custom_psx_maps[1].buffer=tmpaddr+0x1800000;
319 custom_psx_maps[2].buffer=tmpaddr+0x1c00000;
320 custom_psx_maps[3].buffer=tmpaddr+0x0000000;
321 custom_psx_maps[4].buffer=tmpaddr+0x1000000;
323 for(n = 0; n < 5; n++){
324 sceClibPrintf("addr reserved %x\n",custom_psx_maps[n].buffer);
330 void deinit_vita_mmap(){
334 void* pl_vita_mmap(unsigned long addr, size_t size, int is_fixed,
341 psx_map_t* custom_map = custom_psx_maps;
343 for (; custom_map->size; custom_map++)
345 if ((custom_map->size == size) && (custom_map->tag == tag))
347 return custom_map->buffer;
355 void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag)
359 psx_map_t* custom_map = custom_psx_maps;
361 for (; custom_map->size; custom_map++)
363 if ((custom_map->buffer == ptr))
373 static void *pl_mmap(unsigned int size)
375 return psxMap(0, size, 0, MAP_TAG_VRAM);
378 static void pl_munmap(void *ptr, unsigned int size)
380 psxUnmap(ptr, size, MAP_TAG_VRAM);
383 struct rearmed_cbs pl_rearmed_cbs = {
384 .pl_vout_open = vout_open,
385 .pl_vout_set_mode = vout_set_mode,
386 .pl_vout_flip = vout_flip,
387 .pl_vout_close = vout_close,
390 /* from psxcounters */
391 .gpu_hcnt = &hSyncCount,
392 .gpu_frame_count = &frame_counter,
395 void pl_frame_limit(void)
397 /* called once per frame, make psxCpu->Execute() above return */
401 void pl_timing_prepare(int is_pal)
403 is_pal_mode = is_pal;
406 void plat_trigger_vibrate(int pad, int low, int high)
408 if(in_enable_vibration)
410 rumble.set_rumble_state(pad, RETRO_RUMBLE_STRONG, high << 8);
411 rumble.set_rumble_state(pad, RETRO_RUMBLE_WEAK, low ? 0xffff : 0x0);
415 void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
420 static void snd_feed(void *buf, int bytes)
422 if (audio_batch_cb != NULL)
423 audio_batch_cb(buf, bytes / 4);
426 void out_register_libretro(struct out_driver *drv)
428 drv->name = "libretro";
429 drv->init = snd_init;
430 drv->finish = snd_finish;
431 drv->busy = snd_busy;
432 drv->feed = snd_feed;
436 void retro_set_environment(retro_environment_t cb)
438 static const struct retro_variable vars[] = {
439 { "pcsx_rearmed_frameskip", "Frameskip; 0|1|2|3" },
440 { "pcsx_rearmed_region", "Region; auto|NTSC|PAL" },
441 { "pcsx_rearmed_pad1type", "Pad 1 Type; default|none|standard|analog|negcon" },
442 { "pcsx_rearmed_pad2type", "Pad 2 Type; default|none|standard|analog|negcon" },
443 { "pcsx_rearmed_pad3type", "Pad 3 Type; default|none|standard|analog|negcon" },
444 { "pcsx_rearmed_pad4type", "Pad 4 Type; default|none|standard|analog|negcon" },
445 { "pcsx_rearmed_pad5type", "Pad 5 Type; default|none|standard|analog|negcon" },
446 { "pcsx_rearmed_pad6type", "Pad 6 Type; default|none|standard|analog|negcon" },
447 { "pcsx_rearmed_pad7type", "Pad 7 Type; default|none|standard|analog|negcon" },
448 { "pcsx_rearmed_pad8type", "Pad 8 Type; default|none|standard|analog|negcon" },
449 { "pcsx_rearmed_multitap1", "Multitap 1; auto|disabled|enabled" },
450 { "pcsx_rearmed_multitap2", "Multitap 2; auto|disabled|enabled" },
451 { "pcsx_rearmed_vibration", "Enable Vibration; enabled|disabled" },
452 { "pcsx_rearmed_dithering", "Enable Dithering; enabled|disabled" },
454 { "pcsx_rearmed_drc", "Dynamic recompiler; enabled|disabled" },
457 { "pcsx_rearmed_neon_interlace_enable", "Enable interlacing mode(s); disabled|enabled" },
458 { "pcsx_rearmed_neon_enhancement_enable", "Enhanced resolution (slow); disabled|enabled" },
459 { "pcsx_rearmed_neon_enhancement_no_main", "Enhanced resolution speed hack; disabled|enabled" },
461 { "pcsx_rearmed_duping_enable", "Frame duping; enabled|disabled" },
462 { "pcsx_rearmed_show_bios_bootlogo", "Show Bios Bootlogo(Breaks some games); disabled|enabled" },
463 { "pcsx_rearmed_spu_reverb", "Sound: Reverb; enabled|disabled" },
464 { "pcsx_rearmed_spu_interpolation", "Sound: Interpolation; simple|gaussian|cubic|off" },
465 { "pcsx_rearmed_pe2_fix", "Parasite Eve 2/Vandal Hearts 1/2 Fix; disabled|enabled" },
466 { "pcsx_rearmed_inuyasha_fix", "InuYasha Sengoku Battle Fix; disabled|enabled" },
470 if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
471 log_cb = logging.log;
475 cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)vars);
478 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
479 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
480 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
481 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
482 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
484 unsigned retro_api_version(void)
486 return RETRO_API_VERSION;
489 static int controller_port_variable(unsigned port, struct retro_variable *var)
491 if (port >= PORTS_NUMBER)
500 var->key = "pcsx_rearmed_pad1type";
503 var->key = "pcsx_rearmed_pad2type";
506 var->key = "pcsx_rearmed_pad3type";
509 var->key = "pcsx_rearmed_pad4type";
512 var->key = "pcsx_rearmed_pad5type";
515 var->key = "pcsx_rearmed_pad6type";
518 var->key = "pcsx_rearmed_pad7type";
521 var->key = "pcsx_rearmed_pad8type";
525 return environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, var) || var->value;
528 static void update_controller_port_variable(unsigned port)
530 if (port >= PORTS_NUMBER)
533 struct retro_variable var;
535 if (controller_port_variable(port, &var))
537 if (strcmp(var.value, "standard") == 0)
538 in_type[port] = PSE_PAD_TYPE_STANDARD;
539 else if (strcmp(var.value, "analog") == 0)
540 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
541 else if (strcmp(var.value, "negcon") == 0)
542 in_type[port] = PSE_PAD_TYPE_NEGCON;
543 else if (strcmp(var.value, "none") == 0)
544 in_type[port] = PSE_PAD_TYPE_NONE;
545 // else 'default' case, do nothing
549 static void update_controller_port_device(unsigned port, unsigned device)
551 if (port >= PORTS_NUMBER)
554 struct retro_variable var;
556 if (!controller_port_variable(port, &var))
559 if (strcmp(var.value, "default") != 0)
564 case RETRO_DEVICE_JOYPAD:
565 in_type[port] = PSE_PAD_TYPE_STANDARD;
567 case RETRO_DEVICE_ANALOG:
568 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
570 case RETRO_DEVICE_MOUSE:
571 in_type[port] = PSE_PAD_TYPE_MOUSE;
573 case RETRO_DEVICE_LIGHTGUN:
574 in_type[port] = PSE_PAD_TYPE_GUN;
576 case RETRO_DEVICE_NONE:
578 in_type[port] = PSE_PAD_TYPE_NONE;
582 static void update_multitap()
584 struct retro_variable var;
588 var.key = "pcsx_rearmed_multitap1";
590 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value))
592 if (strcmp(var.value, "enabled") == 0)
594 else if (strcmp(var.value, "disabled") == 0)
604 // If a gamepad is plugged after port 2, we need a first multitap.
606 for (port = 2; port < PORTS_NUMBER; port++)
607 multitap1 |= in_type[port] != PSE_PAD_TYPE_NONE;
611 var.key = "pcsx_rearmed_multitap2";
613 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value))
615 if (strcmp(var.value, "enabled") == 0)
617 else if (strcmp(var.value, "disabled") == 0)
627 // If a gamepad is plugged after port 4, we need a second multitap.
629 for (port = 4; port < PORTS_NUMBER; port++)
630 multitap2 |= in_type[port] != PSE_PAD_TYPE_NONE;
634 void retro_set_controller_port_device(unsigned port, unsigned device)
636 SysPrintf("port %u device %u",port,device);
638 if (port >= PORTS_NUMBER)
641 update_controller_port_device(port, device);
645 void retro_get_system_info(struct retro_system_info *info)
647 memset(info, 0, sizeof(*info));
648 info->library_name = "PCSX-ReARMed";
650 #define GIT_VERSION ""
652 info->library_version = "r22" GIT_VERSION;
653 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u";
654 info->need_fullpath = true;
657 void retro_get_system_av_info(struct retro_system_av_info *info)
659 memset(info, 0, sizeof(*info));
660 info->timing.fps = is_pal_mode ? 50 : 60;
661 info->timing.sample_rate = 44100;
662 info->geometry.base_width = 320;
663 info->geometry.base_height = 240;
664 info->geometry.max_width = VOUT_MAX_WIDTH;
665 info->geometry.max_height = VOUT_MAX_HEIGHT;
666 info->geometry.aspect_ratio = 4.0 / 3.0;
670 size_t retro_serialize_size(void)
672 // it's currently 4380651-4397047 bytes,
673 // but have some reserved for future
683 static void *save_open(const char *name, const char *mode)
687 if (name == NULL || mode == NULL)
690 fp = malloc(sizeof(*fp));
694 fp->buf = (char *)name;
696 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
701 static int save_read(void *file, void *buf, u32 len)
703 struct save_fp *fp = file;
704 if (fp == NULL || buf == NULL)
707 memcpy(buf, fp->buf + fp->pos, len);
712 static int save_write(void *file, const void *buf, u32 len)
714 struct save_fp *fp = file;
715 if (fp == NULL || buf == NULL)
718 memcpy(fp->buf + fp->pos, buf, len);
723 static long save_seek(void *file, long offs, int whence)
725 struct save_fp *fp = file;
741 static void save_close(void *file)
743 struct save_fp *fp = file;
744 size_t r_size = retro_serialize_size();
748 if (fp->pos > r_size)
749 SysPrintf("ERROR: save buffer overflow detected\n");
750 else if (fp->is_write && fp->pos < r_size)
751 // make sure we don't save trash in leftover space
752 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
756 bool retro_serialize(void *data, size_t size)
758 int ret = SaveState(data);
759 return ret == 0 ? true : false;
762 bool retro_unserialize(const void *data, size_t size)
764 int ret = LoadState(data);
765 return ret == 0 ? true : false;
769 void retro_cheat_reset(void)
774 void retro_cheat_set(unsigned index, bool enabled, const char *code)
779 // cheat funcs are destructive, need a copy..
780 strncpy(buf, code, sizeof(buf));
781 buf[sizeof(buf) - 1] = 0;
783 //Prepare buffered cheat for PCSX's AddCheat fucntion.
798 if (index < NumCheats)
799 ret = EditCheat(index, "", buf);
801 ret = AddCheat("", buf);
804 SysPrintf("Failed to set cheat %#u\n", index);
805 else if (index < NumCheats)
806 Cheats[index].Enabled = enabled;
809 /* multidisk support */
810 static bool disk_ejected;
811 static unsigned int disk_current_index;
812 static unsigned int disk_count;
813 static struct disks_state {
815 int internal_index; // for multidisk eboots
818 static bool disk_set_eject_state(bool ejected)
821 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
824 disk_ejected = ejected;
828 static bool disk_get_eject_state(void)
830 /* can't be controlled by emulated software */
834 static unsigned int disk_get_image_index(void)
836 return disk_current_index;
839 static bool disk_set_image_index(unsigned int index)
841 if (index >= sizeof(disks) / sizeof(disks[0]))
845 CdromLabel[0] = '\0';
847 if (disks[index].fname == NULL) {
848 SysPrintf("missing disk #%u\n", index);
851 // RetroArch specifies "no disk" with index == count,
852 // so don't fail here..
853 disk_current_index = index;
857 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
858 disks[index].fname, disks[index].internal_index);
860 cdrIsoMultidiskSelect = disks[index].internal_index;
861 set_cd_image(disks[index].fname);
862 if (ReloadCdromPlugin() < 0) {
863 SysPrintf("failed to load cdr plugin\n");
866 if (CDR_open() < 0) {
867 SysPrintf("failed to open cdr plugin\n");
872 SetCdOpenCaseTime(time(NULL) + 2);
876 disk_current_index = index;
880 static unsigned int disk_get_num_images(void)
885 static bool disk_replace_image_index(unsigned index,
886 const struct retro_game_info *info)
891 if (index >= sizeof(disks) / sizeof(disks[0]))
894 old_fname = disks[index].fname;
895 disks[index].fname = NULL;
896 disks[index].internal_index = 0;
899 disks[index].fname = strdup(info->path);
900 if (index == disk_current_index)
901 ret = disk_set_image_index(index);
904 if (old_fname != NULL)
910 static bool disk_add_image_index(void)
919 static struct retro_disk_control_callback disk_control = {
920 .set_eject_state = disk_set_eject_state,
921 .get_eject_state = disk_get_eject_state,
922 .get_image_index = disk_get_image_index,
923 .set_image_index = disk_set_image_index,
924 .get_num_images = disk_get_num_images,
925 .replace_image_index = disk_replace_image_index,
926 .add_image_index = disk_add_image_index,
929 // just in case, maybe a win-rt port in the future?
937 #define PATH_MAX 4096
940 static char base_dir[PATH_MAX];
942 static bool read_m3u(const char *file)
946 FILE *f = fopen(file, "r");
950 while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) {
953 char *carrige_return = strchr(line, '\r');
955 *carrige_return = '\0';
956 char *newline = strchr(line, '\n');
962 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
963 disks[disk_count++].fname = strdup(name);
968 return (disk_count != 0);
971 static void extract_directory(char *buf, const char *path, size_t size)
974 strncpy(buf, path, size - 1);
975 buf[size - 1] = '\0';
977 base = strrchr(buf, '/');
979 base = strrchr(buf, '\\');
990 #if defined(__QNX__) || defined(_WIN32)
991 /* Blackberry QNX doesn't have strcasestr */
994 * Find the first occurrence of find in s, ignore case.
997 strcasestr(const char *s, const char*find)
1002 if ((c = *find++) != 0) {
1003 c = tolower((unsigned char)c);
1007 if ((sc = *s++) == 0)
1009 } while ((char)tolower((unsigned char)sc) != c);
1010 } while (strncasecmp(s, find, len) != 0);
1017 bool retro_load_game(const struct retro_game_info *info)
1020 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
1022 struct retro_input_descriptor desc[] = {
1023 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1024 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1025 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1026 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1027 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1028 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1029 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1030 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1031 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1032 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1033 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1034 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1035 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1036 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1037 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1038 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1039 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1040 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1041 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1042 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1045 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1046 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1047 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1048 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1049 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1050 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1051 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1052 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1053 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1054 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1055 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1056 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1057 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1058 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1059 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1060 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1061 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1062 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1063 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1064 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1066 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1067 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1068 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1069 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1070 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1071 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1072 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1073 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1074 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1075 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1076 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1077 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1078 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1079 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1080 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1081 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1082 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1083 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1084 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1085 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1087 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1088 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1089 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1090 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1091 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1092 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1093 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1094 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1095 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1096 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1097 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1098 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1099 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1100 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1101 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1102 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1103 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1104 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1105 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1106 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1108 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1109 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1110 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1111 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1112 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1113 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1114 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1115 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1116 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1117 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1118 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1119 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1120 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1121 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1122 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1123 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1124 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1125 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1126 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1127 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1129 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1130 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1131 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1132 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1133 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1134 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1135 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1136 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1137 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1138 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1139 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1140 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1141 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1142 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1143 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1144 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1145 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1146 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1147 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1148 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1150 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1151 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1152 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1153 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1154 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1155 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1156 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1157 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1158 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1159 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1160 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1161 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1162 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1163 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1164 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1165 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1166 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1167 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1168 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1169 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1171 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1172 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1173 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1174 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1175 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1176 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1177 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1178 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1179 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1180 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1181 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1182 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1183 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1184 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1185 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1186 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1187 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1188 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1189 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1190 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1195 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1197 #ifdef FRONTEND_SUPPORTS_RGB565
1198 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
1199 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
1200 SysPrintf("RGB565 supported, using it\n");
1204 if (info == NULL || info->path == NULL) {
1205 SysPrintf("info->path required\n");
1209 if (plugins_opened) {
1214 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
1215 if (disks[i].fname != NULL) {
1216 free(disks[i].fname);
1217 disks[i].fname = NULL;
1219 disks[i].internal_index = 0;
1222 disk_current_index = 0;
1223 extract_directory(base_dir, info->path, sizeof(base_dir));
1226 if (!read_m3u(info->path)) {
1227 log_cb(RETRO_LOG_INFO, "failed to read m3u file\n");
1232 disks[0].fname = strdup(info->path);
1235 set_cd_image(disks[0].fname);
1237 /* have to reload after set_cd_image for correct cdr plugin */
1238 if (LoadPlugins() == -1) {
1239 log_cb(RETRO_LOG_INFO, "failed to load plugins\n");
1246 if (OpenPlugins() == -1) {
1247 log_cb(RETRO_LOG_INFO, "failed to open plugins\n");
1251 plugin_call_rearmed_cbs();
1254 if (CheckCdrom() == -1) {
1255 log_cb(RETRO_LOG_INFO, "unsupported/invalid CD image: %s\n", info->path);
1261 if (LoadCdrom() == -1) {
1262 log_cb(RETRO_LOG_INFO, "could not load CD\n");
1269 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
1270 for (i = 1; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++) {
1271 disks[i].fname = strdup(info->path);
1272 disks[i].internal_index = i;
1279 unsigned retro_get_region(void)
1281 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1284 void *retro_get_memory_data(unsigned id)
1286 if (id == RETRO_MEMORY_SAVE_RAM)
1292 size_t retro_get_memory_size(unsigned id)
1294 if (id == RETRO_MEMORY_SAVE_RAM)
1300 void retro_reset(void)
1302 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
1307 static const unsigned short retro_psx_map[] = {
1308 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
1309 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
1310 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
1311 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
1312 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
1313 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
1314 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
1315 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
1316 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
1317 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
1318 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
1319 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
1320 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
1321 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
1322 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
1323 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
1325 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
1327 static void update_variables(bool in_flight)
1329 struct retro_variable var;
1333 var.key = "pcsx_rearmed_frameskip";
1334 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1335 pl_rearmed_cbs.frameskip = atoi(var.value);
1338 var.key = "pcsx_rearmed_region";
1339 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1342 if (strcmp(var.value, "auto") == 0)
1344 else if (strcmp(var.value, "NTSC") == 0)
1346 else if (strcmp(var.value, "PAL") == 0)
1350 for (i = 0; i < PORTS_NUMBER; i++)
1351 update_controller_port_variable(i);
1356 var.key = "pcsx_rearmed_vibration";
1358 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1360 if (strcmp(var.value, "disabled") == 0)
1361 in_enable_vibration = 0;
1362 else if (strcmp(var.value, "enabled") == 0)
1363 in_enable_vibration = 1;
1367 var.key = "pcsx_rearmed_dithering";
1369 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1371 if (strcmp(var.value, "disabled") == 0) {
1372 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
1373 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 0;
1375 pl_rearmed_cbs.gpu_neon.allow_dithering = 0;
1378 else if (strcmp(var.value, "enabled") == 0) {
1379 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1380 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 1;
1382 pl_rearmed_cbs.gpu_neon.allow_dithering = 1;
1389 var.key = "pcsx_rearmed_neon_interlace_enable";
1391 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1393 if (strcmp(var.value, "disabled") == 0)
1394 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
1395 else if (strcmp(var.value, "enabled") == 0)
1396 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
1400 var.key = "pcsx_rearmed_neon_enhancement_enable";
1402 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1404 if (strcmp(var.value, "disabled") == 0)
1405 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
1406 else if (strcmp(var.value, "enabled") == 0)
1407 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
1411 var.key = "pcsx_rearmed_neon_enhancement_no_main";
1413 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1415 if (strcmp(var.value, "disabled") == 0)
1416 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
1417 else if (strcmp(var.value, "enabled") == 0)
1418 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
1423 var.key = "pcsx_rearmed_duping_enable";
1425 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1427 if (strcmp(var.value, "disabled") == 0)
1428 duping_enable = false;
1429 else if (strcmp(var.value, "enabled") == 0)
1430 duping_enable = true;
1435 var.key = "pcsx_rearmed_drc";
1437 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1439 R3000Acpu *prev_cpu = psxCpu;
1443 Config.Cpu = CPU_INTERPRETER;
1446 if (strcmp(var.value, "disabled") == 0)
1447 Config.Cpu = CPU_INTERPRETER;
1448 else if (strcmp(var.value, "enabled") == 0)
1449 Config.Cpu = CPU_DYNAREC;
1451 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
1452 if (psxCpu != prev_cpu) {
1453 prev_cpu->Shutdown();
1455 psxCpu->Reset(); // not really a reset..
1461 var.key = "pcsx_rearmed_spu_reverb";
1463 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1465 if (strcmp(var.value, "disabled") == 0)
1466 spu_config.iUseReverb = false;
1467 else if (strcmp(var.value, "enabled") == 0)
1468 spu_config.iUseReverb = true;
1472 var.key = "pcsx_rearmed_spu_interpolation";
1474 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1476 if (strcmp(var.value, "simple") == 0)
1477 spu_config.iUseInterpolation = 1;
1478 else if (strcmp(var.value, "gaussian") == 0)
1479 spu_config.iUseInterpolation = 2;
1480 else if (strcmp(var.value, "cubic") == 0)
1481 spu_config.iUseInterpolation = 3;
1482 else if (strcmp(var.value, "off") == 0)
1483 spu_config.iUseInterpolation = 0;
1487 var.key = "pcsx_rearmed_pe2_fix";
1489 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1491 if (strcmp(var.value, "disabled") == 0)
1493 else if (strcmp(var.value, "enabled") == 0)
1498 var.key = "pcsx_rearmed_inuyasha_fix";
1500 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1502 if (strcmp(var.value, "disabled") == 0)
1504 else if (strcmp(var.value, "enabled") == 0)
1509 // inform core things about possible config changes
1510 plugin_call_rearmed_cbs();
1512 if (GPU_open != NULL && GPU_close != NULL) {
1514 GPU_open(&gpuDisp, "PCSX", NULL);
1522 //bootlogo display hack
1525 var.key = "pcsx_rearmed_show_bios_bootlogo";
1526 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1528 if (strcmp(var.value, "enabled") == 0)
1535 void retro_run(void)
1538 //SysReset must be run while core is running,Not in menu (Locks up Retroarch)
1546 bool updated = false;
1547 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
1548 update_variables(true);
1550 // reset all keystate, query libretro for keystate
1552 for(i = 0; i < PORTS_NUMBER; i++) {
1555 if (in_type[i] == PSE_PAD_TYPE_NONE)
1558 // query libretro for keystate
1559 for (j = 0; j < RETRO_PSX_MAP_LEN; j++)
1560 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j))
1561 in_keystate[i] |= retro_psx_map[j];
1563 if (in_type[i] == PSE_PAD_TYPE_ANALOGPAD)
1565 in_analog_left[i][0] = MIN((input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X) / 255) + 128, 255);
1566 in_analog_left[i][1] = MIN((input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y) / 255) + 128, 255);
1567 in_analog_right[i][0] = MIN((input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X) / 255) + 128, 255);
1568 in_analog_right[i][1] = MIN((input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y) / 255) + 128, 255);
1575 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf_ptr : NULL,
1576 vout_width, vout_height, vout_width * 2);
1580 static bool try_use_bios(const char *path)
1586 f = fopen(path, "rb");
1590 fseek(f, 0, SEEK_END);
1594 if (size != 512 * 1024)
1597 name = strrchr(path, SLASH);
1600 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
1605 #include <sys/types.h>
1608 static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
1614 dir = opendir(dirpath);
1618 while ((ent = readdir(dir))) {
1619 if (strncasecmp(ent->d_name, "scph", 4) != 0)
1622 snprintf(path, path_size, "%s/%s", dirpath, ent->d_name);
1623 ret = try_use_bios(path);
1631 #define find_any_bios(...) false
1634 static void check_system_specs(void)
1637 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
1640 void retro_init(void)
1642 const char *bios[] = { "SCPH101", "SCPH7001", "SCPH5501", "SCPH1001" };
1650 // magic sauce to make the dynarec work on iOS
1651 syscall(SYS_ptrace, 0 /*PTRACE_TRACEME*/, 0, 0, 0);
1655 psxMapHook = pl_3ds_mmap;
1656 psxUnmapHook = pl_3ds_munmap;
1659 if(init_vita_mmap()<0)
1661 psxMapHook = pl_vita_mmap;
1662 psxUnmapHook = pl_vita_munmap;
1664 ret = emu_core_preinit();
1666 /* emu_core_preinit sets the cpu to dynarec */
1668 Config.Cpu = CPU_INTERPRETER;
1670 strcpy(Config.Mcd2, "memcard2.mcd");
1672 ret |= emu_core_init();
1674 SysPrintf("PCSX init failed.\n");
1679 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
1680 #elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && !defined(VITA)
1681 posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
1683 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
1686 vout_buf_ptr = vout_buf;
1688 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
1690 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s", dir);
1692 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
1693 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
1694 found_bios = try_use_bios(path);
1700 found_bios = find_any_bios(dir, path, sizeof(path));
1703 SysPrintf("found BIOS file: %s\n", Config.Bios);
1707 SysPrintf("no BIOS files found.\n");
1708 struct retro_message msg =
1710 "No PlayStation BIOS file found - add for better compatibility",
1713 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, (void*)&msg);
1716 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
1717 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
1718 environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble);
1720 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
1721 * we have to do this because cache misses and some IO penalties
1722 * are not emulated. Warning: changing this may break compatibility. */
1723 cycle_multiplier = 175;
1724 #ifdef HAVE_PRE_ARMV7
1725 cycle_multiplier = 200;
1727 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1728 spu_config.iUseFixedUpdates = 1;
1732 init_memcard(Mcd1Data);
1734 SaveFuncs.open = save_open;
1735 SaveFuncs.read = save_read;
1736 SaveFuncs.write = save_write;
1737 SaveFuncs.seek = save_seek;
1738 SaveFuncs.close = save_close;
1740 update_variables(false);
1741 check_system_specs();
1744 void retro_deinit(void)
1748 linearFree(vout_buf);
1760 #include <psp2/kernel/threadmgr.h>
1761 int usleep (unsigned long us)
1763 sceKernelDelayThread(us);