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