4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
12 #include "../libpcsxcore/misc.h"
13 #include "../libpcsxcore/psxcounters.h"
14 #include "../libpcsxcore/psxmem_map.h"
15 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
16 #include "../libpcsxcore/cdrom.h"
17 #include "../libpcsxcore/cdriso.h"
18 #include "../libpcsxcore/cheat.h"
19 #include "../plugins/dfsound/out.h"
23 #include "plugin_lib.h"
27 static retro_video_refresh_t video_cb;
28 static retro_input_poll_t input_poll_cb;
29 static retro_input_state_t input_state_cb;
30 static retro_environment_t environ_cb;
31 static retro_audio_sample_batch_t audio_batch_cb;
33 static void *vout_buf;
34 static int vout_width, vout_height;
35 static int vout_doffs_old, vout_fb_dirty;
36 static bool vout_can_dupe;
38 static int samples_sent, samples_to_send;
39 static int plugins_opened;
40 static int is_pal_mode;
42 /* memory card data */
43 extern char Mcd1Data[MCD_SIZE];
44 extern char McdDisable[2];
46 /* PCSX ReARMed core calls and stuff */
47 int in_type1, in_type2;
48 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
50 int in_enable_vibration;
52 static void init_memcard(char *mcd_data)
57 memset(mcd_data, 0, MCD_SIZE);
59 mcd_data[off++] = 'M';
60 mcd_data[off++] = 'C';
62 mcd_data[off++] = 0x0e;
64 for (i = 0; i < 15; i++) {
65 mcd_data[off++] = 0xa0;
67 mcd_data[off++] = 0xff;
68 mcd_data[off++] = 0xff;
70 mcd_data[off++] = 0xa0;
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;
79 mcd_data[off++] = 0xff;
80 mcd_data[off++] = 0xff;
85 static int vout_open(void)
90 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
96 #ifndef FRONTEND_SUPPORTS_RGB565
97 static void convert(void *buf, size_t bytes)
99 unsigned int i, v, *p = buf;
101 for (i = 0; i < bytes / 4; i++) {
103 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
108 static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
110 unsigned short *dest = vout_buf;
111 const unsigned short *src = vram;
112 int dstride = vout_width, h1 = h;
117 memset(vout_buf, 0, dstride * h * 2);
121 doffs = (vout_height - h) * dstride;
122 doffs += (dstride - w) / 2 & ~1;
123 if (doffs != vout_doffs_old) {
125 memset(vout_buf, 0, dstride * h * 2);
126 vout_doffs_old = doffs;
132 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
133 for (; h1-- > 0; dest += dstride, src += stride)
135 bgr888_to_rgb565(dest, src, w * 3);
140 for (; h1-- > 0; dest += dstride, src += stride)
142 bgr555_to_rgb565(dest, src, w * 2);
147 #ifndef FRONTEND_SUPPORTS_RGB565
148 convert(vout_buf, vout_width * vout_height * 2);
151 pl_rearmed_cbs.flip_cnt++;
154 static void vout_close(void)
158 static void *pl_mmap(unsigned int size)
160 return psxMap(0, size, 0, MAP_TAG_VRAM);
163 static void pl_munmap(void *ptr, unsigned int size)
165 psxUnmap(ptr, size, MAP_TAG_VRAM);
168 struct 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,
175 /* from psxcounters */
176 .gpu_hcnt = &hSyncCount,
177 .gpu_frame_count = &frame_counter,
180 void pl_frame_limit(void)
182 /* called once per frame, make psxCpu->Execute() above return */
186 void pl_timing_prepare(int is_pal)
188 is_pal_mode = is_pal;
191 void plat_trigger_vibrate(int is_strong)
195 void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
200 static int snd_init(void)
205 static void snd_finish(void)
209 static int snd_busy(void)
211 if (samples_to_send > samples_sent)
212 return 0; /* give more samples */
217 static void snd_feed(void *buf, int bytes)
219 audio_batch_cb(buf, bytes / 4);
220 samples_sent += bytes / 4;
223 void out_register_libretro(struct out_driver *drv)
225 drv->name = "libretro";
226 drv->init = snd_init;
227 drv->finish = snd_finish;
228 drv->busy = snd_busy;
229 drv->feed = snd_feed;
233 void retro_set_environment(retro_environment_t cb) { environ_cb = cb; }
234 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
235 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
236 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
237 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
238 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
240 unsigned retro_api_version(void)
242 return RETRO_API_VERSION;
245 void retro_set_controller_port_device(unsigned port, unsigned device)
249 void retro_get_system_info(struct retro_system_info *info)
251 memset(info, 0, sizeof(*info));
252 info->library_name = "PCSX-ReARMed";
253 info->library_version = "r19";
254 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u";
255 info->need_fullpath = true;
258 void retro_get_system_av_info(struct retro_system_av_info *info)
260 memset(info, 0, sizeof(*info));
261 info->timing.fps = is_pal_mode ? 50 : 60;
262 info->timing.sample_rate = 44100;
263 info->geometry.base_width = 320;
264 info->geometry.base_height = 240;
265 info->geometry.max_width = 640;
266 info->geometry.max_height = 512;
267 info->geometry.aspect_ratio = 4.0 / 3.0;
271 size_t retro_serialize_size(void)
273 // it's currently 4380651 bytes, but have some reserved for future
283 static void *save_open(const char *name, const char *mode)
287 if (name == NULL || mode == NULL)
290 fp = malloc(sizeof(*fp));
294 fp->buf = (char *)name;
296 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
301 static int save_read(void *file, void *buf, u32 len)
303 struct save_fp *fp = file;
304 if (fp == NULL || buf == NULL)
307 memcpy(buf, fp->buf + fp->pos, len);
312 static int save_write(void *file, const void *buf, u32 len)
314 struct save_fp *fp = file;
315 if (fp == NULL || buf == NULL)
318 memcpy(fp->buf + fp->pos, buf, len);
323 static long save_seek(void *file, long offs, int whence)
325 struct save_fp *fp = file;
341 static void save_close(void *file)
343 struct save_fp *fp = file;
344 size_t r_size = retro_serialize_size();
348 if (fp->pos > r_size)
349 SysPrintf("ERROR: save buffer overflow detected\n");
350 else if (fp->is_write && fp->pos < r_size)
351 // make sure we don't save trash in leftover space
352 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
356 bool retro_serialize(void *data, size_t size)
358 int ret = SaveState(data);
359 return ret == 0 ? true : false;
362 bool retro_unserialize(const void *data, size_t size)
364 int ret = LoadState(data);
365 return ret == 0 ? true : false;
369 void retro_cheat_reset(void)
374 void retro_cheat_set(unsigned index, bool enabled, const char *code)
379 // cheat funcs are destructive, need a copy..
380 strncpy(buf, code, sizeof(buf));
381 buf[sizeof(buf) - 1] = 0;
383 if (index < NumCheats)
384 ret = EditCheat(index, "", buf);
386 ret = AddCheat("", buf);
389 SysPrintf("Failed to set cheat %#u\n", index);
390 else if (index < NumCheats)
391 Cheats[index].Enabled = enabled;
394 /* multidisk support */
395 static bool disk_ejected;
396 static unsigned int disk_current_index;
397 static unsigned int disk_count;
398 static struct disks_state {
400 int internal_index; // for multidisk eboots
403 static bool disk_set_eject_state(bool ejected)
406 SetCdOpenCaseTime(ejected ? -1 : (time(NULL) + 2));
409 disk_ejected = ejected;
413 static bool disk_get_eject_state(void)
415 /* can't be controlled by emulated software */
419 static unsigned int disk_get_image_index(void)
421 return disk_current_index;
424 static bool disk_set_image_index(unsigned int index)
426 if (index >= sizeof(disks) / sizeof(disks[0]))
430 CdromLabel[0] = '\0';
432 if (disks[index].fname == NULL) {
433 SysPrintf("missing disk #%u\n", index);
436 // RetroArch specifies "no disk" with index == count,
437 // so don't fail here..
438 disk_current_index = index;
442 SysPrintf("switching to disk %u: \"%s\" #%d\n", index,
443 disks[index].fname, disks[index].internal_index);
445 cdrIsoMultidiskSelect = disks[index].internal_index;
446 set_cd_image(disks[index].fname);
447 if (ReloadCdromPlugin() < 0) {
448 SysPrintf("failed to load cdr plugin\n");
451 if (CDR_open() < 0) {
452 SysPrintf("failed to open cdr plugin\n");
457 SetCdOpenCaseTime(time(NULL) + 2);
461 disk_current_index = index;
465 static unsigned int disk_get_num_images(void)
470 static bool disk_replace_image_index(unsigned index,
471 const struct retro_game_info *info)
476 if (index >= sizeof(disks) / sizeof(disks[0]))
479 old_fname = disks[index].fname;
480 disks[index].fname = NULL;
481 disks[index].internal_index = 0;
484 disks[index].fname = strdup(info->path);
485 if (index == disk_current_index)
486 ret = disk_set_image_index(index);
489 if (old_fname != NULL)
495 static bool disk_add_image_index(void)
504 static struct retro_disk_control_callback disk_control = {
505 .set_eject_state = disk_set_eject_state,
506 .get_eject_state = disk_get_eject_state,
507 .get_image_index = disk_get_image_index,
508 .set_image_index = disk_set_image_index,
509 .get_num_images = disk_get_num_images,
510 .replace_image_index = disk_replace_image_index,
511 .add_image_index = disk_add_image_index,
514 // just in case, maybe a win-rt port in the future?
521 static char base_dir[PATH_MAX];
523 static bool read_m3u(const char *file)
527 FILE *f = fopen(file, "r");
531 while (fgets(line, sizeof(line), f) && disk_count < sizeof(disks) / sizeof(disks[0])) {
534 char *carrige_return = strchr(line, '\r');
536 *carrige_return = '\0';
537 char *newline = strchr(line, '\n');
543 snprintf(name, sizeof(name), "%s%c%s", base_dir, SLASH, line);
544 disks[disk_count++].fname = strdup(name);
549 return (disk_count != 0);
552 static void extract_directory(char *buf, const char *path, size_t size)
555 strncpy(buf, path, size - 1);
556 buf[size - 1] = '\0';
558 base = strrchr(buf, '/');
560 base = strrchr(buf, '\\');
571 bool retro_load_game(const struct retro_game_info *info)
574 bool is_m3u = (strcasestr(info->path, ".m3u") != NULL);
576 #ifdef FRONTEND_SUPPORTS_RGB565
577 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
578 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
579 SysPrintf("RGB565 supported, using it\n");
583 if (info == NULL || info->path == NULL) {
584 SysPrintf("info->path required\n");
588 if (plugins_opened) {
593 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
594 if (disks[i].fname != NULL) {
595 free(disks[i].fname);
596 disks[i].fname = NULL;
598 disks[i].internal_index = 0;
601 disk_current_index = 0;
602 extract_directory(base_dir, info->path, sizeof(base_dir));
605 if (!read_m3u(info->path)) {
606 SysPrintf("failed to read m3u file\n");
611 disks[0].fname = strdup(info->path);
614 set_cd_image(disks[0].fname);
616 /* have to reload after set_cd_image for correct cdr plugin */
617 if (LoadPlugins() == -1) {
618 SysPrintf("failed to load plugins\n");
625 if (OpenPlugins() == -1) {
626 SysPrintf("failed to open plugins\n");
630 plugin_call_rearmed_cbs();
633 if (CheckCdrom() == -1) {
634 SysPrintf("unsupported/invalid CD image: %s\n", info->path);
640 if (LoadCdrom() == -1) {
641 SysPrintf("could not load CD-ROM!\n");
648 disk_count = cdrIsoMultidiskCount < 8 ? cdrIsoMultidiskCount : 8;
649 for (i = 1; i < sizeof(disks) / sizeof(disks[0]) && i < cdrIsoMultidiskCount; i++) {
650 disks[i].fname = strdup(info->path);
651 disks[i].internal_index = i;
658 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
663 void retro_unload_game(void)
667 unsigned retro_get_region(void)
669 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
672 void *retro_get_memory_data(unsigned id)
674 if (id == RETRO_MEMORY_SAVE_RAM)
680 size_t retro_get_memory_size(unsigned id)
682 if (id == RETRO_MEMORY_SAVE_RAM)
688 void retro_reset(void)
693 static const unsigned short retro_psx_map[] = {
694 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
695 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
696 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
697 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
698 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
699 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
700 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
701 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
702 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
703 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
704 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
705 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
706 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
707 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
708 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
709 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
711 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
719 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
720 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
721 in_keystate |= retro_psx_map[i];
723 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
724 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
725 in_keystate |= retro_psx_map[i];
730 samples_to_send += is_pal_mode ? 44100 / 50 : 44100 / 60;
732 video_cb((vout_fb_dirty || !vout_can_dupe) ? vout_buf : NULL,
733 vout_width, vout_height, vout_width * 2);
737 void retro_init(void)
739 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
745 ret = emu_core_preinit();
746 ret |= emu_core_init();
748 SysPrintf("PCSX init failed.\n");
752 vout_buf = malloc(640 * 512 * 2);
754 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
756 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
758 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
759 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
760 f = fopen(path, "r");
762 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
768 SysPrintf("found BIOS file: %s\n", Config.Bios);
772 SysPrintf("no BIOS files found.\n");
775 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
777 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
778 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
780 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
781 * we have to do this because cache misses and some IO penalties
782 * are not emulated. Warning: changing this may break compatibility. */
783 #ifdef __ARM_ARCH_7A__
784 cycle_multiplier = 175;
786 cycle_multiplier = 200;
791 init_memcard(Mcd1Data);
793 SaveFuncs.open = save_open;
794 SaveFuncs.read = save_read;
795 SaveFuncs.write = save_write;
796 SaveFuncs.seek = save_seek;
797 SaveFuncs.close = save_close;
800 void retro_deinit(void)