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