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 /* memory card data */
36 extern char Mcd1Data[MCD_SIZE];
37 extern char McdDisable[2];
39 /* PCSX ReARMed core calls and stuff */
40 int in_type1, in_type2;
41 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
43 int in_enable_vibration;
45 static int vout_open(void)
50 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
54 static void convert(void *buf, size_t bytes)
56 unsigned int i, v, *p = buf;
58 for (i = 0; i < bytes / 4; i++) {
60 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
64 static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
66 unsigned short *dest = vout_buf;
67 const unsigned short *src = vram;
68 int dstride = w, h1 = h;
72 memset(vout_buf, 0, dstride * h * 2);
78 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
79 for (; h1-- > 0; dest += dstride, src += stride)
81 bgr888_to_rgb565(dest, src, w * 3);
86 for (; h1-- > 0; dest += dstride, src += stride)
88 bgr555_to_rgb565(dest, src, w * 2);
94 convert(vout_buf, w * h * 2);
95 video_cb(vout_buf, w, h, w * 2);
96 pl_rearmed_cbs.flip_cnt++;
99 static void vout_close(void)
103 static void *pl_mmap(unsigned int size)
105 return psxMap(0, size, 0, MAP_TAG_VRAM);
108 static void pl_munmap(void *ptr, unsigned int size)
110 psxUnmap(ptr, size, MAP_TAG_VRAM);
113 struct rearmed_cbs pl_rearmed_cbs = {
114 .pl_vout_open = vout_open,
115 .pl_vout_set_mode = vout_set_mode,
116 .pl_vout_flip = vout_flip,
117 .pl_vout_close = vout_close,
120 /* from psxcounters */
121 .gpu_hcnt = &hSyncCount,
122 .gpu_frame_count = &frame_counter,
125 void pl_frame_limit(void)
127 /* called once per frame, make psxCpu->Execute() above return */
131 void pl_timing_prepare(int is_pal)
135 void plat_trigger_vibrate(int is_strong)
139 void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
144 static int snd_init(void)
149 static void snd_finish(void)
153 static int snd_busy(void)
155 if (samples_to_send > samples_sent)
156 return 0; /* give more samples */
161 static void snd_feed(void *buf, int bytes)
163 audio_batch_cb(buf, bytes / 4);
164 samples_sent += bytes / 4;
167 void out_register_libretro(struct out_driver *drv)
169 drv->name = "libretro";
170 drv->init = snd_init;
171 drv->finish = snd_finish;
172 drv->busy = snd_busy;
173 drv->feed = snd_feed;
177 void retro_set_environment(retro_environment_t cb) { environ_cb = cb; }
178 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
179 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
180 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
181 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
182 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
184 unsigned retro_api_version(void)
186 return RETRO_API_VERSION;
189 void retro_set_controller_port_device(unsigned port, unsigned device)
193 void retro_get_system_info(struct retro_system_info *info)
195 memset(info, 0, sizeof(*info));
196 info->library_name = "PCSX-ReARMed";
197 info->library_version = REV;
198 info->valid_extensions = "bin|cue|img|mdf|pbp|cbn";
199 info->need_fullpath = true;
202 void retro_get_system_av_info(struct retro_system_av_info *info)
204 memset(info, 0, sizeof(*info));
205 info->timing.fps = 60;
206 info->timing.sample_rate = 44100;
207 info->geometry.base_width = 320;
208 info->geometry.base_height = 240;
209 info->geometry.max_width = 640;
210 info->geometry.max_height = 512;
211 info->geometry.aspect_ratio = 4.0 / 3.0;
215 size_t retro_serialize_size(void)
220 bool retro_serialize(void *data, size_t size)
225 bool retro_unserialize(const void *data, size_t size)
230 void retro_cheat_reset(void)
234 void retro_cheat_set(unsigned index, bool enabled, const char *code)
238 bool retro_load_game(const struct retro_game_info *info)
240 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
241 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
243 fprintf(stderr, "RGB565 supported, using it\n");
246 if (plugins_opened) {
251 set_cd_image(info->path);
253 /* have to reload after set_cd_image for correct cdr plugin */
254 if (LoadPlugins() == -1) {
255 printf("faled to load plugins\n");
262 if (OpenPlugins() == -1) {
263 printf("faled to open plugins\n");
267 plugin_call_rearmed_cbs();
270 if (CheckCdrom() == -1) {
271 printf("unsupported/invalid CD image: %s\n", info->path);
277 if (LoadCdrom() == -1) {
278 printf("could not load CD-ROM!\n");
286 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
291 void retro_unload_game(void)
295 unsigned retro_get_region(void)
297 return RETRO_REGION_NTSC;
300 void *retro_get_memory_data(unsigned id)
305 size_t retro_get_memory_size(unsigned id)
310 void retro_reset(void)
315 static const unsigned short retro_psx_map[] = {
316 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
317 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
318 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
319 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
320 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
321 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
322 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
323 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
324 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
325 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
326 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
327 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
328 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
329 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
330 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
331 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
333 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
341 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
342 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
343 in_keystate |= retro_psx_map[i];
345 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
346 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
347 in_keystate |= retro_psx_map[i];
352 samples_to_send += 44100 / 60;
355 void retro_init(void)
357 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
363 ret = emu_core_preinit();
364 ret |= emu_core_init();
366 printf("PCSX init failed, sorry\n");
370 vout_buf = malloc(640 * 512 * 2);
372 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
374 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
376 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
377 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
378 f = fopen(path, "r");
380 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
386 printf("found BIOS file: %s\n", Config.Bios);
390 printf("no BIOS files found.\n");
393 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
399 void retro_deinit(void)