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