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