Merge pull request #218 from Sakitoshi/master
[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// 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(in_enable_vibration)
434 {
435 rumble.set_rumble_state(pad, RETRO_RUMBLE_STRONG, high << 8);
436 rumble.set_rumble_state(pad, RETRO_RUMBLE_WEAK, low ? 0xffff : 0x0);
437 }
438}
439
440void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
441{
442}
443
444/* sound calls */
445static void snd_feed(void *buf, int bytes)
446{
447 if (audio_batch_cb != NULL)
448 audio_batch_cb(buf, bytes / 4);
449}
450
451void out_register_libretro(struct out_driver *drv)
452{
453 drv->name = "libretro";
454 drv->init = snd_init;
455 drv->finish = snd_finish;
456 drv->busy = snd_busy;
457 drv->feed = snd_feed;
458}
459
460/* libretro */
461void retro_set_environment(retro_environment_t cb)
462{
463 static const struct retro_variable vars[] = {
464 { "pcsx_rearmed_frameskip", "Frameskip; 0|1|2|3" },
465 { "pcsx_rearmed_region", "Region; auto|NTSC|PAL" },
466 { "pcsx_rearmed_memcard2", "Enable second memory card; disabled|enabled" },
467 { "pcsx_rearmed_pad1type", "Pad 1 Type; default|none|standard|analog|dualshock|negcon" },
468 { "pcsx_rearmed_pad2type", "Pad 2 Type; default|none|standard|analog|dualshock|negcon" },
469 { "pcsx_rearmed_pad3type", "Pad 3 Type; default|none|standard|analog|dualshock|negcon" },
470 { "pcsx_rearmed_pad4type", "Pad 4 Type; default|none|standard|analog|dualshock|negcon" },
471 { "pcsx_rearmed_pad5type", "Pad 5 Type; default|none|standard|analog|dualshock|negcon" },
472 { "pcsx_rearmed_pad6type", "Pad 6 Type; default|none|standard|analog|dualshock|negcon" },
473 { "pcsx_rearmed_pad7type", "Pad 7 Type; default|none|standard|analog|dualshock|negcon" },
474 { "pcsx_rearmed_pad8type", "Pad 8 Type; default|none|standard|analog|dualshock|negcon" },
475 { "pcsx_rearmed_multitap1", "Multitap 1; auto|disabled|enabled" },
476 { "pcsx_rearmed_multitap2", "Multitap 2; auto|disabled|enabled" },
477 { "pcsx_rearmed_negcon_deadzone", "NegCon Twist Deadzone (percent); 0|5|10|15|20|25|30" },
478 { "pcsx_rearmed_negcon_response", "NegCon Twist Response; linear|quadratic|cubic" },
479 { "pcsx_rearmed_vibration", "Enable Vibration; enabled|disabled" },
480 { "pcsx_rearmed_dithering", "Enable Dithering; enabled|disabled" },
481#ifndef DRC_DISABLE
482 { "pcsx_rearmed_drc", "Dynamic recompiler; enabled|disabled" },
483#endif
484#if defined(__ARM_NEON__) && !defined(__aarch64__)
485 { "pcsx_rearmed_neon_interlace_enable", "Enable interlacing mode(s); disabled|enabled" },
486 { "pcsx_rearmed_neon_enhancement_enable", "Enhanced resolution (slow); disabled|enabled" },
487 { "pcsx_rearmed_neon_enhancement_no_main", "Enhanced resolution speed hack; disabled|enabled" },
488#endif
489 { "pcsx_rearmed_duping_enable", "Frame duping; enabled|disabled" },
490 { "pcsx_rearmed_show_bios_bootlogo", "Show Bios Bootlogo(Breaks some games); disabled|enabled" },
491 { "pcsx_rearmed_spu_reverb", "Sound: Reverb; enabled|disabled" },
492 { "pcsx_rearmed_spu_interpolation", "Sound: Interpolation; simple|gaussian|cubic|off" },
493 { "pcsx_rearmed_pe2_fix", "Parasite Eve 2/Vandal Hearts 1/2 Fix; disabled|enabled" },
494 { "pcsx_rearmed_inuyasha_fix", "InuYasha Sengoku Battle Fix; disabled|enabled" },
495 { NULL, NULL },
496 };
497
498 if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
499 log_cb = logging.log;
500
501 environ_cb = cb;
502
503 cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)vars);
504}
505
506void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
507void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
508void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
509void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
510void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
511
512unsigned retro_api_version(void)
513{
514 return RETRO_API_VERSION;
515}
516
517static int controller_port_variable(unsigned port, struct retro_variable *var)
518{
519 if (port >= PORTS_NUMBER)
520 return 0;
521
522 if (!environ_cb)
523 return 0;
524
525 var->value = NULL;
526 switch (port) {
527 case 0:
528 var->key = "pcsx_rearmed_pad1type";
529 break;
530 case 1:
531 var->key = "pcsx_rearmed_pad2type";
532 break;
533 case 2:
534 var->key = "pcsx_rearmed_pad3type";
535 break;
536 case 3:
537 var->key = "pcsx_rearmed_pad4type";
538 break;
539 case 4:
540 var->key = "pcsx_rearmed_pad5type";
541 break;
542 case 5:
543 var->key = "pcsx_rearmed_pad6type";
544 break;
545 case 6:
546 var->key = "pcsx_rearmed_pad7type";
547 break;
548 case 7:
549 var->key = "pcsx_rearmed_pad8type";
550 break;
551 }
552
553 return environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, var) || var->value;
554}
555
556static void update_controller_port_variable(unsigned port)
557{
558 if (port >= PORTS_NUMBER)
559 return;
560
561 struct retro_variable var;
562
563 if (controller_port_variable(port, &var))
564 {
565 if (strcmp(var.value, "standard") == 0)
566 in_type[port] = PSE_PAD_TYPE_STANDARD;
567 else if (strcmp(var.value, "analog") == 0)
568 in_type[port] = PSE_PAD_TYPE_ANALOGJOY;
569 else if (strcmp(var.value, "dualshock") == 0)
570 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
571 else if (strcmp(var.value, "negcon") == 0)
572 in_type[port] = PSE_PAD_TYPE_NEGCON;
573 else if (strcmp(var.value, "none") == 0)
574 in_type[port] = PSE_PAD_TYPE_NONE;
575 // else 'default' case, do nothing
576 }
577}
578
579static void update_controller_port_device(unsigned port, unsigned device)
580{
581 if (port >= PORTS_NUMBER)
582 return;
583
584 struct retro_variable var;
585
586 if (!controller_port_variable(port, &var))
587 return;
588
589 if (strcmp(var.value, "default") != 0)
590 return;
591
592 switch (device)
593 {
594 case RETRO_DEVICE_JOYPAD:
595 in_type[port] = PSE_PAD_TYPE_STANDARD;
596 break;
597 case RETRO_DEVICE_ANALOG:
598 in_type[port] = PSE_PAD_TYPE_ANALOGPAD;
599 break;
600 case RETRO_DEVICE_MOUSE:
601 in_type[port] = PSE_PAD_TYPE_MOUSE;
602 break;
603 case RETRO_DEVICE_LIGHTGUN:
604 in_type[port] = PSE_PAD_TYPE_GUN;
605 break;
606 case RETRO_DEVICE_NONE:
607 default:
608 in_type[port] = PSE_PAD_TYPE_NONE;
609 }
610}
611
612static void update_multitap()
613{
614 struct retro_variable var;
615 int auto_case, port;
616
617 var.value = NULL;
618 var.key = "pcsx_rearmed_multitap1";
619 auto_case = 0;
620 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value))
621 {
622 if (strcmp(var.value, "enabled") == 0)
623 multitap1 = 1;
624 else if (strcmp(var.value, "disabled") == 0)
625 multitap1 = 0;
626 else // 'auto' case
627 auto_case = 1;
628 }
629 else
630 auto_case = 1;
631
632 if (auto_case)
633 {
634 // If a gamepad is plugged after port 2, we need a first multitap.
635 multitap1 = 0;
636 for (port = 2; port < PORTS_NUMBER; port++)
637 multitap1 |= in_type[port] != PSE_PAD_TYPE_NONE;
638 }
639
640 var.value = NULL;
641 var.key = "pcsx_rearmed_multitap2";
642 auto_case = 0;
643 if (environ_cb && (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value))
644 {
645 if (strcmp(var.value, "enabled") == 0)
646 multitap2 = 1;
647 else if (strcmp(var.value, "disabled") == 0)
648 multitap2 = 0;
649 else // 'auto' case
650 auto_case = 1;
651 }
652 else
653 auto_case = 1;
654
655 if (auto_case)
656 {
657 // If a gamepad is plugged after port 4, we need a second multitap.
658 multitap2 = 0;
659 for (port = 4; port < PORTS_NUMBER; port++)
660 multitap2 |= in_type[port] != PSE_PAD_TYPE_NONE;
661 }
662}
663
664void retro_set_controller_port_device(unsigned port, unsigned device)
665{
666 SysPrintf("port %u device %u",port,device);
667
668 if (port >= PORTS_NUMBER)
669 return;
670
671 update_controller_port_device(port, device);
672 update_multitap();
673}
674
675void retro_get_system_info(struct retro_system_info *info)
676{
677 memset(info, 0, sizeof(*info));
678 info->library_name = "PCSX-ReARMed";
679#ifndef GIT_VERSION
680#define GIT_VERSION ""
681#endif
682 info->library_version = "r22" GIT_VERSION;
683 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u";
684 info->need_fullpath = true;
685}
686
687void retro_get_system_av_info(struct retro_system_av_info *info)
688{
689 memset(info, 0, sizeof(*info));
690 info->timing.fps = is_pal_mode ? 50 : 60;
691 info->timing.sample_rate = 44100;
692 info->geometry.base_width = 320;
693 info->geometry.base_height = 240;
694 info->geometry.max_width = VOUT_MAX_WIDTH;
695 info->geometry.max_height = VOUT_MAX_HEIGHT;
696 info->geometry.aspect_ratio = 4.0 / 3.0;
697}
698
699/* savestates */
700size_t retro_serialize_size(void)
701{
702 // it's currently 4380651-4397047 bytes,
703 // but have some reserved for future
704 return 0x440000;
705}
706
707struct save_fp {
708 char *buf;
709 size_t pos;
710 int is_write;
711};
712
713static void *save_open(const char *name, const char *mode)
714{
715 struct save_fp *fp;
716
717 if (name == NULL || mode == NULL)
718 return NULL;
719
720 fp = malloc(sizeof(*fp));
721 if (fp == NULL)
722 return NULL;
723
724 fp->buf = (char *)name;
725 fp->pos = 0;
726 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
727
728 return fp;
729}
730
731static int save_read(void *file, void *buf, u32 len)
732{
733 struct save_fp *fp = file;
734 if (fp == NULL || buf == NULL)
735 return -1;
736
737 memcpy(buf, fp->buf + fp->pos, len);
738 fp->pos += len;
739 return len;
740}
741
742static int save_write(void *file, const void *buf, u32 len)
743{
744 struct save_fp *fp = file;
745 if (fp == NULL || buf == NULL)
746 return -1;
747
748 memcpy(fp->buf + fp->pos, buf, len);
749 fp->pos += len;
750 return len;
751}
752
753static long save_seek(void *file, long offs, int whence)
754{
755 struct save_fp *fp = file;
756 if (fp == NULL)
757 return -1;
758
759 switch (whence) {
760 case SEEK_CUR:
761 fp->pos += offs;
762 return fp->pos;
763 case SEEK_SET:
764 fp->pos = offs;
765 return fp->pos;
766 default:
767 return -1;
768 }
769}
770
771static void save_close(void *file)
772{
773 struct save_fp *fp = file;
774 size_t r_size = retro_serialize_size();
775 if (fp == NULL)
776 return;
777
778 if (fp->pos > r_size)
779 SysPrintf("ERROR: save buffer overflow detected\n");
780 else if (fp->is_write && fp->pos < r_size)
781 // make sure we don't save trash in leftover space
782 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
783 free(fp);
784}
785
786bool retro_serialize(void *data, size_t size)
787{
788 int ret = SaveState(data);
789 return ret == 0 ? true : false;
790}
791
792bool retro_unserialize(const void *data, size_t size)
793{
794 int ret = LoadState(data);
795 return ret == 0 ? true : false;
796}
797
798/* cheats */
799void retro_cheat_reset(void)
800{
801 ClearAllCheats();
802}
803
804void retro_cheat_set(unsigned index, bool enabled, const char *code)
805{
806 char buf[256];
807 int ret;
808
809 // cheat funcs are destructive, need a copy..
810 strncpy(buf, code, sizeof(buf));
811 buf[sizeof(buf) - 1] = 0;
812
813 //Prepare buffered cheat for PCSX's AddCheat fucntion.
814 int cursor=0;
815 int nonhexdec=0;
816 while (buf[cursor]){
817 if (!(ISHEXDEC)){
818 if (++nonhexdec%2){
819 buf[cursor]=' ';
820 } else {
821 buf[cursor]='\n';
822 }
823 }
824 cursor++;
825 }
826
827
828 if (index < NumCheats)
829 ret = EditCheat(index, "", buf);
830 else
831 ret = AddCheat("", buf);
832
833 if (ret != 0)
834 SysPrintf("Failed to set cheat %#u\n", index);
835 else if (index < NumCheats)
836 Cheats[index].Enabled = enabled;
837}
838
839/* multidisk support */
840static bool disk_ejected;
841static unsigned int disk_current_index;
842static unsigned int disk_count;
843static struct disks_state {
844 char *fname;
845 int internal_index; // for multidisk eboots
846} disks[8];
847
848static bool disk_set_eject_state(bool ejected)
849{
850 // weird PCSX API..
851 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
852 LidInterrupt();
853
854 disk_ejected = ejected;
855 return true;
856}
857
858static bool disk_get_eject_state(void)
859{
860 /* can't be controlled by emulated software */
861 return disk_ejected;
862}
863
864static unsigned int disk_get_image_index(void)
865{
866 return disk_current_index;
867}
868
869static bool disk_set_image_index(unsigned int index)
870{
871 if (index >= sizeof(disks) / sizeof(disks[0]))
872 return false;
873
874 CdromId[0] = '\0';
875 CdromLabel[0] = '\0';
876
877 if (disks[index].fname == NULL) {
878 SysPrintf("missing disk #%u\n", index);
879 CDR_shutdown();
880
881 // RetroArch specifies "no disk" with index == count,
882 // so don't fail here..
883 disk_current_index = index;
884 return true;
885 }
886
887 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
888 disks[index].fname, disks[index].internal_index);
889
890 cdrIsoMultidiskSelect = disks[index].internal_index;
891 set_cd_image(disks[index].fname);
892 if (ReloadCdromPlugin() < 0) {
893 SysPrintf("failed to load cdr plugin\n");
894 return false;
895 }
896 if (CDR_open() < 0) {
897 SysPrintf("failed to open cdr plugin\n");
898 return false;
899 }
900
901 if (!disk_ejected) {
902 SetCdOpenCaseTime(time(NULL) + 2);
903 LidInterrupt();
904 }
905
906 disk_current_index = index;
907 return true;
908}
909
910static unsigned int disk_get_num_images(void)
911{
912 return disk_count;
913}
914
915static bool disk_replace_image_index(unsigned index,
916 const struct retro_game_info *info)
917{
918 char *old_fname;
919 bool ret = true;
920
921 if (index >= sizeof(disks) / sizeof(disks[0]))
922 return false;
923
924 old_fname = disks[index].fname;
925 disks[index].fname = NULL;
926 disks[index].internal_index = 0;
927
928 if (info != NULL) {
929 disks[index].fname = strdup(info->path);
930 if (index == disk_current_index)
931 ret = disk_set_image_index(index);
932 }
933
934 if (old_fname != NULL)
935 free(old_fname);
936
937 return ret;
938}
939
940static bool disk_add_image_index(void)
941{
942 if (disk_count >= 8)
943 return false;
944
945 disk_count++;
946 return true;
947}
948
949static struct retro_disk_control_callback disk_control = {
950 .set_eject_state = disk_set_eject_state,
951 .get_eject_state = disk_get_eject_state,
952 .get_image_index = disk_get_image_index,
953 .set_image_index = disk_set_image_index,
954 .get_num_images = disk_get_num_images,
955 .replace_image_index = disk_replace_image_index,
956 .add_image_index = disk_add_image_index,
957};
958
959// just in case, maybe a win-rt port in the future?
960#ifdef _WIN32
961#define SLASH '\\'
962#else
963#define SLASH '/'
964#endif
965
966#ifndef PATH_MAX
967#define PATH_MAX 4096
968#endif
969
970static char base_dir[PATH_MAX];
971
972static bool read_m3u(const char *file)
973{
974 char line[PATH_MAX];
975 char name[PATH_MAX];
976 FILE *f = fopen(file, "r");
977 if (!f)
978 return false;
979
980 while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) {
981 if (line[0] == '#')
982 continue;
983 char *carrige_return = strchr(line, '\r');
984 if (carrige_return)
985 *carrige_return = '\0';
986 char *newline = strchr(line, '\n');
987 if (newline)
988 *newline = '\0';
989
990 if (line[0] != '\0')
991 {
992 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
993 disks[disk_count++].fname = strdup(name);
994 }
995 }
996
997 fclose(f);
998 return (disk_count != 0);
999}
1000
1001static void extract_directory(char *buf, const char *path, size_t size)
1002{
1003 char *base;
1004 strncpy(buf, path, size - 1);
1005 buf[size - 1] = '\0';
1006
1007 base = strrchr(buf, '/');
1008 if (!base)
1009 base = strrchr(buf, '\\');
1010
1011 if (base)
1012 *base = '\0';
1013 else
1014 {
1015 buf[0] = '.';
1016 buf[1] = '\0';
1017 }
1018}
1019
1020#if defined(__QNX__) || defined(_WIN32)
1021/* Blackberry QNX doesn't have strcasestr */
1022
1023/*
1024 * Find the first occurrence of find in s, ignore case.
1025 */
1026char *
1027strcasestr(const char *s, const char*find)
1028{
1029 char c, sc;
1030 size_t len;
1031
1032 if ((c = *find++) != 0) {
1033 c = tolower((unsigned char)c);
1034 len = strlen(find);
1035 do {
1036 do {
1037 if ((sc = *s++) == 0)
1038 return (NULL);
1039 } while ((char)tolower((unsigned char)sc) != c);
1040 } while (strncasecmp(s, find, len) != 0);
1041 s--;
1042 }
1043 return ((char *)s);
1044}
1045#endif
1046
1047bool retro_load_game(const struct retro_game_info *info)
1048{
1049 size_t i;
1050 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
1051
1052 struct retro_input_descriptor desc[] = {
1053 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1054 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1055 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1056 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1057 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1058 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1059 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1060 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1061 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1062 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1063 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1064 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1065 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1066 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1067 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1068 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1069 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1070 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1071 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1072 { 0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1073
1074
1075 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1076 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1077 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1078 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1079 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1080 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1081 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1082 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1083 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1084 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1085 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1086 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1087 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1088 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1089 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1090 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1091 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1092 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1093 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1094 { 1, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1095
1096 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1097 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1098 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1099 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1100 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1101 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1102 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1103 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1104 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1105 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1106 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1107 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1108 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1109 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1110 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1111 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1112 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1113 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1114 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1115 { 2, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1116
1117 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1118 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1119 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1120 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1121 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1122 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1123 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1124 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1125 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1126 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1127 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1128 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1129 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1130 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1131 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1132 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1133 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1134 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1135 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1136 { 3, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1137
1138 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1139 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1140 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1141 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1142 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1143 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1144 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1145 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1146 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1147 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1148 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1149 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1150 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1151 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1152 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1153 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1154 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1155 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1156 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1157 { 4, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1158
1159 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1160 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1161 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1162 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1163 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1164 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1165 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1166 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1167 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1168 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1169 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1170 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1171 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1172 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1173 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1174 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1175 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1176 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1177 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1178 { 5, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1179
1180 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1181 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1182 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1183 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1184 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1185 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1186 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1187 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1188 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1189 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1190 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1191 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1192 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1193 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1194 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1195 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1196 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1197 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1198 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1199 { 6, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1200
1201 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
1202 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
1203 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
1204 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
1205 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Cross" },
1206 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Circle" },
1207 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Triangle" },
1208 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Square" },
1209 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L1" },
1210 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
1211 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" },
1212 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R1" },
1213 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
1214 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
1215 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
1216 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
1217 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X, "Left Analog X" },
1218 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y, "Left Analog Y" },
1219 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X, "Right Analog X" },
1220 { 7, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y, "Right Analog Y" },
1221
1222 { 0 },
1223 };
1224
1225 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
1226
1227#ifdef FRONTEND_SUPPORTS_RGB565
1228 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
1229 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
1230 SysPrintf("RGB565 supported, using it\n");
1231 }
1232#endif
1233
1234 if (info == NULL || info->path == NULL) {
1235 SysPrintf("info->path required\n");
1236 return false;
1237 }
1238
1239 if (plugins_opened) {
1240 ClosePlugins();
1241 plugins_opened = 0;
1242 }
1243
1244 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
1245 if (disks[i].fname != NULL) {
1246 free(disks[i].fname);
1247 disks[i].fname = NULL;
1248 }
1249 disks[i].internal_index = 0;
1250 }
1251
1252 disk_current_index = 0;
1253 extract_directory(base_dir, info->path, sizeof(base_dir));
1254
1255 if (is_m3u) {
1256 if (!read_m3u(info->path)) {
1257 log_cb(RETRO_LOG_INFO, "failed to read m3u file\n");
1258 return false;
1259 }
1260 } else {
1261 disk_count = 1;
1262 disks[0].fname = strdup(info->path);
1263 }
1264
1265 set_cd_image(disks[0].fname);
1266
1267 /* have to reload after set_cd_image for correct cdr plugin */
1268 if (LoadPlugins() == -1) {
1269 log_cb(RETRO_LOG_INFO, "failed to load plugins\n");
1270 return false;
1271 }
1272
1273 plugins_opened = 1;
1274 NetOpened = 0;
1275
1276 if (OpenPlugins() == -1) {
1277 log_cb(RETRO_LOG_INFO, "failed to open plugins\n");
1278 return false;
1279 }
1280
1281 plugin_call_rearmed_cbs();
1282 dfinput_activate();
1283
1284 if (CheckCdrom() == -1) {
1285 log_cb(RETRO_LOG_INFO, "unsupported/invalid CD image: %s\n", info->path);
1286 return false;
1287 }
1288
1289 SysReset();
1290
1291 if (LoadCdrom() == -1) {
1292 log_cb(RETRO_LOG_INFO, "could not load CD\n");
1293 return false;
1294 }
1295 emu_on_new_cd(0);
1296
1297 // multidisk images
1298 if (!is_m3u) {
1299 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
1300 for (i = 1; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++) {
1301 disks[i].fname = strdup(info->path);
1302 disks[i].internal_index = i;
1303 }
1304 }
1305
1306 return true;
1307}
1308
1309unsigned retro_get_region(void)
1310{
1311 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1312}
1313
1314void *retro_get_memory_data(unsigned id)
1315{
1316 if (id == RETRO_MEMORY_SAVE_RAM)
1317 return Mcd1Data;
1318 else
1319 return NULL;
1320}
1321
1322size_t retro_get_memory_size(unsigned id)
1323{
1324 if (id == RETRO_MEMORY_SAVE_RAM)
1325 return MCD_SIZE;
1326 else
1327 return 0;
1328}
1329
1330void retro_reset(void)
1331{
1332 //hack to prevent retroarch freezing when reseting in the menu but not while running with the hot key
1333 rebootemu = 1;
1334 //SysReset();
1335}
1336
1337static const unsigned short retro_psx_map[] = {
1338 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
1339 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
1340 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
1341 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
1342 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
1343 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
1344 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
1345 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
1346 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
1347 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
1348 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
1349 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
1350 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
1351 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
1352 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
1353 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
1354};
1355#define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
1356
1357static void update_variables(bool in_flight)
1358{
1359 struct retro_variable var;
1360 int i;
1361
1362 var.value = NULL;
1363 var.key = "pcsx_rearmed_frameskip";
1364 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1365 pl_rearmed_cbs.frameskip = atoi(var.value);
1366
1367 var.value = NULL;
1368 var.key = "pcsx_rearmed_region";
1369 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1370 {
1371 Config.PsxAuto = 0;
1372 if (strcmp(var.value, "auto") == 0)
1373 Config.PsxAuto = 1;
1374 else if (strcmp(var.value, "NTSC") == 0)
1375 Config.PsxType = 0;
1376 else if (strcmp(var.value, "PAL") == 0)
1377 Config.PsxType = 1;
1378 }
1379
1380 for (i = 0; i < PORTS_NUMBER; i++)
1381 update_controller_port_variable(i);
1382
1383 update_multitap();
1384
1385 var.value = NULL;
1386 var.key = "pcsx_rearmed_negcon_deadzone";
1387 negcon_deadzone = 0;
1388 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1389 {
1390 negcon_deadzone = (int)(atoi(var.value) * 0.01f * NEGCON_RANGE);
1391 }
1392
1393 var.value = NULL;
1394 var.key = "pcsx_rearmed_negcon_response";
1395 negcon_linearity = 1;
1396 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1397 {
1398 if (strcmp(var.value, "quadratic") == 0){
1399 negcon_linearity = 2;
1400 } else if (strcmp(var.value, "cubic") == 0){
1401 negcon_linearity = 3;
1402 }
1403 }
1404
1405 var.value = NULL;
1406 var.key = "pcsx_rearmed_vibration";
1407
1408 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1409 {
1410 if (strcmp(var.value, "disabled") == 0)
1411 in_enable_vibration = 0;
1412 else if (strcmp(var.value, "enabled") == 0)
1413 in_enable_vibration = 1;
1414 }
1415
1416 var.value = NULL;
1417 var.key = "pcsx_rearmed_dithering";
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_peops.iUseDither = 0;
1423 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 0;
1424#ifdef __ARM_NEON__
1425 pl_rearmed_cbs.gpu_neon.allow_dithering = 0;
1426#endif
1427 }
1428 else if (strcmp(var.value, "enabled") == 0) {
1429 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1430 pl_rearmed_cbs.gpu_peopsgl.bDrawDither = 1;
1431#ifdef __ARM_NEON__
1432 pl_rearmed_cbs.gpu_neon.allow_dithering = 1;
1433#endif
1434 }
1435 }
1436
1437#ifdef __ARM_NEON__
1438 var.value = "NULL";
1439 var.key = "pcsx_rearmed_neon_interlace_enable";
1440
1441 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1442 {
1443 if (strcmp(var.value, "disabled") == 0)
1444 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
1445 else if (strcmp(var.value, "enabled") == 0)
1446 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
1447 }
1448
1449 var.value = NULL;
1450 var.key = "pcsx_rearmed_neon_enhancement_enable";
1451
1452 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1453 {
1454 if (strcmp(var.value, "disabled") == 0)
1455 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
1456 else if (strcmp(var.value, "enabled") == 0)
1457 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
1458 }
1459
1460 var.value = NULL;
1461 var.key = "pcsx_rearmed_neon_enhancement_no_main";
1462
1463 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1464 {
1465 if (strcmp(var.value, "disabled") == 0)
1466 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
1467 else if (strcmp(var.value, "enabled") == 0)
1468 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
1469 }
1470#endif
1471
1472 var.value = "NULL";
1473 var.key = "pcsx_rearmed_duping_enable";
1474
1475 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1476 {
1477 if (strcmp(var.value, "disabled") == 0)
1478 duping_enable = false;
1479 else if (strcmp(var.value, "enabled") == 0)
1480 duping_enable = true;
1481 }
1482
1483#ifndef DRC_DISABLE
1484 var.value = NULL;
1485 var.key = "pcsx_rearmed_drc";
1486
1487 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1488 {
1489 R3000Acpu *prev_cpu = psxCpu;
1490
1491#ifdef _3DS
1492 if(!__ctr_svchax)
1493 Config.Cpu = CPU_INTERPRETER;
1494 else
1495#endif
1496 if (strcmp(var.value, "disabled") == 0)
1497 Config.Cpu = CPU_INTERPRETER;
1498 else if (strcmp(var.value, "enabled") == 0)
1499 Config.Cpu = CPU_DYNAREC;
1500
1501 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
1502 if (psxCpu != prev_cpu) {
1503 prev_cpu->Shutdown();
1504 psxCpu->Init();
1505 psxCpu->Reset(); // not really a reset..
1506 }
1507 }
1508#endif
1509
1510 var.value = "NULL";
1511 var.key = "pcsx_rearmed_spu_reverb";
1512
1513 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1514 {
1515 if (strcmp(var.value, "disabled") == 0)
1516 spu_config.iUseReverb = false;
1517 else if (strcmp(var.value, "enabled") == 0)
1518 spu_config.iUseReverb = true;
1519 }
1520
1521 var.value = "NULL";
1522 var.key = "pcsx_rearmed_spu_interpolation";
1523
1524 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1525 {
1526 if (strcmp(var.value, "simple") == 0)
1527 spu_config.iUseInterpolation = 1;
1528 else if (strcmp(var.value, "gaussian") == 0)
1529 spu_config.iUseInterpolation = 2;
1530 else if (strcmp(var.value, "cubic") == 0)
1531 spu_config.iUseInterpolation = 3;
1532 else if (strcmp(var.value, "off") == 0)
1533 spu_config.iUseInterpolation = 0;
1534 }
1535
1536 var.value = "NULL";
1537 var.key = "pcsx_rearmed_pe2_fix";
1538
1539 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1540 {
1541 if (strcmp(var.value, "disabled") == 0)
1542 Config.RCntFix = 0;
1543 else if (strcmp(var.value, "enabled") == 0)
1544 Config.RCntFix = 1;
1545 }
1546
1547 var.value = "NULL";
1548 var.key = "pcsx_rearmed_inuyasha_fix";
1549
1550 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1551 {
1552 if (strcmp(var.value, "disabled") == 0)
1553 Config.VSyncWA = 0;
1554 else if (strcmp(var.value, "enabled") == 0)
1555 Config.VSyncWA = 1;
1556 }
1557
1558 if (in_flight) {
1559 // inform core things about possible config changes
1560 plugin_call_rearmed_cbs();
1561
1562 if (GPU_open != NULL && GPU_close != NULL) {
1563 GPU_close();
1564 GPU_open(&gpuDisp, "PCSX", NULL);
1565 }
1566
1567 dfinput_activate();
1568 }
1569 else{
1570 //not yet running
1571
1572 //bootlogo display hack
1573 if (found_bios) {
1574 var.value = "NULL";
1575 var.key = "pcsx_rearmed_show_bios_bootlogo";
1576 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
1577 {
1578 if (strcmp(var.value, "enabled") == 0)
1579 rebootemu = 1;
1580 }
1581 }
1582 }
1583}
1584
1585// Taken from beetle-psx-libretro
1586static uint16_t get_analog_button(retro_input_state_t input_state_cb, int player_index, int id)
1587{
1588 uint16_t button;
1589
1590 // NOTE: Analog buttons were added Nov 2017. Not all front-ends support this
1591 // feature (or pre-date it) so we need to handle this in a graceful way.
1592
1593 // First, try and get an analog value using the new libretro API constant
1594 button = input_state_cb(player_index,
1595 RETRO_DEVICE_ANALOG,
1596 RETRO_DEVICE_INDEX_ANALOG_BUTTON,
1597 id);
1598 button = MIN(button / 128, 255);
1599
1600 if (button == 0)
1601 {
1602 // If we got exactly zero, we're either not pressing the button, or the front-end
1603 // is not reporting analog values. We need to do a second check using the classic
1604 // digital API method, to at least get some response - better than nothing.
1605
1606 // NOTE: If we're really just not holding the button, we're still going to get zero.
1607
1608 button = input_state_cb(player_index,
1609 RETRO_DEVICE_JOYPAD,
1610 0,
1611 id) ? 255 : 0;
1612 }
1613
1614 return button;
1615}
1616
1617void retro_run(void)
1618{
1619 int i;
1620 //SysReset must be run while core is running,Not in menu (Locks up Retroarch)
1621 if(rebootemu != 0){
1622 rebootemu = 0;
1623 SysReset();
1624 }
1625
1626 input_poll_cb();
1627
1628 bool updated = false;
1629 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
1630 update_variables(true);
1631
1632 // reset all keystate, query libretro for keystate
1633 int j;
1634 int lsx;
1635 int rsy;
1636 float negcon_twist_amplitude;
1637 int negcon_i_rs;
1638 int negcon_ii_rs;
1639 for(i = 0; i < PORTS_NUMBER; i++) {
1640 in_keystate[i] = 0;
1641
1642 if (in_type[i] == PSE_PAD_TYPE_NONE)
1643 continue;
1644
1645 if (in_type[i] == PSE_PAD_TYPE_NEGCON)
1646 {
1647 // Query digital inputs
1648 //
1649 // > Pad-Up
1650 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP)){
1651 in_keystate[i] |= (1 << DKEY_UP);
1652 }
1653 // > Pad-Right
1654 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT)){
1655 in_keystate[i] |= (1 << DKEY_RIGHT);
1656 }
1657 // > Pad-Down
1658 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN)){
1659 in_keystate[i] |= (1 << DKEY_DOWN);
1660 }
1661 // > Pad-Left
1662 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT)){
1663 in_keystate[i] |= (1 << DKEY_LEFT);
1664 }
1665 // > Start
1666 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START)){
1667 in_keystate[i] |= (1 << DKEY_START);
1668 }
1669 // > neGcon A
1670 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A)){
1671 in_keystate[i] |= (1 << DKEY_CIRCLE);
1672 }
1673 // > neGcon B
1674 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X)){
1675 in_keystate[i] |= (1 << DKEY_TRIANGLE);
1676 }
1677 // > neGcon R shoulder (digital)
1678 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R)){
1679 in_keystate[i] |= (1 << DKEY_R1);
1680 }
1681 // Query analog inputs
1682 //
1683 // From studying 'libpcsxcore/plugins.c' and 'frontend/plugin.c':
1684 // >> pad->leftJoyX == in_analog_left[i][0] == NeGcon II
1685 // >> pad->leftJoyY == in_analog_left[i][1] == NeGcon L
1686 // >> pad->rightJoyX == in_analog_right[i][0] == NeGcon twist
1687 // >> pad->rightJoyY == in_analog_right[i][1] == NeGcon I
1688 // So we just have to map in_analog_left/right to more
1689 // appropriate inputs...
1690 //
1691 // > NeGcon twist
1692 // >> Get raw analog stick value and account for deadzone
1693 lsx = input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
1694 if (lsx > negcon_deadzone){
1695 lsx = lsx - negcon_deadzone;
1696 } else if (lsx < -negcon_deadzone){
1697 lsx = lsx + negcon_deadzone;
1698 } else {
1699 lsx = 0;
1700 }
1701 // >> Convert to an 'amplitude' [-1.0,1.0] and adjust response
1702 negcon_twist_amplitude = (float)lsx / (float)(NEGCON_RANGE - negcon_deadzone);
1703 if (negcon_linearity == 2){
1704 if (negcon_twist_amplitude < 0.0){
1705 negcon_twist_amplitude = -(negcon_twist_amplitude * negcon_twist_amplitude);
1706 } else {
1707 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude;
1708 }
1709 } else if (negcon_linearity == 3){
1710 negcon_twist_amplitude = negcon_twist_amplitude * negcon_twist_amplitude * negcon_twist_amplitude;
1711 }
1712 // >> Convert to final 'in_analog' integer value [0,255]
1713 in_analog_right[i][0] = MAX(MIN((int)(negcon_twist_amplitude * 128.0f) + 128, 255), 0);
1714 // > NeGcon I + II
1715 // >> Handle right analog stick vertical axis mapping...
1716 // - Up (-Y) == accelerate == neGcon I
1717 // - Down (+Y) == brake == neGcon II
1718 negcon_i_rs = 0;
1719 negcon_ii_rs = 0;
1720 rsy = input_state_cb(i, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y);
1721 if (rsy >= 0){
1722 // Account for deadzone
1723 // (Note: have never encountered a gamepad with significant differences
1724 // in deadzone between left/right analog sticks, so use the regular 'twist'
1725 // deadzone here)
1726 if (rsy > negcon_deadzone){
1727 rsy = rsy - negcon_deadzone;
1728 } else {
1729 rsy = 0;
1730 }
1731 // Convert to 'in_analog' integer value [0,255]
1732 negcon_ii_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
1733 } else {
1734 if (rsy < -negcon_deadzone){
1735 rsy = -1 * (rsy + negcon_deadzone);
1736 } else {
1737 rsy = 0;
1738 }
1739 negcon_i_rs = MIN((int)(((float)rsy / (float)(NEGCON_RANGE - negcon_deadzone)) * 255.0f), 255);
1740 }
1741 // >> NeGcon I
1742 in_analog_right[i][1] = MAX(
1743 MAX(
1744 get_analog_button(input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_R2),
1745 get_analog_button(input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_B)
1746 ),
1747 negcon_i_rs
1748 );
1749 // >> NeGcon II
1750 in_analog_left[i][0] = MAX(
1751 MAX(
1752 get_analog_button(input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_L2),
1753 get_analog_button(input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_Y)
1754 ),
1755 negcon_ii_rs
1756 );
1757 // > NeGcon L
1758 in_analog_left[i][1] = get_analog_button(input_state_cb, i, RETRO_DEVICE_ID_JOYPAD_L);
1759 }
1760 else
1761 {
1762 // Query digital inputs
1763 for (j = 0; j < RETRO_PSX_MAP_LEN; j++){
1764 if (input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j)){
1765 in_keystate[i] |= retro_psx_map[j];
1766 }
1767 }
1768 // Query analog inputs
1769 if (in_type[i] == PSE_PAD_TYPE_ANALOGJOY || in_type[i] == PSE_PAD_TYPE_ANALOGPAD)
1770 {
1771 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);
1772 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);
1773 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);
1774 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);
1775 }
1776 }
1777 }
1778
1779 stop = 0;
1780 psxCpu->Execute();
1781
1782 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf_ptr : NULL,
1783 vout_width, vout_height, vout_width * 2);
1784 vout_fb_dirty = 0;
1785
1786 set_vout_fb();
1787}
1788
1789static bool try_use_bios(const char *path)
1790{
1791 FILE *f;
1792 long size;
1793 const char *name;
1794
1795 f = fopen(path, "rb");
1796 if (f == NULL)
1797 return false;
1798
1799 fseek(f, 0, SEEK_END);
1800 size = ftell(f);
1801 fclose(f);
1802
1803 if (size != 512 * 1024)
1804 return false;
1805
1806 name = strrchr(path, SLASH);
1807 if (name++ == NULL)
1808 name = path;
1809 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
1810 return true;
1811}
1812
1813#ifndef VITA
1814#include <sys/types.h>
1815#include <dirent.h>
1816
1817static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
1818{
1819 DIR *dir;
1820 struct dirent *ent;
1821 bool ret = false;
1822
1823 dir = opendir(dirpath);
1824 if (dir == NULL)
1825 return false;
1826
1827 while ((ent = readdir(dir))) {
1828 if (strncasecmp(ent->d_name, "scph", 4) != 0)
1829 continue;
1830
1831 snprintf(path, path_size, "%s%c%s", dirpath, SLASH, ent->d_name);
1832 ret = try_use_bios(path);
1833 if (ret)
1834 break;
1835 }
1836 closedir(dir);
1837 return ret;
1838}
1839#else
1840#define find_any_bios(...) false
1841#endif
1842
1843static void check_system_specs(void)
1844{
1845 unsigned level = 6;
1846 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
1847}
1848
1849static int init_memcards(void)
1850{
1851 int ret = 0;
1852 const char *dir;
1853 struct retro_variable var = { .key="pcsx_rearmed_memcard2", .value=NULL };
1854 static const char CARD2_FILE[] = "pcsx-card2.mcd";
1855
1856 McdDisable[0] = 0;
1857 // Disable memcard 2 by default
1858 McdDisable[1] = 1;
1859 init_memcard(Mcd1Data);
1860 // Memcard 2 is managed by the emulator on the filesystem,
1861 // There is no need to initialize Mcd2Data like Mcd1Data.
1862
1863 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
1864 SysPrintf("Memcard 2: %s\n", var.value);
1865 if (memcmp(var.value, "enabled", 7) == 0) {
1866 if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir) {
1867 if (strlen(dir) + strlen(CARD2_FILE) + 2 > sizeof(Config.Mcd2)) {
1868 SysPrintf("Path '%s' is too long. Cannot use memcard 2. Use a shorter path.\n", dir);
1869 ret = -1;
1870 } else {
1871 McdDisable[1] = 0;
1872 snprintf(Config.Mcd2, sizeof(Config.Mcd2), "%s/%s", dir, CARD2_FILE);
1873 SysPrintf("Use memcard 2: %s\n", Config.Mcd2);
1874 }
1875 } else {
1876 SysPrintf("Could not get save directory! Could not create memcard 2.");
1877 ret = -1;
1878 }
1879 }
1880 }
1881 return ret;
1882}
1883
1884void retro_init(void)
1885{
1886 const char *bios[] = {
1887 "SCPH101", "SCPH7001", "SCPH5501", "SCPH1001",
1888 "scph101", "scph7001", "scph5501", "scph1001"
1889 };
1890 const char *dir;
1891 char path[256];
1892 int i, ret;
1893
1894 found_bios = false;
1895
1896#ifdef __MACH__
1897 // magic sauce to make the dynarec work on iOS
1898 syscall(SYS_ptrace, 0 /*PTRACE_TRACEME*/, 0, 0, 0);
1899#endif
1900
1901#ifdef _3DS
1902 psxMapHook = pl_3ds_mmap;
1903 psxUnmapHook = pl_3ds_munmap;
1904#endif
1905#ifdef VITA
1906 if(init_vita_mmap()<0)
1907 abort();
1908 psxMapHook = pl_vita_mmap;
1909 psxUnmapHook = pl_vita_munmap;
1910#endif
1911 ret = emu_core_preinit();
1912#ifdef _3DS
1913 /* emu_core_preinit sets the cpu to dynarec */
1914 if(!__ctr_svchax)
1915 Config.Cpu = CPU_INTERPRETER;
1916#endif
1917 ret |= init_memcards();
1918
1919 ret |= emu_core_init();
1920 if (ret != 0) {
1921 SysPrintf("PCSX init failed.\n");
1922 exit(1);
1923 }
1924
1925#ifdef _3DS
1926 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
1927#elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && !defined(VITA)
1928 posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
1929#else
1930 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
1931#endif
1932
1933 vout_buf_ptr = vout_buf;
1934
1935 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
1936 {
1937 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s", dir);
1938
1939 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
1940 snprintf(path, sizeof(path), "%s%c%s.bin", dir, SLASH, bios[i]);
1941 found_bios = try_use_bios(path);
1942 if (found_bios)
1943 break;
1944 }
1945
1946 if (!found_bios)
1947 found_bios = find_any_bios(dir, path, sizeof(path));
1948 }
1949 if (found_bios) {
1950 SysPrintf("found BIOS file: %s\n", Config.Bios);
1951 }
1952 else
1953 {
1954 SysPrintf("no BIOS files found.\n");
1955 struct retro_message msg =
1956 {
1957 "No PlayStation BIOS file found - add for better compatibility",
1958 180
1959 };
1960 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, (void*)&msg);
1961 }
1962
1963 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
1964 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
1965 environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble);
1966
1967 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
1968 * we have to do this because cache misses and some IO penalties
1969 * are not emulated. Warning: changing this may break compatibility. */
1970 cycle_multiplier = 175;
1971#ifdef HAVE_PRE_ARMV7
1972 cycle_multiplier = 200;
1973#endif
1974 pl_rearmed_cbs.gpu_peops.iUseDither = 1;
1975 spu_config.iUseFixedUpdates = 1;
1976
1977 SaveFuncs.open = save_open;
1978 SaveFuncs.read = save_read;
1979 SaveFuncs.write = save_write;
1980 SaveFuncs.seek = save_seek;
1981 SaveFuncs.close = save_close;
1982
1983 update_variables(false);
1984 check_system_specs();
1985}
1986
1987void retro_deinit(void)
1988{
1989 SysClose();
1990#ifdef _3DS
1991 linearFree(vout_buf);
1992#else
1993 free(vout_buf);
1994#endif
1995 vout_buf = NULL;
1996
1997#ifdef VITA
1998 deinit_vita_mmap();
1999#endif
2000}
2001
2002#ifdef VITA
2003#include <psp2/kernel/threadmgr.h>
2004int usleep (unsigned long us)
2005{
2006 sceKernelDelayThread(us);
2007}
2008#endif