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