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