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/cheat.h"
17 #include "../plugins/dfsound/out.h"
21 #include "plugin_lib.h"
25 static retro_video_refresh_t video_cb;
26 static retro_input_poll_t input_poll_cb;
27 static retro_input_state_t input_state_cb;
28 static retro_environment_t environ_cb;
29 static retro_audio_sample_batch_t audio_batch_cb;
31 static void *vout_buf;
32 static int vout_width, vout_height;
33 static int vout_doffs_old, vout_fb_dirty;
34 static bool vout_can_dupe;
36 static int samples_sent, samples_to_send;
37 static int plugins_opened;
38 static int is_pal_mode;
40 /* memory card data */
41 extern char Mcd1Data[MCD_SIZE];
42 extern char McdDisable[2];
44 /* PCSX ReARMed core calls and stuff */
45 int in_type1, in_type2;
46 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
48 int in_enable_vibration;
50 static void init_memcard(char *mcd_data)
55 memset(mcd_data, 0, MCD_SIZE);
57 mcd_data[off++] = 'M';
58 mcd_data[off++] = 'C';
60 mcd_data[off++] = 0x0e;
62 for (i = 0; i < 15; i++) {
63 mcd_data[off++] = 0xa0;
65 mcd_data[off++] = 0xff;
66 mcd_data[off++] = 0xff;
68 mcd_data[off++] = 0xa0;
71 for (i = 0; i < 20; i++) {
72 mcd_data[off++] = 0xff;
73 mcd_data[off++] = 0xff;
74 mcd_data[off++] = 0xff;
75 mcd_data[off++] = 0xff;
77 mcd_data[off++] = 0xff;
78 mcd_data[off++] = 0xff;
83 static int vout_open(void)
88 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
94 #ifndef FRONTEND_SUPPORTS_RGB565
95 static void convert(void *buf, size_t bytes)
97 unsigned int i, v, *p = buf;
99 for (i = 0; i < bytes / 4; i++) {
101 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
106 static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
108 unsigned short *dest = vout_buf;
109 const unsigned short *src = vram;
110 int dstride = vout_width, h1 = h;
115 memset(vout_buf, 0, dstride * h * 2);
119 doffs = (vout_height - h) * dstride;
120 doffs += (dstride - w) / 2 & ~1;
121 if (doffs != vout_doffs_old) {
123 memset(vout_buf, 0, dstride * h * 2);
124 vout_doffs_old = doffs;
130 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
131 for (; h1-- > 0; dest += dstride, src += stride)
133 bgr888_to_rgb565(dest, src, w * 3);
138 for (; h1-- > 0; dest += dstride, src += stride)
140 bgr555_to_rgb565(dest, src, w * 2);
145 #ifndef FRONTEND_SUPPORTS_RGB565
146 convert(vout_buf, vout_width * vout_height * 2);
149 pl_rearmed_cbs.flip_cnt++;
152 static void vout_close(void)
156 static void *pl_mmap(unsigned int size)
158 return psxMap(0, size, 0, MAP_TAG_VRAM);
161 static void pl_munmap(void *ptr, unsigned int size)
163 psxUnmap(ptr, size, MAP_TAG_VRAM);
166 struct rearmed_cbs pl_rearmed_cbs = {
167 .pl_vout_open = vout_open,
168 .pl_vout_set_mode = vout_set_mode,
169 .pl_vout_flip = vout_flip,
170 .pl_vout_close = vout_close,
173 /* from psxcounters */
174 .gpu_hcnt = &hSyncCount,
175 .gpu_frame_count = &frame_counter,
178 void pl_frame_limit(void)
180 /* called once per frame, make psxCpu->Execute() above return */
184 void pl_timing_prepare(int is_pal)
186 is_pal_mode = is_pal;
189 void plat_trigger_vibrate(int is_strong)
193 void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
198 static int snd_init(void)
203 static void snd_finish(void)
207 static int snd_busy(void)
209 if (samples_to_send > samples_sent)
210 return 0; /* give more samples */
215 static void snd_feed(void *buf, int bytes)
217 audio_batch_cb(buf, bytes / 4);
218 samples_sent += bytes / 4;
221 void out_register_libretro(struct out_driver *drv)
223 drv->name = "libretro";
224 drv->init = snd_init;
225 drv->finish = snd_finish;
226 drv->busy = snd_busy;
227 drv->feed = snd_feed;
231 void retro_set_environment(retro_environment_t cb) { environ_cb = cb; }
232 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
233 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
234 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
235 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
236 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
238 unsigned retro_api_version(void)
240 return RETRO_API_VERSION;
243 void retro_set_controller_port_device(unsigned port, unsigned device)
247 void retro_get_system_info(struct retro_system_info *info)
249 memset(info, 0, sizeof(*info));
250 info->library_name = "PCSX-ReARMed";
251 info->library_version = REV;
252 info->valid_extensions = "bin|cue|img|mdf|pbp|cbn";
253 info->need_fullpath = true;
256 void retro_get_system_av_info(struct retro_system_av_info *info)
258 memset(info, 0, sizeof(*info));
259 info->timing.fps = is_pal_mode ? 50 : 60;
260 info->timing.sample_rate = 44100;
261 info->geometry.base_width = 320;
262 info->geometry.base_height = 240;
263 info->geometry.max_width = 640;
264 info->geometry.max_height = 512;
265 info->geometry.aspect_ratio = 4.0 / 3.0;
269 size_t retro_serialize_size(void)
271 // it's currently 4380651 bytes, but have some reserved for future
281 static void *save_open(const char *name, const char *mode)
285 if (name == NULL || mode == NULL)
288 fp = malloc(sizeof(*fp));
292 fp->buf = (char *)name;
294 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
299 static int save_read(void *file, void *buf, u32 len)
301 struct save_fp *fp = file;
302 if (fp == NULL || buf == NULL)
305 memcpy(buf, fp->buf + fp->pos, len);
310 static int save_write(void *file, const void *buf, u32 len)
312 struct save_fp *fp = file;
313 if (fp == NULL || buf == NULL)
316 memcpy(fp->buf + fp->pos, buf, len);
321 static long save_seek(void *file, long offs, int whence)
323 struct save_fp *fp = file;
339 static void save_close(void *file)
341 struct save_fp *fp = file;
342 size_t r_size = retro_serialize_size();
346 if (fp->pos > r_size)
347 SysPrintf("ERROR: save buffer overflow detected\n");
348 else if (fp->is_write && fp->pos < r_size)
349 // make sure we don't save trash in leftover space
350 memset(fp->buf + fp->pos, 0, r_size - fp->pos);
354 bool retro_serialize(void *data, size_t size)
356 int ret = SaveState(data);
357 return ret == 0 ? true : false;
360 bool retro_unserialize(const void *data, size_t size)
362 int ret = LoadState(data);
363 return ret == 0 ? true : false;
366 void retro_cheat_reset(void)
371 void retro_cheat_set(unsigned index, bool enabled, const char *code)
376 // cheat funcs are destructive, need a copy..
377 strncpy(buf, code, sizeof(buf));
378 buf[sizeof(buf) - 1] = 0;
380 if (index < NumCheats)
381 ret = EditCheat(index, "", buf);
383 ret = AddCheat("", buf);
386 SysPrintf("Failed to set cheat %#u\n", index);
387 else if (index < NumCheats)
388 Cheats[index].Enabled = enabled;
391 bool retro_load_game(const struct retro_game_info *info)
393 #ifdef FRONTEND_SUPPORTS_RGB565
394 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
395 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
396 SysPrintf("RGB565 supported, using it\n");
400 if (plugins_opened) {
405 set_cd_image(info->path);
407 /* have to reload after set_cd_image for correct cdr plugin */
408 if (LoadPlugins() == -1) {
409 SysPrintf("failed to load plugins\n");
416 if (OpenPlugins() == -1) {
417 SysPrintf("failed to open plugins\n");
421 plugin_call_rearmed_cbs();
424 if (CheckCdrom() == -1) {
425 SysPrintf("unsupported/invalid CD image: %s\n", info->path);
431 if (LoadCdrom() == -1) {
432 SysPrintf("could not load CD-ROM!\n");
440 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
445 void retro_unload_game(void)
449 unsigned retro_get_region(void)
451 return is_pal_mode ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
454 void *retro_get_memory_data(unsigned id)
459 size_t retro_get_memory_size(unsigned id)
464 void retro_reset(void)
469 static const unsigned short retro_psx_map[] = {
470 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
471 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
472 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
473 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
474 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
475 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
476 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
477 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
478 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
479 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
480 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
481 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
482 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
483 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
484 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
485 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
487 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
495 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
496 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
497 in_keystate |= retro_psx_map[i];
499 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
500 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
501 in_keystate |= retro_psx_map[i];
506 samples_to_send += is_pal_mode ? 44100 / 50 : 44100 / 60;
508 video_cb((vout_fb_dirty || !vout_can_dupe) ? vout_buf : NULL,
509 vout_width, vout_height, vout_width * 2);
513 void retro_init(void)
515 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
521 ret = emu_core_preinit();
522 ret |= emu_core_init();
524 SysPrintf("PCSX init failed.\n");
528 vout_buf = malloc(640 * 512 * 2);
530 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
532 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
534 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
535 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
536 f = fopen(path, "r");
538 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
544 SysPrintf("found BIOS file: %s\n", Config.Bios);
548 SysPrintf("no BIOS files found.\n");
551 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
553 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
555 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
556 * we have to do this because cache misses and some IO penalties
557 * are not emulated. Warning: changing this may break compatibility. */
558 #ifdef __ARM_ARCH_7A__
559 cycle_multiplier = 175;
561 cycle_multiplier = 200;
566 init_memcard(Mcd1Data);
568 SaveFuncs.open = save_open;
569 SaveFuncs.read = save_read;
570 SaveFuncs.write = save_write;
571 SaveFuncs.seek = save_seek;
572 SaveFuncs.close = save_close;
575 void retro_deinit(void)