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