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