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
65 u32 event_cycles[PSXINT_COUNT];
67 int new_dynarec_hacks;
69 void new_dyna_before_save(void) { }
70 void new_dyna_after_save(void) { }
71 void new_dyna_freeze(void *f, int i) { }
74 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
75 static int rebootemu = 0;
77 static retro_video_refresh_t video_cb;
78 static retro_input_poll_t input_poll_cb;
79 static retro_input_state_t input_state_cb;
80 static retro_environment_t environ_cb;
81 static retro_audio_sample_batch_t audio_batch_cb;
82 static retro_set_rumble_state_t rumble_cb;
83 static struct retro_log_callback logging;
84 static retro_log_printf_t log_cb;
86 static void *vout_buf;
87 static void * vout_buf_ptr;
88 static int vout_width, vout_height;
89 static int vout_doffs_old, vout_fb_dirty;
90 static bool vout_can_dupe;
91 static bool duping_enable;
92 static bool found_bios;
93 static bool display_internal_fps = false;
94 static unsigned frame_count = 0;
95 static bool libretro_supports_bitmasks = false;
97 static int show_advanced_gpu_peops_settings = -1;
100 static int show_advanced_gpu_unai_settings = -1;
103 static unsigned previous_width = 0;
104 static unsigned previous_height = 0;
106 static int plugins_opened;
107 static int is_pal_mode;
109 /* memory card data */
110 extern char Mcd1Data[MCD_SIZE];
111 extern char Mcd2Data[MCD_SIZE];
112 extern char McdDisable[2];
114 /* PCSX ReARMed core calls and stuff */
116 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
117 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
118 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
119 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE
121 int in_analog_left[8][2] = {{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 }};
122 int in_analog_right[8][2] = {{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 }};
123 unsigned short in_keystate[PORTS_NUMBER];
126 int in_enable_vibration = 1;
128 // NegCon adjustment parameters
129 // > The NegCon 'twist' action is somewhat awkward when mapped
130 // to a standard analog stick -> user should be able to tweak
131 // response/deadzone for comfort
132 // > When response is linear, 'additional' deadzone (set here)
133 // may be left at zero, since this is normally handled via in-game
135 // > When response is non-linear, deadzone should be set to match the
136 // controller being used (otherwise precision may be lost)
137 // > negcon_linearity:
138 // - 1: Response is linear - recommended when using racing wheel
139 // peripherals, not recommended for standard gamepads
140 // - 2: Response is quadratic - optimal setting for gamepads
141 // - 3: Response is cubic - enables precise fine control, but
142 // difficult to use...
143 #define NEGCON_RANGE 0x7FFF
144 static int negcon_deadzone = 0;
145 static int negcon_linearity = 1;
147 static bool axis_bounds_modifier;
149 /* PSX max resolution is 640x512, but with enhancement it's 1024x512 */
150 #define VOUT_MAX_WIDTH 1024
151 #define VOUT_MAX_HEIGHT 512
154 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info){return false;}
155 void retro_unload_game(void){}
156 static int vout_open(void){return 0;}
157 static void vout_close(void){}
158 static int snd_init(void){return 0;}
159 static void snd_finish(void){}
160 static int snd_busy(void){return 0;}
162 #define GPU_PEOPS_ODD_EVEN_BIT (1 << 0)
163 #define GPU_PEOPS_EXPAND_SCREEN_WIDTH (1 << 1)
164 #define GPU_PEOPS_IGNORE_BRIGHTNESS (1 << 2)
165 #define GPU_PEOPS_DISABLE_COORD_CHECK (1 << 3)
166 #define GPU_PEOPS_LAZY_SCREEN_UPDATE (1 << 6)
167 #define GPU_PEOPS_OLD_FRAME_SKIP (1 << 7)
168 #define GPU_PEOPS_REPEATED_TRIANGLES (1 << 8)
169 #define GPU_PEOPS_QUADS_WITH_TRIANGLES (1 << 9)
170 #define GPU_PEOPS_FAKE_BUSY_STATE (1 << 10)
172 static void init_memcard(char *mcd_data)
177 memset(mcd_data, 0, MCD_SIZE);
179 mcd_data[off++] = 'M';
180 mcd_data[off++] = 'C';
182 mcd_data[off++] = 0x0e;
184 for (i = 0; i < 15; i++) {
185 mcd_data[off++] = 0xa0;
187 mcd_data[off++] = 0xff;
188 mcd_data[off++] = 0xff;
190 mcd_data[off++] = 0xa0;
193 for (i = 0; i < 20; i++) {
194 mcd_data[off++] = 0xff;
195 mcd_data[off++] = 0xff;
196 mcd_data[off++] = 0xff;
197 mcd_data[off++] = 0xff;
199 mcd_data[off++] = 0xff;
200 mcd_data[off++] = 0xff;
205 static void set_vout_fb()
207 struct retro_framebuffer fb = {0};
209 fb.width = vout_width;
210 fb.height = vout_height;
211 fb.access_flags = RETRO_MEMORY_ACCESS_WRITE;
213 if (environ_cb(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &fb) && fb.format == RETRO_PIXEL_FORMAT_RGB565)
214 vout_buf_ptr = (uint16_t*)fb.data;
216 vout_buf_ptr = vout_buf;
219 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
224 if (previous_width != vout_width || previous_height != vout_height)
226 previous_width = vout_width;
227 previous_height = vout_height;
229 struct retro_system_av_info info;
230 retro_get_system_av_info(&info);
231 environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &info.geometry);
237 #ifndef FRONTEND_SUPPORTS_RGB565
238 static void convert(void *buf, size_t bytes)
240 unsigned int i, v, *p = buf;
242 for (i = 0; i < bytes / 4; i++) {
244 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
249 static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
251 unsigned short *dest = vout_buf_ptr;
252 const unsigned short *src = vram;
253 int dstride = vout_width, h1 = h;
258 memset(vout_buf_ptr, 0, dstride * h * 2);
262 doffs = (vout_height - h) * dstride;
263 doffs += (dstride - w) / 2 & ~1;
264 if (doffs != vout_doffs_old) {
266 memset(vout_buf_ptr, 0, dstride * h * 2);
267 vout_doffs_old = doffs;
273 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
274 for (; h1-- > 0; dest += dstride, src += stride)
276 bgr888_to_rgb565(dest, src, w * 3);
281 for (; h1-- > 0; dest += dstride, src += stride)
283 bgr555_to_rgb565(dest, src, w * 2);
288 #ifndef FRONTEND_SUPPORTS_RGB565
289 convert(vout_buf_ptr, vout_width * vout_height * 2);
292 pl_rearmed_cbs.flip_cnt++;
304 psx_map_t custom_psx_maps[] = {
305 {NULL, 0x13000000, 0x210000, MAP_TAG_RAM}, // 0x80000000
306 {NULL, 0x12800000, 0x010000, MAP_TAG_OTHER}, // 0x1f800000
307 {NULL, 0x12c00000, 0x080000, MAP_TAG_OTHER}, // 0x1fc00000
308 {NULL, 0x11000000, 0x800000, MAP_TAG_LUTS}, // 0x08000000
309 {NULL, 0x12000000, 0x200000, MAP_TAG_VRAM}, // 0x00000000
312 void* pl_3ds_mmap(unsigned long addr, size_t size, int is_fixed,
320 psx_map_t* custom_map = custom_psx_maps;
322 for (; custom_map->size; custom_map++)
324 if ((custom_map->size == size) && (custom_map->tag == tag))
326 uint32_t ptr_aligned, tmp;
328 custom_map->buffer = malloc(size + 0x1000);
329 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
331 if(svcControlMemory(&tmp, (void*)custom_map->target_map, (void*)ptr_aligned, size, MEMOP_MAP, 0x3) < 0)
333 SysPrintf("could not map memory @0x%08X\n", custom_map->target_map);
337 return (void*)custom_map->target_map;
345 void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
351 psx_map_t* custom_map = custom_psx_maps;
353 for (; custom_map->size; custom_map++)
355 if ((custom_map->target_map == (uint32_t)ptr))
357 uint32_t ptr_aligned, tmp;
359 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
361 svcControlMemory(&tmp, (void*)custom_map->target_map, (void*)ptr_aligned, size, MEMOP_UNMAP, 0x3);
363 free(custom_map->buffer);
364 custom_map->buffer = NULL;
385 psx_map_t custom_psx_maps[] = {
386 {NULL, NULL, 0x210000, MAP_TAG_RAM}, // 0x80000000
387 {NULL, NULL, 0x010000, MAP_TAG_OTHER}, // 0x1f800000
388 {NULL, NULL, 0x080000, MAP_TAG_OTHER}, // 0x1fc00000
389 {NULL, NULL, 0x800000, MAP_TAG_LUTS}, // 0x08000000
390 {NULL, NULL, 0x200000, MAP_TAG_VRAM}, // 0x00000000
393 int init_vita_mmap(){
396 addr = malloc(64*1024*1024);
399 tmpaddr = ((u32)(addr+0xFFFFFF))&~0xFFFFFF;
400 custom_psx_maps[0].buffer=tmpaddr+0x2000000;
401 custom_psx_maps[1].buffer=tmpaddr+0x1800000;
402 custom_psx_maps[2].buffer=tmpaddr+0x1c00000;
403 custom_psx_maps[3].buffer=tmpaddr+0x0000000;
404 custom_psx_maps[4].buffer=tmpaddr+0x1000000;
406 for(n = 0; n < 5; n++){
407 sceClibPrintf("addr reserved %x\n",custom_psx_maps[n].buffer);
413 void deinit_vita_mmap(){
417 void* pl_vita_mmap(unsigned long addr, size_t size, int is_fixed,
424 psx_map_t* custom_map = custom_psx_maps;
426 for (; custom_map->size; custom_map++)
428 if ((custom_map->size == size) && (custom_map->tag == tag))
430 return custom_map->buffer;
437 void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag)
441 psx_map_t* custom_map = custom_psx_maps;
443 for (; custom_map->size; custom_map++)
445 if ((custom_map->buffer == ptr))
455 static void *pl_mmap(unsigned int size)
457 return psxMap(0, size, 0, MAP_TAG_VRAM);
460 static void pl_munmap(void *ptr, unsigned int size)
462 psxUnmap(ptr, size, MAP_TAG_VRAM);
465 struct rearmed_cbs pl_rearmed_cbs = {
466 .pl_vout_open = vout_open,
467 .pl_vout_set_mode = vout_set_mode,
468 .pl_vout_flip = vout_flip,
469 .pl_vout_close = vout_close,
472 /* from psxcounters */
473 .gpu_hcnt = &hSyncCount,
474 .gpu_frame_count = &frame_counter,
477 void pl_frame_limit(void)
479 /* called once per frame, make psxCpu->Execute() above return */
483 void pl_timing_prepare(int is_pal)
485 is_pal_mode = is_pal;
488 void plat_trigger_vibrate(int pad, int low, int high)
493 if (in_enable_vibration)
495 rumble_cb(pad, RETRO_RUMBLE_STRONG, high << 8);
496 rumble_cb(pad, RETRO_RUMBLE_WEAK, low ? 0xffff : 0x0);
500 void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
505 static void snd_feed(void *buf, int bytes)
507 if (audio_batch_cb != NULL)
508 audio_batch_cb(buf, bytes / 4);
511 void out_register_libretro(struct out_driver *drv)
513 drv->name = "libretro";
514 drv->init = snd_init;
515 drv->finish = snd_finish;
516 drv->busy = snd_busy;
517 drv->feed = snd_feed;
521 void retro_set_environment(retro_environment_t cb)
525 if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
526 log_cb = logging.log;
528 libretro_set_core_options(environ_cb);
531 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
532 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
533 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
534 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
535 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
537 unsigned retro_api_version(void)
539 return RETRO_API_VERSION;
542 static int controller_port_variable(unsigned port, struct retro_variable *var)
544 if (port >= PORTS_NUMBER)
553 var->key = "pcsx_rearmed_pad1type";
556 var->key = "pcsx_rearmed_pad2type";
559 var->key = "pcsx_rearmed_pad3type";
562 var->key = "pcsx_rearmed_pad4type";
565 var->key = "pcsx_rearmed_pad5type";
568 var->key = "pcsx_rearmed_pad6type";
571 var->key = "pcsx_rearmed_pad7type";
574 var->key = "pcsx_rearmed_pad8type";
578 return environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, var) && var->value;
581 static void update_controller_port_variable(unsigned port)
583 if (port >= PORTS_NUMBER)
586 struct retro_variable var;
588 if (controller_port_variable(port, &var))
590 if (strcmp(var.value, "standard") == 0)
591 in_type[port] = PSE_PAD_TYPE_STANDARD;
592 else if (strcmp(var.value, "analog") == 0)
593 in_type[port] = PSE_PAD_TYPE_ANALOGJOY;
594 else if (strcmp(var.value, "dualshock") == 0)
595 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
596 else if (strcmp(var.value, "negcon") == 0)
597 in_type[port] = PSE_PAD_TYPE_NEGCON;
598 else if (strcmp(var.value, "guncon") == 0)
599 in_type[port] = PSE_PAD_TYPE_GUNCON;
600 else if (strcmp(var.value, "none") == 0)
601 in_type[port] = PSE_PAD_TYPE_NONE;
602 // else 'default' case, do nothing
606 static void update_controller_port_device(unsigned port, unsigned device)
608 if (port >= PORTS_NUMBER)
611 struct retro_variable var;
613 if (!controller_port_variable(port, &var))
616 if (strcmp(var.value, "default") != 0)
621 case RETRO_DEVICE_JOYPAD:
622 in_type[port] = PSE_PAD_TYPE_STANDARD;
624 case RETRO_DEVICE_ANALOG:
625 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
627 case RETRO_DEVICE_MOUSE:
628 in_type[port] = PSE_PAD_TYPE_MOUSE;
630 case RETRO_DEVICE_LIGHTGUN:
631 in_type[port] = PSE_PAD_TYPE_GUN;
633 case RETRO_DEVICE_NONE:
635 in_type[port] = PSE_PAD_TYPE_NONE;
639 static void update_multitap()
641 struct retro_variable var;
645 var.key = "pcsx_rearmed_multitap1";
647 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value))
649 if (strcmp(var.value, "enabled") == 0)
651 else if (strcmp(var.value, "disabled") == 0)
661 // If a gamepad is plugged after port 2, we need a first multitap.
663 for (port = 2; port < PORTS_NUMBER; port++)
664 multitap1 |= in_type[port] != PSE_PAD_TYPE_NONE;
668 var.key = "pcsx_rearmed_multitap2";
670 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value))
672 if (strcmp(var.value, "enabled") == 0)
674 else if (strcmp(var.value, "disabled") == 0)
684 // If a gamepad is plugged after port 4, we need a second multitap.
686 for (port = 4; port < PORTS_NUMBER; port++)
687 multitap2 |= in_type[port] != PSE_PAD_TYPE_NONE;
691 void retro_set_controller_port_device(unsigned port, unsigned device)
693 SysPrintf("port %u device %u",port,device);
695 if (port >= PORTS_NUMBER)
698 update_controller_port_device(port, device);
702 void retro_get_system_info(struct retro_system_info *info)
705 #define GIT_VERSION ""
707 memset(info, 0, sizeof(*info));
708 info->library_name = "PCSX-ReARMed";
709 info->library_version = "r22" GIT_VERSION;
710 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u|chd";
711 info->need_fullpath = true;
714 void retro_get_system_av_info(struct retro_system_av_info *info)
716 unsigned geom_height = vout_height > 0 ? vout_height : 240;
717 unsigned geom_width = vout_width > 0 ? vout_width : 320;
719 memset(info, 0, sizeof(*info));
720 info->timing.fps = is_pal_mode ? 50 : 60;
721 info->timing.sample_rate = 44100;
722 info->geometry.base_width = geom_width;
723 info->geometry.base_height = geom_height;
724 info->geometry.max_width = VOUT_MAX_WIDTH;
725 info->geometry.max_height = VOUT_MAX_HEIGHT;
726 info->geometry.aspect_ratio = 4.0 / 3.0;
730 size_t retro_serialize_size(void)
732 // it's currently 4380651-4397047 bytes,
733 // but have some reserved for future
743 static void *save_open(const char *name, const char *mode)
747 if (name == NULL || mode == NULL)
750 fp = malloc(sizeof(*fp));
754 fp->buf = (char *)name;
756 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
761 static int save_read(void *file, void *buf, u32 len)
763 struct save_fp *fp = file;
764 if (fp == NULL || buf == NULL)
767 memcpy(buf, fp->buf + fp->pos, len);
772 static int save_write(void *file, const void *buf, u32 len)
774 struct save_fp *fp = file;
775 if (fp == NULL || buf == NULL)
778 memcpy(fp->buf + fp->pos, buf, len);
783 static long save_seek(void *file, long offs, int whence)
785 struct save_fp *fp = file;
801 static void save_close(void *file)
803 struct save_fp *fp = file;
804 size_t r_size = retro_serialize_size();
808 if (fp->pos > r_size)
809 SysPrintf("ERROR: save buffer overflow detected\n");
810 else if (fp->is_write && fp->pos < r_size)
811 // make sure we don't save trash in leftover space
812 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
816 bool retro_serialize(void *data, size_t size)
818 int ret = SaveState(data);
819 return ret == 0 ? true : false;
822 bool retro_unserialize(const void *data, size_t size)
824 int ret = LoadState(data);
825 return ret == 0 ? true : false;
829 void retro_cheat_reset(void)
834 void retro_cheat_set(unsigned index, bool enabled, const char *code)
839 // cheat funcs are destructive, need a copy..
840 strncpy(buf, code, sizeof(buf));
841 buf[sizeof(buf) - 1] = 0;
843 //Prepare buffered cheat for PCSX's AddCheat fucntion.
857 if (index < NumCheats)
858 ret = EditCheat(index, "", buf);
860 ret = AddCheat("", buf);
863 SysPrintf("Failed to set cheat %#u\n", index);
864 else if (index < NumCheats)
865 Cheats[index].Enabled = enabled;
868 // just in case, maybe a win-rt port in the future?
876 #define PATH_MAX 4096
879 /* multidisk support */
880 static unsigned int disk_initial_index;
881 static char disk_initial_path[PATH_MAX];
882 static bool disk_ejected;
883 static unsigned int disk_current_index;
884 static unsigned int disk_count;
885 static struct disks_state {
888 int internal_index; // for multidisk eboots
891 static void get_disk_label(char *disk_label, const char *disk_path, size_t len)
893 const char *base = NULL;
895 if (!disk_path || (*disk_path == '\0'))
898 base = strrchr(disk_path, SLASH);
905 strncpy(disk_label, base, len - 1);
906 disk_label[len - 1] = '\0';
908 char *ext = strrchr(disk_label, '.');
913 static void disk_init(void)
917 disk_ejected = false;
918 disk_current_index = 0;
921 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
922 if (disks[i].fname != NULL) {
923 free(disks[i].fname);
924 disks[i].fname = NULL;
926 if (disks[i].flabel != NULL) {
927 free(disks[i].flabel);
928 disks[i].flabel = NULL;
930 disks[i].internal_index = 0;
934 static bool disk_set_eject_state(bool ejected)
937 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
940 disk_ejected = ejected;
944 static bool disk_get_eject_state(void)
946 /* can't be controlled by emulated software */
950 static unsigned int disk_get_image_index(void)
952 return disk_current_index;
955 static bool disk_set_image_index(unsigned int index)
957 if (index >= sizeof(disks) / sizeof(disks[0]))
961 CdromLabel[0] = '\0';
963 if (disks[index].fname == NULL) {
964 SysPrintf("missing disk #%u\n", index);
967 // RetroArch specifies "no disk" with index == count,
968 // so don't fail here..
969 disk_current_index = index;
973 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
974 disks[index].fname, disks[index].internal_index);
976 cdrIsoMultidiskSelect = disks[index].internal_index;
977 set_cd_image(disks[index].fname);
978 if (ReloadCdromPlugin() < 0) {
979 SysPrintf("failed to load cdr plugin\n");
982 if (CDR_open() < 0) {
983 SysPrintf("failed to open cdr plugin\n");
988 SetCdOpenCaseTime(time(NULL) + 2);
992 disk_current_index = index;
996 static unsigned int disk_get_num_images(void)
1001 static bool disk_replace_image_index(unsigned index,
1002 const struct retro_game_info *info)
1004 char *old_fname = NULL;
1005 char *old_flabel = NULL;
1008 if (index >= sizeof(disks) / sizeof(disks[0]))
1011 old_fname = disks[index].fname;
1012 old_flabel = disks[index].flabel;
1014 disks[index].fname = NULL;
1015 disks[index].flabel = NULL;
1016 disks[index].internal_index = 0;
1019 char disk_label[PATH_MAX];
1020 disk_label[0] = '\0';
1022 disks[index].fname = strdup(info->path);
1024 get_disk_label(disk_label, info->path, PATH_MAX);
1025 disks[index].flabel = strdup(disk_label);
1027 if (index == disk_current_index)
1028 ret = disk_set_image_index(index);
1031 if (old_fname != NULL)
1034 if (old_flabel != NULL)
1040 static bool disk_add_image_index(void)
1042 if (disk_count >= 8)
1049 static bool disk_set_initial_image(unsigned index, const char *path)
1051 if (index >= sizeof(disks) / sizeof(disks[0]))
1054 if (!path || (*path == '\0'))
1057 disk_initial_index = index;
1059 strncpy(disk_initial_path, path, sizeof(disk_initial_path) - 1);
1060 disk_initial_path[sizeof(disk_initial_path) - 1] = '\0';
1065 static bool disk_get_image_path(unsigned index, char *path, size_t len)
1067 const char *fname = NULL;
1072 if (index >= sizeof(disks) / sizeof(disks[0]))
1075 fname = disks[index].fname;
1077 if (!fname || (*fname == '\0'))
1080 strncpy(path, fname, len - 1);
1081 path[len - 1] = '\0';
1086 static bool disk_get_image_label(unsigned index, char *label, size_t len)
1088 const char *flabel = NULL;
1093 if (index >= sizeof(disks) / sizeof(disks[0]))
1096 flabel = disks[index].flabel;
1098 if (!flabel || (*flabel == '\0'))
1101 strncpy(label, flabel, len - 1);
1102 label[len - 1] = '\0';
1107 static struct retro_disk_control_callback disk_control = {
1108 .set_eject_state = disk_set_eject_state,
1109 .get_eject_state = disk_get_eject_state,
1110 .get_image_index = disk_get_image_index,
1111 .set_image_index = disk_set_image_index,
1112 .get_num_images = disk_get_num_images,
1113 .replace_image_index = disk_replace_image_index,
1114 .add_image_index = disk_add_image_index,
1117 static struct retro_disk_control_ext_callback disk_control_ext = {
1118 .set_eject_state = disk_set_eject_state,
1119 .get_eject_state = disk_get_eject_state,
1120 .get_image_index = disk_get_image_index,
1121 .set_image_index = disk_set_image_index,
1122 .get_num_images = disk_get_num_images,
1123 .replace_image_index = disk_replace_image_index,
1124 .add_image_index = disk_add_image_index,
1125 .set_initial_image = disk_set_initial_image,
1126 .get_image_path = disk_get_image_path,
1127 .get_image_label = disk_get_image_label,
1130 static char base_dir[1024];
1132 static bool read_m3u(const char *file)
1135 char name[PATH_MAX];
1136 FILE *f = fopen(file, "r");
1140 while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) {
1143 char *carrige_return = strchr(line, '\r');
1145 *carrige_return = '\0';
1146 char *newline = strchr(line, '\n');
1150 if (line[0] != '\0')
1152 char disk_label[PATH_MAX];
1153 disk_label[0] = '\0';
1155 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
1156 disks[disk_count].fname = strdup(name);
1158 get_disk_label(disk_label, name, PATH_MAX);
1159 disks[disk_count].flabel = strdup(disk_label);
1166 return (disk_count != 0);
1169 static void extract_directory(char *buf, const char *path, size_t size)
1172 strncpy(buf, path, size - 1);
1173 buf[size - 1] = '\0';
1175 base = strrchr(buf, '/');
1177 base = strrchr(buf, '\\');
1188 #if defined(__QNX__) || defined(_WIN32)
1189 /* Blackberry QNX doesn't have strcasestr */
1192 * Find the first occurrence of find in s, ignore case.
1195 strcasestr(const char *s, const char*find)
1200 if ((c = *find++) != 0) {
1201 c = tolower((unsigned char)c);
1205 if ((sc = *s++) == 0)
1207 } while ((char)tolower((unsigned char)sc) != c);
1208 } while (strncasecmp(s, find, len) != 0);
1215 static void set_retro_memmap(void)
1217 struct retro_memory_map retromap = { 0 };
1218 struct retro_memory_descriptor mmap =
1220 0, psxM, 0, 0, 0, 0, 0x200000
1223 retromap.descriptors = &mmap;
1224 retromap.num_descriptors = 1;
1226 environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &retromap);
1229 static void update_variables(bool in_flight);
1230 bool retro_load_game(const struct retro_game_info *info)
1233 unsigned int cd_index = 0;
1234 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
1236 struct retro_input_descriptor desc[] = {
1237 #define JOYP(port) \
1238 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" }, \
1239 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" }, \
1240 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" }, \
1241 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" }, \
1242 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" }, \
1243 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" }, \
1244 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" }, \
1245 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" }, \
1246 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" }, \
1247 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" }, \
1248 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" }, \
1249 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" }, \
1250 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" }, \
1251 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" }, \
1252 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" }, \
1253 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, \
1254 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" }, \
1255 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" }, \
1256 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" }, \
1257 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1273 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1275 #ifdef FRONTEND_SUPPORTS_RGB565
1276 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
1277 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
1278 SysPrintf("RGB565 supported, using it\n");
1282 if (info == NULL || info->path == NULL) {
1283 SysPrintf("info->path required\n");
1287 update_variables(false);
1289 if (plugins_opened) {
1296 extract_directory(base_dir, info->path, sizeof(base_dir));
1299 if (!read_m3u(info->path)) {
1300 log_cb(RETRO_LOG_INFO, "failed to read m3u file\n");
1304 char disk_label[PATH_MAX];
1305 disk_label[0] = '\0';
1308 disks[0].fname = strdup(info->path);
1310 get_disk_label(disk_label, info->path, PATH_MAX);
1311 disks[0].flabel = strdup(disk_label);
1314 /* If this is an M3U file, attempt to set the
1315 * initial disk image */
1317 (disk_initial_index > 0) &&
1318 (disk_initial_index < disk_count)) {
1319 const char *fname = disks[disk_initial_index].fname;
1321 if (fname && (*fname != '\0'))
1322 if (strcmp(disk_initial_path, fname) == 0)
1323 cd_index = disk_initial_index;
1326 set_cd_image(disks[cd_index].fname);
1327 disk_current_index = cd_index;
1329 /* have to reload after set_cd_image for correct cdr plugin */
1330 if (LoadPlugins() == -1) {
1331 log_cb(RETRO_LOG_INFO, "failed to load plugins\n");
1338 if (OpenPlugins() == -1) {
1339 log_cb(RETRO_LOG_INFO, "failed to open plugins\n");
1343 /* Handle multi-disk images (i.e. PBP)
1344 * > Cannot do this until after OpenPlugins() is
1345 * called (since this sets the value of
1346 * cdrIsoMultidiskCount) */
1347 if (!is_m3u && (cdrIsoMultidiskCount > 1)) {
1348 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
1350 /* Small annoyance: We need to change the label
1351 * of disk 0, so have to clear existing entries */
1352 if (disks[0].fname != NULL)
1353 free(disks[0].fname);
1354 disks[0].fname = NULL;
1356 if (disks[0].flabel != NULL)
1357 free(disks[0].flabel);
1358 disks[0].flabel = NULL;
1360 for (i = 0; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++) {
1361 char disk_name[PATH_MAX];
1362 char disk_label[PATH_MAX];
1363 disk_name[0] = '\0';
1364 disk_label[0] = '\0';
1366 disks[i].fname = strdup(info->path);
1368 get_disk_label(disk_name, info->path, PATH_MAX);
1369 snprintf(disk_label, sizeof(disk_label), "%s #%u", disk_name, (unsigned)i + 1);
1370 disks[i].flabel = strdup(disk_label);
1372 disks[i].internal_index = i;
1375 /* This is not an M3U file, so initial disk
1376 * image has not yet been set - attempt to
1378 if ((disk_initial_index > 0) &&
1379 (disk_initial_index < disk_count)) {
1380 const char *fname = disks[disk_initial_index].fname;
1382 if (fname && (*fname != '\0'))
1383 if (strcmp(disk_initial_path, fname) == 0)
1384 cd_index = disk_initial_index;
1389 CdromLabel[0] = '\0';
1391 cdrIsoMultidiskSelect = disks[cd_index].internal_index;
1392 disk_current_index = cd_index;
1393 set_cd_image(disks[cd_index].fname);
1395 if (ReloadCdromPlugin() < 0) {
1396 log_cb(RETRO_LOG_INFO, "failed to reload cdr plugins\n");
1399 if (CDR_open() < 0) {
1400 log_cb(RETRO_LOG_INFO, "failed to open cdr plugin\n");
1406 plugin_call_rearmed_cbs();
1409 if (CheckCdrom() == -1) {
1410 log_cb(RETRO_LOG_INFO, "unsupported/invalid CD image: %s\n", info->path);
1416 if (LoadCdrom() == -1) {
1417 log_cb(RETRO_LOG_INFO, "could not load CD\n");
1427 unsigned retro_get_region(void)
1429 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1432 void *retro_get_memory_data(unsigned id)
1434 if (id == RETRO_MEMORY_SAVE_RAM)
1436 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1442 size_t retro_get_memory_size(unsigned id)
1444 if (id == RETRO_MEMORY_SAVE_RAM)
1446 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1452 void retro_reset(void)
1454 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
1459 static const unsigned short retro_psx_map[] = {
1460 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
1461 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
1462 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
1463 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
1464 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
1465 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
1466 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
1467 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
1468 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
1469 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
1470 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
1471 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
1472 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
1473 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
1474 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
1475 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
1477 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
1479 //Percentage distance of screen to adjust
1480 static int GunconAdjustX = 0;
1481 static int GunconAdjustY = 0;
1483 //Used when out by a percentage
1484 static float GunconAdjustRatioX = 1;
1485 static float GunconAdjustRatioY = 1;
1487 static void update_variables(bool in_flight)
1489 struct retro_variable var;
1492 int gpu_peops_fix = 0;
1496 var.key = "pcsx_rearmed_frameskip";
1497 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1498 pl_rearmed_cbs.frameskip = atoi(var.value);
1501 var.key = "pcsx_rearmed_region";
1502 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1505 if (strcmp(var.value, "auto") == 0)
1507 else if (strcmp(var.value, "NTSC") == 0)
1509 else if (strcmp(var.value, "PAL") == 0)
1513 for (i = 0; i < PORTS_NUMBER; i++)
1514 update_controller_port_variable(i);
1519 var.key = "pcsx_rearmed_negcon_deadzone";
1520 negcon_deadzone = 0;
1521 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1523 negcon_deadzone = (int)(atoi(var.value) * 0.01f * NEGCON_RANGE);
1527 var.key = "pcsx_rearmed_negcon_response";
1528 negcon_linearity = 1;
1529 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1531 if (strcmp(var.value, "quadratic") == 0) {
1532 negcon_linearity = 2;
1533 } else if (strcmp(var.value, "cubic") == 0) {
1534 negcon_linearity = 3;
1539 var.key = "pcsx_rearmed_analog_axis_modifier";
1540 axis_bounds_modifier = true;
1541 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1543 if (strcmp(var.value, "square") == 0) {
1544 axis_bounds_modifier = true;
1545 } else if (strcmp(var.value, "circle") == 0) {
1546 axis_bounds_modifier = false;
1551 var.key = "pcsx_rearmed_vibration";
1553 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1555 if (strcmp(var.value, "disabled") == 0)
1556 in_enable_vibration = 0;
1557 else if (strcmp(var.value, "enabled") == 0)
1558 in_enable_vibration = 1;
1562 var.key = "pcsx_rearmed_dithering";
1564 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1566 if (strcmp(var.value, "disabled") == 0) {
1567 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
1568 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 0;
1569 pl_rearmed_cbs.gpu_unai.dithering = 0;
1571 pl_rearmed_cbs.gpu_neon.allow_dithering = 0;
1574 else if (strcmp(var.value, "enabled") == 0) {
1575 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1576 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 1;
1577 pl_rearmed_cbs.gpu_unai.dithering = 1;
1579 pl_rearmed_cbs.gpu_neon.allow_dithering = 1;
1586 var.key = "pcsx_rearmed_neon_interlace_enable";
1588 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1590 if (strcmp(var.value, "disabled") == 0)
1591 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
1592 else if (strcmp(var.value, "enabled") == 0)
1593 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
1597 var.key = "pcsx_rearmed_neon_enhancement_enable";
1599 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1601 if (strcmp(var.value, "disabled") == 0)
1602 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
1603 else if (strcmp(var.value, "enabled") == 0)
1604 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
1608 var.key = "pcsx_rearmed_neon_enhancement_no_main";
1610 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1612 if (strcmp(var.value, "disabled") == 0)
1613 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
1614 else if (strcmp(var.value, "enabled") == 0)
1615 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
1620 var.key = "pcsx_rearmed_duping_enable";
1622 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1624 if (strcmp(var.value, "disabled") == 0)
1625 duping_enable = false;
1626 else if (strcmp(var.value, "enabled") == 0)
1627 duping_enable = true;
1631 var.key = "pcsx_rearmed_display_internal_fps";
1633 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1635 if (strcmp(var.value, "disabled") == 0)
1636 display_internal_fps = false;
1637 else if (strcmp(var.value, "enabled") == 0)
1638 display_internal_fps = true;
1641 #if defined(LIGHTREC) || defined(NEW_DYNAREC)
1643 var.key = "pcsx_rearmed_drc";
1645 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1647 R3000Acpu *prev_cpu = psxCpu;
1648 #if defined(LIGHTREC)
1649 bool can_use_dynarec = found_bios;
1651 bool can_use_dynarec = 1;
1656 Config.Cpu = CPU_INTERPRETER;
1659 if (strcmp(var.value, "disabled") == 0 || !can_use_dynarec)
1660 Config.Cpu = CPU_INTERPRETER;
1661 else if (strcmp(var.value, "enabled") == 0)
1662 Config.Cpu = CPU_DYNAREC;
1664 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
1665 if (psxCpu != prev_cpu) {
1666 prev_cpu->Shutdown();
1668 psxCpu->Reset(); // not really a reset..
1671 #endif /* LIGHTREC || NEW_DYNAREC */
1674 var.key = "pcsx_rearmed_spu_reverb";
1676 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1678 if (strcmp(var.value, "disabled") == 0)
1679 spu_config.iUseReverb = false;
1680 else if (strcmp(var.value, "enabled") == 0)
1681 spu_config.iUseReverb = true;
1685 var.key = "pcsx_rearmed_spu_interpolation";
1687 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1689 if (strcmp(var.value, "simple") == 0)
1690 spu_config.iUseInterpolation = 1;
1691 else if (strcmp(var.value, "gaussian") == 0)
1692 spu_config.iUseInterpolation = 2;
1693 else if (strcmp(var.value, "cubic") == 0)
1694 spu_config.iUseInterpolation = 3;
1695 else if (strcmp(var.value, "off") == 0)
1696 spu_config.iUseInterpolation = 0;
1700 var.key = "pcsx_rearmed_pe2_fix";
1702 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1704 if (strcmp(var.value, "disabled") == 0)
1706 else if (strcmp(var.value, "enabled") == 0)
1711 var.key = "pcsx_rearmed_idiablofix";
1713 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1715 if (strcmp(var.value, "disabled") == 0)
1716 spu_config.idiablofix = 0;
1717 else if (strcmp(var.value, "enabled") == 0)
1718 spu_config.idiablofix = 1;
1722 var.key = "pcsx_rearmed_inuyasha_fix";
1724 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1726 if (strcmp(var.value, "disabled") == 0)
1728 else if (strcmp(var.value, "enabled") == 0)
1734 var.key = "pcsx_rearmed_async_cd";
1735 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1737 if (strcmp(var.value, "async") == 0)
1745 var.key = "pcsx_rearmed_noxadecoding";
1746 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1748 if (strcmp(var.value, "disabled") == 0)
1755 var.key = "pcsx_rearmed_nocdaudio";
1756 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1758 if (strcmp(var.value, "disabled") == 0)
1765 var.key = "pcsx_rearmed_spuirq";
1766 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1768 if (strcmp(var.value, "disabled") == 0)
1776 var.key = "pcsx_rearmed_nosmccheck";
1777 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1779 if (strcmp(var.value, "enabled") == 0)
1780 new_dynarec_hacks |= NDHACK_NO_SMC_CHECK;
1782 new_dynarec_hacks &= ~NDHACK_NO_SMC_CHECK;
1786 var.key = "pcsx_rearmed_gteregsunneeded";
1787 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1789 if (strcmp(var.value, "enabled") == 0)
1790 new_dynarec_hacks |= NDHACK_GTE_UNNEEDED;
1792 new_dynarec_hacks &= ~NDHACK_GTE_UNNEEDED;
1796 var.key = "pcsx_rearmed_nogteflags";
1797 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1799 if (strcmp(var.value, "enabled") == 0)
1800 new_dynarec_hacks |= NDHACK_GTE_NO_FLAGS;
1802 new_dynarec_hacks &= ~NDHACK_GTE_NO_FLAGS;
1804 #endif /* NEW_DYNAREC */
1808 var.key = "pcsx_rearmed_gpu_peops_odd_even_bit";
1810 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1812 if (strcmp(var.value, "enabled") == 0)
1813 gpu_peops_fix |= GPU_PEOPS_ODD_EVEN_BIT;
1817 var.key = "pcsx_rearmed_gpu_peops_expand_screen_width";
1819 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1821 if (strcmp(var.value, "enabled") == 0)
1822 gpu_peops_fix |= GPU_PEOPS_EXPAND_SCREEN_WIDTH;
1826 var.key = "pcsx_rearmed_gpu_peops_ignore_brightness";
1828 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1830 if (strcmp(var.value, "enabled") == 0)
1831 gpu_peops_fix |= GPU_PEOPS_IGNORE_BRIGHTNESS;
1835 var.key = "pcsx_rearmed_gpu_peops_disable_coord_check";
1837 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1839 if (strcmp(var.value, "enabled") == 0)
1840 gpu_peops_fix |= GPU_PEOPS_DISABLE_COORD_CHECK;
1844 var.key = "pcsx_rearmed_gpu_peops_lazy_screen_update";
1846 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1848 if (strcmp(var.value, "enabled") == 0)
1849 gpu_peops_fix |= GPU_PEOPS_LAZY_SCREEN_UPDATE;
1853 var.key = "pcsx_rearmed_gpu_peops_old_frame_skip";
1855 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1857 if (strcmp(var.value, "enabled") == 0)
1858 gpu_peops_fix |= GPU_PEOPS_OLD_FRAME_SKIP;
1862 var.key = "pcsx_rearmed_gpu_peops_repeated_triangles";
1864 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1866 if (strcmp(var.value, "enabled") == 0)
1867 gpu_peops_fix |= GPU_PEOPS_REPEATED_TRIANGLES;
1871 var.key = "pcsx_rearmed_gpu_peops_quads_with_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_QUADS_WITH_TRIANGLES;
1880 var.key = "pcsx_rearmed_gpu_peops_fake_busy_state";
1882 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1884 if (strcmp(var.value, "enabled") == 0)
1885 gpu_peops_fix |= GPU_PEOPS_FAKE_BUSY_STATE;
1888 if (pl_rearmed_cbs.gpu_peops.dwActFixes != gpu_peops_fix)
1889 pl_rearmed_cbs.gpu_peops.dwActFixes = gpu_peops_fix;
1891 /* Show/hide core options */
1893 var.key = "pcsx_rearmed_show_gpu_peops_settings";
1896 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1898 int show_advanced_gpu_peops_settings_prev = show_advanced_gpu_peops_settings;
1900 show_advanced_gpu_peops_settings = 1;
1901 if (strcmp(var.value, "disabled") == 0)
1902 show_advanced_gpu_peops_settings = 0;
1904 if (show_advanced_gpu_peops_settings != show_advanced_gpu_peops_settings_prev)
1907 struct retro_core_option_display option_display;
1908 char gpu_peops_option[9][45] = {
1909 "pcsx_rearmed_gpu_peops_odd_even_bit",
1910 "pcsx_rearmed_gpu_peops_expand_screen_width",
1911 "pcsx_rearmed_gpu_peops_ignore_brightness",
1912 "pcsx_rearmed_gpu_peops_disable_coord_check",
1913 "pcsx_rearmed_gpu_peops_lazy_screen_update",
1914 "pcsx_rearmed_gpu_peops_old_frame_skip",
1915 "pcsx_rearmed_gpu_peops_repeated_triangles",
1916 "pcsx_rearmed_gpu_peops_quads_with_triangles",
1917 "pcsx_rearmed_gpu_peops_fake_busy_state"
1920 option_display.visible = show_advanced_gpu_peops_settings;
1922 for (i = 0; i < 9; i++)
1924 option_display.key = gpu_peops_option[i];
1925 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
1932 var.key = "pcsx_rearmed_gpu_unai_ilace_force";
1935 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1937 if (strcmp(var.value, "disabled") == 0)
1938 pl_rearmed_cbs.gpu_unai.ilace_force = 0;
1939 else if (strcmp(var.value, "enabled") == 0)
1940 pl_rearmed_cbs.gpu_unai.ilace_force = 1;
1943 var.key = "pcsx_rearmed_gpu_unai_pixel_skip";
1946 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1948 if (strcmp(var.value, "disabled") == 0)
1949 pl_rearmed_cbs.gpu_unai.pixel_skip = 0;
1950 else if (strcmp(var.value, "enabled") == 0)
1951 pl_rearmed_cbs.gpu_unai.pixel_skip = 1;
1954 var.key = "pcsx_rearmed_gpu_unai_lighting";
1957 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1959 if (strcmp(var.value, "disabled") == 0)
1960 pl_rearmed_cbs.gpu_unai.lighting = 0;
1961 else if (strcmp(var.value, "enabled") == 0)
1962 pl_rearmed_cbs.gpu_unai.lighting = 1;
1965 var.key = "pcsx_rearmed_gpu_unai_fast_lighting";
1968 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1970 if (strcmp(var.value, "disabled") == 0)
1971 pl_rearmed_cbs.gpu_unai.fast_lighting = 0;
1972 else if (strcmp(var.value, "enabled") == 0)
1973 pl_rearmed_cbs.gpu_unai.fast_lighting = 1;
1976 var.key = "pcsx_rearmed_gpu_unai_blending";
1979 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1981 if (strcmp(var.value, "disabled") == 0)
1982 pl_rearmed_cbs.gpu_unai.blending = 0;
1983 else if (strcmp(var.value, "enabled") == 0)
1984 pl_rearmed_cbs.gpu_unai.blending = 1;
1987 var.key = "pcsx_rearmed_show_gpu_unai_settings";
1990 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1992 int show_advanced_gpu_unai_settings_prev = show_advanced_gpu_unai_settings;
1994 show_advanced_gpu_unai_settings = 1;
1995 if (strcmp(var.value, "disabled") == 0)
1996 show_advanced_gpu_unai_settings = 0;
1998 if (show_advanced_gpu_unai_settings != show_advanced_gpu_unai_settings_prev)
2001 struct retro_core_option_display option_display;
2002 char gpu_unai_option[5][40] = {
2003 "pcsx_rearmed_gpu_unai_blending",
2004 "pcsx_rearmed_gpu_unai_lighting",
2005 "pcsx_rearmed_gpu_unai_fast_lighting",
2006 "pcsx_rearmed_gpu_unai_ilace_force",
2007 "pcsx_rearmed_gpu_unai_pixel_skip"
2010 option_display.visible = show_advanced_gpu_unai_settings;
2012 for (i = 0; i < 5; i++)
2014 option_display.key = gpu_unai_option[i];
2015 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY, &option_display);
2021 //This adjustment process gives the user the ability to manually align the mouse up better
2022 //with where the shots are in the emulator.
2025 var.key = "pcsx_rearmed_gunconadjustx";
2027 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2029 GunconAdjustX = atoi(var.value);
2033 var.key = "pcsx_rearmed_gunconadjusty";
2035 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2037 GunconAdjustY = atoi(var.value);
2041 var.key = "pcsx_rearmed_gunconadjustratiox";
2043 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2045 GunconAdjustRatioX = atof(var.value);
2049 var.key = "pcsx_rearmed_gunconadjustratioy";
2051 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2053 GunconAdjustRatioY = atof(var.value);
2057 // inform core things about possible config changes
2058 plugin_call_rearmed_cbs();
2060 if (GPU_open != NULL && GPU_close != NULL) {
2062 GPU_open(&gpuDisp, "PCSX", NULL);
2071 //bootlogo display hack
2074 var.key = "pcsx_rearmed_show_bios_bootlogo";
2075 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2077 Config.SlowBoot = 0;
2079 if (strcmp(var.value, "enabled") == 0)
2081 Config.SlowBoot = 1;
2088 var.key = "pcsx_rearmed_psxclock";
2089 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2091 int psxclock = atoi(var.value);
2092 cycle_multiplier = 10000 / psxclock;
2098 // Taken from beetle-psx-libretro
2099 static uint16_t get_analog_button(int16_t ret, retro_input_state_t input_state_cb, int player_index, int id)
2101 // NOTE: Analog buttons were added Nov 2017. Not all front-ends support this
2102 // feature (or pre-date it) so we need to handle this in a graceful way.
2104 // First, try and get an analog value using the new libretro API constant
2105 uint16_t button = input_state_cb(player_index,
2106 RETRO_DEVICE_ANALOG,
2107 RETRO_DEVICE_INDEX_ANALOG_BUTTON,
2109 button = MIN(button / 128, 255);
2113 // If we got exactly zero, we're either not pressing the button, or the front-end
2114 // is not reporting analog values. We need to do a second check using the classic
2115 // digital API method, to at least get some response - better than nothing.
2117 // NOTE: If we're really just not holding the button, we're still going to get zero.
2119 button = (ret & (1 << id)) ? 255 : 0;
2125 unsigned char axis_range_modifier(int16_t axis_value, bool is_square) {
2126 float modifier_axis_range = 0;
2129 modifier_axis_range = round((axis_value >> 8) / 0.785) + 128;
2130 if(modifier_axis_range < 0) {
2131 modifier_axis_range = 0;
2132 } else if(modifier_axis_range > 255) {
2133 modifier_axis_range = 255;
2136 modifier_axis_range = MIN(((axis_value >> 8) + 128), 255);
2139 return modifier_axis_range;
2142 void retro_run(void)
2145 //SysReset must be run while core is running,Not in menu (Locks up Retroarch)
2146 if (rebootemu != 0) {
2149 if (!Config.HLE && !Config.SlowBoot) {
2151 psxRegs.pc = psxRegs.GPR.n.ra;
2155 if (display_internal_fps) {
2158 if (frame_count % INTERNAL_FPS_SAMPLE_PERIOD == 0) {
2159 unsigned internal_fps = pl_rearmed_cbs.flip_cnt * (is_pal_mode ? 50 : 60) / INTERNAL_FPS_SAMPLE_PERIOD;
2161 const char *strc = (const char*)str;
2162 struct retro_message msg =
2170 snprintf(str, sizeof(str), "Internal FPS: %2d", internal_fps);
2172 pl_rearmed_cbs.flip_cnt = 0;
2174 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
2182 bool updated = false;
2183 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
2184 update_variables(true);
2186 // reset all keystate, query libretro for keystate
2190 float negcon_twist_amplitude;
2194 for(i = 0; i < PORTS_NUMBER; i++)
2199 if (in_type[i] == PSE_PAD_TYPE_NONE)
2202 if (libretro_supports_bitmasks)
2203 ret = input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
2207 for (j = 0; j < (RETRO_DEVICE_ID_JOYPAD_R3+1); j++)
2209 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j))
2214 if (in_type[i] == PSE_PAD_TYPE_GUNCON)
2216 //ToDo move across to:
2217 //RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X
2218 //RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y
2219 //RETRO_DEVICE_ID_LIGHTGUN_TRIGGER
2220 //RETRO_DEVICE_ID_LIGHTGUN_RELOAD
2221 //RETRO_DEVICE_ID_LIGHTGUN_AUX_A
2222 //RETRO_DEVICE_ID_LIGHTGUN_AUX_B
2223 //Though not sure these are hooked up properly on the Pi
2226 //Put the controller index back to i instead of hardcoding to 1 when the libretro overlay crash bug is fixed
2227 //This is required for 2 player
2229 //GUNCON has 3 controls, Trigger,A,B which equal Circle,Start,Cross
2232 //The 1 is hardcoded instead of i to prevent the overlay mouse button libretro crash bug
2233 if (input_state_cb(1, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_LEFT)){
2234 in_keystate[i] |= (1 << DKEY_CIRCLE);
2238 if (input_state_cb(1, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_RIGHT)){
2239 in_keystate[i] |= (1 << DKEY_START);
2243 if (input_state_cb(1, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_MIDDLE)){
2244 in_keystate[i] |= (1 << DKEY_CROSS);
2247 //The 1 is hardcoded instead of i to prevent the overlay mouse button libretro crash bug
2248 int gunx = input_state_cb(1, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_X);
2249 int guny = input_state_cb(1, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_Y);
2251 //Mouse range is -32767 -> 32767
2253 //Use the left analog stick field to store the absolute coordinates
2254 in_analog_left[0][0] = (gunx*GunconAdjustRatioX) + (GunconAdjustX * 655);
2255 in_analog_left[0][1] = (guny*GunconAdjustRatioY) + (GunconAdjustY * 655);
2257 if (in_type[i] == PSE_PAD_TYPE_NEGCON)
2259 // Query digital inputs
2262 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_UP))
2263 in_keystate[i] |= (1 << DKEY_UP);
2265 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT))
2266 in_keystate[i] |= (1 << DKEY_RIGHT);
2268 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN))
2269 in_keystate[i] |= (1 << DKEY_DOWN);
2271 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT))
2272 in_keystate[i] |= (1 << DKEY_LEFT);
2274 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_START))
2275 in_keystate[i] |= (1 << DKEY_START);
2277 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_A))
2278 in_keystate[i] |= (1 << DKEY_CIRCLE);
2280 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_X))
2281 in_keystate[i] |= (1 << DKEY_TRIANGLE);
2282 // > neGcon R shoulder (digital)
2283 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_R))
2284 in_keystate[i] |= (1 << DKEY_R1);
2285 // Query analog inputs
2287 // From studying 'libpcsxcore/plugins.c' and 'frontend/plugin.c':
2288 // >> pad->leftJoyX == in_analog_left[i][0] == NeGcon II
2289 // >> pad->leftJoyY == in_analog_left[i][1] == NeGcon L
2290 // >> pad->rightJoyX == in_analog_right[i][0] == NeGcon twist
2291 // >> pad->rightJoyY == in_analog_right[i][1] == NeGcon I
2292 // So we just have to map in_analog_left/right to more
2293 // appropriate inputs...
2296 // >> Get raw analog stick value and account for deadzone
2297 lsx = input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
2298 if (lsx > negcon_deadzone)
2299 lsx = lsx - negcon_deadzone;
2300 else if (lsx < -negcon_deadzone)
2301 lsx = lsx + negcon_deadzone;
2304 // >> Convert to an 'amplitude' [-1.0,1.0] and adjust response
2305 negcon_twist_amplitude = (float)lsx / (float)(NEGCON_RANGE - negcon_deadzone);
2306 if (negcon_linearity == 2)
2308 if (negcon_twist_amplitude < 0.0)
2309 negcon_twist_amplitude = -(negcon_twist_amplitude * negcon_twist_amplitude);
2311 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude;
2313 else if (negcon_linearity == 3)
2314 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude * negcon_twist_amplitude;
2315 // >> Convert to final 'in_analog' integer value [0,255]
2316 in_analog_right[i][0] = MAX(MIN((int)(negcon_twist_amplitude * 128.0f) + 128, 255), 0);
2318 // >> Handle right analog stick vertical axis mapping...
2319 // - Up (-Y) == accelerate == neGcon I
2320 // - Down (+Y) == brake == neGcon II
2323 rsy = input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
2325 // Account for deadzone
2326 // (Note: have never encountered a gamepad with significant differences
2327 // in deadzone between left/right analog sticks, so use the regular 'twist'
2329 if (rsy > negcon_deadzone)
2330 rsy = rsy - negcon_deadzone;
2333 // Convert to 'in_analog' integer value [0,255]
2334 negcon_ii_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2336 if (rsy < -negcon_deadzone)
2337 rsy = -1 * (rsy + negcon_deadzone);
2340 negcon_i_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2343 in_analog_right[i][1] = MAX(
2345 get_analog_button(ret, input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_R2),
2346 get_analog_button(ret, input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_B)
2351 in_analog_left[i][0] = MAX(
2353 get_analog_button(ret, input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_L2),
2354 get_analog_button(ret, input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_Y)
2359 in_analog_left[i][1] = get_analog_button(ret, input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_L);
2361 if (in_type[i] != PSE_PAD_TYPE_NEGCON && in_type[i] != PSE_PAD_TYPE_GUNCON)
2363 // Query digital inputs
2364 for (j = 0; j < RETRO_PSX_MAP_LEN; j++)
2366 in_keystate[i] |= retro_psx_map[j];
2368 // Query analog inputs
2369 if (in_type[i] == PSE_PAD_TYPE_ANALOGJOY || in_type[i] == PSE_PAD_TYPE_ANALOGPAD)
2371 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);
2372 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);
2373 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);
2374 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);
2382 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf_ptr : NULL,
2383 vout_width, vout_height, vout_width * 2);
2389 static bool try_use_bios(const char *path)
2395 f = fopen(path, "rb");
2399 fseek(f, 0, SEEK_END);
2403 if (size != 512 * 1024)
2406 name = strrchr(path, SLASH);
2409 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
2414 #include <sys/types.h>
2417 static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
2423 dir = opendir(dirpath);
2427 while ((ent = readdir(dir))) {
2428 if ((strncasecmp(ent->d_name, "scph", 4) != 0) && (strncasecmp(ent->d_name, "psx", 3) != 0))
2431 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
2432 ret = try_use_bios(path);
2440 #define find_any_bios(...) false
2443 static void check_system_specs(void)
2446 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
2449 static int init_memcards(void)
2453 struct retro_variable var = { .key="pcsx_rearmed_memcard2", .value=NULL };
2454 static const char CARD2_FILE[] = "pcsx-card2.mcd";
2456 // Memcard2 will be handled and is re-enabled if needed using core
2458 // Memcard1 is handled by libretro, doing this will set core to
2459 // skip file io operations for memcard1 like SaveMcd
2460 snprintf(Config.Mcd1, sizeof(Config.Mcd1), "none");
2461 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "none");
2462 init_memcard(Mcd1Data);
2463 // Memcard 2 is managed by the emulator on the filesystem,
2464 // There is no need to initialize Mcd2Data like Mcd1Data.
2466 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
2467 SysPrintf("Memcard 2: %s\n", var.value);
2468 if (memcmp(var.value, "enabled", 7) == 0) {
2469 if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir) {
2470 if (strlen(dir) + strlen(CARD2_FILE) + 2 > sizeof(Config.Mcd2)) {
2471 SysPrintf("Path '%s' is too long. Cannot use memcard 2. Use a shorter path.\n", dir);
2475 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "%s/%s", dir, CARD2_FILE);
2476 SysPrintf("Use memcard 2: %s\n", Config.Mcd2);
2479 SysPrintf("Could not get save directory! Could not create memcard 2.");
2487 static void loadPSXBios(void)
2490 char path[PATH_MAX];
2491 unsigned useHLE = 0;
2493 const char *bios[] = {
2494 "PSXONPSP660", "psxonpsp660",
2495 "SCPH101", "scph101",
2496 "SCPH5501", "scph5501",
2497 "SCPH7001", "scph7001",
2498 "SCPH1001", "scph1001"
2501 struct retro_variable var = {
2502 .key = "pcsx_rearmed_bios",
2508 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
2509 if (!strcmp(var.value, "HLE"))
2515 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
2518 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s", dir);
2520 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
2521 snprintf(path, sizeof(path), "%s%c%s.bin", dir, SLASH, bios[i]);
2522 found_bios = try_use_bios(path);
2528 found_bios = find_any_bios(dir, path, sizeof(path));
2531 SysPrintf("found BIOS file: %s\n", Config.Bios);
2535 if (useHLE || !found_bios)
2537 SysPrintf("no BIOS files found.\n");
2538 struct retro_message msg =
2540 "No PlayStation BIOS file found - add for better compatibility",
2543 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, (void*)&msg);
2547 void retro_init(void)
2549 unsigned dci_version = 0;
2550 struct retro_rumble_interface rumble;
2554 // magic sauce to make the dynarec work on iOS
2555 syscall(SYS_ptrace, 0 /*PTRACE_TRACEME*/, 0, 0, 0);
2559 psxMapHook = pl_3ds_mmap;
2560 psxUnmapHook = pl_3ds_munmap;
2563 if(init_vita_mmap()<0)
2565 psxMapHook = pl_vita_mmap;
2566 psxUnmapHook = pl_vita_munmap;
2568 ret = emu_core_preinit();
2570 /* emu_core_preinit sets the cpu to dynarec */
2572 Config.Cpu = CPU_INTERPRETER;
2574 ret |= init_memcards();
2576 ret |= emu_core_init();
2578 SysPrintf("PCSX init failed.\n");
2583 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
2584 #elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && !defined(VITA) && !defined(__SWITCH__)
2585 posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
2587 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
2590 vout_buf_ptr = vout_buf;
2594 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
2596 disk_initial_index = 0;
2597 disk_initial_path[0] = '\0';
2598 if (environ_cb(RETRO_ENVIRONMENT_GET_DISK_CONTROL_INTERFACE_VERSION, &dci_version) && (dci_version >= 1))
2599 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_EXT_INTERFACE, &disk_control_ext);
2601 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
2604 if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble))
2605 rumble_cb = rumble.set_rumble_state;
2607 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
2608 * we have to do this because cache misses and some IO penalties
2609 * are not emulated. Warning: changing this may break compatibility. */
2610 cycle_multiplier = 175;
2611 #ifdef HAVE_PRE_ARMV7
2612 cycle_multiplier = 200;
2614 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
2615 pl_rearmed_cbs.gpu_peops.dwActFixes = GPU_PEOPS_OLD_FRAME_SKIP;
2616 spu_config.iUseFixedUpdates = 1;
2618 SaveFuncs.open = save_open;
2619 SaveFuncs.read = save_read;
2620 SaveFuncs.write = save_write;
2621 SaveFuncs.seek = save_seek;
2622 SaveFuncs.close = save_close;
2624 if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL))
2625 libretro_supports_bitmasks = true;
2627 check_system_specs();
2630 void retro_deinit(void)
2632 if (plugins_opened) {
2638 linearFree(vout_buf);
2647 libretro_supports_bitmasks = false;
2649 /* Have to reset disks struct, otherwise
2650 * fnames/flabels will leak memory */
2655 #include <psp2/kernel/threadmgr.h>
2656 int usleep (unsigned long us)
2658 sceKernelDelayThread(us);
2662 void SysPrintf(const char *fmt, ...) {
2666 va_start(list, fmt);
2667 vsprintf(msg, fmt, list);
2671 log_cb(RETRO_LOG_INFO, "%s", msg);