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