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