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