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>
22 #include "../libpcsxcore/misc.h"
23 #include "../libpcsxcore/psxcounters.h"
24 #include "../libpcsxcore/psxmem_map.h"
25 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
26 #include "../libpcsxcore/cdrom.h"
27 #include "../libpcsxcore/cdriso.h"
28 #include "../libpcsxcore/cheat.h"
29 #include "../libpcsxcore/r3000a.h"
30 #include "../plugins/dfsound/out.h"
31 #include "../plugins/dfsound/spu_config.h"
32 #include "../plugins/dfinput/externals.h"
37 #include "plugin_lib.h"
38 #include "arm_features.h"
42 #include "libretro_core_options.h"
45 #include "3ds/3ds_utils.h"
48 #define PORTS_NUMBER 8
51 #define MIN(a, b) ((a) < (b) ? (a) : (b))
55 #define MAX(a, b) ((a) > (b) ? (a) : (b))
58 #define ISHEXDEC ((buf[cursor] >= '0') && (buf[cursor] <= '9')) || ((buf[cursor] >= 'a') && (buf[cursor] <= 'f')) || ((buf[cursor] >= 'A') && (buf[cursor] <= 'F'))
60 #define INTERNAL_FPS_SAMPLE_PERIOD 64
62 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
63 static int rebootemu = 0;
65 static retro_video_refresh_t video_cb;
66 static retro_input_poll_t input_poll_cb;
67 static retro_input_state_t input_state_cb;
68 static retro_environment_t environ_cb;
69 static retro_audio_sample_batch_t audio_batch_cb;
70 static retro_set_rumble_state_t rumble_cb;
71 static struct retro_log_callback logging;
72 static retro_log_printf_t log_cb;
74 static void *vout_buf;
75 static void *vout_buf_ptr;
76 static int vout_width, vout_height;
77 static int vout_doffs_old, vout_fb_dirty;
78 static bool vout_can_dupe;
79 static bool duping_enable;
80 static bool found_bios;
81 static bool display_internal_fps = false;
82 static unsigned frame_count = 0;
83 static bool libretro_supports_bitmasks = false;
85 static int show_advanced_gpu_peops_settings = -1;
88 static int show_advanced_gpu_unai_settings = -1;
90 static int show_other_input_settings = -1;
92 static unsigned previous_width = 0;
93 static unsigned previous_height = 0;
95 static int plugins_opened;
96 static int is_pal_mode;
98 /* memory card data */
99 extern char Mcd1Data[MCD_SIZE];
100 extern char Mcd2Data[MCD_SIZE];
101 extern char McdDisable[2];
103 /* PCSX ReARMed core calls and stuff */
105 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
106 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
107 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
108 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE
110 int in_analog_left[8][2] = { { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 } };
111 int in_analog_right[8][2] = { { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 } };
112 unsigned short in_keystate[PORTS_NUMBER];
115 int in_enable_vibration = 1;
117 // NegCon adjustment parameters
118 // > The NegCon 'twist' action is somewhat awkward when mapped
119 // to a standard analog stick -> user should be able to tweak
120 // response/deadzone for comfort
121 // > When response is linear, 'additional' deadzone (set here)
122 // may be left at zero, since this is normally handled via in-game
124 // > When response is non-linear, deadzone should be set to match the
125 // controller being used (otherwise precision may be lost)
126 // > negcon_linearity:
127 // - 1: Response is linear - recommended when using racing wheel
128 // peripherals, not recommended for standard gamepads
129 // - 2: Response is quadratic - optimal setting for gamepads
130 // - 3: Response is cubic - enables precise fine control, but
131 // difficult to use...
132 #define NEGCON_RANGE 0x7FFF
133 static int negcon_deadzone = 0;
134 static int negcon_linearity = 1;
136 static bool axis_bounds_modifier;
138 /* PSX max resolution is 640x512, but with enhancement it's 1024x512 */
139 #define VOUT_MAX_WIDTH 1024
140 #define VOUT_MAX_HEIGHT 512
143 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info) { return false; }
144 void retro_unload_game(void) {}
145 static int vout_open(void) { return 0; }
146 static void vout_close(void) {}
147 static int snd_init(void) { return 0; }
148 static void snd_finish(void) {}
149 static int snd_busy(void) { return 0; }
151 #define GPU_PEOPS_ODD_EVEN_BIT (1 << 0)
152 #define GPU_PEOPS_EXPAND_SCREEN_WIDTH (1 << 1)
153 #define GPU_PEOPS_IGNORE_BRIGHTNESS (1 << 2)
154 #define GPU_PEOPS_DISABLE_COORD_CHECK (1 << 3)
155 #define GPU_PEOPS_LAZY_SCREEN_UPDATE (1 << 6)
156 #define GPU_PEOPS_OLD_FRAME_SKIP (1 << 7)
157 #define GPU_PEOPS_REPEATED_TRIANGLES (1 << 8)
158 #define GPU_PEOPS_QUADS_WITH_TRIANGLES (1 << 9)
159 #define GPU_PEOPS_FAKE_BUSY_STATE (1 << 10)
161 static void init_memcard(char *mcd_data)
166 memset(mcd_data, 0, MCD_SIZE);
168 mcd_data[off++] = 'M';
169 mcd_data[off++] = 'C';
171 mcd_data[off++] = 0x0e;
173 for (i = 0; i < 15; i++)
175 mcd_data[off++] = 0xa0;
177 mcd_data[off++] = 0xff;
178 mcd_data[off++] = 0xff;
180 mcd_data[off++] = 0xa0;
183 for (i = 0; i < 20; i++)
185 mcd_data[off++] = 0xff;
186 mcd_data[off++] = 0xff;
187 mcd_data[off++] = 0xff;
188 mcd_data[off++] = 0xff;
190 mcd_data[off++] = 0xff;
191 mcd_data[off++] = 0xff;
196 static void set_vout_fb()
198 struct retro_framebuffer fb = { 0 };
200 fb.width = vout_width;
201 fb.height = vout_height;
202 fb.access_flags = RETRO_MEMORY_ACCESS_WRITE;
204 if (environ_cb(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &fb) && fb.format == RETRO_PIXEL_FORMAT_RGB565)
205 vout_buf_ptr = (uint16_t *)fb.data;
207 vout_buf_ptr = vout_buf;
210 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
215 if (previous_width != vout_width || previous_height != vout_height)
217 previous_width = vout_width;
218 previous_height = vout_height;
220 struct retro_system_av_info info;
221 retro_get_system_av_info(&info);
222 environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &info.geometry);
228 #ifndef FRONTEND_SUPPORTS_RGB565
229 static void convert(void *buf, size_t bytes)
231 unsigned int i, v, *p = buf;
233 for (i = 0; i < bytes / 4; i++)
236 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
241 static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
243 unsigned short *dest = vout_buf_ptr;
244 const unsigned short *src = vram;
245 int dstride = vout_width, h1 = h;
251 memset(vout_buf_ptr, 0, dstride * h * 2);
255 doffs = (vout_height - h) * dstride;
256 doffs += (dstride - w) / 2 & ~1;
257 if (doffs != vout_doffs_old)
260 memset(vout_buf_ptr, 0, dstride * h * 2);
261 vout_doffs_old = doffs;
267 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
268 for (; h1-- > 0; dest += dstride, src += stride)
270 bgr888_to_rgb565(dest, src, w * 3);
275 for (; h1-- > 0; dest += dstride, src += stride)
277 bgr555_to_rgb565(dest, src, w * 2);
282 #ifndef FRONTEND_SUPPORTS_RGB565
283 convert(vout_buf_ptr, vout_width * vout_height * 2);
286 pl_rearmed_cbs.flip_cnt++;
298 psx_map_t custom_psx_maps[] = {
299 { NULL, 0x13000000, 0x210000, MAP_TAG_RAM }, // 0x80000000
300 { NULL, 0x12800000, 0x010000, MAP_TAG_OTHER }, // 0x1f800000
301 { NULL, 0x12c00000, 0x080000, MAP_TAG_OTHER }, // 0x1fc00000
302 { NULL, 0x11000000, 0x800000, MAP_TAG_LUTS }, // 0x08000000
303 { NULL, 0x12000000, 0x200000, MAP_TAG_VRAM }, // 0x00000000
306 void *pl_3ds_mmap(unsigned long addr, size_t size, int is_fixed,
314 psx_map_t *custom_map = custom_psx_maps;
316 for (; custom_map->size; custom_map++)
318 if ((custom_map->size == size) && (custom_map->tag == tag))
320 uint32_t ptr_aligned, tmp;
322 custom_map->buffer = malloc(size + 0x1000);
323 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
325 if (svcControlMemory(&tmp, (void *)custom_map->target_map, (void *)ptr_aligned, size, MEMOP_MAP, 0x3) < 0)
327 SysPrintf("could not map memory @0x%08X\n", custom_map->target_map);
331 return (void *)custom_map->target_map;
339 void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
345 psx_map_t *custom_map = custom_psx_maps;
347 for (; custom_map->size; custom_map++)
349 if ((custom_map->target_map == (uint32_t)ptr))
351 uint32_t ptr_aligned, tmp;
353 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
355 svcControlMemory(&tmp, (void *)custom_map->target_map, (void *)ptr_aligned, size, MEMOP_UNMAP, 0x3);
357 free(custom_map->buffer);
358 custom_map->buffer = NULL;
379 psx_map_t custom_psx_maps[] = {
380 { NULL, NULL, 0x210000, MAP_TAG_RAM }, // 0x80000000
381 { NULL, NULL, 0x010000, MAP_TAG_OTHER }, // 0x1f800000
382 { NULL, NULL, 0x080000, MAP_TAG_OTHER }, // 0x1fc00000
383 { NULL, NULL, 0x800000, MAP_TAG_LUTS }, // 0x08000000
384 { NULL, NULL, 0x200000, MAP_TAG_VRAM }, // 0x00000000
391 addr = malloc(64 * 1024 * 1024);
394 tmpaddr = ((u32)(addr + 0xFFFFFF)) & ~0xFFFFFF;
395 custom_psx_maps[0].buffer = tmpaddr + 0x2000000;
396 custom_psx_maps[1].buffer = tmpaddr + 0x1800000;
397 custom_psx_maps[2].buffer = tmpaddr + 0x1c00000;
398 custom_psx_maps[3].buffer = tmpaddr + 0x0000000;
399 custom_psx_maps[4].buffer = tmpaddr + 0x1000000;
401 for(n = 0; n < 5; n++){
402 sceClibPrintf("addr reserved %x\n",custom_psx_maps[n].buffer);
408 void deinit_vita_mmap()
413 void *pl_vita_mmap(unsigned long addr, size_t size, int is_fixed,
419 psx_map_t *custom_map = custom_psx_maps;
421 for (; custom_map->size; custom_map++)
423 if ((custom_map->size == size) && (custom_map->tag == tag))
425 return custom_map->buffer;
432 void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag)
436 psx_map_t *custom_map = custom_psx_maps;
438 for (; custom_map->size; custom_map++)
440 if ((custom_map->buffer == ptr))
450 static void *pl_mmap(unsigned int size)
452 return psxMap(0, size, 0, MAP_TAG_VRAM);
455 static void pl_munmap(void *ptr, unsigned int size)
457 psxUnmap(ptr, size, MAP_TAG_VRAM);
460 struct rearmed_cbs pl_rearmed_cbs = {
461 .pl_vout_open = vout_open,
462 .pl_vout_set_mode = vout_set_mode,
463 .pl_vout_flip = vout_flip,
464 .pl_vout_close = vout_close,
467 /* from psxcounters */
468 .gpu_hcnt = &hSyncCount,
469 .gpu_frame_count = &frame_counter,
472 void pl_frame_limit(void)
474 /* called once per frame, make psxCpu->Execute() above return */
478 void pl_timing_prepare(int is_pal)
480 is_pal_mode = is_pal;
483 void plat_trigger_vibrate(int pad, int low, int high)
488 if (in_enable_vibration)
490 rumble_cb(pad, RETRO_RUMBLE_STRONG, high << 8);
491 rumble_cb(pad, RETRO_RUMBLE_WEAK, low ? 0xffff : 0x0);
495 void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
500 static void snd_feed(void *buf, int bytes)
502 if (audio_batch_cb != NULL)
503 audio_batch_cb(buf, bytes / 4);
506 void out_register_libretro(struct out_driver *drv)
508 drv->name = "libretro";
509 drv->init = snd_init;
510 drv->finish = snd_finish;
511 drv->busy = snd_busy;
512 drv->feed = snd_feed;
516 void retro_set_environment(retro_environment_t cb)
520 if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
521 log_cb = logging.log;
523 libretro_set_core_options(environ_cb);
526 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
527 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
528 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
529 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
530 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
532 unsigned retro_api_version(void)
534 return RETRO_API_VERSION;
537 static int controller_port_variable(unsigned port, struct retro_variable *var)
539 if (port >= PORTS_NUMBER)
549 var->key = "pcsx_rearmed_pad1type";
552 var->key = "pcsx_rearmed_pad2type";
555 var->key = "pcsx_rearmed_pad3type";
558 var->key = "pcsx_rearmed_pad4type";
561 var->key = "pcsx_rearmed_pad5type";
564 var->key = "pcsx_rearmed_pad6type";
567 var->key = "pcsx_rearmed_pad7type";
570 var->key = "pcsx_rearmed_pad8type";
574 return environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, var) && var->value;
577 static void update_controller_port_variable(unsigned port)
579 if (port >= PORTS_NUMBER)
582 struct retro_variable var;
584 if (controller_port_variable(port, &var))
586 if (strcmp(var.value, "standard") == 0)
587 in_type[port] = PSE_PAD_TYPE_STANDARD;
588 else if (strcmp(var.value, "analog") == 0)
589 in_type[port] = PSE_PAD_TYPE_ANALOGJOY;
590 else if (strcmp(var.value, "dualshock") == 0)
591 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
592 else if (strcmp(var.value, "negcon") == 0)
593 in_type[port] = PSE_PAD_TYPE_NEGCON;
594 else if (strcmp(var.value, "guncon") == 0)
595 in_type[port] = PSE_PAD_TYPE_GUNCON;
596 else if (strcmp(var.value, "none") == 0)
597 in_type[port] = PSE_PAD_TYPE_NONE;
598 // else 'default' case, do nothing
602 static void update_controller_port_device(unsigned port, unsigned device)
604 if (port >= PORTS_NUMBER)
607 struct retro_variable var;
609 if (!controller_port_variable(port, &var))
612 if (strcmp(var.value, "default") != 0)
617 case RETRO_DEVICE_JOYPAD:
618 in_type[port] = PSE_PAD_TYPE_STANDARD;
620 case RETRO_DEVICE_ANALOG:
621 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
623 case RETRO_DEVICE_MOUSE:
624 in_type[port] = PSE_PAD_TYPE_MOUSE;
626 case RETRO_DEVICE_LIGHTGUN:
627 in_type[port] = PSE_PAD_TYPE_GUN;
629 case RETRO_DEVICE_NONE:
631 in_type[port] = PSE_PAD_TYPE_NONE;
635 static void update_multitap()
637 struct retro_variable var;
641 var.key = "pcsx_rearmed_multitap1";
643 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value))
645 if (strcmp(var.value, "enabled") == 0)
647 else if (strcmp(var.value, "disabled") == 0)
657 // If a gamepad is plugged after port 2, we need a first multitap.
659 for (port = 2; port < PORTS_NUMBER; port++)
660 multitap1 |= in_type[port] != PSE_PAD_TYPE_NONE;
664 var.key = "pcsx_rearmed_multitap2";
666 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value))
668 if (strcmp(var.value, "enabled") == 0)
670 else if (strcmp(var.value, "disabled") == 0)
680 // If a gamepad is plugged after port 4, we need a second multitap.
682 for (port = 4; port < PORTS_NUMBER; port++)
683 multitap2 |= in_type[port] != PSE_PAD_TYPE_NONE;
687 void retro_set_controller_port_device(unsigned port, unsigned device)
689 SysPrintf("port %u device %u", port, device);
691 if (port >= PORTS_NUMBER)
694 update_controller_port_device(port, device);
698 void retro_get_system_info(struct retro_system_info *info)
701 #define GIT_VERSION ""
703 memset(info, 0, sizeof(*info));
704 info->library_name = "PCSX-ReARMed";
705 info->library_version = "r22" GIT_VERSION;
706 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u|chd";
707 info->need_fullpath = true;
710 void retro_get_system_av_info(struct retro_system_av_info *info)
712 unsigned geom_height = vout_height > 0 ? vout_height : 240;
713 unsigned geom_width = vout_width > 0 ? vout_width : 320;
715 memset(info, 0, sizeof(*info));
716 info->timing.fps = is_pal_mode ? 50.0 : 60.0;
717 info->timing.sample_rate = 44100.0;
718 info->geometry.base_width = geom_width;
719 info->geometry.base_height = geom_height;
720 info->geometry.max_width = VOUT_MAX_WIDTH;
721 info->geometry.max_height = VOUT_MAX_HEIGHT;
722 info->geometry.aspect_ratio = 4.0 / 3.0;
726 size_t retro_serialize_size(void)
728 // it's currently 4380651-4397047 bytes,
729 // but have some reserved for future
740 static void *save_open(const char *name, const char *mode)
744 if (name == NULL || mode == NULL)
747 fp = malloc(sizeof(*fp));
751 fp->buf = (char *)name;
753 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
758 static int save_read(void *file, void *buf, u32 len)
760 struct save_fp *fp = file;
761 if (fp == NULL || buf == NULL)
764 memcpy(buf, fp->buf + fp->pos, len);
769 static int save_write(void *file, const void *buf, u32 len)
771 struct save_fp *fp = file;
772 if (fp == NULL || buf == NULL)
775 memcpy(fp->buf + fp->pos, buf, len);
780 static long save_seek(void *file, long offs, int whence)
782 struct save_fp *fp = file;
799 static void save_close(void *file)
801 struct save_fp *fp = file;
802 size_t r_size = retro_serialize_size();
806 if (fp->pos > r_size)
807 SysPrintf("ERROR: save buffer overflow detected\n");
808 else if (fp->is_write && fp->pos < r_size)
809 // make sure we don't save trash in leftover space
810 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
814 bool retro_serialize(void *data, size_t size)
816 int ret = SaveState(data);
817 return ret == 0 ? true : false;
820 bool retro_unserialize(const void *data, size_t size)
822 int ret = LoadState(data);
823 return ret == 0 ? true : false;
827 void retro_cheat_reset(void)
832 void retro_cheat_set(unsigned index, bool enabled, const char *code)
837 // cheat funcs are destructive, need a copy..
838 strncpy(buf, code, sizeof(buf));
839 buf[sizeof(buf) - 1] = 0;
841 //Prepare buffered cheat for PCSX's AddCheat fucntion.
860 if (index < NumCheats)
861 ret = EditCheat(index, "", buf);
863 ret = AddCheat("", buf);
866 SysPrintf("Failed to set cheat %#u\n", index);
867 else if (index < NumCheats)
868 Cheats[index].Enabled = enabled;
871 // just in case, maybe a win-rt port in the future?
879 #define PATH_MAX 4096
882 /* multidisk support */
883 static unsigned int disk_initial_index;
884 static char disk_initial_path[PATH_MAX];
885 static bool disk_ejected;
886 static unsigned int disk_current_index;
887 static unsigned int disk_count;
888 static struct disks_state
892 int internal_index; // for multidisk eboots
895 static void get_disk_label(char *disk_label, const char *disk_path, size_t len)
897 const char *base = NULL;
899 if (!disk_path || (*disk_path == '\0'))
902 base = strrchr(disk_path, SLASH);
909 strncpy(disk_label, base, len - 1);
910 disk_label[len - 1] = '\0';
912 char *ext = strrchr(disk_label, '.');
917 static void disk_init(void)
921 disk_ejected = false;
922 disk_current_index = 0;
925 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++)
927 if (disks[i].fname != NULL)
929 free(disks[i].fname);
930 disks[i].fname = NULL;
932 if (disks[i].flabel != NULL)
934 free(disks[i].flabel);
935 disks[i].flabel = NULL;
937 disks[i].internal_index = 0;
941 static bool disk_set_eject_state(bool ejected)
944 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
947 disk_ejected = ejected;
951 static bool disk_get_eject_state(void)
953 /* can't be controlled by emulated software */
957 static unsigned int disk_get_image_index(void)
959 return disk_current_index;
962 static bool disk_set_image_index(unsigned int index)
964 if (index >= sizeof(disks) / sizeof(disks[0]))
968 CdromLabel[0] = '\0';
970 if (disks[index].fname == NULL)
972 SysPrintf("missing disk #%u\n", index);
975 // RetroArch specifies "no disk" with index == count,
976 // so don't fail here..
977 disk_current_index = index;
981 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
982 disks[index].fname, disks[index].internal_index);
984 cdrIsoMultidiskSelect = disks[index].internal_index;
985 set_cd_image(disks[index].fname);
986 if (ReloadCdromPlugin() < 0)
988 SysPrintf("failed to load cdr plugin\n");
993 SysPrintf("failed to open cdr plugin\n");
999 SetCdOpenCaseTime(time(NULL) + 2);
1003 disk_current_index = index;
1007 static unsigned int disk_get_num_images(void)
1012 static bool disk_replace_image_index(unsigned index,
1013 const struct retro_game_info *info)
1015 char *old_fname = NULL;
1016 char *old_flabel = NULL;
1019 if (index >= sizeof(disks) / sizeof(disks[0]))
1022 old_fname = disks[index].fname;
1023 old_flabel = disks[index].flabel;
1025 disks[index].fname = NULL;
1026 disks[index].flabel = NULL;
1027 disks[index].internal_index = 0;
1031 char disk_label[PATH_MAX];
1032 disk_label[0] = '\0';
1034 disks[index].fname = strdup(info->path);
1036 get_disk_label(disk_label, info->path, PATH_MAX);
1037 disks[index].flabel = strdup(disk_label);
1039 if (index == disk_current_index)
1040 ret = disk_set_image_index(index);
1043 if (old_fname != NULL)
1046 if (old_flabel != NULL)
1052 static bool disk_add_image_index(void)
1054 if (disk_count >= 8)
1061 static bool disk_set_initial_image(unsigned index, const char *path)
1063 if (index >= sizeof(disks) / sizeof(disks[0]))
1066 if (!path || (*path == '\0'))
1069 disk_initial_index = index;
1071 strncpy(disk_initial_path, path, sizeof(disk_initial_path) - 1);
1072 disk_initial_path[sizeof(disk_initial_path) - 1] = '\0';
1077 static bool disk_get_image_path(unsigned index, char *path, size_t len)
1079 const char *fname = NULL;
1084 if (index >= sizeof(disks) / sizeof(disks[0]))
1087 fname = disks[index].fname;
1089 if (!fname || (*fname == '\0'))
1092 strncpy(path, fname, len - 1);
1093 path[len - 1] = '\0';
1098 static bool disk_get_image_label(unsigned index, char *label, size_t len)
1100 const char *flabel = NULL;
1105 if (index >= sizeof(disks) / sizeof(disks[0]))
1108 flabel = disks[index].flabel;
1110 if (!flabel || (*flabel == '\0'))
1113 strncpy(label, flabel, len - 1);
1114 label[len - 1] = '\0';
1119 static struct retro_disk_control_callback disk_control = {
1120 .set_eject_state = disk_set_eject_state,
1121 .get_eject_state = disk_get_eject_state,
1122 .get_image_index = disk_get_image_index,
1123 .set_image_index = disk_set_image_index,
1124 .get_num_images = disk_get_num_images,
1125 .replace_image_index = disk_replace_image_index,
1126 .add_image_index = disk_add_image_index,
1129 static struct retro_disk_control_ext_callback disk_control_ext = {
1130 .set_eject_state = disk_set_eject_state,
1131 .get_eject_state = disk_get_eject_state,
1132 .get_image_index = disk_get_image_index,
1133 .set_image_index = disk_set_image_index,
1134 .get_num_images = disk_get_num_images,
1135 .replace_image_index = disk_replace_image_index,
1136 .add_image_index = disk_add_image_index,
1137 .set_initial_image = disk_set_initial_image,
1138 .get_image_path = disk_get_image_path,
1139 .get_image_label = disk_get_image_label,
1142 static char base_dir[1024];
1144 static bool read_m3u(const char *file)
1147 char name[PATH_MAX];
1148 FILE *f = fopen(file, "r");
1152 while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0]))
1156 char *carrige_return = strchr(line, '\r');
1158 *carrige_return = '\0';
1159 char *newline = strchr(line, '\n');
1163 if (line[0] != '\0')
1165 char disk_label[PATH_MAX];
1166 disk_label[0] = '\0';
1168 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
1169 disks[disk_count].fname = strdup(name);
1171 get_disk_label(disk_label, name, PATH_MAX);
1172 disks[disk_count].flabel = strdup(disk_label);
1179 return (disk_count != 0);
1182 static void extract_directory(char *buf, const char *path, size_t size)
1185 strncpy(buf, path, size - 1);
1186 buf[size - 1] = '\0';
1188 base = strrchr(buf, '/');
1190 base = strrchr(buf, '\\');
1201 #if defined(__QNX__) || defined(_WIN32)
1202 /* Blackberry QNX doesn't have strcasestr */
1205 * Find the first occurrence of find in s, ignore case.
1208 strcasestr(const char *s, const char *find)
1213 if ((c = *find++) != 0)
1215 c = tolower((unsigned char)c);
1221 if ((sc = *s++) == 0)
1223 } while ((char)tolower((unsigned char)sc) != c);
1224 } while (strncasecmp(s, find, len) != 0);
1231 static void set_retro_memmap(void)
1234 struct retro_memory_map retromap = { 0 };
1235 struct retro_memory_descriptor mmap = {
1236 0, psxM, 0, 0, 0, 0, 0x200000
1239 retromap.descriptors = &mmap;
1240 retromap.num_descriptors = 1;
1242 environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &retromap);
1246 static void update_variables(bool in_flight);
1247 bool retro_load_game(const struct retro_game_info *info)
1250 unsigned int cd_index = 0;
1251 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
1253 struct retro_input_descriptor desc[] = {
1254 #define JOYP(port) \
1255 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" }, \
1256 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" }, \
1257 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" }, \
1258 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" }, \
1259 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" }, \
1260 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" }, \
1261 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" }, \
1262 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" }, \
1263 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" }, \
1264 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" }, \
1265 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" }, \
1266 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" }, \
1267 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" }, \
1268 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" }, \
1269 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" }, \
1270 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, \
1271 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" }, \
1272 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" }, \
1273 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" }, \
1274 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1290 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1292 #ifdef FRONTEND_SUPPORTS_RGB565
1293 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
1294 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
1296 SysPrintf("RGB565 supported, using it\n");
1300 if (info == NULL || info->path == NULL)
1302 SysPrintf("info->path required\n");
1306 update_variables(false);
1316 extract_directory(base_dir, info->path, sizeof(base_dir));
1320 if (!read_m3u(info->path))
1322 log_cb(RETRO_LOG_INFO, "failed to read m3u file\n");
1328 char disk_label[PATH_MAX];
1329 disk_label[0] = '\0';
1332 disks[0].fname = strdup(info->path);
1334 get_disk_label(disk_label, info->path, PATH_MAX);
1335 disks[0].flabel = strdup(disk_label);
1338 /* If this is an M3U file, attempt to set the
1339 * initial disk image */
1340 if (is_m3u && (disk_initial_index > 0) && (disk_initial_index < disk_count))
1342 const char *fname = disks[disk_initial_index].fname;
1344 if (fname && (*fname != '\0'))
1345 if (strcmp(disk_initial_path, fname) == 0)
1346 cd_index = disk_initial_index;
1349 set_cd_image(disks[cd_index].fname);
1350 disk_current_index = cd_index;
1352 /* have to reload after set_cd_image for correct cdr plugin */
1353 if (LoadPlugins() == -1)
1355 log_cb(RETRO_LOG_INFO, "failed to load plugins\n");
1362 if (OpenPlugins() == -1)
1364 log_cb(RETRO_LOG_INFO, "failed to open plugins\n");
1368 /* Handle multi-disk images (i.e. PBP)
1369 * > Cannot do this until after OpenPlugins() is
1370 * called (since this sets the value of
1371 * cdrIsoMultidiskCount) */
1372 if (!is_m3u && (cdrIsoMultidiskCount > 1))
1374 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
1376 /* Small annoyance: We need to change the label
1377 * of disk 0, so have to clear existing entries */
1378 if (disks[0].fname != NULL)
1379 free(disks[0].fname);
1380 disks[0].fname = NULL;
1382 if (disks[0].flabel != NULL)
1383 free(disks[0].flabel);
1384 disks[0].flabel = NULL;
1386 for (i = 0; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++)
1388 char disk_name[PATH_MAX];
1389 char disk_label[PATH_MAX];
1390 disk_name[0] = '\0';
1391 disk_label[0] = '\0';
1393 disks[i].fname = strdup(info->path);
1395 get_disk_label(disk_name, info->path, PATH_MAX);
1396 snprintf(disk_label, sizeof(disk_label), "%s #%u", disk_name, (unsigned)i + 1);
1397 disks[i].flabel = strdup(disk_label);
1399 disks[i].internal_index = i;
1402 /* This is not an M3U file, so initial disk
1403 * image has not yet been set - attempt to
1405 if ((disk_initial_index > 0) && (disk_initial_index < disk_count))
1407 const char *fname = disks[disk_initial_index].fname;
1409 if (fname && (*fname != '\0'))
1410 if (strcmp(disk_initial_path, fname) == 0)
1411 cd_index = disk_initial_index;
1417 CdromLabel[0] = '\0';
1419 cdrIsoMultidiskSelect = disks[cd_index].internal_index;
1420 disk_current_index = cd_index;
1421 set_cd_image(disks[cd_index].fname);
1423 if (ReloadCdromPlugin() < 0)
1425 log_cb(RETRO_LOG_INFO, "failed to reload cdr plugins\n");
1430 log_cb(RETRO_LOG_INFO, "failed to open cdr plugin\n");
1436 plugin_call_rearmed_cbs();
1439 if (CheckCdrom() == -1)
1441 log_cb(RETRO_LOG_INFO, "unsupported/invalid CD image: %s\n", info->path);
1447 if (LoadCdrom() == -1)
1449 log_cb(RETRO_LOG_INFO, "could not load CD\n");
1459 unsigned retro_get_region(void)
1461 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1464 void *retro_get_memory_data(unsigned id)
1466 if (id == RETRO_MEMORY_SAVE_RAM)
1468 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1474 size_t retro_get_memory_size(unsigned id)
1476 if (id == RETRO_MEMORY_SAVE_RAM)
1478 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1484 void retro_reset(void)
1486 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
1491 static const unsigned short retro_psx_map[] = {
1492 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
1493 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
1494 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
1495 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
1496 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
1497 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
1498 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
1499 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
1500 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
1501 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
1502 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
1503 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
1504 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
1505 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
1506 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
1507 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
1509 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
1511 //Percentage distance of screen to adjust
1512 static int GunconAdjustX = 0;
1513 static int GunconAdjustY = 0;
1515 //Used when out by a percentage
1516 static float GunconAdjustRatioX = 1;
1517 static float GunconAdjustRatioY = 1;
1519 static void update_variables(bool in_flight)
1521 struct retro_variable var;
1524 int gpu_peops_fix = 0;
1528 var.key = "pcsx_rearmed_frameskip";
1529 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1530 pl_rearmed_cbs.frameskip = atoi(var.value);
1533 var.key = "pcsx_rearmed_region";
1534 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1537 if (strcmp(var.value, "auto") == 0)
1539 else if (strcmp(var.value, "NTSC") == 0)
1541 else if (strcmp(var.value, "PAL") == 0)
1545 for (i = 0; i < PORTS_NUMBER; i++)
1546 update_controller_port_variable(i);
1551 var.key = "pcsx_rearmed_negcon_deadzone";
1552 negcon_deadzone = 0;
1553 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1555 negcon_deadzone = (int)(atoi(var.value) * 0.01f * NEGCON_RANGE);
1559 var.key = "pcsx_rearmed_negcon_response";
1560 negcon_linearity = 1;
1561 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1563 if (strcmp(var.value, "quadratic") == 0)
1565 negcon_linearity = 2;
1567 else if (strcmp(var.value, "cubic") == 0)
1569 negcon_linearity = 3;
1574 var.key = "pcsx_rearmed_analog_axis_modifier";
1575 axis_bounds_modifier = true;
1576 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1578 if (strcmp(var.value, "square") == 0)
1580 axis_bounds_modifier = true;
1582 else if (strcmp(var.value, "circle") == 0)
1584 axis_bounds_modifier = false;
1589 var.key = "pcsx_rearmed_vibration";
1591 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1593 if (strcmp(var.value, "disabled") == 0)
1594 in_enable_vibration = 0;
1595 else if (strcmp(var.value, "enabled") == 0)
1596 in_enable_vibration = 1;
1600 var.key = "pcsx_rearmed_dithering";
1602 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1604 if (strcmp(var.value, "disabled") == 0)
1606 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
1607 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 0;
1608 pl_rearmed_cbs.gpu_unai.dithering = 0;
1610 pl_rearmed_cbs.gpu_neon.allow_dithering = 0;
1613 else if (strcmp(var.value, "enabled") == 0)
1615 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1616 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 1;
1617 pl_rearmed_cbs.gpu_unai.dithering = 1;
1619 pl_rearmed_cbs.gpu_neon.allow_dithering = 1;
1626 var.key = "pcsx_rearmed_neon_interlace_enable";
1628 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1630 if (strcmp(var.value, "disabled") == 0)
1631 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
1632 else if (strcmp(var.value, "enabled") == 0)
1633 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
1637 var.key = "pcsx_rearmed_neon_enhancement_enable";
1639 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1641 if (strcmp(var.value, "disabled") == 0)
1642 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
1643 else if (strcmp(var.value, "enabled") == 0)
1644 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
1648 var.key = "pcsx_rearmed_neon_enhancement_no_main";
1650 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1652 if (strcmp(var.value, "disabled") == 0)
1653 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
1654 else if (strcmp(var.value, "enabled") == 0)
1655 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
1660 var.key = "pcsx_rearmed_duping_enable";
1662 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1664 if (strcmp(var.value, "disabled") == 0)
1665 duping_enable = false;
1666 else if (strcmp(var.value, "enabled") == 0)
1667 duping_enable = true;
1671 var.key = "pcsx_rearmed_display_internal_fps";
1673 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1675 if (strcmp(var.value, "disabled") == 0)
1676 display_internal_fps = false;
1677 else if (strcmp(var.value, "enabled") == 0)
1678 display_internal_fps = true;
1681 #if defined(LIGHTREC) || defined(NEW_DYNAREC)
1683 var.key = "pcsx_rearmed_drc";
1685 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1687 R3000Acpu *prev_cpu = psxCpu;
1688 #if defined(LIGHTREC)
1689 bool can_use_dynarec = found_bios;
1691 bool can_use_dynarec = 1;
1696 Config.Cpu = CPU_INTERPRETER;
1699 if (strcmp(var.value, "disabled") == 0 || !can_use_dynarec)
1700 Config.Cpu = CPU_INTERPRETER;
1701 else if (strcmp(var.value, "enabled") == 0)
1702 Config.Cpu = CPU_DYNAREC;
1704 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
1705 if (psxCpu != prev_cpu)
1707 prev_cpu->Shutdown();
1709 psxCpu->Reset(); // not really a reset..
1712 #endif /* LIGHTREC || NEW_DYNAREC */
1715 var.key = "pcsx_rearmed_spu_reverb";
1717 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1719 if (strcmp(var.value, "disabled") == 0)
1720 spu_config.iUseReverb = false;
1721 else if (strcmp(var.value, "enabled") == 0)
1722 spu_config.iUseReverb = true;
1726 var.key = "pcsx_rearmed_spu_interpolation";
1728 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1730 if (strcmp(var.value, "simple") == 0)
1731 spu_config.iUseInterpolation = 1;
1732 else if (strcmp(var.value, "gaussian") == 0)
1733 spu_config.iUseInterpolation = 2;
1734 else if (strcmp(var.value, "cubic") == 0)
1735 spu_config.iUseInterpolation = 3;
1736 else if (strcmp(var.value, "off") == 0)
1737 spu_config.iUseInterpolation = 0;
1741 var.key = "pcsx_rearmed_pe2_fix";
1743 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1745 if (strcmp(var.value, "disabled") == 0)
1747 else if (strcmp(var.value, "enabled") == 0)
1752 var.key = "pcsx_rearmed_idiablofix";
1754 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1756 if (strcmp(var.value, "disabled") == 0)
1757 spu_config.idiablofix = 0;
1758 else if (strcmp(var.value, "enabled") == 0)
1759 spu_config.idiablofix = 1;
1763 var.key = "pcsx_rearmed_inuyasha_fix";
1765 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1767 if (strcmp(var.value, "disabled") == 0)
1769 else if (strcmp(var.value, "enabled") == 0)
1775 var.key = "pcsx_rearmed_async_cd";
1776 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1778 if (strcmp(var.value, "async") == 0)
1786 var.key = "pcsx_rearmed_noxadecoding";
1787 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1789 if (strcmp(var.value, "disabled") == 0)
1796 var.key = "pcsx_rearmed_nocdaudio";
1797 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1799 if (strcmp(var.value, "disabled") == 0)
1806 var.key = "pcsx_rearmed_spuirq";
1807 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1809 if (strcmp(var.value, "disabled") == 0)
1817 var.key = "pcsx_rearmed_gpu_peops_odd_even_bit";
1819 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1821 if (strcmp(var.value, "enabled") == 0)
1822 gpu_peops_fix |= GPU_PEOPS_ODD_EVEN_BIT;
1826 var.key = "pcsx_rearmed_gpu_peops_expand_screen_width";
1828 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1830 if (strcmp(var.value, "enabled") == 0)
1831 gpu_peops_fix |= GPU_PEOPS_EXPAND_SCREEN_WIDTH;
1835 var.key = "pcsx_rearmed_gpu_peops_ignore_brightness";
1837 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1839 if (strcmp(var.value, "enabled") == 0)
1840 gpu_peops_fix |= GPU_PEOPS_IGNORE_BRIGHTNESS;
1844 var.key = "pcsx_rearmed_gpu_peops_disable_coord_check";
1846 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1848 if (strcmp(var.value, "enabled") == 0)
1849 gpu_peops_fix |= GPU_PEOPS_DISABLE_COORD_CHECK;
1853 var.key = "pcsx_rearmed_gpu_peops_lazy_screen_update";
1855 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1857 if (strcmp(var.value, "enabled") == 0)
1858 gpu_peops_fix |= GPU_PEOPS_LAZY_SCREEN_UPDATE;
1862 var.key = "pcsx_rearmed_gpu_peops_old_frame_skip";
1864 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1866 if (strcmp(var.value, "enabled") == 0)
1867 gpu_peops_fix |= GPU_PEOPS_OLD_FRAME_SKIP;
1871 var.key = "pcsx_rearmed_gpu_peops_repeated_triangles";
1873 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1875 if (strcmp(var.value, "enabled") == 0)
1876 gpu_peops_fix |= GPU_PEOPS_REPEATED_TRIANGLES;
1880 var.key = "pcsx_rearmed_gpu_peops_quads_with_triangles";
1882 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1884 if (strcmp(var.value, "enabled") == 0)
1885 gpu_peops_fix |= GPU_PEOPS_QUADS_WITH_TRIANGLES;
1889 var.key = "pcsx_rearmed_gpu_peops_fake_busy_state";
1891 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1893 if (strcmp(var.value, "enabled") == 0)
1894 gpu_peops_fix |= GPU_PEOPS_FAKE_BUSY_STATE;
1897 if (pl_rearmed_cbs.gpu_peops.dwActFixes != gpu_peops_fix)
1898 pl_rearmed_cbs.gpu_peops.dwActFixes = gpu_peops_fix;
1900 /* Show/hide core options */
1902 var.key = "pcsx_rearmed_show_gpu_peops_settings";
1905 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1907 int show_advanced_gpu_peops_settings_prev = show_advanced_gpu_peops_settings;
1909 show_advanced_gpu_peops_settings = 1;
1910 if (strcmp(var.value, "disabled") == 0)
1911 show_advanced_gpu_peops_settings = 0;
1913 if (show_advanced_gpu_peops_settings != show_advanced_gpu_peops_settings_prev)
1916 struct retro_core_option_display option_display;
1917 char gpu_peops_option[9][45] = {
1918 "pcsx_rearmed_gpu_peops_odd_even_bit",
1919 "pcsx_rearmed_gpu_peops_expand_screen_width",
1920 "pcsx_rearmed_gpu_peops_ignore_brightness",
1921 "pcsx_rearmed_gpu_peops_disable_coord_check",
1922 "pcsx_rearmed_gpu_peops_lazy_screen_update",
1923 "pcsx_rearmed_gpu_peops_old_frame_skip",
1924 "pcsx_rearmed_gpu_peops_repeated_triangles",
1925 "pcsx_rearmed_gpu_peops_quads_with_triangles",
1926 "pcsx_rearmed_gpu_peops_fake_busy_state"
1929 option_display.visible = show_advanced_gpu_peops_settings;
1931 for (i = 0; i < 9; i++)
1933 option_display.key = gpu_peops_option[i];
1934 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
1941 var.key = "pcsx_rearmed_gpu_unai_ilace_force";
1944 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1946 if (strcmp(var.value, "disabled") == 0)
1947 pl_rearmed_cbs.gpu_unai.ilace_force = 0;
1948 else if (strcmp(var.value, "enabled") == 0)
1949 pl_rearmed_cbs.gpu_unai.ilace_force = 1;
1952 var.key = "pcsx_rearmed_gpu_unai_pixel_skip";
1955 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1957 if (strcmp(var.value, "disabled") == 0)
1958 pl_rearmed_cbs.gpu_unai.pixel_skip = 0;
1959 else if (strcmp(var.value, "enabled") == 0)
1960 pl_rearmed_cbs.gpu_unai.pixel_skip = 1;
1963 var.key = "pcsx_rearmed_gpu_unai_lighting";
1966 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1968 if (strcmp(var.value, "disabled") == 0)
1969 pl_rearmed_cbs.gpu_unai.lighting = 0;
1970 else if (strcmp(var.value, "enabled") == 0)
1971 pl_rearmed_cbs.gpu_unai.lighting = 1;
1974 var.key = "pcsx_rearmed_gpu_unai_fast_lighting";
1977 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1979 if (strcmp(var.value, "disabled") == 0)
1980 pl_rearmed_cbs.gpu_unai.fast_lighting = 0;
1981 else if (strcmp(var.value, "enabled") == 0)
1982 pl_rearmed_cbs.gpu_unai.fast_lighting = 1;
1985 var.key = "pcsx_rearmed_gpu_unai_blending";
1988 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1990 if (strcmp(var.value, "disabled") == 0)
1991 pl_rearmed_cbs.gpu_unai.blending = 0;
1992 else if (strcmp(var.value, "enabled") == 0)
1993 pl_rearmed_cbs.gpu_unai.blending = 1;
1996 var.key = "pcsx_rearmed_show_gpu_unai_settings";
1999 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2001 int show_advanced_gpu_unai_settings_prev = show_advanced_gpu_unai_settings;
2003 show_advanced_gpu_unai_settings = 1;
2004 if (strcmp(var.value, "disabled") == 0)
2005 show_advanced_gpu_unai_settings = 0;
2007 if (show_advanced_gpu_unai_settings != show_advanced_gpu_unai_settings_prev)
2010 struct retro_core_option_display option_display;
2011 char gpu_unai_option[5][40] = {
2012 "pcsx_rearmed_gpu_unai_blending",
2013 "pcsx_rearmed_gpu_unai_lighting",
2014 "pcsx_rearmed_gpu_unai_fast_lighting",
2015 "pcsx_rearmed_gpu_unai_ilace_force",
2016 "pcsx_rearmed_gpu_unai_pixel_skip"
2019 option_display.visible = show_advanced_gpu_unai_settings;
2021 for (i = 0; i < 5; i++)
2023 option_display.key = gpu_unai_option[i];
2024 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
2030 //This adjustment process gives the user the ability to manually align the mouse up better
2031 //with where the shots are in the emulator.
2034 var.key = "pcsx_rearmed_gunconadjustx";
2036 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2038 GunconAdjustX = atoi(var.value);
2042 var.key = "pcsx_rearmed_gunconadjusty";
2044 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2046 GunconAdjustY = atoi(var.value);
2050 var.key = "pcsx_rearmed_gunconadjustratiox";
2052 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2054 GunconAdjustRatioX = atof(var.value);
2058 var.key = "pcsx_rearmed_gunconadjustratioy";
2060 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2062 GunconAdjustRatioY = atof(var.value);
2067 var.key = "pcsx_rearmed_nosmccheck";
2068 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2070 if (strcmp(var.value, "enabled") == 0)
2071 new_dynarec_hacks |= NDHACK_NO_SMC_CHECK;
2073 new_dynarec_hacks &= ~NDHACK_NO_SMC_CHECK;
2077 var.key = "pcsx_rearmed_gteregsunneeded";
2078 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2080 if (strcmp(var.value, "enabled") == 0)
2081 new_dynarec_hacks |= NDHACK_GTE_UNNEEDED;
2083 new_dynarec_hacks &= ~NDHACK_GTE_UNNEEDED;
2087 var.key = "pcsx_rearmed_nogteflags";
2088 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2090 if (strcmp(var.value, "enabled") == 0)
2091 new_dynarec_hacks |= NDHACK_GTE_NO_FLAGS;
2093 new_dynarec_hacks &= ~NDHACK_GTE_NO_FLAGS;
2096 /* this probably is safe to change in real-time */
2098 var.key = "pcsx_rearmed_psxclock";
2099 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2101 int psxclock = atoi(var.value);
2102 cycle_multiplier = 10000 / psxclock;
2104 #endif /* NEW_DYNAREC */
2106 var.key = "pcsx_rearmed_show_other_input_settings";
2109 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2111 int previous_settings = show_other_input_settings;
2113 show_other_input_settings = 1;
2114 if (strcmp(var.value, "disabled") == 0)
2115 show_other_input_settings = 0;
2117 if (show_other_input_settings != previous_settings)
2120 struct retro_core_option_display option_display;
2121 char gpu_peops_option[][50] = {
2122 "pcsx_rearmed_multitap1",
2123 "pcsx_rearmed_multitap2",
2124 "pcsx_rearmed_pad3type",
2125 "pcsx_rearmed_pad4type",
2126 "pcsx_rearmed_pad5type",
2127 "pcsx_rearmed_pad6type",
2128 "pcsx_rearmed_pad7type",
2129 "pcsx_rearmed_pad8type",
2130 "pcsx_rearmed_negcon_deadzone",
2131 "pcsx_rearmed_negcon_response",
2132 "pcsx_rearmed_analog_axis_modifier",
2133 "pcsx_rearmed_gunconadjustx",
2134 "pcsx_rearmed_gunconadjusty",
2135 "pcsx_rearmed_gunconadjustratiox",
2136 "pcsx_rearmed_gunconadjustratioy"
2138 #define INPUT_LIST (sizeof(gpu_peops_option) / sizeof(gpu_peops_option[0]))
2140 option_display.visible = show_other_input_settings;
2142 for (i = 0; i < INPUT_LIST; i++)
2144 option_display.key = gpu_peops_option[i];
2145 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
2152 // inform core things about possible config changes
2153 plugin_call_rearmed_cbs();
2155 if (GPU_open != NULL && GPU_close != NULL)
2158 GPU_open(&gpuDisp, "PCSX", NULL);
2167 //bootlogo display hack
2171 var.key = "pcsx_rearmed_show_bios_bootlogo";
2172 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2174 Config.SlowBoot = 0;
2176 if (strcmp(var.value, "enabled") == 0)
2178 Config.SlowBoot = 1;
2186 // Taken from beetle-psx-libretro
2187 static uint16_t get_analog_button(int16_t ret, retro_input_state_t input_state_cb, int player_index, int id)
2189 // NOTE: Analog buttons were added Nov 2017. Not all front-ends support this
2190 // feature (or pre-date it) so we need to handle this in a graceful way.
2192 // First, try and get an analog value using the new libretro API constant
2193 uint16_t button = input_state_cb(player_index,
2194 RETRO_DEVICE_ANALOG,
2195 RETRO_DEVICE_INDEX_ANALOG_BUTTON,
2197 button = MIN(button / 128, 255);
2201 // If we got exactly zero, we're either not pressing the button, or the front-end
2202 // is not reporting analog values. We need to do a second check using the classic
2203 // digital API method, to at least get some response - better than nothing.
2205 // NOTE: If we're really just not holding the button, we're still going to get zero.
2207 button = (ret & (1 << id)) ? 255 : 0;
2213 unsigned char axis_range_modifier(int16_t axis_value, bool is_square)
2215 float modifier_axis_range = 0;
2219 modifier_axis_range = round((axis_value >> 8) / 0.785) + 128;
2220 if (modifier_axis_range < 0)
2222 modifier_axis_range = 0;
2224 else if (modifier_axis_range > 255)
2226 modifier_axis_range = 255;
2231 modifier_axis_range = MIN(((axis_value >> 8) + 128), 255);
2234 return modifier_axis_range;
2237 static void update_input_guncon(int port, int ret)
2239 //ToDo move across to:
2240 //RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X
2241 //RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y
2242 //RETRO_DEVICE_ID_LIGHTGUN_TRIGGER
2243 //RETRO_DEVICE_ID_LIGHTGUN_RELOAD
2244 //RETRO_DEVICE_ID_LIGHTGUN_AUX_A
2245 //RETRO_DEVICE_ID_LIGHTGUN_AUX_B
2246 //Though not sure these are hooked up properly on the Pi
2249 //Put the controller index back to port instead of hardcoding to 1 when the libretro overlay crash bug is fixed
2250 //This is required for 2 player
2252 //GUNCON has 3 controls, Trigger,A,B which equal Circle,Start,Cross
2255 //The 1 is hardcoded instead of port to prevent the overlay mouse button libretro crash bug
2256 if (input_state_cb(1, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_LEFT))
2258 in_keystate[port] |= (1 << DKEY_CIRCLE);
2262 if (input_state_cb(1, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_RIGHT))
2264 in_keystate[port] |= (1 << DKEY_START);
2268 if (input_state_cb(1, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_MIDDLE))
2270 in_keystate[port] |= (1 << DKEY_CROSS);
2273 //The 1 is hardcoded instead of port to prevent the overlay mouse button libretro crash bug
2274 int gunx = input_state_cb(1, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_X);
2275 int guny = input_state_cb(1, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_Y);
2277 //Mouse range is -32767 -> 32767
2279 //Use the left analog stick field to store the absolute coordinates
2280 in_analog_left[0][0] = (gunx * GunconAdjustRatioX) + (GunconAdjustX * 655);
2281 in_analog_left[0][1] = (guny * GunconAdjustRatioY) + (GunconAdjustY * 655);
2284 static void update_input_negcon(int port, int ret)
2290 float negcon_twist_amplitude;
2292 // Query digital inputs
2295 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_UP))
2296 in_keystate[port] |= (1 << DKEY_UP);
2298 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT))
2299 in_keystate[port] |= (1 << DKEY_RIGHT);
2301 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN))
2302 in_keystate[port] |= (1 << DKEY_DOWN);
2304 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT))
2305 in_keystate[port] |= (1 << DKEY_LEFT);
2307 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_START))
2308 in_keystate[port] |= (1 << DKEY_START);
2310 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_A))
2311 in_keystate[port] |= (1 << DKEY_CIRCLE);
2313 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_X))
2314 in_keystate[port] |= (1 << DKEY_TRIANGLE);
2315 // > neGcon R shoulder (digital)
2316 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_R))
2317 in_keystate[port] |= (1 << DKEY_R1);
2318 // Query analog inputs
2320 // From studying 'libpcsxcore/plugins.c' and 'frontend/plugin.c':
2321 // >> pad->leftJoyX == in_analog_left[port][0] == NeGcon II
2322 // >> pad->leftJoyY == in_analog_left[port][1] == NeGcon L
2323 // >> pad->rightJoyX == in_analog_right[port][0] == NeGcon twist
2324 // >> pad->rightJoyY == in_analog_right[port][1] == NeGcon I
2325 // So we just have to map in_analog_left/right to more
2326 // appropriate inputs...
2329 // >> Get raw analog stick value and account for deadzone
2330 lsx = input_state_cb(port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
2331 if (lsx > negcon_deadzone)
2332 lsx = lsx - negcon_deadzone;
2333 else if (lsx < -negcon_deadzone)
2334 lsx = lsx + negcon_deadzone;
2337 // >> Convert to an 'amplitude' [-1.0,1.0] and adjust response
2338 negcon_twist_amplitude = (float)lsx / (float)(NEGCON_RANGE - negcon_deadzone);
2339 if (negcon_linearity == 2)
2341 if (negcon_twist_amplitude < 0.0)
2342 negcon_twist_amplitude = -(negcon_twist_amplitude * negcon_twist_amplitude);
2344 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude;
2346 else if (negcon_linearity == 3)
2347 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude * negcon_twist_amplitude;
2348 // >> Convert to final 'in_analog' integer value [0,255]
2349 in_analog_right[port][0] = MAX(MIN((int)(negcon_twist_amplitude * 128.0f) + 128, 255), 0);
2351 // >> Handle right analog stick vertical axis mapping...
2352 // - Up (-Y) == accelerate == neGcon I
2353 // - Down (+Y) == brake == neGcon II
2356 rsy = input_state_cb(port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
2359 // Account for deadzone
2360 // (Note: have never encountered a gamepad with significant differences
2361 // in deadzone between left/right analog sticks, so use the regular 'twist'
2363 if (rsy > negcon_deadzone)
2364 rsy = rsy - negcon_deadzone;
2367 // Convert to 'in_analog' integer value [0,255]
2368 negcon_ii_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2372 if (rsy < -negcon_deadzone)
2373 rsy = -1 * (rsy + negcon_deadzone);
2376 negcon_i_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2379 in_analog_right[port][1] = MAX(
2381 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_R2),
2382 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_B)),
2385 in_analog_left[port][0] = MAX(
2387 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_L2),
2388 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_Y)),
2391 in_analog_left[port][1] = get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_L);
2394 static void update_input(void)
2396 // reset all keystate, query libretro for keystate
2400 for (i = 0; i < PORTS_NUMBER; i++)
2403 int type = in_type[i];
2407 if (type == PSE_PAD_TYPE_NONE)
2410 if (libretro_supports_bitmasks)
2411 ret = input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
2414 for (j = 0; j < (RETRO_DEVICE_ID_JOYPAD_R3 + 1); j++)
2416 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j))
2423 case PSE_PAD_TYPE_GUNCON:
2424 update_input_guncon(i, ret);
2426 case PSE_PAD_TYPE_NEGCON:
2427 update_input_negcon(i, ret);
2430 // Query digital inputs
2431 for (j = 0; j < RETRO_PSX_MAP_LEN; j++)
2433 in_keystate[i] |= retro_psx_map[j];
2435 // Query analog inputs
2436 if (type == PSE_PAD_TYPE_ANALOGJOY || type == PSE_PAD_TYPE_ANALOGPAD)
2438 in_analog_left[i][0] = axis_range_modifier(input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X), axis_bounds_modifier);
2439 in_analog_left[i][1] = axis_range_modifier(input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y), axis_bounds_modifier);
2440 in_analog_right[i][0] = axis_range_modifier(input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X), axis_bounds_modifier);
2441 in_analog_right[i][1] = axis_range_modifier(input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y), axis_bounds_modifier);
2447 static void print_internal_fps(void)
2449 if (display_internal_fps)
2453 if (frame_count % INTERNAL_FPS_SAMPLE_PERIOD == 0)
2455 unsigned internal_fps = pl_rearmed_cbs.flip_cnt * (is_pal_mode ? 50 : 60) / INTERNAL_FPS_SAMPLE_PERIOD;
2457 const char *strc = (const char *)str;
2458 struct retro_message msg = {
2465 snprintf(str, sizeof(str), "Internal FPS: %2d", internal_fps);
2467 pl_rearmed_cbs.flip_cnt = 0;
2469 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
2476 void retro_run(void)
2478 //SysReset must be run while core is running,Not in menu (Locks up Retroarch)
2483 if (!Config.HLE && !Config.SlowBoot)
2486 psxRegs.pc = psxRegs.GPR.n.ra;
2490 print_internal_fps();
2496 bool updated = false;
2497 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
2498 update_variables(true);
2503 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf_ptr : NULL,
2504 vout_width, vout_height, vout_width * 2);
2510 static bool try_use_bios(const char *path)
2516 f = fopen(path, "rb");
2520 fseek(f, 0, SEEK_END);
2524 if (size != 512 * 1024)
2527 name = strrchr(path, SLASH);
2530 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
2535 #include <sys/types.h>
2538 static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
2544 dir = opendir(dirpath);
2548 while ((ent = readdir(dir)))
2550 if ((strncasecmp(ent->d_name, "scph", 4) != 0) && (strncasecmp(ent->d_name, "psx", 3) != 0))
2553 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
2554 ret = try_use_bios(path);
2562 #define find_any_bios(...) false
2565 static void check_system_specs(void)
2568 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
2571 static int init_memcards(void)
2575 struct retro_variable var = { .key = "pcsx_rearmed_memcard2", .value = NULL };
2576 static const char CARD2_FILE[] = "pcsx-card2.mcd";
2578 // Memcard2 will be handled and is re-enabled if needed using core
2580 // Memcard1 is handled by libretro, doing this will set core to
2581 // skip file io operations for memcard1 like SaveMcd
2582 snprintf(Config.Mcd1, sizeof(Config.Mcd1), "none");
2583 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "none");
2584 init_memcard(Mcd1Data);
2585 // Memcard 2 is managed by the emulator on the filesystem,
2586 // There is no need to initialize Mcd2Data like Mcd1Data.
2588 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2590 SysPrintf("Memcard 2: %s\n", var.value);
2591 if (memcmp(var.value, "enabled", 7) == 0)
2593 if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir)
2595 if (strlen(dir) + strlen(CARD2_FILE) + 2 > sizeof(Config.Mcd2))
2597 SysPrintf("Path '%s' is too long. Cannot use memcard 2. Use a shorter path.\n", dir);
2603 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "%s/%s", dir, CARD2_FILE);
2604 SysPrintf("Use memcard 2: %s\n", Config.Mcd2);
2609 SysPrintf("Could not get save directory! Could not create memcard 2.");
2617 static void loadPSXBios(void)
2620 char path[PATH_MAX];
2621 unsigned useHLE = 0;
2623 const char *bios[] = {
2624 "PSXONPSP660", "psxonpsp660",
2625 "SCPH101", "scph101",
2626 "SCPH5501", "scph5501",
2627 "SCPH7001", "scph7001",
2628 "SCPH1001", "scph1001"
2631 struct retro_variable var = {
2632 .key = "pcsx_rearmed_bios",
2638 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2640 if (!strcmp(var.value, "HLE"))
2646 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
2649 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s", dir);
2651 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++)
2653 snprintf(path, sizeof(path), "%s%c%s.bin", dir, SLASH, bios[i]);
2654 found_bios = try_use_bios(path);
2660 found_bios = find_any_bios(dir, path, sizeof(path));
2664 SysPrintf("found BIOS file: %s\n", Config.Bios);
2668 if (useHLE || !found_bios)
2670 SysPrintf("no BIOS files found.\n");
2671 struct retro_message msg = {
2672 "No PlayStation BIOS file found - add for better compatibility",
2675 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, (void *)&msg);
2679 void retro_init(void)
2681 unsigned dci_version = 0;
2682 struct retro_rumble_interface rumble;
2686 // magic sauce to make the dynarec work on iOS
2687 syscall(SYS_ptrace, 0 /*PTRACE_TRACEME*/, 0, 0, 0);
2691 psxMapHook = pl_3ds_mmap;
2692 psxUnmapHook = pl_3ds_munmap;
2695 if (init_vita_mmap() < 0)
2697 psxMapHook = pl_vita_mmap;
2698 psxUnmapHook = pl_vita_munmap;
2700 ret = emu_core_preinit();
2702 /* emu_core_preinit sets the cpu to dynarec */
2704 Config.Cpu = CPU_INTERPRETER;
2706 ret |= init_memcards();
2708 ret |= emu_core_init();
2711 SysPrintf("PCSX init failed.\n");
2716 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
2717 #elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && !defined(VITA) && !defined(__SWITCH__)
2718 posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
2720 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
2723 vout_buf_ptr = vout_buf;
2727 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
2729 disk_initial_index = 0;
2730 disk_initial_path[0] = '\0';
2731 if (environ_cb(RETRO_ENVIRONMENT_GET_DISK_CONTROL_INTERFACE_VERSION, &dci_version) && (dci_version >= 1))
2732 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_EXT_INTERFACE, &disk_control_ext);
2734 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
2737 if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble))
2738 rumble_cb = rumble.set_rumble_state;
2740 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
2741 * we have to do this because cache misses and some IO penalties
2742 * are not emulated. Warning: changing this may break compatibility. */
2743 cycle_multiplier = 175;
2744 #ifdef HAVE_PRE_ARMV7
2745 cycle_multiplier = 200;
2747 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
2748 pl_rearmed_cbs.gpu_peops.dwActFixes = GPU_PEOPS_OLD_FRAME_SKIP;
2749 spu_config.iUseFixedUpdates = 1;
2751 SaveFuncs.open = save_open;
2752 SaveFuncs.read = save_read;
2753 SaveFuncs.write = save_write;
2754 SaveFuncs.seek = save_seek;
2755 SaveFuncs.close = save_close;
2757 if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL))
2758 libretro_supports_bitmasks = true;
2760 check_system_specs();
2763 void retro_deinit(void)
2772 linearFree(vout_buf);
2781 libretro_supports_bitmasks = false;
2783 /* Have to reset disks struct, otherwise
2784 * fnames/flabels will leak memory */
2789 #include <psp2/kernel/threadmgr.h>
2790 int usleep(unsigned long us)
2792 sceKernelDelayThread(us);
2796 void SysPrintf(const char *fmt, ...)
2801 va_start(list, fmt);
2802 vsprintf(msg, fmt, list);
2806 log_cb(RETRO_LOG_INFO, "%s", msg);