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