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