libretro: Add rumble support
[pcsx_rearmed.git] / frontend / libretro.c
CommitLineData
38c2028e 1/*
2 * (C) notaz, 2012
3 *
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
805fe9bc 8#define _GNU_SOURCE 1 // strcasestr
38c2028e 9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
74ebc05b 12#include <strings.h>
38c2028e 13
14#include "../libpcsxcore/misc.h"
15#include "../libpcsxcore/psxcounters.h"
87e5b45f 16#include "../libpcsxcore/psxmem_map.h"
38c2028e 17#include "../libpcsxcore/new_dynarec/new_dynarec.h"
23ea11bd 18#include "../libpcsxcore/cdrom.h"
19#include "../libpcsxcore/cdriso.h"
6e921e1d 20#include "../libpcsxcore/cheat.h"
07c13dfd 21#include "../plugins/dfsound/out.h"
805fe9bc 22#include "../plugins/dfinput/externals.h"
c82f907a 23#include "cspace.h"
38c2028e 24#include "main.h"
25#include "plugin.h"
26#include "plugin_lib.h"
27#include "revision.h"
28#include "libretro.h"
29
30static retro_video_refresh_t video_cb;
31static retro_input_poll_t input_poll_cb;
32static retro_input_state_t input_state_cb;
33static retro_environment_t environ_cb;
34static retro_audio_sample_batch_t audio_batch_cb;
8d24cb85 35static struct retro_rumble_interface rumble;
38c2028e 36
37static void *vout_buf;
70d56ca3 38static int vout_width, vout_height;
39static int vout_doffs_old, vout_fb_dirty;
1a6164a1 40static bool vout_can_dupe;
483afec9 41static bool duping_enable;
70d56ca3 42
38c2028e 43static int samples_sent, samples_to_send;
44static int plugins_opened;
17f84149 45static int is_pal_mode;
38c2028e 46
f9f60dae
TK
47/* memory card data */
48extern char Mcd1Data[MCD_SIZE];
49extern char McdDisable[2];
38c2028e 50
51/* PCSX ReARMed core calls and stuff */
52int in_type1, in_type2;
53int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
54int in_keystate;
8d24cb85 55int in_enable_vibration = 1;
38c2028e 56
805fe9bc 57/* PSX max resolution is 640x512, but with enhancement it's 1024x512 */
58#define VOUT_MAX_WIDTH 1024
59#define VOUT_MAX_HEIGHT 512
60
7df396ea
TK
61static void init_memcard(char *mcd_data)
62{
63 unsigned off = 0;
64 unsigned i;
65
66 memset(mcd_data, 0, MCD_SIZE);
67
68 mcd_data[off++] = 'M';
69 mcd_data[off++] = 'C';
70 off += 0x7d;
71 mcd_data[off++] = 0x0e;
72
73 for (i = 0; i < 15; i++) {
74 mcd_data[off++] = 0xa0;
75 off += 0x07;
76 mcd_data[off++] = 0xff;
77 mcd_data[off++] = 0xff;
78 off += 0x75;
79 mcd_data[off++] = 0xa0;
80 }
81
82 for (i = 0; i < 20; i++) {
83 mcd_data[off++] = 0xff;
84 mcd_data[off++] = 0xff;
85 mcd_data[off++] = 0xff;
86 mcd_data[off++] = 0xff;
87 off += 0x04;
88 mcd_data[off++] = 0xff;
89 mcd_data[off++] = 0xff;
90 off += 0x76;
91 }
92}
93
38c2028e 94static int vout_open(void)
95{
96 return 0;
97}
98
e4c83ca6 99static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
38c2028e 100{
70d56ca3 101 vout_width = w;
102 vout_height = h;
38c2028e 103}
104
cc4f9b70 105#ifndef FRONTEND_SUPPORTS_RGB565
9c59f120 106static void convert(void *buf, size_t bytes)
38c2028e 107{
108 unsigned int i, v, *p = buf;
109
9c59f120 110 for (i = 0; i < bytes / 4; i++) {
38c2028e 111 v = p[i];
112 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
113 }
114}
46aa5b98 115#endif
38c2028e 116
fa56d360 117static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
38c2028e 118{
fa56d360 119 unsigned short *dest = vout_buf;
120 const unsigned short *src = vram;
70d56ca3 121 int dstride = vout_width, h1 = h;
122 int doffs;
38c2028e 123
fa56d360 124 if (vram == NULL) {
125 // blanking
cc56203b 126 memset(vout_buf, 0, dstride * h * 2);
fa56d360 127 goto out;
128 }
129
70d56ca3 130 doffs = (vout_height - h) * dstride;
131 doffs += (dstride - w) / 2 & ~1;
132 if (doffs != vout_doffs_old) {
133 // clear borders
134 memset(vout_buf, 0, dstride * h * 2);
135 vout_doffs_old = doffs;
136 }
137 dest += doffs;
138
fa56d360 139 if (bgr24)
140 {
141 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
142 for (; h1-- > 0; dest += dstride, src += stride)
143 {
144 bgr888_to_rgb565(dest, src, w * 3);
145 }
146 }
147 else
148 {
149 for (; h1-- > 0; dest += dstride, src += stride)
150 {
151 bgr555_to_rgb565(dest, src, w * 2);
152 }
153 }
38c2028e 154
fa56d360 155out:
46aa5b98 156#ifndef FRONTEND_SUPPORTS_RGB565
70d56ca3 157 convert(vout_buf, vout_width * vout_height * 2);
46aa5b98 158#endif
70d56ca3 159 vout_fb_dirty = 1;
fa56d360 160 pl_rearmed_cbs.flip_cnt++;
38c2028e 161}
162
163static void vout_close(void)
164{
165}
166
cc56203b 167static void *pl_mmap(unsigned int size)
168{
87e5b45f 169 return psxMap(0, size, 0, MAP_TAG_VRAM);
cc56203b 170}
171
172static void pl_munmap(void *ptr, unsigned int size)
173{
87e5b45f 174 psxUnmap(ptr, size, MAP_TAG_VRAM);
cc56203b 175}
176
38c2028e 177struct rearmed_cbs pl_rearmed_cbs = {
178 .pl_vout_open = vout_open,
179 .pl_vout_set_mode = vout_set_mode,
180 .pl_vout_flip = vout_flip,
181 .pl_vout_close = vout_close,
cc56203b 182 .mmap = pl_mmap,
183 .munmap = pl_munmap,
38c2028e 184 /* from psxcounters */
185 .gpu_hcnt = &hSyncCount,
186 .gpu_frame_count = &frame_counter,
187};
188
189void pl_frame_limit(void)
190{
191 /* called once per frame, make psxCpu->Execute() above return */
192 stop = 1;
193}
194
195void pl_timing_prepare(int is_pal)
196{
17f84149 197 is_pal_mode = is_pal;
38c2028e 198}
199
8d24cb85 200void plat_trigger_vibrate(int pad, uint32_t low, uint32_t high)
38c2028e 201{
8d24cb85 202 rumble.set_rumble_state(pad, RETRO_RUMBLE_STRONG, high << 8);
203 rumble.set_rumble_state(pad, RETRO_RUMBLE_WEAK, low ? 0xffff : 0x0);
38c2028e 204}
205
e4c83ca6 206void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
38c2028e 207{
208}
209
210/* sound calls */
07c13dfd 211static int snd_init(void)
38c2028e 212{
07c13dfd 213 return 0;
38c2028e 214}
215
07c13dfd 216static void snd_finish(void)
38c2028e 217{
218}
219
07c13dfd 220static int snd_busy(void)
38c2028e 221{
222 if (samples_to_send > samples_sent)
223 return 0; /* give more samples */
224 else
225 return 1;
226}
227
07c13dfd 228static void snd_feed(void *buf, int bytes)
38c2028e 229{
230 audio_batch_cb(buf, bytes / 4);
231 samples_sent += bytes / 4;
232}
233
07c13dfd 234void out_register_libretro(struct out_driver *drv)
235{
236 drv->name = "libretro";
237 drv->init = snd_init;
238 drv->finish = snd_finish;
239 drv->busy = snd_busy;
240 drv->feed = snd_feed;
241}
242
38c2028e 243/* libretro */
e268a47e 244void retro_set_environment(retro_environment_t cb)
245{
e268a47e 246 static const struct retro_variable vars[] = {
8d24cb85 247 { "pcsx_rearmed_frameskip", "Frameskip; 0|1|2|3" },
248 { "pcsx_rearmed_region", "Region; Auto|NTSC|PAL" },
249 { "pcsx_rearmed_pad1type", "Pad 1 Type; standard|analog" },
250 { "pcsx_rearmed_pad2type", "Pad 2 Type; standard|analog" },
48f615ba 251#ifndef DRC_DISABLE
8d24cb85 252 { "pcsx_rearmed_drc", "Dynamic recompiler; enabled|disabled" },
48f615ba 253#endif
c82d5b4b 254#if defined(__ARM_NEON__) || defined(NEON_PC)
9f5a28d9 255 { "neon_interlace_enable", "Enable interlacing mode(s); disabled|enabled" },
e268a47e 256 { "neon_enhancement_enable", "Enhanced resolution (slow); disabled|enabled" },
354329fa 257 { "neon_enhancement_no_main", "Enhanced resolution speed hack; disabled|enabled" },
7d68d030 258#endif
483afec9 259 { "pcsx_rearmed_duping_enable", "Frame duping; on|off" },
e268a47e 260 { NULL, NULL },
261 };
262
263 environ_cb = cb;
264
265 cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)vars);
e268a47e 266}
267
38c2028e 268void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
269void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
270void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
271void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
272void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
273
274unsigned retro_api_version(void)
275{
276 return RETRO_API_VERSION;
277}
278
279void retro_set_controller_port_device(unsigned port, unsigned device)
280{
281}
282
283void retro_get_system_info(struct retro_system_info *info)
284{
285 memset(info, 0, sizeof(*info));
286 info->library_name = "PCSX-ReARMed";
0ed87574 287 info->library_version = "r19";
9fba39a9 288 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u";
38c2028e 289 info->need_fullpath = true;
290}
291
292void retro_get_system_av_info(struct retro_system_av_info *info)
293{
294 memset(info, 0, sizeof(*info));
17f84149 295 info->timing.fps = is_pal_mode ? 50 : 60;
38c2028e 296 info->timing.sample_rate = 44100;
297 info->geometry.base_width = 320;
298 info->geometry.base_height = 240;
805fe9bc 299 info->geometry.max_width = VOUT_MAX_WIDTH;
300 info->geometry.max_height = VOUT_MAX_HEIGHT;
38c2028e 301 info->geometry.aspect_ratio = 4.0 / 3.0;
302}
303
6e921e1d 304/* savestates */
38c2028e 305size_t retro_serialize_size(void)
306{
6e921e1d 307 // it's currently 4380651 bytes, but have some reserved for future
308 return 0x430000;
309}
310
311struct save_fp {
312 char *buf;
313 size_t pos;
314 int is_write;
315};
316
317static void *save_open(const char *name, const char *mode)
318{
319 struct save_fp *fp;
320
321 if (name == NULL || mode == NULL)
322 return NULL;
323
324 fp = malloc(sizeof(*fp));
325 if (fp == NULL)
326 return NULL;
327
328 fp->buf = (char *)name;
329 fp->pos = 0;
330 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
331
332 return fp;
333}
334
335static int save_read(void *file, void *buf, u32 len)
336{
337 struct save_fp *fp = file;
338 if (fp == NULL || buf == NULL)
339 return -1;
340
341 memcpy(buf, fp->buf + fp->pos, len);
342 fp->pos += len;
343 return len;
344}
345
346static int save_write(void *file, const void *buf, u32 len)
347{
348 struct save_fp *fp = file;
349 if (fp == NULL || buf == NULL)
350 return -1;
351
352 memcpy(fp->buf + fp->pos, buf, len);
353 fp->pos += len;
354 return len;
355}
356
357static long save_seek(void *file, long offs, int whence)
358{
359 struct save_fp *fp = file;
360 if (fp == NULL)
361 return -1;
362
363 switch (whence) {
364 case SEEK_CUR:
365 fp->pos += offs;
366 return fp->pos;
367 case SEEK_SET:
368 fp->pos = offs;
369 return fp->pos;
370 default:
371 return -1;
372 }
373}
374
375static void save_close(void *file)
376{
377 struct save_fp *fp = file;
378 size_t r_size = retro_serialize_size();
379 if (fp == NULL)
380 return;
381
382 if (fp->pos > r_size)
383 SysPrintf("ERROR: save buffer overflow detected\n");
384 else if (fp->is_write && fp->pos < r_size)
385 // make sure we don't save trash in leftover space
386 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
387 free(fp);
38c2028e 388}
389
390bool retro_serialize(void *data, size_t size)
391{
6e921e1d 392 int ret = SaveState(data);
393 return ret == 0 ? true : false;
38c2028e 394}
395
396bool retro_unserialize(const void *data, size_t size)
397{
6e921e1d 398 int ret = LoadState(data);
399 return ret == 0 ? true : false;
38c2028e 400}
401
23ea11bd 402/* cheats */
38c2028e 403void retro_cheat_reset(void)
404{
6e921e1d 405 ClearAllCheats();
38c2028e 406}
407
408void retro_cheat_set(unsigned index, bool enabled, const char *code)
409{
6e921e1d 410 char buf[256];
411 int ret;
412
413 // cheat funcs are destructive, need a copy..
414 strncpy(buf, code, sizeof(buf));
415 buf[sizeof(buf) - 1] = 0;
416
417 if (index < NumCheats)
418 ret = EditCheat(index, "", buf);
419 else
420 ret = AddCheat("", buf);
421
422 if (ret != 0)
423 SysPrintf("Failed to set cheat %#u\n", index);
424 else if (index < NumCheats)
425 Cheats[index].Enabled = enabled;
38c2028e 426}
427
23ea11bd 428/* multidisk support */
429static bool disk_ejected;
430static unsigned int disk_current_index;
51ba7408 431static unsigned int disk_count;
23ea11bd 432static struct disks_state {
433 char *fname;
434 int internal_index; // for multidisk eboots
435} disks[8];
436
437static bool disk_set_eject_state(bool ejected)
438{
439 // weird PCSX API..
440 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
441 LidInterrupt();
442
443 disk_ejected = ejected;
444 return true;
445}
446
447static bool disk_get_eject_state(void)
448{
449 /* can't be controlled by emulated software */
450 return disk_ejected;
451}
452
453static unsigned int disk_get_image_index(void)
454{
455 return disk_current_index;
456}
457
458static bool disk_set_image_index(unsigned int index)
459{
460 if (index >= sizeof(disks) / sizeof(disks[0]))
461 return false;
462
463 CdromId[0] = '\0';
464 CdromLabel[0] = '\0';
465
466 if (disks[index].fname == NULL) {
467 SysPrintf("missing disk #%u\n", index);
468 CDR_shutdown();
469
470 // RetroArch specifies "no disk" with index == count,
471 // so don't fail here..
472 disk_current_index = index;
473 return true;
474 }
475
476 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
477 disks[index].fname, disks[index].internal_index);
478
479 cdrIsoMultidiskSelect = disks[index].internal_index;
480 set_cd_image(disks[index].fname);
481 if (ReloadCdromPlugin() < 0) {
482 SysPrintf("failed to load cdr plugin\n");
483 return false;
484 }
485 if (CDR_open() < 0) {
486 SysPrintf("failed to open cdr plugin\n");
487 return false;
488 }
489
490 if (!disk_ejected) {
491 SetCdOpenCaseTime(time(NULL) + 2);
492 LidInterrupt();
493 }
494
495 disk_current_index = index;
496 return true;
497}
498
499static unsigned int disk_get_num_images(void)
500{
51ba7408 501 return disk_count;
23ea11bd 502}
503
504static bool disk_replace_image_index(unsigned index,
505 const struct retro_game_info *info)
506{
507 char *old_fname;
508 bool ret = true;
509
510 if (index >= sizeof(disks) / sizeof(disks[0]))
511 return false;
512
513 old_fname = disks[index].fname;
514 disks[index].fname = NULL;
515 disks[index].internal_index = 0;
516
517 if (info != NULL) {
518 disks[index].fname = strdup(info->path);
519 if (index == disk_current_index)
520 ret = disk_set_image_index(index);
521 }
522
523 if (old_fname != NULL)
524 free(old_fname);
525
526 return ret;
527}
528
529static bool disk_add_image_index(void)
530{
51ba7408
T
531 if (disk_count >= 8)
532 return false;
533
534 disk_count++;
23ea11bd 535 return true;
536}
537
538static struct retro_disk_control_callback disk_control = {
539 .set_eject_state = disk_set_eject_state,
540 .get_eject_state = disk_get_eject_state,
541 .get_image_index = disk_get_image_index,
542 .set_image_index = disk_set_image_index,
543 .get_num_images = disk_get_num_images,
544 .replace_image_index = disk_replace_image_index,
545 .add_image_index = disk_add_image_index,
546};
547
9fba39a9
T
548// just in case, maybe a win-rt port in the future?
549#ifdef _WIN32
550#define SLASH '\\'
551#else
552#define SLASH '/'
553#endif
554
555static char base_dir[PATH_MAX];
556
557static bool read_m3u(const char *file)
558{
559 char line[PATH_MAX];
560 char name[PATH_MAX];
9fba39a9
T
561 FILE *f = fopen(file, "r");
562 if (!f)
563 return false;
564
51ba7408 565 while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) {
9fba39a9
T
566 if (line[0] == '#')
567 continue;
568 char *carrige_return = strchr(line, '\r');
569 if (carrige_return)
570 *carrige_return = '\0';
571 char *newline = strchr(line, '\n');
572 if (newline)
573 *newline = '\0';
574
575 if (line[0] != '\0')
576 {
577 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
51ba7408 578 disks[disk_count++].fname = strdup(name);
9fba39a9
T
579 }
580 }
581
582 fclose(f);
51ba7408 583 return (disk_count != 0);
9fba39a9
T
584}
585
586static void extract_directory(char *buf, const char *path, size_t size)
587{
588 char *base;
589 strncpy(buf, path, size - 1);
590 buf[size - 1] = '\0';
591
592 base = strrchr(buf, '/');
593 if (!base)
594 base = strrchr(buf, '\\');
595
596 if (base)
597 *base = '\0';
598 else
599 {
600 buf[0] = '.';
601 buf[1] = '\0';
602 }
603}
604
41294f4d 605#ifdef __QNX__
606/* Blackberry QNX doesn't have strcasestr */
607
608/*
609 * Find the first occurrence of find in s, ignore case.
610 */
611char *
612strcasestr(const char *s, const char*find)
613{
614 char c, sc;
615 size_t len;
616
617 if ((c = *find++) != 0) {
618 c = tolower((unsigned char)c);
619 len = strlen(find);
620 do {
621 do {
622 if ((sc = *s++) == 0)
623 return (NULL);
624 } while ((char)tolower((unsigned char)sc) != c);
625 } while (strncasecmp(s, find, len) != 0);
626 s--;
627 }
628 return ((char *)s);
629}
630#endif
631
38c2028e 632bool retro_load_game(const struct retro_game_info *info)
633{
23ea11bd 634 size_t i;
9fba39a9
T
635 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
636
46aa5b98 637#ifdef FRONTEND_SUPPORTS_RGB565
c19aba43 638 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
9f766dbe 639 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
f29fbd53 640 SysPrintf("RGB565 supported, using it\n");
c19aba43 641 }
46aa5b98 642#endif
c19aba43 643
23ea11bd 644 if (info == NULL || info->path == NULL) {
645 SysPrintf("info->path required\n");
646 return false;
647 }
648
38c2028e 649 if (plugins_opened) {
650 ClosePlugins();
651 plugins_opened = 0;
652 }
653
23ea11bd 654 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
655 if (disks[i].fname != NULL) {
656 free(disks[i].fname);
657 disks[i].fname = NULL;
658 }
659 disks[i].internal_index = 0;
660 }
661
662 disk_current_index = 0;
9fba39a9
T
663 extract_directory(base_dir, info->path, sizeof(base_dir));
664
665 if (is_m3u) {
666 if (!read_m3u(info->path)) {
667 SysPrintf("failed to read m3u file\n");
668 return false;
669 }
51ba7408
T
670 } else {
671 disk_count = 1;
9fba39a9 672 disks[0].fname = strdup(info->path);
51ba7408 673 }
23ea11bd 674
675 set_cd_image(disks[0].fname);
38c2028e 676
677 /* have to reload after set_cd_image for correct cdr plugin */
678 if (LoadPlugins() == -1) {
f29fbd53 679 SysPrintf("failed to load plugins\n");
38c2028e 680 return false;
681 }
682
683 plugins_opened = 1;
684 NetOpened = 0;
685
686 if (OpenPlugins() == -1) {
f29fbd53 687 SysPrintf("failed to open plugins\n");
38c2028e 688 return false;
689 }
690
691 plugin_call_rearmed_cbs();
19a784e6 692 dfinput_activate();
38c2028e 693
694 Config.PsxAuto = 1;
695 if (CheckCdrom() == -1) {
f29fbd53 696 SysPrintf("unsupported/invalid CD image: %s\n", info->path);
38c2028e 697 return false;
698 }
699
700 SysReset();
701
702 if (LoadCdrom() == -1) {
f29fbd53 703 SysPrintf("could not load CD-ROM!\n");
38c2028e 704 return false;
705 }
79f216e3 706 emu_on_new_cd(0);
38c2028e 707
23ea11bd 708 // multidisk images
9fba39a9 709 if (!is_m3u) {
51ba7408 710 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
9fba39a9
T
711 for (i = 1; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++) {
712 disks[i].fname = strdup(info->path);
713 disks[i].internal_index = i;
714 }
23ea11bd 715 }
716
38c2028e 717 return true;
718}
719
720bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
721{
722 return false;
723}
724
725void retro_unload_game(void)
726{
727}
728
729unsigned retro_get_region(void)
730{
17f84149 731 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
38c2028e 732}
733
734void *retro_get_memory_data(unsigned id)
735{
b40a83db
T
736 if (id == RETRO_MEMORY_SAVE_RAM)
737 return Mcd1Data;
738 else
739 return NULL;
38c2028e 740}
741
742size_t retro_get_memory_size(unsigned id)
743{
b40a83db
T
744 if (id == RETRO_MEMORY_SAVE_RAM)
745 return MCD_SIZE;
746 else
747 return 0;
38c2028e 748}
749
750void retro_reset(void)
751{
752 SysReset();
753}
754
755static const unsigned short retro_psx_map[] = {
756 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
757 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
758 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
759 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
760 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
761 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
762 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
763 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
764 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
765 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
766 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
767 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
768 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
769 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
770 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
771 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
772};
773#define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
774
805fe9bc 775static void update_variables(bool in_flight)
e268a47e 776{
e268a47e 777 struct retro_variable var;
7d68d030 778
e268a47e 779 var.value = NULL;
8d24cb85 780 var.key = "pcsx_rearmed_frameskip";
e268a47e 781
7d68d030 782 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
783 pl_rearmed_cbs.frameskip = atoi(var.value);
9f5a28d9 784
785 var.value = NULL;
8d24cb85 786 var.key = "pcsx_rearmed_region";
9f5a28d9 787
788 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
789 {
790 Config.PsxAuto = 0;
791 if (strcmp(var.value, "Automatic") == 0)
792 Config.PsxAuto = 1;
793 else if (strcmp(var.value, "NTSC") == 0)
794 Config.PsxType = 0;
795 else if (strcmp(var.value, "PAL") == 0)
796 Config.PsxType = 1;
797 }
4615b075 798
799 var.value = NULL;
8d24cb85 800 var.key = "pcsx_rearmed_pad1type";
4615b075 801
802 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
803 {
804 in_type1 = PSE_PAD_TYPE_STANDARD;
805 if (strcmp(var.value, "analog") == 0)
806 in_type1 = PSE_PAD_TYPE_ANALOGPAD;
807 }
808
8d24cb85 809 var.value = NULL;
810 var.key = "pcsx_rearmed_pad2type";
811
812 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
813 {
814 in_type2 = PSE_PAD_TYPE_STANDARD;
815 if (strcmp(var.value, "analog") == 0)
816 in_type2 = PSE_PAD_TYPE_ANALOGPAD;
817 }
818
c82d5b4b 819#if defined(__ARM_NEON__) || defined(NEON_PC)
9f5a28d9 820 var.value = "NULL";
821 var.key = "neon_interlace_enable";
822
823 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
824 {
825 if (strcmp(var.value, "disabled") == 0)
826 pl_rearmed_cbs.gpu_neon.allow_interlace = 0;
827 else if (strcmp(var.value, "enabled") == 0)
828 pl_rearmed_cbs.gpu_neon.allow_interlace = 1;
829 }
830
7d68d030 831 var.value = NULL;
832 var.key = "neon_enhancement_enable";
833
834 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
835 {
836 if (strcmp(var.value, "disabled") == 0)
837 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
838 else if (strcmp(var.value, "enabled") == 0)
839 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
840 }
354329fa 841
842 var.value = NULL;
843 var.key = "neon_enhancement_no_main";
844
845 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
846 {
847 if (strcmp(var.value, "disabled") == 0)
848 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
849 else if (strcmp(var.value, "enabled") == 0)
850 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 1;
851 }
094ec204 852#endif
c82d5b4b 853
483afec9 854 var.value = "NULL";
855 var.key = "pcsx_rearmed_duping_enable";
856
857 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
858 {
859 if (strcmp(var.value, "off") == 0)
860 duping_enable = false;
861 else if (strcmp(var.value, "on") == 0)
862 duping_enable = true;
863 }
864
48f615ba 865#ifndef DRC_DISABLE
866 var.value = NULL;
8d24cb85 867 var.key = "pcsx_rearmed_drc";
48f615ba 868
869 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) || var.value)
870 {
871 R3000Acpu *prev_cpu = psxCpu;
872
873 if (strcmp(var.value, "disabled") == 0)
874 Config.Cpu = CPU_INTERPRETER;
875 else if (strcmp(var.value, "enabled") == 0)
876 Config.Cpu = CPU_DYNAREC;
877
878 psxCpu = (Config.Cpu == CPU_INTERPRETER) ? &psxInt : &psxRec;
879 if (psxCpu != prev_cpu) {
880 prev_cpu->Shutdown();
881 psxCpu->Init();
882 psxCpu->Reset(); // not really a reset..
883 }
884 }
885#endif
805fe9bc 886
a19f6faa
TJ
887 if (in_flight) {
888 // inform core things about possible config changes
889 plugin_call_rearmed_cbs();
805fe9bc 890
a19f6faa
TJ
891 if (GPU_open != NULL && GPU_close != NULL) {
892 GPU_close();
893 GPU_open(&gpuDisp, "PCSX", NULL);
894 }
805fe9bc 895
a19f6faa
TJ
896 dfinput_activate();
897 }
e268a47e 898}
899
38c2028e 900void retro_run(void)
901{
902 int i;
903
904 input_poll_cb();
e268a47e 905
805fe9bc 906 bool updated = false;
907 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
908 update_variables(true);
e268a47e 909
38c2028e 910 in_keystate = 0;
911 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
912 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
913 in_keystate |= retro_psx_map[i];
914 in_keystate <<= 16;
915 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
916 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
917 in_keystate |= retro_psx_map[i];
918
a19f6faa
TJ
919 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD)
920 {
921 in_a1[0] = (input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X) / 256) + 128;
922 in_a1[1] = (input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y) / 256) + 128;
923 in_a2[0] = (input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_X) / 256) + 128;
924 in_a2[1] = (input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT, RETRO_DEVICE_ID_ANALOG_Y) / 256) + 128;
925 }
4615b075 926
38c2028e 927 stop = 0;
928 psxCpu->Execute();
929
17f84149 930 samples_to_send += is_pal_mode ? 44100 / 50 : 44100 / 60;
e2bdb933 931
483afec9 932 video_cb((vout_fb_dirty || !vout_can_dupe || !duping_enable) ? vout_buf : NULL,
1a6164a1 933 vout_width, vout_height, vout_width * 2);
70d56ca3 934 vout_fb_dirty = 0;
38c2028e 935}
936
74ebc05b 937static bool try_use_bios(const char *path)
938{
939 FILE *f;
940 long size;
941 const char *name;
942
943 f = fopen(path, "rb");
944 if (f == NULL)
945 return false;
946
947 fseek(f, 0, SEEK_END);
948 size = ftell(f);
949 fclose(f);
950
951 if (size != 512 * 1024)
952 return false;
953
954 name = strrchr(path, SLASH);
955 if (name++ == NULL)
956 name = path;
957 snprintf(Config.Bios, sizeof(Config.Bios), "%s", name);
958 return true;
959}
960
961#if 1
962#include <sys/types.h>
963#include <dirent.h>
964
965static bool find_any_bios(const char *dirpath, char *path, size_t path_size)
966{
967 DIR *dir;
968 struct dirent *ent;
969 bool ret = false;
970
971 dir = opendir(dirpath);
972 if (dir == NULL)
973 return false;
974
975 while ((ent = readdir(dir))) {
976 if (strncasecmp(ent->d_name, "scph", 4) != 0)
977 continue;
978
979 snprintf(path, path_size, "%s/%s", dirpath, ent->d_name);
980 ret = try_use_bios(path);
981 if (ret)
982 break;
983 }
984 closedir(dir);
985 return ret;
986}
987#else
988#define find_any_bios(...) false
989#endif
990
496d5596 991static void check_system_specs(void)
992{
993 unsigned level = 6;
994 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
995}
996
38c2028e 997void retro_init(void)
998{
999 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
1000 const char *dir;
1001 char path[256];
38c2028e 1002 int i, ret, level;
74ebc05b 1003 bool found_bios = false;
38c2028e 1004
1005 ret = emu_core_preinit();
1006 ret |= emu_core_init();
1007 if (ret != 0) {
f29fbd53 1008 SysPrintf("PCSX init failed.\n");
38c2028e 1009 exit(1);
1010 }
1011
63d480d0 1012#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)
252e4935 1013 posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
63d480d0
TJ
1014#else
1015 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
1016#endif
38c2028e 1017
1018 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
1019 {
1020 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
1021
1022 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
1023 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
74ebc05b 1024 found_bios = try_use_bios(path);
1025 if (found_bios)
38c2028e 1026 break;
38c2028e 1027 }
74ebc05b 1028
1029 if (!found_bios)
1030 found_bios = find_any_bios(dir, path, sizeof(path));
38c2028e 1031 }
74ebc05b 1032 if (found_bios) {
f29fbd53 1033 SysPrintf("found BIOS file: %s\n", Config.Bios);
38c2028e 1034 }
1035 else
74ebc05b 1036 {
f29fbd53 1037 SysPrintf("no BIOS files found.\n");
74ebc05b 1038 struct retro_message msg =
1039 {
1040 "no BIOS found, expect bugs!",
1041 180
1042 };
1043 environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, (void*)&msg);
1044 }
38c2028e 1045
1046 level = 1;
1047 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
f9f60dae 1048
1a6164a1 1049 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
23ea11bd 1050 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
8d24cb85 1051 environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble);
1a6164a1 1052
5ca2ec64 1053 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
1054 * we have to do this because cache misses and some IO penalties
1055 * are not emulated. Warning: changing this may break compatibility. */
1056#ifdef __ARM_ARCH_7A__
1057 cycle_multiplier = 175;
1058#else
1059 cycle_multiplier = 200;
1060#endif
1061
f9f60dae
TK
1062 McdDisable[0] = 0;
1063 McdDisable[1] = 1;
7df396ea 1064 init_memcard(Mcd1Data);
6e921e1d 1065
1066 SaveFuncs.open = save_open;
1067 SaveFuncs.read = save_read;
1068 SaveFuncs.write = save_write;
1069 SaveFuncs.seek = save_seek;
1070 SaveFuncs.close = save_close;
e268a47e 1071
805fe9bc 1072 update_variables(false);
a19f6faa 1073 check_system_specs();
38c2028e 1074}
1075
1076void retro_deinit(void)
1077{
1078 SysClose();
1079 free(vout_buf);
1080 vout_buf = NULL;
1081}