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