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