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 "../plugins/dfsound/out.h"
32 #include "../plugins/dfsound/spu_config.h"
37 #include "plugin_lib.h"
38 #include "arm_features.h"
42 #include "libretro_core_options.h"
44 #ifdef USE_LIBRETRO_VFS
45 #include <streams/file_stream_transforms.h>
49 #include "3ds/3ds_utils.h"
52 #define PORTS_NUMBER 8
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
59 #define MAX(a, b) ((a) > (b) ? (a) : (b))
62 #define ISHEXDEC ((buf[cursor] >= '0') && (buf[cursor] <= '9')) || ((buf[cursor] >= 'a') && (buf[cursor] <= 'f')) || ((buf[cursor] >= 'A') && (buf[cursor] <= 'F'))
64 #define INTERNAL_FPS_SAMPLE_PERIOD 64
66 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
67 static int rebootemu = 0;
69 static retro_video_refresh_t video_cb;
70 static retro_input_poll_t input_poll_cb;
71 static retro_input_state_t input_state_cb;
72 static retro_environment_t environ_cb;
73 static retro_audio_sample_batch_t audio_batch_cb;
74 static retro_set_rumble_state_t rumble_cb;
75 static struct retro_log_callback logging;
76 static retro_log_printf_t log_cb;
78 static unsigned msg_interface_version = 0;
80 static void *vout_buf;
81 static void *vout_buf_ptr;
82 static int vout_width, vout_height;
83 static int vout_fb_dirty;
84 static int psx_w, psx_h;
85 static bool vout_can_dupe;
86 static bool duping_enable;
87 static bool found_bios;
88 static bool display_internal_fps = false;
89 static unsigned frame_count = 0;
90 static bool libretro_supports_bitmasks = false;
91 static bool libretro_supports_option_categories = false;
92 static bool show_input_settings = true;
94 static bool show_advanced_gpu_peops_settings = true;
97 static bool show_advanced_gpu_unai_settings = true;
99 static float mouse_sensitivity = 1.0f;
105 FRAMESKIP_AUTO_THRESHOLD,
106 FRAMESKIP_FIXED_INTERVAL
109 static unsigned frameskip_type = FRAMESKIP_NONE;
110 static unsigned frameskip_threshold = 0;
111 static unsigned frameskip_interval = 0;
112 static unsigned frameskip_counter = 0;
114 static int retro_audio_buff_active = false;
115 static unsigned retro_audio_buff_occupancy = 0;
116 static int retro_audio_buff_underrun = false;
118 static unsigned retro_audio_latency = 0;
119 static int update_audio_latency = false;
121 static unsigned previous_width = 0;
122 static unsigned previous_height = 0;
124 static int plugins_opened;
125 static int is_pal_mode;
127 /* memory card data */
128 extern char Mcd1Data[MCD_SIZE];
129 extern char Mcd2Data[MCD_SIZE];
130 extern char McdDisable[2];
132 /* PCSX ReARMed core calls and stuff */
134 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
135 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
136 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
137 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE
139 int in_analog_left[8][2] = { { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 } };
140 int in_analog_right[8][2] = { { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 } };
141 unsigned short in_keystate[PORTS_NUMBER];
145 int in_enable_vibration = 1;
146 int in_enable_crosshair[2] = { 0, 0 };
148 // NegCon adjustment parameters
149 // > The NegCon 'twist' action is somewhat awkward when mapped
150 // to a standard analog stick -> user should be able to tweak
151 // response/deadzone for comfort
152 // > When response is linear, 'additional' deadzone (set here)
153 // may be left at zero, since this is normally handled via in-game
155 // > When response is non-linear, deadzone should be set to match the
156 // controller being used (otherwise precision may be lost)
157 // > negcon_linearity:
158 // - 1: Response is linear - recommended when using racing wheel
159 // peripherals, not recommended for standard gamepads
160 // - 2: Response is quadratic - optimal setting for gamepads
161 // - 3: Response is cubic - enables precise fine control, but
162 // difficult to use...
163 #define NEGCON_RANGE 0x7FFF
164 static int negcon_deadzone = 0;
165 static int negcon_linearity = 1;
167 static bool axis_bounds_modifier;
169 /* PSX max resolution is 640x512, but with enhancement it's 1024x512 */
170 #define VOUT_MAX_WIDTH 1024
171 #define VOUT_MAX_HEIGHT 512
174 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info) { return false; }
175 void retro_unload_game(void) {}
176 static int vout_open(void) { return 0; }
177 static void vout_close(void) {}
178 static int snd_init(void) { return 0; }
179 static void snd_finish(void) {}
180 static int snd_busy(void) { return 0; }
182 #define GPU_PEOPS_ODD_EVEN_BIT (1 << 0)
183 #define GPU_PEOPS_EXPAND_SCREEN_WIDTH (1 << 1)
184 #define GPU_PEOPS_IGNORE_BRIGHTNESS (1 << 2)
185 #define GPU_PEOPS_DISABLE_COORD_CHECK (1 << 3)
186 #define GPU_PEOPS_LAZY_SCREEN_UPDATE (1 << 6)
187 #define GPU_PEOPS_OLD_FRAME_SKIP (1 << 7)
188 #define GPU_PEOPS_REPEATED_TRIANGLES (1 << 8)
189 #define GPU_PEOPS_QUADS_WITH_TRIANGLES (1 << 9)
190 #define GPU_PEOPS_FAKE_BUSY_STATE (1 << 10)
192 static void init_memcard(char *mcd_data)
197 memset(mcd_data, 0, MCD_SIZE);
199 mcd_data[off++] = 'M';
200 mcd_data[off++] = 'C';
202 mcd_data[off++] = 0x0e;
204 for (i = 0; i < 15; i++)
206 mcd_data[off++] = 0xa0;
208 mcd_data[off++] = 0xff;
209 mcd_data[off++] = 0xff;
211 mcd_data[off++] = 0xa0;
214 for (i = 0; i < 20; i++)
216 mcd_data[off++] = 0xff;
217 mcd_data[off++] = 0xff;
218 mcd_data[off++] = 0xff;
219 mcd_data[off++] = 0xff;
221 mcd_data[off++] = 0xff;
222 mcd_data[off++] = 0xff;
227 static void set_vout_fb()
229 struct retro_framebuffer fb = { 0 };
231 fb.width = vout_width;
232 fb.height = vout_height;
233 fb.access_flags = RETRO_MEMORY_ACCESS_WRITE;
235 if (environ_cb(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &fb) && fb.format == RETRO_PIXEL_FORMAT_RGB565)
236 vout_buf_ptr = (uint16_t *)fb.data;
238 vout_buf_ptr = vout_buf;
241 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
248 if (previous_width != vout_width || previous_height != vout_height)
250 previous_width = vout_width;
251 previous_height = vout_height;
253 struct retro_system_av_info info;
254 retro_get_system_av_info(&info);
255 environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &info.geometry);
261 #ifndef FRONTEND_SUPPORTS_RGB565
262 static void convert(void *buf, size_t bytes)
264 unsigned int i, v, *p = buf;
266 for (i = 0; i < bytes / 4; i++)
269 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
274 // Function to add crosshairs
275 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) {
276 for (port = 0; port < 2; port++) {
277 // Draw the horizontal line of the crosshair
278 for (int i = pos_y - thickness / 2; i <= pos_y + thickness / 2; i++) {
279 for (int j = pos_x - size_x / 2; j <= pos_x + size_x / 2; j++) {
280 if ((i + vout_height) >= 0 && (i + vout_height) < bufferStride && j >= 0 && j < bufferStride && in_enable_crosshair[port] > 0)
281 buffer[i * bufferStride + j] = crosshair_color;
285 // Draw the vertical line of the crosshair
286 for (int i = pos_x - thickness / 2; i <= pos_x + thickness / 2; i++) {
287 for (int j = pos_y - size_y / 2; j <= pos_y + size_y / 2; j++) {
288 if (i >= 0 && i < bufferStride && (j + vout_height) >= 0 && (j + vout_height) < bufferStride && in_enable_crosshair[port] > 0)
289 buffer[j * bufferStride + i] = crosshair_color;
295 struct CrosshairInfo {
296 int pos_x, pos_y, thickness, size_x, size_y;
299 // Calculate size and position of crosshairs
300 static void CrosshairDimensions(int port, struct CrosshairInfo *info) {
301 int gunx = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X);
302 int guny = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y);
303 if (gunx == 32767) // Prevent crosshairs from wrapping around right side of screen to left
304 info->pos_x = (gunx + 32767.0f) * vout_width / 65534.0f - 0.5f;
306 info->pos_x = (gunx + 32767.0f) * vout_width / 65534.0f;
307 info->pos_y = (guny + 32767.0f) * vout_height / 65534.0f - vout_height;
308 info->thickness = pl_rearmed_cbs.gpu_neon.enhancement_enable ? 4 : 2;
309 info->size_x = psx_w * (pl_rearmed_cbs.gpu_neon.enhancement_enable ? 2 : 1) / 40.0f;
310 info->size_y = psx_h * (pl_rearmed_cbs.gpu_neon.enhancement_enable ? 2 : 1) * (4.0f / 3.0f) / 40.0f;
313 static void vout_flip(const void *vram, int stride, int bgr24,
314 int x, int y, int w, int h, int dims_changed)
316 unsigned short *dest = vout_buf_ptr;
317 const unsigned short *src = vram;
318 int dstride = vout_width, h1 = h;
321 if (vram == NULL || dims_changed || (in_enable_crosshair[0] + in_enable_crosshair[1]) > 0)
323 memset(vout_buf_ptr, 0, dstride * vout_height * 2);
329 dest += x + y * dstride;
333 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
334 for (; h1-- > 0; dest += dstride, src += stride)
336 bgr888_to_rgb565(dest, src, w * 3);
341 for (; h1-- > 0; dest += dstride, src += stride)
343 bgr555_to_rgb565(dest, src, w * 2);
347 for (port = 0; port < 2; port++) {
348 if (in_enable_crosshair[port] > 0 && (in_type[port] == PSE_PAD_TYPE_GUNCON || in_type[port] == PSE_PAD_TYPE_GUN))
350 struct CrosshairInfo crosshairInfo;
351 CrosshairDimensions(port, &crosshairInfo);
352 addCrosshair(port, in_enable_crosshair[port], dest, dstride, crosshairInfo.pos_x, crosshairInfo.pos_y, crosshairInfo.thickness, crosshairInfo.size_x, crosshairInfo.size_y);
357 #ifndef FRONTEND_SUPPORTS_RGB565
358 convert(vout_buf_ptr, vout_width * vout_height * 2);
361 pl_rearmed_cbs.flip_cnt++;
373 psx_map_t custom_psx_maps[] = {
374 { NULL, 0x13000000, 0x210000, MAP_TAG_RAM }, // 0x80000000
375 { NULL, 0x12800000, 0x010000, MAP_TAG_OTHER }, // 0x1f800000
376 { NULL, 0x12c00000, 0x080000, MAP_TAG_OTHER }, // 0x1fc00000
377 { NULL, 0x11000000, 0x800000, MAP_TAG_LUTS }, // 0x08000000
378 { NULL, 0x12000000, 0x200000, MAP_TAG_VRAM }, // 0x00000000
381 void *pl_3ds_mmap(unsigned long addr, size_t size, int is_fixed,
389 psx_map_t *custom_map = custom_psx_maps;
391 for (; custom_map->size; custom_map++)
393 if ((custom_map->size == size) && (custom_map->tag == tag))
395 uint32_t ptr_aligned, tmp;
397 custom_map->buffer = malloc(size + 0x1000);
398 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
400 if (svcControlMemory(&tmp, (void *)custom_map->target_map, (void *)ptr_aligned, size, MEMOP_MAP, 0x3) < 0)
402 SysPrintf("could not map memory @0x%08X\n", custom_map->target_map);
406 return (void *)custom_map->target_map;
414 void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
420 psx_map_t *custom_map = custom_psx_maps;
422 for (; custom_map->size; custom_map++)
424 if ((custom_map->target_map == (uint32_t)ptr))
426 uint32_t ptr_aligned, tmp;
428 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
430 svcControlMemory(&tmp, (void *)custom_map->target_map, (void *)ptr_aligned, size, MEMOP_UNMAP, 0x3);
432 free(custom_map->buffer);
433 custom_map->buffer = NULL;
454 psx_map_t custom_psx_maps[] = {
455 { NULL, NULL, 0x210000, MAP_TAG_RAM }, // 0x80000000
456 { NULL, NULL, 0x010000, MAP_TAG_OTHER }, // 0x1f800000
457 { NULL, NULL, 0x080000, MAP_TAG_OTHER }, // 0x1fc00000
458 { NULL, NULL, 0x800000, MAP_TAG_LUTS }, // 0x08000000
459 { NULL, NULL, 0x200000, MAP_TAG_VRAM }, // 0x00000000
466 addr = malloc(64 * 1024 * 1024);
469 tmpaddr = ((u32)(addr + 0xFFFFFF)) & ~0xFFFFFF;
470 custom_psx_maps[0].buffer = tmpaddr + 0x2000000;
471 custom_psx_maps[1].buffer = tmpaddr + 0x1800000;
472 custom_psx_maps[2].buffer = tmpaddr + 0x1c00000;
473 custom_psx_maps[3].buffer = tmpaddr + 0x0000000;
474 custom_psx_maps[4].buffer = tmpaddr + 0x1000000;
476 for(n = 0; n < 5; n++){
477 sceClibPrintf("addr reserved %x\n",custom_psx_maps[n].buffer);
483 void deinit_vita_mmap()
488 void *pl_vita_mmap(unsigned long addr, size_t size, int is_fixed,
494 psx_map_t *custom_map = custom_psx_maps;
496 for (; custom_map->size; custom_map++)
498 if ((custom_map->size == size) && (custom_map->tag == tag))
500 return custom_map->buffer;
507 void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag)
511 psx_map_t *custom_map = custom_psx_maps;
513 for (; custom_map->size; custom_map++)
515 if ((custom_map->buffer == ptr))
525 static void *pl_mmap(unsigned int size)
527 return psxMap(0, size, 0, MAP_TAG_VRAM);
530 static void pl_munmap(void *ptr, unsigned int size)
532 psxUnmap(ptr, size, MAP_TAG_VRAM);
535 struct rearmed_cbs pl_rearmed_cbs = {
536 .pl_vout_open = vout_open,
537 .pl_vout_set_mode = vout_set_mode,
538 .pl_vout_flip = vout_flip,
539 .pl_vout_close = vout_close,
542 .gpu_state_change = gpu_state_change,
543 /* from psxcounters */
544 .gpu_hcnt = &hSyncCount,
545 .gpu_frame_count = &frame_counter,
548 void pl_frame_limit(void)
550 /* called once per frame, make psxCpu->Execute() above return */
554 void pl_timing_prepare(int is_pal)
556 is_pal_mode = is_pal;
559 void plat_trigger_vibrate(int pad, int low, int high)
564 if (in_enable_vibration)
566 rumble_cb(pad, RETRO_RUMBLE_STRONG, high << 8);
567 rumble_cb(pad, RETRO_RUMBLE_WEAK, low ? 0xffff : 0x0);
571 //Percentage distance of screen to adjust for Konami Gun
572 static float KonamiGunAdjustX = 0;
573 static float KonamiGunAdjustY = 0;
575 void pl_gun_byte2(int port, unsigned char byte)
578 float justifier_multiplier = 0;
579 int justifier_width = psx_w;
580 int justifier_height = psx_h;
581 int justifier_offscreen = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_IS_OFFSCREEN);
582 int justifier_reload = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_RELOAD);
584 if (justifier_width == 256)
585 justifier_multiplier = is_pal_mode ? .157086f : .158532f;
586 else if (justifier_width == 320)
587 justifier_multiplier = is_pal_mode ? .196358f : .198166f;
588 else if (justifier_width == 384)
589 justifier_multiplier = is_pal_mode ? .224409f : .226475f;
590 else if (justifier_width == 512)
591 justifier_multiplier = is_pal_mode ? .314173f : .317065f;
592 else // (justifier_width == 640)
593 justifier_multiplier = is_pal_mode ? .392717f : .396332f;
595 int gunx = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X);
596 int guny = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y);
598 //Default offset of +105 for X and -12 for Y is chosen to obtain alignment in Die Hard Trilogy, which has no calibration feature
599 int gunx_scaled = ((gunx + 32767.0f) / 65534.0f + KonamiGunAdjustX) * justifier_width / justifier_multiplier + 105.0f;
600 int guny_scaled = ((guny + 32767.0f) / 65534.0f + KonamiGunAdjustY) * justifier_height - 12.0f;
602 if ((byte & 0x10) && !justifier_offscreen && !justifier_reload)
604 psxScheduleIrq10(irq_count, gunx_scaled, guny_scaled);
609 static void snd_feed(void *buf, int bytes)
611 if (audio_batch_cb != NULL)
612 audio_batch_cb(buf, bytes / 4);
615 void out_register_libretro(struct out_driver *drv)
617 drv->name = "libretro";
618 drv->init = snd_init;
619 drv->finish = snd_finish;
620 drv->busy = snd_busy;
621 drv->feed = snd_feed;
624 #define RETRO_DEVICE_PSE_STANDARD RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0)
625 #define RETRO_DEVICE_PSE_ANALOG RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 0)
626 #define RETRO_DEVICE_PSE_DUALSHOCK RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 1)
627 #define RETRO_DEVICE_PSE_NEGCON RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 2)
628 #define RETRO_DEVICE_PSE_GUNCON RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_LIGHTGUN, 0)
629 #define RETRO_DEVICE_PSE_JUSTIFIER RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_LIGHTGUN, 1)
630 #define RETRO_DEVICE_PSE_MOUSE RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_MOUSE, 0)
632 static char *get_pse_pad_label[] = {
633 "none", "mouse", "negcon", "konami gun", "standard", "analog", "guncon", "dualshock"
636 static const struct retro_controller_description pads[8] =
638 { "standard", RETRO_DEVICE_JOYPAD },
639 { "analog", RETRO_DEVICE_PSE_ANALOG },
640 { "dualshock", RETRO_DEVICE_PSE_DUALSHOCK },
641 { "negcon", RETRO_DEVICE_PSE_NEGCON },
642 { "guncon", RETRO_DEVICE_PSE_GUNCON },
643 { "konami gun", RETRO_DEVICE_PSE_JUSTIFIER },
644 { "mouse", RETRO_DEVICE_PSE_MOUSE },
648 static const struct retro_controller_info ports[9] =
663 static bool update_option_visibility(void)
665 struct retro_variable var = {0};
666 struct retro_core_option_display option_display = {0};
667 bool updated = false;
670 /* If frontend supports core option categories
671 * then show/hide core option entries are ignored
672 * and no options should be hidden */
673 if (libretro_supports_option_categories)
676 var.key = "pcsx_rearmed_show_input_settings";
679 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
681 bool show_input_settings_prev =
684 show_input_settings = true;
685 if (strcmp(var.value, "disabled") == 0)
686 show_input_settings = false;
688 if (show_input_settings !=
689 show_input_settings_prev)
691 char input_option[][50] = {
692 "pcsx_rearmed_analog_axis_modifier",
693 "pcsx_rearmed_vibration",
694 "pcsx_rearmed_multitap",
695 "pcsx_rearmed_negcon_deadzone",
696 "pcsx_rearmed_negcon_response",
697 "pcsx_rearmed_input_sensitivity",
698 "pcsx_rearmed_crosshair1",
699 "pcsx_rearmed_crosshair2",
700 "pcsx_rearmed_konamigunadjustx",
701 "pcsx_rearmed_konamigunadjusty",
702 "pcsx_rearmed_gunconadjustx",
703 "pcsx_rearmed_gunconadjusty",
704 "pcsx_rearmed_gunconadjustratiox",
705 "pcsx_rearmed_gunconadjustratioy"
708 option_display.visible = show_input_settings;
711 i < (sizeof(input_option) /
712 sizeof(input_option[0]));
715 option_display.key = input_option[i];
716 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
724 var.key = "pcsx_rearmed_show_gpu_peops_settings";
727 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
729 bool show_advanced_gpu_peops_settings_prev =
730 show_advanced_gpu_peops_settings;
732 show_advanced_gpu_peops_settings = true;
733 if (strcmp(var.value, "disabled") == 0)
734 show_advanced_gpu_peops_settings = false;
736 if (show_advanced_gpu_peops_settings !=
737 show_advanced_gpu_peops_settings_prev)
740 struct retro_core_option_display option_display;
741 char gpu_peops_option[][45] = {
742 "pcsx_rearmed_gpu_peops_odd_even_bit",
743 "pcsx_rearmed_gpu_peops_expand_screen_width",
744 "pcsx_rearmed_gpu_peops_ignore_brightness",
745 "pcsx_rearmed_gpu_peops_disable_coord_check",
746 "pcsx_rearmed_gpu_peops_lazy_screen_update",
747 "pcsx_rearmed_gpu_peops_repeated_triangles",
748 "pcsx_rearmed_gpu_peops_quads_with_triangles",
749 "pcsx_rearmed_gpu_peops_fake_busy_state"
752 option_display.visible = show_advanced_gpu_peops_settings;
755 i < (sizeof(gpu_peops_option) /
756 sizeof(gpu_peops_option[0]));
759 option_display.key = gpu_peops_option[i];
760 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
769 var.key = "pcsx_rearmed_show_gpu_unai_settings";
772 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
774 bool show_advanced_gpu_unai_settings_prev =
775 show_advanced_gpu_unai_settings;
777 show_advanced_gpu_unai_settings = true;
778 if (strcmp(var.value, "disabled") == 0)
779 show_advanced_gpu_unai_settings = false;
781 if (show_advanced_gpu_unai_settings !=
782 show_advanced_gpu_unai_settings_prev)
785 struct retro_core_option_display option_display;
786 char gpu_unai_option[][40] = {
787 "pcsx_rearmed_gpu_unai_blending",
788 "pcsx_rearmed_gpu_unai_lighting",
789 "pcsx_rearmed_gpu_unai_fast_lighting",
790 "pcsx_rearmed_gpu_unai_scale_hires",
793 option_display.visible = show_advanced_gpu_unai_settings;
796 i < (sizeof(gpu_unai_option) /
797 sizeof(gpu_unai_option[0]));
800 option_display.key = gpu_unai_option[i];
801 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
812 void retro_set_environment(retro_environment_t cb)
814 bool option_categories = false;
815 #ifdef USE_LIBRETRO_VFS
816 struct retro_vfs_interface_info vfs_iface_info;
821 if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
822 log_cb = logging.log;
824 environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
827 * An annoyance: retro_set_environment() can be called
828 * multiple times, and depending upon the current frontend
829 * state various environment callbacks may be disabled.
830 * This means the reported 'categories_supported' status
831 * may change on subsequent iterations. We therefore have
832 * to record whether 'categories_supported' is true on any
833 * iteration, and latch the result */
834 libretro_set_core_options(environ_cb, &option_categories);
835 libretro_supports_option_categories |= option_categories;
837 /* If frontend supports core option categories,
838 * any show/hide core option entries are unused
839 * and should be hidden */
840 if (libretro_supports_option_categories)
842 struct retro_core_option_display option_display;
843 option_display.visible = false;
845 option_display.key = "pcsx_rearmed_show_input_settings";
846 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
850 option_display.key = "pcsx_rearmed_show_gpu_peops_settings";
851 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
855 option_display.key = "pcsx_rearmed_show_gpu_unai_settings";
856 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
860 /* If frontend does not support core option
861 * categories, core options may be shown/hidden
862 * at runtime. In this case, register 'update
863 * display' callback, so frontend can update
864 * core options menu without calling retro_run() */
867 struct retro_core_options_update_display_callback update_display_cb;
868 update_display_cb.callback = update_option_visibility;
870 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_UPDATE_DISPLAY_CALLBACK,
874 #ifdef USE_LIBRETRO_VFS
875 vfs_iface_info.required_interface_version = 1;
876 vfs_iface_info.iface = NULL;
877 if (environ_cb(RETRO_ENVIRONMENT_GET_VFS_INTERFACE, &vfs_iface_info))
878 filestream_vfs_init(&vfs_iface_info);
882 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
883 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
884 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
885 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
886 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
888 unsigned retro_api_version(void)
890 return RETRO_API_VERSION;
893 static void update_multitap(void)
895 struct retro_variable var = { 0 };
901 var.key = "pcsx_rearmed_multitap";
902 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value))
904 if (strcmp(var.value, "port 1") == 0)
906 else if (strcmp(var.value, "port 2") == 0)
908 else if (strcmp(var.value, "ports 1 and 2") == 0)
916 void retro_set_controller_port_device(unsigned port, unsigned device)
918 if (port >= PORTS_NUMBER)
923 case RETRO_DEVICE_JOYPAD:
924 case RETRO_DEVICE_PSE_STANDARD:
925 in_type[port] = PSE_PAD_TYPE_STANDARD;
927 case RETRO_DEVICE_PSE_ANALOG:
928 in_type[port] = PSE_PAD_TYPE_ANALOGJOY;
930 case RETRO_DEVICE_PSE_DUALSHOCK:
931 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
933 case RETRO_DEVICE_PSE_MOUSE:
934 in_type[port] = PSE_PAD_TYPE_MOUSE;
936 case RETRO_DEVICE_PSE_NEGCON:
937 in_type[port] = PSE_PAD_TYPE_NEGCON;
939 case RETRO_DEVICE_PSE_GUNCON:
940 in_type[port] = PSE_PAD_TYPE_GUNCON;
942 case RETRO_DEVICE_PSE_JUSTIFIER:
943 in_type[port] = PSE_PAD_TYPE_GUN;
945 case RETRO_DEVICE_NONE:
947 in_type[port] = PSE_PAD_TYPE_NONE;
951 SysPrintf("port: %u device: %s\n", port + 1, get_pse_pad_label[in_type[port]]);
954 void retro_get_system_info(struct retro_system_info *info)
957 #define GIT_VERSION ""
959 memset(info, 0, sizeof(*info));
960 info->library_name = "PCSX-ReARMed";
961 info->library_version = "r23l" GIT_VERSION;
962 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u|chd|iso|exe";
963 info->need_fullpath = true;
966 void retro_get_system_av_info(struct retro_system_av_info *info)
968 unsigned geom_height = vout_height > 0 ? vout_height : 240;
969 unsigned geom_width = vout_width > 0 ? vout_width : 320;
971 memset(info, 0, sizeof(*info));
972 info->timing.fps = is_pal_mode ? 50.0 : 60.0;
973 info->timing.sample_rate = 44100.0;
974 info->geometry.base_width = geom_width;
975 info->geometry.base_height = geom_height;
976 info->geometry.max_width = VOUT_MAX_WIDTH;
977 info->geometry.max_height = VOUT_MAX_HEIGHT;
978 info->geometry.aspect_ratio = 4.0 / 3.0;
982 size_t retro_serialize_size(void)
984 // it's currently 4380651-4397047 bytes,
985 // but have some reserved for future
996 static void *save_open(const char *name, const char *mode)
1000 if (name == NULL || mode == NULL)
1003 fp = malloc(sizeof(*fp));
1007 fp->buf = (char *)name;
1009 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
1014 static int save_read(void *file, void *buf, u32 len)
1016 struct save_fp *fp = file;
1017 if (fp == NULL || buf == NULL)
1020 memcpy(buf, fp->buf + fp->pos, len);
1025 static int save_write(void *file, const void *buf, u32 len)
1027 struct save_fp *fp = file;
1028 if (fp == NULL || buf == NULL)
1031 memcpy(fp->buf + fp->pos, buf, len);
1036 static long save_seek(void *file, long offs, int whence)
1038 struct save_fp *fp = file;
1055 static void save_close(void *file)
1057 struct save_fp *fp = file;
1058 size_t r_size = retro_serialize_size();
1062 if (fp->pos > r_size)
1063 SysPrintf("ERROR: save buffer overflow detected\n");
1064 else if (fp->is_write && fp->pos < r_size)
1065 // make sure we don't save trash in leftover space
1066 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
1070 bool retro_serialize(void *data, size_t size)
1072 int ret = SaveState(data);
1073 return ret == 0 ? true : false;
1076 bool retro_unserialize(const void *data, size_t size)
1078 int ret = LoadState(data);
1079 return ret == 0 ? true : false;
1083 void retro_cheat_reset(void)
1088 void retro_cheat_set(unsigned index, bool enabled, const char *code)
1093 // cheat funcs are destructive, need a copy...
1098 //Prepare buffered cheat for PCSX's AddCheat fucntion.
1105 if (++nonhexdec % 2)
1117 if (index < NumCheats)
1118 ret = EditCheat(index, "", buf);
1120 ret = AddCheat("", buf);
1124 SysPrintf("Failed to set cheat %#u\n", index);
1125 else if (index < NumCheats)
1126 Cheats[index].Enabled = enabled;
1130 // just in case, maybe a win-rt port in the future?
1138 #define PATH_MAX 4096
1141 /* multidisk support */
1142 static unsigned int disk_initial_index;
1143 static char disk_initial_path[PATH_MAX];
1144 static bool disk_ejected;
1145 static unsigned int disk_current_index;
1146 static unsigned int disk_count;
1147 static struct disks_state
1151 int internal_index; // for multidisk eboots
1154 static void get_disk_label(char *disk_label, const char *disk_path, size_t len)
1156 const char *base = NULL;
1158 if (!disk_path || (*disk_path == '\0'))
1161 base = strrchr(disk_path, SLASH);
1168 strncpy(disk_label, base, len - 1);
1169 disk_label[len - 1] = '\0';
1171 char *ext = strrchr(disk_label, '.');
1176 static void disk_init(void)
1180 disk_ejected = false;
1181 disk_current_index = 0;
1184 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++)
1186 if (disks[i].fname != NULL)
1188 free(disks[i].fname);
1189 disks[i].fname = NULL;
1191 if (disks[i].flabel != NULL)
1193 free(disks[i].flabel);
1194 disks[i].flabel = NULL;
1196 disks[i].internal_index = 0;
1200 static bool disk_set_eject_state(bool ejected)
1203 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
1206 disk_ejected = ejected;
1210 static bool disk_get_eject_state(void)
1212 /* can't be controlled by emulated software */
1213 return disk_ejected;
1216 static unsigned int disk_get_image_index(void)
1218 return disk_current_index;
1221 static bool disk_set_image_index(unsigned int index)
1223 if (index >= sizeof(disks) / sizeof(disks[0]))
1227 CdromLabel[0] = '\0';
1229 if (disks[index].fname == NULL)
1231 SysPrintf("missing disk #%u\n", index);
1234 // RetroArch specifies "no disk" with index == count,
1235 // so don't fail here..
1236 disk_current_index = index;
1240 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
1241 disks[index].fname, disks[index].internal_index);
1243 cdrIsoMultidiskSelect = disks[index].internal_index;
1244 set_cd_image(disks[index].fname);
1245 if (ReloadCdromPlugin() < 0)
1247 SysPrintf("failed to load cdr plugin\n");
1252 SysPrintf("failed to open cdr plugin\n");
1258 SetCdOpenCaseTime(time(NULL) + 2);
1262 disk_current_index = index;
1266 static unsigned int disk_get_num_images(void)
1271 static bool disk_replace_image_index(unsigned index,
1272 const struct retro_game_info *info)
1274 char *old_fname = NULL;
1275 char *old_flabel = NULL;
1278 if (index >= sizeof(disks) / sizeof(disks[0]))
1281 old_fname = disks[index].fname;
1282 old_flabel = disks[index].flabel;
1284 disks[index].fname = NULL;
1285 disks[index].flabel = NULL;
1286 disks[index].internal_index = 0;
1290 char disk_label[PATH_MAX];
1291 disk_label[0] = '\0';
1293 disks[index].fname = strdup(info->path);
1295 get_disk_label(disk_label, info->path, PATH_MAX);
1296 disks[index].flabel = strdup(disk_label);
1298 if (index == disk_current_index)
1299 ret = disk_set_image_index(index);
1302 if (old_fname != NULL)
1305 if (old_flabel != NULL)
1311 static bool disk_add_image_index(void)
1313 if (disk_count >= 8)
1320 static bool disk_set_initial_image(unsigned index, const char *path)
1322 if (index >= sizeof(disks) / sizeof(disks[0]))
1325 if (!path || (*path == '\0'))
1328 disk_initial_index = index;
1330 strncpy(disk_initial_path, path, sizeof(disk_initial_path) - 1);
1331 disk_initial_path[sizeof(disk_initial_path) - 1] = '\0';
1336 static bool disk_get_image_path(unsigned index, char *path, size_t len)
1338 const char *fname = NULL;
1343 if (index >= sizeof(disks) / sizeof(disks[0]))
1346 fname = disks[index].fname;
1348 if (!fname || (*fname == '\0'))
1351 strncpy(path, fname, len - 1);
1352 path[len - 1] = '\0';
1357 static bool disk_get_image_label(unsigned index, char *label, size_t len)
1359 const char *flabel = NULL;
1364 if (index >= sizeof(disks) / sizeof(disks[0]))
1367 flabel = disks[index].flabel;
1369 if (!flabel || (*flabel == '\0'))
1372 strncpy(label, flabel, len - 1);
1373 label[len - 1] = '\0';
1378 static struct retro_disk_control_callback disk_control = {
1379 .set_eject_state = disk_set_eject_state,
1380 .get_eject_state = disk_get_eject_state,
1381 .get_image_index = disk_get_image_index,
1382 .set_image_index = disk_set_image_index,
1383 .get_num_images = disk_get_num_images,
1384 .replace_image_index = disk_replace_image_index,
1385 .add_image_index = disk_add_image_index,
1388 static struct retro_disk_control_ext_callback disk_control_ext = {
1389 .set_eject_state = disk_set_eject_state,
1390 .get_eject_state = disk_get_eject_state,
1391 .get_image_index = disk_get_image_index,
1392 .set_image_index = disk_set_image_index,
1393 .get_num_images = disk_get_num_images,
1394 .replace_image_index = disk_replace_image_index,
1395 .add_image_index = disk_add_image_index,
1396 .set_initial_image = disk_set_initial_image,
1397 .get_image_path = disk_get_image_path,
1398 .get_image_label = disk_get_image_label,
1401 static char base_dir[1024];
1403 static bool read_m3u(const char *file)
1406 char name[PATH_MAX];
1407 FILE *fp = fopen(file, "r");
1411 while (fgets(line, sizeof(line), fp) && disk_count < sizeof(disks) / sizeof(disks[0]))
1415 char *carrige_return = strchr(line, '\r');
1417 *carrige_return = '\0';
1418 char *newline = strchr(line, '\n');
1422 if (line[0] != '\0')
1424 char disk_label[PATH_MAX];
1425 disk_label[0] = '\0';
1427 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
1428 disks[disk_count].fname = strdup(name);
1430 get_disk_label(disk_label, name, PATH_MAX);
1431 disks[disk_count].flabel = strdup(disk_label);
1438 return (disk_count != 0);
1441 static void extract_directory(char *buf, const char *path, size_t size)
1444 strncpy(buf, path, size - 1);
1445 buf[size - 1] = '\0';
1447 base = strrchr(buf, '/');
1449 base = strrchr(buf, '\\');
1460 #if defined(__QNX__) || defined(_WIN32)
1461 /* Blackberry QNX doesn't have strcasestr */
1464 * Find the first occurrence of find in s, ignore case.
1467 strcasestr(const char *s, const char *find)
1472 if ((c = *find++) != 0)
1474 c = tolower((unsigned char)c);
1480 if ((sc = *s++) == 0)
1482 } while ((char)tolower((unsigned char)sc) != c);
1483 } while (strncasecmp(s, find, len) != 0);
1490 static void set_retro_memmap(void)
1493 struct retro_memory_map retromap = { 0 };
1494 struct retro_memory_descriptor mmap = {
1495 0, psxM, 0, 0, 0, 0, 0x200000
1498 retromap.descriptors = &mmap;
1499 retromap.num_descriptors = 1;
1501 environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &retromap);
1505 static void retro_audio_buff_status_cb(
1506 bool active, unsigned occupancy, bool underrun_likely)
1508 retro_audio_buff_active = active;
1509 retro_audio_buff_occupancy = occupancy;
1510 retro_audio_buff_underrun = underrun_likely;
1513 static void retro_set_audio_buff_status_cb(void)
1515 if (frameskip_type == FRAMESKIP_NONE)
1517 environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, NULL);
1518 retro_audio_latency = 0;
1522 bool calculate_audio_latency = true;
1524 if (frameskip_type == FRAMESKIP_FIXED_INTERVAL)
1525 environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, NULL);
1528 struct retro_audio_buffer_status_callback buf_status_cb;
1529 buf_status_cb.callback = retro_audio_buff_status_cb;
1530 if (!environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK,
1533 retro_audio_buff_active = false;
1534 retro_audio_buff_occupancy = 0;
1535 retro_audio_buff_underrun = false;
1536 retro_audio_latency = 0;
1537 calculate_audio_latency = false;
1541 if (calculate_audio_latency)
1543 /* Frameskip is enabled - increase frontend
1544 * audio latency to minimise potential
1545 * buffer underruns */
1546 uint32_t frame_time_usec = 1000000.0 / (is_pal_mode ? 50.0 : 60.0);
1548 /* Set latency to 6x current frame time... */
1549 retro_audio_latency = (unsigned)(6 * frame_time_usec / 1000);
1551 /* ...then round up to nearest multiple of 32 */
1552 retro_audio_latency = (retro_audio_latency + 0x1F) & ~0x1F;
1556 update_audio_latency = true;
1557 frameskip_counter = 0;
1560 static void update_variables(bool in_flight);
1561 bool retro_load_game(const struct retro_game_info *info)
1564 unsigned int cd_index = 0;
1565 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
1566 bool is_exe = (strcasestr(info->path, ".exe") != NULL);
1569 struct retro_input_descriptor desc[] = {
1570 #define JOYP(port) \
1571 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" }, \
1572 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" }, \
1573 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" }, \
1574 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" }, \
1575 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" }, \
1576 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" }, \
1577 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" }, \
1578 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" }, \
1579 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" }, \
1580 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" }, \
1581 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" }, \
1582 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" }, \
1583 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" }, \
1584 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" }, \
1585 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" }, \
1586 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, \
1587 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" }, \
1588 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" }, \
1589 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" }, \
1590 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" }, \
1591 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_TRIGGER, "Gun Trigger" }, \
1592 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_RELOAD, "Gun Reload" }, \
1593 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A, "Gun Aux A" }, \
1594 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_B, "Gun Aux B" }, \
1595 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_START, "Gun Start" },
1611 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1613 #ifdef FRONTEND_SUPPORTS_RGB565
1614 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
1615 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
1617 SysPrintf("RGB565 supported, using it\n");
1621 if (info == NULL || info->path == NULL)
1623 SysPrintf("info->path required\n");
1627 update_variables(false);
1637 extract_directory(base_dir, info->path, sizeof(base_dir));
1641 if (!read_m3u(info->path))
1643 log_cb(RETRO_LOG_INFO, "failed to read m3u file\n");
1649 char disk_label[PATH_MAX];
1650 disk_label[0] = '\0';
1653 disks[0].fname = strdup(info->path);
1655 get_disk_label(disk_label, info->path, PATH_MAX);
1656 disks[0].flabel = strdup(disk_label);
1659 /* If this is an M3U file, attempt to set the
1660 * initial disk image */
1661 if (is_m3u && (disk_initial_index > 0) && (disk_initial_index < disk_count))
1663 const char *fname = disks[disk_initial_index].fname;
1665 if (fname && (*fname != '\0'))
1666 if (strcmp(disk_initial_path, fname) == 0)
1667 cd_index = disk_initial_index;
1670 set_cd_image(disks[cd_index].fname);
1671 disk_current_index = cd_index;
1673 /* have to reload after set_cd_image for correct cdr plugin */
1674 if (LoadPlugins() == -1)
1676 log_cb(RETRO_LOG_INFO, "failed to load plugins\n");
1683 if (OpenPlugins() == -1)
1685 log_cb(RETRO_LOG_INFO, "failed to open plugins\n");
1689 /* Handle multi-disk images (i.e. PBP)
1690 * > Cannot do this until after OpenPlugins() is
1691 * called (since this sets the value of
1692 * cdrIsoMultidiskCount) */
1693 if (!is_m3u && (cdrIsoMultidiskCount > 1))
1695 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
1697 /* Small annoyance: We need to change the label
1698 * of disk 0, so have to clear existing entries */
1699 if (disks[0].fname != NULL)
1700 free(disks[0].fname);
1701 disks[0].fname = NULL;
1703 if (disks[0].flabel != NULL)
1704 free(disks[0].flabel);
1705 disks[0].flabel = NULL;
1707 for (i = 0; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++)
1709 char disk_name[PATH_MAX - 16] = { 0 };
1710 char disk_label[PATH_MAX] = { 0 };
1712 disks[i].fname = strdup(info->path);
1714 get_disk_label(disk_name, info->path, sizeof(disk_name));
1715 snprintf(disk_label, sizeof(disk_label), "%s #%u", disk_name, (unsigned)i + 1);
1716 disks[i].flabel = strdup(disk_label);
1718 disks[i].internal_index = i;
1721 /* This is not an M3U file, so initial disk
1722 * image has not yet been set - attempt to
1724 if ((disk_initial_index > 0) && (disk_initial_index < disk_count))
1726 const char *fname = disks[disk_initial_index].fname;
1728 if (fname && (*fname != '\0'))
1729 if (strcmp(disk_initial_path, fname) == 0)
1730 cd_index = disk_initial_index;
1736 CdromLabel[0] = '\0';
1738 cdrIsoMultidiskSelect = disks[cd_index].internal_index;
1739 disk_current_index = cd_index;
1740 set_cd_image(disks[cd_index].fname);
1742 if (ReloadCdromPlugin() < 0)
1744 log_cb(RETRO_LOG_INFO, "failed to reload cdr plugins\n");
1749 log_cb(RETRO_LOG_INFO, "failed to open cdr plugin\n");
1755 /* set ports to use "standard controller" initially */
1756 for (i = 0; i < 8; ++i)
1757 in_type[i] = PSE_PAD_TYPE_STANDARD;
1759 plugin_call_rearmed_cbs();
1760 /* dfinput_activate(); */
1762 if (!is_exe && CheckCdrom() == -1)
1764 log_cb(RETRO_LOG_INFO, "unsupported/invalid CD image: %s\n", info->path);
1771 ret = Load(info->path);
1776 log_cb(RETRO_LOG_INFO, "could not load %s (%d)\n", is_exe ? "exe" : "CD", ret);
1782 retro_set_audio_buff_status_cb();
1787 unsigned retro_get_region(void)
1789 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1792 void *retro_get_memory_data(unsigned id)
1794 if (id == RETRO_MEMORY_SAVE_RAM)
1796 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1802 size_t retro_get_memory_size(unsigned id)
1804 if (id == RETRO_MEMORY_SAVE_RAM)
1806 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1812 void retro_reset(void)
1814 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
1819 static const unsigned short retro_psx_map[] = {
1820 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
1821 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
1822 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
1823 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
1824 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
1825 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
1826 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
1827 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
1828 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
1829 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
1830 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
1831 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
1832 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
1833 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
1834 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
1835 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
1837 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
1839 //Percentage distance of screen to adjust for Guncon
1840 static int GunconAdjustX = 0;
1841 static int GunconAdjustY = 0;
1843 //Used when out by a percentage with Guncon
1844 static float GunconAdjustRatioX = 1;
1845 static float GunconAdjustRatioY = 1;
1847 static void update_variables(bool in_flight)
1849 struct retro_variable var;
1851 // Always enable GPU_PEOPS_OLD_FRAME_SKIP flag
1852 // (this is set in standalone, with no option
1854 int gpu_peops_fix = GPU_PEOPS_OLD_FRAME_SKIP;
1856 frameskip_type_t prev_frameskip_type;
1859 var.key = "pcsx_rearmed_frameskip_type";
1861 prev_frameskip_type = frameskip_type;
1862 frameskip_type = FRAMESKIP_NONE;
1863 pl_rearmed_cbs.frameskip = 0;
1865 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1867 if (strcmp(var.value, "auto") == 0)
1868 frameskip_type = FRAMESKIP_AUTO;
1869 if (strcmp(var.value, "auto_threshold") == 0)
1870 frameskip_type = FRAMESKIP_AUTO_THRESHOLD;
1871 if (strcmp(var.value, "fixed_interval") == 0)
1872 frameskip_type = FRAMESKIP_FIXED_INTERVAL;
1875 if (frameskip_type != 0)
1876 pl_rearmed_cbs.frameskip = -1;
1879 var.key = "pcsx_rearmed_frameskip_threshold";
1880 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1882 frameskip_threshold = strtol(var.value, NULL, 10);
1886 var.key = "pcsx_rearmed_frameskip_interval";
1887 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1889 frameskip_interval = strtol(var.value, NULL, 10);
1893 var.key = "pcsx_rearmed_region";
1894 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1897 if (strcmp(var.value, "auto") == 0)
1899 else if (strcmp(var.value, "NTSC") == 0)
1901 else if (strcmp(var.value, "PAL") == 0)
1908 var.key = "pcsx_rearmed_negcon_deadzone";
1909 negcon_deadzone = 0;
1910 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1912 negcon_deadzone = (int)(atoi(var.value) * 0.01f * NEGCON_RANGE);
1916 var.key = "pcsx_rearmed_negcon_response";
1917 negcon_linearity = 1;
1918 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1920 if (strcmp(var.value, "quadratic") == 0)
1922 negcon_linearity = 2;
1924 else if (strcmp(var.value, "cubic") == 0)
1926 negcon_linearity = 3;
1931 var.key = "pcsx_rearmed_analog_axis_modifier";
1932 axis_bounds_modifier = true;
1933 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1935 if (strcmp(var.value, "square") == 0)
1937 axis_bounds_modifier = true;
1939 else if (strcmp(var.value, "circle") == 0)
1941 axis_bounds_modifier = false;
1946 var.key = "pcsx_rearmed_vibration";
1948 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1950 if (strcmp(var.value, "disabled") == 0)
1951 in_enable_vibration = 0;
1952 else if (strcmp(var.value, "enabled") == 0)
1953 in_enable_vibration = 1;
1957 var.key = "pcsx_rearmed_dithering";
1959 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1961 if (strcmp(var.value, "disabled") == 0)
1963 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
1964 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 0;
1965 pl_rearmed_cbs.gpu_unai.dithering = 0;
1967 pl_rearmed_cbs.gpu_neon.allow_dithering = 0;
1970 else if (strcmp(var.value, "enabled") == 0)
1972 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1973 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 1;
1974 pl_rearmed_cbs.gpu_unai.dithering = 1;
1976 pl_rearmed_cbs.gpu_neon.allow_dithering = 1;
1983 var.key = "pcsx_rearmed_neon_interlace_enable_v2";
1985 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1987 if (strcmp(var.value, "disabled") == 0)
1988 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
1989 else if (strcmp(var.value, "enabled") == 0)
1990 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
1992 pl_rearmed_cbs.gpu_neon.allow_interlace = 2;
1996 var.key = "pcsx_rearmed_neon_enhancement_enable";
1998 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2000 if (strcmp(var.value, "disabled") == 0)
2001 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
2002 else if (strcmp(var.value, "enabled") == 0)
2003 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
2007 var.key = "pcsx_rearmed_neon_enhancement_no_main";
2009 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2011 if (strcmp(var.value, "disabled") == 0)
2012 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
2013 else if (strcmp(var.value, "enabled") == 0)
2014 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
2019 var.key = "pcsx_rearmed_duping_enable";
2021 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2023 if (strcmp(var.value, "disabled") == 0)
2024 duping_enable = false;
2025 else if (strcmp(var.value, "enabled") == 0)
2026 duping_enable = true;
2030 var.key = "pcsx_rearmed_display_internal_fps";
2032 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2034 if (strcmp(var.value, "disabled") == 0)
2035 display_internal_fps = false;
2036 else if (strcmp(var.value, "enabled") == 0)
2037 display_internal_fps = true;
2041 // CPU emulation related config
2044 var.key = "pcsx_rearmed_drc";
2046 if (!environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
2047 var.value = "enabled";
2050 R3000Acpu *prev_cpu = psxCpu;
2054 Config.Cpu = CPU_INTERPRETER;
2057 if (strcmp(var.value, "disabled") == 0)
2058 Config.Cpu = CPU_INTERPRETER;
2059 else if (strcmp(var.value, "enabled") == 0)
2060 Config.Cpu = CPU_DYNAREC;
2062 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
2063 if (psxCpu != prev_cpu)
2065 prev_cpu->Notify(R3000ACPU_NOTIFY_BEFORE_SAVE, NULL);
2066 prev_cpu->Shutdown();
2069 psxCpu->Notify(R3000ACPU_NOTIFY_AFTER_LOAD, NULL);
2072 #endif /* !DRC_DISABLE */
2075 var.key = "pcsx_rearmed_psxclock";
2076 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2078 int psxclock = atoi(var.value);
2079 Config.cycle_multiplier = 10000 / psxclock;
2082 #if !defined(DRC_DISABLE) && !defined(LIGHTREC)
2084 var.key = "pcsx_rearmed_nosmccheck";
2085 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2087 if (strcmp(var.value, "enabled") == 0)
2088 new_dynarec_hacks |= NDHACK_NO_SMC_CHECK;
2090 new_dynarec_hacks &= ~NDHACK_NO_SMC_CHECK;
2094 var.key = "pcsx_rearmed_gteregsunneeded";
2095 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2097 if (strcmp(var.value, "enabled") == 0)
2098 new_dynarec_hacks |= NDHACK_GTE_UNNEEDED;
2100 new_dynarec_hacks &= ~NDHACK_GTE_UNNEEDED;
2104 var.key = "pcsx_rearmed_nogteflags";
2105 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2107 if (strcmp(var.value, "enabled") == 0)
2108 new_dynarec_hacks |= NDHACK_GTE_NO_FLAGS;
2110 new_dynarec_hacks &= ~NDHACK_GTE_NO_FLAGS;
2114 var.key = "pcsx_rearmed_nocompathacks";
2115 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2117 if (strcmp(var.value, "enabled") == 0)
2118 new_dynarec_hacks |= NDHACK_NO_COMPAT_HACKS;
2120 new_dynarec_hacks &= ~NDHACK_NO_COMPAT_HACKS;
2122 #endif /* !DRC_DISABLE && !LIGHTREC */
2125 var.key = "pcsx_rearmed_nostalls";
2126 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2128 if (strcmp(var.value, "enabled") == 0)
2129 Config.DisableStalls = 1;
2131 Config.DisableStalls = 0;
2135 var.key = "pcsx_rearmed_icache_emulation";
2136 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2138 if (strcmp(var.value, "disabled") == 0)
2139 Config.icache_emulation = 0;
2140 else if (strcmp(var.value, "enabled") == 0)
2141 Config.icache_emulation = 1;
2145 var.key = "pcsx_rearmed_exception_emulation";
2146 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2148 if (strcmp(var.value, "enabled") == 0)
2149 Config.PreciseExceptions = 1;
2151 Config.PreciseExceptions = 0;
2154 psxCpu->ApplyConfig();
2156 // end of CPU emu config
2160 var.key = "pcsx_rearmed_spu_reverb";
2162 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2164 if (strcmp(var.value, "disabled") == 0)
2165 spu_config.iUseReverb = false;
2166 else if (strcmp(var.value, "enabled") == 0)
2167 spu_config.iUseReverb = true;
2171 var.key = "pcsx_rearmed_spu_interpolation";
2173 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2175 if (strcmp(var.value, "simple") == 0)
2176 spu_config.iUseInterpolation = 1;
2177 else if (strcmp(var.value, "gaussian") == 0)
2178 spu_config.iUseInterpolation = 2;
2179 else if (strcmp(var.value, "cubic") == 0)
2180 spu_config.iUseInterpolation = 3;
2181 else if (strcmp(var.value, "off") == 0)
2182 spu_config.iUseInterpolation = 0;
2186 var.key = "pcsx_rearmed_spu_thread";
2187 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2189 if (strcmp(var.value, "enabled") == 0)
2190 spu_config.iUseThread = 1;
2192 spu_config.iUseThread = 0;
2195 if (P_HAVE_PTHREAD) {
2197 var.key = "pcsx_rearmed_async_cd";
2198 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2200 if (strcmp(var.value, "async") == 0)
2203 Config.CHD_Precache = 0;
2205 else if (strcmp(var.value, "sync") == 0)
2208 Config.CHD_Precache = 0;
2210 else if (strcmp(var.value, "precache") == 0)
2213 Config.CHD_Precache = 1;
2219 var.key = "pcsx_rearmed_noxadecoding";
2220 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2222 if (strcmp(var.value, "disabled") == 0)
2229 var.key = "pcsx_rearmed_nocdaudio";
2230 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2232 if (strcmp(var.value, "disabled") == 0)
2239 var.key = "pcsx_rearmed_gpu_slow_llists";
2240 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2242 if (strcmp(var.value, "disabled") == 0)
2243 Config.GpuListWalking = 0;
2244 else if (strcmp(var.value, "enabled") == 0)
2245 Config.GpuListWalking = 1;
2247 Config.GpuListWalking = -1;
2251 var.key = "pcsx_rearmed_screen_centering";
2252 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2254 if (strcmp(var.value, "game") == 0)
2255 pl_rearmed_cbs.screen_centering_type = 1;
2256 else if (strcmp(var.value, "manual") == 0)
2257 pl_rearmed_cbs.screen_centering_type = 2;
2259 pl_rearmed_cbs.screen_centering_type = 0;
2263 var.key = "pcsx_rearmed_screen_centering_x";
2264 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2266 pl_rearmed_cbs.screen_centering_x = atoi(var.value);
2270 var.key = "pcsx_rearmed_screen_centering_y";
2271 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2273 pl_rearmed_cbs.screen_centering_y = atoi(var.value);
2276 #ifdef THREAD_RENDERING
2277 var.key = "pcsx_rearmed_gpu_thread_rendering";
2280 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2282 if (strcmp(var.value, "disabled") == 0)
2283 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_OFF;
2284 else if (strcmp(var.value, "sync") == 0)
2285 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_SYNC;
2286 else if (strcmp(var.value, "async") == 0)
2287 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_ASYNC;
2293 var.key = "pcsx_rearmed_gpu_peops_odd_even_bit";
2295 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2297 if (strcmp(var.value, "enabled") == 0)
2298 gpu_peops_fix |= GPU_PEOPS_ODD_EVEN_BIT;
2302 var.key = "pcsx_rearmed_gpu_peops_expand_screen_width";
2304 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2306 if (strcmp(var.value, "enabled") == 0)
2307 gpu_peops_fix |= GPU_PEOPS_EXPAND_SCREEN_WIDTH;
2311 var.key = "pcsx_rearmed_gpu_peops_ignore_brightness";
2313 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2315 if (strcmp(var.value, "enabled") == 0)
2316 gpu_peops_fix |= GPU_PEOPS_IGNORE_BRIGHTNESS;
2320 var.key = "pcsx_rearmed_gpu_peops_disable_coord_check";
2322 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2324 if (strcmp(var.value, "enabled") == 0)
2325 gpu_peops_fix |= GPU_PEOPS_DISABLE_COORD_CHECK;
2329 var.key = "pcsx_rearmed_gpu_peops_lazy_screen_update";
2331 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2333 if (strcmp(var.value, "enabled") == 0)
2334 gpu_peops_fix |= GPU_PEOPS_LAZY_SCREEN_UPDATE;
2338 var.key = "pcsx_rearmed_gpu_peops_repeated_triangles";
2340 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2342 if (strcmp(var.value, "enabled") == 0)
2343 gpu_peops_fix |= GPU_PEOPS_REPEATED_TRIANGLES;
2347 var.key = "pcsx_rearmed_gpu_peops_quads_with_triangles";
2349 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2351 if (strcmp(var.value, "enabled") == 0)
2352 gpu_peops_fix |= GPU_PEOPS_QUADS_WITH_TRIANGLES;
2356 var.key = "pcsx_rearmed_gpu_peops_fake_busy_state";
2358 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2360 if (strcmp(var.value, "enabled") == 0)
2361 gpu_peops_fix |= GPU_PEOPS_FAKE_BUSY_STATE;
2364 if (pl_rearmed_cbs.gpu_peops.dwActFixes != gpu_peops_fix)
2365 pl_rearmed_cbs.gpu_peops.dwActFixes = gpu_peops_fix;
2369 /* Note: This used to be an option, but it only works
2370 * (correctly) when running high resolution games
2371 * (480i, 512i) and has been obsoleted by
2372 * pcsx_rearmed_gpu_unai_scale_hires */
2373 pl_rearmed_cbs.gpu_unai.ilace_force = 0;
2374 /* Note: This used to be an option, but it has no
2375 * discernable effect and has been obsoleted by
2376 * pcsx_rearmed_gpu_unai_scale_hires */
2377 pl_rearmed_cbs.gpu_unai.pixel_skip = 0;
2379 var.key = "pcsx_rearmed_gpu_unai_lighting";
2382 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2384 if (strcmp(var.value, "disabled") == 0)
2385 pl_rearmed_cbs.gpu_unai.lighting = 0;
2386 else if (strcmp(var.value, "enabled") == 0)
2387 pl_rearmed_cbs.gpu_unai.lighting = 1;
2390 var.key = "pcsx_rearmed_gpu_unai_fast_lighting";
2393 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2395 if (strcmp(var.value, "disabled") == 0)
2396 pl_rearmed_cbs.gpu_unai.fast_lighting = 0;
2397 else if (strcmp(var.value, "enabled") == 0)
2398 pl_rearmed_cbs.gpu_unai.fast_lighting = 1;
2401 var.key = "pcsx_rearmed_gpu_unai_blending";
2404 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2406 if (strcmp(var.value, "disabled") == 0)
2407 pl_rearmed_cbs.gpu_unai.blending = 0;
2408 else if (strcmp(var.value, "enabled") == 0)
2409 pl_rearmed_cbs.gpu_unai.blending = 1;
2412 var.key = "pcsx_rearmed_gpu_unai_scale_hires";
2415 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2417 if (strcmp(var.value, "disabled") == 0)
2418 pl_rearmed_cbs.gpu_unai.scale_hires = 0;
2419 else if (strcmp(var.value, "enabled") == 0)
2420 pl_rearmed_cbs.gpu_unai.scale_hires = 1;
2425 var.key = "pcsx_rearmed_crosshair1";
2427 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2429 if (strcmp(var.value, "disabled") == 0)
2430 in_enable_crosshair[0] = 0;
2431 else if (strcmp(var.value, "blue") == 0)
2432 in_enable_crosshair[0] = 0x1F;
2433 else if (strcmp(var.value, "green") == 0)
2434 in_enable_crosshair[0] = 0x7E0;
2435 else if (strcmp(var.value, "red") == 0)
2436 in_enable_crosshair[0] = 0xF800;
2437 else if (strcmp(var.value, "white") == 0)
2438 in_enable_crosshair[0] = 0xFFFF;
2442 var.key = "pcsx_rearmed_crosshair2";
2444 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2446 if (strcmp(var.value, "disabled") == 0)
2447 in_enable_crosshair[1] = 0;
2448 else if (strcmp(var.value, "blue") == 0)
2449 in_enable_crosshair[1] = 0x1F;
2450 else if (strcmp(var.value, "green") == 0)
2451 in_enable_crosshair[1] = 0x7E0;
2452 else if (strcmp(var.value, "red") == 0)
2453 in_enable_crosshair[1] = 0xF800;
2454 else if (strcmp(var.value, "white") == 0)
2455 in_enable_crosshair[1] = 0xFFFF;
2458 //This adjustment process gives the user the ability to manually align the mouse up better
2459 //with where the shots are in the emulator.
2462 var.key = "pcsx_rearmed_konamigunadjustx";
2464 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2466 KonamiGunAdjustX = atof(var.value) / 100.0f;
2470 var.key = "pcsx_rearmed_konamigunadjusty";
2472 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2474 KonamiGunAdjustY = atof(var.value) / 100.0f;
2478 var.key = "pcsx_rearmed_gunconadjustx";
2480 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2482 GunconAdjustX = atoi(var.value);
2486 var.key = "pcsx_rearmed_gunconadjusty";
2488 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2490 GunconAdjustY = atoi(var.value);
2494 var.key = "pcsx_rearmed_gunconadjustratiox";
2496 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2498 GunconAdjustRatioX = atof(var.value);
2502 var.key = "pcsx_rearmed_gunconadjustratioy";
2504 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2506 GunconAdjustRatioY = atof(var.value);
2510 var.key = "pcsx_rearmed_input_sensitivity";
2511 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2513 mouse_sensitivity = atof(var.value);
2518 // inform core things about possible config changes
2519 plugin_call_rearmed_cbs();
2521 if (GPU_open != NULL && GPU_close != NULL)
2524 GPU_open(&gpuDisp, "PCSX", NULL);
2527 /* Reinitialise frameskipping, if required */
2528 if (((frameskip_type != prev_frameskip_type)))
2529 retro_set_audio_buff_status_cb();
2531 /* dfinput_activate(); */
2537 //bootlogo display hack
2541 var.key = "pcsx_rearmed_show_bios_bootlogo";
2542 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2544 Config.SlowBoot = 0;
2546 if (strcmp(var.value, "enabled") == 0)
2548 Config.SlowBoot = 1;
2555 update_option_visibility();
2558 // Taken from beetle-psx-libretro
2559 static uint16_t get_analog_button(int16_t ret, retro_input_state_t input_state_cb, int player_index, int id)
2561 // NOTE: Analog buttons were added Nov 2017. Not all front-ends support this
2562 // feature (or pre-date it) so we need to handle this in a graceful way.
2564 // First, try and get an analog value using the new libretro API constant
2565 uint16_t button = input_state_cb(player_index,
2566 RETRO_DEVICE_ANALOG,
2567 RETRO_DEVICE_INDEX_ANALOG_BUTTON,
2569 button = MIN(button / 128, 255);
2573 // If we got exactly zero, we're either not pressing the button, or the front-end
2574 // is not reporting analog values. We need to do a second check using the classic
2575 // digital API method, to at least get some response - better than nothing.
2577 // NOTE: If we're really just not holding the button, we're still going to get zero.
2579 button = (ret & (1 << id)) ? 255 : 0;
2585 unsigned char axis_range_modifier(int16_t axis_value, bool is_square)
2587 float modifier_axis_range = 0;
2591 modifier_axis_range = round((axis_value >> 8) / 0.785) + 128;
2592 if (modifier_axis_range < 0)
2594 modifier_axis_range = 0;
2596 else if (modifier_axis_range > 255)
2598 modifier_axis_range = 255;
2603 modifier_axis_range = MIN(((axis_value >> 8) + 128), 255);
2606 return modifier_axis_range;
2609 static void update_input_guncon(int port, int ret)
2612 //Separate pointer and lightgun control types
2614 //Mouse range is -32767 -> 32767
2616 //Use the left analog stick field to store the absolute coordinates
2618 int gunx = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X);
2619 int guny = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y);
2621 //Have the Libretro API let /libpcsxcore/plugins.c know when the lightgun is pointed offscreen
2622 //Offscreen value is chosen to be well out of range of any possible scaling done via core options
2623 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))
2625 in_analog_left[port][0] = 65536;
2626 in_analog_left[port][1] = 65536;
2630 in_analog_left[port][0] = ((gunx * GunconAdjustRatioX) + (GunconAdjustX * 655)) / 64 + 512;
2631 in_analog_left[port][1] = ((guny * GunconAdjustRatioY) + (GunconAdjustY * 655)) / 64 + 512;
2634 //GUNCON has 3 controls, Trigger,A,B which equal Circle,Start,Cross
2637 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))
2638 in_keystate[port] |= (1 << DKEY_CIRCLE);
2641 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A))
2642 in_keystate[port] |= (1 << DKEY_START);
2645 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_B))
2646 in_keystate[port] |= (1 << DKEY_CROSS);
2650 static void update_input_justifier(int port, int ret)
2653 //Separate pointer and lightgun control types
2655 //RetroArch lightgun range is -32767 -> 32767 on both axes (positive Y is down)
2657 //JUSTIFIER has 3 controls, Trigger,Special,Start which equal Square,Cross,Start
2660 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))
2661 in_keystate[port] |= (1 << DKEY_SQUARE);
2664 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A))
2665 in_keystate[port] |= (1 << DKEY_CROSS);
2668 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_START))
2669 in_keystate[port] |= (1 << DKEY_START);
2673 static void update_input_negcon(int port, int ret)
2679 float negcon_twist_amplitude;
2681 // Query digital inputs
2684 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_UP))
2685 in_keystate[port] |= (1 << DKEY_UP);
2687 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT))
2688 in_keystate[port] |= (1 << DKEY_RIGHT);
2690 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN))
2691 in_keystate[port] |= (1 << DKEY_DOWN);
2693 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT))
2694 in_keystate[port] |= (1 << DKEY_LEFT);
2696 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_START))
2697 in_keystate[port] |= (1 << DKEY_START);
2699 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_A))
2700 in_keystate[port] |= (1 << DKEY_CIRCLE);
2702 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_X))
2703 in_keystate[port] |= (1 << DKEY_TRIANGLE);
2704 // > neGcon R shoulder (digital)
2705 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_R))
2706 in_keystate[port] |= (1 << DKEY_R1);
2707 // Query analog inputs
2709 // From studying 'libpcsxcore/plugins.c' and 'frontend/plugin.c':
2710 // >> pad->leftJoyX == in_analog_left[port][0] == NeGcon II
2711 // >> pad->leftJoyY == in_analog_left[port][1] == NeGcon L
2712 // >> pad->rightJoyX == in_analog_right[port][0] == NeGcon twist
2713 // >> pad->rightJoyY == in_analog_right[port][1] == NeGcon I
2714 // So we just have to map in_analog_left/right to more
2715 // appropriate inputs...
2718 // >> Get raw analog stick value and account for deadzone
2719 lsx = input_state_cb(port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
2720 if (lsx > negcon_deadzone)
2721 lsx = lsx - negcon_deadzone;
2722 else if (lsx < -negcon_deadzone)
2723 lsx = lsx + negcon_deadzone;
2726 // >> Convert to an 'amplitude' [-1.0,1.0] and adjust response
2727 negcon_twist_amplitude = (float)lsx / (float)(NEGCON_RANGE - negcon_deadzone);
2728 if (negcon_linearity == 2)
2730 if (negcon_twist_amplitude < 0.0)
2731 negcon_twist_amplitude = -(negcon_twist_amplitude * negcon_twist_amplitude);
2733 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude;
2735 else if (negcon_linearity == 3)
2736 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude * negcon_twist_amplitude;
2737 // >> Convert to final 'in_analog' integer value [0,255]
2738 in_analog_right[port][0] = MAX(MIN((int)(negcon_twist_amplitude * 128.0f) + 128, 255), 0);
2740 // >> Handle right analog stick vertical axis mapping...
2741 // - Up (-Y) == accelerate == neGcon I
2742 // - Down (+Y) == brake == neGcon II
2745 rsy = input_state_cb(port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
2748 // Account for deadzone
2749 // (Note: have never encountered a gamepad with significant differences
2750 // in deadzone between left/right analog sticks, so use the regular 'twist'
2752 if (rsy > negcon_deadzone)
2753 rsy = rsy - negcon_deadzone;
2756 // Convert to 'in_analog' integer value [0,255]
2757 negcon_ii_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2761 if (rsy < -negcon_deadzone)
2762 rsy = -1 * (rsy + negcon_deadzone);
2765 negcon_i_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2768 in_analog_right[port][1] = MAX(
2770 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_R2),
2771 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_B)),
2774 in_analog_left[port][0] = MAX(
2776 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_L2),
2777 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_Y)),
2780 in_analog_left[port][1] = get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_L);
2783 static void update_input_mouse(int port, int ret)
2785 float raw_x = input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X);
2786 float raw_y = input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y);
2788 int x = (int)roundf(raw_x * mouse_sensitivity);
2789 int y = (int)roundf(raw_y * mouse_sensitivity);
2791 if (x > 127) x = 127;
2792 else if (x < -128) x = -128;
2794 if (y > 127) y = 127;
2795 else if (y < -128) y = -128;
2797 in_mouse[port][0] = x; /* -128..+128 left/right movement, 0 = no movement */
2798 in_mouse[port][1] = y; /* -128..+128 down/up movement, 0 = no movement */
2800 /* left mouse button state */
2801 if (input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_LEFT))
2802 in_keystate[port] |= 1 << 11;
2804 /* right mouse button state */
2805 if (input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_RIGHT))
2806 in_keystate[port] |= 1 << 10;
2809 static void update_input(void)
2811 // reset all keystate, query libretro for keystate
2815 for (i = 0; i < PORTS_NUMBER; i++)
2818 int type = in_type[i];
2822 if (type == PSE_PAD_TYPE_NONE)
2825 if (libretro_supports_bitmasks)
2826 ret = input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
2829 for (j = 0; j < (RETRO_DEVICE_ID_JOYPAD_R3 + 1); j++)
2831 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j))
2838 case PSE_PAD_TYPE_GUNCON:
2839 update_input_guncon(i, ret);
2841 case PSE_PAD_TYPE_GUN:
2842 update_input_justifier(i, ret);
2844 case PSE_PAD_TYPE_NEGCON:
2845 update_input_negcon(i, ret);
2847 case PSE_PAD_TYPE_MOUSE:
2848 update_input_mouse(i, ret);
2851 // Query digital inputs
2852 for (j = 0; j < RETRO_PSX_MAP_LEN; j++)
2854 in_keystate[i] |= retro_psx_map[j];
2856 // Query analog inputs
2857 if (type == PSE_PAD_TYPE_ANALOGJOY || type == PSE_PAD_TYPE_ANALOGPAD)
2859 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);
2860 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);
2861 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);
2862 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);
2868 static void print_internal_fps(void)
2870 if (display_internal_fps)
2874 if (frame_count % INTERNAL_FPS_SAMPLE_PERIOD == 0)
2876 unsigned internal_fps = pl_rearmed_cbs.flip_cnt * (is_pal_mode ? 50 : 60) / INTERNAL_FPS_SAMPLE_PERIOD;
2878 const char *strc = (const char *)str;
2882 snprintf(str, sizeof(str), "Internal FPS: %2d", internal_fps);
2884 pl_rearmed_cbs.flip_cnt = 0;
2886 if (msg_interface_version >= 1)
2888 struct retro_message_ext msg = {
2893 RETRO_MESSAGE_TARGET_OSD,
2894 RETRO_MESSAGE_TYPE_STATUS,
2897 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE_EXT, &msg);
2901 struct retro_message msg = {
2905 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
2913 void retro_run(void)
2915 //SysReset must be run while core is running,Not in menu (Locks up Retroarch)
2924 print_internal_fps();
2926 /* Check whether current frame should
2928 pl_rearmed_cbs.fskip_force = 0;
2929 pl_rearmed_cbs.fskip_dirty = 0;
2931 if (frameskip_type != FRAMESKIP_NONE)
2933 bool skip_frame = false;
2935 switch (frameskip_type)
2937 case FRAMESKIP_AUTO:
2938 skip_frame = retro_audio_buff_active && retro_audio_buff_underrun;
2940 case FRAMESKIP_AUTO_THRESHOLD:
2941 skip_frame = retro_audio_buff_active && (retro_audio_buff_occupancy < frameskip_threshold);
2943 case FRAMESKIP_FIXED_INTERVAL:
2950 if (skip_frame && frameskip_counter < frameskip_interval)
2951 pl_rearmed_cbs.fskip_force = 1;
2954 /* If frameskip/timing settings have changed,
2955 * update frontend audio latency
2956 * > Can do this before or after the frameskip
2957 * check, but doing it after means we at least
2958 * retain the current frame's audio output */
2959 if (update_audio_latency)
2961 environ_cb(RETRO_ENVIRONMENT_SET_MINIMUM_AUDIO_LATENCY,
2962 &retro_audio_latency);
2963 update_audio_latency = false;
2970 bool updated = false;
2971 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
2972 update_variables(true);
2977 if (pl_rearmed_cbs.fskip_dirty == 1) {
2978 if (frameskip_counter < frameskip_interval)
2979 frameskip_counter++;
2980 else if (frameskip_counter >= frameskip_interval || !pl_rearmed_cbs.fskip_force)
2981 frameskip_counter = 0;
2984 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf_ptr : NULL,
2985 vout_width, vout_height, vout_width * 2);
2991 static bool try_use_bios(const char *path, bool preferred_only)
2995 FILE *fp = fopen(path, "rb");
2999 fseek(fp, 0, SEEK_END);
3003 name = strrchr(path, SLASH);
3007 if (preferred_only && size != 512 * 1024)
3009 if (size != 512 * 1024 && size != 4 * 1024 * 1024)
3011 if (strstr(name, "unirom"))
3013 // jp bios have an addidional region check
3014 if (preferred_only && (strcasestr(name, "00.") || strcasestr(name, "j.bin")))
3017 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
3022 #include <sys/types.h>
3025 static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
3027 static const char *substr_pref[] = { "scph", "ps" };
3028 static const char *substr_alt[] = { "scph", "ps", "openbios" };
3034 dir = opendir(dirpath);
3038 // try to find a "better" bios
3039 while ((ent = readdir(dir)))
3041 for (i = 0; i < sizeof(substr_pref) / sizeof(substr_pref[0]); i++)
3043 const char *substr = substr_pref[i];
3044 if ((strncasecmp(ent->d_name, substr, strlen(substr)) != 0))
3046 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
3047 ret = try_use_bios(path, true);
3053 // another pass to look for anything fitting, even ps2 bios
3055 while ((ent = readdir(dir)))
3057 for (i = 0; i < sizeof(substr_alt) / sizeof(substr_alt[0]); i++)
3059 const char *substr = substr_alt[i];
3060 if ((strncasecmp(ent->d_name, substr, strlen(substr)) != 0))
3062 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
3063 ret = try_use_bios(path, false);
3075 #define find_any_bios(...) false
3078 static void check_system_specs(void)
3081 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
3084 static int init_memcards(void)
3088 struct retro_variable var = { .key = "pcsx_rearmed_memcard2", .value = NULL };
3089 static const char CARD2_FILE[] = "pcsx-card2.mcd";
3091 // Memcard2 will be handled and is re-enabled if needed using core
3093 // Memcard1 is handled by libretro, doing this will set core to
3094 // skip file io operations for memcard1 like SaveMcd
3095 snprintf(Config.Mcd1, sizeof(Config.Mcd1), "none");
3096 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "none");
3097 init_memcard(Mcd1Data);
3098 // Memcard 2 is managed by the emulator on the filesystem,
3099 // There is no need to initialize Mcd2Data like Mcd1Data.
3101 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
3103 SysPrintf("Memcard 2: %s\n", var.value);
3104 if (memcmp(var.value, "enabled", 7) == 0)
3106 if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir)
3108 if (strlen(dir) + strlen(CARD2_FILE) + 2 > sizeof(Config.Mcd2))
3110 SysPrintf("Path '%s' is too long. Cannot use memcard 2. Use a shorter path.\n", dir);
3116 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "%s/%s", dir, CARD2_FILE);
3117 SysPrintf("Use memcard 2: %s\n", Config.Mcd2);
3122 SysPrintf("Could not get save directory! Could not create memcard 2.");
3130 static void loadPSXBios(void)
3133 char path[PATH_MAX];
3134 unsigned useHLE = 0;
3136 const char *bios[] = {
3137 "PSXONPSP660", "psxonpsp660",
3138 "SCPH101", "scph101",
3139 "SCPH5501", "scph5501",
3140 "SCPH7001", "scph7001",
3141 "SCPH1001", "scph1001"
3144 struct retro_variable var = {
3145 .key = "pcsx_rearmed_bios",
3151 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
3153 if (!strcmp(var.value, "HLE"))
3159 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
3162 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s", dir);
3164 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++)
3166 snprintf(path, sizeof(path), "%s%c%s.bin", dir, SLASH, bios[i]);
3167 found_bios = try_use_bios(path, true);
3173 found_bios = find_any_bios(dir, path, sizeof(path));
3177 SysPrintf("found BIOS file: %s\n", Config.Bios);
3183 const char *msg_str;
3186 msg_str = "BIOS set to \'hle\' in core options - real BIOS will be ignored";
3187 SysPrintf("Using HLE BIOS.\n");
3191 msg_str = "No PlayStation BIOS file found - add for better compatibility";
3192 SysPrintf("No BIOS files found.\n");
3195 if (msg_interface_version >= 1)
3197 struct retro_message_ext msg = {
3202 RETRO_MESSAGE_TARGET_ALL,
3203 RETRO_MESSAGE_TYPE_NOTIFICATION,
3206 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE_EXT, &msg);
3210 struct retro_message msg = {
3214 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
3219 void retro_init(void)
3221 unsigned dci_version = 0;
3222 struct retro_rumble_interface rumble;
3225 msg_interface_version = 0;
3226 environ_cb(RETRO_ENVIRONMENT_GET_MESSAGE_INTERFACE_VERSION, &msg_interface_version);
3228 #if defined(__MACH__) && !defined(TVOS)
3229 // magic sauce to make the dynarec work on iOS
3230 syscall(SYS_ptrace, 0 /*PTRACE_TRACEME*/, 0, 0, 0);
3234 psxMapHook = pl_3ds_mmap;
3235 psxUnmapHook = pl_3ds_munmap;
3238 if (init_vita_mmap() < 0)
3240 psxMapHook = pl_vita_mmap;
3241 psxUnmapHook = pl_vita_munmap;
3243 ret = emu_core_preinit();
3245 /* emu_core_preinit sets the cpu to dynarec */
3247 Config.Cpu = CPU_INTERPRETER;
3249 ret |= init_memcards();
3251 ret |= emu_core_init();
3254 SysPrintf("PCSX init failed.\n");
3259 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
3260 #elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && P_HAVE_POSIX_MEMALIGN
3261 if (posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2) != 0)
3262 vout_buf = (void *) 0;
3264 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
3267 vout_buf_ptr = vout_buf;
3271 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
3273 disk_initial_index = 0;
3274 disk_initial_path[0] = '\0';
3275 if (environ_cb(RETRO_ENVIRONMENT_GET_DISK_CONTROL_INTERFACE_VERSION, &dci_version) && (dci_version >= 1))
3276 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_EXT_INTERFACE, &disk_control_ext);
3278 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
3281 if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble))
3282 rumble_cb = rumble.set_rumble_state;
3284 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
3285 * we have to do this because cache misses and some IO penalties
3286 * are not emulated. Warning: changing this may break compatibility. */
3287 Config.cycle_multiplier = CYCLE_MULT_DEFAULT;
3288 #if defined(HAVE_PRE_ARMV7) && !defined(_3DS)
3289 Config.cycle_multiplier = 200;
3291 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
3292 pl_rearmed_cbs.gpu_peops.dwActFixes = GPU_PEOPS_OLD_FRAME_SKIP;
3294 SaveFuncs.open = save_open;
3295 SaveFuncs.read = save_read;
3296 SaveFuncs.write = save_write;
3297 SaveFuncs.seek = save_seek;
3298 SaveFuncs.close = save_close;
3300 if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL))
3301 libretro_supports_bitmasks = true;
3303 check_system_specs();
3306 void retro_deinit(void)
3315 linearFree(vout_buf);
3324 libretro_supports_bitmasks = false;
3325 libretro_supports_option_categories = false;
3327 show_input_settings = true;
3329 show_advanced_gpu_peops_settings = true;
3332 show_advanced_gpu_unai_settings = true;
3335 /* Have to reset disks struct, otherwise
3336 * fnames/flabels will leak memory */
3338 frameskip_type = FRAMESKIP_NONE;
3339 frameskip_threshold = 0;
3340 frameskip_interval = 0;
3341 frameskip_counter = 0;
3342 retro_audio_buff_active = false;
3343 retro_audio_buff_occupancy = 0;
3344 retro_audio_buff_underrun = false;
3345 retro_audio_latency = 0;
3346 update_audio_latency = false;
3350 #include <psp2/kernel/threadmgr.h>
3351 int usleep(unsigned long us)
3353 sceKernelDelayThread(us);
3357 void SysPrintf(const char *fmt, ...)
3362 va_start(list, fmt);
3363 vsprintf(msg, fmt, list);
3367 log_cb(RETRO_LOG_INFO, "%s", msg);
3370 /* Prints debug-level logs */
3371 void SysDLog(const char *fmt, ...)
3376 va_start(list, fmt);
3377 vsprintf(msg, fmt, list);
3381 log_cb(RETRO_LOG_DEBUG, "%s", msg);
3384 // vim:sw=3:ts=3:expandtab