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