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