Fix detection for rumble interface
[pcsx_rearmed.git] / frontend / libretro.c
... / ...
CommitLineData
1/*
2 * (C) notaz, 2012,2014,2015
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
8#define _GNU_SOURCE 1 // strcasestr
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <strings.h>
13#ifdef __MACH__
14#include <unistd.h>
15#include <sys/syscall.h>
16#endif
17
18#include "../libpcsxcore/misc.h"
19#include "../libpcsxcore/psxcounters.h"
20#include "../libpcsxcore/psxmem_map.h"
21#include "../libpcsxcore/new_dynarec/new_dynarec.h"
22#include "../libpcsxcore/cdrom.h"
23#include "../libpcsxcore/cdriso.h"
24#include "../libpcsxcore/cheat.h"
25#include "../plugins/dfsound/out.h"
26#include "../plugins/dfsound/spu_config.h"
27#include "../plugins/dfinput/externals.h"
28#include "cspace.h"
29#include "main.h"
30#include "plugin.h"
31#include "plugin_lib.h"
32#include "arm_features.h"
33#include "revision.h"
34#include "libretro.h"
35
36#ifdef _3DS
37#include "3ds/3ds_utils.h"
38#endif
39
40#define PORTS_NUMBER 8
41
42#ifndef MIN
43#define MIN(a, b) ((a) < (b) ? (a) : (b))
44#endif
45
46#ifndef MAX
47#define MAX(a, b) ((a) > (b) ? (a) : (b))
48#endif
49
50#define ISHEXDEC ((buf[cursor]>='0') && (buf[cursor]<='9')) || ((buf[cursor]>='a') && (buf[cursor]<='f')) || ((buf[cursor]>='A') && (buf[cursor]<='F'))
51
52//hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
53static int rebootemu = 0;
54
55static retro_video_refresh_t video_cb;
56static retro_input_poll_t input_poll_cb;
57static retro_input_state_t input_state_cb;
58static retro_environment_t environ_cb;
59static retro_audio_sample_batch_t audio_batch_cb;
60static retro_set_rumble_state_t rumble_cb;
61static struct retro_log_callback logging;
62static retro_log_printf_t log_cb;
63
64static void *vout_buf;
65static void * vout_buf_ptr;
66static int vout_width, vout_height;
67static int vout_doffs_old, vout_fb_dirty;
68static bool vout_can_dupe;
69static bool duping_enable;
70static bool found_bios;
71
72static int plugins_opened;
73static int is_pal_mode;
74
75/* memory card data */
76extern char Mcd1Data[MCD_SIZE];
77extern char Mcd2Data[MCD_SIZE];
78extern char McdDisable[2];
79
80/* PCSX ReARMed core calls and stuff */
81int in_type[8] = { PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
82 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
83 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE,
84 PSE_PAD_TYPE_NONE, PSE_PAD_TYPE_NONE };
85int in_analog_left[8][2] = {{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 }};
86int in_analog_right[8][2] = {{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 }};
87unsigned short in_keystate[PORTS_NUMBER];
88int multitap1 = 0;
89int multitap2 = 0;
90int in_enable_vibration = 1;
91
92// NegCon adjustment parameters
93// > The NegCon 'twist' action is somewhat awkward when mapped
94// to a standard analog stick -> user should be able to tweak
95// response/deadzone for comfort
96// > When response is linear, 'additional' deadzone (set here)
97// may be left at zero, since this is normally handled via in-game
98// options menus
99// > When response is non-linear, deadzone should be set to match the
100// controller being used (otherwise precision may be lost)
101// > negcon_linearity:
102// - 1: Response is linear - recommended when using racing wheel
103// peripherals, not recommended for standard gamepads
104// - 2: Response is quadratic - optimal setting for gamepads
105// - 3: Response is cubic - enables precise fine control, but
106// difficult to use...
107#define NEGCON_RANGE 0x7FFF
108static int negcon_deadzone = 0;
109static int negcon_linearity = 1;
110
111/* PSX max resolution is 640x512, but with enhancement it's 1024x512 */
112#define VOUT_MAX_WIDTH 1024
113#define VOUT_MAX_HEIGHT 512
114
115//Dummy functions
116bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info){return false;}
117void retro_unload_game(void){}
118static int vout_open(void){return 0;}
119static void vout_close(void){}
120static int snd_init(void){return 0;}
121static void snd_finish(void){}
122static int snd_busy(void){return 0;}
123
124static void init_memcard(char *mcd_data)
125{
126 unsigned off = 0;
127 unsigned i;
128
129 memset(mcd_data, 0, MCD_SIZE);
130
131 mcd_data[off++] = 'M';
132 mcd_data[off++] = 'C';
133 off += 0x7d;
134 mcd_data[off++] = 0x0e;
135
136 for (i = 0; i < 15; i++) {
137 mcd_data[off++] = 0xa0;
138 off += 0x07;
139 mcd_data[off++] = 0xff;
140 mcd_data[off++] = 0xff;
141 off += 0x75;
142 mcd_data[off++] = 0xa0;
143 }
144
145 for (i = 0; i < 20; i++) {
146 mcd_data[off++] = 0xff;
147 mcd_data[off++] = 0xff;
148 mcd_data[off++] = 0xff;
149 mcd_data[off++] = 0xff;
150 off += 0x04;
151 mcd_data[off++] = 0xff;
152 mcd_data[off++] = 0xff;
153 off += 0x76;
154 }
155}
156
157static void set_vout_fb()
158{
159 struct retro_framebuffer fb = {0};
160
161 fb.width = vout_width;
162 fb.height = vout_height;
163 fb.access_flags = RETRO_MEMORY_ACCESS_WRITE;
164
165 if (environ_cb(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &fb) && fb.format == RETRO_PIXEL_FORMAT_RGB565)
166 vout_buf_ptr = (uint16_t*)fb.data;
167 else
168 vout_buf_ptr = vout_buf;
169}
170
171static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
172{
173 vout_width = w;
174 vout_height = h;
175
176 set_vout_fb();
177}
178
179#ifndef FRONTEND_SUPPORTS_RGB565
180static void convert(void *buf, size_t bytes)
181{
182 unsigned int i, v, *p = buf;
183
184 for (i = 0; i < bytes / 4; i++) {
185 v = p[i];
186 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
187 }
188}
189#endif
190
191static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
192{
193 unsigned short *dest = vout_buf_ptr;
194 const unsigned short *src = vram;
195 int dstride = vout_width, h1 = h;
196 int doffs;
197
198 if (vram == NULL) {
199 // blanking
200 memset(vout_buf_ptr, 0, dstride * h * 2);
201 goto out;
202 }
203
204 doffs = (vout_height - h) * dstride;
205 doffs += (dstride - w) / 2 & ~1;
206 if (doffs != vout_doffs_old) {
207 // clear borders
208 memset(vout_buf_ptr, 0, dstride * h * 2);
209 vout_doffs_old = doffs;
210 }
211 dest += doffs;
212
213 if (bgr24)
214 {
215 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
216 for (; h1-- > 0; dest += dstride, src += stride)
217 {
218 bgr888_to_rgb565(dest, src, w * 3);
219 }
220 }
221 else
222 {
223 for (; h1-- > 0; dest += dstride, src += stride)
224 {
225 bgr555_to_rgb565(dest, src, w * 2);
226 }
227 }
228
229out:
230#ifndef FRONTEND_SUPPORTS_RGB565
231 convert(vout_buf_ptr, vout_width * vout_height * 2);
232#endif
233 vout_fb_dirty = 1;
234 pl_rearmed_cbs.flip_cnt++;
235}
236
237#ifdef _3DS
238typedef struct
239{
240 void* buffer;
241 uint32_t target_map;
242 size_t size;
243 enum psxMapTag tag;
244}psx_map_t;
245
246psx_map_t custom_psx_maps[] = {
247 {NULL, 0x13000000, 0x210000, MAP_TAG_RAM}, // 0x80000000
248 {NULL, 0x12800000, 0x010000, MAP_TAG_OTHER}, // 0x1f800000
249 {NULL, 0x12c00000, 0x080000, MAP_TAG_OTHER}, // 0x1fc00000
250 {NULL, 0x11000000, 0x800000, MAP_TAG_LUTS}, // 0x08000000
251 {NULL, 0x12000000, 0x200000, MAP_TAG_VRAM}, // 0x00000000
252};
253
254void* pl_3ds_mmap(unsigned long addr, size_t size, int is_fixed,
255 enum psxMapTag tag)
256{
257 (void)is_fixed;
258 (void)addr;
259
260 if (__ctr_svchax)
261 {
262 psx_map_t* custom_map = custom_psx_maps;
263
264 for (; custom_map->size; custom_map++)
265 {
266 if ((custom_map->size == size) && (custom_map->tag == tag))
267 {
268 uint32_t ptr_aligned, tmp;
269
270 custom_map->buffer = malloc(size + 0x1000);
271 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
272
273 if(svcControlMemory(&tmp, (void*)custom_map->target_map, (void*)ptr_aligned, size, MEMOP_MAP, 0x3) < 0)
274 {
275 SysPrintf("could not map memory @0x%08X\n", custom_map->target_map);
276 exit(1);
277 }
278
279 return (void*)custom_map->target_map;
280 }
281 }
282 }
283
284 return malloc(size);
285}
286
287void pl_3ds_munmap(void *ptr, size_t size, enum psxMapTag tag)
288{
289 (void)tag;
290
291 if (__ctr_svchax)
292 {
293 psx_map_t* custom_map = custom_psx_maps;
294
295 for (; custom_map->size; custom_map++)
296 {
297 if ((custom_map->target_map == (uint32_t)ptr))
298 {
299 uint32_t ptr_aligned, tmp;
300
301 ptr_aligned = (((u32)custom_map->buffer) + 0xFFF) & ~0xFFF;
302
303 svcControlMemory(&tmp, (void*)custom_map->target_map, (void*)ptr_aligned, size, MEMOP_UNMAP, 0x3);
304
305 free(custom_map->buffer);
306 custom_map->buffer = NULL;
307 return;
308 }
309 }
310 }
311
312 free(ptr);
313}
314#endif
315
316#ifdef VITA
317typedef struct
318{
319 void* buffer;
320 uint32_t target_map;
321 size_t size;
322 enum psxMapTag tag;
323}psx_map_t;
324
325void* addr = NULL;
326
327psx_map_t custom_psx_maps[] = {
328 {NULL, NULL, 0x210000, MAP_TAG_RAM}, // 0x80000000
329 {NULL, NULL, 0x010000, MAP_TAG_OTHER}, // 0x1f800000
330 {NULL, NULL, 0x080000, MAP_TAG_OTHER}, // 0x1fc00000
331 {NULL, NULL, 0x800000, MAP_TAG_LUTS}, // 0x08000000
332 {NULL, NULL, 0x200000, MAP_TAG_VRAM}, // 0x00000000
333};
334
335int init_vita_mmap(){
336 int n;
337 void * tmpaddr;
338 addr = malloc(64*1024*1024);
339 if(addr==NULL)
340 return -1;
341 tmpaddr = ((u32)(addr+0xFFFFFF))&~0xFFFFFF;
342 custom_psx_maps[0].buffer=tmpaddr+0x2000000;
343 custom_psx_maps[1].buffer=tmpaddr+0x1800000;
344 custom_psx_maps[2].buffer=tmpaddr+0x1c00000;
345 custom_psx_maps[3].buffer=tmpaddr+0x0000000;
346 custom_psx_maps[4].buffer=tmpaddr+0x1000000;
347#if 0
348 for(n = 0; n < 5; n++){
349 sceClibPrintf("addr reserved %x\n",custom_psx_maps[n].buffer);
350 }
351#endif
352 return 0;
353}
354
355void deinit_vita_mmap(){
356 free(addr);
357}
358
359void* pl_vita_mmap(unsigned long addr, size_t size, int is_fixed,
360 enum psxMapTag tag)
361{
362 (void)is_fixed;
363 (void)addr;
364
365
366 psx_map_t* custom_map = custom_psx_maps;
367
368 for (; custom_map->size; custom_map++)
369 {
370 if ((custom_map->size == size) && (custom_map->tag == tag))
371 {
372 return custom_map->buffer;
373 }
374 }
375
376
377 return malloc(size);
378}
379
380void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag)
381{
382 (void)tag;
383
384 psx_map_t* custom_map = custom_psx_maps;
385
386 for (; custom_map->size; custom_map++)
387 {
388 if ((custom_map->buffer == ptr))
389 {
390 return;
391 }
392 }
393
394 free(ptr);
395}
396#endif
397
398static void *pl_mmap(unsigned int size)
399{
400 return psxMap(0, size, 0, MAP_TAG_VRAM);
401}
402
403static void pl_munmap(void *ptr, unsigned int size)
404{
405 psxUnmap(ptr, size, MAP_TAG_VRAM);
406}
407
408struct rearmed_cbs pl_rearmed_cbs = {
409 .pl_vout_open = vout_open,
410 .pl_vout_set_mode = vout_set_mode,
411 .pl_vout_flip = vout_flip,
412 .pl_vout_close = vout_close,
413 .mmap = pl_mmap,
414 .munmap = pl_munmap,
415 /* from psxcounters */
416 .gpu_hcnt = &hSyncCount,
417 .gpu_frame_count = &frame_counter,
418};
419
420void pl_frame_limit(void)
421{
422 /* called once per frame, make psxCpu->Execute() above return */
423 stop = 1;
424}
425
426void pl_timing_prepare(int is_pal)
427{
428 is_pal_mode = is_pal;
429}
430
431void plat_trigger_vibrate(int pad, int low, int high)
432{
433 if (!rumble_cb)
434 return;
435
436 if (in_enable_vibration)
437 {
438 rumble_cb(pad, RETRO_RUMBLE_STRONG, high << 8);
439 rumble_cb(pad, RETRO_RUMBLE_WEAK, low ? 0xffff : 0x0);
440 }
441}
442
443void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
444{
445}
446
447/* sound calls */
448static void snd_feed(void *buf, int bytes)
449{
450 if (audio_batch_cb != NULL)
451 audio_batch_cb(buf, bytes / 4);
452}
453
454void out_register_libretro(struct out_driver *drv)
455{
456 drv->name = "libretro";
457 drv->init = snd_init;
458 drv->finish = snd_finish;
459 drv->busy = snd_busy;
460 drv->feed = snd_feed;
461}
462
463/* libretro */
464void retro_set_environment(retro_environment_t cb)
465{
466 static const struct retro_variable vars[] = {
467 { "pcsx_rearmed_frameskip", "Frameskip; 0|1|2|3" },
468 { "pcsx_rearmed_region", "Region; auto|NTSC|PAL" },
469 { "pcsx_rearmed_memcard2", "Enable second memory card; disabled|enabled" },
470 { "pcsx_rearmed_pad1type", "Pad 1 Type; default|none|standard|analog|dualshock|negcon" },
471 { "pcsx_rearmed_pad2type", "Pad 2 Type; default|none|standard|analog|dualshock|negcon" },
472 { "pcsx_rearmed_pad3type", "Pad 3 Type; default|none|standard|analog|dualshock|negcon" },
473 { "pcsx_rearmed_pad4type", "Pad 4 Type; default|none|standard|analog|dualshock|negcon" },
474 { "pcsx_rearmed_pad5type", "Pad 5 Type; default|none|standard|analog|dualshock|negcon" },
475 { "pcsx_rearmed_pad6type", "Pad 6 Type; default|none|standard|analog|dualshock|negcon" },
476 { "pcsx_rearmed_pad7type", "Pad 7 Type; default|none|standard|analog|dualshock|negcon" },
477 { "pcsx_rearmed_pad8type", "Pad 8 Type; default|none|standard|analog|dualshock|negcon" },
478 { "pcsx_rearmed_multitap1", "Multitap 1; auto|disabled|enabled" },
479 { "pcsx_rearmed_multitap2", "Multitap 2; auto|disabled|enabled" },
480 { "pcsx_rearmed_negcon_deadzone", "NegCon Twist Deadzone (percent); 0|5|10|15|20|25|30" },
481 { "pcsx_rearmed_negcon_response", "NegCon Twist Response; linear|quadratic|cubic" },
482 { "pcsx_rearmed_vibration", "Enable Vibration; enabled|disabled" },
483 { "pcsx_rearmed_dithering", "Enable Dithering; enabled|disabled" },
484#ifndef DRC_DISABLE
485 { "pcsx_rearmed_drc", "Dynamic recompiler; enabled|disabled" },
486#endif
487#ifdef __ARM_NEON__
488 { "pcsx_rearmed_neon_interlace_enable", "Enable interlacing mode(s); disabled|enabled" },
489 { "pcsx_rearmed_neon_enhancement_enable", "Enhanced resolution (slow); disabled|enabled" },
490 { "pcsx_rearmed_neon_enhancement_no_main", "Enhanced resolution speed hack; disabled|enabled" },
491#endif
492 { "pcsx_rearmed_duping_enable", "Frame duping; enabled|disabled" },
493 { "pcsx_rearmed_show_bios_bootlogo", "Show Bios Bootlogo(Breaks some games); disabled|enabled" },
494 { "pcsx_rearmed_spu_reverb", "Sound: Reverb; enabled|disabled" },
495 { "pcsx_rearmed_spu_interpolation", "Sound: Interpolation; simple|gaussian|cubic|off" },
496 { "pcsx_rearmed_pe2_fix", "Parasite Eve 2/Vandal Hearts 1/2 Fix; disabled|enabled" },
497 { "pcsx_rearmed_inuyasha_fix", "InuYasha Sengoku Battle Fix; disabled|enabled" },
498 { NULL, NULL },
499 };
500
501 if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
502 log_cb = logging.log;
503
504 environ_cb = cb;
505
506 cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)vars);
507}
508
509void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
510void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
511void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
512void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
513void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
514
515unsigned retro_api_version(void)
516{
517 return RETRO_API_VERSION;
518}
519
520static int controller_port_variable(unsigned port, struct retro_variable *var)
521{
522 if (port >= PORTS_NUMBER)
523 return 0;
524
525 if (!environ_cb)
526 return 0;
527
528 var->value = NULL;
529 switch (port) {
530 case 0:
531 var->key = "pcsx_rearmed_pad1type";
532 break;
533 case 1:
534 var->key = "pcsx_rearmed_pad2type";
535 break;
536 case 2:
537 var->key = "pcsx_rearmed_pad3type";
538 break;
539 case 3:
540 var->key = "pcsx_rearmed_pad4type";
541 break;
542 case 4:
543 var->key = "pcsx_rearmed_pad5type";
544 break;
545 case 5:
546 var->key = "pcsx_rearmed_pad6type";
547 break;
548 case 6:
549 var->key = "pcsx_rearmed_pad7type";
550 break;
551 case 7:
552 var->key = "pcsx_rearmed_pad8type";
553 break;
554 }
555
556 return environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, var) || var->value;
557}
558
559static void update_controller_port_variable(unsigned port)
560{
561 if (port >= PORTS_NUMBER)
562 return;
563
564 struct retro_variable var;
565
566 if (controller_port_variable(port, &var))
567 {
568 if (strcmp(var.value, "standard") == 0)
569 in_type[port] = PSE_PAD_TYPE_STANDARD;
570 else if (strcmp(var.value, "analog") == 0)
571 in_type[port] = PSE_PAD_TYPE_ANALOGJOY;
572 else if (strcmp(var.value, "dualshock") == 0)
573 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
574 else if (strcmp(var.value, "negcon") == 0)
575 in_type[port] = PSE_PAD_TYPE_NEGCON;
576 else if (strcmp(var.value, "none") == 0)
577 in_type[port] = PSE_PAD_TYPE_NONE;
578 // else 'default' case, do nothing
579 }
580}
581
582static void update_controller_port_device(unsigned port, unsigned device)
583{
584 if (port >= PORTS_NUMBER)
585 return;
586
587 struct retro_variable var;
588
589 if (!controller_port_variable(port, &var))
590 return;
591
592 if (strcmp(var.value, "default") != 0)
593 return;
594
595 switch (device)
596 {
597 case RETRO_DEVICE_JOYPAD:
598 in_type[port] = PSE_PAD_TYPE_STANDARD;
599 break;
600 case RETRO_DEVICE_ANALOG:
601 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
602 break;
603 case RETRO_DEVICE_MOUSE:
604 in_type[port] = PSE_PAD_TYPE_MOUSE;
605 break;
606 case RETRO_DEVICE_LIGHTGUN:
607 in_type[port] = PSE_PAD_TYPE_GUN;
608 break;
609 case RETRO_DEVICE_NONE:
610 default:
611 in_type[port] = PSE_PAD_TYPE_NONE;
612 }
613}
614
615static void update_multitap()
616{
617 struct retro_variable var;
618 int auto_case, port;
619
620 var.value = NULL;
621 var.key = "pcsx_rearmed_multitap1";
622 auto_case = 0;
623 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value))
624 {
625 if (strcmp(var.value, "enabled") == 0)
626 multitap1 = 1;
627 else if (strcmp(var.value, "disabled") == 0)
628 multitap1 = 0;
629 else // 'auto' case
630 auto_case = 1;
631 }
632 else
633 auto_case = 1;
634
635 if (auto_case)
636 {
637 // If a gamepad is plugged after port 2, we need a first multitap.
638 multitap1 = 0;
639 for (port = 2; port < PORTS_NUMBER; port++)
640 multitap1 |= in_type[port] != PSE_PAD_TYPE_NONE;
641 }
642
643 var.value = NULL;
644 var.key = "pcsx_rearmed_multitap2";
645 auto_case = 0;
646 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value))
647 {
648 if (strcmp(var.value, "enabled") == 0)
649 multitap2 = 1;
650 else if (strcmp(var.value, "disabled") == 0)
651 multitap2 = 0;
652 else // 'auto' case
653 auto_case = 1;
654 }
655 else
656 auto_case = 1;
657
658 if (auto_case)
659 {
660 // If a gamepad is plugged after port 4, we need a second multitap.
661 multitap2 = 0;
662 for (port = 4; port < PORTS_NUMBER; port++)
663 multitap2 |= in_type[port] != PSE_PAD_TYPE_NONE;
664 }
665}
666
667void retro_set_controller_port_device(unsigned port, unsigned device)
668{
669 SysPrintf("port %u device %u",port,device);
670
671 if (port >= PORTS_NUMBER)
672 return;
673
674 update_controller_port_device(port, device);
675 update_multitap();
676}
677
678void retro_get_system_info(struct retro_system_info *info)
679{
680 memset(info, 0, sizeof(*info));
681 info->library_name = "PCSX-ReARMed";
682#ifndef GIT_VERSION
683#define GIT_VERSION ""
684#endif
685 info->library_version = "r22" GIT_VERSION;
686 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u";
687 info->need_fullpath = true;
688}
689
690void retro_get_system_av_info(struct retro_system_av_info *info)
691{
692 memset(info, 0, sizeof(*info));
693 info->timing.fps = is_pal_mode ? 50 : 60;
694 info->timing.sample_rate = 44100;
695 info->geometry.base_width = 320;
696 info->geometry.base_height = 240;
697 info->geometry.max_width = VOUT_MAX_WIDTH;
698 info->geometry.max_height = VOUT_MAX_HEIGHT;
699 info->geometry.aspect_ratio = 4.0 / 3.0;
700}
701
702/* savestates */
703size_t retro_serialize_size(void)
704{
705 // it's currently 4380651-4397047 bytes,
706 // but have some reserved for future
707 return 0x440000;
708}
709
710struct save_fp {
711 char *buf;
712 size_t pos;
713 int is_write;
714};
715
716static void *save_open(const char *name, const char *mode)
717{
718 struct save_fp *fp;
719
720 if (name == NULL || mode == NULL)
721 return NULL;
722
723 fp = malloc(sizeof(*fp));
724 if (fp == NULL)
725 return NULL;
726
727 fp->buf = (char *)name;
728 fp->pos = 0;
729 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
730
731 return fp;
732}
733
734static int save_read(void *file, void *buf, u32 len)
735{
736 struct save_fp *fp = file;
737 if (fp == NULL || buf == NULL)
738 return -1;
739
740 memcpy(buf, fp->buf + fp->pos, len);
741 fp->pos += len;
742 return len;
743}
744
745static int save_write(void *file, const void *buf, u32 len)
746{
747 struct save_fp *fp = file;
748 if (fp == NULL || buf == NULL)
749 return -1;
750
751 memcpy(fp->buf + fp->pos, buf, len);
752 fp->pos += len;
753 return len;
754}
755
756static long save_seek(void *file, long offs, int whence)
757{
758 struct save_fp *fp = file;
759 if (fp == NULL)
760 return -1;
761
762 switch (whence) {
763 case SEEK_CUR:
764 fp->pos += offs;
765 return fp->pos;
766 case SEEK_SET:
767 fp->pos = offs;
768 return fp->pos;
769 default:
770 return -1;
771 }
772}
773
774static void save_close(void *file)
775{
776 struct save_fp *fp = file;
777 size_t r_size = retro_serialize_size();
778 if (fp == NULL)
779 return;
780
781 if (fp->pos > r_size)
782 SysPrintf("ERROR: save buffer overflow detected\n");
783 else if (fp->is_write && fp->pos < r_size)
784 // make sure we don't save trash in leftover space
785 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
786 free(fp);
787}
788
789bool retro_serialize(void *data, size_t size)
790{
791 int ret = SaveState(data);
792 return ret == 0 ? true : false;
793}
794
795bool retro_unserialize(const void *data, size_t size)
796{
797 int ret = LoadState(data);
798 return ret == 0 ? true : false;
799}
800
801/* cheats */
802void retro_cheat_reset(void)
803{
804 ClearAllCheats();
805}
806
807void retro_cheat_set(unsigned index, bool enabled, const char *code)
808{
809 char buf[256];
810 int ret;
811
812 // cheat funcs are destructive, need a copy..
813 strncpy(buf, code, sizeof(buf));
814 buf[sizeof(buf) - 1] = 0;
815
816 //Prepare buffered cheat for PCSX's AddCheat fucntion.
817 int cursor=0;
818 int nonhexdec=0;
819 while (buf[cursor]){
820 if (!(ISHEXDEC)){
821 if (++nonhexdec%2){
822 buf[cursor]=' ';
823 } else {
824 buf[cursor]='\n';
825 }
826 }
827 cursor++;
828 }
829
830
831 if (index < NumCheats)
832 ret = EditCheat(index, "", buf);
833 else
834 ret = AddCheat("", buf);
835
836 if (ret != 0)
837 SysPrintf("Failed to set cheat %#u\n", index);
838 else if (index < NumCheats)
839 Cheats[index].Enabled = enabled;
840}
841
842/* multidisk support */
843static bool disk_ejected;
844static unsigned int disk_current_index;
845static unsigned int disk_count;
846static struct disks_state {
847 char *fname;
848 int internal_index; // for multidisk eboots
849} disks[8];
850
851static bool disk_set_eject_state(bool ejected)
852{
853 // weird PCSX API..
854 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
855 LidInterrupt();
856
857 disk_ejected = ejected;
858 return true;
859}
860
861static bool disk_get_eject_state(void)
862{
863 /* can't be controlled by emulated software */
864 return disk_ejected;
865}
866
867static unsigned int disk_get_image_index(void)
868{
869 return disk_current_index;
870}
871
872static bool disk_set_image_index(unsigned int index)
873{
874 if (index >= sizeof(disks) / sizeof(disks[0]))
875 return false;
876
877 CdromId[0] = '\0';
878 CdromLabel[0] = '\0';
879
880 if (disks[index].fname == NULL) {
881 SysPrintf("missing disk #%u\n", index);
882 CDR_shutdown();
883
884 // RetroArch specifies "no disk" with index == count,
885 // so don't fail here..
886 disk_current_index = index;
887 return true;
888 }
889
890 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
891 disks[index].fname, disks[index].internal_index);
892
893 cdrIsoMultidiskSelect = disks[index].internal_index;
894 set_cd_image(disks[index].fname);
895 if (ReloadCdromPlugin() < 0) {
896 SysPrintf("failed to load cdr plugin\n");
897 return false;
898 }
899 if (CDR_open() < 0) {
900 SysPrintf("failed to open cdr plugin\n");
901 return false;
902 }
903
904 if (!disk_ejected) {
905 SetCdOpenCaseTime(time(NULL) + 2);
906 LidInterrupt();
907 }
908
909 disk_current_index = index;
910 return true;
911}
912
913static unsigned int disk_get_num_images(void)
914{
915 return disk_count;
916}
917
918static bool disk_replace_image_index(unsigned index,
919 const struct retro_game_info *info)
920{
921 char *old_fname;
922 bool ret = true;
923
924 if (index >= sizeof(disks) / sizeof(disks[0]))
925 return false;
926
927 old_fname = disks[index].fname;
928 disks[index].fname = NULL;
929 disks[index].internal_index = 0;
930
931 if (info != NULL) {
932 disks[index].fname = strdup(info->path);
933 if (index == disk_current_index)
934 ret = disk_set_image_index(index);
935 }
936
937 if (old_fname != NULL)
938 free(old_fname);
939
940 return ret;
941}
942
943static bool disk_add_image_index(void)
944{
945 if (disk_count >= 8)
946 return false;
947
948 disk_count++;
949 return true;
950}
951
952static struct retro_disk_control_callback disk_control = {
953 .set_eject_state = disk_set_eject_state,
954 .get_eject_state = disk_get_eject_state,
955 .get_image_index = disk_get_image_index,
956 .set_image_index = disk_set_image_index,
957 .get_num_images = disk_get_num_images,
958 .replace_image_index = disk_replace_image_index,
959 .add_image_index = disk_add_image_index,
960};
961
962// just in case, maybe a win-rt port in the future?
963#ifdef _WIN32
964#define SLASH '\\'
965#else
966#define SLASH '/'
967#endif
968
969#ifndef PATH_MAX
970#define PATH_MAX 4096
971#endif
972
973static char base_dir[PATH_MAX];
974
975static bool read_m3u(const char *file)
976{
977 char line[PATH_MAX];
978 char name[PATH_MAX];
979 FILE *f = fopen(file, "r");
980 if (!f)
981 return false;
982
983 while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) {
984 if (line[0] == '#')
985 continue;
986 char *carrige_return = strchr(line, '\r');
987 if (carrige_return)
988 *carrige_return = '\0';
989 char *newline = strchr(line, '\n');
990 if (newline)
991 *newline = '\0';
992
993 if (line[0] != '\0')
994 {
995 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
996 disks[disk_count++].fname = strdup(name);
997 }
998 }
999
1000 fclose(f);
1001 return (disk_count != 0);
1002}
1003
1004static void extract_directory(char *buf, const char *path, size_t size)
1005{
1006 char *base;
1007 strncpy(buf, path, size - 1);
1008 buf[size - 1] = '\0';
1009
1010 base = strrchr(buf, '/');
1011 if (!base)
1012 base = strrchr(buf, '\\');
1013
1014 if (base)
1015 *base = '\0';
1016 else
1017 {
1018 buf[0] = '.';
1019 buf[1] = '\0';
1020 }
1021}
1022
1023#if defined(__QNX__) || defined(_WIN32)
1024/* Blackberry QNX doesn't have strcasestr */
1025
1026/*
1027 * Find the first occurrence of find in s, ignore case.
1028 */
1029char *
1030strcasestr(const char *s, const char*find)
1031{
1032 char c, sc;
1033 size_t len;
1034
1035 if ((c = *find++) != 0) {
1036 c = tolower((unsigned char)c);
1037 len = strlen(find);
1038 do {
1039 do {
1040 if ((sc = *s++) == 0)
1041 return (NULL);
1042 } while ((char)tolower((unsigned char)sc) != c);
1043 } while (strncasecmp(s, find, len) != 0);
1044 s--;
1045 }
1046 return ((char *)s);
1047}
1048#endif
1049
1050bool retro_load_game(const struct retro_game_info *info)
1051{
1052 size_t i;
1053 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
1054
1055 struct retro_input_descriptor desc[] = {
1056 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1057 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1058 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1059 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1060 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1061 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1062 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1063 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1064 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1065 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1066 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1067 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1068 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1069 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1070 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1071 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1072 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1073 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1074 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1075 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1076
1077
1078 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1079 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1080 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1081 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1082 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1083 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1084 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1085 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1086 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1087 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1088 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1089 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1090 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1091 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1092 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1093 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1094 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1095 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1096 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1097 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1098
1099 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1100 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1101 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1102 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1103 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1104 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1105 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1106 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1107 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1108 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1109 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1110 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1111 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1112 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1113 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1114 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1115 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1116 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1117 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1118 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1119
1120 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1121 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1122 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1123 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1124 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1125 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1126 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1127 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1128 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1129 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1130 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1131 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1132 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1133 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1134 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1135 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1136 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1137 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1138 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1139 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1140
1141 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1142 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1143 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1144 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1145 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1146 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1147 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1148 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1149 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1150 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1151 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1152 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1153 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1154 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1155 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1156 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1157 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1158 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1159 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1160 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1161
1162 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1163 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1164 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1165 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1166 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1167 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1168 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1169 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1170 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1171 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1172 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1173 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1174 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1175 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1176 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1177 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1178 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1179 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1180 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1181 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1182
1183 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1184 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1185 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1186 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1187 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1188 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1189 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1190 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1191 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1192 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1193 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1194 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1195 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1196 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1197 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1198 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1199 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1200 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1201 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1202 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1203
1204 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1205 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1206 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1207 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1208 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1209 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1210 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1211 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1212 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1213 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1214 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1215 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1216 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1217 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1218 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1219 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1220 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1221 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1222 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1223 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1224
1225 { 0 },
1226 };
1227
1228 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1229
1230#ifdef FRONTEND_SUPPORTS_RGB565
1231 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
1232 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
1233 SysPrintf("RGB565 supported, using it\n");
1234 }
1235#endif
1236
1237 if (info == NULL || info->path == NULL) {
1238 SysPrintf("info->path required\n");
1239 return false;
1240 }
1241
1242 if (plugins_opened) {
1243 ClosePlugins();
1244 plugins_opened = 0;
1245 }
1246
1247 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
1248 if (disks[i].fname != NULL) {
1249 free(disks[i].fname);
1250 disks[i].fname = NULL;
1251 }
1252 disks[i].internal_index = 0;
1253 }
1254
1255 disk_current_index = 0;
1256 extract_directory(base_dir, info->path, sizeof(base_dir));
1257
1258 if (is_m3u) {
1259 if (!read_m3u(info->path)) {
1260 log_cb(RETRO_LOG_INFO, "failed to read m3u file\n");
1261 return false;
1262 }
1263 } else {
1264 disk_count = 1;
1265 disks[0].fname = strdup(info->path);
1266 }
1267
1268 set_cd_image(disks[0].fname);
1269
1270 /* have to reload after set_cd_image for correct cdr plugin */
1271 if (LoadPlugins() == -1) {
1272 log_cb(RETRO_LOG_INFO, "failed to load plugins\n");
1273 return false;
1274 }
1275
1276 plugins_opened = 1;
1277 NetOpened = 0;
1278
1279 if (OpenPlugins() == -1) {
1280 log_cb(RETRO_LOG_INFO, "failed to open plugins\n");
1281 return false;
1282 }
1283
1284 plugin_call_rearmed_cbs();
1285 dfinput_activate();
1286
1287 if (CheckCdrom() == -1) {
1288 log_cb(RETRO_LOG_INFO, "unsupported/invalid CD image: %s\n", info->path);
1289 return false;
1290 }
1291
1292 SysReset();
1293
1294 if (LoadCdrom() == -1) {
1295 log_cb(RETRO_LOG_INFO, "could not load CD\n");
1296 return false;
1297 }
1298 emu_on_new_cd(0);
1299
1300 // multidisk images
1301 if (!is_m3u) {
1302 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
1303 for (i = 1; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++) {
1304 disks[i].fname = strdup(info->path);
1305 disks[i].internal_index = i;
1306 }
1307 }
1308
1309 return true;
1310}
1311
1312unsigned retro_get_region(void)
1313{
1314 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1315}
1316
1317void *retro_get_memory_data(unsigned id)
1318{
1319 if (id == RETRO_MEMORY_SAVE_RAM)
1320 return Mcd1Data;
1321 else
1322 return NULL;
1323}
1324
1325size_t retro_get_memory_size(unsigned id)
1326{
1327 if (id == RETRO_MEMORY_SAVE_RAM)
1328 return MCD_SIZE;
1329 else
1330 return 0;
1331}
1332
1333void retro_reset(void)
1334{
1335 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
1336 rebootemu = 1;
1337 //SysReset();
1338}
1339
1340static const unsigned short retro_psx_map[] = {
1341 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
1342 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
1343 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
1344 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
1345 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
1346 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
1347 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
1348 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
1349 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
1350 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
1351 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
1352 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
1353 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
1354 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
1355 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
1356 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
1357};
1358#define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
1359
1360static void update_variables(bool in_flight)
1361{
1362 struct retro_variable var;
1363 int i;
1364
1365 var.value = NULL;
1366 var.key = "pcsx_rearmed_frameskip";
1367 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1368 pl_rearmed_cbs.frameskip = atoi(var.value);
1369
1370 var.value = NULL;
1371 var.key = "pcsx_rearmed_region";
1372 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1373 {
1374 Config.PsxAuto = 0;
1375 if (strcmp(var.value, "auto") == 0)
1376 Config.PsxAuto = 1;
1377 else if (strcmp(var.value, "NTSC") == 0)
1378 Config.PsxType = 0;
1379 else if (strcmp(var.value, "PAL") == 0)
1380 Config.PsxType = 1;
1381 }
1382
1383 for (i = 0; i < PORTS_NUMBER; i++)
1384 update_controller_port_variable(i);
1385
1386 update_multitap();
1387
1388 var.value = NULL;
1389 var.key = "pcsx_rearmed_negcon_deadzone";
1390 negcon_deadzone = 0;
1391 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1392 {
1393 negcon_deadzone = (int)(atoi(var.value) * 0.01f * NEGCON_RANGE);
1394 }
1395
1396 var.value = NULL;
1397 var.key = "pcsx_rearmed_negcon_response";
1398 negcon_linearity = 1;
1399 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1400 {
1401 if (strcmp(var.value, "quadratic") == 0){
1402 negcon_linearity = 2;
1403 } else if (strcmp(var.value, "cubic") == 0){
1404 negcon_linearity = 3;
1405 }
1406 }
1407
1408 var.value = NULL;
1409 var.key = "pcsx_rearmed_vibration";
1410
1411 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1412 {
1413 if (strcmp(var.value, "disabled") == 0)
1414 in_enable_vibration = 0;
1415 else if (strcmp(var.value, "enabled") == 0)
1416 in_enable_vibration = 1;
1417 }
1418
1419 var.value = NULL;
1420 var.key = "pcsx_rearmed_dithering";
1421
1422 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1423 {
1424 if (strcmp(var.value, "disabled") == 0) {
1425 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
1426 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 0;
1427#ifdef __ARM_NEON__
1428 pl_rearmed_cbs.gpu_neon.allow_dithering = 0;
1429#endif
1430 }
1431 else if (strcmp(var.value, "enabled") == 0) {
1432 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1433 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 1;
1434#ifdef __ARM_NEON__
1435 pl_rearmed_cbs.gpu_neon.allow_dithering = 1;
1436#endif
1437 }
1438 }
1439
1440#ifdef __ARM_NEON__
1441 var.value = "NULL";
1442 var.key = "pcsx_rearmed_neon_interlace_enable";
1443
1444 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1445 {
1446 if (strcmp(var.value, "disabled") == 0)
1447 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
1448 else if (strcmp(var.value, "enabled") == 0)
1449 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
1450 }
1451
1452 var.value = NULL;
1453 var.key = "pcsx_rearmed_neon_enhancement_enable";
1454
1455 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1456 {
1457 if (strcmp(var.value, "disabled") == 0)
1458 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
1459 else if (strcmp(var.value, "enabled") == 0)
1460 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
1461 }
1462
1463 var.value = NULL;
1464 var.key = "pcsx_rearmed_neon_enhancement_no_main";
1465
1466 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1467 {
1468 if (strcmp(var.value, "disabled") == 0)
1469 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
1470 else if (strcmp(var.value, "enabled") == 0)
1471 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
1472 }
1473#endif
1474
1475 var.value = "NULL";
1476 var.key = "pcsx_rearmed_duping_enable";
1477
1478 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1479 {
1480 if (strcmp(var.value, "disabled") == 0)
1481 duping_enable = false;
1482 else if (strcmp(var.value, "enabled") == 0)
1483 duping_enable = true;
1484 }
1485
1486#ifndef DRC_DISABLE
1487 var.value = NULL;
1488 var.key = "pcsx_rearmed_drc";
1489
1490 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1491 {
1492 R3000Acpu *prev_cpu = psxCpu;
1493
1494#ifdef _3DS
1495 if(!__ctr_svchax)
1496 Config.Cpu = CPU_INTERPRETER;
1497 else
1498#endif
1499 if (strcmp(var.value, "disabled") == 0)
1500 Config.Cpu = CPU_INTERPRETER;
1501 else if (strcmp(var.value, "enabled") == 0)
1502 Config.Cpu = CPU_DYNAREC;
1503
1504 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
1505 if (psxCpu != prev_cpu) {
1506 prev_cpu->Shutdown();
1507 psxCpu->Init();
1508 psxCpu->Reset(); // not really a reset..
1509 }
1510 }
1511#endif
1512
1513 var.value = "NULL";
1514 var.key = "pcsx_rearmed_spu_reverb";
1515
1516 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1517 {
1518 if (strcmp(var.value, "disabled") == 0)
1519 spu_config.iUseReverb = false;
1520 else if (strcmp(var.value, "enabled") == 0)
1521 spu_config.iUseReverb = true;
1522 }
1523
1524 var.value = "NULL";
1525 var.key = "pcsx_rearmed_spu_interpolation";
1526
1527 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1528 {
1529 if (strcmp(var.value, "simple") == 0)
1530 spu_config.iUseInterpolation = 1;
1531 else if (strcmp(var.value, "gaussian") == 0)
1532 spu_config.iUseInterpolation = 2;
1533 else if (strcmp(var.value, "cubic") == 0)
1534 spu_config.iUseInterpolation = 3;
1535 else if (strcmp(var.value, "off") == 0)
1536 spu_config.iUseInterpolation = 0;
1537 }
1538
1539 var.value = "NULL";
1540 var.key = "pcsx_rearmed_pe2_fix";
1541
1542 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1543 {
1544 if (strcmp(var.value, "disabled") == 0)
1545 Config.RCntFix = 0;
1546 else if (strcmp(var.value, "enabled") == 0)
1547 Config.RCntFix = 1;
1548 }
1549
1550 var.value = "NULL";
1551 var.key = "pcsx_rearmed_inuyasha_fix";
1552
1553 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1554 {
1555 if (strcmp(var.value, "disabled") == 0)
1556 Config.VSyncWA = 0;
1557 else if (strcmp(var.value, "enabled") == 0)
1558 Config.VSyncWA = 1;
1559 }
1560
1561 if (in_flight) {
1562 // inform core things about possible config changes
1563 plugin_call_rearmed_cbs();
1564
1565 if (GPU_open != NULL && GPU_close != NULL) {
1566 GPU_close();
1567 GPU_open(&gpuDisp, "PCSX", NULL);
1568 }
1569
1570 dfinput_activate();
1571 }
1572 else{
1573 //not yet running
1574
1575 //bootlogo display hack
1576 if (found_bios) {
1577 var.value = "NULL";
1578 var.key = "pcsx_rearmed_show_bios_bootlogo";
1579 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1580 {
1581 if (strcmp(var.value, "enabled") == 0)
1582 rebootemu = 1;
1583 }
1584 }
1585 }
1586}
1587
1588// Taken from beetle-psx-libretro
1589static uint16_t get_analog_button(retro_input_state_t input_state_cb, int player_index, int id)
1590{
1591 uint16_t button;
1592
1593 // NOTE: Analog buttons were added Nov 2017. Not all front-ends support this
1594 // feature (or pre-date it) so we need to handle this in a graceful way.
1595
1596 // First, try and get an analog value using the new libretro API constant
1597 button = input_state_cb(player_index,
1598 RETRO_DEVICE_ANALOG,
1599 RETRO_DEVICE_INDEX_ANALOG_BUTTON,
1600 id);
1601 button = MIN(button / 128, 255);
1602
1603 if (button == 0)
1604 {
1605 // If we got exactly zero, we're either not pressing the button, or the front-end
1606 // is not reporting analog values. We need to do a second check using the classic
1607 // digital API method, to at least get some response - better than nothing.
1608
1609 // NOTE: If we're really just not holding the button, we're still going to get zero.
1610
1611 button = input_state_cb(player_index,
1612 RETRO_DEVICE_JOYPAD,
1613 0,
1614 id) ? 255 : 0;
1615 }
1616
1617 return button;
1618}
1619
1620void retro_run(void)
1621{
1622 int i;
1623 //SysReset must be run while core is running,Not in menu (Locks up Retroarch)
1624 if(rebootemu != 0){
1625 rebootemu = 0;
1626 SysReset();
1627 }
1628
1629 input_poll_cb();
1630
1631 bool updated = false;
1632 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
1633 update_variables(true);
1634
1635 // reset all keystate, query libretro for keystate
1636 int j;
1637 int lsx;
1638 int rsy;
1639 float negcon_twist_amplitude;
1640 int negcon_i_rs;
1641 int negcon_ii_rs;
1642 for(i = 0; i < PORTS_NUMBER; i++) {
1643 in_keystate[i] = 0;
1644
1645 if (in_type[i] == PSE_PAD_TYPE_NONE)
1646 continue;
1647
1648 if (in_type[i] == PSE_PAD_TYPE_NEGCON)
1649 {
1650 // Query digital inputs
1651 //
1652 // > Pad-Up
1653 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP)){
1654 in_keystate[i] |= (1 << DKEY_UP);
1655 }
1656 // > Pad-Right
1657 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT)){
1658 in_keystate[i] |= (1 << DKEY_RIGHT);
1659 }
1660 // > Pad-Down
1661 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN)){
1662 in_keystate[i] |= (1 << DKEY_DOWN);
1663 }
1664 // > Pad-Left
1665 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT)){
1666 in_keystate[i] |= (1 << DKEY_LEFT);
1667 }
1668 // > Start
1669 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START)){
1670 in_keystate[i] |= (1 << DKEY_START);
1671 }
1672 // > neGcon A
1673 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A)){
1674 in_keystate[i] |= (1 << DKEY_CIRCLE);
1675 }
1676 // > neGcon B
1677 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X)){
1678 in_keystate[i] |= (1 << DKEY_TRIANGLE);
1679 }
1680 // > neGcon R shoulder (digital)
1681 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R)){
1682 in_keystate[i] |= (1 << DKEY_R1);
1683 }
1684 // Query analog inputs
1685 //
1686 // From studying 'libpcsxcore/plugins.c' and 'frontend/plugin.c':
1687 // >> pad->leftJoyX == in_analog_left[i][0] == NeGcon II
1688 // >> pad->leftJoyY == in_analog_left[i][1] == NeGcon L
1689 // >> pad->rightJoyX == in_analog_right[i][0] == NeGcon twist
1690 // >> pad->rightJoyY == in_analog_right[i][1] == NeGcon I
1691 // So we just have to map in_analog_left/right to more
1692 // appropriate inputs...
1693 //
1694 // > NeGcon twist
1695 // >> Get raw analog stick value and account for deadzone
1696 lsx = input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
1697 if (lsx > negcon_deadzone){
1698 lsx = lsx - negcon_deadzone;
1699 } else if (lsx < -negcon_deadzone){
1700 lsx = lsx + negcon_deadzone;
1701 } else {
1702 lsx = 0;
1703 }
1704 // >> Convert to an 'amplitude' [-1.0,1.0] and adjust response
1705 negcon_twist_amplitude = (float)lsx / (float)(NEGCON_RANGE - negcon_deadzone);
1706 if (negcon_linearity == 2){
1707 if (negcon_twist_amplitude < 0.0){
1708 negcon_twist_amplitude = -(negcon_twist_amplitude * negcon_twist_amplitude);
1709 } else {
1710 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude;
1711 }
1712 } else if (negcon_linearity == 3){
1713 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude * negcon_twist_amplitude;
1714 }
1715 // >> Convert to final 'in_analog' integer value [0,255]
1716 in_analog_right[i][0] = MAX(MIN((int)(negcon_twist_amplitude * 128.0f) + 128, 255), 0);
1717 // > NeGcon I + II
1718 // >> Handle right analog stick vertical axis mapping...
1719 // - Up (-Y) == accelerate == neGcon I
1720 // - Down (+Y) == brake == neGcon II
1721 negcon_i_rs = 0;
1722 negcon_ii_rs = 0;
1723 rsy = input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
1724 if (rsy >= 0){
1725 // Account for deadzone
1726 // (Note: have never encountered a gamepad with significant differences
1727 // in deadzone between left/right analog sticks, so use the regular 'twist'
1728 // deadzone here)
1729 if (rsy > negcon_deadzone){
1730 rsy = rsy - negcon_deadzone;
1731 } else {
1732 rsy = 0;
1733 }
1734 // Convert to 'in_analog' integer value [0,255]
1735 negcon_ii_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
1736 } else {
1737 if (rsy < -negcon_deadzone){
1738 rsy = -1 * (rsy + negcon_deadzone);
1739 } else {
1740 rsy = 0;
1741 }
1742 negcon_i_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
1743 }
1744 // >> NeGcon I
1745 in_analog_right[i][1] = MAX(
1746 MAX(
1747 get_analog_button(input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_R2),
1748 get_analog_button(input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_B)
1749 ),
1750 negcon_i_rs
1751 );
1752 // >> NeGcon II
1753 in_analog_left[i][0] = MAX(
1754 MAX(
1755 get_analog_button(input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_L2),
1756 get_analog_button(input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_Y)
1757 ),
1758 negcon_ii_rs
1759 );
1760 // > NeGcon L
1761 in_analog_left[i][1] = get_analog_button(input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_L);
1762 }
1763 else
1764 {
1765 // Query digital inputs
1766 for (j = 0; j < RETRO_PSX_MAP_LEN; j++){
1767 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j)){
1768 in_keystate[i] |= retro_psx_map[j];
1769 }
1770 }
1771 // Query analog inputs
1772 if (in_type[i] == PSE_PAD_TYPE_ANALOGJOY || in_type[i] == PSE_PAD_TYPE_ANALOGPAD)
1773 {
1774 in_analog_left[i][0] = MIN((input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X) / 255) + 128, 255);
1775 in_analog_left[i][1] = MIN((input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y) / 255) + 128, 255);
1776 in_analog_right[i][0] = MIN((input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X) / 255) + 128, 255);
1777 in_analog_right[i][1] = MIN((input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y) / 255) + 128, 255);
1778 }
1779 }
1780 }
1781
1782 stop = 0;
1783 psxCpu->Execute();
1784
1785 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf_ptr : NULL,
1786 vout_width, vout_height, vout_width * 2);
1787 vout_fb_dirty = 0;
1788
1789 set_vout_fb();
1790}
1791
1792static bool try_use_bios(const char *path)
1793{
1794 FILE *f;
1795 long size;
1796 const char *name;
1797
1798 f = fopen(path, "rb");
1799 if (f == NULL)
1800 return false;
1801
1802 fseek(f, 0, SEEK_END);
1803 size = ftell(f);
1804 fclose(f);
1805
1806 if (size != 512 * 1024)
1807 return false;
1808
1809 name = strrchr(path, SLASH);
1810 if (name++ == NULL)
1811 name = path;
1812 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
1813 return true;
1814}
1815
1816#ifndef VITA
1817#include <sys/types.h>
1818#include <dirent.h>
1819
1820static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
1821{
1822 DIR *dir;
1823 struct dirent *ent;
1824 bool ret = false;
1825
1826 dir = opendir(dirpath);
1827 if (dir == NULL)
1828 return false;
1829
1830 while ((ent = readdir(dir))) {
1831 if (strncasecmp(ent->d_name, "scph", 4) != 0)
1832 continue;
1833
1834 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
1835 ret = try_use_bios(path);
1836 if (ret)
1837 break;
1838 }
1839 closedir(dir);
1840 return ret;
1841}
1842#else
1843#define find_any_bios(...) false
1844#endif
1845
1846static void check_system_specs(void)
1847{
1848 unsigned level = 6;
1849 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
1850}
1851
1852static int init_memcards(void)
1853{
1854 int ret = 0;
1855 const char *dir;
1856 struct retro_variable var = { .key="pcsx_rearmed_memcard2", .value=NULL };
1857 static const char CARD2_FILE[] = "pcsx-card2.mcd";
1858
1859 McdDisable[0] = 0;
1860 // Disable memcard 2 by default
1861 McdDisable[1] = 1;
1862 init_memcard(Mcd1Data);
1863 // Memcard 2 is managed by the emulator on the filesystem,
1864 // There is no need to initialize Mcd2Data like Mcd1Data.
1865
1866 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
1867 SysPrintf("Memcard 2: %s\n", var.value);
1868 if (memcmp(var.value, "enabled", 7) == 0) {
1869 if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir) {
1870 if (strlen(dir) + strlen(CARD2_FILE) + 2 > sizeof(Config.Mcd2)) {
1871 SysPrintf("Path '%s' is too long. Cannot use memcard 2. Use a shorter path.\n", dir);
1872 ret = -1;
1873 } else {
1874 McdDisable[1] = 0;
1875 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "%s/%s", dir, CARD2_FILE);
1876 SysPrintf("Use memcard 2: %s\n", Config.Mcd2);
1877 }
1878 } else {
1879 SysPrintf("Could not get save directory! Could not create memcard 2.");
1880 ret = -1;
1881 }
1882 }
1883 }
1884 return ret;
1885}
1886
1887void retro_init(void)
1888{
1889 struct retro_rumble_interface rumble;
1890 const char *bios[] = {
1891 "SCPH101", "SCPH7001", "SCPH5501", "SCPH1001",
1892 "scph101", "scph7001", "scph5501", "scph1001"
1893 };
1894 const char *dir;
1895 char path[256];
1896 int i, ret;
1897
1898 found_bios = false;
1899
1900#ifdef __MACH__
1901 // magic sauce to make the dynarec work on iOS
1902 syscall(SYS_ptrace, 0 /*PTRACE_TRACEME*/, 0, 0, 0);
1903#endif
1904
1905#ifdef _3DS
1906 psxMapHook = pl_3ds_mmap;
1907 psxUnmapHook = pl_3ds_munmap;
1908#endif
1909#ifdef VITA
1910 if(init_vita_mmap()<0)
1911 abort();
1912 psxMapHook = pl_vita_mmap;
1913 psxUnmapHook = pl_vita_munmap;
1914#endif
1915 ret = emu_core_preinit();
1916#ifdef _3DS
1917 /* emu_core_preinit sets the cpu to dynarec */
1918 if(!__ctr_svchax)
1919 Config.Cpu = CPU_INTERPRETER;
1920#endif
1921 ret |= init_memcards();
1922
1923 ret |= emu_core_init();
1924 if (ret != 0) {
1925 SysPrintf("PCSX init failed.\n");
1926 exit(1);
1927 }
1928
1929#ifdef _3DS
1930 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
1931#elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && !defined(VITA)
1932 posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
1933#else
1934 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
1935#endif
1936
1937 vout_buf_ptr = vout_buf;
1938
1939 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
1940 {
1941 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s", dir);
1942
1943 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
1944 snprintf(path, sizeof(path), "%s%c%s.bin", dir, SLASH, bios[i]);
1945 found_bios = try_use_bios(path);
1946 if (found_bios)
1947 break;
1948 }
1949
1950 if (!found_bios)
1951 found_bios = find_any_bios(dir, path, sizeof(path));
1952 }
1953 if (found_bios) {
1954 SysPrintf("found BIOS file: %s\n", Config.Bios);
1955 }
1956 else
1957 {
1958 SysPrintf("no BIOS files found.\n");
1959 struct retro_message msg =
1960 {
1961 "No PlayStation BIOS file found - add for better compatibility",
1962 180
1963 };
1964 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, (void*)&msg);
1965 }
1966
1967 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
1968 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
1969
1970 rumble_cb = NULL;
1971 if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble))
1972 rumble_cb = rumble.set_rumble_state;
1973
1974 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
1975 * we have to do this because cache misses and some IO penalties
1976 * are not emulated. Warning: changing this may break compatibility. */
1977 cycle_multiplier = 175;
1978#ifdef HAVE_PRE_ARMV7
1979 cycle_multiplier = 200;
1980#endif
1981 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1982 spu_config.iUseFixedUpdates = 1;
1983
1984 SaveFuncs.open = save_open;
1985 SaveFuncs.read = save_read;
1986 SaveFuncs.write = save_write;
1987 SaveFuncs.seek = save_seek;
1988 SaveFuncs.close = save_close;
1989
1990 update_variables(false);
1991 check_system_specs();
1992}
1993
1994void retro_deinit(void)
1995{
1996 SysClose();
1997#ifdef _3DS
1998 linearFree(vout_buf);
1999#else
2000 free(vout_buf);
2001#endif
2002 vout_buf = NULL;
2003
2004#ifdef VITA
2005 deinit_vita_mmap();
2006#endif
2007}
2008
2009#ifdef VITA
2010#include <psp2/kernel/threadmgr.h>
2011int usleep (unsigned long us)
2012{
2013 sceKernelDelayThread(us);
2014}
2015#endif