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 "../plugins/dfsound/out.h"
17 #include "../plugins/gpulib/cspace.h"
20 #include "plugin_lib.h"
24 static retro_video_refresh_t video_cb;
25 static retro_input_poll_t input_poll_cb;
26 static retro_input_state_t input_state_cb;
27 static retro_environment_t environ_cb;
28 static retro_audio_sample_batch_t audio_batch_cb;
30 static void *vout_buf;
31 static int samples_sent, samples_to_send;
32 static int plugins_opened;
33 static int native_rgb565;
35 /* PCSX ReARMed core calls and stuff */
36 int in_type1, in_type2;
37 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
39 int in_enable_vibration;
41 static int vout_open(void)
46 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
50 static void convert(void *buf, size_t bytes)
52 unsigned int i, v, *p = buf;
54 for (i = 0; i < bytes / 4; i++) {
56 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
60 static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
62 unsigned short *dest = vout_buf;
63 const unsigned short *src = vram;
64 int dstride = w, h1 = h;
68 memset(vout_buf, 0, dstride * h * 2);
74 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
75 for (; h1-- > 0; dest += dstride, src += stride)
77 bgr888_to_rgb565(dest, src, w * 3);
82 for (; h1-- > 0; dest += dstride, src += stride)
84 bgr555_to_rgb565(dest, src, w * 2);
90 convert(vout_buf, w * h * 2);
91 video_cb(vout_buf, w, h, w * 2);
92 pl_rearmed_cbs.flip_cnt++;
95 static void vout_close(void)
99 static void *pl_mmap(unsigned int size)
101 return psxMap(0, size, 0, MAP_TAG_VRAM);
104 static void pl_munmap(void *ptr, unsigned int size)
106 psxUnmap(ptr, size, MAP_TAG_VRAM);
109 struct rearmed_cbs pl_rearmed_cbs = {
110 .pl_vout_open = vout_open,
111 .pl_vout_set_mode = vout_set_mode,
112 .pl_vout_flip = vout_flip,
113 .pl_vout_close = vout_close,
116 /* from psxcounters */
117 .gpu_hcnt = &hSyncCount,
118 .gpu_frame_count = &frame_counter,
121 void pl_frame_limit(void)
123 /* called once per frame, make psxCpu->Execute() above return */
127 void pl_timing_prepare(int is_pal)
131 void plat_trigger_vibrate(int is_strong)
135 void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
140 static int snd_init(void)
145 static void snd_finish(void)
149 static int snd_busy(void)
151 if (samples_to_send > samples_sent)
152 return 0; /* give more samples */
157 static void snd_feed(void *buf, int bytes)
159 audio_batch_cb(buf, bytes / 4);
160 samples_sent += bytes / 4;
163 void out_register_libretro(struct out_driver *drv)
165 drv->name = "libretro";
166 drv->init = snd_init;
167 drv->finish = snd_finish;
168 drv->busy = snd_busy;
169 drv->feed = snd_feed;
173 void retro_set_environment(retro_environment_t cb) { environ_cb = cb; }
174 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
175 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
176 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
177 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
178 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
180 unsigned retro_api_version(void)
182 return RETRO_API_VERSION;
185 void retro_set_controller_port_device(unsigned port, unsigned device)
189 void retro_get_system_info(struct retro_system_info *info)
191 memset(info, 0, sizeof(*info));
192 info->library_name = "PCSX-ReARMed";
193 info->library_version = REV;
194 info->valid_extensions = "bin|cue|img|mdf|pbp|cbn";
195 info->need_fullpath = true;
198 void retro_get_system_av_info(struct retro_system_av_info *info)
200 memset(info, 0, sizeof(*info));
201 info->timing.fps = 60;
202 info->timing.sample_rate = 44100;
203 info->geometry.base_width = 320;
204 info->geometry.base_height = 240;
205 info->geometry.max_width = 640;
206 info->geometry.max_height = 512;
207 info->geometry.aspect_ratio = 4.0 / 3.0;
211 size_t retro_serialize_size(void)
216 bool retro_serialize(void *data, size_t size)
221 bool retro_unserialize(const void *data, size_t size)
226 void retro_cheat_reset(void)
230 void retro_cheat_set(unsigned index, bool enabled, const char *code)
234 bool retro_load_game(const struct retro_game_info *info)
236 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
237 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
239 fprintf(stderr, "RGB565 supported, using it\n");
242 if (plugins_opened) {
247 set_cd_image(info->path);
249 /* have to reload after set_cd_image for correct cdr plugin */
250 if (LoadPlugins() == -1) {
251 printf("faled to load plugins\n");
258 if (OpenPlugins() == -1) {
259 printf("faled to open plugins\n");
263 plugin_call_rearmed_cbs();
266 if (CheckCdrom() == -1) {
267 printf("unsupported/invalid CD image: %s\n", info->path);
273 if (LoadCdrom() == -1) {
274 printf("could not load CD-ROM!\n");
282 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
287 void retro_unload_game(void)
291 unsigned retro_get_region(void)
293 return RETRO_REGION_NTSC;
296 void *retro_get_memory_data(unsigned id)
301 size_t retro_get_memory_size(unsigned id)
306 void retro_reset(void)
311 static const unsigned short retro_psx_map[] = {
312 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
313 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
314 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
315 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
316 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
317 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
318 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
319 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
320 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
321 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
322 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
323 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
324 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
325 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
326 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
327 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
329 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
337 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
338 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
339 in_keystate |= retro_psx_map[i];
341 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
342 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
343 in_keystate |= retro_psx_map[i];
348 samples_to_send += 44100 / 60;
351 void retro_init(void)
353 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
359 ret = emu_core_preinit();
360 ret |= emu_core_init();
362 printf("PCSX init failed, sorry\n");
366 vout_buf = malloc(640 * 512 * 2);
368 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
370 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
372 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
373 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
374 f = fopen(path, "r");
376 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
382 printf("found BIOS file: %s\n", Config.Bios);
386 printf("no BIOS files found.\n");
389 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
392 void retro_deinit(void)