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 vout_width, vout_height;
32 static int vout_doffs_old, vout_fb_dirty;
34 static int samples_sent, samples_to_send;
35 static int plugins_opened;
37 /* memory card data */
38 extern char Mcd1Data[MCD_SIZE];
39 extern char McdDisable[2];
41 /* PCSX ReARMed core calls and stuff */
42 int in_type1, in_type2;
43 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
45 int in_enable_vibration;
47 static void init_memcard(char *mcd_data)
52 memset(mcd_data, 0, MCD_SIZE);
54 mcd_data[off++] = 'M';
55 mcd_data[off++] = 'C';
57 mcd_data[off++] = 0x0e;
59 for (i = 0; i < 15; i++) {
60 mcd_data[off++] = 0xa0;
62 mcd_data[off++] = 0xff;
63 mcd_data[off++] = 0xff;
65 mcd_data[off++] = 0xa0;
68 for (i = 0; i < 20; i++) {
69 mcd_data[off++] = 0xff;
70 mcd_data[off++] = 0xff;
71 mcd_data[off++] = 0xff;
72 mcd_data[off++] = 0xff;
74 mcd_data[off++] = 0xff;
75 mcd_data[off++] = 0xff;
80 static int vout_open(void)
85 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
91 #ifndef FRONTEND_SUPPORTS_RGB565
92 static void convert(void *buf, size_t bytes)
94 unsigned int i, v, *p = buf;
96 for (i = 0; i < bytes / 4; i++) {
98 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
103 static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
105 unsigned short *dest = vout_buf;
106 const unsigned short *src = vram;
107 int dstride = vout_width, h1 = h;
112 memset(vout_buf, 0, dstride * h * 2);
116 doffs = (vout_height - h) * dstride;
117 doffs += (dstride - w) / 2 & ~1;
118 if (doffs != vout_doffs_old) {
120 memset(vout_buf, 0, dstride * h * 2);
121 vout_doffs_old = doffs;
127 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
128 for (; h1-- > 0; dest += dstride, src += stride)
130 bgr888_to_rgb565(dest, src, w * 3);
135 for (; h1-- > 0; dest += dstride, src += stride)
137 bgr555_to_rgb565(dest, src, w * 2);
142 #ifndef FRONTEND_SUPPORTS_RGB565
143 convert(vout_buf, vout_width * vout_height * 2);
146 pl_rearmed_cbs.flip_cnt++;
149 static void vout_close(void)
153 static void *pl_mmap(unsigned int size)
155 return psxMap(0, size, 0, MAP_TAG_VRAM);
158 static void pl_munmap(void *ptr, unsigned int size)
160 psxUnmap(ptr, size, MAP_TAG_VRAM);
163 struct rearmed_cbs pl_rearmed_cbs = {
164 .pl_vout_open = vout_open,
165 .pl_vout_set_mode = vout_set_mode,
166 .pl_vout_flip = vout_flip,
167 .pl_vout_close = vout_close,
170 /* from psxcounters */
171 .gpu_hcnt = &hSyncCount,
172 .gpu_frame_count = &frame_counter,
175 void pl_frame_limit(void)
177 /* called once per frame, make psxCpu->Execute() above return */
181 void pl_timing_prepare(int is_pal)
185 void plat_trigger_vibrate(int is_strong)
189 void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
194 static int snd_init(void)
199 static void snd_finish(void)
203 static int snd_busy(void)
205 if (samples_to_send > samples_sent)
206 return 0; /* give more samples */
211 static void snd_feed(void *buf, int bytes)
213 audio_batch_cb(buf, bytes / 4);
214 samples_sent += bytes / 4;
217 void out_register_libretro(struct out_driver *drv)
219 drv->name = "libretro";
220 drv->init = snd_init;
221 drv->finish = snd_finish;
222 drv->busy = snd_busy;
223 drv->feed = snd_feed;
227 void retro_set_environment(retro_environment_t cb) { environ_cb = cb; }
228 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
229 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
230 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
231 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
232 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
234 unsigned retro_api_version(void)
236 return RETRO_API_VERSION;
239 void retro_set_controller_port_device(unsigned port, unsigned device)
243 void retro_get_system_info(struct retro_system_info *info)
245 memset(info, 0, sizeof(*info));
246 info->library_name = "PCSX-ReARMed";
247 info->library_version = REV;
248 info->valid_extensions = "bin|cue|img|mdf|pbp|cbn";
249 info->need_fullpath = true;
252 void retro_get_system_av_info(struct retro_system_av_info *info)
254 memset(info, 0, sizeof(*info));
255 info->timing.fps = 60;
256 info->timing.sample_rate = 44100;
257 info->geometry.base_width = 320;
258 info->geometry.base_height = 240;
259 info->geometry.max_width = 640;
260 info->geometry.max_height = 512;
261 info->geometry.aspect_ratio = 4.0 / 3.0;
265 size_t retro_serialize_size(void)
270 bool retro_serialize(void *data, size_t size)
275 bool retro_unserialize(const void *data, size_t size)
280 void retro_cheat_reset(void)
284 void retro_cheat_set(unsigned index, bool enabled, const char *code)
288 bool retro_load_game(const struct retro_game_info *info)
290 #ifdef FRONTEND_SUPPORTS_RGB565
291 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
292 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
293 SysPrintf("RGB565 supported, using it\n");
297 if (plugins_opened) {
302 set_cd_image(info->path);
304 /* have to reload after set_cd_image for correct cdr plugin */
305 if (LoadPlugins() == -1) {
306 SysPrintf("failed to load plugins\n");
313 if (OpenPlugins() == -1) {
314 SysPrintf("failed to open plugins\n");
318 plugin_call_rearmed_cbs();
321 if (CheckCdrom() == -1) {
322 SysPrintf("unsupported/invalid CD image: %s\n", info->path);
328 if (LoadCdrom() == -1) {
329 SysPrintf("could not load CD-ROM!\n");
337 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
342 void retro_unload_game(void)
346 unsigned retro_get_region(void)
348 return RETRO_REGION_NTSC;
351 void *retro_get_memory_data(unsigned id)
356 size_t retro_get_memory_size(unsigned id)
361 void retro_reset(void)
366 static const unsigned short retro_psx_map[] = {
367 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
368 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
369 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
370 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
371 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
372 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
373 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
374 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
375 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
376 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
377 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
378 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
379 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
380 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
381 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
382 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
384 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
392 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
393 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
394 in_keystate |= retro_psx_map[i];
396 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
397 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
398 in_keystate |= retro_psx_map[i];
403 samples_to_send += 44100 / 60;
405 video_cb(vout_fb_dirty ? vout_buf : NULL, vout_width, vout_height, vout_width * 2);
409 void retro_init(void)
411 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
417 ret = emu_core_preinit();
418 ret |= emu_core_init();
420 SysPrintf("PCSX init failed.\n");
424 vout_buf = malloc(640 * 512 * 2);
426 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
428 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
430 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
431 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
432 f = fopen(path, "r");
434 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
440 SysPrintf("found BIOS file: %s\n", Config.Bios);
444 SysPrintf("no BIOS files found.\n");
447 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
449 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
450 * we have to do this because cache misses and some IO penalties
451 * are not emulated. Warning: changing this may break compatibility. */
452 #ifdef __ARM_ARCH_7A__
453 cycle_multiplier = 175;
455 cycle_multiplier = 200;
460 init_memcard(Mcd1Data);
463 void retro_deinit(void)