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"
18 #include "plugin_lib.h"
22 static retro_video_refresh_t video_cb;
23 static retro_input_poll_t input_poll_cb;
24 static retro_input_state_t input_state_cb;
25 static retro_environment_t environ_cb;
26 static retro_audio_sample_batch_t audio_batch_cb;
28 static void *vout_buf;
29 static int vout_width, vout_height;
30 static int samples_sent, samples_to_send;
31 static int plugins_opened;
33 /* PCSX ReARMed core calls and stuff */
34 int in_type1, in_type2;
35 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
37 int in_enable_vibration;
39 static int vout_open(void)
44 static void *vout_set_mode(int w, int h, int bpp)
51 /* FIXME: either teach PCSX to blit to RGB1555 or RetroArch to support RGB565 */
52 static void convert(void *buf, size_t bytes)
54 unsigned int i, v, *p = buf;
56 for (i = 0; i < bytes / 4; i++) {
58 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
62 static void *vout_flip(void)
64 pl_rearmed_cbs.flip_cnt++;
65 convert(vout_buf, vout_width * vout_height * 2);
66 video_cb(vout_buf, vout_width, vout_height, vout_width * 2);
71 static void vout_close(void)
75 struct rearmed_cbs pl_rearmed_cbs = {
76 .pl_vout_open = vout_open,
77 .pl_vout_set_mode = vout_set_mode,
78 .pl_vout_flip = vout_flip,
79 .pl_vout_close = vout_close,
80 /* from psxcounters */
81 .gpu_hcnt = &hSyncCount,
82 .gpu_frame_count = &frame_counter,
85 void pl_frame_limit(void)
87 /* called once per frame, make psxCpu->Execute() above return */
91 void pl_timing_prepare(int is_pal)
95 void plat_trigger_vibrate(int is_strong)
99 void pl_update_gun(int *xn, int *xres, int *y, int *in)
104 static int snd_init(void)
109 static void snd_finish(void)
113 static int snd_busy(void)
115 if (samples_to_send > samples_sent)
116 return 0; /* give more samples */
121 static void snd_feed(void *buf, int bytes)
123 audio_batch_cb(buf, bytes / 4);
124 samples_sent += bytes / 4;
127 void out_register_libretro(struct out_driver *drv)
129 drv->name = "libretro";
130 drv->init = snd_init;
131 drv->finish = snd_finish;
132 drv->busy = snd_busy;
133 drv->feed = snd_feed;
137 void retro_set_environment(retro_environment_t cb) { environ_cb = cb; }
138 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
139 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
140 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
141 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
142 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
144 unsigned retro_api_version(void)
146 return RETRO_API_VERSION;
149 void retro_set_controller_port_device(unsigned port, unsigned device)
153 void retro_get_system_info(struct retro_system_info *info)
155 memset(info, 0, sizeof(*info));
156 info->library_name = "PCSX-ReARMed";
157 info->library_version = REV;
158 info->valid_extensions = "bin|cue|img|mdf|pbp|cbn";
159 info->need_fullpath = true;
162 void retro_get_system_av_info(struct retro_system_av_info *info)
164 memset(info, 0, sizeof(*info));
165 info->timing.fps = 60;
166 info->timing.sample_rate = 44100;
167 info->geometry.base_width = 320;
168 info->geometry.base_height = 240;
169 info->geometry.max_width = 640;
170 info->geometry.max_height = 512;
171 info->geometry.aspect_ratio = 4.0 / 3.0;
175 size_t retro_serialize_size(void)
180 bool retro_serialize(void *data, size_t size)
185 bool retro_unserialize(const void *data, size_t size)
190 void retro_cheat_reset(void)
194 void retro_cheat_set(unsigned index, bool enabled, const char *code)
198 bool retro_load_game(const struct retro_game_info *info)
200 if (plugins_opened) {
205 set_cd_image(info->path);
207 /* have to reload after set_cd_image for correct cdr plugin */
208 if (LoadPlugins() == -1) {
209 printf("faled to load plugins\n");
216 if (OpenPlugins() == -1) {
217 printf("faled to open plugins\n");
221 plugin_call_rearmed_cbs();
224 if (CheckCdrom() == -1) {
225 printf("unsupported/invalid CD image: %s\n", info->path);
231 if (LoadCdrom() == -1) {
232 printf("could not load CD-ROM!\n");
240 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
245 void retro_unload_game(void)
249 unsigned retro_get_region(void)
251 return RETRO_REGION_NTSC;
254 void *retro_get_memory_data(unsigned id)
259 size_t retro_get_memory_size(unsigned id)
264 void retro_reset(void)
269 static const unsigned short retro_psx_map[] = {
270 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
271 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
272 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
273 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
274 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
275 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
276 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
277 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
278 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
279 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
280 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
281 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
282 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
283 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
284 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
285 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
287 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
295 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
296 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
297 in_keystate |= retro_psx_map[i];
299 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
300 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
301 in_keystate |= retro_psx_map[i];
306 samples_to_send += 44100 / 60;
309 void retro_init(void)
311 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
317 ret = emu_core_preinit();
318 ret |= emu_core_init();
320 printf("PCSX init failed, sorry\n");
324 vout_buf = malloc(640 * 512 * 2);
326 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
328 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
330 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
331 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
332 f = fopen(path, "r");
334 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
340 printf("found BIOS file: %s\n", Config.Bios);
344 printf("no BIOS files found.\n");
347 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
350 void retro_deinit(void)