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 = 256, vout_height = 240, vout_pitch = 256;
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 vout_pitch = vout_width;
238 if (environ_cb(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &fb) && fb.format == RETRO_PIXEL_FORMAT_RGB565) {
239 vout_buf_ptr = fb.data;
240 if (fb.pitch / 2 != vout_pitch && fb.pitch != vout_width * 2)
241 SysPrintf("got unusual pitch %zd for resolution %dx%d\n", fb.pitch, vout_width, vout_height);
242 vout_pitch = fb.pitch / 2;
245 vout_buf_ptr = vout_buf;
248 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
255 if (previous_width != vout_width || previous_height != vout_height)
257 previous_width = vout_width;
258 previous_height = vout_height;
260 struct retro_system_av_info info;
261 retro_get_system_av_info(&info);
262 environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &info.geometry);
268 #ifndef FRONTEND_SUPPORTS_RGB565
269 static void convert(void *buf, size_t bytes)
271 unsigned int i, v, *p = buf;
273 for (i = 0; i < bytes / 4; i++)
276 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
281 // Function to add crosshairs
282 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) {
283 for (port = 0; port < 2; port++) {
284 // Draw the horizontal line of the crosshair
285 for (int i = pos_y - thickness / 2; i <= pos_y + thickness / 2; i++) {
286 for (int j = pos_x - size_x / 2; j <= pos_x + size_x / 2; j++) {
287 if ((i + vout_height) >= 0 && (i + vout_height) < bufferStride && j >= 0 && j < bufferStride && in_enable_crosshair[port] > 0)
288 buffer[i * bufferStride + j] = crosshair_color;
292 // Draw the vertical line of the crosshair
293 for (int i = pos_x - thickness / 2; i <= pos_x + thickness / 2; i++) {
294 for (int j = pos_y - size_y / 2; j <= pos_y + size_y / 2; j++) {
295 if (i >= 0 && i < bufferStride && (j + vout_height) >= 0 && (j + vout_height) < bufferStride && in_enable_crosshair[port] > 0)
296 buffer[j * bufferStride + i] = crosshair_color;
302 struct CrosshairInfo {
303 int pos_x, pos_y, thickness, size_x, size_y;
306 // Calculate size and position of crosshairs
307 static void CrosshairDimensions(int port, struct CrosshairInfo *info) {
308 int gunx = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X);
309 int guny = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y);
310 if (gunx == 32767) // Prevent crosshairs from wrapping around right side of screen to left
311 info->pos_x = (gunx + 32767.0f) * vout_width / 65534.0f - 0.5f;
313 info->pos_x = (gunx + 32767.0f) * vout_width / 65534.0f;
314 info->pos_y = (guny + 32767.0f) * vout_height / 65534.0f - vout_height;
315 info->thickness = pl_rearmed_cbs.gpu_neon.enhancement_enable ? 4 : 2;
316 info->size_x = psx_w * (pl_rearmed_cbs.gpu_neon.enhancement_enable ? 2 : 1) / 40.0f;
317 info->size_y = psx_h * (pl_rearmed_cbs.gpu_neon.enhancement_enable ? 2 : 1) * (4.0f / 3.0f) / 40.0f;
320 static void vout_flip(const void *vram, int stride, int bgr24,
321 int x, int y, int w, int h, int dims_changed)
323 unsigned short *dest = vout_buf_ptr;
324 const unsigned short *src = vram;
325 int dstride = vout_pitch, h1 = h;
328 if (vram == NULL || dims_changed || (in_enable_crosshair[0] + in_enable_crosshair[1]) > 0)
330 memset(vout_buf_ptr, 0, dstride * vout_height * 2);
336 dest += x + y * dstride;
340 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
341 for (; h1-- > 0; dest += dstride, src += stride)
343 bgr888_to_rgb565(dest, src, w * 3);
348 for (; h1-- > 0; dest += dstride, src += stride)
350 bgr555_to_rgb565(dest, src, w * 2);
354 for (port = 0; port < 2; port++) {
355 if (in_enable_crosshair[port] > 0 && (in_type[port] == PSE_PAD_TYPE_GUNCON || in_type[port] == PSE_PAD_TYPE_GUN))
357 struct CrosshairInfo crosshairInfo;
358 CrosshairDimensions(port, &crosshairInfo);
359 addCrosshair(port, in_enable_crosshair[port], dest, dstride, crosshairInfo.pos_x, crosshairInfo.pos_y, crosshairInfo.thickness, crosshairInfo.size_x, crosshairInfo.size_y);
364 #ifndef FRONTEND_SUPPORTS_RGB565
365 convert(vout_buf_ptr, vout_pitch * vout_height * 2);
368 pl_rearmed_cbs.flip_cnt++;
380 psx_map_t custom_psx_maps[] = {
381 { NULL, 0x13000000, 0x210000, MAP_TAG_RAM }, // 0x80000000
382 { NULL, 0x12800000, 0x010000, MAP_TAG_OTHER }, // 0x1f800000
383 { NULL, 0x12c00000, 0x080000, MAP_TAG_OTHER }, // 0x1fc00000
384 { NULL, 0x11000000, 0x800000, MAP_TAG_LUTS }, // 0x08000000
385 { NULL, 0x12000000, 0x201000, MAP_TAG_VRAM }, // 0x00000000
388 void *pl_3ds_mmap(unsigned long addr, size_t size, int is_fixed,
396 psx_map_t *custom_map = custom_psx_maps;
398 for (; custom_map->size; custom_map++)
400 if ((custom_map->size == size) && (custom_map->tag == tag))
402 uint32_t ptr_aligned, tmp;
405 custom_map->buffer = malloc(size + 0x1000);
406 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
408 if (svcControlMemory(&tmp, (void *)custom_map->target_map, (void *)ptr_aligned, size, MEMOP_MAP, 0x3) < 0)
410 SysPrintf("could not map memory @0x%08X\n", custom_map->target_map);
414 ret = (void *)custom_map->target_map;
415 memset(ret, 0, size);
421 return calloc(size, 1);
424 void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
430 psx_map_t *custom_map = custom_psx_maps;
432 for (; custom_map->size; custom_map++)
434 if ((custom_map->target_map == (uint32_t)ptr))
436 uint32_t ptr_aligned, tmp;
438 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
440 svcControlMemory(&tmp, (void *)custom_map->target_map, (void *)ptr_aligned, size, MEMOP_UNMAP, 0x3);
442 free(custom_map->buffer);
443 custom_map->buffer = NULL;
462 static void *addr = NULL;
464 psx_map_t custom_psx_maps[] = {
465 { NULL, 0x800000, MAP_TAG_LUTS },
466 { NULL, 0x080000, MAP_TAG_OTHER },
467 { NULL, 0x010000, MAP_TAG_OTHER },
468 { NULL, 0x201000, MAP_TAG_VRAM },
469 { NULL, 0x802000, MAP_TAG_VRAM }, // enhanced renderer
470 { NULL, 0x210000, MAP_TAG_RAM },
477 addr = malloc(64 * 1024 * 1024);
480 tmpaddr = (void *)(((size_t)addr + 0xFFFFFF) & ~0xFFFFFF);
481 custom_psx_maps[0].buffer = tmpaddr + 0x0000000;
482 custom_psx_maps[1].buffer = tmpaddr + 0x0800000;
483 custom_psx_maps[2].buffer = tmpaddr + 0x0880000;
484 custom_psx_maps[3].buffer = tmpaddr + 0x0900000;
485 custom_psx_maps[4].buffer = tmpaddr + 0x1000000;
486 custom_psx_maps[5].buffer = tmpaddr + 0x2000000;
487 memset(tmpaddr, 0, 0x2210000);
489 for(n = 0; n < 5; n++){
490 sceClibPrintf("addr reserved %x\n",custom_psx_maps[n].buffer);
496 void deinit_vita_mmap()
499 for (i = 0; i < sizeof(custom_psx_maps) / sizeof(custom_psx_maps[0]); i++) {
500 custom_psx_maps[i].buffer = NULL;
501 custom_psx_maps[i].used = 0;
506 void *pl_vita_mmap(unsigned long addr, size_t size, int is_fixed,
512 psx_map_t *custom_map = custom_psx_maps;
514 for (; custom_map->size; custom_map++)
516 if (custom_map->size == size && custom_map->tag == tag && !custom_map->used)
518 custom_map->used = 1;
519 return custom_map->buffer;
523 return calloc(size, 1);
526 void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag)
530 psx_map_t *custom_map = custom_psx_maps;
532 for (; custom_map->size; custom_map++)
534 if ((custom_map->buffer == ptr))
536 custom_map->used = 0;
545 static void *pl_mmap(unsigned int size)
547 return psxMap(0, size, 0, MAP_TAG_VRAM);
550 static void pl_munmap(void *ptr, unsigned int size)
552 psxUnmap(ptr, size, MAP_TAG_VRAM);
555 struct rearmed_cbs pl_rearmed_cbs = {
556 .pl_vout_open = vout_open,
557 .pl_vout_set_mode = vout_set_mode,
558 .pl_vout_flip = vout_flip,
559 .pl_vout_close = vout_close,
562 .gpu_state_change = gpu_state_change,
563 /* from psxcounters */
564 .gpu_hcnt = &hSyncCount,
565 .gpu_frame_count = &frame_counter,
568 void pl_frame_limit(void)
570 /* called once per frame, make psxCpu->Execute() above return */
574 void pl_timing_prepare(int is_pal)
576 is_pal_mode = is_pal;
579 void plat_trigger_vibrate(int pad, int low, int high)
584 if (in_enable_vibration)
586 rumble_cb(pad, RETRO_RUMBLE_STRONG, high << 8);
587 rumble_cb(pad, RETRO_RUMBLE_WEAK, low ? 0xffff : 0x0);
591 //Percentage distance of screen to adjust for Konami Gun
592 static float KonamiGunAdjustX = 0;
593 static float KonamiGunAdjustY = 0;
595 void pl_gun_byte2(int port, unsigned char byte)
598 float justifier_multiplier = 0;
599 int justifier_width = psx_w;
600 int justifier_height = psx_h;
601 int justifier_offscreen = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN);
602 int justifier_reload = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_RELOAD);
604 if (justifier_width == 256)
605 justifier_multiplier = is_pal_mode ? .157086f : .158532f;
606 else if (justifier_width == 320)
607 justifier_multiplier = is_pal_mode ? .196358f : .198166f;
608 else if (justifier_width == 384)
609 justifier_multiplier = is_pal_mode ? .224409f : .226475f;
610 else if (justifier_width == 512)
611 justifier_multiplier = is_pal_mode ? .314173f : .317065f;
612 else // (justifier_width == 640)
613 justifier_multiplier = is_pal_mode ? .392717f : .396332f;
615 int gunx = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X);
616 int guny = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y);
618 //Default offset of +105 for X and -12 for Y is chosen to obtain alignment in Die Hard Trilogy, which has no calibration feature
619 int gunx_scaled = ((gunx + 32767.0f) / 65534.0f + KonamiGunAdjustX) * justifier_width / justifier_multiplier + 105.0f;
620 int guny_scaled = ((guny + 32767.0f) / 65534.0f + KonamiGunAdjustY) * justifier_height - 12.0f;
622 if ((byte & 0x10) && !justifier_offscreen && !justifier_reload)
624 psxScheduleIrq10(irq_count, gunx_scaled, guny_scaled);
629 static void snd_feed(void *buf, int bytes)
631 if (audio_batch_cb != NULL)
632 audio_batch_cb(buf, bytes / 4);
635 void out_register_libretro(struct out_driver *drv)
637 drv->name = "libretro";
638 drv->init = snd_init;
639 drv->finish = snd_finish;
640 drv->busy = snd_busy;
641 drv->feed = snd_feed;
644 #define RETRO_DEVICE_PSE_STANDARD RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0)
645 #define RETRO_DEVICE_PSE_ANALOG RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 0)
646 #define RETRO_DEVICE_PSE_DUALSHOCK RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 1)
647 #define RETRO_DEVICE_PSE_NEGCON RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 2)
648 #define RETRO_DEVICE_PSE_GUNCON RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_LIGHTGUN, 0)
649 #define RETRO_DEVICE_PSE_JUSTIFIER RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_LIGHTGUN, 1)
650 #define RETRO_DEVICE_PSE_MOUSE RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_MOUSE, 0)
652 static char *get_pse_pad_label[] = {
653 "none", "mouse", "negcon", "konami gun", "standard", "analog", "guncon", "dualshock"
656 static const struct retro_controller_description pads[8] =
658 { "standard", RETRO_DEVICE_JOYPAD },
659 { "analog", RETRO_DEVICE_PSE_ANALOG },
660 { "dualshock", RETRO_DEVICE_PSE_DUALSHOCK },
661 { "negcon", RETRO_DEVICE_PSE_NEGCON },
662 { "guncon", RETRO_DEVICE_PSE_GUNCON },
663 { "konami gun", RETRO_DEVICE_PSE_JUSTIFIER },
664 { "mouse", RETRO_DEVICE_PSE_MOUSE },
668 static const struct retro_controller_info ports[9] =
683 static bool update_option_visibility(void)
685 struct retro_variable var = {0};
686 struct retro_core_option_display option_display = {0};
687 bool updated = false;
690 /* If frontend supports core option categories
691 * then show/hide core option entries are ignored
692 * and no options should be hidden */
693 if (libretro_supports_option_categories)
696 var.key = "pcsx_rearmed_show_input_settings";
699 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
701 bool show_input_settings_prev =
704 show_input_settings = true;
705 if (strcmp(var.value, "disabled") == 0)
706 show_input_settings = false;
708 if (show_input_settings !=
709 show_input_settings_prev)
711 char input_option[][50] = {
712 "pcsx_rearmed_analog_axis_modifier",
713 "pcsx_rearmed_vibration",
714 "pcsx_rearmed_multitap",
715 "pcsx_rearmed_negcon_deadzone",
716 "pcsx_rearmed_negcon_response",
717 "pcsx_rearmed_input_sensitivity",
718 "pcsx_rearmed_crosshair1",
719 "pcsx_rearmed_crosshair2",
720 "pcsx_rearmed_konamigunadjustx",
721 "pcsx_rearmed_konamigunadjusty",
722 "pcsx_rearmed_gunconadjustx",
723 "pcsx_rearmed_gunconadjusty",
724 "pcsx_rearmed_gunconadjustratiox",
725 "pcsx_rearmed_gunconadjustratioy"
728 option_display.visible = show_input_settings;
731 i < (sizeof(input_option) /
732 sizeof(input_option[0]));
735 option_display.key = input_option[i];
736 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
744 var.key = "pcsx_rearmed_show_gpu_peops_settings";
747 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
749 bool show_advanced_gpu_peops_settings_prev =
750 show_advanced_gpu_peops_settings;
752 show_advanced_gpu_peops_settings = true;
753 if (strcmp(var.value, "disabled") == 0)
754 show_advanced_gpu_peops_settings = false;
756 if (show_advanced_gpu_peops_settings !=
757 show_advanced_gpu_peops_settings_prev)
760 struct retro_core_option_display option_display;
761 char gpu_peops_option[][45] = {
762 "pcsx_rearmed_gpu_peops_odd_even_bit",
763 "pcsx_rearmed_gpu_peops_expand_screen_width",
764 "pcsx_rearmed_gpu_peops_ignore_brightness",
765 "pcsx_rearmed_gpu_peops_disable_coord_check",
766 "pcsx_rearmed_gpu_peops_lazy_screen_update",
767 "pcsx_rearmed_gpu_peops_repeated_triangles",
768 "pcsx_rearmed_gpu_peops_quads_with_triangles",
769 "pcsx_rearmed_gpu_peops_fake_busy_state"
772 option_display.visible = show_advanced_gpu_peops_settings;
775 i < (sizeof(gpu_peops_option) /
776 sizeof(gpu_peops_option[0]));
779 option_display.key = gpu_peops_option[i];
780 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
789 var.key = "pcsx_rearmed_show_gpu_unai_settings";
792 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
794 bool show_advanced_gpu_unai_settings_prev =
795 show_advanced_gpu_unai_settings;
797 show_advanced_gpu_unai_settings = true;
798 if (strcmp(var.value, "disabled") == 0)
799 show_advanced_gpu_unai_settings = false;
801 if (show_advanced_gpu_unai_settings !=
802 show_advanced_gpu_unai_settings_prev)
805 struct retro_core_option_display option_display;
806 char gpu_unai_option[][40] = {
807 "pcsx_rearmed_gpu_unai_blending",
808 "pcsx_rearmed_gpu_unai_lighting",
809 "pcsx_rearmed_gpu_unai_fast_lighting",
810 "pcsx_rearmed_gpu_unai_scale_hires",
813 option_display.visible = show_advanced_gpu_unai_settings;
816 i < (sizeof(gpu_unai_option) /
817 sizeof(gpu_unai_option[0]));
820 option_display.key = gpu_unai_option[i];
821 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
832 void retro_set_environment(retro_environment_t cb)
834 bool option_categories = false;
835 #ifdef USE_LIBRETRO_VFS
836 struct retro_vfs_interface_info vfs_iface_info;
841 if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
842 log_cb = logging.log;
844 environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
847 * An annoyance: retro_set_environment() can be called
848 * multiple times, and depending upon the current frontend
849 * state various environment callbacks may be disabled.
850 * This means the reported 'categories_supported' status
851 * may change on subsequent iterations. We therefore have
852 * to record whether 'categories_supported' is true on any
853 * iteration, and latch the result */
854 libretro_set_core_options(environ_cb, &option_categories);
855 libretro_supports_option_categories |= option_categories;
857 /* If frontend supports core option categories,
858 * any show/hide core option entries are unused
859 * and should be hidden */
860 if (libretro_supports_option_categories)
862 struct retro_core_option_display option_display;
863 option_display.visible = false;
865 option_display.key = "pcsx_rearmed_show_input_settings";
866 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
870 option_display.key = "pcsx_rearmed_show_gpu_peops_settings";
871 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
875 option_display.key = "pcsx_rearmed_show_gpu_unai_settings";
876 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
880 /* If frontend does not support core option
881 * categories, core options may be shown/hidden
882 * at runtime. In this case, register 'update
883 * display' callback, so frontend can update
884 * core options menu without calling retro_run() */
887 struct retro_core_options_update_display_callback update_display_cb;
888 update_display_cb.callback = update_option_visibility;
890 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_UPDATE_DISPLAY_CALLBACK,
894 #ifdef USE_LIBRETRO_VFS
895 vfs_iface_info.required_interface_version = 1;
896 vfs_iface_info.iface = NULL;
897 if (environ_cb(RETRO_ENVIRONMENT_GET_VFS_INTERFACE, &vfs_iface_info))
898 filestream_vfs_init(&vfs_iface_info);
902 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
903 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
904 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
905 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
906 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
908 unsigned retro_api_version(void)
910 return RETRO_API_VERSION;
913 static void update_multitap(void)
915 struct retro_variable var = { 0 };
921 var.key = "pcsx_rearmed_multitap";
922 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value))
924 if (strcmp(var.value, "port 1") == 0)
926 else if (strcmp(var.value, "port 2") == 0)
928 else if (strcmp(var.value, "ports 1 and 2") == 0)
936 void retro_set_controller_port_device(unsigned port, unsigned device)
938 if (port >= PORTS_NUMBER)
943 case RETRO_DEVICE_JOYPAD:
944 case RETRO_DEVICE_PSE_STANDARD:
945 in_type[port] = PSE_PAD_TYPE_STANDARD;
947 case RETRO_DEVICE_PSE_ANALOG:
948 in_type[port] = PSE_PAD_TYPE_ANALOGJOY;
950 case RETRO_DEVICE_PSE_DUALSHOCK:
951 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
953 case RETRO_DEVICE_PSE_MOUSE:
954 in_type[port] = PSE_PAD_TYPE_MOUSE;
956 case RETRO_DEVICE_PSE_NEGCON:
957 in_type[port] = PSE_PAD_TYPE_NEGCON;
959 case RETRO_DEVICE_PSE_GUNCON:
960 in_type[port] = PSE_PAD_TYPE_GUNCON;
962 case RETRO_DEVICE_PSE_JUSTIFIER:
963 in_type[port] = PSE_PAD_TYPE_GUN;
965 case RETRO_DEVICE_NONE:
967 in_type[port] = PSE_PAD_TYPE_NONE;
971 SysPrintf("port: %u device: %s\n", port + 1, get_pse_pad_label[in_type[port]]);
974 void retro_get_system_info(struct retro_system_info *info)
977 #define GIT_VERSION ""
979 memset(info, 0, sizeof(*info));
980 info->library_name = "PCSX-ReARMed";
981 info->library_version = "r23l" GIT_VERSION;
982 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u|chd|iso|exe";
983 info->need_fullpath = true;
986 void retro_get_system_av_info(struct retro_system_av_info *info)
988 unsigned geom_height = vout_height;
989 unsigned geom_width = vout_width;
991 memset(info, 0, sizeof(*info));
992 info->timing.fps = is_pal_mode ? 50.0 : 60.0;
993 info->timing.sample_rate = 44100.0;
994 info->geometry.base_width = geom_width;
995 info->geometry.base_height = geom_height;
996 info->geometry.max_width = VOUT_MAX_WIDTH;
997 info->geometry.max_height = VOUT_MAX_HEIGHT;
998 info->geometry.aspect_ratio = 4.0 / 3.0;
1002 size_t retro_serialize_size(void)
1004 // it's currently 4380651-4397047 bytes,
1005 // but have some reserved for future
1016 static void *save_open(const char *name, const char *mode)
1020 if (name == NULL || mode == NULL)
1023 fp = malloc(sizeof(*fp));
1027 fp->buf = (char *)name;
1029 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
1034 static int save_read(void *file, void *buf, u32 len)
1036 struct save_fp *fp = file;
1037 if (fp == NULL || buf == NULL)
1040 memcpy(buf, fp->buf + fp->pos, len);
1045 static int save_write(void *file, const void *buf, u32 len)
1047 struct save_fp *fp = file;
1048 if (fp == NULL || buf == NULL)
1051 memcpy(fp->buf + fp->pos, buf, len);
1056 static long save_seek(void *file, long offs, int whence)
1058 struct save_fp *fp = file;
1075 static void save_close(void *file)
1077 struct save_fp *fp = file;
1078 size_t r_size = retro_serialize_size();
1082 if (fp->pos > r_size)
1083 SysPrintf("ERROR: save buffer overflow detected\n");
1084 else if (fp->is_write && fp->pos < r_size)
1085 // make sure we don't save trash in leftover space
1086 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
1090 bool retro_serialize(void *data, size_t size)
1093 CdromFrontendId = disk_current_index;
1094 ret = SaveState(data);
1095 return ret == 0 ? true : false;
1098 static bool disk_set_image_index(unsigned int index);
1100 bool retro_unserialize(const void *data, size_t size)
1103 CdromFrontendId = -1;
1104 ret = LoadState(data);
1107 if (CdromFrontendId != -1 && CdromFrontendId != disk_current_index)
1108 disk_set_image_index(CdromFrontendId);
1113 void retro_cheat_reset(void)
1118 void retro_cheat_set(unsigned index, bool enabled, const char *code)
1123 // cheat funcs are destructive, need a copy...
1128 //Prepare buffered cheat for PCSX's AddCheat fucntion.
1135 if (++nonhexdec % 2)
1147 if (index < NumCheats)
1148 ret = EditCheat(index, "", buf);
1150 ret = AddCheat("", buf);
1154 SysPrintf("Failed to set cheat %#u\n", index);
1155 else if (index < NumCheats)
1156 Cheats[index].Enabled = enabled;
1160 // just in case, maybe a win-rt port in the future?
1168 #define PATH_MAX 4096
1171 /* multidisk support */
1172 static unsigned int disk_initial_index;
1173 static char disk_initial_path[PATH_MAX];
1174 static bool disk_ejected;
1175 static unsigned int disk_count;
1176 static struct disks_state
1180 int internal_index; // for multidisk eboots
1183 static void get_disk_label(char *disk_label, const char *disk_path, size_t len)
1185 const char *base = NULL;
1187 if (!disk_path || (*disk_path == '\0'))
1190 base = strrchr(disk_path, SLASH);
1197 strncpy(disk_label, base, len - 1);
1198 disk_label[len - 1] = '\0';
1200 char *ext = strrchr(disk_label, '.');
1205 static void disk_init(void)
1209 disk_ejected = false;
1210 disk_current_index = 0;
1213 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++)
1215 if (disks[i].fname != NULL)
1217 free(disks[i].fname);
1218 disks[i].fname = NULL;
1220 if (disks[i].flabel != NULL)
1222 free(disks[i].flabel);
1223 disks[i].flabel = NULL;
1225 disks[i].internal_index = 0;
1229 static bool disk_set_eject_state(bool ejected)
1232 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
1235 disk_ejected = ejected;
1239 static bool disk_get_eject_state(void)
1241 /* can't be controlled by emulated software */
1242 return disk_ejected;
1245 static unsigned int disk_get_image_index(void)
1247 return disk_current_index;
1250 static bool disk_set_image_index(unsigned int index)
1252 if (index >= sizeof(disks) / sizeof(disks[0]))
1256 CdromLabel[0] = '\0';
1258 if (disks[index].fname == NULL)
1260 SysPrintf("missing disk #%u\n", index);
1263 // RetroArch specifies "no disk" with index == count,
1264 // so don't fail here..
1265 disk_current_index = index;
1269 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
1270 disks[index].fname, disks[index].internal_index);
1272 cdrIsoMultidiskSelect = disks[index].internal_index;
1273 set_cd_image(disks[index].fname);
1274 if (ReloadCdromPlugin() < 0)
1276 SysPrintf("failed to load cdr plugin\n");
1281 SysPrintf("failed to open cdr plugin\n");
1287 SetCdOpenCaseTime(time(NULL) + 2);
1291 disk_current_index = index;
1295 static unsigned int disk_get_num_images(void)
1300 static bool disk_replace_image_index(unsigned index,
1301 const struct retro_game_info *info)
1303 char *old_fname = NULL;
1304 char *old_flabel = NULL;
1307 if (index >= sizeof(disks) / sizeof(disks[0]))
1310 old_fname = disks[index].fname;
1311 old_flabel = disks[index].flabel;
1313 disks[index].fname = NULL;
1314 disks[index].flabel = NULL;
1315 disks[index].internal_index = 0;
1319 char disk_label[PATH_MAX];
1320 disk_label[0] = '\0';
1322 disks[index].fname = strdup(info->path);
1324 get_disk_label(disk_label, info->path, PATH_MAX);
1325 disks[index].flabel = strdup(disk_label);
1327 if (index == disk_current_index)
1328 ret = disk_set_image_index(index);
1331 if (old_fname != NULL)
1334 if (old_flabel != NULL)
1340 static bool disk_add_image_index(void)
1342 if (disk_count >= 8)
1349 static bool disk_set_initial_image(unsigned index, const char *path)
1351 if (index >= sizeof(disks) / sizeof(disks[0]))
1354 if (!path || (*path == '\0'))
1357 disk_initial_index = index;
1359 strncpy(disk_initial_path, path, sizeof(disk_initial_path) - 1);
1360 disk_initial_path[sizeof(disk_initial_path) - 1] = '\0';
1365 static bool disk_get_image_path(unsigned index, char *path, size_t len)
1367 const char *fname = NULL;
1372 if (index >= sizeof(disks) / sizeof(disks[0]))
1375 fname = disks[index].fname;
1377 if (!fname || (*fname == '\0'))
1380 strncpy(path, fname, len - 1);
1381 path[len - 1] = '\0';
1386 static bool disk_get_image_label(unsigned index, char *label, size_t len)
1388 const char *flabel = NULL;
1393 if (index >= sizeof(disks) / sizeof(disks[0]))
1396 flabel = disks[index].flabel;
1398 if (!flabel || (*flabel == '\0'))
1401 strncpy(label, flabel, len - 1);
1402 label[len - 1] = '\0';
1407 static struct retro_disk_control_callback disk_control = {
1408 .set_eject_state = disk_set_eject_state,
1409 .get_eject_state = disk_get_eject_state,
1410 .get_image_index = disk_get_image_index,
1411 .set_image_index = disk_set_image_index,
1412 .get_num_images = disk_get_num_images,
1413 .replace_image_index = disk_replace_image_index,
1414 .add_image_index = disk_add_image_index,
1417 static struct retro_disk_control_ext_callback disk_control_ext = {
1418 .set_eject_state = disk_set_eject_state,
1419 .get_eject_state = disk_get_eject_state,
1420 .get_image_index = disk_get_image_index,
1421 .set_image_index = disk_set_image_index,
1422 .get_num_images = disk_get_num_images,
1423 .replace_image_index = disk_replace_image_index,
1424 .add_image_index = disk_add_image_index,
1425 .set_initial_image = disk_set_initial_image,
1426 .get_image_path = disk_get_image_path,
1427 .get_image_label = disk_get_image_label,
1430 static char base_dir[1024];
1432 static bool read_m3u(const char *file)
1435 char name[PATH_MAX];
1436 FILE *fp = fopen(file, "r");
1440 while (fgets(line, sizeof(line), fp) && disk_count < sizeof(disks) / sizeof(disks[0]))
1444 char *carrige_return = strchr(line, '\r');
1446 *carrige_return = '\0';
1447 char *newline = strchr(line, '\n');
1451 if (line[0] != '\0')
1453 char disk_label[PATH_MAX];
1454 disk_label[0] = '\0';
1456 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
1457 disks[disk_count].fname = strdup(name);
1459 get_disk_label(disk_label, name, PATH_MAX);
1460 disks[disk_count].flabel = strdup(disk_label);
1467 return (disk_count != 0);
1470 static void extract_directory(char *buf, const char *path, size_t size)
1473 strncpy(buf, path, size - 1);
1474 buf[size - 1] = '\0';
1476 base = strrchr(buf, '/');
1478 base = strrchr(buf, '\\');
1489 #if defined(__QNX__) || defined(_WIN32)
1490 /* Blackberry QNX doesn't have strcasestr */
1493 * Find the first occurrence of find in s, ignore case.
1496 strcasestr(const char *s, const char *find)
1501 if ((c = *find++) != 0)
1503 c = tolower((unsigned char)c);
1509 if ((sc = *s++) == 0)
1511 } while ((char)tolower((unsigned char)sc) != c);
1512 } while (strncasecmp(s, find, len) != 0);
1519 static void set_retro_memmap(void)
1522 struct retro_memory_map retromap = { 0 };
1523 struct retro_memory_descriptor mmap = {
1524 0, psxM, 0, 0, 0, 0, 0x200000
1527 retromap.descriptors = &mmap;
1528 retromap.num_descriptors = 1;
1530 environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &retromap);
1534 static void show_notification(const char *msg_str,
1535 unsigned duration_ms, unsigned priority)
1537 if (msg_interface_version >= 1)
1539 struct retro_message_ext msg = {
1544 RETRO_MESSAGE_TARGET_ALL,
1545 RETRO_MESSAGE_TYPE_NOTIFICATION,
1548 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE_EXT, &msg);
1552 struct retro_message msg = {
1556 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
1560 static void retro_audio_buff_status_cb(
1561 bool active, unsigned occupancy, bool underrun_likely)
1563 retro_audio_buff_active = active;
1564 retro_audio_buff_occupancy = occupancy;
1565 retro_audio_buff_underrun = underrun_likely;
1568 static void retro_set_audio_buff_status_cb(void)
1570 if (frameskip_type == FRAMESKIP_NONE)
1572 environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, NULL);
1573 retro_audio_latency = 0;
1577 bool calculate_audio_latency = true;
1579 if (frameskip_type == FRAMESKIP_FIXED_INTERVAL)
1580 environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, NULL);
1583 struct retro_audio_buffer_status_callback buf_status_cb;
1584 buf_status_cb.callback = retro_audio_buff_status_cb;
1585 if (!environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK,
1588 retro_audio_buff_active = false;
1589 retro_audio_buff_occupancy = 0;
1590 retro_audio_buff_underrun = false;
1591 retro_audio_latency = 0;
1592 calculate_audio_latency = false;
1596 if (calculate_audio_latency)
1598 /* Frameskip is enabled - increase frontend
1599 * audio latency to minimise potential
1600 * buffer underruns */
1601 uint32_t frame_time_usec = 1000000.0 / (is_pal_mode ? 50.0 : 60.0);
1603 /* Set latency to 6x current frame time... */
1604 retro_audio_latency = (unsigned)(6 * frame_time_usec / 1000);
1606 /* ...then round up to nearest multiple of 32 */
1607 retro_audio_latency = (retro_audio_latency + 0x1F) & ~0x1F;
1611 update_audio_latency = true;
1612 frameskip_counter = 0;
1615 static void update_variables(bool in_flight);
1616 bool retro_load_game(const struct retro_game_info *info)
1619 unsigned int cd_index = 0;
1620 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
1621 bool is_exe = (strcasestr(info->path, ".exe") != NULL);
1624 struct retro_input_descriptor desc[] = {
1625 #define JOYP(port) \
1626 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" }, \
1627 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" }, \
1628 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" }, \
1629 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" }, \
1630 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" }, \
1631 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" }, \
1632 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" }, \
1633 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" }, \
1634 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" }, \
1635 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" }, \
1636 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" }, \
1637 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" }, \
1638 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" }, \
1639 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" }, \
1640 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" }, \
1641 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, \
1642 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" }, \
1643 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" }, \
1644 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" }, \
1645 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" }, \
1646 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_TRIGGER, "Gun Trigger" }, \
1647 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_RELOAD, "Gun Reload" }, \
1648 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A, "Gun Aux A" }, \
1649 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_B, "Gun Aux B" }, \
1650 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_START, "Gun Start" },
1666 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1668 #ifdef FRONTEND_SUPPORTS_RGB565
1669 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
1670 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
1672 SysPrintf("RGB565 supported, using it\n");
1676 if (info == NULL || info->path == NULL)
1678 SysPrintf("info->path required\n");
1682 update_variables(false);
1692 extract_directory(base_dir, info->path, sizeof(base_dir));
1696 if (!read_m3u(info->path))
1698 log_cb(RETRO_LOG_INFO, "failed to read m3u file\n");
1704 char disk_label[PATH_MAX];
1705 disk_label[0] = '\0';
1708 disks[0].fname = strdup(info->path);
1710 get_disk_label(disk_label, info->path, PATH_MAX);
1711 disks[0].flabel = strdup(disk_label);
1714 /* If this is an M3U file, attempt to set the
1715 * initial disk image */
1716 if (is_m3u && (disk_initial_index > 0) && (disk_initial_index < disk_count))
1718 const char *fname = disks[disk_initial_index].fname;
1720 if (fname && (*fname != '\0'))
1721 if (strcmp(disk_initial_path, fname) == 0)
1722 cd_index = disk_initial_index;
1725 set_cd_image(disks[cd_index].fname);
1726 disk_current_index = cd_index;
1728 /* have to reload after set_cd_image for correct cdr plugin */
1729 if (LoadPlugins() == -1)
1731 log_cb(RETRO_LOG_INFO, "failed to load plugins\n");
1738 if (OpenPlugins() == -1)
1740 log_cb(RETRO_LOG_INFO, "failed to open plugins\n");
1744 /* Handle multi-disk images (i.e. PBP)
1745 * > Cannot do this until after OpenPlugins() is
1746 * called (since this sets the value of
1747 * cdrIsoMultidiskCount) */
1748 if (!is_m3u && (cdrIsoMultidiskCount > 1))
1750 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
1752 /* Small annoyance: We need to change the label
1753 * of disk 0, so have to clear existing entries */
1754 if (disks[0].fname != NULL)
1755 free(disks[0].fname);
1756 disks[0].fname = NULL;
1758 if (disks[0].flabel != NULL)
1759 free(disks[0].flabel);
1760 disks[0].flabel = NULL;
1762 for (i = 0; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++)
1764 char disk_name[PATH_MAX - 16] = { 0 };
1765 char disk_label[PATH_MAX] = { 0 };
1767 disks[i].fname = strdup(info->path);
1769 get_disk_label(disk_name, info->path, sizeof(disk_name));
1770 snprintf(disk_label, sizeof(disk_label), "%s #%u", disk_name, (unsigned)i + 1);
1771 disks[i].flabel = strdup(disk_label);
1773 disks[i].internal_index = i;
1776 /* This is not an M3U file, so initial disk
1777 * image has not yet been set - attempt to
1779 if ((disk_initial_index > 0) && (disk_initial_index < disk_count))
1781 const char *fname = disks[disk_initial_index].fname;
1783 if (fname && (*fname != '\0'))
1784 if (strcmp(disk_initial_path, fname) == 0)
1785 cd_index = disk_initial_index;
1791 CdromLabel[0] = '\0';
1793 cdrIsoMultidiskSelect = disks[cd_index].internal_index;
1794 disk_current_index = cd_index;
1795 set_cd_image(disks[cd_index].fname);
1797 if (ReloadCdromPlugin() < 0)
1799 log_cb(RETRO_LOG_INFO, "failed to reload cdr plugins\n");
1804 log_cb(RETRO_LOG_INFO, "failed to open cdr plugin\n");
1810 /* set ports to use "standard controller" initially */
1811 for (i = 0; i < 8; ++i)
1812 in_type[i] = PSE_PAD_TYPE_STANDARD;
1814 plugin_call_rearmed_cbs();
1815 /* dfinput_activate(); */
1817 if (!is_exe && CheckCdrom() == -1)
1819 log_cb(RETRO_LOG_INFO, "unsupported/invalid CD image: %s\n", info->path);
1826 ret = Load(info->path);
1831 log_cb(RETRO_LOG_INFO, "could not load %s (%d)\n", is_exe ? "exe" : "CD", ret);
1837 retro_set_audio_buff_status_cb();
1839 if (check_unsatisfied_libcrypt())
1840 show_notification("LibCrypt protected game with missing SBI detected", 3000, 3);
1845 unsigned retro_get_region(void)
1847 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1850 void *retro_get_memory_data(unsigned id)
1852 if (id == RETRO_MEMORY_SAVE_RAM)
1854 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1860 size_t retro_get_memory_size(unsigned id)
1862 if (id == RETRO_MEMORY_SAVE_RAM)
1864 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1870 void retro_reset(void)
1872 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
1877 static const unsigned short retro_psx_map[] = {
1878 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
1879 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
1880 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
1881 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
1882 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
1883 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
1884 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
1885 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
1886 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
1887 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
1888 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
1889 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
1890 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
1891 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
1892 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
1893 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
1895 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
1897 //Percentage distance of screen to adjust for Guncon
1898 static int GunconAdjustX = 0;
1899 static int GunconAdjustY = 0;
1901 //Used when out by a percentage with Guncon
1902 static float GunconAdjustRatioX = 1;
1903 static float GunconAdjustRatioY = 1;
1905 static void update_variables(bool in_flight)
1907 struct retro_variable var;
1909 // Always enable GPU_PEOPS_OLD_FRAME_SKIP flag
1910 // (this is set in standalone, with no option
1912 int gpu_peops_fix = GPU_PEOPS_OLD_FRAME_SKIP;
1914 frameskip_type_t prev_frameskip_type;
1917 var.key = "pcsx_rearmed_frameskip_type";
1919 prev_frameskip_type = frameskip_type;
1920 frameskip_type = FRAMESKIP_NONE;
1921 pl_rearmed_cbs.frameskip = 0;
1923 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1925 if (strcmp(var.value, "auto") == 0)
1926 frameskip_type = FRAMESKIP_AUTO;
1927 if (strcmp(var.value, "auto_threshold") == 0)
1928 frameskip_type = FRAMESKIP_AUTO_THRESHOLD;
1929 if (strcmp(var.value, "fixed_interval") == 0)
1930 frameskip_type = FRAMESKIP_FIXED_INTERVAL;
1933 if (frameskip_type != 0)
1934 pl_rearmed_cbs.frameskip = -1;
1937 var.key = "pcsx_rearmed_frameskip_threshold";
1938 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1940 frameskip_threshold = strtol(var.value, NULL, 10);
1944 var.key = "pcsx_rearmed_frameskip_interval";
1945 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1947 frameskip_interval = strtol(var.value, NULL, 10);
1951 var.key = "pcsx_rearmed_region";
1952 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1955 if (strcmp(var.value, "auto") == 0)
1957 else if (strcmp(var.value, "NTSC") == 0)
1959 else if (strcmp(var.value, "PAL") == 0)
1966 var.key = "pcsx_rearmed_negcon_deadzone";
1967 negcon_deadzone = 0;
1968 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1970 negcon_deadzone = (int)(atoi(var.value) * 0.01f * NEGCON_RANGE);
1974 var.key = "pcsx_rearmed_negcon_response";
1975 negcon_linearity = 1;
1976 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1978 if (strcmp(var.value, "quadratic") == 0)
1980 negcon_linearity = 2;
1982 else if (strcmp(var.value, "cubic") == 0)
1984 negcon_linearity = 3;
1989 var.key = "pcsx_rearmed_analog_axis_modifier";
1990 axis_bounds_modifier = true;
1991 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1993 if (strcmp(var.value, "square") == 0)
1995 axis_bounds_modifier = true;
1997 else if (strcmp(var.value, "circle") == 0)
1999 axis_bounds_modifier = false;
2004 var.key = "pcsx_rearmed_vibration";
2006 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2008 if (strcmp(var.value, "disabled") == 0)
2009 in_enable_vibration = 0;
2010 else if (strcmp(var.value, "enabled") == 0)
2011 in_enable_vibration = 1;
2015 var.key = "pcsx_rearmed_dithering";
2017 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2019 if (strcmp(var.value, "disabled") == 0)
2021 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
2022 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 0;
2023 pl_rearmed_cbs.gpu_unai.dithering = 0;
2025 pl_rearmed_cbs.gpu_neon.allow_dithering = 0;
2028 else if (strcmp(var.value, "enabled") == 0)
2030 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
2031 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 1;
2032 pl_rearmed_cbs.gpu_unai.dithering = 1;
2034 pl_rearmed_cbs.gpu_neon.allow_dithering = 1;
2041 var.key = "pcsx_rearmed_neon_interlace_enable_v2";
2043 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2045 if (strcmp(var.value, "disabled") == 0)
2046 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
2047 else if (strcmp(var.value, "enabled") == 0)
2048 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
2050 pl_rearmed_cbs.gpu_neon.allow_interlace = 2;
2054 var.key = "pcsx_rearmed_neon_enhancement_enable";
2056 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2058 if (strcmp(var.value, "disabled") == 0)
2059 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
2060 else if (strcmp(var.value, "enabled") == 0)
2061 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
2065 var.key = "pcsx_rearmed_neon_enhancement_no_main";
2067 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2069 if (strcmp(var.value, "disabled") == 0)
2070 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
2071 else if (strcmp(var.value, "enabled") == 0)
2072 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
2077 var.key = "pcsx_rearmed_duping_enable";
2079 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2081 if (strcmp(var.value, "disabled") == 0)
2082 duping_enable = false;
2083 else if (strcmp(var.value, "enabled") == 0)
2084 duping_enable = true;
2088 var.key = "pcsx_rearmed_display_internal_fps";
2090 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2092 if (strcmp(var.value, "disabled") == 0)
2093 display_internal_fps = false;
2094 else if (strcmp(var.value, "enabled") == 0)
2095 display_internal_fps = true;
2099 // CPU emulation related config
2102 var.key = "pcsx_rearmed_drc";
2104 if (!environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
2105 var.value = "enabled";
2108 R3000Acpu *prev_cpu = psxCpu;
2112 Config.Cpu = CPU_INTERPRETER;
2115 if (strcmp(var.value, "disabled") == 0)
2116 Config.Cpu = CPU_INTERPRETER;
2117 else if (strcmp(var.value, "enabled") == 0)
2118 Config.Cpu = CPU_DYNAREC;
2120 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
2121 if (psxCpu != prev_cpu)
2123 prev_cpu->Notify(R3000ACPU_NOTIFY_BEFORE_SAVE, NULL);
2124 prev_cpu->Shutdown();
2127 psxCpu->Notify(R3000ACPU_NOTIFY_AFTER_LOAD, NULL);
2130 #endif /* !DRC_DISABLE */
2133 var.key = "pcsx_rearmed_psxclock";
2134 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2136 int psxclock = atoi(var.value);
2137 Config.cycle_multiplier = 10000 / psxclock;
2140 #if !defined(DRC_DISABLE) && !defined(LIGHTREC)
2142 var.key = "pcsx_rearmed_nosmccheck";
2143 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2145 if (strcmp(var.value, "enabled") == 0)
2146 new_dynarec_hacks |= NDHACK_NO_SMC_CHECK;
2148 new_dynarec_hacks &= ~NDHACK_NO_SMC_CHECK;
2152 var.key = "pcsx_rearmed_gteregsunneeded";
2153 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2155 if (strcmp(var.value, "enabled") == 0)
2156 new_dynarec_hacks |= NDHACK_GTE_UNNEEDED;
2158 new_dynarec_hacks &= ~NDHACK_GTE_UNNEEDED;
2162 var.key = "pcsx_rearmed_nogteflags";
2163 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2165 if (strcmp(var.value, "enabled") == 0)
2166 new_dynarec_hacks |= NDHACK_GTE_NO_FLAGS;
2168 new_dynarec_hacks &= ~NDHACK_GTE_NO_FLAGS;
2172 var.key = "pcsx_rearmed_nocompathacks";
2173 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2175 if (strcmp(var.value, "enabled") == 0)
2176 new_dynarec_hacks |= NDHACK_NO_COMPAT_HACKS;
2178 new_dynarec_hacks &= ~NDHACK_NO_COMPAT_HACKS;
2180 #endif /* !DRC_DISABLE && !LIGHTREC */
2183 var.key = "pcsx_rearmed_nostalls";
2184 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2186 if (strcmp(var.value, "enabled") == 0)
2187 Config.DisableStalls = 1;
2189 Config.DisableStalls = 0;
2193 var.key = "pcsx_rearmed_icache_emulation";
2194 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2196 if (strcmp(var.value, "disabled") == 0)
2197 Config.icache_emulation = 0;
2198 else if (strcmp(var.value, "enabled") == 0)
2199 Config.icache_emulation = 1;
2203 var.key = "pcsx_rearmed_exception_emulation";
2204 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2206 if (strcmp(var.value, "enabled") == 0)
2207 Config.PreciseExceptions = 1;
2209 Config.PreciseExceptions = 0;
2212 psxCpu->ApplyConfig();
2214 // end of CPU emu config
2218 var.key = "pcsx_rearmed_spu_reverb";
2220 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2222 if (strcmp(var.value, "disabled") == 0)
2223 spu_config.iUseReverb = false;
2224 else if (strcmp(var.value, "enabled") == 0)
2225 spu_config.iUseReverb = true;
2229 var.key = "pcsx_rearmed_spu_interpolation";
2231 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2233 if (strcmp(var.value, "simple") == 0)
2234 spu_config.iUseInterpolation = 1;
2235 else if (strcmp(var.value, "gaussian") == 0)
2236 spu_config.iUseInterpolation = 2;
2237 else if (strcmp(var.value, "cubic") == 0)
2238 spu_config.iUseInterpolation = 3;
2239 else if (strcmp(var.value, "off") == 0)
2240 spu_config.iUseInterpolation = 0;
2245 var.key = "pcsx_rearmed_spu_thread";
2246 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2248 if (strcmp(var.value, "enabled") == 0)
2249 spu_config.iUseThread = 1;
2251 spu_config.iUseThread = 0;
2255 #if 0 // currently disabled, see USE_READ_THREAD in libpcsxcore/cdriso.c
2256 if (P_HAVE_PTHREAD) {
2258 var.key = "pcsx_rearmed_async_cd";
2259 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2261 if (strcmp(var.value, "async") == 0)
2264 Config.CHD_Precache = 0;
2266 else if (strcmp(var.value, "sync") == 0)
2269 Config.CHD_Precache = 0;
2271 else if (strcmp(var.value, "precache") == 0)
2274 Config.CHD_Precache = 1;
2281 var.key = "pcsx_rearmed_noxadecoding";
2282 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2284 if (strcmp(var.value, "disabled") == 0)
2291 var.key = "pcsx_rearmed_nocdaudio";
2292 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2294 if (strcmp(var.value, "disabled") == 0)
2301 var.key = "pcsx_rearmed_gpu_slow_llists";
2302 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2304 if (strcmp(var.value, "disabled") == 0)
2305 Config.GpuListWalking = 0;
2306 else if (strcmp(var.value, "enabled") == 0)
2307 Config.GpuListWalking = 1;
2309 Config.GpuListWalking = -1;
2313 var.key = "pcsx_rearmed_screen_centering";
2314 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2316 if (strcmp(var.value, "game") == 0)
2317 pl_rearmed_cbs.screen_centering_type = 1;
2318 else if (strcmp(var.value, "borderless") == 0)
2319 pl_rearmed_cbs.screen_centering_type = 2;
2320 else if (strcmp(var.value, "manual") == 0)
2321 pl_rearmed_cbs.screen_centering_type = 3;
2323 pl_rearmed_cbs.screen_centering_type = 0;
2327 var.key = "pcsx_rearmed_screen_centering_x";
2328 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2330 pl_rearmed_cbs.screen_centering_x = atoi(var.value);
2334 var.key = "pcsx_rearmed_screen_centering_y";
2335 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2337 pl_rearmed_cbs.screen_centering_y = atoi(var.value);
2340 #ifdef THREAD_RENDERING
2341 var.key = "pcsx_rearmed_gpu_thread_rendering";
2344 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2346 if (strcmp(var.value, "disabled") == 0)
2347 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_OFF;
2348 else if (strcmp(var.value, "sync") == 0)
2349 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_SYNC;
2350 else if (strcmp(var.value, "async") == 0)
2351 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_ASYNC;
2357 var.key = "pcsx_rearmed_gpu_peops_odd_even_bit";
2359 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2361 if (strcmp(var.value, "enabled") == 0)
2362 gpu_peops_fix |= GPU_PEOPS_ODD_EVEN_BIT;
2366 var.key = "pcsx_rearmed_gpu_peops_expand_screen_width";
2368 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2370 if (strcmp(var.value, "enabled") == 0)
2371 gpu_peops_fix |= GPU_PEOPS_EXPAND_SCREEN_WIDTH;
2375 var.key = "pcsx_rearmed_gpu_peops_ignore_brightness";
2377 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2379 if (strcmp(var.value, "enabled") == 0)
2380 gpu_peops_fix |= GPU_PEOPS_IGNORE_BRIGHTNESS;
2384 var.key = "pcsx_rearmed_gpu_peops_disable_coord_check";
2386 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2388 if (strcmp(var.value, "enabled") == 0)
2389 gpu_peops_fix |= GPU_PEOPS_DISABLE_COORD_CHECK;
2393 var.key = "pcsx_rearmed_gpu_peops_lazy_screen_update";
2395 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2397 if (strcmp(var.value, "enabled") == 0)
2398 gpu_peops_fix |= GPU_PEOPS_LAZY_SCREEN_UPDATE;
2402 var.key = "pcsx_rearmed_gpu_peops_repeated_triangles";
2404 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2406 if (strcmp(var.value, "enabled") == 0)
2407 gpu_peops_fix |= GPU_PEOPS_REPEATED_TRIANGLES;
2411 var.key = "pcsx_rearmed_gpu_peops_quads_with_triangles";
2413 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2415 if (strcmp(var.value, "enabled") == 0)
2416 gpu_peops_fix |= GPU_PEOPS_QUADS_WITH_TRIANGLES;
2420 var.key = "pcsx_rearmed_gpu_peops_fake_busy_state";
2422 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2424 if (strcmp(var.value, "enabled") == 0)
2425 gpu_peops_fix |= GPU_PEOPS_FAKE_BUSY_STATE;
2428 if (pl_rearmed_cbs.gpu_peops.dwActFixes != gpu_peops_fix)
2429 pl_rearmed_cbs.gpu_peops.dwActFixes = gpu_peops_fix;
2433 /* Note: This used to be an option, but it only works
2434 * (correctly) when running high resolution games
2435 * (480i, 512i) and has been obsoleted by
2436 * pcsx_rearmed_gpu_unai_scale_hires */
2437 pl_rearmed_cbs.gpu_unai.ilace_force = 0;
2438 /* Note: This used to be an option, but it has no
2439 * discernable effect and has been obsoleted by
2440 * pcsx_rearmed_gpu_unai_scale_hires */
2441 pl_rearmed_cbs.gpu_unai.pixel_skip = 0;
2443 var.key = "pcsx_rearmed_gpu_unai_lighting";
2446 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2448 if (strcmp(var.value, "disabled") == 0)
2449 pl_rearmed_cbs.gpu_unai.lighting = 0;
2450 else if (strcmp(var.value, "enabled") == 0)
2451 pl_rearmed_cbs.gpu_unai.lighting = 1;
2454 var.key = "pcsx_rearmed_gpu_unai_fast_lighting";
2457 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2459 if (strcmp(var.value, "disabled") == 0)
2460 pl_rearmed_cbs.gpu_unai.fast_lighting = 0;
2461 else if (strcmp(var.value, "enabled") == 0)
2462 pl_rearmed_cbs.gpu_unai.fast_lighting = 1;
2465 var.key = "pcsx_rearmed_gpu_unai_blending";
2468 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2470 if (strcmp(var.value, "disabled") == 0)
2471 pl_rearmed_cbs.gpu_unai.blending = 0;
2472 else if (strcmp(var.value, "enabled") == 0)
2473 pl_rearmed_cbs.gpu_unai.blending = 1;
2476 var.key = "pcsx_rearmed_gpu_unai_scale_hires";
2479 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2481 if (strcmp(var.value, "disabled") == 0)
2482 pl_rearmed_cbs.gpu_unai.scale_hires = 0;
2483 else if (strcmp(var.value, "enabled") == 0)
2484 pl_rearmed_cbs.gpu_unai.scale_hires = 1;
2489 var.key = "pcsx_rearmed_crosshair1";
2491 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2493 if (strcmp(var.value, "disabled") == 0)
2494 in_enable_crosshair[0] = 0;
2495 else if (strcmp(var.value, "blue") == 0)
2496 in_enable_crosshair[0] = 0x1F;
2497 else if (strcmp(var.value, "green") == 0)
2498 in_enable_crosshair[0] = 0x7E0;
2499 else if (strcmp(var.value, "red") == 0)
2500 in_enable_crosshair[0] = 0xF800;
2501 else if (strcmp(var.value, "white") == 0)
2502 in_enable_crosshair[0] = 0xFFFF;
2506 var.key = "pcsx_rearmed_crosshair2";
2508 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2510 if (strcmp(var.value, "disabled") == 0)
2511 in_enable_crosshair[1] = 0;
2512 else if (strcmp(var.value, "blue") == 0)
2513 in_enable_crosshair[1] = 0x1F;
2514 else if (strcmp(var.value, "green") == 0)
2515 in_enable_crosshair[1] = 0x7E0;
2516 else if (strcmp(var.value, "red") == 0)
2517 in_enable_crosshair[1] = 0xF800;
2518 else if (strcmp(var.value, "white") == 0)
2519 in_enable_crosshair[1] = 0xFFFF;
2522 //This adjustment process gives the user the ability to manually align the mouse up better
2523 //with where the shots are in the emulator.
2526 var.key = "pcsx_rearmed_konamigunadjustx";
2528 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2530 KonamiGunAdjustX = atof(var.value) / 100.0f;
2534 var.key = "pcsx_rearmed_konamigunadjusty";
2536 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2538 KonamiGunAdjustY = atof(var.value) / 100.0f;
2542 var.key = "pcsx_rearmed_gunconadjustx";
2544 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2546 GunconAdjustX = atoi(var.value);
2550 var.key = "pcsx_rearmed_gunconadjusty";
2552 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2554 GunconAdjustY = atoi(var.value);
2558 var.key = "pcsx_rearmed_gunconadjustratiox";
2560 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2562 GunconAdjustRatioX = atof(var.value);
2566 var.key = "pcsx_rearmed_gunconadjustratioy";
2568 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2570 GunconAdjustRatioY = atof(var.value);
2574 var.key = "pcsx_rearmed_input_sensitivity";
2575 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2577 mouse_sensitivity = atof(var.value);
2583 var.key = "pcsx_rearmed_show_bios_bootlogo";
2584 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2586 Config.SlowBoot = 0;
2587 if (strcmp(var.value, "enabled") == 0)
2588 Config.SlowBoot = 1;
2594 // inform core things about possible config changes
2595 plugin_call_rearmed_cbs();
2597 if (GPU_open != NULL && GPU_close != NULL)
2600 GPU_open(&gpuDisp, "PCSX", NULL);
2603 /* Reinitialise frameskipping, if required */
2604 if (((frameskip_type != prev_frameskip_type)))
2605 retro_set_audio_buff_status_cb();
2607 /* dfinput_activate(); */
2610 update_option_visibility();
2613 // Taken from beetle-psx-libretro
2614 static uint16_t get_analog_button(int16_t ret, retro_input_state_t input_state_cb, int player_index, int id)
2616 // NOTE: Analog buttons were added Nov 2017. Not all front-ends support this
2617 // feature (or pre-date it) so we need to handle this in a graceful way.
2619 // First, try and get an analog value using the new libretro API constant
2620 uint16_t button = input_state_cb(player_index,
2621 RETRO_DEVICE_ANALOG,
2622 RETRO_DEVICE_INDEX_ANALOG_BUTTON,
2624 button = MIN(button / 128, 255);
2628 // If we got exactly zero, we're either not pressing the button, or the front-end
2629 // is not reporting analog values. We need to do a second check using the classic
2630 // digital API method, to at least get some response - better than nothing.
2632 // NOTE: If we're really just not holding the button, we're still going to get zero.
2634 button = (ret & (1 << id)) ? 255 : 0;
2640 unsigned char axis_range_modifier(int16_t axis_value, bool is_square)
2642 float modifier_axis_range = 0;
2646 modifier_axis_range = round((axis_value >> 8) / 0.785) + 128;
2647 if (modifier_axis_range < 0)
2649 modifier_axis_range = 0;
2651 else if (modifier_axis_range > 255)
2653 modifier_axis_range = 255;
2658 modifier_axis_range = MIN(((axis_value >> 8) + 128), 255);
2661 return modifier_axis_range;
2664 static void update_input_guncon(int port, int ret)
2667 //Separate pointer and lightgun control types
2669 //Mouse range is -32767 -> 32767
2671 //Use the left analog stick field to store the absolute coordinates
2673 int gunx = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X);
2674 int guny = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y);
2676 //Have the Libretro API let /libpcsxcore/plugins.c know when the lightgun is pointed offscreen
2677 //Offscreen value is chosen to be well out of range of any possible scaling done via core options
2678 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))
2680 in_analog_left[port][0] = 65536;
2681 in_analog_left[port][1] = 65536;
2685 in_analog_left[port][0] = ((gunx * GunconAdjustRatioX) + (GunconAdjustX * 655)) / 64 + 512;
2686 in_analog_left[port][1] = ((guny * GunconAdjustRatioY) + (GunconAdjustY * 655)) / 64 + 512;
2689 //GUNCON has 3 controls, Trigger,A,B which equal Circle,Start,Cross
2692 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))
2693 in_keystate[port] |= (1 << DKEY_CIRCLE);
2696 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A))
2697 in_keystate[port] |= (1 << DKEY_START);
2700 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_B))
2701 in_keystate[port] |= (1 << DKEY_CROSS);
2705 static void update_input_justifier(int port, int ret)
2708 //Separate pointer and lightgun control types
2710 //RetroArch lightgun range is -32767 -> 32767 on both axes (positive Y is down)
2712 //JUSTIFIER has 3 controls, Trigger,Special,Start which equal Square,Cross,Start
2715 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))
2716 in_keystate[port] |= (1 << DKEY_SQUARE);
2719 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A))
2720 in_keystate[port] |= (1 << DKEY_CROSS);
2723 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_START))
2724 in_keystate[port] |= (1 << DKEY_START);
2728 static void update_input_negcon(int port, int ret)
2734 float negcon_twist_amplitude;
2736 // Query digital inputs
2739 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_UP))
2740 in_keystate[port] |= (1 << DKEY_UP);
2742 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT))
2743 in_keystate[port] |= (1 << DKEY_RIGHT);
2745 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN))
2746 in_keystate[port] |= (1 << DKEY_DOWN);
2748 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT))
2749 in_keystate[port] |= (1 << DKEY_LEFT);
2751 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_START))
2752 in_keystate[port] |= (1 << DKEY_START);
2754 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_A))
2755 in_keystate[port] |= (1 << DKEY_CIRCLE);
2757 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_X))
2758 in_keystate[port] |= (1 << DKEY_TRIANGLE);
2759 // > neGcon R shoulder (digital)
2760 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_R))
2761 in_keystate[port] |= (1 << DKEY_R1);
2762 // Query analog inputs
2764 // From studying 'libpcsxcore/plugins.c' and 'frontend/plugin.c':
2765 // >> pad->leftJoyX == in_analog_left[port][0] == NeGcon II
2766 // >> pad->leftJoyY == in_analog_left[port][1] == NeGcon L
2767 // >> pad->rightJoyX == in_analog_right[port][0] == NeGcon twist
2768 // >> pad->rightJoyY == in_analog_right[port][1] == NeGcon I
2769 // So we just have to map in_analog_left/right to more
2770 // appropriate inputs...
2773 // >> Get raw analog stick value and account for deadzone
2774 lsx = input_state_cb(port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
2775 if (lsx > negcon_deadzone)
2776 lsx = lsx - negcon_deadzone;
2777 else if (lsx < -negcon_deadzone)
2778 lsx = lsx + negcon_deadzone;
2781 // >> Convert to an 'amplitude' [-1.0,1.0] and adjust response
2782 negcon_twist_amplitude = (float)lsx / (float)(NEGCON_RANGE - negcon_deadzone);
2783 if (negcon_linearity == 2)
2785 if (negcon_twist_amplitude < 0.0)
2786 negcon_twist_amplitude = -(negcon_twist_amplitude * negcon_twist_amplitude);
2788 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude;
2790 else if (negcon_linearity == 3)
2791 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude * negcon_twist_amplitude;
2792 // >> Convert to final 'in_analog' integer value [0,255]
2793 in_analog_right[port][0] = MAX(MIN((int)(negcon_twist_amplitude * 128.0f) + 128, 255), 0);
2795 // >> Handle right analog stick vertical axis mapping...
2796 // - Up (-Y) == accelerate == neGcon I
2797 // - Down (+Y) == brake == neGcon II
2800 rsy = input_state_cb(port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
2803 // Account for deadzone
2804 // (Note: have never encountered a gamepad with significant differences
2805 // in deadzone between left/right analog sticks, so use the regular 'twist'
2807 if (rsy > negcon_deadzone)
2808 rsy = rsy - negcon_deadzone;
2811 // Convert to 'in_analog' integer value [0,255]
2812 negcon_ii_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2816 if (rsy < -negcon_deadzone)
2817 rsy = -1 * (rsy + negcon_deadzone);
2820 negcon_i_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2823 in_analog_right[port][1] = MAX(
2825 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_R2),
2826 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_B)),
2829 in_analog_left[port][0] = MAX(
2831 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_L2),
2832 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_Y)),
2835 in_analog_left[port][1] = get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_L);
2838 static void update_input_mouse(int port, int ret)
2840 float raw_x = input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X);
2841 float raw_y = input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y);
2843 int x = (int)roundf(raw_x * mouse_sensitivity);
2844 int y = (int)roundf(raw_y * mouse_sensitivity);
2846 if (x > 127) x = 127;
2847 else if (x < -128) x = -128;
2849 if (y > 127) y = 127;
2850 else if (y < -128) y = -128;
2852 in_mouse[port][0] = x; /* -128..+128 left/right movement, 0 = no movement */
2853 in_mouse[port][1] = y; /* -128..+128 down/up movement, 0 = no movement */
2855 /* left mouse button state */
2856 if (input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_LEFT))
2857 in_keystate[port] |= 1 << 11;
2859 /* right mouse button state */
2860 if (input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_RIGHT))
2861 in_keystate[port] |= 1 << 10;
2864 static void update_input(void)
2866 // reset all keystate, query libretro for keystate
2870 for (i = 0; i < PORTS_NUMBER; i++)
2873 int type = in_type[i];
2877 if (type == PSE_PAD_TYPE_NONE)
2880 if (libretro_supports_bitmasks)
2881 ret = input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
2884 for (j = 0; j < (RETRO_DEVICE_ID_JOYPAD_R3 + 1); j++)
2886 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j))
2893 case PSE_PAD_TYPE_GUNCON:
2894 update_input_guncon(i, ret);
2896 case PSE_PAD_TYPE_GUN:
2897 update_input_justifier(i, ret);
2899 case PSE_PAD_TYPE_NEGCON:
2900 update_input_negcon(i, ret);
2902 case PSE_PAD_TYPE_MOUSE:
2903 update_input_mouse(i, ret);
2906 // Query digital inputs
2907 for (j = 0; j < RETRO_PSX_MAP_LEN; j++)
2909 in_keystate[i] |= retro_psx_map[j];
2911 // Query analog inputs
2912 if (type == PSE_PAD_TYPE_ANALOGJOY || type == PSE_PAD_TYPE_ANALOGPAD)
2914 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);
2915 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);
2916 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);
2917 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);
2923 static void print_internal_fps(void)
2925 if (display_internal_fps)
2929 if (frame_count % INTERNAL_FPS_SAMPLE_PERIOD == 0)
2931 unsigned internal_fps = pl_rearmed_cbs.flip_cnt * (is_pal_mode ? 50 : 60) / INTERNAL_FPS_SAMPLE_PERIOD;
2933 const char *strc = (const char *)str;
2937 snprintf(str, sizeof(str), "Internal FPS: %2d", internal_fps);
2939 pl_rearmed_cbs.flip_cnt = 0;
2941 if (msg_interface_version >= 1)
2943 struct retro_message_ext msg = {
2948 RETRO_MESSAGE_TARGET_OSD,
2949 RETRO_MESSAGE_TYPE_STATUS,
2952 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE_EXT, &msg);
2956 struct retro_message msg = {
2960 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
2968 void retro_run(void)
2970 //SysReset must be run while core is running,Not in menu (Locks up Retroarch)
2980 print_internal_fps();
2982 /* Check whether current frame should
2984 pl_rearmed_cbs.fskip_force = 0;
2985 pl_rearmed_cbs.fskip_dirty = 0;
2987 if (frameskip_type != FRAMESKIP_NONE)
2989 bool skip_frame = false;
2991 switch (frameskip_type)
2993 case FRAMESKIP_AUTO:
2994 skip_frame = retro_audio_buff_active && retro_audio_buff_underrun;
2996 case FRAMESKIP_AUTO_THRESHOLD:
2997 skip_frame = retro_audio_buff_active && (retro_audio_buff_occupancy < frameskip_threshold);
2999 case FRAMESKIP_FIXED_INTERVAL:
3006 if (skip_frame && frameskip_counter < frameskip_interval)
3007 pl_rearmed_cbs.fskip_force = 1;
3010 /* If frameskip/timing settings have changed,
3011 * update frontend audio latency
3012 * > Can do this before or after the frameskip
3013 * check, but doing it after means we at least
3014 * retain the current frame's audio output */
3015 if (update_audio_latency)
3017 environ_cb(RETRO_ENVIRONMENT_SET_MINIMUM_AUDIO_LATENCY,
3018 &retro_audio_latency);
3019 update_audio_latency = false;
3026 bool updated = false;
3027 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
3028 update_variables(true);
3033 if (pl_rearmed_cbs.fskip_dirty == 1) {
3034 if (frameskip_counter < frameskip_interval)
3035 frameskip_counter++;
3036 else if (frameskip_counter >= frameskip_interval || !pl_rearmed_cbs.fskip_force)
3037 frameskip_counter = 0;
3040 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf_ptr : NULL,
3041 vout_width, vout_height, vout_pitch * 2);
3045 static bool try_use_bios(const char *path, bool preferred_only)
3049 FILE *fp = fopen(path, "rb");
3053 fseek(fp, 0, SEEK_END);
3057 name = strrchr(path, SLASH);
3061 if (preferred_only && size != 512 * 1024)
3063 if (size != 512 * 1024 && size != 4 * 1024 * 1024)
3065 if (strstr(name, "unirom"))
3067 // jp bios have an addidional region check
3068 if (preferred_only && (strcasestr(name, "00.") || strcasestr(name, "j.bin")))
3071 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
3076 #include <sys/types.h>
3079 static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
3081 static const char *substr_pref[] = { "scph", "ps" };
3082 static const char *substr_alt[] = { "scph", "ps", "openbios" };
3088 dir = opendir(dirpath);
3092 // try to find a "better" bios
3093 while ((ent = readdir(dir)))
3095 for (i = 0; i < sizeof(substr_pref) / sizeof(substr_pref[0]); i++)
3097 const char *substr = substr_pref[i];
3098 if ((strncasecmp(ent->d_name, substr, strlen(substr)) != 0))
3100 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
3101 ret = try_use_bios(path, true);
3107 // another pass to look for anything fitting, even ps2 bios
3109 while ((ent = readdir(dir)))
3111 for (i = 0; i < sizeof(substr_alt) / sizeof(substr_alt[0]); i++)
3113 const char *substr = substr_alt[i];
3114 if ((strncasecmp(ent->d_name, substr, strlen(substr)) != 0))
3116 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
3117 ret = try_use_bios(path, false);
3129 #define find_any_bios(...) false
3132 static void check_system_specs(void)
3135 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
3138 static int init_memcards(void)
3142 struct retro_variable var = { .key = "pcsx_rearmed_memcard2", .value = NULL };
3143 static const char CARD2_FILE[] = "pcsx-card2.mcd";
3145 // Memcard2 will be handled and is re-enabled if needed using core
3147 // Memcard1 is handled by libretro, doing this will set core to
3148 // skip file io operations for memcard1 like SaveMcd
3149 snprintf(Config.Mcd1, sizeof(Config.Mcd1), "none");
3150 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "none");
3151 init_memcard(Mcd1Data);
3152 // Memcard 2 is managed by the emulator on the filesystem,
3153 // There is no need to initialize Mcd2Data like Mcd1Data.
3155 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
3157 SysPrintf("Memcard 2: %s\n", var.value);
3158 if (memcmp(var.value, "enabled", 7) == 0)
3160 if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir)
3162 if (strlen(dir) + strlen(CARD2_FILE) + 2 > sizeof(Config.Mcd2))
3164 SysPrintf("Path '%s' is too long. Cannot use memcard 2. Use a shorter path.\n", dir);
3170 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "%s/%s", dir, CARD2_FILE);
3171 SysPrintf("Use memcard 2: %s\n", Config.Mcd2);
3176 SysPrintf("Could not get save directory! Could not create memcard 2.");
3184 static void loadPSXBios(void)
3187 char path[PATH_MAX];
3188 unsigned useHLE = 0;
3190 const char *bios[] = {
3191 "PSXONPSP660", "psxonpsp660",
3192 "SCPH101", "scph101",
3193 "SCPH5501", "scph5501",
3194 "SCPH7001", "scph7001",
3195 "SCPH1001", "scph1001"
3198 struct retro_variable var = {
3199 .key = "pcsx_rearmed_bios",
3205 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
3207 if (!strcmp(var.value, "HLE"))
3213 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
3216 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s", dir);
3218 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++)
3220 snprintf(path, sizeof(path), "%s%c%s.bin", dir, SLASH, bios[i]);
3221 found_bios = try_use_bios(path, true);
3227 found_bios = find_any_bios(dir, path, sizeof(path));
3231 SysPrintf("found BIOS file: %s\n", Config.Bios);
3237 const char *msg_str;
3241 msg_str = "BIOS set to \'hle\'";
3242 SysPrintf("Using HLE BIOS.\n");
3243 // shorter as the user probably intentionally wants to use HLE
3248 msg_str = "No PlayStation BIOS file found - add for better compatibility";
3249 SysPrintf("No BIOS files found.\n");
3252 show_notification(msg_str, duration, 2);
3256 void retro_init(void)
3258 unsigned dci_version = 0;
3259 struct retro_rumble_interface rumble;
3262 msg_interface_version = 0;
3263 environ_cb(RETRO_ENVIRONMENT_GET_MESSAGE_INTERFACE_VERSION, &msg_interface_version);
3265 #if defined(__MACH__) && !defined(TVOS)
3266 // magic sauce to make the dynarec work on iOS
3267 syscall(SYS_ptrace, 0 /*PTRACE_TRACEME*/, 0, 0, 0);
3271 psxMapHook = pl_3ds_mmap;
3272 psxUnmapHook = pl_3ds_munmap;
3275 if (init_vita_mmap() < 0)
3277 psxMapHook = pl_vita_mmap;
3278 psxUnmapHook = pl_vita_munmap;
3280 ret = emu_core_preinit();
3282 /* emu_core_preinit sets the cpu to dynarec */
3284 Config.Cpu = CPU_INTERPRETER;
3286 ret |= init_memcards();
3288 ret |= emu_core_init();
3291 SysPrintf("PCSX init failed.\n");
3296 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
3297 #elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && P_HAVE_POSIX_MEMALIGN
3298 if (posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2) != 0)
3299 vout_buf = (void *) 0;
3301 memset(vout_buf, 0, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
3303 vout_buf = calloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT, 2);
3306 vout_buf_ptr = vout_buf;
3310 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
3312 disk_initial_index = 0;
3313 disk_initial_path[0] = '\0';
3314 if (environ_cb(RETRO_ENVIRONMENT_GET_DISK_CONTROL_INTERFACE_VERSION, &dci_version) && (dci_version >= 1))
3315 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_EXT_INTERFACE, &disk_control_ext);
3317 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
3320 if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble))
3321 rumble_cb = rumble.set_rumble_state;
3323 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
3324 * we have to do this because cache misses and some IO penalties
3325 * are not emulated. Warning: changing this may break compatibility. */
3326 Config.cycle_multiplier = CYCLE_MULT_DEFAULT;
3327 #if defined(HAVE_PRE_ARMV7) && !defined(_3DS)
3328 Config.cycle_multiplier = 200;
3330 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
3331 pl_rearmed_cbs.gpu_peops.dwActFixes = GPU_PEOPS_OLD_FRAME_SKIP;
3333 SaveFuncs.open = save_open;
3334 SaveFuncs.read = save_read;
3335 SaveFuncs.write = save_write;
3336 SaveFuncs.seek = save_seek;
3337 SaveFuncs.close = save_close;
3339 if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL))
3340 libretro_supports_bitmasks = true;
3342 check_system_specs();
3345 void retro_deinit(void)
3354 linearFree(vout_buf);
3363 libretro_supports_bitmasks = false;
3364 libretro_supports_option_categories = false;
3366 show_input_settings = true;
3368 show_advanced_gpu_peops_settings = true;
3371 show_advanced_gpu_unai_settings = true;
3374 /* Have to reset disks struct, otherwise
3375 * fnames/flabels will leak memory */
3377 frameskip_type = FRAMESKIP_NONE;
3378 frameskip_threshold = 0;
3379 frameskip_interval = 0;
3380 frameskip_counter = 0;
3381 retro_audio_buff_active = false;
3382 retro_audio_buff_occupancy = 0;
3383 retro_audio_buff_underrun = false;
3384 retro_audio_latency = 0;
3385 update_audio_latency = false;
3389 #include <psp2/kernel/threadmgr.h>
3390 int usleep(unsigned long us)
3392 sceKernelDelayThread(us);
3396 void SysPrintf(const char *fmt, ...)
3401 va_start(list, fmt);
3402 vsprintf(msg, fmt, list);
3406 log_cb(RETRO_LOG_INFO, "%s", msg);
3409 /* Prints debug-level logs */
3410 void SysDLog(const char *fmt, ...)
3415 va_start(list, fmt);
3416 vsprintf(msg, fmt, list);
3420 log_cb(RETRO_LOG_DEBUG, "%s", msg);
3423 // vim:sw=3:ts=3:expandtab