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