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