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