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