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