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