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 "../libpcsxcore/gpu.h"
31 #include "../libpcsxcore/database.h"
32 #include "../plugins/dfsound/out.h"
33 #include "../plugins/dfsound/spu_config.h"
38 #include "plugin_lib.h"
39 #include "arm_features.h"
43 #include "libretro_core_options.h"
45 #ifdef USE_LIBRETRO_VFS
46 #include <streams/file_stream_transforms.h>
50 #include "3ds/3ds_utils.h"
53 #define PORTS_NUMBER 8
56 #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 #define MAX(a, b) ((a) > (b) ? (a) : (b))
63 #define ISHEXDEC ((buf[cursor] >= '0') && (buf[cursor] <= '9')) || ((buf[cursor] >= 'a') && (buf[cursor] <= 'f')) || ((buf[cursor] >= 'A') && (buf[cursor] <= 'F'))
65 #define INTERNAL_FPS_SAMPLE_PERIOD 64
67 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
68 static int rebootemu = 0;
70 static retro_video_refresh_t video_cb;
71 static retro_input_poll_t input_poll_cb;
72 static retro_input_state_t input_state_cb;
73 static retro_environment_t environ_cb;
74 static retro_audio_sample_batch_t audio_batch_cb;
75 static retro_set_rumble_state_t rumble_cb;
76 static struct retro_log_callback logging;
77 static retro_log_printf_t log_cb;
79 static unsigned msg_interface_version = 0;
81 static void *vout_buf;
82 static void *vout_buf_ptr;
83 static int vout_width, vout_height;
84 static int vout_fb_dirty;
85 static int psx_w, psx_h;
86 static bool vout_can_dupe;
87 static bool duping_enable;
88 static bool found_bios;
89 static bool display_internal_fps = false;
90 static unsigned frame_count = 0;
91 static bool libretro_supports_bitmasks = false;
92 static bool libretro_supports_option_categories = false;
93 static bool show_input_settings = true;
95 static bool show_advanced_gpu_peops_settings = true;
98 static bool show_advanced_gpu_unai_settings = true;
100 static float mouse_sensitivity = 1.0f;
101 static unsigned int disk_current_index;
107 FRAMESKIP_AUTO_THRESHOLD,
108 FRAMESKIP_FIXED_INTERVAL
111 static unsigned frameskip_type = FRAMESKIP_NONE;
112 static unsigned frameskip_threshold = 0;
113 static unsigned frameskip_interval = 0;
114 static unsigned frameskip_counter = 0;
116 static int retro_audio_buff_active = false;
117 static unsigned retro_audio_buff_occupancy = 0;
118 static int retro_audio_buff_underrun = false;
120 static unsigned retro_audio_latency = 0;
121 static int update_audio_latency = false;
123 static unsigned previous_width = 0;
124 static unsigned previous_height = 0;
126 static int plugins_opened;
127 static int is_pal_mode;
129 /* memory card data */
130 extern char Mcd1Data[MCD_SIZE];
131 extern char Mcd2Data[MCD_SIZE];
132 extern char McdDisable[2];
134 /* PCSX ReARMed core calls and stuff */
136 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
137 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
138 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
139 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE
141 int in_analog_left[8][2] = { { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 } };
142 int in_analog_right[8][2] = { { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 } };
143 unsigned short in_keystate[PORTS_NUMBER];
147 int in_enable_vibration = 1;
148 int in_enable_crosshair[2] = { 0, 0 };
150 // NegCon adjustment parameters
151 // > The NegCon 'twist' action is somewhat awkward when mapped
152 // to a standard analog stick -> user should be able to tweak
153 // response/deadzone for comfort
154 // > When response is linear, 'additional' deadzone (set here)
155 // may be left at zero, since this is normally handled via in-game
157 // > When response is non-linear, deadzone should be set to match the
158 // controller being used (otherwise precision may be lost)
159 // > negcon_linearity:
160 // - 1: Response is linear - recommended when using racing wheel
161 // peripherals, not recommended for standard gamepads
162 // - 2: Response is quadratic - optimal setting for gamepads
163 // - 3: Response is cubic - enables precise fine control, but
164 // difficult to use...
165 #define NEGCON_RANGE 0x7FFF
166 static int negcon_deadzone = 0;
167 static int negcon_linearity = 1;
169 static bool axis_bounds_modifier;
171 /* PSX max resolution is 640x512, but with enhancement it's 1024x512 */
172 #define VOUT_MAX_WIDTH 1024
173 #define VOUT_MAX_HEIGHT 512
176 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info) { return false; }
177 void retro_unload_game(void) {}
178 static int vout_open(void) { return 0; }
179 static void vout_close(void) {}
180 static int snd_init(void) { return 0; }
181 static void snd_finish(void) {}
182 static int snd_busy(void) { return 0; }
184 #define GPU_PEOPS_ODD_EVEN_BIT (1 << 0)
185 #define GPU_PEOPS_EXPAND_SCREEN_WIDTH (1 << 1)
186 #define GPU_PEOPS_IGNORE_BRIGHTNESS (1 << 2)
187 #define GPU_PEOPS_DISABLE_COORD_CHECK (1 << 3)
188 #define GPU_PEOPS_LAZY_SCREEN_UPDATE (1 << 6)
189 #define GPU_PEOPS_OLD_FRAME_SKIP (1 << 7)
190 #define GPU_PEOPS_REPEATED_TRIANGLES (1 << 8)
191 #define GPU_PEOPS_QUADS_WITH_TRIANGLES (1 << 9)
192 #define GPU_PEOPS_FAKE_BUSY_STATE (1 << 10)
194 static void init_memcard(char *mcd_data)
199 memset(mcd_data, 0, MCD_SIZE);
201 mcd_data[off++] = 'M';
202 mcd_data[off++] = 'C';
204 mcd_data[off++] = 0x0e;
206 for (i = 0; i < 15; i++)
208 mcd_data[off++] = 0xa0;
210 mcd_data[off++] = 0xff;
211 mcd_data[off++] = 0xff;
213 mcd_data[off++] = 0xa0;
216 for (i = 0; i < 20; i++)
218 mcd_data[off++] = 0xff;
219 mcd_data[off++] = 0xff;
220 mcd_data[off++] = 0xff;
221 mcd_data[off++] = 0xff;
223 mcd_data[off++] = 0xff;
224 mcd_data[off++] = 0xff;
229 static void set_vout_fb()
231 struct retro_framebuffer fb = { 0 };
233 fb.width = vout_width;
234 fb.height = vout_height;
235 fb.access_flags = RETRO_MEMORY_ACCESS_WRITE;
237 if (environ_cb(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &fb) && fb.format == RETRO_PIXEL_FORMAT_RGB565)
238 vout_buf_ptr = (uint16_t *)fb.data;
240 vout_buf_ptr = vout_buf;
243 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
250 if (previous_width != vout_width || previous_height != vout_height)
252 previous_width = vout_width;
253 previous_height = vout_height;
255 struct retro_system_av_info info;
256 retro_get_system_av_info(&info);
257 environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &info.geometry);
263 #ifndef FRONTEND_SUPPORTS_RGB565
264 static void convert(void *buf, size_t bytes)
266 unsigned int i, v, *p = buf;
268 for (i = 0; i < bytes / 4; i++)
271 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
276 // Function to add crosshairs
277 static void addCrosshair(int port, int crosshair_color, unsigned short *buffer, int bufferStride, int pos_x, int pos_y, int thickness, int size_x, int size_y) {
278 for (port = 0; port < 2; port++) {
279 // Draw the horizontal line of the crosshair
280 for (int i = pos_y - thickness / 2; i <= pos_y + thickness / 2; i++) {
281 for (int j = pos_x - size_x / 2; j <= pos_x + size_x / 2; j++) {
282 if ((i + vout_height) >= 0 && (i + vout_height) < bufferStride && j >= 0 && j < bufferStride && in_enable_crosshair[port] > 0)
283 buffer[i * bufferStride + j] = crosshair_color;
287 // Draw the vertical line of the crosshair
288 for (int i = pos_x - thickness / 2; i <= pos_x + thickness / 2; i++) {
289 for (int j = pos_y - size_y / 2; j <= pos_y + size_y / 2; j++) {
290 if (i >= 0 && i < bufferStride && (j + vout_height) >= 0 && (j + vout_height) < bufferStride && in_enable_crosshair[port] > 0)
291 buffer[j * bufferStride + i] = crosshair_color;
297 struct CrosshairInfo {
298 int pos_x, pos_y, thickness, size_x, size_y;
301 // Calculate size and position of crosshairs
302 static void CrosshairDimensions(int port, struct CrosshairInfo *info) {
303 int gunx = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X);
304 int guny = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y);
305 if (gunx == 32767) // Prevent crosshairs from wrapping around right side of screen to left
306 info->pos_x = (gunx + 32767.0f) * vout_width / 65534.0f - 0.5f;
308 info->pos_x = (gunx + 32767.0f) * vout_width / 65534.0f;
309 info->pos_y = (guny + 32767.0f) * vout_height / 65534.0f - vout_height;
310 info->thickness = pl_rearmed_cbs.gpu_neon.enhancement_enable ? 4 : 2;
311 info->size_x = psx_w * (pl_rearmed_cbs.gpu_neon.enhancement_enable ? 2 : 1) / 40.0f;
312 info->size_y = psx_h * (pl_rearmed_cbs.gpu_neon.enhancement_enable ? 2 : 1) * (4.0f / 3.0f) / 40.0f;
315 static void vout_flip(const void *vram, int stride, int bgr24,
316 int x, int y, int w, int h, int dims_changed)
318 unsigned short *dest = vout_buf_ptr;
319 const unsigned short *src = vram;
320 int dstride = vout_width, h1 = h;
323 if (vram == NULL || dims_changed || (in_enable_crosshair[0] + in_enable_crosshair[1]) > 0)
325 memset(vout_buf_ptr, 0, dstride * vout_height * 2);
331 dest += x + y * dstride;
335 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
336 for (; h1-- > 0; dest += dstride, src += stride)
338 bgr888_to_rgb565(dest, src, w * 3);
343 for (; h1-- > 0; dest += dstride, src += stride)
345 bgr555_to_rgb565(dest, src, w * 2);
349 for (port = 0; port < 2; port++) {
350 if (in_enable_crosshair[port] > 0 && (in_type[port] == PSE_PAD_TYPE_GUNCON || in_type[port] == PSE_PAD_TYPE_GUN))
352 struct CrosshairInfo crosshairInfo;
353 CrosshairDimensions(port, &crosshairInfo);
354 addCrosshair(port, in_enable_crosshair[port], dest, dstride, crosshairInfo.pos_x, crosshairInfo.pos_y, crosshairInfo.thickness, crosshairInfo.size_x, crosshairInfo.size_y);
359 #ifndef FRONTEND_SUPPORTS_RGB565
360 convert(vout_buf_ptr, vout_width * vout_height * 2);
363 pl_rearmed_cbs.flip_cnt++;
375 psx_map_t custom_psx_maps[] = {
376 { NULL, 0x13000000, 0x210000, MAP_TAG_RAM }, // 0x80000000
377 { NULL, 0x12800000, 0x010000, MAP_TAG_OTHER }, // 0x1f800000
378 { NULL, 0x12c00000, 0x080000, MAP_TAG_OTHER }, // 0x1fc00000
379 { NULL, 0x11000000, 0x800000, MAP_TAG_LUTS }, // 0x08000000
380 { NULL, 0x12000000, 0x201000, MAP_TAG_VRAM }, // 0x00000000
383 void *pl_3ds_mmap(unsigned long addr, size_t size, int is_fixed,
391 psx_map_t *custom_map = custom_psx_maps;
393 for (; custom_map->size; custom_map++)
395 if ((custom_map->size == size) && (custom_map->tag == tag))
397 uint32_t ptr_aligned, tmp;
399 custom_map->buffer = malloc(size + 0x1000);
400 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
402 if (svcControlMemory(&tmp, (void *)custom_map->target_map, (void *)ptr_aligned, size, MEMOP_MAP, 0x3) < 0)
404 SysPrintf("could not map memory @0x%08X\n", custom_map->target_map);
408 return (void *)custom_map->target_map;
416 void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
422 psx_map_t *custom_map = custom_psx_maps;
424 for (; custom_map->size; custom_map++)
426 if ((custom_map->target_map == (uint32_t)ptr))
428 uint32_t ptr_aligned, tmp;
430 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
432 svcControlMemory(&tmp, (void *)custom_map->target_map, (void *)ptr_aligned, size, MEMOP_UNMAP, 0x3);
434 free(custom_map->buffer);
435 custom_map->buffer = NULL;
454 static void *addr = NULL;
456 psx_map_t custom_psx_maps[] = {
457 { NULL, 0x800000, MAP_TAG_LUTS },
458 { NULL, 0x080000, MAP_TAG_OTHER },
459 { NULL, 0x010000, MAP_TAG_OTHER },
460 { NULL, 0x201000, MAP_TAG_VRAM },
461 { NULL, 0x802000, MAP_TAG_VRAM }, // enhanced renderer
462 { NULL, 0x210000, MAP_TAG_RAM },
469 addr = malloc(64 * 1024 * 1024);
472 tmpaddr = (void *)(((size_t)addr + 0xFFFFFF) & ~0xFFFFFF);
473 custom_psx_maps[0].buffer = tmpaddr + 0x0000000;
474 custom_psx_maps[1].buffer = tmpaddr + 0x0800000;
475 custom_psx_maps[2].buffer = tmpaddr + 0x0880000;
476 custom_psx_maps[3].buffer = tmpaddr + 0x0900000;
477 custom_psx_maps[4].buffer = tmpaddr + 0x1000000;
478 custom_psx_maps[5].buffer = tmpaddr + 0x2000000;
480 for(n = 0; n < 5; n++){
481 sceClibPrintf("addr reserved %x\n",custom_psx_maps[n].buffer);
487 void deinit_vita_mmap()
490 for (i = 0; i < sizeof(custom_psx_maps) / sizeof(custom_psx_maps[0]); i++) {
491 custom_psx_maps[i].buffer = NULL;
492 custom_psx_maps[i].used = 0;
497 void *pl_vita_mmap(unsigned long addr, size_t size, int is_fixed,
503 psx_map_t *custom_map = custom_psx_maps;
505 for (; custom_map->size; custom_map++)
507 if (custom_map->size == size && custom_map->tag == tag && !custom_map->used)
509 custom_map->used = 1;
510 return custom_map->buffer;
517 void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag)
521 psx_map_t *custom_map = custom_psx_maps;
523 for (; custom_map->size; custom_map++)
525 if ((custom_map->buffer == ptr))
527 custom_map->used = 0;
536 static void *pl_mmap(unsigned int size)
538 return psxMap(0, size, 0, MAP_TAG_VRAM);
541 static void pl_munmap(void *ptr, unsigned int size)
543 psxUnmap(ptr, size, MAP_TAG_VRAM);
546 struct rearmed_cbs pl_rearmed_cbs = {
547 .pl_vout_open = vout_open,
548 .pl_vout_set_mode = vout_set_mode,
549 .pl_vout_flip = vout_flip,
550 .pl_vout_close = vout_close,
553 .gpu_state_change = gpu_state_change,
554 /* from psxcounters */
555 .gpu_hcnt = &hSyncCount,
556 .gpu_frame_count = &frame_counter,
559 void pl_frame_limit(void)
561 /* called once per frame, make psxCpu->Execute() above return */
565 void pl_timing_prepare(int is_pal)
567 is_pal_mode = is_pal;
570 void plat_trigger_vibrate(int pad, int low, int high)
575 if (in_enable_vibration)
577 rumble_cb(pad, RETRO_RUMBLE_STRONG, high << 8);
578 rumble_cb(pad, RETRO_RUMBLE_WEAK, low ? 0xffff : 0x0);
582 //Percentage distance of screen to adjust for Konami Gun
583 static float KonamiGunAdjustX = 0;
584 static float KonamiGunAdjustY = 0;
586 void pl_gun_byte2(int port, unsigned char byte)
589 float justifier_multiplier = 0;
590 int justifier_width = psx_w;
591 int justifier_height = psx_h;
592 int justifier_offscreen = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN);
593 int justifier_reload = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_RELOAD);
595 if (justifier_width == 256)
596 justifier_multiplier = is_pal_mode ? .157086f : .158532f;
597 else if (justifier_width == 320)
598 justifier_multiplier = is_pal_mode ? .196358f : .198166f;
599 else if (justifier_width == 384)
600 justifier_multiplier = is_pal_mode ? .224409f : .226475f;
601 else if (justifier_width == 512)
602 justifier_multiplier = is_pal_mode ? .314173f : .317065f;
603 else // (justifier_width == 640)
604 justifier_multiplier = is_pal_mode ? .392717f : .396332f;
606 int gunx = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X);
607 int guny = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y);
609 //Default offset of +105 for X and -12 for Y is chosen to obtain alignment in Die Hard Trilogy, which has no calibration feature
610 int gunx_scaled = ((gunx + 32767.0f) / 65534.0f + KonamiGunAdjustX) * justifier_width / justifier_multiplier + 105.0f;
611 int guny_scaled = ((guny + 32767.0f) / 65534.0f + KonamiGunAdjustY) * justifier_height - 12.0f;
613 if ((byte & 0x10) && !justifier_offscreen && !justifier_reload)
615 psxScheduleIrq10(irq_count, gunx_scaled, guny_scaled);
620 static void snd_feed(void *buf, int bytes)
622 if (audio_batch_cb != NULL)
623 audio_batch_cb(buf, bytes / 4);
626 void out_register_libretro(struct out_driver *drv)
628 drv->name = "libretro";
629 drv->init = snd_init;
630 drv->finish = snd_finish;
631 drv->busy = snd_busy;
632 drv->feed = snd_feed;
635 #define RETRO_DEVICE_PSE_STANDARD RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0)
636 #define RETRO_DEVICE_PSE_ANALOG RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 0)
637 #define RETRO_DEVICE_PSE_DUALSHOCK RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 1)
638 #define RETRO_DEVICE_PSE_NEGCON RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 2)
639 #define RETRO_DEVICE_PSE_GUNCON RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_LIGHTGUN, 0)
640 #define RETRO_DEVICE_PSE_JUSTIFIER RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_LIGHTGUN, 1)
641 #define RETRO_DEVICE_PSE_MOUSE RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_MOUSE, 0)
643 static char *get_pse_pad_label[] = {
644 "none", "mouse", "negcon", "konami gun", "standard", "analog", "guncon", "dualshock"
647 static const struct retro_controller_description pads[8] =
649 { "standard", RETRO_DEVICE_JOYPAD },
650 { "analog", RETRO_DEVICE_PSE_ANALOG },
651 { "dualshock", RETRO_DEVICE_PSE_DUALSHOCK },
652 { "negcon", RETRO_DEVICE_PSE_NEGCON },
653 { "guncon", RETRO_DEVICE_PSE_GUNCON },
654 { "konami gun", RETRO_DEVICE_PSE_JUSTIFIER },
655 { "mouse", RETRO_DEVICE_PSE_MOUSE },
659 static const struct retro_controller_info ports[9] =
674 static bool update_option_visibility(void)
676 struct retro_variable var = {0};
677 struct retro_core_option_display option_display = {0};
678 bool updated = false;
681 /* If frontend supports core option categories
682 * then show/hide core option entries are ignored
683 * and no options should be hidden */
684 if (libretro_supports_option_categories)
687 var.key = "pcsx_rearmed_show_input_settings";
690 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
692 bool show_input_settings_prev =
695 show_input_settings = true;
696 if (strcmp(var.value, "disabled") == 0)
697 show_input_settings = false;
699 if (show_input_settings !=
700 show_input_settings_prev)
702 char input_option[][50] = {
703 "pcsx_rearmed_analog_axis_modifier",
704 "pcsx_rearmed_vibration",
705 "pcsx_rearmed_multitap",
706 "pcsx_rearmed_negcon_deadzone",
707 "pcsx_rearmed_negcon_response",
708 "pcsx_rearmed_input_sensitivity",
709 "pcsx_rearmed_crosshair1",
710 "pcsx_rearmed_crosshair2",
711 "pcsx_rearmed_konamigunadjustx",
712 "pcsx_rearmed_konamigunadjusty",
713 "pcsx_rearmed_gunconadjustx",
714 "pcsx_rearmed_gunconadjusty",
715 "pcsx_rearmed_gunconadjustratiox",
716 "pcsx_rearmed_gunconadjustratioy"
719 option_display.visible = show_input_settings;
722 i < (sizeof(input_option) /
723 sizeof(input_option[0]));
726 option_display.key = input_option[i];
727 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
735 var.key = "pcsx_rearmed_show_gpu_peops_settings";
738 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
740 bool show_advanced_gpu_peops_settings_prev =
741 show_advanced_gpu_peops_settings;
743 show_advanced_gpu_peops_settings = true;
744 if (strcmp(var.value, "disabled") == 0)
745 show_advanced_gpu_peops_settings = false;
747 if (show_advanced_gpu_peops_settings !=
748 show_advanced_gpu_peops_settings_prev)
751 struct retro_core_option_display option_display;
752 char gpu_peops_option[][45] = {
753 "pcsx_rearmed_gpu_peops_odd_even_bit",
754 "pcsx_rearmed_gpu_peops_expand_screen_width",
755 "pcsx_rearmed_gpu_peops_ignore_brightness",
756 "pcsx_rearmed_gpu_peops_disable_coord_check",
757 "pcsx_rearmed_gpu_peops_lazy_screen_update",
758 "pcsx_rearmed_gpu_peops_repeated_triangles",
759 "pcsx_rearmed_gpu_peops_quads_with_triangles",
760 "pcsx_rearmed_gpu_peops_fake_busy_state"
763 option_display.visible = show_advanced_gpu_peops_settings;
766 i < (sizeof(gpu_peops_option) /
767 sizeof(gpu_peops_option[0]));
770 option_display.key = gpu_peops_option[i];
771 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
780 var.key = "pcsx_rearmed_show_gpu_unai_settings";
783 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
785 bool show_advanced_gpu_unai_settings_prev =
786 show_advanced_gpu_unai_settings;
788 show_advanced_gpu_unai_settings = true;
789 if (strcmp(var.value, "disabled") == 0)
790 show_advanced_gpu_unai_settings = false;
792 if (show_advanced_gpu_unai_settings !=
793 show_advanced_gpu_unai_settings_prev)
796 struct retro_core_option_display option_display;
797 char gpu_unai_option[][40] = {
798 "pcsx_rearmed_gpu_unai_blending",
799 "pcsx_rearmed_gpu_unai_lighting",
800 "pcsx_rearmed_gpu_unai_fast_lighting",
801 "pcsx_rearmed_gpu_unai_scale_hires",
804 option_display.visible = show_advanced_gpu_unai_settings;
807 i < (sizeof(gpu_unai_option) /
808 sizeof(gpu_unai_option[0]));
811 option_display.key = gpu_unai_option[i];
812 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
823 void retro_set_environment(retro_environment_t cb)
825 bool option_categories = false;
826 #ifdef USE_LIBRETRO_VFS
827 struct retro_vfs_interface_info vfs_iface_info;
832 if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
833 log_cb = logging.log;
835 environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
838 * An annoyance: retro_set_environment() can be called
839 * multiple times, and depending upon the current frontend
840 * state various environment callbacks may be disabled.
841 * This means the reported 'categories_supported' status
842 * may change on subsequent iterations. We therefore have
843 * to record whether 'categories_supported' is true on any
844 * iteration, and latch the result */
845 libretro_set_core_options(environ_cb, &option_categories);
846 libretro_supports_option_categories |= option_categories;
848 /* If frontend supports core option categories,
849 * any show/hide core option entries are unused
850 * and should be hidden */
851 if (libretro_supports_option_categories)
853 struct retro_core_option_display option_display;
854 option_display.visible = false;
856 option_display.key = "pcsx_rearmed_show_input_settings";
857 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
861 option_display.key = "pcsx_rearmed_show_gpu_peops_settings";
862 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
866 option_display.key = "pcsx_rearmed_show_gpu_unai_settings";
867 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
871 /* If frontend does not support core option
872 * categories, core options may be shown/hidden
873 * at runtime. In this case, register 'update
874 * display' callback, so frontend can update
875 * core options menu without calling retro_run() */
878 struct retro_core_options_update_display_callback update_display_cb;
879 update_display_cb.callback = update_option_visibility;
881 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_UPDATE_DISPLAY_CALLBACK,
885 #ifdef USE_LIBRETRO_VFS
886 vfs_iface_info.required_interface_version = 1;
887 vfs_iface_info.iface = NULL;
888 if (environ_cb(RETRO_ENVIRONMENT_GET_VFS_INTERFACE, &vfs_iface_info))
889 filestream_vfs_init(&vfs_iface_info);
893 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
894 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
895 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
896 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
897 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
899 unsigned retro_api_version(void)
901 return RETRO_API_VERSION;
904 static void update_multitap(void)
906 struct retro_variable var = { 0 };
912 var.key = "pcsx_rearmed_multitap";
913 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value))
915 if (strcmp(var.value, "port 1") == 0)
917 else if (strcmp(var.value, "port 2") == 0)
919 else if (strcmp(var.value, "ports 1 and 2") == 0)
927 void retro_set_controller_port_device(unsigned port, unsigned device)
929 if (port >= PORTS_NUMBER)
934 case RETRO_DEVICE_JOYPAD:
935 case RETRO_DEVICE_PSE_STANDARD:
936 in_type[port] = PSE_PAD_TYPE_STANDARD;
938 case RETRO_DEVICE_PSE_ANALOG:
939 in_type[port] = PSE_PAD_TYPE_ANALOGJOY;
941 case RETRO_DEVICE_PSE_DUALSHOCK:
942 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
944 case RETRO_DEVICE_PSE_MOUSE:
945 in_type[port] = PSE_PAD_TYPE_MOUSE;
947 case RETRO_DEVICE_PSE_NEGCON:
948 in_type[port] = PSE_PAD_TYPE_NEGCON;
950 case RETRO_DEVICE_PSE_GUNCON:
951 in_type[port] = PSE_PAD_TYPE_GUNCON;
953 case RETRO_DEVICE_PSE_JUSTIFIER:
954 in_type[port] = PSE_PAD_TYPE_GUN;
956 case RETRO_DEVICE_NONE:
958 in_type[port] = PSE_PAD_TYPE_NONE;
962 SysPrintf("port: %u device: %s\n", port + 1, get_pse_pad_label[in_type[port]]);
965 void retro_get_system_info(struct retro_system_info *info)
968 #define GIT_VERSION ""
970 memset(info, 0, sizeof(*info));
971 info->library_name = "PCSX-ReARMed";
972 info->library_version = "r23l" GIT_VERSION;
973 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u|chd|iso|exe";
974 info->need_fullpath = true;
977 void retro_get_system_av_info(struct retro_system_av_info *info)
979 unsigned geom_height = vout_height > 0 ? vout_height : 240;
980 unsigned geom_width = vout_width > 0 ? vout_width : 320;
982 memset(info, 0, sizeof(*info));
983 info->timing.fps = is_pal_mode ? 50.0 : 60.0;
984 info->timing.sample_rate = 44100.0;
985 info->geometry.base_width = geom_width;
986 info->geometry.base_height = geom_height;
987 info->geometry.max_width = VOUT_MAX_WIDTH;
988 info->geometry.max_height = VOUT_MAX_HEIGHT;
989 info->geometry.aspect_ratio = 4.0 / 3.0;
993 size_t retro_serialize_size(void)
995 // it's currently 4380651-4397047 bytes,
996 // but have some reserved for future
1007 static void *save_open(const char *name, const char *mode)
1011 if (name == NULL || mode == NULL)
1014 fp = malloc(sizeof(*fp));
1018 fp->buf = (char *)name;
1020 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
1025 static int save_read(void *file, void *buf, u32 len)
1027 struct save_fp *fp = file;
1028 if (fp == NULL || buf == NULL)
1031 memcpy(buf, fp->buf + fp->pos, len);
1036 static int save_write(void *file, const void *buf, u32 len)
1038 struct save_fp *fp = file;
1039 if (fp == NULL || buf == NULL)
1042 memcpy(fp->buf + fp->pos, buf, len);
1047 static long save_seek(void *file, long offs, int whence)
1049 struct save_fp *fp = file;
1066 static void save_close(void *file)
1068 struct save_fp *fp = file;
1069 size_t r_size = retro_serialize_size();
1073 if (fp->pos > r_size)
1074 SysPrintf("ERROR: save buffer overflow detected\n");
1075 else if (fp->is_write && fp->pos < r_size)
1076 // make sure we don't save trash in leftover space
1077 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
1081 bool retro_serialize(void *data, size_t size)
1084 CdromFrontendId = disk_current_index;
1085 ret = SaveState(data);
1086 return ret == 0 ? true : false;
1089 static bool disk_set_image_index(unsigned int index);
1091 bool retro_unserialize(const void *data, size_t size)
1094 CdromFrontendId = -1;
1095 ret = LoadState(data);
1098 if (CdromFrontendId != -1 && CdromFrontendId != disk_current_index)
1099 disk_set_image_index(CdromFrontendId);
1104 void retro_cheat_reset(void)
1109 void retro_cheat_set(unsigned index, bool enabled, const char *code)
1114 // cheat funcs are destructive, need a copy...
1119 //Prepare buffered cheat for PCSX's AddCheat fucntion.
1126 if (++nonhexdec % 2)
1138 if (index < NumCheats)
1139 ret = EditCheat(index, "", buf);
1141 ret = AddCheat("", buf);
1145 SysPrintf("Failed to set cheat %#u\n", index);
1146 else if (index < NumCheats)
1147 Cheats[index].Enabled = enabled;
1151 // just in case, maybe a win-rt port in the future?
1159 #define PATH_MAX 4096
1162 /* multidisk support */
1163 static unsigned int disk_initial_index;
1164 static char disk_initial_path[PATH_MAX];
1165 static bool disk_ejected;
1166 static unsigned int disk_count;
1167 static struct disks_state
1171 int internal_index; // for multidisk eboots
1174 static void get_disk_label(char *disk_label, const char *disk_path, size_t len)
1176 const char *base = NULL;
1178 if (!disk_path || (*disk_path == '\0'))
1181 base = strrchr(disk_path, SLASH);
1188 strncpy(disk_label, base, len - 1);
1189 disk_label[len - 1] = '\0';
1191 char *ext = strrchr(disk_label, '.');
1196 static void disk_init(void)
1200 disk_ejected = false;
1201 disk_current_index = 0;
1204 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++)
1206 if (disks[i].fname != NULL)
1208 free(disks[i].fname);
1209 disks[i].fname = NULL;
1211 if (disks[i].flabel != NULL)
1213 free(disks[i].flabel);
1214 disks[i].flabel = NULL;
1216 disks[i].internal_index = 0;
1220 static bool disk_set_eject_state(bool ejected)
1223 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
1226 disk_ejected = ejected;
1230 static bool disk_get_eject_state(void)
1232 /* can't be controlled by emulated software */
1233 return disk_ejected;
1236 static unsigned int disk_get_image_index(void)
1238 return disk_current_index;
1241 static bool disk_set_image_index(unsigned int index)
1243 if (index >= sizeof(disks) / sizeof(disks[0]))
1247 CdromLabel[0] = '\0';
1249 if (disks[index].fname == NULL)
1251 SysPrintf("missing disk #%u\n", index);
1254 // RetroArch specifies "no disk" with index == count,
1255 // so don't fail here..
1256 disk_current_index = index;
1260 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
1261 disks[index].fname, disks[index].internal_index);
1263 cdrIsoMultidiskSelect = disks[index].internal_index;
1264 set_cd_image(disks[index].fname);
1265 if (ReloadCdromPlugin() < 0)
1267 SysPrintf("failed to load cdr plugin\n");
1272 SysPrintf("failed to open cdr plugin\n");
1278 SetCdOpenCaseTime(time(NULL) + 2);
1282 disk_current_index = index;
1286 static unsigned int disk_get_num_images(void)
1291 static bool disk_replace_image_index(unsigned index,
1292 const struct retro_game_info *info)
1294 char *old_fname = NULL;
1295 char *old_flabel = NULL;
1298 if (index >= sizeof(disks) / sizeof(disks[0]))
1301 old_fname = disks[index].fname;
1302 old_flabel = disks[index].flabel;
1304 disks[index].fname = NULL;
1305 disks[index].flabel = NULL;
1306 disks[index].internal_index = 0;
1310 char disk_label[PATH_MAX];
1311 disk_label[0] = '\0';
1313 disks[index].fname = strdup(info->path);
1315 get_disk_label(disk_label, info->path, PATH_MAX);
1316 disks[index].flabel = strdup(disk_label);
1318 if (index == disk_current_index)
1319 ret = disk_set_image_index(index);
1322 if (old_fname != NULL)
1325 if (old_flabel != NULL)
1331 static bool disk_add_image_index(void)
1333 if (disk_count >= 8)
1340 static bool disk_set_initial_image(unsigned index, const char *path)
1342 if (index >= sizeof(disks) / sizeof(disks[0]))
1345 if (!path || (*path == '\0'))
1348 disk_initial_index = index;
1350 strncpy(disk_initial_path, path, sizeof(disk_initial_path) - 1);
1351 disk_initial_path[sizeof(disk_initial_path) - 1] = '\0';
1356 static bool disk_get_image_path(unsigned index, char *path, size_t len)
1358 const char *fname = NULL;
1363 if (index >= sizeof(disks) / sizeof(disks[0]))
1366 fname = disks[index].fname;
1368 if (!fname || (*fname == '\0'))
1371 strncpy(path, fname, len - 1);
1372 path[len - 1] = '\0';
1377 static bool disk_get_image_label(unsigned index, char *label, size_t len)
1379 const char *flabel = NULL;
1384 if (index >= sizeof(disks) / sizeof(disks[0]))
1387 flabel = disks[index].flabel;
1389 if (!flabel || (*flabel == '\0'))
1392 strncpy(label, flabel, len - 1);
1393 label[len - 1] = '\0';
1398 static struct retro_disk_control_callback disk_control = {
1399 .set_eject_state = disk_set_eject_state,
1400 .get_eject_state = disk_get_eject_state,
1401 .get_image_index = disk_get_image_index,
1402 .set_image_index = disk_set_image_index,
1403 .get_num_images = disk_get_num_images,
1404 .replace_image_index = disk_replace_image_index,
1405 .add_image_index = disk_add_image_index,
1408 static struct retro_disk_control_ext_callback disk_control_ext = {
1409 .set_eject_state = disk_set_eject_state,
1410 .get_eject_state = disk_get_eject_state,
1411 .get_image_index = disk_get_image_index,
1412 .set_image_index = disk_set_image_index,
1413 .get_num_images = disk_get_num_images,
1414 .replace_image_index = disk_replace_image_index,
1415 .add_image_index = disk_add_image_index,
1416 .set_initial_image = disk_set_initial_image,
1417 .get_image_path = disk_get_image_path,
1418 .get_image_label = disk_get_image_label,
1421 static char base_dir[1024];
1423 static bool read_m3u(const char *file)
1426 char name[PATH_MAX];
1427 FILE *fp = fopen(file, "r");
1431 while (fgets(line, sizeof(line), fp) && disk_count < sizeof(disks) / sizeof(disks[0]))
1435 char *carrige_return = strchr(line, '\r');
1437 *carrige_return = '\0';
1438 char *newline = strchr(line, '\n');
1442 if (line[0] != '\0')
1444 char disk_label[PATH_MAX];
1445 disk_label[0] = '\0';
1447 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
1448 disks[disk_count].fname = strdup(name);
1450 get_disk_label(disk_label, name, PATH_MAX);
1451 disks[disk_count].flabel = strdup(disk_label);
1458 return (disk_count != 0);
1461 static void extract_directory(char *buf, const char *path, size_t size)
1464 strncpy(buf, path, size - 1);
1465 buf[size - 1] = '\0';
1467 base = strrchr(buf, '/');
1469 base = strrchr(buf, '\\');
1480 #if defined(__QNX__) || defined(_WIN32)
1481 /* Blackberry QNX doesn't have strcasestr */
1484 * Find the first occurrence of find in s, ignore case.
1487 strcasestr(const char *s, const char *find)
1492 if ((c = *find++) != 0)
1494 c = tolower((unsigned char)c);
1500 if ((sc = *s++) == 0)
1502 } while ((char)tolower((unsigned char)sc) != c);
1503 } while (strncasecmp(s, find, len) != 0);
1510 static void set_retro_memmap(void)
1513 struct retro_memory_map retromap = { 0 };
1514 struct retro_memory_descriptor mmap = {
1515 0, psxM, 0, 0, 0, 0, 0x200000
1518 retromap.descriptors = &mmap;
1519 retromap.num_descriptors = 1;
1521 environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &retromap);
1525 static void show_notification(const char *msg_str,
1526 unsigned duration_ms, unsigned priority)
1528 if (msg_interface_version >= 1)
1530 struct retro_message_ext msg = {
1535 RETRO_MESSAGE_TARGET_ALL,
1536 RETRO_MESSAGE_TYPE_NOTIFICATION,
1539 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE_EXT, &msg);
1543 struct retro_message msg = {
1547 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
1551 static void retro_audio_buff_status_cb(
1552 bool active, unsigned occupancy, bool underrun_likely)
1554 retro_audio_buff_active = active;
1555 retro_audio_buff_occupancy = occupancy;
1556 retro_audio_buff_underrun = underrun_likely;
1559 static void retro_set_audio_buff_status_cb(void)
1561 if (frameskip_type == FRAMESKIP_NONE)
1563 environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, NULL);
1564 retro_audio_latency = 0;
1568 bool calculate_audio_latency = true;
1570 if (frameskip_type == FRAMESKIP_FIXED_INTERVAL)
1571 environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, NULL);
1574 struct retro_audio_buffer_status_callback buf_status_cb;
1575 buf_status_cb.callback = retro_audio_buff_status_cb;
1576 if (!environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK,
1579 retro_audio_buff_active = false;
1580 retro_audio_buff_occupancy = 0;
1581 retro_audio_buff_underrun = false;
1582 retro_audio_latency = 0;
1583 calculate_audio_latency = false;
1587 if (calculate_audio_latency)
1589 /* Frameskip is enabled - increase frontend
1590 * audio latency to minimise potential
1591 * buffer underruns */
1592 uint32_t frame_time_usec = 1000000.0 / (is_pal_mode ? 50.0 : 60.0);
1594 /* Set latency to 6x current frame time... */
1595 retro_audio_latency = (unsigned)(6 * frame_time_usec / 1000);
1597 /* ...then round up to nearest multiple of 32 */
1598 retro_audio_latency = (retro_audio_latency + 0x1F) & ~0x1F;
1602 update_audio_latency = true;
1603 frameskip_counter = 0;
1606 static void update_variables(bool in_flight);
1607 bool retro_load_game(const struct retro_game_info *info)
1610 unsigned int cd_index = 0;
1611 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
1612 bool is_exe = (strcasestr(info->path, ".exe") != NULL);
1615 struct retro_input_descriptor desc[] = {
1616 #define JOYP(port) \
1617 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" }, \
1618 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" }, \
1619 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" }, \
1620 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" }, \
1621 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" }, \
1622 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" }, \
1623 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" }, \
1624 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" }, \
1625 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" }, \
1626 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" }, \
1627 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" }, \
1628 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" }, \
1629 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" }, \
1630 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" }, \
1631 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" }, \
1632 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, \
1633 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" }, \
1634 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" }, \
1635 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" }, \
1636 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" }, \
1637 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_TRIGGER, "Gun Trigger" }, \
1638 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_RELOAD, "Gun Reload" }, \
1639 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A, "Gun Aux A" }, \
1640 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_B, "Gun Aux B" }, \
1641 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_START, "Gun Start" },
1657 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1659 #ifdef FRONTEND_SUPPORTS_RGB565
1660 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
1661 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
1663 SysPrintf("RGB565 supported, using it\n");
1667 if (info == NULL || info->path == NULL)
1669 SysPrintf("info->path required\n");
1673 update_variables(false);
1683 extract_directory(base_dir, info->path, sizeof(base_dir));
1687 if (!read_m3u(info->path))
1689 log_cb(RETRO_LOG_INFO, "failed to read m3u file\n");
1695 char disk_label[PATH_MAX];
1696 disk_label[0] = '\0';
1699 disks[0].fname = strdup(info->path);
1701 get_disk_label(disk_label, info->path, PATH_MAX);
1702 disks[0].flabel = strdup(disk_label);
1705 /* If this is an M3U file, attempt to set the
1706 * initial disk image */
1707 if (is_m3u && (disk_initial_index > 0) && (disk_initial_index < disk_count))
1709 const char *fname = disks[disk_initial_index].fname;
1711 if (fname && (*fname != '\0'))
1712 if (strcmp(disk_initial_path, fname) == 0)
1713 cd_index = disk_initial_index;
1716 set_cd_image(disks[cd_index].fname);
1717 disk_current_index = cd_index;
1719 /* have to reload after set_cd_image for correct cdr plugin */
1720 if (LoadPlugins() == -1)
1722 log_cb(RETRO_LOG_INFO, "failed to load plugins\n");
1729 if (OpenPlugins() == -1)
1731 log_cb(RETRO_LOG_INFO, "failed to open plugins\n");
1735 /* Handle multi-disk images (i.e. PBP)
1736 * > Cannot do this until after OpenPlugins() is
1737 * called (since this sets the value of
1738 * cdrIsoMultidiskCount) */
1739 if (!is_m3u && (cdrIsoMultidiskCount > 1))
1741 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
1743 /* Small annoyance: We need to change the label
1744 * of disk 0, so have to clear existing entries */
1745 if (disks[0].fname != NULL)
1746 free(disks[0].fname);
1747 disks[0].fname = NULL;
1749 if (disks[0].flabel != NULL)
1750 free(disks[0].flabel);
1751 disks[0].flabel = NULL;
1753 for (i = 0; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++)
1755 char disk_name[PATH_MAX - 16] = { 0 };
1756 char disk_label[PATH_MAX] = { 0 };
1758 disks[i].fname = strdup(info->path);
1760 get_disk_label(disk_name, info->path, sizeof(disk_name));
1761 snprintf(disk_label, sizeof(disk_label), "%s #%u", disk_name, (unsigned)i + 1);
1762 disks[i].flabel = strdup(disk_label);
1764 disks[i].internal_index = i;
1767 /* This is not an M3U file, so initial disk
1768 * image has not yet been set - attempt to
1770 if ((disk_initial_index > 0) && (disk_initial_index < disk_count))
1772 const char *fname = disks[disk_initial_index].fname;
1774 if (fname && (*fname != '\0'))
1775 if (strcmp(disk_initial_path, fname) == 0)
1776 cd_index = disk_initial_index;
1782 CdromLabel[0] = '\0';
1784 cdrIsoMultidiskSelect = disks[cd_index].internal_index;
1785 disk_current_index = cd_index;
1786 set_cd_image(disks[cd_index].fname);
1788 if (ReloadCdromPlugin() < 0)
1790 log_cb(RETRO_LOG_INFO, "failed to reload cdr plugins\n");
1795 log_cb(RETRO_LOG_INFO, "failed to open cdr plugin\n");
1801 /* set ports to use "standard controller" initially */
1802 for (i = 0; i < 8; ++i)
1803 in_type[i] = PSE_PAD_TYPE_STANDARD;
1805 plugin_call_rearmed_cbs();
1806 /* dfinput_activate(); */
1808 if (!is_exe && CheckCdrom() == -1)
1810 log_cb(RETRO_LOG_INFO, "unsupported/invalid CD image: %s\n", info->path);
1817 ret = Load(info->path);
1822 log_cb(RETRO_LOG_INFO, "could not load %s (%d)\n", is_exe ? "exe" : "CD", ret);
1828 retro_set_audio_buff_status_cb();
1830 if (check_unsatisfied_libcrypt())
1831 show_notification("LibCrypt protected game with missing SBI detected", 3000, 3);
1836 unsigned retro_get_region(void)
1838 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1841 void *retro_get_memory_data(unsigned id)
1843 if (id == RETRO_MEMORY_SAVE_RAM)
1845 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1851 size_t retro_get_memory_size(unsigned id)
1853 if (id == RETRO_MEMORY_SAVE_RAM)
1855 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1861 void retro_reset(void)
1863 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
1868 static const unsigned short retro_psx_map[] = {
1869 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
1870 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
1871 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
1872 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
1873 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
1874 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
1875 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
1876 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
1877 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
1878 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
1879 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
1880 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
1881 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
1882 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
1883 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
1884 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
1886 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
1888 //Percentage distance of screen to adjust for Guncon
1889 static int GunconAdjustX = 0;
1890 static int GunconAdjustY = 0;
1892 //Used when out by a percentage with Guncon
1893 static float GunconAdjustRatioX = 1;
1894 static float GunconAdjustRatioY = 1;
1896 static void update_variables(bool in_flight)
1898 struct retro_variable var;
1900 // Always enable GPU_PEOPS_OLD_FRAME_SKIP flag
1901 // (this is set in standalone, with no option
1903 int gpu_peops_fix = GPU_PEOPS_OLD_FRAME_SKIP;
1905 frameskip_type_t prev_frameskip_type;
1908 var.key = "pcsx_rearmed_frameskip_type";
1910 prev_frameskip_type = frameskip_type;
1911 frameskip_type = FRAMESKIP_NONE;
1912 pl_rearmed_cbs.frameskip = 0;
1914 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1916 if (strcmp(var.value, "auto") == 0)
1917 frameskip_type = FRAMESKIP_AUTO;
1918 if (strcmp(var.value, "auto_threshold") == 0)
1919 frameskip_type = FRAMESKIP_AUTO_THRESHOLD;
1920 if (strcmp(var.value, "fixed_interval") == 0)
1921 frameskip_type = FRAMESKIP_FIXED_INTERVAL;
1924 if (frameskip_type != 0)
1925 pl_rearmed_cbs.frameskip = -1;
1928 var.key = "pcsx_rearmed_frameskip_threshold";
1929 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1931 frameskip_threshold = strtol(var.value, NULL, 10);
1935 var.key = "pcsx_rearmed_frameskip_interval";
1936 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1938 frameskip_interval = strtol(var.value, NULL, 10);
1942 var.key = "pcsx_rearmed_region";
1943 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1946 if (strcmp(var.value, "auto") == 0)
1948 else if (strcmp(var.value, "NTSC") == 0)
1950 else if (strcmp(var.value, "PAL") == 0)
1957 var.key = "pcsx_rearmed_negcon_deadzone";
1958 negcon_deadzone = 0;
1959 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1961 negcon_deadzone = (int)(atoi(var.value) * 0.01f * NEGCON_RANGE);
1965 var.key = "pcsx_rearmed_negcon_response";
1966 negcon_linearity = 1;
1967 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1969 if (strcmp(var.value, "quadratic") == 0)
1971 negcon_linearity = 2;
1973 else if (strcmp(var.value, "cubic") == 0)
1975 negcon_linearity = 3;
1980 var.key = "pcsx_rearmed_analog_axis_modifier";
1981 axis_bounds_modifier = true;
1982 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1984 if (strcmp(var.value, "square") == 0)
1986 axis_bounds_modifier = true;
1988 else if (strcmp(var.value, "circle") == 0)
1990 axis_bounds_modifier = false;
1995 var.key = "pcsx_rearmed_vibration";
1997 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1999 if (strcmp(var.value, "disabled") == 0)
2000 in_enable_vibration = 0;
2001 else if (strcmp(var.value, "enabled") == 0)
2002 in_enable_vibration = 1;
2006 var.key = "pcsx_rearmed_dithering";
2008 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2010 if (strcmp(var.value, "disabled") == 0)
2012 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
2013 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 0;
2014 pl_rearmed_cbs.gpu_unai.dithering = 0;
2016 pl_rearmed_cbs.gpu_neon.allow_dithering = 0;
2019 else if (strcmp(var.value, "enabled") == 0)
2021 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
2022 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 1;
2023 pl_rearmed_cbs.gpu_unai.dithering = 1;
2025 pl_rearmed_cbs.gpu_neon.allow_dithering = 1;
2032 var.key = "pcsx_rearmed_neon_interlace_enable_v2";
2034 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2036 if (strcmp(var.value, "disabled") == 0)
2037 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
2038 else if (strcmp(var.value, "enabled") == 0)
2039 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
2041 pl_rearmed_cbs.gpu_neon.allow_interlace = 2;
2045 var.key = "pcsx_rearmed_neon_enhancement_enable";
2047 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2049 if (strcmp(var.value, "disabled") == 0)
2050 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
2051 else if (strcmp(var.value, "enabled") == 0)
2052 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
2056 var.key = "pcsx_rearmed_neon_enhancement_no_main";
2058 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2060 if (strcmp(var.value, "disabled") == 0)
2061 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
2062 else if (strcmp(var.value, "enabled") == 0)
2063 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
2068 var.key = "pcsx_rearmed_duping_enable";
2070 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2072 if (strcmp(var.value, "disabled") == 0)
2073 duping_enable = false;
2074 else if (strcmp(var.value, "enabled") == 0)
2075 duping_enable = true;
2079 var.key = "pcsx_rearmed_display_internal_fps";
2081 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2083 if (strcmp(var.value, "disabled") == 0)
2084 display_internal_fps = false;
2085 else if (strcmp(var.value, "enabled") == 0)
2086 display_internal_fps = true;
2090 // CPU emulation related config
2093 var.key = "pcsx_rearmed_drc";
2095 if (!environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
2096 var.value = "enabled";
2099 R3000Acpu *prev_cpu = psxCpu;
2103 Config.Cpu = CPU_INTERPRETER;
2106 if (strcmp(var.value, "disabled") == 0)
2107 Config.Cpu = CPU_INTERPRETER;
2108 else if (strcmp(var.value, "enabled") == 0)
2109 Config.Cpu = CPU_DYNAREC;
2111 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
2112 if (psxCpu != prev_cpu)
2114 prev_cpu->Notify(R3000ACPU_NOTIFY_BEFORE_SAVE, NULL);
2115 prev_cpu->Shutdown();
2118 psxCpu->Notify(R3000ACPU_NOTIFY_AFTER_LOAD, NULL);
2121 #endif /* !DRC_DISABLE */
2124 var.key = "pcsx_rearmed_psxclock";
2125 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2127 int psxclock = atoi(var.value);
2128 Config.cycle_multiplier = 10000 / psxclock;
2131 #if !defined(DRC_DISABLE) && !defined(LIGHTREC)
2133 var.key = "pcsx_rearmed_nosmccheck";
2134 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2136 if (strcmp(var.value, "enabled") == 0)
2137 new_dynarec_hacks |= NDHACK_NO_SMC_CHECK;
2139 new_dynarec_hacks &= ~NDHACK_NO_SMC_CHECK;
2143 var.key = "pcsx_rearmed_gteregsunneeded";
2144 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2146 if (strcmp(var.value, "enabled") == 0)
2147 new_dynarec_hacks |= NDHACK_GTE_UNNEEDED;
2149 new_dynarec_hacks &= ~NDHACK_GTE_UNNEEDED;
2153 var.key = "pcsx_rearmed_nogteflags";
2154 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2156 if (strcmp(var.value, "enabled") == 0)
2157 new_dynarec_hacks |= NDHACK_GTE_NO_FLAGS;
2159 new_dynarec_hacks &= ~NDHACK_GTE_NO_FLAGS;
2163 var.key = "pcsx_rearmed_nocompathacks";
2164 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2166 if (strcmp(var.value, "enabled") == 0)
2167 new_dynarec_hacks |= NDHACK_NO_COMPAT_HACKS;
2169 new_dynarec_hacks &= ~NDHACK_NO_COMPAT_HACKS;
2171 #endif /* !DRC_DISABLE && !LIGHTREC */
2174 var.key = "pcsx_rearmed_nostalls";
2175 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2177 if (strcmp(var.value, "enabled") == 0)
2178 Config.DisableStalls = 1;
2180 Config.DisableStalls = 0;
2184 var.key = "pcsx_rearmed_icache_emulation";
2185 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2187 if (strcmp(var.value, "disabled") == 0)
2188 Config.icache_emulation = 0;
2189 else if (strcmp(var.value, "enabled") == 0)
2190 Config.icache_emulation = 1;
2194 var.key = "pcsx_rearmed_exception_emulation";
2195 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2197 if (strcmp(var.value, "enabled") == 0)
2198 Config.PreciseExceptions = 1;
2200 Config.PreciseExceptions = 0;
2203 psxCpu->ApplyConfig();
2205 // end of CPU emu config
2209 var.key = "pcsx_rearmed_spu_reverb";
2211 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2213 if (strcmp(var.value, "disabled") == 0)
2214 spu_config.iUseReverb = false;
2215 else if (strcmp(var.value, "enabled") == 0)
2216 spu_config.iUseReverb = true;
2220 var.key = "pcsx_rearmed_spu_interpolation";
2222 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2224 if (strcmp(var.value, "simple") == 0)
2225 spu_config.iUseInterpolation = 1;
2226 else if (strcmp(var.value, "gaussian") == 0)
2227 spu_config.iUseInterpolation = 2;
2228 else if (strcmp(var.value, "cubic") == 0)
2229 spu_config.iUseInterpolation = 3;
2230 else if (strcmp(var.value, "off") == 0)
2231 spu_config.iUseInterpolation = 0;
2235 var.key = "pcsx_rearmed_spu_thread";
2236 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2238 if (strcmp(var.value, "enabled") == 0)
2239 spu_config.iUseThread = 1;
2241 spu_config.iUseThread = 0;
2244 if (P_HAVE_PTHREAD) {
2246 var.key = "pcsx_rearmed_async_cd";
2247 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2249 if (strcmp(var.value, "async") == 0)
2252 Config.CHD_Precache = 0;
2254 else if (strcmp(var.value, "sync") == 0)
2257 Config.CHD_Precache = 0;
2259 else if (strcmp(var.value, "precache") == 0)
2262 Config.CHD_Precache = 1;
2268 var.key = "pcsx_rearmed_noxadecoding";
2269 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2271 if (strcmp(var.value, "disabled") == 0)
2278 var.key = "pcsx_rearmed_nocdaudio";
2279 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2281 if (strcmp(var.value, "disabled") == 0)
2288 var.key = "pcsx_rearmed_gpu_slow_llists";
2289 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2291 if (strcmp(var.value, "disabled") == 0)
2292 Config.GpuListWalking = 0;
2293 else if (strcmp(var.value, "enabled") == 0)
2294 Config.GpuListWalking = 1;
2296 Config.GpuListWalking = -1;
2300 var.key = "pcsx_rearmed_screen_centering";
2301 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2303 if (strcmp(var.value, "game") == 0)
2304 pl_rearmed_cbs.screen_centering_type = 1;
2305 else if (strcmp(var.value, "borderless") == 0)
2306 pl_rearmed_cbs.screen_centering_type = 2;
2307 else if (strcmp(var.value, "manual") == 0)
2308 pl_rearmed_cbs.screen_centering_type = 3;
2310 pl_rearmed_cbs.screen_centering_type = 0;
2314 var.key = "pcsx_rearmed_screen_centering_x";
2315 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2317 pl_rearmed_cbs.screen_centering_x = atoi(var.value);
2321 var.key = "pcsx_rearmed_screen_centering_y";
2322 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2324 pl_rearmed_cbs.screen_centering_y = atoi(var.value);
2327 #ifdef THREAD_RENDERING
2328 var.key = "pcsx_rearmed_gpu_thread_rendering";
2331 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2333 if (strcmp(var.value, "disabled") == 0)
2334 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_OFF;
2335 else if (strcmp(var.value, "sync") == 0)
2336 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_SYNC;
2337 else if (strcmp(var.value, "async") == 0)
2338 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_ASYNC;
2344 var.key = "pcsx_rearmed_gpu_peops_odd_even_bit";
2346 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2348 if (strcmp(var.value, "enabled") == 0)
2349 gpu_peops_fix |= GPU_PEOPS_ODD_EVEN_BIT;
2353 var.key = "pcsx_rearmed_gpu_peops_expand_screen_width";
2355 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2357 if (strcmp(var.value, "enabled") == 0)
2358 gpu_peops_fix |= GPU_PEOPS_EXPAND_SCREEN_WIDTH;
2362 var.key = "pcsx_rearmed_gpu_peops_ignore_brightness";
2364 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2366 if (strcmp(var.value, "enabled") == 0)
2367 gpu_peops_fix |= GPU_PEOPS_IGNORE_BRIGHTNESS;
2371 var.key = "pcsx_rearmed_gpu_peops_disable_coord_check";
2373 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2375 if (strcmp(var.value, "enabled") == 0)
2376 gpu_peops_fix |= GPU_PEOPS_DISABLE_COORD_CHECK;
2380 var.key = "pcsx_rearmed_gpu_peops_lazy_screen_update";
2382 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2384 if (strcmp(var.value, "enabled") == 0)
2385 gpu_peops_fix |= GPU_PEOPS_LAZY_SCREEN_UPDATE;
2389 var.key = "pcsx_rearmed_gpu_peops_repeated_triangles";
2391 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2393 if (strcmp(var.value, "enabled") == 0)
2394 gpu_peops_fix |= GPU_PEOPS_REPEATED_TRIANGLES;
2398 var.key = "pcsx_rearmed_gpu_peops_quads_with_triangles";
2400 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2402 if (strcmp(var.value, "enabled") == 0)
2403 gpu_peops_fix |= GPU_PEOPS_QUADS_WITH_TRIANGLES;
2407 var.key = "pcsx_rearmed_gpu_peops_fake_busy_state";
2409 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2411 if (strcmp(var.value, "enabled") == 0)
2412 gpu_peops_fix |= GPU_PEOPS_FAKE_BUSY_STATE;
2415 if (pl_rearmed_cbs.gpu_peops.dwActFixes != gpu_peops_fix)
2416 pl_rearmed_cbs.gpu_peops.dwActFixes = gpu_peops_fix;
2420 /* Note: This used to be an option, but it only works
2421 * (correctly) when running high resolution games
2422 * (480i, 512i) and has been obsoleted by
2423 * pcsx_rearmed_gpu_unai_scale_hires */
2424 pl_rearmed_cbs.gpu_unai.ilace_force = 0;
2425 /* Note: This used to be an option, but it has no
2426 * discernable effect and has been obsoleted by
2427 * pcsx_rearmed_gpu_unai_scale_hires */
2428 pl_rearmed_cbs.gpu_unai.pixel_skip = 0;
2430 var.key = "pcsx_rearmed_gpu_unai_lighting";
2433 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2435 if (strcmp(var.value, "disabled") == 0)
2436 pl_rearmed_cbs.gpu_unai.lighting = 0;
2437 else if (strcmp(var.value, "enabled") == 0)
2438 pl_rearmed_cbs.gpu_unai.lighting = 1;
2441 var.key = "pcsx_rearmed_gpu_unai_fast_lighting";
2444 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2446 if (strcmp(var.value, "disabled") == 0)
2447 pl_rearmed_cbs.gpu_unai.fast_lighting = 0;
2448 else if (strcmp(var.value, "enabled") == 0)
2449 pl_rearmed_cbs.gpu_unai.fast_lighting = 1;
2452 var.key = "pcsx_rearmed_gpu_unai_blending";
2455 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2457 if (strcmp(var.value, "disabled") == 0)
2458 pl_rearmed_cbs.gpu_unai.blending = 0;
2459 else if (strcmp(var.value, "enabled") == 0)
2460 pl_rearmed_cbs.gpu_unai.blending = 1;
2463 var.key = "pcsx_rearmed_gpu_unai_scale_hires";
2466 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2468 if (strcmp(var.value, "disabled") == 0)
2469 pl_rearmed_cbs.gpu_unai.scale_hires = 0;
2470 else if (strcmp(var.value, "enabled") == 0)
2471 pl_rearmed_cbs.gpu_unai.scale_hires = 1;
2476 var.key = "pcsx_rearmed_crosshair1";
2478 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2480 if (strcmp(var.value, "disabled") == 0)
2481 in_enable_crosshair[0] = 0;
2482 else if (strcmp(var.value, "blue") == 0)
2483 in_enable_crosshair[0] = 0x1F;
2484 else if (strcmp(var.value, "green") == 0)
2485 in_enable_crosshair[0] = 0x7E0;
2486 else if (strcmp(var.value, "red") == 0)
2487 in_enable_crosshair[0] = 0xF800;
2488 else if (strcmp(var.value, "white") == 0)
2489 in_enable_crosshair[0] = 0xFFFF;
2493 var.key = "pcsx_rearmed_crosshair2";
2495 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2497 if (strcmp(var.value, "disabled") == 0)
2498 in_enable_crosshair[1] = 0;
2499 else if (strcmp(var.value, "blue") == 0)
2500 in_enable_crosshair[1] = 0x1F;
2501 else if (strcmp(var.value, "green") == 0)
2502 in_enable_crosshair[1] = 0x7E0;
2503 else if (strcmp(var.value, "red") == 0)
2504 in_enable_crosshair[1] = 0xF800;
2505 else if (strcmp(var.value, "white") == 0)
2506 in_enable_crosshair[1] = 0xFFFF;
2509 //This adjustment process gives the user the ability to manually align the mouse up better
2510 //with where the shots are in the emulator.
2513 var.key = "pcsx_rearmed_konamigunadjustx";
2515 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2517 KonamiGunAdjustX = atof(var.value) / 100.0f;
2521 var.key = "pcsx_rearmed_konamigunadjusty";
2523 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2525 KonamiGunAdjustY = atof(var.value) / 100.0f;
2529 var.key = "pcsx_rearmed_gunconadjustx";
2531 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2533 GunconAdjustX = atoi(var.value);
2537 var.key = "pcsx_rearmed_gunconadjusty";
2539 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2541 GunconAdjustY = atoi(var.value);
2545 var.key = "pcsx_rearmed_gunconadjustratiox";
2547 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2549 GunconAdjustRatioX = atof(var.value);
2553 var.key = "pcsx_rearmed_gunconadjustratioy";
2555 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2557 GunconAdjustRatioY = atof(var.value);
2561 var.key = "pcsx_rearmed_input_sensitivity";
2562 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2564 mouse_sensitivity = atof(var.value);
2569 // inform core things about possible config changes
2570 plugin_call_rearmed_cbs();
2572 if (GPU_open != NULL && GPU_close != NULL)
2575 GPU_open(&gpuDisp, "PCSX", NULL);
2578 /* Reinitialise frameskipping, if required */
2579 if (((frameskip_type != prev_frameskip_type)))
2580 retro_set_audio_buff_status_cb();
2582 /* dfinput_activate(); */
2588 //bootlogo display hack
2592 var.key = "pcsx_rearmed_show_bios_bootlogo";
2593 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2595 Config.SlowBoot = 0;
2597 if (strcmp(var.value, "enabled") == 0)
2599 Config.SlowBoot = 1;
2606 update_option_visibility();
2609 // Taken from beetle-psx-libretro
2610 static uint16_t get_analog_button(int16_t ret, retro_input_state_t input_state_cb, int player_index, int id)
2612 // NOTE: Analog buttons were added Nov 2017. Not all front-ends support this
2613 // feature (or pre-date it) so we need to handle this in a graceful way.
2615 // First, try and get an analog value using the new libretro API constant
2616 uint16_t button = input_state_cb(player_index,
2617 RETRO_DEVICE_ANALOG,
2618 RETRO_DEVICE_INDEX_ANALOG_BUTTON,
2620 button = MIN(button / 128, 255);
2624 // If we got exactly zero, we're either not pressing the button, or the front-end
2625 // is not reporting analog values. We need to do a second check using the classic
2626 // digital API method, to at least get some response - better than nothing.
2628 // NOTE: If we're really just not holding the button, we're still going to get zero.
2630 button = (ret & (1 << id)) ? 255 : 0;
2636 unsigned char axis_range_modifier(int16_t axis_value, bool is_square)
2638 float modifier_axis_range = 0;
2642 modifier_axis_range = round((axis_value >> 8) / 0.785) + 128;
2643 if (modifier_axis_range < 0)
2645 modifier_axis_range = 0;
2647 else if (modifier_axis_range > 255)
2649 modifier_axis_range = 255;
2654 modifier_axis_range = MIN(((axis_value >> 8) + 128), 255);
2657 return modifier_axis_range;
2660 static void update_input_guncon(int port, int ret)
2663 //Separate pointer and lightgun control types
2665 //Mouse range is -32767 -> 32767
2667 //Use the left analog stick field to store the absolute coordinates
2669 int gunx = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X);
2670 int guny = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y);
2672 //Have the Libretro API let /libpcsxcore/plugins.c know when the lightgun is pointed offscreen
2673 //Offscreen value is chosen to be well out of range of any possible scaling done via core options
2674 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))
2676 in_analog_left[port][0] = 65536;
2677 in_analog_left[port][1] = 65536;
2681 in_analog_left[port][0] = ((gunx * GunconAdjustRatioX) + (GunconAdjustX * 655)) / 64 + 512;
2682 in_analog_left[port][1] = ((guny * GunconAdjustRatioY) + (GunconAdjustY * 655)) / 64 + 512;
2685 //GUNCON has 3 controls, Trigger,A,B which equal Circle,Start,Cross
2688 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))
2689 in_keystate[port] |= (1 << DKEY_CIRCLE);
2692 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A))
2693 in_keystate[port] |= (1 << DKEY_START);
2696 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_B))
2697 in_keystate[port] |= (1 << DKEY_CROSS);
2701 static void update_input_justifier(int port, int ret)
2704 //Separate pointer and lightgun control types
2706 //RetroArch lightgun range is -32767 -> 32767 on both axes (positive Y is down)
2708 //JUSTIFIER has 3 controls, Trigger,Special,Start which equal Square,Cross,Start
2711 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))
2712 in_keystate[port] |= (1 << DKEY_SQUARE);
2715 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A))
2716 in_keystate[port] |= (1 << DKEY_CROSS);
2719 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_START))
2720 in_keystate[port] |= (1 << DKEY_START);
2724 static void update_input_negcon(int port, int ret)
2730 float negcon_twist_amplitude;
2732 // Query digital inputs
2735 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_UP))
2736 in_keystate[port] |= (1 << DKEY_UP);
2738 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT))
2739 in_keystate[port] |= (1 << DKEY_RIGHT);
2741 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN))
2742 in_keystate[port] |= (1 << DKEY_DOWN);
2744 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT))
2745 in_keystate[port] |= (1 << DKEY_LEFT);
2747 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_START))
2748 in_keystate[port] |= (1 << DKEY_START);
2750 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_A))
2751 in_keystate[port] |= (1 << DKEY_CIRCLE);
2753 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_X))
2754 in_keystate[port] |= (1 << DKEY_TRIANGLE);
2755 // > neGcon R shoulder (digital)
2756 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_R))
2757 in_keystate[port] |= (1 << DKEY_R1);
2758 // Query analog inputs
2760 // From studying 'libpcsxcore/plugins.c' and 'frontend/plugin.c':
2761 // >> pad->leftJoyX == in_analog_left[port][0] == NeGcon II
2762 // >> pad->leftJoyY == in_analog_left[port][1] == NeGcon L
2763 // >> pad->rightJoyX == in_analog_right[port][0] == NeGcon twist
2764 // >> pad->rightJoyY == in_analog_right[port][1] == NeGcon I
2765 // So we just have to map in_analog_left/right to more
2766 // appropriate inputs...
2769 // >> Get raw analog stick value and account for deadzone
2770 lsx = input_state_cb(port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
2771 if (lsx > negcon_deadzone)
2772 lsx = lsx - negcon_deadzone;
2773 else if (lsx < -negcon_deadzone)
2774 lsx = lsx + negcon_deadzone;
2777 // >> Convert to an 'amplitude' [-1.0,1.0] and adjust response
2778 negcon_twist_amplitude = (float)lsx / (float)(NEGCON_RANGE - negcon_deadzone);
2779 if (negcon_linearity == 2)
2781 if (negcon_twist_amplitude < 0.0)
2782 negcon_twist_amplitude = -(negcon_twist_amplitude * negcon_twist_amplitude);
2784 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude;
2786 else if (negcon_linearity == 3)
2787 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude * negcon_twist_amplitude;
2788 // >> Convert to final 'in_analog' integer value [0,255]
2789 in_analog_right[port][0] = MAX(MIN((int)(negcon_twist_amplitude * 128.0f) + 128, 255), 0);
2791 // >> Handle right analog stick vertical axis mapping...
2792 // - Up (-Y) == accelerate == neGcon I
2793 // - Down (+Y) == brake == neGcon II
2796 rsy = input_state_cb(port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
2799 // Account for deadzone
2800 // (Note: have never encountered a gamepad with significant differences
2801 // in deadzone between left/right analog sticks, so use the regular 'twist'
2803 if (rsy > negcon_deadzone)
2804 rsy = rsy - negcon_deadzone;
2807 // Convert to 'in_analog' integer value [0,255]
2808 negcon_ii_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2812 if (rsy < -negcon_deadzone)
2813 rsy = -1 * (rsy + negcon_deadzone);
2816 negcon_i_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2819 in_analog_right[port][1] = MAX(
2821 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_R2),
2822 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_B)),
2825 in_analog_left[port][0] = MAX(
2827 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_L2),
2828 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_Y)),
2831 in_analog_left[port][1] = get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_L);
2834 static void update_input_mouse(int port, int ret)
2836 float raw_x = input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X);
2837 float raw_y = input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y);
2839 int x = (int)roundf(raw_x * mouse_sensitivity);
2840 int y = (int)roundf(raw_y * mouse_sensitivity);
2842 if (x > 127) x = 127;
2843 else if (x < -128) x = -128;
2845 if (y > 127) y = 127;
2846 else if (y < -128) y = -128;
2848 in_mouse[port][0] = x; /* -128..+128 left/right movement, 0 = no movement */
2849 in_mouse[port][1] = y; /* -128..+128 down/up movement, 0 = no movement */
2851 /* left mouse button state */
2852 if (input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_LEFT))
2853 in_keystate[port] |= 1 << 11;
2855 /* right mouse button state */
2856 if (input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_RIGHT))
2857 in_keystate[port] |= 1 << 10;
2860 static void update_input(void)
2862 // reset all keystate, query libretro for keystate
2866 for (i = 0; i < PORTS_NUMBER; i++)
2869 int type = in_type[i];
2873 if (type == PSE_PAD_TYPE_NONE)
2876 if (libretro_supports_bitmasks)
2877 ret = input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
2880 for (j = 0; j < (RETRO_DEVICE_ID_JOYPAD_R3 + 1); j++)
2882 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j))
2889 case PSE_PAD_TYPE_GUNCON:
2890 update_input_guncon(i, ret);
2892 case PSE_PAD_TYPE_GUN:
2893 update_input_justifier(i, ret);
2895 case PSE_PAD_TYPE_NEGCON:
2896 update_input_negcon(i, ret);
2898 case PSE_PAD_TYPE_MOUSE:
2899 update_input_mouse(i, ret);
2902 // Query digital inputs
2903 for (j = 0; j < RETRO_PSX_MAP_LEN; j++)
2905 in_keystate[i] |= retro_psx_map[j];
2907 // Query analog inputs
2908 if (type == PSE_PAD_TYPE_ANALOGJOY || type == PSE_PAD_TYPE_ANALOGPAD)
2910 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);
2911 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);
2912 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);
2913 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);
2919 static void print_internal_fps(void)
2921 if (display_internal_fps)
2925 if (frame_count % INTERNAL_FPS_SAMPLE_PERIOD == 0)
2927 unsigned internal_fps = pl_rearmed_cbs.flip_cnt * (is_pal_mode ? 50 : 60) / INTERNAL_FPS_SAMPLE_PERIOD;
2929 const char *strc = (const char *)str;
2933 snprintf(str, sizeof(str), "Internal FPS: %2d", internal_fps);
2935 pl_rearmed_cbs.flip_cnt = 0;
2937 if (msg_interface_version >= 1)
2939 struct retro_message_ext msg = {
2944 RETRO_MESSAGE_TARGET_OSD,
2945 RETRO_MESSAGE_TYPE_STATUS,
2948 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE_EXT, &msg);
2952 struct retro_message msg = {
2956 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
2964 void retro_run(void)
2966 //SysReset must be run while core is running,Not in menu (Locks up Retroarch)
2975 print_internal_fps();
2977 /* Check whether current frame should
2979 pl_rearmed_cbs.fskip_force = 0;
2980 pl_rearmed_cbs.fskip_dirty = 0;
2982 if (frameskip_type != FRAMESKIP_NONE)
2984 bool skip_frame = false;
2986 switch (frameskip_type)
2988 case FRAMESKIP_AUTO:
2989 skip_frame = retro_audio_buff_active && retro_audio_buff_underrun;
2991 case FRAMESKIP_AUTO_THRESHOLD:
2992 skip_frame = retro_audio_buff_active && (retro_audio_buff_occupancy < frameskip_threshold);
2994 case FRAMESKIP_FIXED_INTERVAL:
3001 if (skip_frame && frameskip_counter < frameskip_interval)
3002 pl_rearmed_cbs.fskip_force = 1;
3005 /* If frameskip/timing settings have changed,
3006 * update frontend audio latency
3007 * > Can do this before or after the frameskip
3008 * check, but doing it after means we at least
3009 * retain the current frame's audio output */
3010 if (update_audio_latency)
3012 environ_cb(RETRO_ENVIRONMENT_SET_MINIMUM_AUDIO_LATENCY,
3013 &retro_audio_latency);
3014 update_audio_latency = false;
3021 bool updated = false;
3022 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
3023 update_variables(true);
3028 if (pl_rearmed_cbs.fskip_dirty == 1) {
3029 if (frameskip_counter < frameskip_interval)
3030 frameskip_counter++;
3031 else if (frameskip_counter >= frameskip_interval || !pl_rearmed_cbs.fskip_force)
3032 frameskip_counter = 0;
3035 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf_ptr : NULL,
3036 vout_width, vout_height, vout_width * 2);
3042 static bool try_use_bios(const char *path, bool preferred_only)
3046 FILE *fp = fopen(path, "rb");
3050 fseek(fp, 0, SEEK_END);
3054 name = strrchr(path, SLASH);
3058 if (preferred_only && size != 512 * 1024)
3060 if (size != 512 * 1024 && size != 4 * 1024 * 1024)
3062 if (strstr(name, "unirom"))
3064 // jp bios have an addidional region check
3065 if (preferred_only && (strcasestr(name, "00.") || strcasestr(name, "j.bin")))
3068 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
3073 #include <sys/types.h>
3076 static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
3078 static const char *substr_pref[] = { "scph", "ps" };
3079 static const char *substr_alt[] = { "scph", "ps", "openbios" };
3085 dir = opendir(dirpath);
3089 // try to find a "better" bios
3090 while ((ent = readdir(dir)))
3092 for (i = 0; i < sizeof(substr_pref) / sizeof(substr_pref[0]); i++)
3094 const char *substr = substr_pref[i];
3095 if ((strncasecmp(ent->d_name, substr, strlen(substr)) != 0))
3097 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
3098 ret = try_use_bios(path, true);
3104 // another pass to look for anything fitting, even ps2 bios
3106 while ((ent = readdir(dir)))
3108 for (i = 0; i < sizeof(substr_alt) / sizeof(substr_alt[0]); i++)
3110 const char *substr = substr_alt[i];
3111 if ((strncasecmp(ent->d_name, substr, strlen(substr)) != 0))
3113 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
3114 ret = try_use_bios(path, false);
3126 #define find_any_bios(...) false
3129 static void check_system_specs(void)
3132 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
3135 static int init_memcards(void)
3139 struct retro_variable var = { .key = "pcsx_rearmed_memcard2", .value = NULL };
3140 static const char CARD2_FILE[] = "pcsx-card2.mcd";
3142 // Memcard2 will be handled and is re-enabled if needed using core
3144 // Memcard1 is handled by libretro, doing this will set core to
3145 // skip file io operations for memcard1 like SaveMcd
3146 snprintf(Config.Mcd1, sizeof(Config.Mcd1), "none");
3147 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "none");
3148 init_memcard(Mcd1Data);
3149 // Memcard 2 is managed by the emulator on the filesystem,
3150 // There is no need to initialize Mcd2Data like Mcd1Data.
3152 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
3154 SysPrintf("Memcard 2: %s\n", var.value);
3155 if (memcmp(var.value, "enabled", 7) == 0)
3157 if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir)
3159 if (strlen(dir) + strlen(CARD2_FILE) + 2 > sizeof(Config.Mcd2))
3161 SysPrintf("Path '%s' is too long. Cannot use memcard 2. Use a shorter path.\n", dir);
3167 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "%s/%s", dir, CARD2_FILE);
3168 SysPrintf("Use memcard 2: %s\n", Config.Mcd2);
3173 SysPrintf("Could not get save directory! Could not create memcard 2.");
3181 static void loadPSXBios(void)
3184 char path[PATH_MAX];
3185 unsigned useHLE = 0;
3187 const char *bios[] = {
3188 "PSXONPSP660", "psxonpsp660",
3189 "SCPH101", "scph101",
3190 "SCPH5501", "scph5501",
3191 "SCPH7001", "scph7001",
3192 "SCPH1001", "scph1001"
3195 struct retro_variable var = {
3196 .key = "pcsx_rearmed_bios",
3202 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
3204 if (!strcmp(var.value, "HLE"))
3210 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
3213 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s", dir);
3215 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++)
3217 snprintf(path, sizeof(path), "%s%c%s.bin", dir, SLASH, bios[i]);
3218 found_bios = try_use_bios(path, true);
3224 found_bios = find_any_bios(dir, path, sizeof(path));
3228 SysPrintf("found BIOS file: %s\n", Config.Bios);
3234 const char *msg_str;
3238 msg_str = "BIOS set to \'hle\'";
3239 SysPrintf("Using HLE BIOS.\n");
3240 // shorter as the user probably intentionally wants to use HLE
3245 msg_str = "No PlayStation BIOS file found - add for better compatibility";
3246 SysPrintf("No BIOS files found.\n");
3249 show_notification(msg_str, duration, 2);
3253 void retro_init(void)
3255 unsigned dci_version = 0;
3256 struct retro_rumble_interface rumble;
3259 msg_interface_version = 0;
3260 environ_cb(RETRO_ENVIRONMENT_GET_MESSAGE_INTERFACE_VERSION, &msg_interface_version);
3262 #if defined(__MACH__) && !defined(TVOS)
3263 // magic sauce to make the dynarec work on iOS
3264 syscall(SYS_ptrace, 0 /*PTRACE_TRACEME*/, 0, 0, 0);
3268 psxMapHook = pl_3ds_mmap;
3269 psxUnmapHook = pl_3ds_munmap;
3272 if (init_vita_mmap() < 0)
3274 psxMapHook = pl_vita_mmap;
3275 psxUnmapHook = pl_vita_munmap;
3277 ret = emu_core_preinit();
3279 /* emu_core_preinit sets the cpu to dynarec */
3281 Config.Cpu = CPU_INTERPRETER;
3283 ret |= init_memcards();
3285 ret |= emu_core_init();
3288 SysPrintf("PCSX init failed.\n");
3293 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
3294 #elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && P_HAVE_POSIX_MEMALIGN
3295 if (posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2) != 0)
3296 vout_buf = (void *) 0;
3298 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
3301 vout_buf_ptr = vout_buf;
3305 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
3307 disk_initial_index = 0;
3308 disk_initial_path[0] = '\0';
3309 if (environ_cb(RETRO_ENVIRONMENT_GET_DISK_CONTROL_INTERFACE_VERSION, &dci_version) && (dci_version >= 1))
3310 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_EXT_INTERFACE, &disk_control_ext);
3312 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
3315 if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble))
3316 rumble_cb = rumble.set_rumble_state;
3318 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
3319 * we have to do this because cache misses and some IO penalties
3320 * are not emulated. Warning: changing this may break compatibility. */
3321 Config.cycle_multiplier = CYCLE_MULT_DEFAULT;
3322 #if defined(HAVE_PRE_ARMV7) && !defined(_3DS)
3323 Config.cycle_multiplier = 200;
3325 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
3326 pl_rearmed_cbs.gpu_peops.dwActFixes = GPU_PEOPS_OLD_FRAME_SKIP;
3328 SaveFuncs.open = save_open;
3329 SaveFuncs.read = save_read;
3330 SaveFuncs.write = save_write;
3331 SaveFuncs.seek = save_seek;
3332 SaveFuncs.close = save_close;
3334 if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL))
3335 libretro_supports_bitmasks = true;
3337 check_system_specs();
3340 void retro_deinit(void)
3349 linearFree(vout_buf);
3358 libretro_supports_bitmasks = false;
3359 libretro_supports_option_categories = false;
3361 show_input_settings = true;
3363 show_advanced_gpu_peops_settings = true;
3366 show_advanced_gpu_unai_settings = true;
3369 /* Have to reset disks struct, otherwise
3370 * fnames/flabels will leak memory */
3372 frameskip_type = FRAMESKIP_NONE;
3373 frameskip_threshold = 0;
3374 frameskip_interval = 0;
3375 frameskip_counter = 0;
3376 retro_audio_buff_active = false;
3377 retro_audio_buff_occupancy = 0;
3378 retro_audio_buff_underrun = false;
3379 retro_audio_latency = 0;
3380 update_audio_latency = false;
3384 #include <psp2/kernel/threadmgr.h>
3385 int usleep(unsigned long us)
3387 sceKernelDelayThread(us);
3391 void SysPrintf(const char *fmt, ...)
3396 va_start(list, fmt);
3397 vsprintf(msg, fmt, list);
3401 log_cb(RETRO_LOG_INFO, "%s", msg);
3404 /* Prints debug-level logs */
3405 void SysDLog(const char *fmt, ...)
3410 va_start(list, fmt);
3411 vsprintf(msg, fmt, list);
3415 log_cb(RETRO_LOG_DEBUG, "%s", msg);
3418 // vim:sw=3:ts=3:expandtab