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/new_dynarec/new_dynarec.h"
15 #include "../plugins/dfsound/out.h"
16 #include "../plugins/gpulib/cspace.h"
19 #include "plugin_lib.h"
23 static retro_video_refresh_t video_cb;
24 static retro_input_poll_t input_poll_cb;
25 static retro_input_state_t input_state_cb;
26 static retro_environment_t environ_cb;
27 static retro_audio_sample_batch_t audio_batch_cb;
29 static void *vout_buf;
30 static int samples_sent, samples_to_send;
31 static int plugins_opened;
32 static int native_rgb565;
34 /* PCSX ReARMed core calls and stuff */
35 int in_type1, in_type2;
36 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
38 int in_enable_vibration;
40 static int vout_open(void)
45 static void vout_set_mode(int w, int h, int bpp)
49 static void convert(void *buf, size_t bytes)
51 unsigned int i, v, *p = buf;
53 for (i = 0; i < bytes / 4; i++) {
55 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
59 static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
61 unsigned short *dest = vout_buf;
62 const unsigned short *src = vram;
63 int dstride = w, h1 = h;
67 memset(pl_vout_buf, 0, dstride * h * 2);
73 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
74 for (; h1-- > 0; dest += dstride, src += stride)
76 bgr888_to_rgb565(dest, src, w * 3);
81 for (; h1-- > 0; dest += dstride, src += stride)
83 bgr555_to_rgb565(dest, src, w * 2);
89 convert(vout_buf, w * h * 2);
90 video_cb(vout_buf, w, h, w * 2);
91 pl_rearmed_cbs.flip_cnt++;
94 static void vout_close(void)
98 struct rearmed_cbs pl_rearmed_cbs = {
99 .pl_vout_open = vout_open,
100 .pl_vout_set_mode = vout_set_mode,
101 .pl_vout_flip = vout_flip,
102 .pl_vout_close = vout_close,
103 /* from psxcounters */
104 .gpu_hcnt = &hSyncCount,
105 .gpu_frame_count = &frame_counter,
108 void pl_frame_limit(void)
110 /* called once per frame, make psxCpu->Execute() above return */
114 void pl_timing_prepare(int is_pal)
118 void plat_trigger_vibrate(int is_strong)
122 void pl_update_gun(int *xn, int *xres, int *y, int *in)
127 static int snd_init(void)
132 static void snd_finish(void)
136 static int snd_busy(void)
138 if (samples_to_send > samples_sent)
139 return 0; /* give more samples */
144 static void snd_feed(void *buf, int bytes)
146 audio_batch_cb(buf, bytes / 4);
147 samples_sent += bytes / 4;
150 void out_register_libretro(struct out_driver *drv)
152 drv->name = "libretro";
153 drv->init = snd_init;
154 drv->finish = snd_finish;
155 drv->busy = snd_busy;
156 drv->feed = snd_feed;
160 void retro_set_environment(retro_environment_t cb) { environ_cb = cb; }
161 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
162 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
163 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
164 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
165 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
167 unsigned retro_api_version(void)
169 return RETRO_API_VERSION;
172 void retro_set_controller_port_device(unsigned port, unsigned device)
176 void retro_get_system_info(struct retro_system_info *info)
178 memset(info, 0, sizeof(*info));
179 info->library_name = "PCSX-ReARMed";
180 info->library_version = REV;
181 info->valid_extensions = "bin|cue|img|mdf|pbp|cbn";
182 info->need_fullpath = true;
185 void retro_get_system_av_info(struct retro_system_av_info *info)
187 memset(info, 0, sizeof(*info));
188 info->timing.fps = 60;
189 info->timing.sample_rate = 44100;
190 info->geometry.base_width = 320;
191 info->geometry.base_height = 240;
192 info->geometry.max_width = 640;
193 info->geometry.max_height = 512;
194 info->geometry.aspect_ratio = 4.0 / 3.0;
198 size_t retro_serialize_size(void)
203 bool retro_serialize(void *data, size_t size)
208 bool retro_unserialize(const void *data, size_t size)
213 void retro_cheat_reset(void)
217 void retro_cheat_set(unsigned index, bool enabled, const char *code)
221 bool retro_load_game(const struct retro_game_info *info)
223 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
224 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
226 fprintf(stderr, "RGB565 supported, using it\n");
229 if (plugins_opened) {
234 set_cd_image(info->path);
236 /* have to reload after set_cd_image for correct cdr plugin */
237 if (LoadPlugins() == -1) {
238 printf("faled to load plugins\n");
245 if (OpenPlugins() == -1) {
246 printf("faled to open plugins\n");
250 plugin_call_rearmed_cbs();
253 if (CheckCdrom() == -1) {
254 printf("unsupported/invalid CD image: %s\n", info->path);
260 if (LoadCdrom() == -1) {
261 printf("could not load CD-ROM!\n");
269 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
274 void retro_unload_game(void)
278 unsigned retro_get_region(void)
280 return RETRO_REGION_NTSC;
283 void *retro_get_memory_data(unsigned id)
288 size_t retro_get_memory_size(unsigned id)
293 void retro_reset(void)
298 static const unsigned short retro_psx_map[] = {
299 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
300 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
301 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
302 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
303 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
304 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
305 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
306 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
307 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
308 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
309 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
310 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
311 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
312 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
313 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
314 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
316 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
324 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
325 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
326 in_keystate |= retro_psx_map[i];
328 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
329 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
330 in_keystate |= retro_psx_map[i];
335 samples_to_send += 44100 / 60;
338 void retro_init(void)
340 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
346 ret = emu_core_preinit();
347 ret |= emu_core_init();
349 printf("PCSX init failed, sorry\n");
353 vout_buf = malloc(640 * 512 * 2);
355 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
357 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
359 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
360 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
361 f = fopen(path, "r");
363 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
369 printf("found BIOS file: %s\n", Config.Bios);
373 printf("no BIOS files found.\n");
376 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
379 void retro_deinit(void)