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