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"
44 #ifdef USE_LIBRETRO_VFS
45 #include <streams/file_stream_transforms.h>
49 #include "3ds/3ds_utils.h"
52 #define PORTS_NUMBER 8
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
59 #define MAX(a, b) ((a) > (b) ? (a) : (b))
62 #define ISHEXDEC ((buf[cursor] >= '0') && (buf[cursor] <= '9')) || ((buf[cursor] >= 'a') && (buf[cursor] <= 'f')) || ((buf[cursor] >= 'A') && (buf[cursor] <= 'F'))
64 #define INTERNAL_FPS_SAMPLE_PERIOD 64
66 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
67 static int rebootemu = 0;
69 static retro_video_refresh_t video_cb;
70 static retro_input_poll_t input_poll_cb;
71 static retro_input_state_t input_state_cb;
72 static retro_environment_t environ_cb;
73 static retro_audio_sample_batch_t audio_batch_cb;
74 static retro_set_rumble_state_t rumble_cb;
75 static struct retro_log_callback logging;
76 static retro_log_printf_t log_cb;
78 static unsigned msg_interface_version = 0;
80 static void *vout_buf;
81 static void *vout_buf_ptr;
82 static int vout_width, vout_height;
83 static int vout_fb_dirty;
84 static bool vout_can_dupe;
85 static bool duping_enable;
86 static bool found_bios;
87 static bool display_internal_fps = false;
88 static unsigned frame_count = 0;
89 static bool libretro_supports_bitmasks = false;
90 static bool libretro_supports_option_categories = false;
91 static bool show_input_settings = true;
93 static bool show_advanced_gpu_peops_settings = true;
96 static bool show_advanced_gpu_unai_settings = true;
98 static float mouse_sensitivity = 1.0f;
104 FRAMESKIP_AUTO_THRESHOLD,
105 FRAMESKIP_FIXED_INTERVAL
108 static unsigned frameskip_type = FRAMESKIP_NONE;
109 static unsigned frameskip_threshold = 0;
110 static unsigned frameskip_interval = 0;
111 static unsigned frameskip_counter = 0;
113 static int retro_audio_buff_active = false;
114 static unsigned retro_audio_buff_occupancy = 0;
115 static int retro_audio_buff_underrun = false;
117 static unsigned retro_audio_latency = 0;
118 static int update_audio_latency = false;
120 static unsigned previous_width = 0;
121 static unsigned previous_height = 0;
123 static int plugins_opened;
124 static int is_pal_mode;
126 /* memory card data */
127 extern char Mcd1Data[MCD_SIZE];
128 extern char Mcd2Data[MCD_SIZE];
129 extern char McdDisable[2];
131 /* PCSX ReARMed core calls and stuff */
133 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
134 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
135 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
136 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE
138 int in_analog_left[8][2] = { { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 } };
139 int in_analog_right[8][2] = { { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 } };
140 unsigned short in_keystate[PORTS_NUMBER];
144 int in_enable_vibration = 1;
146 // NegCon adjustment parameters
147 // > The NegCon 'twist' action is somewhat awkward when mapped
148 // to a standard analog stick -> user should be able to tweak
149 // response/deadzone for comfort
150 // > When response is linear, 'additional' deadzone (set here)
151 // may be left at zero, since this is normally handled via in-game
153 // > When response is non-linear, deadzone should be set to match the
154 // controller being used (otherwise precision may be lost)
155 // > negcon_linearity:
156 // - 1: Response is linear - recommended when using racing wheel
157 // peripherals, not recommended for standard gamepads
158 // - 2: Response is quadratic - optimal setting for gamepads
159 // - 3: Response is cubic - enables precise fine control, but
160 // difficult to use...
161 #define NEGCON_RANGE 0x7FFF
162 static int negcon_deadzone = 0;
163 static int negcon_linearity = 1;
165 static bool axis_bounds_modifier;
167 /* PSX max resolution is 640x512, but with enhancement it's 1024x512 */
168 #define VOUT_MAX_WIDTH 1024
169 #define VOUT_MAX_HEIGHT 512
172 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info) { return false; }
173 void retro_unload_game(void) {}
174 static int vout_open(void) { return 0; }
175 static void vout_close(void) {}
176 static int snd_init(void) { return 0; }
177 static void snd_finish(void) {}
178 static int snd_busy(void) { return 0; }
180 #define GPU_PEOPS_ODD_EVEN_BIT (1 << 0)
181 #define GPU_PEOPS_EXPAND_SCREEN_WIDTH (1 << 1)
182 #define GPU_PEOPS_IGNORE_BRIGHTNESS (1 << 2)
183 #define GPU_PEOPS_DISABLE_COORD_CHECK (1 << 3)
184 #define GPU_PEOPS_LAZY_SCREEN_UPDATE (1 << 6)
185 #define GPU_PEOPS_OLD_FRAME_SKIP (1 << 7)
186 #define GPU_PEOPS_REPEATED_TRIANGLES (1 << 8)
187 #define GPU_PEOPS_QUADS_WITH_TRIANGLES (1 << 9)
188 #define GPU_PEOPS_FAKE_BUSY_STATE (1 << 10)
190 static void init_memcard(char *mcd_data)
195 memset(mcd_data, 0, MCD_SIZE);
197 mcd_data[off++] = 'M';
198 mcd_data[off++] = 'C';
200 mcd_data[off++] = 0x0e;
202 for (i = 0; i < 15; i++)
204 mcd_data[off++] = 0xa0;
206 mcd_data[off++] = 0xff;
207 mcd_data[off++] = 0xff;
209 mcd_data[off++] = 0xa0;
212 for (i = 0; i < 20; i++)
214 mcd_data[off++] = 0xff;
215 mcd_data[off++] = 0xff;
216 mcd_data[off++] = 0xff;
217 mcd_data[off++] = 0xff;
219 mcd_data[off++] = 0xff;
220 mcd_data[off++] = 0xff;
225 static void set_vout_fb()
227 struct retro_framebuffer fb = { 0 };
229 fb.width = vout_width;
230 fb.height = vout_height;
231 fb.access_flags = RETRO_MEMORY_ACCESS_WRITE;
233 if (environ_cb(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &fb) && fb.format == RETRO_PIXEL_FORMAT_RGB565)
234 vout_buf_ptr = (uint16_t *)fb.data;
236 vout_buf_ptr = vout_buf;
239 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
244 if (previous_width != vout_width || previous_height != vout_height)
246 previous_width = vout_width;
247 previous_height = vout_height;
249 struct retro_system_av_info info;
250 retro_get_system_av_info(&info);
251 environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &info.geometry);
257 #ifndef FRONTEND_SUPPORTS_RGB565
258 static void convert(void *buf, size_t bytes)
260 unsigned int i, v, *p = buf;
262 for (i = 0; i < bytes / 4; i++)
265 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
270 static void vout_flip(const void *vram, int stride, int bgr24,
271 int x, int y, int w, int h, int dims_changed)
273 unsigned short *dest = vout_buf_ptr;
274 const unsigned short *src = vram;
275 int dstride = vout_width, h1 = h;
277 if (vram == NULL || dims_changed)
279 memset(vout_buf_ptr, 0, dstride * vout_height * 2);
285 dest += x + y * dstride;
289 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
290 for (; h1-- > 0; dest += dstride, src += stride)
292 bgr888_to_rgb565(dest, src, w * 3);
297 for (; h1-- > 0; dest += dstride, src += stride)
299 bgr555_to_rgb565(dest, src, w * 2);
304 #ifndef FRONTEND_SUPPORTS_RGB565
305 convert(vout_buf_ptr, vout_width * vout_height * 2);
308 pl_rearmed_cbs.flip_cnt++;
320 psx_map_t custom_psx_maps[] = {
321 { NULL, 0x13000000, 0x210000, MAP_TAG_RAM }, // 0x80000000
322 { NULL, 0x12800000, 0x010000, MAP_TAG_OTHER }, // 0x1f800000
323 { NULL, 0x12c00000, 0x080000, MAP_TAG_OTHER }, // 0x1fc00000
324 { NULL, 0x11000000, 0x800000, MAP_TAG_LUTS }, // 0x08000000
325 { NULL, 0x12000000, 0x200000, MAP_TAG_VRAM }, // 0x00000000
328 void *pl_3ds_mmap(unsigned long addr, size_t size, int is_fixed,
336 psx_map_t *custom_map = custom_psx_maps;
338 for (; custom_map->size; custom_map++)
340 if ((custom_map->size == size) && (custom_map->tag == tag))
342 uint32_t ptr_aligned, tmp;
344 custom_map->buffer = malloc(size + 0x1000);
345 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
347 if (svcControlMemory(&tmp, (void *)custom_map->target_map, (void *)ptr_aligned, size, MEMOP_MAP, 0x3) < 0)
349 SysPrintf("could not map memory @0x%08X\n", custom_map->target_map);
353 return (void *)custom_map->target_map;
361 void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
367 psx_map_t *custom_map = custom_psx_maps;
369 for (; custom_map->size; custom_map++)
371 if ((custom_map->target_map == (uint32_t)ptr))
373 uint32_t ptr_aligned, tmp;
375 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
377 svcControlMemory(&tmp, (void *)custom_map->target_map, (void *)ptr_aligned, size, MEMOP_UNMAP, 0x3);
379 free(custom_map->buffer);
380 custom_map->buffer = NULL;
401 psx_map_t custom_psx_maps[] = {
402 { NULL, NULL, 0x210000, MAP_TAG_RAM }, // 0x80000000
403 { NULL, NULL, 0x010000, MAP_TAG_OTHER }, // 0x1f800000
404 { NULL, NULL, 0x080000, MAP_TAG_OTHER }, // 0x1fc00000
405 { NULL, NULL, 0x800000, MAP_TAG_LUTS }, // 0x08000000
406 { NULL, NULL, 0x200000, MAP_TAG_VRAM }, // 0x00000000
413 addr = malloc(64 * 1024 * 1024);
416 tmpaddr = ((u32)(addr + 0xFFFFFF)) & ~0xFFFFFF;
417 custom_psx_maps[0].buffer = tmpaddr + 0x2000000;
418 custom_psx_maps[1].buffer = tmpaddr + 0x1800000;
419 custom_psx_maps[2].buffer = tmpaddr + 0x1c00000;
420 custom_psx_maps[3].buffer = tmpaddr + 0x0000000;
421 custom_psx_maps[4].buffer = tmpaddr + 0x1000000;
423 for(n = 0; n < 5; n++){
424 sceClibPrintf("addr reserved %x\n",custom_psx_maps[n].buffer);
430 void deinit_vita_mmap()
435 void *pl_vita_mmap(unsigned long addr, size_t size, int is_fixed,
441 psx_map_t *custom_map = custom_psx_maps;
443 for (; custom_map->size; custom_map++)
445 if ((custom_map->size == size) && (custom_map->tag == tag))
447 return custom_map->buffer;
454 void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag)
458 psx_map_t *custom_map = custom_psx_maps;
460 for (; custom_map->size; custom_map++)
462 if ((custom_map->buffer == ptr))
472 static void *pl_mmap(unsigned int size)
474 return psxMap(0, size, 0, MAP_TAG_VRAM);
477 static void pl_munmap(void *ptr, unsigned int size)
479 psxUnmap(ptr, size, MAP_TAG_VRAM);
482 struct rearmed_cbs pl_rearmed_cbs = {
483 .pl_vout_open = vout_open,
484 .pl_vout_set_mode = vout_set_mode,
485 .pl_vout_flip = vout_flip,
486 .pl_vout_close = vout_close,
489 /* from psxcounters */
490 .gpu_hcnt = &hSyncCount,
491 .gpu_frame_count = &frame_counter,
494 void pl_frame_limit(void)
496 /* called once per frame, make psxCpu->Execute() above return */
500 void pl_timing_prepare(int is_pal)
502 is_pal_mode = is_pal;
505 void plat_trigger_vibrate(int pad, int low, int high)
510 if (in_enable_vibration)
512 rumble_cb(pad, RETRO_RUMBLE_STRONG, high << 8);
513 rumble_cb(pad, RETRO_RUMBLE_WEAK, low ? 0xffff : 0x0);
517 void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
521 void pl_gun_byte2(int port, unsigned char byte)
526 static void snd_feed(void *buf, int bytes)
528 if (audio_batch_cb != NULL)
529 audio_batch_cb(buf, bytes / 4);
532 void out_register_libretro(struct out_driver *drv)
534 drv->name = "libretro";
535 drv->init = snd_init;
536 drv->finish = snd_finish;
537 drv->busy = snd_busy;
538 drv->feed = snd_feed;
541 #define RETRO_DEVICE_PSE_STANDARD RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0)
542 #define RETRO_DEVICE_PSE_ANALOG RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 0)
543 #define RETRO_DEVICE_PSE_DUALSHOCK RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 1)
544 #define RETRO_DEVICE_PSE_NEGCON RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 2)
545 #define RETRO_DEVICE_PSE_GUNCON RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_LIGHTGUN, 0)
546 #define RETRO_DEVICE_PSE_MOUSE RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_MOUSE, 0)
548 static char *get_pse_pad_label[] = {
549 "none", "mouse", "negcon", "konami gun", "standard", "analog", "guncon", "dualshock"
552 static const struct retro_controller_description pads[7] =
554 { "standard", RETRO_DEVICE_JOYPAD },
555 { "analog", RETRO_DEVICE_PSE_ANALOG },
556 { "dualshock", RETRO_DEVICE_PSE_DUALSHOCK },
557 { "negcon", RETRO_DEVICE_PSE_NEGCON },
558 { "guncon", RETRO_DEVICE_PSE_GUNCON },
559 { "mouse", RETRO_DEVICE_PSE_MOUSE },
563 static const struct retro_controller_info ports[9] =
578 static bool update_option_visibility(void)
580 struct retro_variable var = {0};
581 struct retro_core_option_display option_display = {0};
582 bool updated = false;
585 /* If frontend supports core option categories
586 * then show/hide core option entries are ignored
587 * and no options should be hidden */
588 if (libretro_supports_option_categories)
591 var.key = "pcsx_rearmed_show_input_settings";
594 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
596 bool show_input_settings_prev =
599 show_input_settings = true;
600 if (strcmp(var.value, "disabled") == 0)
601 show_input_settings = false;
603 if (show_input_settings !=
604 show_input_settings_prev)
606 char input_option[][50] = {
607 "pcsx_rearmed_analog_axis_modifier",
608 "pcsx_rearmed_vibration",
609 "pcsx_rearmed_multitap",
610 "pcsx_rearmed_negcon_deadzone",
611 "pcsx_rearmed_negcon_response",
612 "pcsx_rearmed_input_sensitivity",
613 "pcsx_rearmed_gunconadjustx",
614 "pcsx_rearmed_gunconadjusty",
615 "pcsx_rearmed_gunconadjustratiox",
616 "pcsx_rearmed_gunconadjustratioy"
619 option_display.visible = show_input_settings;
622 i < (sizeof(input_option) /
623 sizeof(input_option[0]));
626 option_display.key = input_option[i];
627 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
635 var.key = "pcsx_rearmed_show_gpu_peops_settings";
638 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
640 bool show_advanced_gpu_peops_settings_prev =
641 show_advanced_gpu_peops_settings;
643 show_advanced_gpu_peops_settings = true;
644 if (strcmp(var.value, "disabled") == 0)
645 show_advanced_gpu_peops_settings = false;
647 if (show_advanced_gpu_peops_settings !=
648 show_advanced_gpu_peops_settings_prev)
651 struct retro_core_option_display option_display;
652 char gpu_peops_option[][45] = {
653 "pcsx_rearmed_gpu_peops_odd_even_bit",
654 "pcsx_rearmed_gpu_peops_expand_screen_width",
655 "pcsx_rearmed_gpu_peops_ignore_brightness",
656 "pcsx_rearmed_gpu_peops_disable_coord_check",
657 "pcsx_rearmed_gpu_peops_lazy_screen_update",
658 "pcsx_rearmed_gpu_peops_repeated_triangles",
659 "pcsx_rearmed_gpu_peops_quads_with_triangles",
660 "pcsx_rearmed_gpu_peops_fake_busy_state"
663 option_display.visible = show_advanced_gpu_peops_settings;
666 i < (sizeof(gpu_peops_option) /
667 sizeof(gpu_peops_option[0]));
670 option_display.key = gpu_peops_option[i];
671 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
680 var.key = "pcsx_rearmed_show_gpu_unai_settings";
683 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
685 bool show_advanced_gpu_unai_settings_prev =
686 show_advanced_gpu_unai_settings;
688 show_advanced_gpu_unai_settings = true;
689 if (strcmp(var.value, "disabled") == 0)
690 show_advanced_gpu_unai_settings = false;
692 if (show_advanced_gpu_unai_settings !=
693 show_advanced_gpu_unai_settings_prev)
696 struct retro_core_option_display option_display;
697 char gpu_unai_option[][40] = {
698 "pcsx_rearmed_gpu_unai_blending",
699 "pcsx_rearmed_gpu_unai_lighting",
700 "pcsx_rearmed_gpu_unai_fast_lighting",
701 "pcsx_rearmed_gpu_unai_scale_hires",
704 option_display.visible = show_advanced_gpu_unai_settings;
707 i < (sizeof(gpu_unai_option) /
708 sizeof(gpu_unai_option[0]));
711 option_display.key = gpu_unai_option[i];
712 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
723 void retro_set_environment(retro_environment_t cb)
725 bool option_categories = false;
726 #ifdef USE_LIBRETRO_VFS
727 struct retro_vfs_interface_info vfs_iface_info;
732 if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
733 log_cb = logging.log;
735 environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
738 * An annoyance: retro_set_environment() can be called
739 * multiple times, and depending upon the current frontend
740 * state various environment callbacks may be disabled.
741 * This means the reported 'categories_supported' status
742 * may change on subsequent iterations. We therefore have
743 * to record whether 'categories_supported' is true on any
744 * iteration, and latch the result */
745 libretro_set_core_options(environ_cb, &option_categories);
746 libretro_supports_option_categories |= option_categories;
748 /* If frontend supports core option categories,
749 * any show/hide core option entries are unused
750 * and should be hidden */
751 if (libretro_supports_option_categories)
753 struct retro_core_option_display option_display;
754 option_display.visible = false;
756 option_display.key = "pcsx_rearmed_show_input_settings";
757 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
761 option_display.key = "pcsx_rearmed_show_gpu_peops_settings";
762 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
766 option_display.key = "pcsx_rearmed_show_gpu_unai_settings";
767 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
771 /* If frontend does not support core option
772 * categories, core options may be shown/hidden
773 * at runtime. In this case, register 'update
774 * display' callback, so frontend can update
775 * core options menu without calling retro_run() */
778 struct retro_core_options_update_display_callback update_display_cb;
779 update_display_cb.callback = update_option_visibility;
781 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_UPDATE_DISPLAY_CALLBACK,
785 #ifdef USE_LIBRETRO_VFS
786 vfs_iface_info.required_interface_version = 1;
787 vfs_iface_info.iface = NULL;
788 if (environ_cb(RETRO_ENVIRONMENT_GET_VFS_INTERFACE, &vfs_iface_info))
789 filestream_vfs_init(&vfs_iface_info);
793 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
794 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
795 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
796 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
797 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
799 unsigned retro_api_version(void)
801 return RETRO_API_VERSION;
804 static void update_multitap(void)
806 struct retro_variable var = { 0 };
812 var.key = "pcsx_rearmed_multitap";
813 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value))
815 if (strcmp(var.value, "port 1") == 0)
817 else if (strcmp(var.value, "port 2") == 0)
819 else if (strcmp(var.value, "ports 1 and 2") == 0)
827 void retro_set_controller_port_device(unsigned port, unsigned device)
829 if (port >= PORTS_NUMBER)
834 case RETRO_DEVICE_JOYPAD:
835 case RETRO_DEVICE_PSE_STANDARD:
836 in_type[port] = PSE_PAD_TYPE_STANDARD;
838 case RETRO_DEVICE_PSE_ANALOG:
839 in_type[port] = PSE_PAD_TYPE_ANALOGJOY;
841 case RETRO_DEVICE_PSE_DUALSHOCK:
842 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
844 case RETRO_DEVICE_PSE_MOUSE:
845 in_type[port] = PSE_PAD_TYPE_MOUSE;
847 case RETRO_DEVICE_PSE_NEGCON:
848 in_type[port] = PSE_PAD_TYPE_NEGCON;
850 case RETRO_DEVICE_PSE_GUNCON:
851 in_type[port] = PSE_PAD_TYPE_GUNCON;
853 case RETRO_DEVICE_NONE:
855 in_type[port] = PSE_PAD_TYPE_NONE;
859 SysPrintf("port: %u device: %s\n", port + 1, get_pse_pad_label[in_type[port]]);
862 void retro_get_system_info(struct retro_system_info *info)
865 #define GIT_VERSION ""
867 memset(info, 0, sizeof(*info));
868 info->library_name = "PCSX-ReARMed";
869 info->library_version = "r23l" GIT_VERSION;
870 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u|chd|iso|exe";
871 info->need_fullpath = true;
874 void retro_get_system_av_info(struct retro_system_av_info *info)
876 unsigned geom_height = vout_height > 0 ? vout_height : 240;
877 unsigned geom_width = vout_width > 0 ? vout_width : 320;
879 memset(info, 0, sizeof(*info));
880 info->timing.fps = is_pal_mode ? 50.0 : 60.0;
881 info->timing.sample_rate = 44100.0;
882 info->geometry.base_width = geom_width;
883 info->geometry.base_height = geom_height;
884 info->geometry.max_width = VOUT_MAX_WIDTH;
885 info->geometry.max_height = VOUT_MAX_HEIGHT;
886 info->geometry.aspect_ratio = 4.0 / 3.0;
890 size_t retro_serialize_size(void)
892 // it's currently 4380651-4397047 bytes,
893 // but have some reserved for future
904 static void *save_open(const char *name, const char *mode)
908 if (name == NULL || mode == NULL)
911 fp = malloc(sizeof(*fp));
915 fp->buf = (char *)name;
917 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
922 static int save_read(void *file, void *buf, u32 len)
924 struct save_fp *fp = file;
925 if (fp == NULL || buf == NULL)
928 memcpy(buf, fp->buf + fp->pos, len);
933 static int save_write(void *file, const void *buf, u32 len)
935 struct save_fp *fp = file;
936 if (fp == NULL || buf == NULL)
939 memcpy(fp->buf + fp->pos, buf, len);
944 static long save_seek(void *file, long offs, int whence)
946 struct save_fp *fp = file;
963 static void save_close(void *file)
965 struct save_fp *fp = file;
966 size_t r_size = retro_serialize_size();
970 if (fp->pos > r_size)
971 SysPrintf("ERROR: save buffer overflow detected\n");
972 else if (fp->is_write && fp->pos < r_size)
973 // make sure we don't save trash in leftover space
974 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
978 bool retro_serialize(void *data, size_t size)
980 int ret = SaveState(data);
981 return ret == 0 ? true : false;
984 bool retro_unserialize(const void *data, size_t size)
986 int ret = LoadState(data);
987 return ret == 0 ? true : false;
991 void retro_cheat_reset(void)
996 void retro_cheat_set(unsigned index, bool enabled, const char *code)
1001 // cheat funcs are destructive, need a copy...
1006 //Prepare buffered cheat for PCSX's AddCheat fucntion.
1013 if (++nonhexdec % 2)
1025 if (index < NumCheats)
1026 ret = EditCheat(index, "", buf);
1028 ret = AddCheat("", buf);
1032 SysPrintf("Failed to set cheat %#u\n", index);
1033 else if (index < NumCheats)
1034 Cheats[index].Enabled = enabled;
1038 // just in case, maybe a win-rt port in the future?
1046 #define PATH_MAX 4096
1049 /* multidisk support */
1050 static unsigned int disk_initial_index;
1051 static char disk_initial_path[PATH_MAX];
1052 static bool disk_ejected;
1053 static unsigned int disk_current_index;
1054 static unsigned int disk_count;
1055 static struct disks_state
1059 int internal_index; // for multidisk eboots
1062 static void get_disk_label(char *disk_label, const char *disk_path, size_t len)
1064 const char *base = NULL;
1066 if (!disk_path || (*disk_path == '\0'))
1069 base = strrchr(disk_path, SLASH);
1076 strncpy(disk_label, base, len - 1);
1077 disk_label[len - 1] = '\0';
1079 char *ext = strrchr(disk_label, '.');
1084 static void disk_init(void)
1088 disk_ejected = false;
1089 disk_current_index = 0;
1092 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++)
1094 if (disks[i].fname != NULL)
1096 free(disks[i].fname);
1097 disks[i].fname = NULL;
1099 if (disks[i].flabel != NULL)
1101 free(disks[i].flabel);
1102 disks[i].flabel = NULL;
1104 disks[i].internal_index = 0;
1108 static bool disk_set_eject_state(bool ejected)
1111 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
1114 disk_ejected = ejected;
1118 static bool disk_get_eject_state(void)
1120 /* can't be controlled by emulated software */
1121 return disk_ejected;
1124 static unsigned int disk_get_image_index(void)
1126 return disk_current_index;
1129 static bool disk_set_image_index(unsigned int index)
1131 if (index >= sizeof(disks) / sizeof(disks[0]))
1135 CdromLabel[0] = '\0';
1137 if (disks[index].fname == NULL)
1139 SysPrintf("missing disk #%u\n", index);
1142 // RetroArch specifies "no disk" with index == count,
1143 // so don't fail here..
1144 disk_current_index = index;
1148 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
1149 disks[index].fname, disks[index].internal_index);
1151 cdrIsoMultidiskSelect = disks[index].internal_index;
1152 set_cd_image(disks[index].fname);
1153 if (ReloadCdromPlugin() < 0)
1155 SysPrintf("failed to load cdr plugin\n");
1160 SysPrintf("failed to open cdr plugin\n");
1166 SetCdOpenCaseTime(time(NULL) + 2);
1170 disk_current_index = index;
1174 static unsigned int disk_get_num_images(void)
1179 static bool disk_replace_image_index(unsigned index,
1180 const struct retro_game_info *info)
1182 char *old_fname = NULL;
1183 char *old_flabel = NULL;
1186 if (index >= sizeof(disks) / sizeof(disks[0]))
1189 old_fname = disks[index].fname;
1190 old_flabel = disks[index].flabel;
1192 disks[index].fname = NULL;
1193 disks[index].flabel = NULL;
1194 disks[index].internal_index = 0;
1198 char disk_label[PATH_MAX];
1199 disk_label[0] = '\0';
1201 disks[index].fname = strdup(info->path);
1203 get_disk_label(disk_label, info->path, PATH_MAX);
1204 disks[index].flabel = strdup(disk_label);
1206 if (index == disk_current_index)
1207 ret = disk_set_image_index(index);
1210 if (old_fname != NULL)
1213 if (old_flabel != NULL)
1219 static bool disk_add_image_index(void)
1221 if (disk_count >= 8)
1228 static bool disk_set_initial_image(unsigned index, const char *path)
1230 if (index >= sizeof(disks) / sizeof(disks[0]))
1233 if (!path || (*path == '\0'))
1236 disk_initial_index = index;
1238 strncpy(disk_initial_path, path, sizeof(disk_initial_path) - 1);
1239 disk_initial_path[sizeof(disk_initial_path) - 1] = '\0';
1244 static bool disk_get_image_path(unsigned index, char *path, size_t len)
1246 const char *fname = NULL;
1251 if (index >= sizeof(disks) / sizeof(disks[0]))
1254 fname = disks[index].fname;
1256 if (!fname || (*fname == '\0'))
1259 strncpy(path, fname, len - 1);
1260 path[len - 1] = '\0';
1265 static bool disk_get_image_label(unsigned index, char *label, size_t len)
1267 const char *flabel = NULL;
1272 if (index >= sizeof(disks) / sizeof(disks[0]))
1275 flabel = disks[index].flabel;
1277 if (!flabel || (*flabel == '\0'))
1280 strncpy(label, flabel, len - 1);
1281 label[len - 1] = '\0';
1286 static struct retro_disk_control_callback disk_control = {
1287 .set_eject_state = disk_set_eject_state,
1288 .get_eject_state = disk_get_eject_state,
1289 .get_image_index = disk_get_image_index,
1290 .set_image_index = disk_set_image_index,
1291 .get_num_images = disk_get_num_images,
1292 .replace_image_index = disk_replace_image_index,
1293 .add_image_index = disk_add_image_index,
1296 static struct retro_disk_control_ext_callback disk_control_ext = {
1297 .set_eject_state = disk_set_eject_state,
1298 .get_eject_state = disk_get_eject_state,
1299 .get_image_index = disk_get_image_index,
1300 .set_image_index = disk_set_image_index,
1301 .get_num_images = disk_get_num_images,
1302 .replace_image_index = disk_replace_image_index,
1303 .add_image_index = disk_add_image_index,
1304 .set_initial_image = disk_set_initial_image,
1305 .get_image_path = disk_get_image_path,
1306 .get_image_label = disk_get_image_label,
1309 static char base_dir[1024];
1311 static bool read_m3u(const char *file)
1314 char name[PATH_MAX];
1315 FILE *fp = fopen(file, "r");
1319 while (fgets(line, sizeof(line), fp) && disk_count < sizeof(disks) / sizeof(disks[0]))
1323 char *carrige_return = strchr(line, '\r');
1325 *carrige_return = '\0';
1326 char *newline = strchr(line, '\n');
1330 if (line[0] != '\0')
1332 char disk_label[PATH_MAX];
1333 disk_label[0] = '\0';
1335 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
1336 disks[disk_count].fname = strdup(name);
1338 get_disk_label(disk_label, name, PATH_MAX);
1339 disks[disk_count].flabel = strdup(disk_label);
1346 return (disk_count != 0);
1349 static void extract_directory(char *buf, const char *path, size_t size)
1352 strncpy(buf, path, size - 1);
1353 buf[size - 1] = '\0';
1355 base = strrchr(buf, '/');
1357 base = strrchr(buf, '\\');
1368 #if defined(__QNX__) || defined(_WIN32)
1369 /* Blackberry QNX doesn't have strcasestr */
1372 * Find the first occurrence of find in s, ignore case.
1375 strcasestr(const char *s, const char *find)
1380 if ((c = *find++) != 0)
1382 c = tolower((unsigned char)c);
1388 if ((sc = *s++) == 0)
1390 } while ((char)tolower((unsigned char)sc) != c);
1391 } while (strncasecmp(s, find, len) != 0);
1398 static void set_retro_memmap(void)
1401 struct retro_memory_map retromap = { 0 };
1402 struct retro_memory_descriptor mmap = {
1403 0, psxM, 0, 0, 0, 0, 0x200000
1406 retromap.descriptors = &mmap;
1407 retromap.num_descriptors = 1;
1409 environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &retromap);
1413 static void retro_audio_buff_status_cb(
1414 bool active, unsigned occupancy, bool underrun_likely)
1416 retro_audio_buff_active = active;
1417 retro_audio_buff_occupancy = occupancy;
1418 retro_audio_buff_underrun = underrun_likely;
1421 static void retro_set_audio_buff_status_cb(void)
1423 if (frameskip_type == FRAMESKIP_NONE)
1425 environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, NULL);
1426 retro_audio_latency = 0;
1430 bool calculate_audio_latency = true;
1432 if (frameskip_type == FRAMESKIP_FIXED_INTERVAL)
1433 environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, NULL);
1436 struct retro_audio_buffer_status_callback buf_status_cb;
1437 buf_status_cb.callback = retro_audio_buff_status_cb;
1438 if (!environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK,
1441 retro_audio_buff_active = false;
1442 retro_audio_buff_occupancy = 0;
1443 retro_audio_buff_underrun = false;
1444 retro_audio_latency = 0;
1445 calculate_audio_latency = false;
1449 if (calculate_audio_latency)
1451 /* Frameskip is enabled - increase frontend
1452 * audio latency to minimise potential
1453 * buffer underruns */
1454 uint32_t frame_time_usec = 1000000.0 / (is_pal_mode ? 50.0 : 60.0);
1456 /* Set latency to 6x current frame time... */
1457 retro_audio_latency = (unsigned)(6 * frame_time_usec / 1000);
1459 /* ...then round up to nearest multiple of 32 */
1460 retro_audio_latency = (retro_audio_latency + 0x1F) & ~0x1F;
1464 update_audio_latency = true;
1465 frameskip_counter = 0;
1468 static void update_variables(bool in_flight);
1469 bool retro_load_game(const struct retro_game_info *info)
1472 unsigned int cd_index = 0;
1473 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
1474 bool is_exe = (strcasestr(info->path, ".exe") != NULL);
1477 struct retro_input_descriptor desc[] = {
1478 #define JOYP(port) \
1479 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" }, \
1480 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" }, \
1481 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" }, \
1482 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" }, \
1483 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" }, \
1484 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" }, \
1485 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" }, \
1486 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" }, \
1487 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" }, \
1488 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" }, \
1489 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" }, \
1490 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" }, \
1491 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" }, \
1492 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" }, \
1493 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" }, \
1494 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, \
1495 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" }, \
1496 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" }, \
1497 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" }, \
1498 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" }, \
1499 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_TRIGGER, "Gun Trigger" }, \
1500 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_RELOAD, "Gun Reload" }, \
1501 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A, "Gun Aux A" }, \
1502 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_B, "Gun Aux B" },
1518 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1520 #ifdef FRONTEND_SUPPORTS_RGB565
1521 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
1522 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
1524 SysPrintf("RGB565 supported, using it\n");
1528 if (info == NULL || info->path == NULL)
1530 SysPrintf("info->path required\n");
1534 update_variables(false);
1544 extract_directory(base_dir, info->path, sizeof(base_dir));
1548 if (!read_m3u(info->path))
1550 log_cb(RETRO_LOG_INFO, "failed to read m3u file\n");
1556 char disk_label[PATH_MAX];
1557 disk_label[0] = '\0';
1560 disks[0].fname = strdup(info->path);
1562 get_disk_label(disk_label, info->path, PATH_MAX);
1563 disks[0].flabel = strdup(disk_label);
1566 /* If this is an M3U file, attempt to set the
1567 * initial disk image */
1568 if (is_m3u && (disk_initial_index > 0) && (disk_initial_index < disk_count))
1570 const char *fname = disks[disk_initial_index].fname;
1572 if (fname && (*fname != '\0'))
1573 if (strcmp(disk_initial_path, fname) == 0)
1574 cd_index = disk_initial_index;
1577 set_cd_image(disks[cd_index].fname);
1578 disk_current_index = cd_index;
1580 /* have to reload after set_cd_image for correct cdr plugin */
1581 if (LoadPlugins() == -1)
1583 log_cb(RETRO_LOG_INFO, "failed to load plugins\n");
1590 if (OpenPlugins() == -1)
1592 log_cb(RETRO_LOG_INFO, "failed to open plugins\n");
1596 /* Handle multi-disk images (i.e. PBP)
1597 * > Cannot do this until after OpenPlugins() is
1598 * called (since this sets the value of
1599 * cdrIsoMultidiskCount) */
1600 if (!is_m3u && (cdrIsoMultidiskCount > 1))
1602 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
1604 /* Small annoyance: We need to change the label
1605 * of disk 0, so have to clear existing entries */
1606 if (disks[0].fname != NULL)
1607 free(disks[0].fname);
1608 disks[0].fname = NULL;
1610 if (disks[0].flabel != NULL)
1611 free(disks[0].flabel);
1612 disks[0].flabel = NULL;
1614 for (i = 0; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++)
1616 char disk_name[PATH_MAX - 16] = { 0 };
1617 char disk_label[PATH_MAX] = { 0 };
1619 disks[i].fname = strdup(info->path);
1621 get_disk_label(disk_name, info->path, sizeof(disk_name));
1622 snprintf(disk_label, sizeof(disk_label), "%s #%u", disk_name, (unsigned)i + 1);
1623 disks[i].flabel = strdup(disk_label);
1625 disks[i].internal_index = i;
1628 /* This is not an M3U file, so initial disk
1629 * image has not yet been set - attempt to
1631 if ((disk_initial_index > 0) && (disk_initial_index < disk_count))
1633 const char *fname = disks[disk_initial_index].fname;
1635 if (fname && (*fname != '\0'))
1636 if (strcmp(disk_initial_path, fname) == 0)
1637 cd_index = disk_initial_index;
1643 CdromLabel[0] = '\0';
1645 cdrIsoMultidiskSelect = disks[cd_index].internal_index;
1646 disk_current_index = cd_index;
1647 set_cd_image(disks[cd_index].fname);
1649 if (ReloadCdromPlugin() < 0)
1651 log_cb(RETRO_LOG_INFO, "failed to reload cdr plugins\n");
1656 log_cb(RETRO_LOG_INFO, "failed to open cdr plugin\n");
1662 /* set ports to use "standard controller" initially */
1663 for (i = 0; i < 8; ++i)
1664 in_type[i] = PSE_PAD_TYPE_STANDARD;
1666 plugin_call_rearmed_cbs();
1667 /* dfinput_activate(); */
1669 if (!is_exe && CheckCdrom() == -1)
1671 log_cb(RETRO_LOG_INFO, "unsupported/invalid CD image: %s\n", info->path);
1678 ret = Load(info->path);
1683 log_cb(RETRO_LOG_INFO, "could not load %s (%d)\n", is_exe ? "exe" : "CD", ret);
1689 retro_set_audio_buff_status_cb();
1694 unsigned retro_get_region(void)
1696 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1699 void *retro_get_memory_data(unsigned id)
1701 if (id == RETRO_MEMORY_SAVE_RAM)
1703 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1709 size_t retro_get_memory_size(unsigned id)
1711 if (id == RETRO_MEMORY_SAVE_RAM)
1713 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1719 void retro_reset(void)
1721 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
1726 static const unsigned short retro_psx_map[] = {
1727 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
1728 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
1729 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
1730 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
1731 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
1732 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
1733 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
1734 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
1735 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
1736 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
1737 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
1738 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
1739 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
1740 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
1741 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
1742 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
1744 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
1746 //Percentage distance of screen to adjust
1747 static int GunconAdjustX = 0;
1748 static int GunconAdjustY = 0;
1750 //Used when out by a percentage
1751 static float GunconAdjustRatioX = 1;
1752 static float GunconAdjustRatioY = 1;
1754 static void update_variables(bool in_flight)
1756 struct retro_variable var;
1758 // Always enable GPU_PEOPS_OLD_FRAME_SKIP flag
1759 // (this is set in standalone, with no option
1761 int gpu_peops_fix = GPU_PEOPS_OLD_FRAME_SKIP;
1763 frameskip_type_t prev_frameskip_type;
1766 var.key = "pcsx_rearmed_frameskip_type";
1768 prev_frameskip_type = frameskip_type;
1769 frameskip_type = FRAMESKIP_NONE;
1770 pl_rearmed_cbs.frameskip = 0;
1772 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1774 if (strcmp(var.value, "auto") == 0)
1775 frameskip_type = FRAMESKIP_AUTO;
1776 if (strcmp(var.value, "auto_threshold") == 0)
1777 frameskip_type = FRAMESKIP_AUTO_THRESHOLD;
1778 if (strcmp(var.value, "fixed_interval") == 0)
1779 frameskip_type = FRAMESKIP_FIXED_INTERVAL;
1782 if (frameskip_type != 0)
1783 pl_rearmed_cbs.frameskip = -1;
1786 var.key = "pcsx_rearmed_frameskip_threshold";
1787 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1789 frameskip_threshold = strtol(var.value, NULL, 10);
1793 var.key = "pcsx_rearmed_frameskip_interval";
1794 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1796 frameskip_interval = strtol(var.value, NULL, 10);
1800 var.key = "pcsx_rearmed_region";
1801 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1804 if (strcmp(var.value, "auto") == 0)
1806 else if (strcmp(var.value, "NTSC") == 0)
1808 else if (strcmp(var.value, "PAL") == 0)
1815 var.key = "pcsx_rearmed_negcon_deadzone";
1816 negcon_deadzone = 0;
1817 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1819 negcon_deadzone = (int)(atoi(var.value) * 0.01f * NEGCON_RANGE);
1823 var.key = "pcsx_rearmed_negcon_response";
1824 negcon_linearity = 1;
1825 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1827 if (strcmp(var.value, "quadratic") == 0)
1829 negcon_linearity = 2;
1831 else if (strcmp(var.value, "cubic") == 0)
1833 negcon_linearity = 3;
1838 var.key = "pcsx_rearmed_analog_axis_modifier";
1839 axis_bounds_modifier = true;
1840 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1842 if (strcmp(var.value, "square") == 0)
1844 axis_bounds_modifier = true;
1846 else if (strcmp(var.value, "circle") == 0)
1848 axis_bounds_modifier = false;
1853 var.key = "pcsx_rearmed_vibration";
1855 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1857 if (strcmp(var.value, "disabled") == 0)
1858 in_enable_vibration = 0;
1859 else if (strcmp(var.value, "enabled") == 0)
1860 in_enable_vibration = 1;
1864 var.key = "pcsx_rearmed_dithering";
1866 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1868 if (strcmp(var.value, "disabled") == 0)
1870 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
1871 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 0;
1872 pl_rearmed_cbs.gpu_unai.dithering = 0;
1874 pl_rearmed_cbs.gpu_neon.allow_dithering = 0;
1877 else if (strcmp(var.value, "enabled") == 0)
1879 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1880 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 1;
1881 pl_rearmed_cbs.gpu_unai.dithering = 1;
1883 pl_rearmed_cbs.gpu_neon.allow_dithering = 1;
1890 var.key = "pcsx_rearmed_neon_interlace_enable_v2";
1892 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1894 if (strcmp(var.value, "disabled") == 0)
1895 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
1896 else if (strcmp(var.value, "enabled") == 0)
1897 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
1899 pl_rearmed_cbs.gpu_neon.allow_interlace = 2;
1903 var.key = "pcsx_rearmed_neon_enhancement_enable";
1905 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1907 if (strcmp(var.value, "disabled") == 0)
1908 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
1909 else if (strcmp(var.value, "enabled") == 0)
1910 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
1914 var.key = "pcsx_rearmed_neon_enhancement_no_main";
1916 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1918 if (strcmp(var.value, "disabled") == 0)
1919 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
1920 else if (strcmp(var.value, "enabled") == 0)
1921 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
1926 var.key = "pcsx_rearmed_duping_enable";
1928 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1930 if (strcmp(var.value, "disabled") == 0)
1931 duping_enable = false;
1932 else if (strcmp(var.value, "enabled") == 0)
1933 duping_enable = true;
1937 var.key = "pcsx_rearmed_display_internal_fps";
1939 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1941 if (strcmp(var.value, "disabled") == 0)
1942 display_internal_fps = false;
1943 else if (strcmp(var.value, "enabled") == 0)
1944 display_internal_fps = true;
1948 // CPU emulation related config
1951 var.key = "pcsx_rearmed_drc";
1953 if (!environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
1954 var.value = "enabled";
1957 R3000Acpu *prev_cpu = psxCpu;
1961 Config.Cpu = CPU_INTERPRETER;
1964 if (strcmp(var.value, "disabled") == 0)
1965 Config.Cpu = CPU_INTERPRETER;
1966 else if (strcmp(var.value, "enabled") == 0)
1967 Config.Cpu = CPU_DYNAREC;
1969 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
1970 if (psxCpu != prev_cpu)
1972 prev_cpu->Notify(R3000ACPU_NOTIFY_BEFORE_SAVE, NULL);
1973 prev_cpu->Shutdown();
1976 psxCpu->Notify(R3000ACPU_NOTIFY_AFTER_LOAD, NULL);
1979 #endif /* !DRC_DISABLE */
1982 var.key = "pcsx_rearmed_psxclock";
1983 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1985 int psxclock = atoi(var.value);
1986 Config.cycle_multiplier = 10000 / psxclock;
1989 #if !defined(DRC_DISABLE) && !defined(LIGHTREC)
1991 var.key = "pcsx_rearmed_nosmccheck";
1992 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1994 if (strcmp(var.value, "enabled") == 0)
1995 new_dynarec_hacks |= NDHACK_NO_SMC_CHECK;
1997 new_dynarec_hacks &= ~NDHACK_NO_SMC_CHECK;
2001 var.key = "pcsx_rearmed_gteregsunneeded";
2002 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2004 if (strcmp(var.value, "enabled") == 0)
2005 new_dynarec_hacks |= NDHACK_GTE_UNNEEDED;
2007 new_dynarec_hacks &= ~NDHACK_GTE_UNNEEDED;
2011 var.key = "pcsx_rearmed_nogteflags";
2012 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2014 if (strcmp(var.value, "enabled") == 0)
2015 new_dynarec_hacks |= NDHACK_GTE_NO_FLAGS;
2017 new_dynarec_hacks &= ~NDHACK_GTE_NO_FLAGS;
2021 var.key = "pcsx_rearmed_nocompathacks";
2022 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2024 if (strcmp(var.value, "enabled") == 0)
2025 new_dynarec_hacks |= NDHACK_NO_COMPAT_HACKS;
2027 new_dynarec_hacks &= ~NDHACK_NO_COMPAT_HACKS;
2029 #endif /* !DRC_DISABLE && !LIGHTREC */
2032 var.key = "pcsx_rearmed_nostalls";
2033 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2035 if (strcmp(var.value, "enabled") == 0)
2036 Config.DisableStalls = 1;
2038 Config.DisableStalls = 0;
2042 var.key = "pcsx_rearmed_icache_emulation";
2043 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2045 if (strcmp(var.value, "disabled") == 0)
2046 Config.icache_emulation = 0;
2047 else if (strcmp(var.value, "enabled") == 0)
2048 Config.icache_emulation = 1;
2052 var.key = "pcsx_rearmed_exception_emulation";
2053 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2055 if (strcmp(var.value, "enabled") == 0)
2056 Config.PreciseExceptions = 1;
2058 Config.PreciseExceptions = 0;
2061 psxCpu->ApplyConfig();
2063 // end of CPU emu config
2067 var.key = "pcsx_rearmed_spu_reverb";
2069 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2071 if (strcmp(var.value, "disabled") == 0)
2072 spu_config.iUseReverb = false;
2073 else if (strcmp(var.value, "enabled") == 0)
2074 spu_config.iUseReverb = true;
2078 var.key = "pcsx_rearmed_spu_interpolation";
2080 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2082 if (strcmp(var.value, "simple") == 0)
2083 spu_config.iUseInterpolation = 1;
2084 else if (strcmp(var.value, "gaussian") == 0)
2085 spu_config.iUseInterpolation = 2;
2086 else if (strcmp(var.value, "cubic") == 0)
2087 spu_config.iUseInterpolation = 3;
2088 else if (strcmp(var.value, "off") == 0)
2089 spu_config.iUseInterpolation = 0;
2093 var.key = "pcsx_rearmed_spu_thread";
2094 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2096 if (strcmp(var.value, "enabled") == 0)
2097 spu_config.iUseThread = 1;
2099 spu_config.iUseThread = 0;
2102 if (P_HAVE_PTHREAD) {
2104 var.key = "pcsx_rearmed_async_cd";
2105 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2107 if (strcmp(var.value, "async") == 0)
2110 Config.CHD_Precache = 0;
2112 else if (strcmp(var.value, "sync") == 0)
2115 Config.CHD_Precache = 0;
2117 else if (strcmp(var.value, "precache") == 0)
2120 Config.CHD_Precache = 1;
2126 var.key = "pcsx_rearmed_noxadecoding";
2127 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2129 if (strcmp(var.value, "disabled") == 0)
2136 var.key = "pcsx_rearmed_nocdaudio";
2137 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2139 if (strcmp(var.value, "disabled") == 0)
2146 var.key = "pcsx_rearmed_gpu_slow_llists";
2147 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2149 if (strcmp(var.value, "disabled") == 0)
2150 Config.GpuListWalking = 0;
2151 else if (strcmp(var.value, "enabled") == 0)
2152 Config.GpuListWalking = 1;
2154 Config.GpuListWalking = -1;
2158 var.key = "pcsx_rearmed_screen_centering";
2159 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2161 if (strcmp(var.value, "game") == 0)
2162 pl_rearmed_cbs.screen_centering_type = 1;
2163 else if (strcmp(var.value, "manual") == 0)
2164 pl_rearmed_cbs.screen_centering_type = 2;
2166 pl_rearmed_cbs.screen_centering_type = 0;
2170 var.key = "pcsx_rearmed_screen_centering_x";
2171 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2173 pl_rearmed_cbs.screen_centering_x = atoi(var.value);
2177 var.key = "pcsx_rearmed_screen_centering_y";
2178 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2180 pl_rearmed_cbs.screen_centering_y = atoi(var.value);
2183 #ifdef THREAD_RENDERING
2184 var.key = "pcsx_rearmed_gpu_thread_rendering";
2187 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2189 if (strcmp(var.value, "disabled") == 0)
2190 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_OFF;
2191 else if (strcmp(var.value, "sync") == 0)
2192 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_SYNC;
2193 else if (strcmp(var.value, "async") == 0)
2194 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_ASYNC;
2200 var.key = "pcsx_rearmed_gpu_peops_odd_even_bit";
2202 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2204 if (strcmp(var.value, "enabled") == 0)
2205 gpu_peops_fix |= GPU_PEOPS_ODD_EVEN_BIT;
2209 var.key = "pcsx_rearmed_gpu_peops_expand_screen_width";
2211 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2213 if (strcmp(var.value, "enabled") == 0)
2214 gpu_peops_fix |= GPU_PEOPS_EXPAND_SCREEN_WIDTH;
2218 var.key = "pcsx_rearmed_gpu_peops_ignore_brightness";
2220 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2222 if (strcmp(var.value, "enabled") == 0)
2223 gpu_peops_fix |= GPU_PEOPS_IGNORE_BRIGHTNESS;
2227 var.key = "pcsx_rearmed_gpu_peops_disable_coord_check";
2229 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2231 if (strcmp(var.value, "enabled") == 0)
2232 gpu_peops_fix |= GPU_PEOPS_DISABLE_COORD_CHECK;
2236 var.key = "pcsx_rearmed_gpu_peops_lazy_screen_update";
2238 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2240 if (strcmp(var.value, "enabled") == 0)
2241 gpu_peops_fix |= GPU_PEOPS_LAZY_SCREEN_UPDATE;
2245 var.key = "pcsx_rearmed_gpu_peops_repeated_triangles";
2247 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2249 if (strcmp(var.value, "enabled") == 0)
2250 gpu_peops_fix |= GPU_PEOPS_REPEATED_TRIANGLES;
2254 var.key = "pcsx_rearmed_gpu_peops_quads_with_triangles";
2256 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2258 if (strcmp(var.value, "enabled") == 0)
2259 gpu_peops_fix |= GPU_PEOPS_QUADS_WITH_TRIANGLES;
2263 var.key = "pcsx_rearmed_gpu_peops_fake_busy_state";
2265 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2267 if (strcmp(var.value, "enabled") == 0)
2268 gpu_peops_fix |= GPU_PEOPS_FAKE_BUSY_STATE;
2271 if (pl_rearmed_cbs.gpu_peops.dwActFixes != gpu_peops_fix)
2272 pl_rearmed_cbs.gpu_peops.dwActFixes = gpu_peops_fix;
2276 /* Note: This used to be an option, but it only works
2277 * (correctly) when running high resolution games
2278 * (480i, 512i) and has been obsoleted by
2279 * pcsx_rearmed_gpu_unai_scale_hires */
2280 pl_rearmed_cbs.gpu_unai.ilace_force = 0;
2281 /* Note: This used to be an option, but it has no
2282 * discernable effect and has been obsoleted by
2283 * pcsx_rearmed_gpu_unai_scale_hires */
2284 pl_rearmed_cbs.gpu_unai.pixel_skip = 0;
2286 var.key = "pcsx_rearmed_gpu_unai_lighting";
2289 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2291 if (strcmp(var.value, "disabled") == 0)
2292 pl_rearmed_cbs.gpu_unai.lighting = 0;
2293 else if (strcmp(var.value, "enabled") == 0)
2294 pl_rearmed_cbs.gpu_unai.lighting = 1;
2297 var.key = "pcsx_rearmed_gpu_unai_fast_lighting";
2300 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2302 if (strcmp(var.value, "disabled") == 0)
2303 pl_rearmed_cbs.gpu_unai.fast_lighting = 0;
2304 else if (strcmp(var.value, "enabled") == 0)
2305 pl_rearmed_cbs.gpu_unai.fast_lighting = 1;
2308 var.key = "pcsx_rearmed_gpu_unai_blending";
2311 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2313 if (strcmp(var.value, "disabled") == 0)
2314 pl_rearmed_cbs.gpu_unai.blending = 0;
2315 else if (strcmp(var.value, "enabled") == 0)
2316 pl_rearmed_cbs.gpu_unai.blending = 1;
2319 var.key = "pcsx_rearmed_gpu_unai_scale_hires";
2322 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2324 if (strcmp(var.value, "disabled") == 0)
2325 pl_rearmed_cbs.gpu_unai.scale_hires = 0;
2326 else if (strcmp(var.value, "enabled") == 0)
2327 pl_rearmed_cbs.gpu_unai.scale_hires = 1;
2331 //This adjustment process gives the user the ability to manually align the mouse up better
2332 //with where the shots are in the emulator.
2335 var.key = "pcsx_rearmed_gunconadjustx";
2337 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2339 GunconAdjustX = atoi(var.value);
2343 var.key = "pcsx_rearmed_gunconadjusty";
2345 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2347 GunconAdjustY = atoi(var.value);
2351 var.key = "pcsx_rearmed_gunconadjustratiox";
2353 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2355 GunconAdjustRatioX = atof(var.value);
2359 var.key = "pcsx_rearmed_gunconadjustratioy";
2361 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2363 GunconAdjustRatioY = atof(var.value);
2367 var.key = "pcsx_rearmed_input_sensitivity";
2368 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2370 mouse_sensitivity = atof(var.value);
2375 // inform core things about possible config changes
2376 plugin_call_rearmed_cbs();
2378 if (GPU_open != NULL && GPU_close != NULL)
2381 GPU_open(&gpuDisp, "PCSX", NULL);
2384 /* Reinitialise frameskipping, if required */
2385 if (((frameskip_type != prev_frameskip_type)))
2386 retro_set_audio_buff_status_cb();
2388 /* dfinput_activate(); */
2394 //bootlogo display hack
2398 var.key = "pcsx_rearmed_show_bios_bootlogo";
2399 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2401 Config.SlowBoot = 0;
2403 if (strcmp(var.value, "enabled") == 0)
2405 Config.SlowBoot = 1;
2412 update_option_visibility();
2415 // Taken from beetle-psx-libretro
2416 static uint16_t get_analog_button(int16_t ret, retro_input_state_t input_state_cb, int player_index, int id)
2418 // NOTE: Analog buttons were added Nov 2017. Not all front-ends support this
2419 // feature (or pre-date it) so we need to handle this in a graceful way.
2421 // First, try and get an analog value using the new libretro API constant
2422 uint16_t button = input_state_cb(player_index,
2423 RETRO_DEVICE_ANALOG,
2424 RETRO_DEVICE_INDEX_ANALOG_BUTTON,
2426 button = MIN(button / 128, 255);
2430 // If we got exactly zero, we're either not pressing the button, or the front-end
2431 // is not reporting analog values. We need to do a second check using the classic
2432 // digital API method, to at least get some response - better than nothing.
2434 // NOTE: If we're really just not holding the button, we're still going to get zero.
2436 button = (ret & (1 << id)) ? 255 : 0;
2442 unsigned char axis_range_modifier(int16_t axis_value, bool is_square)
2444 float modifier_axis_range = 0;
2448 modifier_axis_range = round((axis_value >> 8) / 0.785) + 128;
2449 if (modifier_axis_range < 0)
2451 modifier_axis_range = 0;
2453 else if (modifier_axis_range > 255)
2455 modifier_axis_range = 255;
2460 modifier_axis_range = MIN(((axis_value >> 8) + 128), 255);
2463 return modifier_axis_range;
2466 static void update_input_guncon(int port, int ret)
2469 //Core option for cursors for both players
2470 //Separate pointer and lightgun control types
2472 //Mouse range is -32767 -> 32767
2474 //Use the left analog stick field to store the absolute coordinates
2476 int gunx = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X);
2477 int guny = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y);
2479 //Have the Libretro API let /libpcsxcore/plugins.c know when the lightgun is pointed offscreen
2480 //Offscreen value is chosen to be well out of range of any possible scaling done via core options
2481 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN) || input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_RELOAD))
2483 in_analog_left[port][0] = (65536 - 512) * 64;
2484 in_analog_left[port][1] = (65536 - 512) * 64;
2488 in_analog_left[port][0] = (gunx * GunconAdjustRatioX) + (GunconAdjustX * 655);
2489 in_analog_left[port][1] = (guny * GunconAdjustRatioY) + (GunconAdjustY * 655);
2492 //GUNCON has 3 controls, Trigger,A,B which equal Circle,Start,Cross
2495 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_TRIGGER) || input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_RELOAD))
2496 in_keystate[port] |= (1 << DKEY_CIRCLE);
2499 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A))
2500 in_keystate[port] |= (1 << DKEY_START);
2503 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_B))
2504 in_keystate[port] |= (1 << DKEY_CROSS);
2508 static void update_input_negcon(int port, int ret)
2514 float negcon_twist_amplitude;
2516 // Query digital inputs
2519 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_UP))
2520 in_keystate[port] |= (1 << DKEY_UP);
2522 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT))
2523 in_keystate[port] |= (1 << DKEY_RIGHT);
2525 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN))
2526 in_keystate[port] |= (1 << DKEY_DOWN);
2528 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT))
2529 in_keystate[port] |= (1 << DKEY_LEFT);
2531 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_START))
2532 in_keystate[port] |= (1 << DKEY_START);
2534 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_A))
2535 in_keystate[port] |= (1 << DKEY_CIRCLE);
2537 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_X))
2538 in_keystate[port] |= (1 << DKEY_TRIANGLE);
2539 // > neGcon R shoulder (digital)
2540 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_R))
2541 in_keystate[port] |= (1 << DKEY_R1);
2542 // Query analog inputs
2544 // From studying 'libpcsxcore/plugins.c' and 'frontend/plugin.c':
2545 // >> pad->leftJoyX == in_analog_left[port][0] == NeGcon II
2546 // >> pad->leftJoyY == in_analog_left[port][1] == NeGcon L
2547 // >> pad->rightJoyX == in_analog_right[port][0] == NeGcon twist
2548 // >> pad->rightJoyY == in_analog_right[port][1] == NeGcon I
2549 // So we just have to map in_analog_left/right to more
2550 // appropriate inputs...
2553 // >> Get raw analog stick value and account for deadzone
2554 lsx = input_state_cb(port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
2555 if (lsx > negcon_deadzone)
2556 lsx = lsx - negcon_deadzone;
2557 else if (lsx < -negcon_deadzone)
2558 lsx = lsx + negcon_deadzone;
2561 // >> Convert to an 'amplitude' [-1.0,1.0] and adjust response
2562 negcon_twist_amplitude = (float)lsx / (float)(NEGCON_RANGE - negcon_deadzone);
2563 if (negcon_linearity == 2)
2565 if (negcon_twist_amplitude < 0.0)
2566 negcon_twist_amplitude = -(negcon_twist_amplitude * negcon_twist_amplitude);
2568 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude;
2570 else if (negcon_linearity == 3)
2571 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude * negcon_twist_amplitude;
2572 // >> Convert to final 'in_analog' integer value [0,255]
2573 in_analog_right[port][0] = MAX(MIN((int)(negcon_twist_amplitude * 128.0f) + 128, 255), 0);
2575 // >> Handle right analog stick vertical axis mapping...
2576 // - Up (-Y) == accelerate == neGcon I
2577 // - Down (+Y) == brake == neGcon II
2580 rsy = input_state_cb(port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
2583 // Account for deadzone
2584 // (Note: have never encountered a gamepad with significant differences
2585 // in deadzone between left/right analog sticks, so use the regular 'twist'
2587 if (rsy > negcon_deadzone)
2588 rsy = rsy - negcon_deadzone;
2591 // Convert to 'in_analog' integer value [0,255]
2592 negcon_ii_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2596 if (rsy < -negcon_deadzone)
2597 rsy = -1 * (rsy + negcon_deadzone);
2600 negcon_i_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2603 in_analog_right[port][1] = MAX(
2605 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_R2),
2606 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_B)),
2609 in_analog_left[port][0] = MAX(
2611 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_L2),
2612 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_Y)),
2615 in_analog_left[port][1] = get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_L);
2618 static void update_input_mouse(int port, int ret)
2620 float raw_x = input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X);
2621 float raw_y = input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y);
2623 int x = (int)roundf(raw_x * mouse_sensitivity);
2624 int y = (int)roundf(raw_y * mouse_sensitivity);
2626 if (x > 127) x = 127;
2627 else if (x < -128) x = -128;
2629 if (y > 127) y = 127;
2630 else if (y < -128) y = -128;
2632 in_mouse[port][0] = x; /* -128..+128 left/right movement, 0 = no movement */
2633 in_mouse[port][1] = y; /* -128..+128 down/up movement, 0 = no movement */
2635 /* left mouse button state */
2636 if (input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_LEFT))
2637 in_keystate[port] |= 1 << 11;
2639 /* right mouse button state */
2640 if (input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_RIGHT))
2641 in_keystate[port] |= 1 << 10;
2644 static void update_input(void)
2646 // reset all keystate, query libretro for keystate
2650 for (i = 0; i < PORTS_NUMBER; i++)
2653 int type = in_type[i];
2657 if (type == PSE_PAD_TYPE_NONE)
2660 if (libretro_supports_bitmasks)
2661 ret = input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
2664 for (j = 0; j < (RETRO_DEVICE_ID_JOYPAD_R3 + 1); j++)
2666 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j))
2673 case PSE_PAD_TYPE_GUNCON:
2674 update_input_guncon(i, ret);
2676 case PSE_PAD_TYPE_NEGCON:
2677 update_input_negcon(i, ret);
2679 case PSE_PAD_TYPE_MOUSE:
2680 update_input_mouse(i, ret);
2683 // Query digital inputs
2684 for (j = 0; j < RETRO_PSX_MAP_LEN; j++)
2686 in_keystate[i] |= retro_psx_map[j];
2688 // Query analog inputs
2689 if (type == PSE_PAD_TYPE_ANALOGJOY || type == PSE_PAD_TYPE_ANALOGPAD)
2691 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);
2692 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);
2693 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);
2694 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);
2700 static void print_internal_fps(void)
2702 if (display_internal_fps)
2706 if (frame_count % INTERNAL_FPS_SAMPLE_PERIOD == 0)
2708 unsigned internal_fps = pl_rearmed_cbs.flip_cnt * (is_pal_mode ? 50 : 60) / INTERNAL_FPS_SAMPLE_PERIOD;
2710 const char *strc = (const char *)str;
2714 snprintf(str, sizeof(str), "Internal FPS: %2d", internal_fps);
2716 pl_rearmed_cbs.flip_cnt = 0;
2718 if (msg_interface_version >= 1)
2720 struct retro_message_ext msg = {
2725 RETRO_MESSAGE_TARGET_OSD,
2726 RETRO_MESSAGE_TYPE_STATUS,
2729 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE_EXT, &msg);
2733 struct retro_message msg = {
2737 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
2745 void retro_run(void)
2747 //SysReset must be run while core is running,Not in menu (Locks up Retroarch)
2756 print_internal_fps();
2758 /* Check whether current frame should
2760 pl_rearmed_cbs.fskip_force = 0;
2761 pl_rearmed_cbs.fskip_dirty = 0;
2763 if (frameskip_type != FRAMESKIP_NONE)
2765 bool skip_frame = false;
2767 switch (frameskip_type)
2769 case FRAMESKIP_AUTO:
2770 skip_frame = retro_audio_buff_active && retro_audio_buff_underrun;
2772 case FRAMESKIP_AUTO_THRESHOLD:
2773 skip_frame = retro_audio_buff_active && (retro_audio_buff_occupancy < frameskip_threshold);
2775 case FRAMESKIP_FIXED_INTERVAL:
2782 if (skip_frame && frameskip_counter < frameskip_interval)
2783 pl_rearmed_cbs.fskip_force = 1;
2786 /* If frameskip/timing settings have changed,
2787 * update frontend audio latency
2788 * > Can do this before or after the frameskip
2789 * check, but doing it after means we at least
2790 * retain the current frame's audio output */
2791 if (update_audio_latency)
2793 environ_cb(RETRO_ENVIRONMENT_SET_MINIMUM_AUDIO_LATENCY,
2794 &retro_audio_latency);
2795 update_audio_latency = false;
2802 bool updated = false;
2803 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
2804 update_variables(true);
2809 if (pl_rearmed_cbs.fskip_dirty == 1) {
2810 if (frameskip_counter < frameskip_interval)
2811 frameskip_counter++;
2812 else if (frameskip_counter >= frameskip_interval || !pl_rearmed_cbs.fskip_force)
2813 frameskip_counter = 0;
2816 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf_ptr : NULL,
2817 vout_width, vout_height, vout_width * 2);
2823 static bool try_use_bios(const char *path, bool preferred_only)
2827 FILE *fp = fopen(path, "rb");
2831 fseek(fp, 0, SEEK_END);
2835 name = strrchr(path, SLASH);
2839 if (preferred_only && size != 512 * 1024)
2841 if (size != 512 * 1024 && size != 4 * 1024 * 1024)
2843 if (strstr(name, "unirom"))
2845 // jp bios have an addidional region check
2846 if (preferred_only && (strcasestr(name, "00.") || strcasestr(name, "j.bin")))
2849 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
2854 #include <sys/types.h>
2857 static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
2859 static const char *substr_pref[] = { "scph", "ps" };
2860 static const char *substr_alt[] = { "scph", "ps", "openbios" };
2866 dir = opendir(dirpath);
2870 // try to find a "better" bios
2871 while ((ent = readdir(dir)))
2873 for (i = 0; i < sizeof(substr_pref) / sizeof(substr_pref[0]); i++)
2875 const char *substr = substr_pref[i];
2876 if ((strncasecmp(ent->d_name, substr, strlen(substr)) != 0))
2878 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
2879 ret = try_use_bios(path, true);
2885 // another pass to look for anything fitting, even ps2 bios
2887 while ((ent = readdir(dir)))
2889 for (i = 0; i < sizeof(substr_alt) / sizeof(substr_alt[0]); i++)
2891 const char *substr = substr_alt[i];
2892 if ((strncasecmp(ent->d_name, substr, strlen(substr)) != 0))
2894 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
2895 ret = try_use_bios(path, false);
2907 #define find_any_bios(...) false
2910 static void check_system_specs(void)
2913 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
2916 static int init_memcards(void)
2920 struct retro_variable var = { .key = "pcsx_rearmed_memcard2", .value = NULL };
2921 static const char CARD2_FILE[] = "pcsx-card2.mcd";
2923 // Memcard2 will be handled and is re-enabled if needed using core
2925 // Memcard1 is handled by libretro, doing this will set core to
2926 // skip file io operations for memcard1 like SaveMcd
2927 snprintf(Config.Mcd1, sizeof(Config.Mcd1), "none");
2928 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "none");
2929 init_memcard(Mcd1Data);
2930 // Memcard 2 is managed by the emulator on the filesystem,
2931 // There is no need to initialize Mcd2Data like Mcd1Data.
2933 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2935 SysPrintf("Memcard 2: %s\n", var.value);
2936 if (memcmp(var.value, "enabled", 7) == 0)
2938 if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir)
2940 if (strlen(dir) + strlen(CARD2_FILE) + 2 > sizeof(Config.Mcd2))
2942 SysPrintf("Path '%s' is too long. Cannot use memcard 2. Use a shorter path.\n", dir);
2948 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "%s/%s", dir, CARD2_FILE);
2949 SysPrintf("Use memcard 2: %s\n", Config.Mcd2);
2954 SysPrintf("Could not get save directory! Could not create memcard 2.");
2962 static void loadPSXBios(void)
2965 char path[PATH_MAX];
2966 unsigned useHLE = 0;
2968 const char *bios[] = {
2969 "PSXONPSP660", "psxonpsp660",
2970 "SCPH101", "scph101",
2971 "SCPH5501", "scph5501",
2972 "SCPH7001", "scph7001",
2973 "SCPH1001", "scph1001"
2976 struct retro_variable var = {
2977 .key = "pcsx_rearmed_bios",
2983 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2985 if (!strcmp(var.value, "HLE"))
2991 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
2994 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s", dir);
2996 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++)
2998 snprintf(path, sizeof(path), "%s%c%s.bin", dir, SLASH, bios[i]);
2999 found_bios = try_use_bios(path, true);
3005 found_bios = find_any_bios(dir, path, sizeof(path));
3009 SysPrintf("found BIOS file: %s\n", Config.Bios);
3015 const char *msg_str;
3018 msg_str = "BIOS set to \'hle\' in core options - real BIOS will be ignored";
3019 SysPrintf("Using HLE BIOS.\n");
3023 msg_str = "No PlayStation BIOS file found - add for better compatibility";
3024 SysPrintf("No BIOS files found.\n");
3027 if (msg_interface_version >= 1)
3029 struct retro_message_ext msg = {
3034 RETRO_MESSAGE_TARGET_ALL,
3035 RETRO_MESSAGE_TYPE_NOTIFICATION,
3038 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE_EXT, &msg);
3042 struct retro_message msg = {
3046 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
3051 void retro_init(void)
3053 unsigned dci_version = 0;
3054 struct retro_rumble_interface rumble;
3057 msg_interface_version = 0;
3058 environ_cb(RETRO_ENVIRONMENT_GET_MESSAGE_INTERFACE_VERSION, &msg_interface_version);
3060 #if defined(__MACH__) && !defined(TVOS)
3061 // magic sauce to make the dynarec work on iOS
3062 syscall(SYS_ptrace, 0 /*PTRACE_TRACEME*/, 0, 0, 0);
3066 psxMapHook = pl_3ds_mmap;
3067 psxUnmapHook = pl_3ds_munmap;
3070 if (init_vita_mmap() < 0)
3072 psxMapHook = pl_vita_mmap;
3073 psxUnmapHook = pl_vita_munmap;
3075 ret = emu_core_preinit();
3077 /* emu_core_preinit sets the cpu to dynarec */
3079 Config.Cpu = CPU_INTERPRETER;
3081 ret |= init_memcards();
3083 ret |= emu_core_init();
3086 SysPrintf("PCSX init failed.\n");
3091 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
3092 #elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && P_HAVE_POSIX_MEMALIGN
3093 if (posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2) != 0)
3094 vout_buf = (void *) 0;
3096 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
3099 vout_buf_ptr = vout_buf;
3103 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
3105 disk_initial_index = 0;
3106 disk_initial_path[0] = '\0';
3107 if (environ_cb(RETRO_ENVIRONMENT_GET_DISK_CONTROL_INTERFACE_VERSION, &dci_version) && (dci_version >= 1))
3108 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_EXT_INTERFACE, &disk_control_ext);
3110 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
3113 if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble))
3114 rumble_cb = rumble.set_rumble_state;
3116 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
3117 * we have to do this because cache misses and some IO penalties
3118 * are not emulated. Warning: changing this may break compatibility. */
3119 Config.cycle_multiplier = CYCLE_MULT_DEFAULT;
3120 #if defined(HAVE_PRE_ARMV7) && !defined(_3DS)
3121 Config.cycle_multiplier = 200;
3123 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
3124 pl_rearmed_cbs.gpu_peops.dwActFixes = GPU_PEOPS_OLD_FRAME_SKIP;
3125 spu_config.iUseFixedUpdates = 1;
3127 SaveFuncs.open = save_open;
3128 SaveFuncs.read = save_read;
3129 SaveFuncs.write = save_write;
3130 SaveFuncs.seek = save_seek;
3131 SaveFuncs.close = save_close;
3133 if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL))
3134 libretro_supports_bitmasks = true;
3136 check_system_specs();
3139 void retro_deinit(void)
3148 linearFree(vout_buf);
3157 libretro_supports_bitmasks = false;
3158 libretro_supports_option_categories = false;
3160 show_input_settings = true;
3162 show_advanced_gpu_peops_settings = true;
3165 show_advanced_gpu_unai_settings = true;
3168 /* Have to reset disks struct, otherwise
3169 * fnames/flabels will leak memory */
3171 frameskip_type = FRAMESKIP_NONE;
3172 frameskip_threshold = 0;
3173 frameskip_interval = 0;
3174 frameskip_counter = 0;
3175 retro_audio_buff_active = false;
3176 retro_audio_buff_occupancy = 0;
3177 retro_audio_buff_underrun = false;
3178 retro_audio_latency = 0;
3179 update_audio_latency = false;
3183 #include <psp2/kernel/threadmgr.h>
3184 int usleep(unsigned long us)
3186 sceKernelDelayThread(us);
3190 void SysPrintf(const char *fmt, ...)
3195 va_start(list, fmt);
3196 vsprintf(msg, fmt, list);
3200 log_cb(RETRO_LOG_INFO, "%s", msg);
3203 /* Prints debug-level logs */
3204 void SysDLog(const char *fmt, ...)
3209 va_start(list, fmt);
3210 vsprintf(msg, fmt, list);
3214 log_cb(RETRO_LOG_DEBUG, "%s", msg);
3217 // vim:sw=3:ts=3:expandtab