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