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