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