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