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