libretro: check for CAN_DUPE, as suggested by ToadKing
[pcsx_rearmed.git] / frontend / libretro.c
CommitLineData
38c2028e 1/*
2 * (C) notaz, 2012
3 *
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include "../libpcsxcore/misc.h"
13#include "../libpcsxcore/psxcounters.h"
87e5b45f 14#include "../libpcsxcore/psxmem_map.h"
38c2028e 15#include "../libpcsxcore/new_dynarec/new_dynarec.h"
07c13dfd 16#include "../plugins/dfsound/out.h"
fa56d360 17#include "../plugins/gpulib/cspace.h"
38c2028e 18#include "main.h"
19#include "plugin.h"
20#include "plugin_lib.h"
21#include "revision.h"
22#include "libretro.h"
23
24static retro_video_refresh_t video_cb;
25static retro_input_poll_t input_poll_cb;
26static retro_input_state_t input_state_cb;
27static retro_environment_t environ_cb;
28static retro_audio_sample_batch_t audio_batch_cb;
29
30static void *vout_buf;
70d56ca3 31static int vout_width, vout_height;
32static int vout_doffs_old, vout_fb_dirty;
1a6164a1 33static bool vout_can_dupe;
70d56ca3 34
38c2028e 35static int samples_sent, samples_to_send;
36static int plugins_opened;
37
f9f60dae
TK
38/* memory card data */
39extern char Mcd1Data[MCD_SIZE];
40extern char McdDisable[2];
38c2028e 41
42/* PCSX ReARMed core calls and stuff */
43int in_type1, in_type2;
44int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
45int in_keystate;
46int in_enable_vibration;
47
7df396ea
TK
48static void init_memcard(char *mcd_data)
49{
50 unsigned off = 0;
51 unsigned i;
52
53 memset(mcd_data, 0, MCD_SIZE);
54
55 mcd_data[off++] = 'M';
56 mcd_data[off++] = 'C';
57 off += 0x7d;
58 mcd_data[off++] = 0x0e;
59
60 for (i = 0; i < 15; i++) {
61 mcd_data[off++] = 0xa0;
62 off += 0x07;
63 mcd_data[off++] = 0xff;
64 mcd_data[off++] = 0xff;
65 off += 0x75;
66 mcd_data[off++] = 0xa0;
67 }
68
69 for (i = 0; i < 20; i++) {
70 mcd_data[off++] = 0xff;
71 mcd_data[off++] = 0xff;
72 mcd_data[off++] = 0xff;
73 mcd_data[off++] = 0xff;
74 off += 0x04;
75 mcd_data[off++] = 0xff;
76 mcd_data[off++] = 0xff;
77 off += 0x76;
78 }
79}
80
38c2028e 81static int vout_open(void)
82{
83 return 0;
84}
85
e4c83ca6 86static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
38c2028e 87{
70d56ca3 88 vout_width = w;
89 vout_height = h;
38c2028e 90}
91
cc4f9b70 92#ifndef FRONTEND_SUPPORTS_RGB565
9c59f120 93static void convert(void *buf, size_t bytes)
38c2028e 94{
95 unsigned int i, v, *p = buf;
96
9c59f120 97 for (i = 0; i < bytes / 4; i++) {
38c2028e 98 v = p[i];
99 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
100 }
101}
46aa5b98 102#endif
38c2028e 103
fa56d360 104static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
38c2028e 105{
fa56d360 106 unsigned short *dest = vout_buf;
107 const unsigned short *src = vram;
70d56ca3 108 int dstride = vout_width, h1 = h;
109 int doffs;
38c2028e 110
fa56d360 111 if (vram == NULL) {
112 // blanking
cc56203b 113 memset(vout_buf, 0, dstride * h * 2);
fa56d360 114 goto out;
115 }
116
70d56ca3 117 doffs = (vout_height - h) * dstride;
118 doffs += (dstride - w) / 2 & ~1;
119 if (doffs != vout_doffs_old) {
120 // clear borders
121 memset(vout_buf, 0, dstride * h * 2);
122 vout_doffs_old = doffs;
123 }
124 dest += doffs;
125
fa56d360 126 if (bgr24)
127 {
128 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
129 for (; h1-- > 0; dest += dstride, src += stride)
130 {
131 bgr888_to_rgb565(dest, src, w * 3);
132 }
133 }
134 else
135 {
136 for (; h1-- > 0; dest += dstride, src += stride)
137 {
138 bgr555_to_rgb565(dest, src, w * 2);
139 }
140 }
38c2028e 141
fa56d360 142out:
46aa5b98 143#ifndef FRONTEND_SUPPORTS_RGB565
70d56ca3 144 convert(vout_buf, vout_width * vout_height * 2);
46aa5b98 145#endif
70d56ca3 146 vout_fb_dirty = 1;
fa56d360 147 pl_rearmed_cbs.flip_cnt++;
38c2028e 148}
149
150static void vout_close(void)
151{
152}
153
cc56203b 154static void *pl_mmap(unsigned int size)
155{
87e5b45f 156 return psxMap(0, size, 0, MAP_TAG_VRAM);
cc56203b 157}
158
159static void pl_munmap(void *ptr, unsigned int size)
160{
87e5b45f 161 psxUnmap(ptr, size, MAP_TAG_VRAM);
cc56203b 162}
163
38c2028e 164struct rearmed_cbs pl_rearmed_cbs = {
165 .pl_vout_open = vout_open,
166 .pl_vout_set_mode = vout_set_mode,
167 .pl_vout_flip = vout_flip,
168 .pl_vout_close = vout_close,
cc56203b 169 .mmap = pl_mmap,
170 .munmap = pl_munmap,
38c2028e 171 /* from psxcounters */
172 .gpu_hcnt = &hSyncCount,
173 .gpu_frame_count = &frame_counter,
174};
175
176void pl_frame_limit(void)
177{
178 /* called once per frame, make psxCpu->Execute() above return */
179 stop = 1;
180}
181
182void pl_timing_prepare(int is_pal)
183{
184}
185
186void plat_trigger_vibrate(int is_strong)
187{
188}
189
e4c83ca6 190void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
38c2028e 191{
192}
193
194/* sound calls */
07c13dfd 195static int snd_init(void)
38c2028e 196{
07c13dfd 197 return 0;
38c2028e 198}
199
07c13dfd 200static void snd_finish(void)
38c2028e 201{
202}
203
07c13dfd 204static int snd_busy(void)
38c2028e 205{
206 if (samples_to_send > samples_sent)
207 return 0; /* give more samples */
208 else
209 return 1;
210}
211
07c13dfd 212static void snd_feed(void *buf, int bytes)
38c2028e 213{
214 audio_batch_cb(buf, bytes / 4);
215 samples_sent += bytes / 4;
216}
217
07c13dfd 218void out_register_libretro(struct out_driver *drv)
219{
220 drv->name = "libretro";
221 drv->init = snd_init;
222 drv->finish = snd_finish;
223 drv->busy = snd_busy;
224 drv->feed = snd_feed;
225}
226
38c2028e 227/* libretro */
228void retro_set_environment(retro_environment_t cb) { environ_cb = cb; }
229void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
230void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
231void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
232void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
233void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
234
235unsigned retro_api_version(void)
236{
237 return RETRO_API_VERSION;
238}
239
240void retro_set_controller_port_device(unsigned port, unsigned device)
241{
242}
243
244void retro_get_system_info(struct retro_system_info *info)
245{
246 memset(info, 0, sizeof(*info));
247 info->library_name = "PCSX-ReARMed";
248 info->library_version = REV;
249 info->valid_extensions = "bin|cue|img|mdf|pbp|cbn";
250 info->need_fullpath = true;
251}
252
253void retro_get_system_av_info(struct retro_system_av_info *info)
254{
255 memset(info, 0, sizeof(*info));
256 info->timing.fps = 60;
257 info->timing.sample_rate = 44100;
258 info->geometry.base_width = 320;
259 info->geometry.base_height = 240;
260 info->geometry.max_width = 640;
261 info->geometry.max_height = 512;
262 info->geometry.aspect_ratio = 4.0 / 3.0;
263}
264
265/* TODO */
266size_t retro_serialize_size(void)
267{
268 return 0;
269}
270
271bool retro_serialize(void *data, size_t size)
272{
273 return false;
274}
275
276bool retro_unserialize(const void *data, size_t size)
277{
278 return false;
279}
280
281void retro_cheat_reset(void)
282{
283}
284
285void retro_cheat_set(unsigned index, bool enabled, const char *code)
286{
287}
288
289bool retro_load_game(const struct retro_game_info *info)
290{
46aa5b98 291#ifdef FRONTEND_SUPPORTS_RGB565
c19aba43 292 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
9f766dbe 293 if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
f29fbd53 294 SysPrintf("RGB565 supported, using it\n");
c19aba43 295 }
46aa5b98 296#endif
c19aba43 297
38c2028e 298 if (plugins_opened) {
299 ClosePlugins();
300 plugins_opened = 0;
301 }
302
303 set_cd_image(info->path);
304
305 /* have to reload after set_cd_image for correct cdr plugin */
306 if (LoadPlugins() == -1) {
f29fbd53 307 SysPrintf("failed to load plugins\n");
38c2028e 308 return false;
309 }
310
311 plugins_opened = 1;
312 NetOpened = 0;
313
314 if (OpenPlugins() == -1) {
f29fbd53 315 SysPrintf("failed to open plugins\n");
38c2028e 316 return false;
317 }
318
319 plugin_call_rearmed_cbs();
320
321 Config.PsxAuto = 1;
322 if (CheckCdrom() == -1) {
f29fbd53 323 SysPrintf("unsupported/invalid CD image: %s\n", info->path);
38c2028e 324 return false;
325 }
326
327 SysReset();
328
329 if (LoadCdrom() == -1) {
f29fbd53 330 SysPrintf("could not load CD-ROM!\n");
38c2028e 331 return false;
332 }
79f216e3 333 emu_on_new_cd(0);
38c2028e 334
335 return true;
336}
337
338bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
339{
340 return false;
341}
342
343void retro_unload_game(void)
344{
345}
346
347unsigned retro_get_region(void)
348{
349 return RETRO_REGION_NTSC;
350}
351
352void *retro_get_memory_data(unsigned id)
353{
f9f60dae 354 return Mcd1Data;
38c2028e 355}
356
357size_t retro_get_memory_size(unsigned id)
358{
f9f60dae 359 return MCD_SIZE;
38c2028e 360}
361
362void retro_reset(void)
363{
364 SysReset();
365}
366
367static const unsigned short retro_psx_map[] = {
368 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
369 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
370 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
371 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
372 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
373 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
374 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
375 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
376 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
377 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
378 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
379 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
380 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
381 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
382 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
383 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
384};
385#define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
386
387void retro_run(void)
388{
389 int i;
390
391 input_poll_cb();
392 in_keystate = 0;
393 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
394 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
395 in_keystate |= retro_psx_map[i];
396 in_keystate <<= 16;
397 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
398 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
399 in_keystate |= retro_psx_map[i];
400
401 stop = 0;
402 psxCpu->Execute();
403
404 samples_to_send += 44100 / 60;
e2bdb933 405
1a6164a1 406 video_cb((vout_fb_dirty || !vout_can_dupe) ? vout_buf : NULL,
407 vout_width, vout_height, vout_width * 2);
70d56ca3 408 vout_fb_dirty = 0;
38c2028e 409}
410
411void retro_init(void)
412{
413 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
414 const char *dir;
415 char path[256];
416 FILE *f = NULL;
417 int i, ret, level;
418
419 ret = emu_core_preinit();
420 ret |= emu_core_init();
421 if (ret != 0) {
f29fbd53 422 SysPrintf("PCSX init failed.\n");
38c2028e 423 exit(1);
424 }
425
426 vout_buf = malloc(640 * 512 * 2);
427
428 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
429 {
430 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
431
432 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
433 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
434 f = fopen(path, "r");
435 if (f != NULL) {
436 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
437 break;
438 }
439 }
440 }
441 if (f != NULL) {
f29fbd53 442 SysPrintf("found BIOS file: %s\n", Config.Bios);
38c2028e 443 fclose(f);
444 }
445 else
f29fbd53 446 SysPrintf("no BIOS files found.\n");
38c2028e 447
448 level = 1;
449 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
f9f60dae 450
1a6164a1 451 environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &vout_can_dupe);
452
5ca2ec64 453 /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times)
454 * we have to do this because cache misses and some IO penalties
455 * are not emulated. Warning: changing this may break compatibility. */
456#ifdef __ARM_ARCH_7A__
457 cycle_multiplier = 175;
458#else
459 cycle_multiplier = 200;
460#endif
461
f9f60dae
TK
462 McdDisable[0] = 0;
463 McdDisable[1] = 1;
7df396ea 464 init_memcard(Mcd1Data);
38c2028e 465}
466
467void retro_deinit(void)
468{
469 SysClose();
470 free(vout_buf);
471 vout_buf = NULL;
472}