setup spu r8 handlers
[pcsx_rearmed.git] / frontend / libretro.c
... / ...
CommitLineData
1/*
2 * (C) notaz, 2012,2014,2015
3 *
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
8#define _GNU_SOURCE 1 // strcasestr
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <strings.h>
13#ifdef __MACH__
14#include <unistd.h>
15#include <sys/syscall.h>
16#endif
17
18#ifdef SWITCH
19#include <switch.h>
20#endif
21
22#include "../libpcsxcore/misc.h"
23#include "../libpcsxcore/psxcounters.h"
24#include "../libpcsxcore/psxmem_map.h"
25#include "../libpcsxcore/new_dynarec/new_dynarec.h"
26#include "../libpcsxcore/cdrom.h"
27#include "../libpcsxcore/cdriso.h"
28#include "../libpcsxcore/cheat.h"
29#include "../libpcsxcore/r3000a.h"
30#include "../plugins/dfsound/out.h"
31#include "../plugins/dfsound/spu_config.h"
32#include "cspace.h"
33#include "main.h"
34#include "menu.h"
35#include "plugin.h"
36#include "plugin_lib.h"
37#include "arm_features.h"
38#include "revision.h"
39
40#include <libretro.h>
41#include "libretro_core_options.h"
42
43#ifdef USE_LIBRETRO_VFS
44#include <streams/file_stream_transforms.h>
45#endif
46
47#ifdef _3DS
48#include "3ds/3ds_utils.h"
49#endif
50
51#define PORTS_NUMBER 8
52
53#ifndef MIN
54#define MIN(a, b) ((a) < (b) ? (a) : (b))
55#endif
56
57#ifndef MAX
58#define MAX(a, b) ((a) > (b) ? (a) : (b))
59#endif
60
61#define ISHEXDEC ((buf[cursor] >= '0') && (buf[cursor] <= '9')) || ((buf[cursor] >= 'a') && (buf[cursor] <= 'f')) || ((buf[cursor] >= 'A') && (buf[cursor] <= 'F'))
62
63#define INTERNAL_FPS_SAMPLE_PERIOD 64
64
65//hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
66static int rebootemu = 0;
67
68static retro_video_refresh_t video_cb;
69static retro_input_poll_t input_poll_cb;
70static retro_input_state_t input_state_cb;
71static retro_environment_t environ_cb;
72static retro_audio_sample_batch_t audio_batch_cb;
73static retro_set_rumble_state_t rumble_cb;
74static struct retro_log_callback logging;
75static retro_log_printf_t log_cb;
76
77static unsigned msg_interface_version = 0;
78
79static void *vout_buf;
80static void *vout_buf_ptr;
81static int vout_width, vout_height;
82static int vout_fb_dirty;
83static int psx_w, psx_h;
84static bool vout_can_dupe;
85static bool duping_enable;
86static bool found_bios;
87static bool display_internal_fps = false;
88static unsigned frame_count = 0;
89static bool libretro_supports_bitmasks = false;
90static bool libretro_supports_option_categories = false;
91static bool show_input_settings = true;
92#ifdef GPU_PEOPS
93static bool show_advanced_gpu_peops_settings = true;
94#endif
95#ifdef GPU_UNAI
96static bool show_advanced_gpu_unai_settings = true;
97#endif
98static float mouse_sensitivity = 1.0f;
99
100typedef enum
101{
102 FRAMESKIP_NONE = 0,
103 FRAMESKIP_AUTO,
104 FRAMESKIP_AUTO_THRESHOLD,
105 FRAMESKIP_FIXED_INTERVAL
106} frameskip_type_t;
107
108static unsigned frameskip_type = FRAMESKIP_NONE;
109static unsigned frameskip_threshold = 0;
110static unsigned frameskip_interval = 0;
111static unsigned frameskip_counter = 0;
112
113static int retro_audio_buff_active = false;
114static unsigned retro_audio_buff_occupancy = 0;
115static int retro_audio_buff_underrun = false;
116
117static unsigned retro_audio_latency = 0;
118static int update_audio_latency = false;
119
120static unsigned previous_width = 0;
121static unsigned previous_height = 0;
122
123static int plugins_opened;
124static int is_pal_mode;
125
126/* memory card data */
127extern char Mcd1Data[MCD_SIZE];
128extern char Mcd2Data[MCD_SIZE];
129extern char McdDisable[2];
130
131/* PCSX ReARMed core calls and stuff */
132int in_type[8] = {
133 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
134 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
135 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
136 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE
137};
138int in_analog_left[8][2] = { { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 } };
139int in_analog_right[8][2] = { { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 }, { 127, 127 } };
140unsigned short in_keystate[PORTS_NUMBER];
141int in_mouse[8][2];
142int multitap1 = 0;
143int multitap2 = 0;
144int in_enable_vibration = 1;
145
146// NegCon adjustment parameters
147// > The NegCon 'twist' action is somewhat awkward when mapped
148// to a standard analog stick -> user should be able to tweak
149// response/deadzone for comfort
150// > When response is linear, 'additional' deadzone (set here)
151// may be left at zero, since this is normally handled via in-game
152// options menus
153// > When response is non-linear, deadzone should be set to match the
154// controller being used (otherwise precision may be lost)
155// > negcon_linearity:
156// - 1: Response is linear - recommended when using racing wheel
157// peripherals, not recommended for standard gamepads
158// - 2: Response is quadratic - optimal setting for gamepads
159// - 3: Response is cubic - enables precise fine control, but
160// difficult to use...
161#define NEGCON_RANGE 0x7FFF
162static int negcon_deadzone = 0;
163static int negcon_linearity = 1;
164
165static bool axis_bounds_modifier;
166
167/* PSX max resolution is 640x512, but with enhancement it's 1024x512 */
168#define VOUT_MAX_WIDTH 1024
169#define VOUT_MAX_HEIGHT 512
170
171//Dummy functions
172bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info) { return false; }
173void retro_unload_game(void) {}
174static int vout_open(void) { return 0; }
175static void vout_close(void) {}
176static int snd_init(void) { return 0; }
177static void snd_finish(void) {}
178static int snd_busy(void) { return 0; }
179
180#define GPU_PEOPS_ODD_EVEN_BIT (1 << 0)
181#define GPU_PEOPS_EXPAND_SCREEN_WIDTH (1 << 1)
182#define GPU_PEOPS_IGNORE_BRIGHTNESS (1 << 2)
183#define GPU_PEOPS_DISABLE_COORD_CHECK (1 << 3)
184#define GPU_PEOPS_LAZY_SCREEN_UPDATE (1 << 6)
185#define GPU_PEOPS_OLD_FRAME_SKIP (1 << 7)
186#define GPU_PEOPS_REPEATED_TRIANGLES (1 << 8)
187#define GPU_PEOPS_QUADS_WITH_TRIANGLES (1 << 9)
188#define GPU_PEOPS_FAKE_BUSY_STATE (1 << 10)
189
190static void init_memcard(char *mcd_data)
191{
192 unsigned off = 0;
193 unsigned i;
194
195 memset(mcd_data, 0, MCD_SIZE);
196
197 mcd_data[off++] = 'M';
198 mcd_data[off++] = 'C';
199 off += 0x7d;
200 mcd_data[off++] = 0x0e;
201
202 for (i = 0; i < 15; i++)
203 {
204 mcd_data[off++] = 0xa0;
205 off += 0x07;
206 mcd_data[off++] = 0xff;
207 mcd_data[off++] = 0xff;
208 off += 0x75;
209 mcd_data[off++] = 0xa0;
210 }
211
212 for (i = 0; i < 20; i++)
213 {
214 mcd_data[off++] = 0xff;
215 mcd_data[off++] = 0xff;
216 mcd_data[off++] = 0xff;
217 mcd_data[off++] = 0xff;
218 off += 0x04;
219 mcd_data[off++] = 0xff;
220 mcd_data[off++] = 0xff;
221 off += 0x76;
222 }
223}
224
225static void set_vout_fb()
226{
227 struct retro_framebuffer fb = { 0 };
228
229 fb.width = vout_width;
230 fb.height = vout_height;
231 fb.access_flags = RETRO_MEMORY_ACCESS_WRITE;
232
233 if (environ_cb(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &fb) && fb.format == RETRO_PIXEL_FORMAT_RGB565)
234 vout_buf_ptr = (uint16_t *)fb.data;
235 else
236 vout_buf_ptr = vout_buf;
237}
238
239static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
240{
241 vout_width = w;
242 vout_height = h;
243 psx_w = raw_w;
244 psx_h = raw_h;
245
246 if (previous_width != vout_width || previous_height != vout_height)
247 {
248 previous_width = vout_width;
249 previous_height = vout_height;
250
251 struct retro_system_av_info info;
252 retro_get_system_av_info(&info);
253 environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &info.geometry);
254 }
255
256 set_vout_fb();
257}
258
259#ifndef FRONTEND_SUPPORTS_RGB565
260static void convert(void *buf, size_t bytes)
261{
262 unsigned int i, v, *p = buf;
263
264 for (i = 0; i < bytes / 4; i++)
265 {
266 v = p[i];
267 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
268 }
269}
270#endif
271
272static void vout_flip(const void *vram, int stride, int bgr24,
273 int x, int y, int w, int h, int dims_changed)
274{
275 unsigned short *dest = vout_buf_ptr;
276 const unsigned short *src = vram;
277 int dstride = vout_width, h1 = h;
278
279 if (vram == NULL || dims_changed)
280 {
281 memset(vout_buf_ptr, 0, dstride * vout_height * 2);
282 // blanking
283 if (vram == NULL)
284 goto out;
285 }
286
287 dest += x + y * dstride;
288
289 if (bgr24)
290 {
291 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
292 for (; h1-- > 0; dest += dstride, src += stride)
293 {
294 bgr888_to_rgb565(dest, src, w * 3);
295 }
296 }
297 else
298 {
299 for (; h1-- > 0; dest += dstride, src += stride)
300 {
301 bgr555_to_rgb565(dest, src, w * 2);
302 }
303 }
304
305out:
306#ifndef FRONTEND_SUPPORTS_RGB565
307 convert(vout_buf_ptr, vout_width * vout_height * 2);
308#endif
309 vout_fb_dirty = 1;
310 pl_rearmed_cbs.flip_cnt++;
311}
312
313#ifdef _3DS
314typedef struct
315{
316 void *buffer;
317 uint32_t target_map;
318 size_t size;
319 enum psxMapTag tag;
320} psx_map_t;
321
322psx_map_t custom_psx_maps[] = {
323 { NULL, 0x13000000, 0x210000, MAP_TAG_RAM }, // 0x80000000
324 { NULL, 0x12800000, 0x010000, MAP_TAG_OTHER }, // 0x1f800000
325 { NULL, 0x12c00000, 0x080000, MAP_TAG_OTHER }, // 0x1fc00000
326 { NULL, 0x11000000, 0x800000, MAP_TAG_LUTS }, // 0x08000000
327 { NULL, 0x12000000, 0x200000, MAP_TAG_VRAM }, // 0x00000000
328};
329
330void *pl_3ds_mmap(unsigned long addr, size_t size, int is_fixed,
331 enum psxMapTag tag)
332{
333 (void)is_fixed;
334 (void)addr;
335
336 if (__ctr_svchax)
337 {
338 psx_map_t *custom_map = custom_psx_maps;
339
340 for (; custom_map->size; custom_map++)
341 {
342 if ((custom_map->size == size) && (custom_map->tag == tag))
343 {
344 uint32_t ptr_aligned, tmp;
345
346 custom_map->buffer = malloc(size + 0x1000);
347 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
348
349 if (svcControlMemory(&tmp, (void *)custom_map->target_map, (void *)ptr_aligned, size, MEMOP_MAP, 0x3) < 0)
350 {
351 SysPrintf("could not map memory @0x%08X\n", custom_map->target_map);
352 exit(1);
353 }
354
355 return (void *)custom_map->target_map;
356 }
357 }
358 }
359
360 return malloc(size);
361}
362
363void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
364{
365 (void)tag;
366
367 if (__ctr_svchax)
368 {
369 psx_map_t *custom_map = custom_psx_maps;
370
371 for (; custom_map->size; custom_map++)
372 {
373 if ((custom_map->target_map == (uint32_t)ptr))
374 {
375 uint32_t ptr_aligned, tmp;
376
377 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
378
379 svcControlMemory(&tmp, (void *)custom_map->target_map, (void *)ptr_aligned, size, MEMOP_UNMAP, 0x3);
380
381 free(custom_map->buffer);
382 custom_map->buffer = NULL;
383 return;
384 }
385 }
386 }
387
388 free(ptr);
389}
390#endif
391
392#ifdef VITA
393typedef struct
394{
395 void *buffer;
396 uint32_t target_map;
397 size_t size;
398 enum psxMapTag tag;
399} psx_map_t;
400
401void *addr = NULL;
402
403psx_map_t custom_psx_maps[] = {
404 { NULL, NULL, 0x210000, MAP_TAG_RAM }, // 0x80000000
405 { NULL, NULL, 0x010000, MAP_TAG_OTHER }, // 0x1f800000
406 { NULL, NULL, 0x080000, MAP_TAG_OTHER }, // 0x1fc00000
407 { NULL, NULL, 0x800000, MAP_TAG_LUTS }, // 0x08000000
408 { NULL, NULL, 0x200000, MAP_TAG_VRAM }, // 0x00000000
409};
410
411int init_vita_mmap()
412{
413 int n;
414 void *tmpaddr;
415 addr = malloc(64 * 1024 * 1024);
416 if (addr == NULL)
417 return -1;
418 tmpaddr = ((u32)(addr + 0xFFFFFF)) & ~0xFFFFFF;
419 custom_psx_maps[0].buffer = tmpaddr + 0x2000000;
420 custom_psx_maps[1].buffer = tmpaddr + 0x1800000;
421 custom_psx_maps[2].buffer = tmpaddr + 0x1c00000;
422 custom_psx_maps[3].buffer = tmpaddr + 0x0000000;
423 custom_psx_maps[4].buffer = tmpaddr + 0x1000000;
424#if 0
425 for(n = 0; n < 5; n++){
426 sceClibPrintf("addr reserved %x\n",custom_psx_maps[n].buffer);
427 }
428#endif
429 return 0;
430}
431
432void deinit_vita_mmap()
433{
434 free(addr);
435}
436
437void *pl_vita_mmap(unsigned long addr, size_t size, int is_fixed,
438 enum psxMapTag tag)
439{
440 (void)is_fixed;
441 (void)addr;
442
443 psx_map_t *custom_map = custom_psx_maps;
444
445 for (; custom_map->size; custom_map++)
446 {
447 if ((custom_map->size == size) && (custom_map->tag == tag))
448 {
449 return custom_map->buffer;
450 }
451 }
452
453 return malloc(size);
454}
455
456void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag)
457{
458 (void)tag;
459
460 psx_map_t *custom_map = custom_psx_maps;
461
462 for (; custom_map->size; custom_map++)
463 {
464 if ((custom_map->buffer == ptr))
465 {
466 return;
467 }
468 }
469
470 free(ptr);
471}
472#endif
473
474static void *pl_mmap(unsigned int size)
475{
476 return psxMap(0, size, 0, MAP_TAG_VRAM);
477}
478
479static void pl_munmap(void *ptr, unsigned int size)
480{
481 psxUnmap(ptr, size, MAP_TAG_VRAM);
482}
483
484struct rearmed_cbs pl_rearmed_cbs = {
485 .pl_vout_open = vout_open,
486 .pl_vout_set_mode = vout_set_mode,
487 .pl_vout_flip = vout_flip,
488 .pl_vout_close = vout_close,
489 .mmap = pl_mmap,
490 .munmap = pl_munmap,
491 /* from psxcounters */
492 .gpu_hcnt = &hSyncCount,
493 .gpu_frame_count = &frame_counter,
494};
495
496void pl_frame_limit(void)
497{
498 /* called once per frame, make psxCpu->Execute() above return */
499 stop++;
500}
501
502void pl_timing_prepare(int is_pal)
503{
504 is_pal_mode = is_pal;
505}
506
507void plat_trigger_vibrate(int pad, int low, int high)
508{
509 if (!rumble_cb)
510 return;
511
512 if (in_enable_vibration)
513 {
514 rumble_cb(pad, RETRO_RUMBLE_STRONG, high << 8);
515 rumble_cb(pad, RETRO_RUMBLE_WEAK, low ? 0xffff : 0x0);
516 }
517}
518
519void pl_gun_byte2(int port, unsigned char byte)
520{
521}
522
523/* sound calls */
524static void snd_feed(void *buf, int bytes)
525{
526 if (audio_batch_cb != NULL)
527 audio_batch_cb(buf, bytes / 4);
528}
529
530void out_register_libretro(struct out_driver *drv)
531{
532 drv->name = "libretro";
533 drv->init = snd_init;
534 drv->finish = snd_finish;
535 drv->busy = snd_busy;
536 drv->feed = snd_feed;
537}
538
539#define RETRO_DEVICE_PSE_STANDARD RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0)
540#define RETRO_DEVICE_PSE_ANALOG RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 0)
541#define RETRO_DEVICE_PSE_DUALSHOCK RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 1)
542#define RETRO_DEVICE_PSE_NEGCON RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 2)
543#define RETRO_DEVICE_PSE_GUNCON RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_LIGHTGUN, 0)
544#define RETRO_DEVICE_PSE_MOUSE RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_MOUSE, 0)
545
546static char *get_pse_pad_label[] = {
547 "none", "mouse", "negcon", "konami gun", "standard", "analog", "guncon", "dualshock"
548};
549
550static const struct retro_controller_description pads[7] =
551{
552 { "standard", RETRO_DEVICE_JOYPAD },
553 { "analog", RETRO_DEVICE_PSE_ANALOG },
554 { "dualshock", RETRO_DEVICE_PSE_DUALSHOCK },
555 { "negcon", RETRO_DEVICE_PSE_NEGCON },
556 { "guncon", RETRO_DEVICE_PSE_GUNCON },
557 { "mouse", RETRO_DEVICE_PSE_MOUSE },
558 { NULL, 0 },
559};
560
561static const struct retro_controller_info ports[9] =
562{
563 { pads, 7 },
564 { pads, 7 },
565 { pads, 7 },
566 { pads, 7 },
567 { pads, 7 },
568 { pads, 7 },
569 { pads, 7 },
570 { pads, 7 },
571 { NULL, 0 },
572};
573
574/* libretro */
575
576static bool update_option_visibility(void)
577{
578 struct retro_variable var = {0};
579 struct retro_core_option_display option_display = {0};
580 bool updated = false;
581 unsigned i;
582
583 /* If frontend supports core option categories
584 * then show/hide core option entries are ignored
585 * and no options should be hidden */
586 if (libretro_supports_option_categories)
587 return false;
588
589 var.key = "pcsx_rearmed_show_input_settings";
590 var.value = NULL;
591
592 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
593 {
594 bool show_input_settings_prev =
595 show_input_settings;
596
597 show_input_settings = true;
598 if (strcmp(var.value, "disabled") == 0)
599 show_input_settings = false;
600
601 if (show_input_settings !=
602 show_input_settings_prev)
603 {
604 char input_option[][50] = {
605 "pcsx_rearmed_analog_axis_modifier",
606 "pcsx_rearmed_vibration",
607 "pcsx_rearmed_multitap",
608 "pcsx_rearmed_negcon_deadzone",
609 "pcsx_rearmed_negcon_response",
610 "pcsx_rearmed_input_sensitivity",
611 "pcsx_rearmed_gunconadjustx",
612 "pcsx_rearmed_gunconadjusty",
613 "pcsx_rearmed_gunconadjustratiox",
614 "pcsx_rearmed_gunconadjustratioy"
615 };
616
617 option_display.visible = show_input_settings;
618
619 for (i = 0;
620 i < (sizeof(input_option) /
621 sizeof(input_option[0]));
622 i++)
623 {
624 option_display.key = input_option[i];
625 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
626 &option_display);
627 }
628
629 updated = true;
630 }
631 }
632#ifdef GPU_PEOPS
633 var.key = "pcsx_rearmed_show_gpu_peops_settings";
634 var.value = NULL;
635
636 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
637 {
638 bool show_advanced_gpu_peops_settings_prev =
639 show_advanced_gpu_peops_settings;
640
641 show_advanced_gpu_peops_settings = true;
642 if (strcmp(var.value, "disabled") == 0)
643 show_advanced_gpu_peops_settings = false;
644
645 if (show_advanced_gpu_peops_settings !=
646 show_advanced_gpu_peops_settings_prev)
647 {
648 unsigned i;
649 struct retro_core_option_display option_display;
650 char gpu_peops_option[][45] = {
651 "pcsx_rearmed_gpu_peops_odd_even_bit",
652 "pcsx_rearmed_gpu_peops_expand_screen_width",
653 "pcsx_rearmed_gpu_peops_ignore_brightness",
654 "pcsx_rearmed_gpu_peops_disable_coord_check",
655 "pcsx_rearmed_gpu_peops_lazy_screen_update",
656 "pcsx_rearmed_gpu_peops_repeated_triangles",
657 "pcsx_rearmed_gpu_peops_quads_with_triangles",
658 "pcsx_rearmed_gpu_peops_fake_busy_state"
659 };
660
661 option_display.visible = show_advanced_gpu_peops_settings;
662
663 for (i = 0;
664 i < (sizeof(gpu_peops_option) /
665 sizeof(gpu_peops_option[0]));
666 i++)
667 {
668 option_display.key = gpu_peops_option[i];
669 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
670 &option_display);
671 }
672
673 updated = true;
674 }
675 }
676#endif
677#ifdef GPU_UNAI
678 var.key = "pcsx_rearmed_show_gpu_unai_settings";
679 var.value = NULL;
680
681 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
682 {
683 bool show_advanced_gpu_unai_settings_prev =
684 show_advanced_gpu_unai_settings;
685
686 show_advanced_gpu_unai_settings = true;
687 if (strcmp(var.value, "disabled") == 0)
688 show_advanced_gpu_unai_settings = false;
689
690 if (show_advanced_gpu_unai_settings !=
691 show_advanced_gpu_unai_settings_prev)
692 {
693 unsigned i;
694 struct retro_core_option_display option_display;
695 char gpu_unai_option[][40] = {
696 "pcsx_rearmed_gpu_unai_blending",
697 "pcsx_rearmed_gpu_unai_lighting",
698 "pcsx_rearmed_gpu_unai_fast_lighting",
699 "pcsx_rearmed_gpu_unai_scale_hires",
700 };
701
702 option_display.visible = show_advanced_gpu_unai_settings;
703
704 for (i = 0;
705 i < (sizeof(gpu_unai_option) /
706 sizeof(gpu_unai_option[0]));
707 i++)
708 {
709 option_display.key = gpu_unai_option[i];
710 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
711 &option_display);
712 }
713
714 updated = true;
715 }
716 }
717#endif
718 return updated;
719}
720
721void retro_set_environment(retro_environment_t cb)
722{
723 bool option_categories = false;
724#ifdef USE_LIBRETRO_VFS
725 struct retro_vfs_interface_info vfs_iface_info;
726#endif
727
728 environ_cb = cb;
729
730 if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
731 log_cb = logging.log;
732
733 environ_cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
734
735 /* Set core options
736 * An annoyance: retro_set_environment() can be called
737 * multiple times, and depending upon the current frontend
738 * state various environment callbacks may be disabled.
739 * This means the reported 'categories_supported' status
740 * may change on subsequent iterations. We therefore have
741 * to record whether 'categories_supported' is true on any
742 * iteration, and latch the result */
743 libretro_set_core_options(environ_cb, &option_categories);
744 libretro_supports_option_categories |= option_categories;
745
746 /* If frontend supports core option categories,
747 * any show/hide core option entries are unused
748 * and should be hidden */
749 if (libretro_supports_option_categories)
750 {
751 struct retro_core_option_display option_display;
752 option_display.visible = false;
753
754 option_display.key = "pcsx_rearmed_show_input_settings";
755 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
756 &option_display);
757
758#ifdef GPU_PEOPS
759 option_display.key = "pcsx_rearmed_show_gpu_peops_settings";
760 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
761 &option_display);
762#endif
763#ifdef GPU_UNAI
764 option_display.key = "pcsx_rearmed_show_gpu_unai_settings";
765 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_DISPLAY,
766 &option_display);
767#endif
768 }
769 /* If frontend does not support core option
770 * categories, core options may be shown/hidden
771 * at runtime. In this case, register 'update
772 * display' callback, so frontend can update
773 * core options menu without calling retro_run() */
774 else
775 {
776 struct retro_core_options_update_display_callback update_display_cb;
777 update_display_cb.callback = update_option_visibility;
778
779 environ_cb(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_UPDATE_DISPLAY_CALLBACK,
780 &update_display_cb);
781 }
782
783#ifdef USE_LIBRETRO_VFS
784 vfs_iface_info.required_interface_version = 1;
785 vfs_iface_info.iface = NULL;
786 if (environ_cb(RETRO_ENVIRONMENT_GET_VFS_INTERFACE, &vfs_iface_info))
787 filestream_vfs_init(&vfs_iface_info);
788#endif
789}
790
791void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
792void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
793void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
794void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
795void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
796
797unsigned retro_api_version(void)
798{
799 return RETRO_API_VERSION;
800}
801
802static void update_multitap(void)
803{
804 struct retro_variable var = { 0 };
805
806 multitap1 = 0;
807 multitap2 = 0;
808
809 var.value = NULL;
810 var.key = "pcsx_rearmed_multitap";
811 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value))
812 {
813 if (strcmp(var.value, "port 1") == 0)
814 multitap1 = 1;
815 else if (strcmp(var.value, "port 2") == 0)
816 multitap2 = 1;
817 else if (strcmp(var.value, "ports 1 and 2") == 0)
818 {
819 multitap1 = 1;
820 multitap2 = 1;
821 }
822 }
823}
824
825void retro_set_controller_port_device(unsigned port, unsigned device)
826{
827 if (port >= PORTS_NUMBER)
828 return;
829
830 switch (device)
831 {
832 case RETRO_DEVICE_JOYPAD:
833 case RETRO_DEVICE_PSE_STANDARD:
834 in_type[port] = PSE_PAD_TYPE_STANDARD;
835 break;
836 case RETRO_DEVICE_PSE_ANALOG:
837 in_type[port] = PSE_PAD_TYPE_ANALOGJOY;
838 break;
839 case RETRO_DEVICE_PSE_DUALSHOCK:
840 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
841 break;
842 case RETRO_DEVICE_PSE_MOUSE:
843 in_type[port] = PSE_PAD_TYPE_MOUSE;
844 break;
845 case RETRO_DEVICE_PSE_NEGCON:
846 in_type[port] = PSE_PAD_TYPE_NEGCON;
847 break;
848 case RETRO_DEVICE_PSE_GUNCON:
849 in_type[port] = PSE_PAD_TYPE_GUNCON;
850 break;
851 case RETRO_DEVICE_NONE:
852 default:
853 in_type[port] = PSE_PAD_TYPE_NONE;
854 break;
855 }
856
857 SysPrintf("port: %u device: %s\n", port + 1, get_pse_pad_label[in_type[port]]);
858}
859
860void retro_get_system_info(struct retro_system_info *info)
861{
862#ifndef GIT_VERSION
863#define GIT_VERSION ""
864#endif
865 memset(info, 0, sizeof(*info));
866 info->library_name = "PCSX-ReARMed";
867 info->library_version = "r23l" GIT_VERSION;
868 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u|chd|iso|exe";
869 info->need_fullpath = true;
870}
871
872void retro_get_system_av_info(struct retro_system_av_info *info)
873{
874 unsigned geom_height = vout_height > 0 ? vout_height : 240;
875 unsigned geom_width = vout_width > 0 ? vout_width : 320;
876
877 memset(info, 0, sizeof(*info));
878 info->timing.fps = is_pal_mode ? 50.0 : 60.0;
879 info->timing.sample_rate = 44100.0;
880 info->geometry.base_width = geom_width;
881 info->geometry.base_height = geom_height;
882 info->geometry.max_width = VOUT_MAX_WIDTH;
883 info->geometry.max_height = VOUT_MAX_HEIGHT;
884 info->geometry.aspect_ratio = 4.0 / 3.0;
885}
886
887/* savestates */
888size_t retro_serialize_size(void)
889{
890 // it's currently 4380651-4397047 bytes,
891 // but have some reserved for future
892 return 0x440000;
893}
894
895struct save_fp
896{
897 char *buf;
898 size_t pos;
899 int is_write;
900};
901
902static void *save_open(const char *name, const char *mode)
903{
904 struct save_fp *fp;
905
906 if (name == NULL || mode == NULL)
907 return NULL;
908
909 fp = malloc(sizeof(*fp));
910 if (fp == NULL)
911 return NULL;
912
913 fp->buf = (char *)name;
914 fp->pos = 0;
915 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
916
917 return fp;
918}
919
920static int save_read(void *file, void *buf, u32 len)
921{
922 struct save_fp *fp = file;
923 if (fp == NULL || buf == NULL)
924 return -1;
925
926 memcpy(buf, fp->buf + fp->pos, len);
927 fp->pos += len;
928 return len;
929}
930
931static int save_write(void *file, const void *buf, u32 len)
932{
933 struct save_fp *fp = file;
934 if (fp == NULL || buf == NULL)
935 return -1;
936
937 memcpy(fp->buf + fp->pos, buf, len);
938 fp->pos += len;
939 return len;
940}
941
942static long save_seek(void *file, long offs, int whence)
943{
944 struct save_fp *fp = file;
945 if (fp == NULL)
946 return -1;
947
948 switch (whence)
949 {
950 case SEEK_CUR:
951 fp->pos += offs;
952 return fp->pos;
953 case SEEK_SET:
954 fp->pos = offs;
955 return fp->pos;
956 default:
957 return -1;
958 }
959}
960
961static void save_close(void *file)
962{
963 struct save_fp *fp = file;
964 size_t r_size = retro_serialize_size();
965 if (fp == NULL)
966 return;
967
968 if (fp->pos > r_size)
969 SysPrintf("ERROR: save buffer overflow detected\n");
970 else if (fp->is_write && fp->pos < r_size)
971 // make sure we don't save trash in leftover space
972 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
973 free(fp);
974}
975
976bool retro_serialize(void *data, size_t size)
977{
978 int ret = SaveState(data);
979 return ret == 0 ? true : false;
980}
981
982bool retro_unserialize(const void *data, size_t size)
983{
984 int ret = LoadState(data);
985 return ret == 0 ? true : false;
986}
987
988/* cheats */
989void retro_cheat_reset(void)
990{
991 ClearAllCheats();
992}
993
994void retro_cheat_set(unsigned index, bool enabled, const char *code)
995{
996 int ret = -1;
997 char *buf;
998
999 // cheat funcs are destructive, need a copy...
1000 buf = strdup(code);
1001 if (buf == NULL)
1002 goto finish;
1003
1004 //Prepare buffered cheat for PCSX's AddCheat fucntion.
1005 int cursor = 0;
1006 int nonhexdec = 0;
1007 while (buf[cursor])
1008 {
1009 if (!(ISHEXDEC))
1010 {
1011 if (++nonhexdec % 2)
1012 {
1013 buf[cursor] = ' ';
1014 }
1015 else
1016 {
1017 buf[cursor] = '\n';
1018 }
1019 }
1020 cursor++;
1021 }
1022
1023 if (index < NumCheats)
1024 ret = EditCheat(index, "", buf);
1025 else
1026 ret = AddCheat("", buf);
1027
1028finish:
1029 if (ret != 0)
1030 SysPrintf("Failed to set cheat %#u\n", index);
1031 else if (index < NumCheats)
1032 Cheats[index].Enabled = enabled;
1033 free(buf);
1034}
1035
1036// just in case, maybe a win-rt port in the future?
1037#ifdef _WIN32
1038#define SLASH '\\'
1039#else
1040#define SLASH '/'
1041#endif
1042
1043#ifndef PATH_MAX
1044#define PATH_MAX 4096
1045#endif
1046
1047/* multidisk support */
1048static unsigned int disk_initial_index;
1049static char disk_initial_path[PATH_MAX];
1050static bool disk_ejected;
1051static unsigned int disk_current_index;
1052static unsigned int disk_count;
1053static struct disks_state
1054{
1055 char *fname;
1056 char *flabel;
1057 int internal_index; // for multidisk eboots
1058} disks[8];
1059
1060static void get_disk_label(char *disk_label, const char *disk_path, size_t len)
1061{
1062 const char *base = NULL;
1063
1064 if (!disk_path || (*disk_path == '\0'))
1065 return;
1066
1067 base = strrchr(disk_path, SLASH);
1068 if (!base)
1069 base = disk_path;
1070
1071 if (*base == SLASH)
1072 base++;
1073
1074 strncpy(disk_label, base, len - 1);
1075 disk_label[len - 1] = '\0';
1076
1077 char *ext = strrchr(disk_label, '.');
1078 if (ext)
1079 *ext = '\0';
1080}
1081
1082static void disk_init(void)
1083{
1084 size_t i;
1085
1086 disk_ejected = false;
1087 disk_current_index = 0;
1088 disk_count = 0;
1089
1090 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++)
1091 {
1092 if (disks[i].fname != NULL)
1093 {
1094 free(disks[i].fname);
1095 disks[i].fname = NULL;
1096 }
1097 if (disks[i].flabel != NULL)
1098 {
1099 free(disks[i].flabel);
1100 disks[i].flabel = NULL;
1101 }
1102 disks[i].internal_index = 0;
1103 }
1104}
1105
1106static bool disk_set_eject_state(bool ejected)
1107{
1108 // weird PCSX API..
1109 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
1110 LidInterrupt();
1111
1112 disk_ejected = ejected;
1113 return true;
1114}
1115
1116static bool disk_get_eject_state(void)
1117{
1118 /* can't be controlled by emulated software */
1119 return disk_ejected;
1120}
1121
1122static unsigned int disk_get_image_index(void)
1123{
1124 return disk_current_index;
1125}
1126
1127static bool disk_set_image_index(unsigned int index)
1128{
1129 if (index >= sizeof(disks) / sizeof(disks[0]))
1130 return false;
1131
1132 CdromId[0] = '\0';
1133 CdromLabel[0] = '\0';
1134
1135 if (disks[index].fname == NULL)
1136 {
1137 SysPrintf("missing disk #%u\n", index);
1138 CDR_shutdown();
1139
1140 // RetroArch specifies "no disk" with index == count,
1141 // so don't fail here..
1142 disk_current_index = index;
1143 return true;
1144 }
1145
1146 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
1147 disks[index].fname, disks[index].internal_index);
1148
1149 cdrIsoMultidiskSelect = disks[index].internal_index;
1150 set_cd_image(disks[index].fname);
1151 if (ReloadCdromPlugin() < 0)
1152 {
1153 SysPrintf("failed to load cdr plugin\n");
1154 return false;
1155 }
1156 if (CDR_open() < 0)
1157 {
1158 SysPrintf("failed to open cdr plugin\n");
1159 return false;
1160 }
1161
1162 if (!disk_ejected)
1163 {
1164 SetCdOpenCaseTime(time(NULL) + 2);
1165 LidInterrupt();
1166 }
1167
1168 disk_current_index = index;
1169 return true;
1170}
1171
1172static unsigned int disk_get_num_images(void)
1173{
1174 return disk_count;
1175}
1176
1177static bool disk_replace_image_index(unsigned index,
1178 const struct retro_game_info *info)
1179{
1180 char *old_fname = NULL;
1181 char *old_flabel = NULL;
1182 bool ret = true;
1183
1184 if (index >= sizeof(disks) / sizeof(disks[0]))
1185 return false;
1186
1187 old_fname = disks[index].fname;
1188 old_flabel = disks[index].flabel;
1189
1190 disks[index].fname = NULL;
1191 disks[index].flabel = NULL;
1192 disks[index].internal_index = 0;
1193
1194 if (info != NULL)
1195 {
1196 char disk_label[PATH_MAX];
1197 disk_label[0] = '\0';
1198
1199 disks[index].fname = strdup(info->path);
1200
1201 get_disk_label(disk_label, info->path, PATH_MAX);
1202 disks[index].flabel = strdup(disk_label);
1203
1204 if (index == disk_current_index)
1205 ret = disk_set_image_index(index);
1206 }
1207
1208 if (old_fname != NULL)
1209 free(old_fname);
1210
1211 if (old_flabel != NULL)
1212 free(old_flabel);
1213
1214 return ret;
1215}
1216
1217static bool disk_add_image_index(void)
1218{
1219 if (disk_count >= 8)
1220 return false;
1221
1222 disk_count++;
1223 return true;
1224}
1225
1226static bool disk_set_initial_image(unsigned index, const char *path)
1227{
1228 if (index >= sizeof(disks) / sizeof(disks[0]))
1229 return false;
1230
1231 if (!path || (*path == '\0'))
1232 return false;
1233
1234 disk_initial_index = index;
1235
1236 strncpy(disk_initial_path, path, sizeof(disk_initial_path) - 1);
1237 disk_initial_path[sizeof(disk_initial_path) - 1] = '\0';
1238
1239 return true;
1240}
1241
1242static bool disk_get_image_path(unsigned index, char *path, size_t len)
1243{
1244 const char *fname = NULL;
1245
1246 if (len < 1)
1247 return false;
1248
1249 if (index >= sizeof(disks) / sizeof(disks[0]))
1250 return false;
1251
1252 fname = disks[index].fname;
1253
1254 if (!fname || (*fname == '\0'))
1255 return false;
1256
1257 strncpy(path, fname, len - 1);
1258 path[len - 1] = '\0';
1259
1260 return true;
1261}
1262
1263static bool disk_get_image_label(unsigned index, char *label, size_t len)
1264{
1265 const char *flabel = NULL;
1266
1267 if (len < 1)
1268 return false;
1269
1270 if (index >= sizeof(disks) / sizeof(disks[0]))
1271 return false;
1272
1273 flabel = disks[index].flabel;
1274
1275 if (!flabel || (*flabel == '\0'))
1276 return false;
1277
1278 strncpy(label, flabel, len - 1);
1279 label[len - 1] = '\0';
1280
1281 return true;
1282}
1283
1284static struct retro_disk_control_callback disk_control = {
1285 .set_eject_state = disk_set_eject_state,
1286 .get_eject_state = disk_get_eject_state,
1287 .get_image_index = disk_get_image_index,
1288 .set_image_index = disk_set_image_index,
1289 .get_num_images = disk_get_num_images,
1290 .replace_image_index = disk_replace_image_index,
1291 .add_image_index = disk_add_image_index,
1292};
1293
1294static struct retro_disk_control_ext_callback disk_control_ext = {
1295 .set_eject_state = disk_set_eject_state,
1296 .get_eject_state = disk_get_eject_state,
1297 .get_image_index = disk_get_image_index,
1298 .set_image_index = disk_set_image_index,
1299 .get_num_images = disk_get_num_images,
1300 .replace_image_index = disk_replace_image_index,
1301 .add_image_index = disk_add_image_index,
1302 .set_initial_image = disk_set_initial_image,
1303 .get_image_path = disk_get_image_path,
1304 .get_image_label = disk_get_image_label,
1305};
1306
1307static char base_dir[1024];
1308
1309static bool read_m3u(const char *file)
1310{
1311 char line[1024];
1312 char name[PATH_MAX];
1313 FILE *fp = fopen(file, "r");
1314 if (!fp)
1315 return false;
1316
1317 while (fgets(line, sizeof(line), fp) && disk_count < sizeof(disks) / sizeof(disks[0]))
1318 {
1319 if (line[0] == '#')
1320 continue;
1321 char *carrige_return = strchr(line, '\r');
1322 if (carrige_return)
1323 *carrige_return = '\0';
1324 char *newline = strchr(line, '\n');
1325 if (newline)
1326 *newline = '\0';
1327
1328 if (line[0] != '\0')
1329 {
1330 char disk_label[PATH_MAX];
1331 disk_label[0] = '\0';
1332
1333 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
1334 disks[disk_count].fname = strdup(name);
1335
1336 get_disk_label(disk_label, name, PATH_MAX);
1337 disks[disk_count].flabel = strdup(disk_label);
1338
1339 disk_count++;
1340 }
1341 }
1342
1343 fclose(fp);
1344 return (disk_count != 0);
1345}
1346
1347static void extract_directory(char *buf, const char *path, size_t size)
1348{
1349 char *base;
1350 strncpy(buf, path, size - 1);
1351 buf[size - 1] = '\0';
1352
1353 base = strrchr(buf, '/');
1354 if (!base)
1355 base = strrchr(buf, '\\');
1356
1357 if (base)
1358 *base = '\0';
1359 else
1360 {
1361 buf[0] = '.';
1362 buf[1] = '\0';
1363 }
1364}
1365
1366#if defined(__QNX__) || defined(_WIN32)
1367/* Blackberry QNX doesn't have strcasestr */
1368
1369/*
1370 * Find the first occurrence of find in s, ignore case.
1371 */
1372char *
1373strcasestr(const char *s, const char *find)
1374{
1375 char c, sc;
1376 size_t len;
1377
1378 if ((c = *find++) != 0)
1379 {
1380 c = tolower((unsigned char)c);
1381 len = strlen(find);
1382 do
1383 {
1384 do
1385 {
1386 if ((sc = *s++) == 0)
1387 return (NULL);
1388 } while ((char)tolower((unsigned char)sc) != c);
1389 } while (strncasecmp(s, find, len) != 0);
1390 s--;
1391 }
1392 return ((char *)s);
1393}
1394#endif
1395
1396static void set_retro_memmap(void)
1397{
1398#ifndef NDEBUG
1399 struct retro_memory_map retromap = { 0 };
1400 struct retro_memory_descriptor mmap = {
1401 0, psxM, 0, 0, 0, 0, 0x200000
1402 };
1403
1404 retromap.descriptors = &mmap;
1405 retromap.num_descriptors = 1;
1406
1407 environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &retromap);
1408#endif
1409}
1410
1411static void retro_audio_buff_status_cb(
1412 bool active, unsigned occupancy, bool underrun_likely)
1413{
1414 retro_audio_buff_active = active;
1415 retro_audio_buff_occupancy = occupancy;
1416 retro_audio_buff_underrun = underrun_likely;
1417}
1418
1419static void retro_set_audio_buff_status_cb(void)
1420{
1421 if (frameskip_type == FRAMESKIP_NONE)
1422 {
1423 environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, NULL);
1424 retro_audio_latency = 0;
1425 }
1426 else
1427 {
1428 bool calculate_audio_latency = true;
1429
1430 if (frameskip_type == FRAMESKIP_FIXED_INTERVAL)
1431 environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, NULL);
1432 else
1433 {
1434 struct retro_audio_buffer_status_callback buf_status_cb;
1435 buf_status_cb.callback = retro_audio_buff_status_cb;
1436 if (!environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK,
1437 &buf_status_cb))
1438 {
1439 retro_audio_buff_active = false;
1440 retro_audio_buff_occupancy = 0;
1441 retro_audio_buff_underrun = false;
1442 retro_audio_latency = 0;
1443 calculate_audio_latency = false;
1444 }
1445 }
1446
1447 if (calculate_audio_latency)
1448 {
1449 /* Frameskip is enabled - increase frontend
1450 * audio latency to minimise potential
1451 * buffer underruns */
1452 uint32_t frame_time_usec = 1000000.0 / (is_pal_mode ? 50.0 : 60.0);
1453
1454 /* Set latency to 6x current frame time... */
1455 retro_audio_latency = (unsigned)(6 * frame_time_usec / 1000);
1456
1457 /* ...then round up to nearest multiple of 32 */
1458 retro_audio_latency = (retro_audio_latency + 0x1F) & ~0x1F;
1459 }
1460 }
1461
1462 update_audio_latency = true;
1463 frameskip_counter = 0;
1464}
1465
1466static void update_variables(bool in_flight);
1467bool retro_load_game(const struct retro_game_info *info)
1468{
1469 size_t i;
1470 unsigned int cd_index = 0;
1471 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
1472 bool is_exe = (strcasestr(info->path, ".exe") != NULL);
1473 int ret;
1474
1475 struct retro_input_descriptor desc[] = {
1476#define JOYP(port) \
1477 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" }, \
1478 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" }, \
1479 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" }, \
1480 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" }, \
1481 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" }, \
1482 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" }, \
1483 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" }, \
1484 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" }, \
1485 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" }, \
1486 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" }, \
1487 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" }, \
1488 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" }, \
1489 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" }, \
1490 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" }, \
1491 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" }, \
1492 { port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, \
1493 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" }, \
1494 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" }, \
1495 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" }, \
1496 { port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" }, \
1497 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_TRIGGER, "Gun Trigger" }, \
1498 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_RELOAD, "Gun Reload" }, \
1499 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A, "Gun Aux A" }, \
1500 { port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_B, "Gun Aux B" },
1501
1502 JOYP(0)
1503 JOYP(1)
1504 JOYP(2)
1505 JOYP(3)
1506 JOYP(4)
1507 JOYP(5)
1508 JOYP(6)
1509 JOYP(7)
1510
1511 { 0 },
1512 };
1513
1514 frame_count = 0;
1515
1516 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1517
1518#ifdef FRONTEND_SUPPORTS_RGB565
1519 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
1520 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
1521 {
1522 SysPrintf("RGB565 supported, using it\n");
1523 }
1524#endif
1525
1526 if (info == NULL || info->path == NULL)
1527 {
1528 SysPrintf("info->path required\n");
1529 return false;
1530 }
1531
1532 update_variables(false);
1533
1534 if (plugins_opened)
1535 {
1536 ClosePlugins();
1537 plugins_opened = 0;
1538 }
1539
1540 disk_init();
1541
1542 extract_directory(base_dir, info->path, sizeof(base_dir));
1543
1544 if (is_m3u)
1545 {
1546 if (!read_m3u(info->path))
1547 {
1548 log_cb(RETRO_LOG_INFO, "failed to read m3u file\n");
1549 return false;
1550 }
1551 }
1552 else
1553 {
1554 char disk_label[PATH_MAX];
1555 disk_label[0] = '\0';
1556
1557 disk_count = 1;
1558 disks[0].fname = strdup(info->path);
1559
1560 get_disk_label(disk_label, info->path, PATH_MAX);
1561 disks[0].flabel = strdup(disk_label);
1562 }
1563
1564 /* If this is an M3U file, attempt to set the
1565 * initial disk image */
1566 if (is_m3u && (disk_initial_index > 0) && (disk_initial_index < disk_count))
1567 {
1568 const char *fname = disks[disk_initial_index].fname;
1569
1570 if (fname && (*fname != '\0'))
1571 if (strcmp(disk_initial_path, fname) == 0)
1572 cd_index = disk_initial_index;
1573 }
1574
1575 set_cd_image(disks[cd_index].fname);
1576 disk_current_index = cd_index;
1577
1578 /* have to reload after set_cd_image for correct cdr plugin */
1579 if (LoadPlugins() == -1)
1580 {
1581 log_cb(RETRO_LOG_INFO, "failed to load plugins\n");
1582 return false;
1583 }
1584
1585 plugins_opened = 1;
1586 NetOpened = 0;
1587
1588 if (OpenPlugins() == -1)
1589 {
1590 log_cb(RETRO_LOG_INFO, "failed to open plugins\n");
1591 return false;
1592 }
1593
1594 /* Handle multi-disk images (i.e. PBP)
1595 * > Cannot do this until after OpenPlugins() is
1596 * called (since this sets the value of
1597 * cdrIsoMultidiskCount) */
1598 if (!is_m3u && (cdrIsoMultidiskCount > 1))
1599 {
1600 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
1601
1602 /* Small annoyance: We need to change the label
1603 * of disk 0, so have to clear existing entries */
1604 if (disks[0].fname != NULL)
1605 free(disks[0].fname);
1606 disks[0].fname = NULL;
1607
1608 if (disks[0].flabel != NULL)
1609 free(disks[0].flabel);
1610 disks[0].flabel = NULL;
1611
1612 for (i = 0; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++)
1613 {
1614 char disk_name[PATH_MAX - 16] = { 0 };
1615 char disk_label[PATH_MAX] = { 0 };
1616
1617 disks[i].fname = strdup(info->path);
1618
1619 get_disk_label(disk_name, info->path, sizeof(disk_name));
1620 snprintf(disk_label, sizeof(disk_label), "%s #%u", disk_name, (unsigned)i + 1);
1621 disks[i].flabel = strdup(disk_label);
1622
1623 disks[i].internal_index = i;
1624 }
1625
1626 /* This is not an M3U file, so initial disk
1627 * image has not yet been set - attempt to
1628 * do so now */
1629 if ((disk_initial_index > 0) && (disk_initial_index < disk_count))
1630 {
1631 const char *fname = disks[disk_initial_index].fname;
1632
1633 if (fname && (*fname != '\0'))
1634 if (strcmp(disk_initial_path, fname) == 0)
1635 cd_index = disk_initial_index;
1636 }
1637
1638 if (cd_index > 0)
1639 {
1640 CdromId[0] = '\0';
1641 CdromLabel[0] = '\0';
1642
1643 cdrIsoMultidiskSelect = disks[cd_index].internal_index;
1644 disk_current_index = cd_index;
1645 set_cd_image(disks[cd_index].fname);
1646
1647 if (ReloadCdromPlugin() < 0)
1648 {
1649 log_cb(RETRO_LOG_INFO, "failed to reload cdr plugins\n");
1650 return false;
1651 }
1652 if (CDR_open() < 0)
1653 {
1654 log_cb(RETRO_LOG_INFO, "failed to open cdr plugin\n");
1655 return false;
1656 }
1657 }
1658 }
1659
1660 /* set ports to use "standard controller" initially */
1661 for (i = 0; i < 8; ++i)
1662 in_type[i] = PSE_PAD_TYPE_STANDARD;
1663
1664 plugin_call_rearmed_cbs();
1665 /* dfinput_activate(); */
1666
1667 if (!is_exe && CheckCdrom() == -1)
1668 {
1669 log_cb(RETRO_LOG_INFO, "unsupported/invalid CD image: %s\n", info->path);
1670 return false;
1671 }
1672
1673 SysReset();
1674
1675 if (is_exe)
1676 ret = Load(info->path);
1677 else
1678 ret = LoadCdrom();
1679 if (ret != 0)
1680 {
1681 log_cb(RETRO_LOG_INFO, "could not load %s (%d)\n", is_exe ? "exe" : "CD", ret);
1682 return false;
1683 }
1684 emu_on_new_cd(0);
1685
1686 set_retro_memmap();
1687 retro_set_audio_buff_status_cb();
1688
1689 return true;
1690}
1691
1692unsigned retro_get_region(void)
1693{
1694 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1695}
1696
1697void *retro_get_memory_data(unsigned id)
1698{
1699 if (id == RETRO_MEMORY_SAVE_RAM)
1700 return Mcd1Data;
1701 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1702 return psxM;
1703 else
1704 return NULL;
1705}
1706
1707size_t retro_get_memory_size(unsigned id)
1708{
1709 if (id == RETRO_MEMORY_SAVE_RAM)
1710 return MCD_SIZE;
1711 else if (id == RETRO_MEMORY_SYSTEM_RAM)
1712 return 0x200000;
1713 else
1714 return 0;
1715}
1716
1717void retro_reset(void)
1718{
1719 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
1720 rebootemu = 1;
1721 //SysReset();
1722}
1723
1724static const unsigned short retro_psx_map[] = {
1725 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
1726 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
1727 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
1728 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
1729 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
1730 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
1731 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
1732 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
1733 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
1734 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
1735 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
1736 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
1737 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
1738 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
1739 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
1740 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
1741};
1742#define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
1743
1744//Percentage distance of screen to adjust
1745static int GunconAdjustX = 0;
1746static int GunconAdjustY = 0;
1747
1748//Used when out by a percentage
1749static float GunconAdjustRatioX = 1;
1750static float GunconAdjustRatioY = 1;
1751
1752static void update_variables(bool in_flight)
1753{
1754 struct retro_variable var;
1755#ifdef GPU_PEOPS
1756 // Always enable GPU_PEOPS_OLD_FRAME_SKIP flag
1757 // (this is set in standalone, with no option
1758 // to change it)
1759 int gpu_peops_fix = GPU_PEOPS_OLD_FRAME_SKIP;
1760#endif
1761 frameskip_type_t prev_frameskip_type;
1762
1763 var.value = NULL;
1764 var.key = "pcsx_rearmed_frameskip_type";
1765
1766 prev_frameskip_type = frameskip_type;
1767 frameskip_type = FRAMESKIP_NONE;
1768 pl_rearmed_cbs.frameskip = 0;
1769
1770 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1771 {
1772 if (strcmp(var.value, "auto") == 0)
1773 frameskip_type = FRAMESKIP_AUTO;
1774 if (strcmp(var.value, "auto_threshold") == 0)
1775 frameskip_type = FRAMESKIP_AUTO_THRESHOLD;
1776 if (strcmp(var.value, "fixed_interval") == 0)
1777 frameskip_type = FRAMESKIP_FIXED_INTERVAL;
1778 }
1779
1780 if (frameskip_type != 0)
1781 pl_rearmed_cbs.frameskip = -1;
1782
1783 var.value = NULL;
1784 var.key = "pcsx_rearmed_frameskip_threshold";
1785 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1786 {
1787 frameskip_threshold = strtol(var.value, NULL, 10);
1788 }
1789
1790 var.value = NULL;
1791 var.key = "pcsx_rearmed_frameskip_interval";
1792 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1793 {
1794 frameskip_interval = strtol(var.value, NULL, 10);
1795 }
1796
1797 var.value = NULL;
1798 var.key = "pcsx_rearmed_region";
1799 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1800 {
1801 Config.PsxAuto = 0;
1802 if (strcmp(var.value, "auto") == 0)
1803 Config.PsxAuto = 1;
1804 else if (strcmp(var.value, "NTSC") == 0)
1805 Config.PsxType = 0;
1806 else if (strcmp(var.value, "PAL") == 0)
1807 Config.PsxType = 1;
1808 }
1809
1810 update_multitap();
1811
1812 var.value = NULL;
1813 var.key = "pcsx_rearmed_negcon_deadzone";
1814 negcon_deadzone = 0;
1815 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1816 {
1817 negcon_deadzone = (int)(atoi(var.value) * 0.01f * NEGCON_RANGE);
1818 }
1819
1820 var.value = NULL;
1821 var.key = "pcsx_rearmed_negcon_response";
1822 negcon_linearity = 1;
1823 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1824 {
1825 if (strcmp(var.value, "quadratic") == 0)
1826 {
1827 negcon_linearity = 2;
1828 }
1829 else if (strcmp(var.value, "cubic") == 0)
1830 {
1831 negcon_linearity = 3;
1832 }
1833 }
1834
1835 var.value = NULL;
1836 var.key = "pcsx_rearmed_analog_axis_modifier";
1837 axis_bounds_modifier = true;
1838 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1839 {
1840 if (strcmp(var.value, "square") == 0)
1841 {
1842 axis_bounds_modifier = true;
1843 }
1844 else if (strcmp(var.value, "circle") == 0)
1845 {
1846 axis_bounds_modifier = false;
1847 }
1848 }
1849
1850 var.value = NULL;
1851 var.key = "pcsx_rearmed_vibration";
1852
1853 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1854 {
1855 if (strcmp(var.value, "disabled") == 0)
1856 in_enable_vibration = 0;
1857 else if (strcmp(var.value, "enabled") == 0)
1858 in_enable_vibration = 1;
1859 }
1860
1861 var.value = NULL;
1862 var.key = "pcsx_rearmed_dithering";
1863
1864 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1865 {
1866 if (strcmp(var.value, "disabled") == 0)
1867 {
1868 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
1869 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 0;
1870 pl_rearmed_cbs.gpu_unai.dithering = 0;
1871#ifdef GPU_NEON
1872 pl_rearmed_cbs.gpu_neon.allow_dithering = 0;
1873#endif
1874 }
1875 else if (strcmp(var.value, "enabled") == 0)
1876 {
1877 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1878 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 1;
1879 pl_rearmed_cbs.gpu_unai.dithering = 1;
1880#ifdef GPU_NEON
1881 pl_rearmed_cbs.gpu_neon.allow_dithering = 1;
1882#endif
1883 }
1884 }
1885
1886#ifdef GPU_NEON
1887 var.value = NULL;
1888 var.key = "pcsx_rearmed_neon_interlace_enable_v2";
1889
1890 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1891 {
1892 if (strcmp(var.value, "disabled") == 0)
1893 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
1894 else if (strcmp(var.value, "enabled") == 0)
1895 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
1896 else // auto
1897 pl_rearmed_cbs.gpu_neon.allow_interlace = 2;
1898 }
1899
1900 var.value = NULL;
1901 var.key = "pcsx_rearmed_neon_enhancement_enable";
1902
1903 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1904 {
1905 if (strcmp(var.value, "disabled") == 0)
1906 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
1907 else if (strcmp(var.value, "enabled") == 0)
1908 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
1909 }
1910
1911 var.value = NULL;
1912 var.key = "pcsx_rearmed_neon_enhancement_no_main";
1913
1914 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1915 {
1916 if (strcmp(var.value, "disabled") == 0)
1917 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
1918 else if (strcmp(var.value, "enabled") == 0)
1919 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
1920 }
1921#endif
1922
1923 var.value = NULL;
1924 var.key = "pcsx_rearmed_duping_enable";
1925
1926 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1927 {
1928 if (strcmp(var.value, "disabled") == 0)
1929 duping_enable = false;
1930 else if (strcmp(var.value, "enabled") == 0)
1931 duping_enable = true;
1932 }
1933
1934 var.value = NULL;
1935 var.key = "pcsx_rearmed_display_internal_fps";
1936
1937 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1938 {
1939 if (strcmp(var.value, "disabled") == 0)
1940 display_internal_fps = false;
1941 else if (strcmp(var.value, "enabled") == 0)
1942 display_internal_fps = true;
1943 }
1944
1945 //
1946 // CPU emulation related config
1947#ifndef DRC_DISABLE
1948 var.value = NULL;
1949 var.key = "pcsx_rearmed_drc";
1950
1951 if (!environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
1952 var.value = "enabled";
1953
1954 {
1955 R3000Acpu *prev_cpu = psxCpu;
1956
1957#ifdef _3DS
1958 if (!__ctr_svchax)
1959 Config.Cpu = CPU_INTERPRETER;
1960 else
1961#endif
1962 if (strcmp(var.value, "disabled") == 0)
1963 Config.Cpu = CPU_INTERPRETER;
1964 else if (strcmp(var.value, "enabled") == 0)
1965 Config.Cpu = CPU_DYNAREC;
1966
1967 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
1968 if (psxCpu != prev_cpu)
1969 {
1970 prev_cpu->Notify(R3000ACPU_NOTIFY_BEFORE_SAVE, NULL);
1971 prev_cpu->Shutdown();
1972 psxCpu->Init();
1973 psxCpu->Reset();
1974 psxCpu->Notify(R3000ACPU_NOTIFY_AFTER_LOAD, NULL);
1975 }
1976 }
1977#endif /* !DRC_DISABLE */
1978
1979 var.value = NULL;
1980 var.key = "pcsx_rearmed_psxclock";
1981 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1982 {
1983 int psxclock = atoi(var.value);
1984 Config.cycle_multiplier = 10000 / psxclock;
1985 }
1986
1987#if !defined(DRC_DISABLE) && !defined(LIGHTREC)
1988 var.value = NULL;
1989 var.key = "pcsx_rearmed_nosmccheck";
1990 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1991 {
1992 if (strcmp(var.value, "enabled") == 0)
1993 new_dynarec_hacks |= NDHACK_NO_SMC_CHECK;
1994 else
1995 new_dynarec_hacks &= ~NDHACK_NO_SMC_CHECK;
1996 }
1997
1998 var.value = NULL;
1999 var.key = "pcsx_rearmed_gteregsunneeded";
2000 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2001 {
2002 if (strcmp(var.value, "enabled") == 0)
2003 new_dynarec_hacks |= NDHACK_GTE_UNNEEDED;
2004 else
2005 new_dynarec_hacks &= ~NDHACK_GTE_UNNEEDED;
2006 }
2007
2008 var.value = NULL;
2009 var.key = "pcsx_rearmed_nogteflags";
2010 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2011 {
2012 if (strcmp(var.value, "enabled") == 0)
2013 new_dynarec_hacks |= NDHACK_GTE_NO_FLAGS;
2014 else
2015 new_dynarec_hacks &= ~NDHACK_GTE_NO_FLAGS;
2016 }
2017
2018 var.value = NULL;
2019 var.key = "pcsx_rearmed_nocompathacks";
2020 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2021 {
2022 if (strcmp(var.value, "enabled") == 0)
2023 new_dynarec_hacks |= NDHACK_NO_COMPAT_HACKS;
2024 else
2025 new_dynarec_hacks &= ~NDHACK_NO_COMPAT_HACKS;
2026 }
2027#endif /* !DRC_DISABLE && !LIGHTREC */
2028
2029 var.value = NULL;
2030 var.key = "pcsx_rearmed_nostalls";
2031 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2032 {
2033 if (strcmp(var.value, "enabled") == 0)
2034 Config.DisableStalls = 1;
2035 else
2036 Config.DisableStalls = 0;
2037 }
2038
2039 var.value = NULL;
2040 var.key = "pcsx_rearmed_icache_emulation";
2041 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2042 {
2043 if (strcmp(var.value, "disabled") == 0)
2044 Config.icache_emulation = 0;
2045 else if (strcmp(var.value, "enabled") == 0)
2046 Config.icache_emulation = 1;
2047 }
2048
2049 var.value = NULL;
2050 var.key = "pcsx_rearmed_exception_emulation";
2051 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2052 {
2053 if (strcmp(var.value, "enabled") == 0)
2054 Config.PreciseExceptions = 1;
2055 else
2056 Config.PreciseExceptions = 0;
2057 }
2058
2059 psxCpu->ApplyConfig();
2060
2061 // end of CPU emu config
2062 //
2063
2064 var.value = NULL;
2065 var.key = "pcsx_rearmed_spu_reverb";
2066
2067 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2068 {
2069 if (strcmp(var.value, "disabled") == 0)
2070 spu_config.iUseReverb = false;
2071 else if (strcmp(var.value, "enabled") == 0)
2072 spu_config.iUseReverb = true;
2073 }
2074
2075 var.value = NULL;
2076 var.key = "pcsx_rearmed_spu_interpolation";
2077
2078 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2079 {
2080 if (strcmp(var.value, "simple") == 0)
2081 spu_config.iUseInterpolation = 1;
2082 else if (strcmp(var.value, "gaussian") == 0)
2083 spu_config.iUseInterpolation = 2;
2084 else if (strcmp(var.value, "cubic") == 0)
2085 spu_config.iUseInterpolation = 3;
2086 else if (strcmp(var.value, "off") == 0)
2087 spu_config.iUseInterpolation = 0;
2088 }
2089
2090 var.value = NULL;
2091 var.key = "pcsx_rearmed_spu_thread";
2092 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2093 {
2094 if (strcmp(var.value, "enabled") == 0)
2095 spu_config.iUseThread = 1;
2096 else
2097 spu_config.iUseThread = 0;
2098 }
2099
2100 if (P_HAVE_PTHREAD) {
2101 var.value = NULL;
2102 var.key = "pcsx_rearmed_async_cd";
2103 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2104 {
2105 if (strcmp(var.value, "async") == 0)
2106 {
2107 Config.AsyncCD = 1;
2108 Config.CHD_Precache = 0;
2109 }
2110 else if (strcmp(var.value, "sync") == 0)
2111 {
2112 Config.AsyncCD = 0;
2113 Config.CHD_Precache = 0;
2114 }
2115 else if (strcmp(var.value, "precache") == 0)
2116 {
2117 Config.AsyncCD = 0;
2118 Config.CHD_Precache = 1;
2119 }
2120 }
2121 }
2122
2123 var.value = NULL;
2124 var.key = "pcsx_rearmed_noxadecoding";
2125 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2126 {
2127 if (strcmp(var.value, "disabled") == 0)
2128 Config.Xa = 1;
2129 else
2130 Config.Xa = 0;
2131 }
2132
2133 var.value = NULL;
2134 var.key = "pcsx_rearmed_nocdaudio";
2135 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2136 {
2137 if (strcmp(var.value, "disabled") == 0)
2138 Config.Cdda = 1;
2139 else
2140 Config.Cdda = 0;
2141 }
2142
2143 var.value = NULL;
2144 var.key = "pcsx_rearmed_gpu_slow_llists";
2145 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2146 {
2147 if (strcmp(var.value, "disabled") == 0)
2148 Config.GpuListWalking = 0;
2149 else if (strcmp(var.value, "enabled") == 0)
2150 Config.GpuListWalking = 1;
2151 else // auto
2152 Config.GpuListWalking = -1;
2153 }
2154
2155 var.value = NULL;
2156 var.key = "pcsx_rearmed_screen_centering";
2157 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2158 {
2159 if (strcmp(var.value, "game") == 0)
2160 pl_rearmed_cbs.screen_centering_type = 1;
2161 else if (strcmp(var.value, "manual") == 0)
2162 pl_rearmed_cbs.screen_centering_type = 2;
2163 else // auto
2164 pl_rearmed_cbs.screen_centering_type = 0;
2165 }
2166
2167 var.value = NULL;
2168 var.key = "pcsx_rearmed_screen_centering_x";
2169 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2170 {
2171 pl_rearmed_cbs.screen_centering_x = atoi(var.value);
2172 }
2173
2174 var.value = NULL;
2175 var.key = "pcsx_rearmed_screen_centering_y";
2176 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2177 {
2178 pl_rearmed_cbs.screen_centering_y = atoi(var.value);
2179 }
2180
2181#ifdef THREAD_RENDERING
2182 var.key = "pcsx_rearmed_gpu_thread_rendering";
2183 var.value = NULL;
2184
2185 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2186 {
2187 if (strcmp(var.value, "disabled") == 0)
2188 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_OFF;
2189 else if (strcmp(var.value, "sync") == 0)
2190 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_SYNC;
2191 else if (strcmp(var.value, "async") == 0)
2192 pl_rearmed_cbs.thread_rendering = THREAD_RENDERING_ASYNC;
2193 }
2194#endif
2195
2196#ifdef GPU_PEOPS
2197 var.value = NULL;
2198 var.key = "pcsx_rearmed_gpu_peops_odd_even_bit";
2199
2200 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2201 {
2202 if (strcmp(var.value, "enabled") == 0)
2203 gpu_peops_fix |= GPU_PEOPS_ODD_EVEN_BIT;
2204 }
2205
2206 var.value = NULL;
2207 var.key = "pcsx_rearmed_gpu_peops_expand_screen_width";
2208
2209 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2210 {
2211 if (strcmp(var.value, "enabled") == 0)
2212 gpu_peops_fix |= GPU_PEOPS_EXPAND_SCREEN_WIDTH;
2213 }
2214
2215 var.value = NULL;
2216 var.key = "pcsx_rearmed_gpu_peops_ignore_brightness";
2217
2218 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2219 {
2220 if (strcmp(var.value, "enabled") == 0)
2221 gpu_peops_fix |= GPU_PEOPS_IGNORE_BRIGHTNESS;
2222 }
2223
2224 var.value = NULL;
2225 var.key = "pcsx_rearmed_gpu_peops_disable_coord_check";
2226
2227 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2228 {
2229 if (strcmp(var.value, "enabled") == 0)
2230 gpu_peops_fix |= GPU_PEOPS_DISABLE_COORD_CHECK;
2231 }
2232
2233 var.value = NULL;
2234 var.key = "pcsx_rearmed_gpu_peops_lazy_screen_update";
2235
2236 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2237 {
2238 if (strcmp(var.value, "enabled") == 0)
2239 gpu_peops_fix |= GPU_PEOPS_LAZY_SCREEN_UPDATE;
2240 }
2241
2242 var.value = NULL;
2243 var.key = "pcsx_rearmed_gpu_peops_repeated_triangles";
2244
2245 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2246 {
2247 if (strcmp(var.value, "enabled") == 0)
2248 gpu_peops_fix |= GPU_PEOPS_REPEATED_TRIANGLES;
2249 }
2250
2251 var.value = NULL;
2252 var.key = "pcsx_rearmed_gpu_peops_quads_with_triangles";
2253
2254 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2255 {
2256 if (strcmp(var.value, "enabled") == 0)
2257 gpu_peops_fix |= GPU_PEOPS_QUADS_WITH_TRIANGLES;
2258 }
2259
2260 var.value = NULL;
2261 var.key = "pcsx_rearmed_gpu_peops_fake_busy_state";
2262
2263 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2264 {
2265 if (strcmp(var.value, "enabled") == 0)
2266 gpu_peops_fix |= GPU_PEOPS_FAKE_BUSY_STATE;
2267 }
2268
2269 if (pl_rearmed_cbs.gpu_peops.dwActFixes != gpu_peops_fix)
2270 pl_rearmed_cbs.gpu_peops.dwActFixes = gpu_peops_fix;
2271#endif
2272
2273#ifdef GPU_UNAI
2274 /* Note: This used to be an option, but it only works
2275 * (correctly) when running high resolution games
2276 * (480i, 512i) and has been obsoleted by
2277 * pcsx_rearmed_gpu_unai_scale_hires */
2278 pl_rearmed_cbs.gpu_unai.ilace_force = 0;
2279 /* Note: This used to be an option, but it has no
2280 * discernable effect and has been obsoleted by
2281 * pcsx_rearmed_gpu_unai_scale_hires */
2282 pl_rearmed_cbs.gpu_unai.pixel_skip = 0;
2283
2284 var.key = "pcsx_rearmed_gpu_unai_lighting";
2285 var.value = NULL;
2286
2287 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2288 {
2289 if (strcmp(var.value, "disabled") == 0)
2290 pl_rearmed_cbs.gpu_unai.lighting = 0;
2291 else if (strcmp(var.value, "enabled") == 0)
2292 pl_rearmed_cbs.gpu_unai.lighting = 1;
2293 }
2294
2295 var.key = "pcsx_rearmed_gpu_unai_fast_lighting";
2296 var.value = NULL;
2297
2298 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2299 {
2300 if (strcmp(var.value, "disabled") == 0)
2301 pl_rearmed_cbs.gpu_unai.fast_lighting = 0;
2302 else if (strcmp(var.value, "enabled") == 0)
2303 pl_rearmed_cbs.gpu_unai.fast_lighting = 1;
2304 }
2305
2306 var.key = "pcsx_rearmed_gpu_unai_blending";
2307 var.value = NULL;
2308
2309 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2310 {
2311 if (strcmp(var.value, "disabled") == 0)
2312 pl_rearmed_cbs.gpu_unai.blending = 0;
2313 else if (strcmp(var.value, "enabled") == 0)
2314 pl_rearmed_cbs.gpu_unai.blending = 1;
2315 }
2316
2317 var.key = "pcsx_rearmed_gpu_unai_scale_hires";
2318 var.value = NULL;
2319
2320 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2321 {
2322 if (strcmp(var.value, "disabled") == 0)
2323 pl_rearmed_cbs.gpu_unai.scale_hires = 0;
2324 else if (strcmp(var.value, "enabled") == 0)
2325 pl_rearmed_cbs.gpu_unai.scale_hires = 1;
2326 }
2327#endif // GPU_UNAI
2328
2329 //This adjustment process gives the user the ability to manually align the mouse up better
2330 //with where the shots are in the emulator.
2331
2332 var.value = NULL;
2333 var.key = "pcsx_rearmed_gunconadjustx";
2334
2335 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2336 {
2337 GunconAdjustX = atoi(var.value);
2338 }
2339
2340 var.value = NULL;
2341 var.key = "pcsx_rearmed_gunconadjusty";
2342
2343 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2344 {
2345 GunconAdjustY = atoi(var.value);
2346 }
2347
2348 var.value = NULL;
2349 var.key = "pcsx_rearmed_gunconadjustratiox";
2350
2351 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2352 {
2353 GunconAdjustRatioX = atof(var.value);
2354 }
2355
2356 var.value = NULL;
2357 var.key = "pcsx_rearmed_gunconadjustratioy";
2358
2359 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2360 {
2361 GunconAdjustRatioY = atof(var.value);
2362 }
2363
2364 var.value = NULL;
2365 var.key = "pcsx_rearmed_input_sensitivity";
2366 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2367 {
2368 mouse_sensitivity = atof(var.value);
2369 }
2370
2371 if (in_flight)
2372 {
2373 // inform core things about possible config changes
2374 plugin_call_rearmed_cbs();
2375
2376 if (GPU_open != NULL && GPU_close != NULL)
2377 {
2378 GPU_close();
2379 GPU_open(&gpuDisp, "PCSX", NULL);
2380 }
2381
2382 /* Reinitialise frameskipping, if required */
2383 if (((frameskip_type != prev_frameskip_type)))
2384 retro_set_audio_buff_status_cb();
2385
2386 /* dfinput_activate(); */
2387 }
2388 else
2389 {
2390 //not yet running
2391
2392 //bootlogo display hack
2393 if (found_bios)
2394 {
2395 var.value = NULL;
2396 var.key = "pcsx_rearmed_show_bios_bootlogo";
2397 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2398 {
2399 Config.SlowBoot = 0;
2400 rebootemu = 0;
2401 if (strcmp(var.value, "enabled") == 0)
2402 {
2403 Config.SlowBoot = 1;
2404 rebootemu = 1;
2405 }
2406 }
2407 }
2408 }
2409
2410 update_option_visibility();
2411}
2412
2413// Taken from beetle-psx-libretro
2414static uint16_t get_analog_button(int16_t ret, retro_input_state_t input_state_cb, int player_index, int id)
2415{
2416 // NOTE: Analog buttons were added Nov 2017. Not all front-ends support this
2417 // feature (or pre-date it) so we need to handle this in a graceful way.
2418
2419 // First, try and get an analog value using the new libretro API constant
2420 uint16_t button = input_state_cb(player_index,
2421 RETRO_DEVICE_ANALOG,
2422 RETRO_DEVICE_INDEX_ANALOG_BUTTON,
2423 id);
2424 button = MIN(button / 128, 255);
2425
2426 if (button == 0)
2427 {
2428 // If we got exactly zero, we're either not pressing the button, or the front-end
2429 // is not reporting analog values. We need to do a second check using the classic
2430 // digital API method, to at least get some response - better than nothing.
2431
2432 // NOTE: If we're really just not holding the button, we're still going to get zero.
2433
2434 button = (ret & (1 << id)) ? 255 : 0;
2435 }
2436
2437 return button;
2438}
2439
2440unsigned char axis_range_modifier(int16_t axis_value, bool is_square)
2441{
2442 float modifier_axis_range = 0;
2443
2444 if (is_square)
2445 {
2446 modifier_axis_range = round((axis_value >> 8) / 0.785) + 128;
2447 if (modifier_axis_range < 0)
2448 {
2449 modifier_axis_range = 0;
2450 }
2451 else if (modifier_axis_range > 255)
2452 {
2453 modifier_axis_range = 255;
2454 }
2455 }
2456 else
2457 {
2458 modifier_axis_range = MIN(((axis_value >> 8) + 128), 255);
2459 }
2460
2461 return modifier_axis_range;
2462}
2463
2464static void update_input_guncon(int port, int ret)
2465{
2466 //ToDo:
2467 //Core option for cursors for both players
2468 //Separate pointer and lightgun control types
2469
2470 //Mouse range is -32767 -> 32767
2471 //1% is about 655
2472 //Use the left analog stick field to store the absolute coordinates
2473
2474 int gunx = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X);
2475 int guny = input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_SCREEN_Y);
2476
2477 //Have the Libretro API let /libpcsxcore/plugins.c know when the lightgun is pointed offscreen
2478 //Offscreen value is chosen to be well out of range of any possible scaling done via core options
2479 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))
2480 {
2481 in_analog_left[port][0] = 65536;
2482 in_analog_left[port][1] = 65536;
2483 }
2484 else
2485 {
2486 in_analog_left[port][0] = ((gunx * GunconAdjustRatioX) + (GunconAdjustX * 655)) / 64 + 512;
2487 in_analog_left[port][1] = ((guny * GunconAdjustRatioY) + (GunconAdjustY * 655)) / 64 + 512;
2488 }
2489
2490 //GUNCON has 3 controls, Trigger,A,B which equal Circle,Start,Cross
2491
2492 // Trigger
2493 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))
2494 in_keystate[port] |= (1 << DKEY_CIRCLE);
2495
2496 // A
2497 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_A))
2498 in_keystate[port] |= (1 << DKEY_START);
2499
2500 // B
2501 if (input_state_cb(port, RETRO_DEVICE_LIGHTGUN, 0, RETRO_DEVICE_ID_LIGHTGUN_AUX_B))
2502 in_keystate[port] |= (1 << DKEY_CROSS);
2503
2504}
2505
2506static void update_input_negcon(int port, int ret)
2507{
2508 int lsx;
2509 int rsy;
2510 int negcon_i_rs;
2511 int negcon_ii_rs;
2512 float negcon_twist_amplitude;
2513
2514 // Query digital inputs
2515 //
2516 // > Pad-Up
2517 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_UP))
2518 in_keystate[port] |= (1 << DKEY_UP);
2519 // > Pad-Right
2520 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT))
2521 in_keystate[port] |= (1 << DKEY_RIGHT);
2522 // > Pad-Down
2523 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN))
2524 in_keystate[port] |= (1 << DKEY_DOWN);
2525 // > Pad-Left
2526 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT))
2527 in_keystate[port] |= (1 << DKEY_LEFT);
2528 // > Start
2529 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_START))
2530 in_keystate[port] |= (1 << DKEY_START);
2531 // > neGcon A
2532 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_A))
2533 in_keystate[port] |= (1 << DKEY_CIRCLE);
2534 // > neGcon B
2535 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_X))
2536 in_keystate[port] |= (1 << DKEY_TRIANGLE);
2537 // > neGcon R shoulder (digital)
2538 if (ret & (1 << RETRO_DEVICE_ID_JOYPAD_R))
2539 in_keystate[port] |= (1 << DKEY_R1);
2540 // Query analog inputs
2541 //
2542 // From studying 'libpcsxcore/plugins.c' and 'frontend/plugin.c':
2543 // >> pad->leftJoyX == in_analog_left[port][0] == NeGcon II
2544 // >> pad->leftJoyY == in_analog_left[port][1] == NeGcon L
2545 // >> pad->rightJoyX == in_analog_right[port][0] == NeGcon twist
2546 // >> pad->rightJoyY == in_analog_right[port][1] == NeGcon I
2547 // So we just have to map in_analog_left/right to more
2548 // appropriate inputs...
2549 //
2550 // > NeGcon twist
2551 // >> Get raw analog stick value and account for deadzone
2552 lsx = input_state_cb(port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
2553 if (lsx > negcon_deadzone)
2554 lsx = lsx - negcon_deadzone;
2555 else if (lsx < -negcon_deadzone)
2556 lsx = lsx + negcon_deadzone;
2557 else
2558 lsx = 0;
2559 // >> Convert to an 'amplitude' [-1.0,1.0] and adjust response
2560 negcon_twist_amplitude = (float)lsx / (float)(NEGCON_RANGE - negcon_deadzone);
2561 if (negcon_linearity == 2)
2562 {
2563 if (negcon_twist_amplitude < 0.0)
2564 negcon_twist_amplitude = -(negcon_twist_amplitude * negcon_twist_amplitude);
2565 else
2566 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude;
2567 }
2568 else if (negcon_linearity == 3)
2569 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude * negcon_twist_amplitude;
2570 // >> Convert to final 'in_analog' integer value [0,255]
2571 in_analog_right[port][0] = MAX(MIN((int)(negcon_twist_amplitude * 128.0f) + 128, 255), 0);
2572 // > NeGcon I + II
2573 // >> Handle right analog stick vertical axis mapping...
2574 // - Up (-Y) == accelerate == neGcon I
2575 // - Down (+Y) == brake == neGcon II
2576 negcon_i_rs = 0;
2577 negcon_ii_rs = 0;
2578 rsy = input_state_cb(port, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
2579 if (rsy >= 0)
2580 {
2581 // Account for deadzone
2582 // (Note: have never encountered a gamepad with significant differences
2583 // in deadzone between left/right analog sticks, so use the regular 'twist'
2584 // deadzone here)
2585 if (rsy > negcon_deadzone)
2586 rsy = rsy - negcon_deadzone;
2587 else
2588 rsy = 0;
2589 // Convert to 'in_analog' integer value [0,255]
2590 negcon_ii_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2591 }
2592 else
2593 {
2594 if (rsy < -negcon_deadzone)
2595 rsy = -1 * (rsy + negcon_deadzone);
2596 else
2597 rsy = 0;
2598 negcon_i_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
2599 }
2600 // >> NeGcon I
2601 in_analog_right[port][1] = MAX(
2602 MAX(
2603 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_R2),
2604 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_B)),
2605 negcon_i_rs);
2606 // >> NeGcon II
2607 in_analog_left[port][0] = MAX(
2608 MAX(
2609 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_L2),
2610 get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_Y)),
2611 negcon_ii_rs);
2612 // > NeGcon L
2613 in_analog_left[port][1] = get_analog_button(ret, input_state_cb, port, RETRO_DEVICE_ID_JOYPAD_L);
2614}
2615
2616static void update_input_mouse(int port, int ret)
2617{
2618 float raw_x = input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X);
2619 float raw_y = input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y);
2620
2621 int x = (int)roundf(raw_x * mouse_sensitivity);
2622 int y = (int)roundf(raw_y * mouse_sensitivity);
2623
2624 if (x > 127) x = 127;
2625 else if (x < -128) x = -128;
2626
2627 if (y > 127) y = 127;
2628 else if (y < -128) y = -128;
2629
2630 in_mouse[port][0] = x; /* -128..+128 left/right movement, 0 = no movement */
2631 in_mouse[port][1] = y; /* -128..+128 down/up movement, 0 = no movement */
2632
2633 /* left mouse button state */
2634 if (input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_LEFT))
2635 in_keystate[port] |= 1 << 11;
2636
2637 /* right mouse button state */
2638 if (input_state_cb(port, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_RIGHT))
2639 in_keystate[port] |= 1 << 10;
2640}
2641
2642static void update_input(void)
2643{
2644 // reset all keystate, query libretro for keystate
2645 int i;
2646 int j;
2647
2648 for (i = 0; i < PORTS_NUMBER; i++)
2649 {
2650 int16_t ret = 0;
2651 int type = in_type[i];
2652
2653 in_keystate[i] = 0;
2654
2655 if (type == PSE_PAD_TYPE_NONE)
2656 continue;
2657
2658 if (libretro_supports_bitmasks)
2659 ret = input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
2660 else
2661 {
2662 for (j = 0; j < (RETRO_DEVICE_ID_JOYPAD_R3 + 1); j++)
2663 {
2664 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j))
2665 ret |= (1 << j);
2666 }
2667 }
2668
2669 switch (type)
2670 {
2671 case PSE_PAD_TYPE_GUNCON:
2672 update_input_guncon(i, ret);
2673 break;
2674 case PSE_PAD_TYPE_NEGCON:
2675 update_input_negcon(i, ret);
2676 break;
2677 case PSE_PAD_TYPE_MOUSE:
2678 update_input_mouse(i, ret);
2679 break;
2680 default:
2681 // Query digital inputs
2682 for (j = 0; j < RETRO_PSX_MAP_LEN; j++)
2683 if (ret & (1 << j))
2684 in_keystate[i] |= retro_psx_map[j];
2685
2686 // Query analog inputs
2687 if (type == PSE_PAD_TYPE_ANALOGJOY || type == PSE_PAD_TYPE_ANALOGPAD)
2688 {
2689 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);
2690 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);
2691 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);
2692 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);
2693 }
2694 }
2695 }
2696}
2697
2698static void print_internal_fps(void)
2699{
2700 if (display_internal_fps)
2701 {
2702 frame_count++;
2703
2704 if (frame_count % INTERNAL_FPS_SAMPLE_PERIOD == 0)
2705 {
2706 unsigned internal_fps = pl_rearmed_cbs.flip_cnt * (is_pal_mode ? 50 : 60) / INTERNAL_FPS_SAMPLE_PERIOD;
2707 char str[64];
2708 const char *strc = (const char *)str;
2709
2710 str[0] = '\0';
2711
2712 snprintf(str, sizeof(str), "Internal FPS: %2d", internal_fps);
2713
2714 pl_rearmed_cbs.flip_cnt = 0;
2715
2716 if (msg_interface_version >= 1)
2717 {
2718 struct retro_message_ext msg = {
2719 strc,
2720 3000,
2721 1,
2722 RETRO_LOG_INFO,
2723 RETRO_MESSAGE_TARGET_OSD,
2724 RETRO_MESSAGE_TYPE_STATUS,
2725 -1
2726 };
2727 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE_EXT, &msg);
2728 }
2729 else
2730 {
2731 struct retro_message msg = {
2732 strc,
2733 180
2734 };
2735 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
2736 }
2737 }
2738 }
2739 else
2740 frame_count = 0;
2741}
2742
2743void retro_run(void)
2744{
2745 //SysReset must be run while core is running,Not in menu (Locks up Retroarch)
2746 if (rebootemu != 0)
2747 {
2748 rebootemu = 0;
2749 SysReset();
2750 if (Config.HLE)
2751 LoadCdrom();
2752 }
2753
2754 print_internal_fps();
2755
2756 /* Check whether current frame should
2757 * be skipped */
2758 pl_rearmed_cbs.fskip_force = 0;
2759 pl_rearmed_cbs.fskip_dirty = 0;
2760
2761 if (frameskip_type != FRAMESKIP_NONE)
2762 {
2763 bool skip_frame = false;
2764
2765 switch (frameskip_type)
2766 {
2767 case FRAMESKIP_AUTO:
2768 skip_frame = retro_audio_buff_active && retro_audio_buff_underrun;
2769 break;
2770 case FRAMESKIP_AUTO_THRESHOLD:
2771 skip_frame = retro_audio_buff_active && (retro_audio_buff_occupancy < frameskip_threshold);
2772 break;
2773 case FRAMESKIP_FIXED_INTERVAL:
2774 skip_frame = true;
2775 break;
2776 default:
2777 break;
2778 }
2779
2780 if (skip_frame && frameskip_counter < frameskip_interval)
2781 pl_rearmed_cbs.fskip_force = 1;
2782 }
2783
2784 /* If frameskip/timing settings have changed,
2785 * update frontend audio latency
2786 * > Can do this before or after the frameskip
2787 * check, but doing it after means we at least
2788 * retain the current frame's audio output */
2789 if (update_audio_latency)
2790 {
2791 environ_cb(RETRO_ENVIRONMENT_SET_MINIMUM_AUDIO_LATENCY,
2792 &retro_audio_latency);
2793 update_audio_latency = false;
2794 }
2795
2796 input_poll_cb();
2797
2798 update_input();
2799
2800 bool updated = false;
2801 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
2802 update_variables(true);
2803
2804 stop = 0;
2805 psxCpu->Execute();
2806
2807 if (pl_rearmed_cbs.fskip_dirty == 1) {
2808 if (frameskip_counter < frameskip_interval)
2809 frameskip_counter++;
2810 else if (frameskip_counter >= frameskip_interval || !pl_rearmed_cbs.fskip_force)
2811 frameskip_counter = 0;
2812 }
2813
2814 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf_ptr : NULL,
2815 vout_width, vout_height, vout_width * 2);
2816 vout_fb_dirty = 0;
2817
2818 set_vout_fb();
2819}
2820
2821static bool try_use_bios(const char *path, bool preferred_only)
2822{
2823 long size;
2824 const char *name;
2825 FILE *fp = fopen(path, "rb");
2826 if (fp == NULL)
2827 return false;
2828
2829 fseek(fp, 0, SEEK_END);
2830 size = ftell(fp);
2831 fclose(fp);
2832
2833 name = strrchr(path, SLASH);
2834 if (name++ == NULL)
2835 name = path;
2836
2837 if (preferred_only && size != 512 * 1024)
2838 return false;
2839 if (size != 512 * 1024 && size != 4 * 1024 * 1024)
2840 return false;
2841 if (strstr(name, "unirom"))
2842 return false;
2843 // jp bios have an addidional region check
2844 if (preferred_only && (strcasestr(name, "00.") || strcasestr(name, "j.bin")))
2845 return false;
2846
2847 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
2848 return true;
2849}
2850
2851#ifndef VITA
2852#include <sys/types.h>
2853#include <dirent.h>
2854
2855static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
2856{
2857 static const char *substr_pref[] = { "scph", "ps" };
2858 static const char *substr_alt[] = { "scph", "ps", "openbios" };
2859 DIR *dir;
2860 struct dirent *ent;
2861 bool ret = false;
2862 size_t i;
2863
2864 dir = opendir(dirpath);
2865 if (dir == NULL)
2866 return false;
2867
2868 // try to find a "better" bios
2869 while ((ent = readdir(dir)))
2870 {
2871 for (i = 0; i < sizeof(substr_pref) / sizeof(substr_pref[0]); i++)
2872 {
2873 const char *substr = substr_pref[i];
2874 if ((strncasecmp(ent->d_name, substr, strlen(substr)) != 0))
2875 continue;
2876 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
2877 ret = try_use_bios(path, true);
2878 if (ret)
2879 goto finish;
2880 }
2881 }
2882
2883 // another pass to look for anything fitting, even ps2 bios
2884 rewinddir(dir);
2885 while ((ent = readdir(dir)))
2886 {
2887 for (i = 0; i < sizeof(substr_alt) / sizeof(substr_alt[0]); i++)
2888 {
2889 const char *substr = substr_alt[i];
2890 if ((strncasecmp(ent->d_name, substr, strlen(substr)) != 0))
2891 continue;
2892 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
2893 ret = try_use_bios(path, false);
2894 if (ret)
2895 goto finish;
2896 }
2897 }
2898
2899
2900finish:
2901 closedir(dir);
2902 return ret;
2903}
2904#else
2905#define find_any_bios(...) false
2906#endif
2907
2908static void check_system_specs(void)
2909{
2910 unsigned level = 6;
2911 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
2912}
2913
2914static int init_memcards(void)
2915{
2916 int ret = 0;
2917 const char *dir;
2918 struct retro_variable var = { .key = "pcsx_rearmed_memcard2", .value = NULL };
2919 static const char CARD2_FILE[] = "pcsx-card2.mcd";
2920
2921 // Memcard2 will be handled and is re-enabled if needed using core
2922 // operations.
2923 // Memcard1 is handled by libretro, doing this will set core to
2924 // skip file io operations for memcard1 like SaveMcd
2925 snprintf(Config.Mcd1, sizeof(Config.Mcd1), "none");
2926 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "none");
2927 init_memcard(Mcd1Data);
2928 // Memcard 2 is managed by the emulator on the filesystem,
2929 // There is no need to initialize Mcd2Data like Mcd1Data.
2930
2931 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2932 {
2933 SysPrintf("Memcard 2: %s\n", var.value);
2934 if (memcmp(var.value, "enabled", 7) == 0)
2935 {
2936 if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir)
2937 {
2938 if (strlen(dir) + strlen(CARD2_FILE) + 2 > sizeof(Config.Mcd2))
2939 {
2940 SysPrintf("Path '%s' is too long. Cannot use memcard 2. Use a shorter path.\n", dir);
2941 ret = -1;
2942 }
2943 else
2944 {
2945 McdDisable[1] = 0;
2946 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "%s/%s", dir, CARD2_FILE);
2947 SysPrintf("Use memcard 2: %s\n", Config.Mcd2);
2948 }
2949 }
2950 else
2951 {
2952 SysPrintf("Could not get save directory! Could not create memcard 2.");
2953 ret = -1;
2954 }
2955 }
2956 }
2957 return ret;
2958}
2959
2960static void loadPSXBios(void)
2961{
2962 const char *dir;
2963 char path[PATH_MAX];
2964 unsigned useHLE = 0;
2965
2966 const char *bios[] = {
2967 "PSXONPSP660", "psxonpsp660",
2968 "SCPH101", "scph101",
2969 "SCPH5501", "scph5501",
2970 "SCPH7001", "scph7001",
2971 "SCPH1001", "scph1001"
2972 };
2973
2974 struct retro_variable var = {
2975 .key = "pcsx_rearmed_bios",
2976 .value = NULL
2977 };
2978
2979 found_bios = 0;
2980
2981 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
2982 {
2983 if (!strcmp(var.value, "HLE"))
2984 useHLE = 1;
2985 }
2986
2987 if (!useHLE)
2988 {
2989 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
2990 {
2991 unsigned i;
2992 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s", dir);
2993
2994 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++)
2995 {
2996 snprintf(path, sizeof(path), "%s%c%s.bin", dir, SLASH, bios[i]);
2997 found_bios = try_use_bios(path, true);
2998 if (found_bios)
2999 break;
3000 }
3001
3002 if (!found_bios)
3003 found_bios = find_any_bios(dir, path, sizeof(path));
3004 }
3005 if (found_bios)
3006 {
3007 SysPrintf("found BIOS file: %s\n", Config.Bios);
3008 }
3009 }
3010
3011 if (!found_bios)
3012 {
3013 const char *msg_str;
3014 if (useHLE)
3015 {
3016 msg_str = "BIOS set to \'hle\' in core options - real BIOS will be ignored";
3017 SysPrintf("Using HLE BIOS.\n");
3018 }
3019 else
3020 {
3021 msg_str = "No PlayStation BIOS file found - add for better compatibility";
3022 SysPrintf("No BIOS files found.\n");
3023 }
3024
3025 if (msg_interface_version >= 1)
3026 {
3027 struct retro_message_ext msg = {
3028 msg_str,
3029 3000,
3030 3,
3031 RETRO_LOG_WARN,
3032 RETRO_MESSAGE_TARGET_ALL,
3033 RETRO_MESSAGE_TYPE_NOTIFICATION,
3034 -1
3035 };
3036 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE_EXT, &msg);
3037 }
3038 else
3039 {
3040 struct retro_message msg = {
3041 msg_str,
3042 180
3043 };
3044 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
3045 }
3046 }
3047}
3048
3049void retro_init(void)
3050{
3051 unsigned dci_version = 0;
3052 struct retro_rumble_interface rumble;
3053 int ret;
3054
3055 msg_interface_version = 0;
3056 environ_cb(RETRO_ENVIRONMENT_GET_MESSAGE_INTERFACE_VERSION, &msg_interface_version);
3057
3058#if defined(__MACH__) && !defined(TVOS)
3059 // magic sauce to make the dynarec work on iOS
3060 syscall(SYS_ptrace, 0 /*PTRACE_TRACEME*/, 0, 0, 0);
3061#endif
3062
3063#ifdef _3DS
3064 psxMapHook = pl_3ds_mmap;
3065 psxUnmapHook = pl_3ds_munmap;
3066#endif
3067#ifdef VITA
3068 if (init_vita_mmap() < 0)
3069 abort();
3070 psxMapHook = pl_vita_mmap;
3071 psxUnmapHook = pl_vita_munmap;
3072#endif
3073 ret = emu_core_preinit();
3074#ifdef _3DS
3075 /* emu_core_preinit sets the cpu to dynarec */
3076 if (!__ctr_svchax)
3077 Config.Cpu = CPU_INTERPRETER;
3078#endif
3079 ret |= init_memcards();
3080
3081 ret |= emu_core_init();
3082 if (ret != 0)
3083 {
3084 SysPrintf("PCSX init failed.\n");
3085 exit(1);
3086 }
3087
3088#ifdef _3DS
3089 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
3090#elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && P_HAVE_POSIX_MEMALIGN
3091 if (posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2) != 0)
3092 vout_buf = (void *) 0;
3093#else
3094 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
3095#endif
3096
3097 vout_buf_ptr = vout_buf;
3098
3099 loadPSXBios();
3100
3101 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
3102
3103 disk_initial_index = 0;
3104 disk_initial_path[0] = '\0';
3105 if (environ_cb(RETRO_ENVIRONMENT_GET_DISK_CONTROL_INTERFACE_VERSION, &dci_version) && (dci_version >= 1))
3106 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_EXT_INTERFACE, &disk_control_ext);
3107 else
3108 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
3109
3110 rumble_cb = NULL;
3111 if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble))
3112 rumble_cb = rumble.set_rumble_state;
3113
3114 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
3115 * we have to do this because cache misses and some IO penalties
3116 * are not emulated. Warning: changing this may break compatibility. */
3117 Config.cycle_multiplier = CYCLE_MULT_DEFAULT;
3118#if defined(HAVE_PRE_ARMV7) && !defined(_3DS)
3119 Config.cycle_multiplier = 200;
3120#endif
3121 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
3122 pl_rearmed_cbs.gpu_peops.dwActFixes = GPU_PEOPS_OLD_FRAME_SKIP;
3123
3124 SaveFuncs.open = save_open;
3125 SaveFuncs.read = save_read;
3126 SaveFuncs.write = save_write;
3127 SaveFuncs.seek = save_seek;
3128 SaveFuncs.close = save_close;
3129
3130 if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL))
3131 libretro_supports_bitmasks = true;
3132
3133 check_system_specs();
3134}
3135
3136void retro_deinit(void)
3137{
3138 if (plugins_opened)
3139 {
3140 ClosePlugins();
3141 plugins_opened = 0;
3142 }
3143 SysClose();
3144#ifdef _3DS
3145 linearFree(vout_buf);
3146#else
3147 free(vout_buf);
3148#endif
3149 vout_buf = NULL;
3150
3151#ifdef VITA
3152 deinit_vita_mmap();
3153#endif
3154 libretro_supports_bitmasks = false;
3155 libretro_supports_option_categories = false;
3156
3157 show_input_settings = true;
3158#ifdef GPU_PEOPS
3159 show_advanced_gpu_peops_settings = true;
3160#endif
3161#ifdef GPU_UNAI
3162 show_advanced_gpu_unai_settings = true;
3163#endif
3164
3165 /* Have to reset disks struct, otherwise
3166 * fnames/flabels will leak memory */
3167 disk_init();
3168 frameskip_type = FRAMESKIP_NONE;
3169 frameskip_threshold = 0;
3170 frameskip_interval = 0;
3171 frameskip_counter = 0;
3172 retro_audio_buff_active = false;
3173 retro_audio_buff_occupancy = 0;
3174 retro_audio_buff_underrun = false;
3175 retro_audio_latency = 0;
3176 update_audio_latency = false;
3177}
3178
3179#ifdef VITA
3180#include <psp2/kernel/threadmgr.h>
3181int usleep(unsigned long us)
3182{
3183 sceKernelDelayThread(us);
3184}
3185#endif
3186
3187void SysPrintf(const char *fmt, ...)
3188{
3189 va_list list;
3190 char msg[512];
3191
3192 va_start(list, fmt);
3193 vsprintf(msg, fmt, list);
3194 va_end(list);
3195
3196 if (log_cb)
3197 log_cb(RETRO_LOG_INFO, "%s", msg);
3198}
3199
3200/* Prints debug-level logs */
3201void SysDLog(const char *fmt, ...)
3202{
3203 va_list list;
3204 char msg[512];
3205
3206 va_start(list, fmt);
3207 vsprintf(msg, fmt, list);
3208 va_end(list);
3209
3210 if (log_cb)
3211 log_cb(RETRO_LOG_DEBUG, "%s", msg);
3212}
3213
3214// vim:sw=3:ts=3:expandtab