Add RGB565 support
[pcsx_rearmed.git] / frontend / plugin_lib.c
CommitLineData
b60f2812 1/*
8f892648 2 * (C) notaz, 2010-2011
b60f2812 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>
69af03a2 10#include <string.h>
11#include <stdarg.h>
799b0b87 12#include <stdint.h>
72228559 13#include <sys/time.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <fcntl.h>
17#include <unistd.h>
7c0f51de 18#include <pthread.h>
b60f2812 19
72228559 20#include "plugin_lib.h"
b60f2812 21#include "linux/fbdev.h"
69af03a2 22#include "common/fonts.h"
23#include "common/input.h"
3c70c47b 24#include "menu.h"
8f892648 25#include "main.h"
47821672 26#include "plat.h"
72228559 27#include "pcnt.h"
4c08b9e7 28#include "pl_gun_ts.h"
69af03a2 29#include "../libpcsxcore/new_dynarec/new_dynarec.h"
799b0b87 30#include "../libpcsxcore/psemu_plugin_defs.h"
b60f2812 31
4c08b9e7 32int in_type1, in_type2;
33int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
1b0c5139 34int in_adev[2] = { -1, -1 }, in_adev_axis[2][2] = {{ 0, 1 }, { 0, 1 }};
4c08b9e7 35int in_keystate, in_state_gun;
b944a30e 36int in_enable_vibration;
faf2b2aa 37void *tsdev;
76f7048e 38void *pl_vout_buf;
6469a8c4 39int g_layer_x, g_layer_y, g_layer_w, g_layer_h;
ddc0a02a 40static int pl_vout_w, pl_vout_h, pl_vout_bpp; /* output display/layer */
41static int psx_w, psx_h, psx_bpp;
a92f6af1 42static int vsync_cnt;
ab423939 43static int is_pal, frame_interval, frame_interval1024;
44static int vsync_usec_time;
72228559 45
4c08b9e7 46
47static __attribute__((noinline)) int get_cpu_ticks(void)
72228559 48{
49 static unsigned long last_utime;
50 static int fd;
51 unsigned long utime, ret;
52 char buf[128];
53
54 if (fd == 0)
55 fd = open("/proc/self/stat", O_RDONLY);
56 lseek(fd, 0, SEEK_SET);
57 buf[0] = 0;
58 read(fd, buf, sizeof(buf));
59 buf[sizeof(buf) - 1] = 0;
60
61 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
62 ret = utime - last_utime;
63 last_utime = utime;
64 return ret;
65}
66
41f55c9f 67static void print_msg(int h, int border)
8f892648 68{
76f7048e 69 if (pl_vout_bpp == 16)
41f55c9f 70 pl_text_out16(border + 2, h - 10, "%s", hud_msg);
8f892648 71}
72
41f55c9f 73static void print_fps(int h, int border)
72228559 74{
76f7048e 75 if (pl_vout_bpp == 16)
a92f6af1 76 pl_text_out16(border + 2, h - 10, "%2d %4.1f",
77 pl_rearmed_cbs.flips_per_sec, pl_rearmed_cbs.vsps_cur);
72228559 78}
79
41f55c9f 80static void print_cpu_usage(int w, int h, int border)
72228559 81{
76f7048e 82 if (pl_vout_bpp == 16)
a92f6af1 83 pl_text_out16(w - border - 28, h - 10, "%3d", pl_rearmed_cbs.cpu_usage);
72228559 84}
b60f2812 85
90f1d26c 86// draw 192x8 status of 24 sound channels
55b0eeea 87static __attribute__((noinline)) void draw_active_chans(int vout_w, int vout_h)
90f1d26c 88{
174c454a 89 extern void spu_get_debug_info(int *chans_out, int *run_chans,
90 int *fmod_chans_out, int *noise_chans_out); // hack
91 int live_chans, run_chans, fmod_chans, noise_chans;
90f1d26c 92
93 static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
76f7048e 94 unsigned short *dest = (unsigned short *)pl_vout_buf +
55b0eeea 95 vout_w * (vout_h - 10) + vout_w / 2 - 192/2;
90f1d26c 96 unsigned short *d, p;
97 int c, x, y;
98
76f7048e 99 if (pl_vout_bpp != 16)
90f1d26c 100 return;
101
174c454a 102 spu_get_debug_info(&live_chans, &run_chans, &fmod_chans, &noise_chans);
90f1d26c 103
104 for (c = 0; c < 24; c++) {
105 d = dest + c * 8;
174c454a 106 p = !(live_chans & (1<<c)) ? (run_chans & (1<<c) ? 0x01c0 : 0) :
90f1d26c 107 (fmod_chans & (1<<c)) ? 0xf000 :
108 (noise_chans & (1<<c)) ? 0x001f :
109 colors[c & 1];
55b0eeea 110 for (y = 0; y < 8; y++, d += vout_w)
90f1d26c 111 for (x = 0; x < 8; x++)
112 d[x] = p;
113 }
114}
115
6469a8c4 116void pl_print_hud(int xborder)
55b0eeea 117{
6469a8c4 118 int w = pl_vout_w, h = pl_vout_h;
55b0eeea 119
aafcb4dd 120 if (h < 16)
121 return;
122
55b0eeea 123 if (g_opts & OPT_SHOWSPU)
124 draw_active_chans(w, h);
125
126 if (hud_msg[0] != 0)
41f55c9f 127 print_msg(h, xborder);
55b0eeea 128 else if (g_opts & OPT_SHOWFPS)
41f55c9f 129 print_fps(h, xborder);
55b0eeea 130
131 if (g_opts & OPT_SHOWCPU)
41f55c9f 132 print_cpu_usage(w, h, xborder);
55b0eeea 133}
134
9e5ac38f 135/* update scaler target size according to user settings */
136static void update_layer_size(int w, int h)
137{
138 float mult;
139 int imult;
140
141 switch (g_scaler) {
142 case SCALE_1_1:
143 g_layer_w = w; g_layer_h = h;
144 break;
145
146 case SCALE_4_3v2:
147 if (h > g_menuscreen_h || (240 < h && h <= 360))
148 goto fractional_4_3;
149
150 // 4:3 that prefers integer scaling
151 imult = g_menuscreen_h / h;
152 g_layer_w = w * imult;
153 g_layer_h = h * imult;
154 mult = (float)g_layer_w / (float)g_layer_h;
155 if (mult < 1.25f || mult > 1.666f)
156 g_layer_w = 4.0f/3.0f * (float)g_layer_h;
157 printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult);
158 break;
159
160 fractional_4_3:
161 case SCALE_4_3:
162 mult = 240.0f / (float)h * 4.0f / 3.0f;
163 if (h > 256)
164 mult *= 2.0f;
165 g_layer_w = mult * (float)g_menuscreen_h;
166 g_layer_h = g_menuscreen_h;
167 printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult);
168 break;
169
170 case SCALE_FULLSCREEN:
171 g_layer_w = g_menuscreen_w;
172 g_layer_h = g_menuscreen_h;
173 break;
174
175 default:
176 break;
177 }
178
179 g_layer_x = g_menuscreen_w / 2 - g_layer_w / 2;
180 g_layer_y = g_menuscreen_h / 2 - g_layer_h / 2;
181 if (g_layer_x < 0) g_layer_x = 0;
182 if (g_layer_y < 0) g_layer_y = 0;
183 if (g_layer_w > g_menuscreen_w) g_layer_w = g_menuscreen_w;
184 if (g_layer_h > g_menuscreen_h) g_layer_h = g_menuscreen_h;
185}
186
76f7048e 187static void *pl_vout_set_mode(int w, int h, int bpp)
69af03a2 188{
a185be70 189 // special h handling, Wipeout likes to change it by 1-6
321ca84d 190 static int vsync_cnt_ms_prev;
191 if ((unsigned int)(vsync_cnt - vsync_cnt_ms_prev) < 5*60)
192 h = (h + 7) & ~7;
193 vsync_cnt_ms_prev = vsync_cnt;
a185be70 194
ddc0a02a 195 if (w == psx_w && h == psx_h && bpp == psx_bpp)
76f7048e 196 return pl_vout_buf;
b60f2812 197
ddc0a02a 198 pl_vout_w = psx_w = w;
199 pl_vout_h = psx_h = h;
200 pl_vout_bpp = psx_bpp = bpp;
d352cde2 201
9e5ac38f 202 update_layer_size(pl_vout_w, pl_vout_h);
203
6469a8c4 204 pl_vout_buf = plat_gvideo_set_mode(&pl_vout_w, &pl_vout_h, &pl_vout_bpp);
ddc0a02a 205 if (pl_vout_buf == NULL && pl_rearmed_cbs.pl_vout_raw_flip == NULL)
206 fprintf(stderr, "failed to set mode %dx%d@%d\n",
207 psx_w, psx_h, psx_bpp);
a185be70 208
ddc0a02a 209 menu_notify_mode_change(pl_vout_w, pl_vout_h, pl_vout_bpp);
3c70c47b 210
76f7048e 211 return pl_vout_buf;
69af03a2 212}
213
6469a8c4 214// only used if raw flip is not defined
76f7048e 215static void *pl_vout_flip(void)
69af03a2 216{
a92f6af1 217 pl_rearmed_cbs.flip_cnt++;
8f892648 218
55b0eeea 219 if (pl_vout_buf != NULL)
6469a8c4 220 pl_print_hud(0);
72228559 221
69af03a2 222 // let's flip now
6469a8c4 223 pl_vout_buf = plat_gvideo_flip();
76f7048e 224 return pl_vout_buf;
b60f2812 225}
226
76f7048e 227static int pl_vout_open(void)
6d1a1ac2 228{
cefe86b7 229 struct timeval now;
6469a8c4 230 int h;
cefe86b7 231
321ca84d 232 // force mode update
ddc0a02a 233 h = psx_h;
234 psx_h--;
235 pl_vout_buf = pl_vout_set_mode(psx_w, h, psx_bpp);
cefe86b7 236
ab423939 237 plat_gvideo_open(is_pal);
76f7048e 238
cefe86b7 239 gettimeofday(&now, 0);
240 vsync_usec_time = now.tv_usec;
76f7048e 241 while (vsync_usec_time >= frame_interval)
242 vsync_usec_time -= frame_interval;
cefe86b7 243
6d1a1ac2 244 return 0;
245}
246
76f7048e 247static void pl_vout_close(void)
b60f2812 248{
6469a8c4 249 plat_gvideo_close();
b60f2812 250}
251
29a8c4f3 252void *pl_prepare_screenshot(int *w, int *h, int *bpp)
253{
6469a8c4 254 void *ret = plat_prepare_screenshot(w, h, bpp);
255 if (ret != NULL)
256 return ret;
257
76f7048e 258 *w = pl_vout_w;
259 *h = pl_vout_h;
260 *bpp = pl_vout_bpp;
29a8c4f3 261
76f7048e 262 return pl_vout_buf;
29a8c4f3 263}
264
1b0c5139 265#ifndef MAEMO
266static void update_analogs(void)
267{
268 int *nubp[2] = { in_a1, in_a2 };
269 int i, a, v, ret;
270
271 for (i = 0; i < 2; i++)
272 {
273 if (in_adev[i] < 0)
274 continue;
275
276 for (a = 0; a < 2; a++) {
277 nubp[i][a] = 127;
278
279 ret = in_update_analog(in_adev[i], in_adev_axis[i][a], &v);
280 if (ret == 0) {
281 v = v / (IN_ABS_RANGE / 128) + 127;
282 nubp[i][a] = v < 0 ? 0 : v;
283 }
284 }
285 }
286 //printf("%4d %4d %4d %4d\n", in_a1[0], in_a1[1], in_a2[0], in_a2[1]);
287}
288
15d46930 289static void update_input(void)
290{
291 int actions[IN_BINDTYPE_COUNT] = { 0, };
8f892648 292 unsigned int emu_act;
15d46930 293
294 in_update(actions);
4c08b9e7 295 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD)
1b0c5139 296 update_analogs();
8f892648 297 emu_act = actions[IN_BINDTYPE_EMU];
4c08b9e7 298 in_state_gun = (emu_act & SACTION_GUN_MASK) >> SACTION_GUN_TRIGGER;
299
300 emu_act &= ~SACTION_GUN_MASK;
8f892648 301 if (emu_act) {
302 int which = 0;
303 for (; !(emu_act & 1); emu_act >>= 1, which++)
304 ;
305 emu_act = which;
306 }
307 emu_set_action(emu_act);
308
799b0b87 309 in_keystate = actions[IN_BINDTYPE_PLAYER12];
447783f8 310#ifdef X11
4218954f 311 extern int x11_update_keys(unsigned int *action);
312 in_keystate |= x11_update_keys(&emu_act);
313 emu_set_action(emu_act);
447783f8 314#endif
15d46930 315}
1b0c5139 316#else /* MAEMO */
317static void update_input(void)
318{
319}
320#endif
15d46930 321
4c08b9e7 322void pl_update_gun(int *xn, int *xres, int *y, int *in)
323{
faf2b2aa 324 if (tsdev)
325 pl_gun_ts_update(tsdev, xn, y, in);
4c08b9e7 326
76f7048e 327 *xres = pl_vout_w;
328 *y = *y * pl_vout_h >> 10;
4c08b9e7 329}
330
bce6b056 331#define MAX_LAG_FRAMES 3
332
333#define tvdiff(tv, tv_old) \
334 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
bce6b056 335
72228559 336/* called on every vsync */
337void pl_frame_limit(void)
338{
bce6b056 339 static struct timeval tv_old, tv_expect;
2f546f9a 340 static int vsync_cnt_prev, drc_active_vsyncs;
bce6b056 341 struct timeval now;
cefe86b7 342 int diff, usadj;
bce6b056 343
344 vsync_cnt++;
72228559 345
15d46930 346 /* doing input here because the pad is polled
347 * thousands of times per frame for some reason */
348 update_input();
349
72228559 350 pcnt_end(PCNT_ALL);
bce6b056 351 gettimeofday(&now, 0);
72228559 352
bce6b056 353 if (now.tv_sec != tv_old.tv_sec) {
354 diff = tvdiff(now, tv_old);
a92f6af1 355 pl_rearmed_cbs.vsps_cur = 0.0f;
bce6b056 356 if (0 < diff && diff < 2000000)
a92f6af1 357 pl_rearmed_cbs.vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
7c0f51de 358 vsync_cnt_prev = vsync_cnt;
a92f6af1 359
360 if (g_opts & OPT_SHOWFPS)
361 pl_rearmed_cbs.flips_per_sec = pl_rearmed_cbs.flip_cnt;
362 pl_rearmed_cbs.flip_cnt = 0;
bd6267e6 363 if (g_opts & OPT_SHOWCPU)
a92f6af1 364 pl_rearmed_cbs.cpu_usage = get_cpu_ticks();
8f892648 365
366 if (hud_new_msg > 0) {
367 hud_new_msg--;
368 if (hud_new_msg == 0)
369 hud_msg[0] = 0;
370 }
a92f6af1 371 tv_old = now;
72228559 372 }
373#ifdef PCNT
374 static int ya_vsync_count;
375 if (++ya_vsync_count == PCNT_FRAMES) {
a92f6af1 376 pcnt_print(pl_rearmed_cbs.vsps_cur);
72228559 377 ya_vsync_count = 0;
378 }
379#endif
380
76f7048e 381 // tv_expect uses usec*1024 units instead of usecs for better accuracy
382 tv_expect.tv_usec += frame_interval1024;
383 if (tv_expect.tv_usec >= (1000000 << 10)) {
384 tv_expect.tv_usec -= (1000000 << 10);
385 tv_expect.tv_sec++;
386 }
387 diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 + (tv_expect.tv_usec >> 10) - now.tv_usec;
388
389 if (diff > MAX_LAG_FRAMES * frame_interval || diff < -MAX_LAG_FRAMES * frame_interval) {
390 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, frame_interval);
c89cd762 391 tv_expect = now;
392 diff = 0;
cefe86b7 393 // try to align with vsync
394 usadj = vsync_usec_time;
76f7048e 395 while (usadj < tv_expect.tv_usec - frame_interval)
396 usadj += frame_interval;
397 tv_expect.tv_usec = usadj << 10;
c89cd762 398 }
399
76f7048e 400 if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) {
c89cd762 401 // yay for working usleep on pandora!
76f7048e 402 //printf("usleep %d\n", diff - frame_interval / 2);
47f37583 403 usleep(diff - frame_interval);
c89cd762 404 }
405
e64dc4c5 406 if (pl_rearmed_cbs.frameskip) {
76f7048e 407 if (diff < -frame_interval)
e64dc4c5 408 pl_rearmed_cbs.fskip_advice = 1;
409 else if (diff >= 0)
410 pl_rearmed_cbs.fskip_advice = 0;
2f546f9a 411
412 // recompilation is not that fast and may cause frame skip on
413 // loading screens and such, resulting in flicker or glitches
414 if (new_dynarec_did_compile) {
415 if (drc_active_vsyncs < 32)
416 pl_rearmed_cbs.fskip_advice = 0;
417 drc_active_vsyncs++;
418 }
419 else
420 drc_active_vsyncs = 0;
421 new_dynarec_did_compile = 0;
bce6b056 422 }
72228559 423
424 pcnt_start(PCNT_ALL);
425}
426
ab423939 427void pl_timing_prepare(int is_pal_)
76f7048e 428{
429 pl_rearmed_cbs.fskip_advice = 0;
a92f6af1 430 pl_rearmed_cbs.flips_per_sec = 0;
431 pl_rearmed_cbs.cpu_usage = 0;
76f7048e 432
ab423939 433 is_pal = is_pal_;
76f7048e 434 frame_interval = is_pal ? 20000 : 16667;
435 frame_interval1024 = is_pal ? 20000*1024 : 17066667;
436
437 // used by P.E.Op.S. frameskip code
438 pl_rearmed_cbs.gpu_peops.fFrameRateHz = is_pal ? 50.0f : 59.94f;
439 pl_rearmed_cbs.gpu_peops.dwFrameRateTicks =
440 (100000*100 / (unsigned long)(pl_rearmed_cbs.gpu_peops.fFrameRateHz*100));
441}
442
69af03a2 443static void pl_text_out16_(int x, int y, const char *text)
b60f2812 444{
76f7048e 445 int i, l, len = strlen(text), w = pl_vout_w;
446 unsigned short *screen = (unsigned short *)pl_vout_buf + x + y * w;
69af03a2 447 unsigned short val = 0xffff;
448
449 for (i = 0; i < len; i++, screen += 8)
450 {
451 for (l = 0; l < 8; l++)
452 {
453 unsigned char fd = fontdata8x8[text[i] * 8 + l];
454 unsigned short *s = screen + l * w;
455 if (fd&0x80) s[0] = val;
456 if (fd&0x40) s[1] = val;
457 if (fd&0x20) s[2] = val;
458 if (fd&0x10) s[3] = val;
459 if (fd&0x08) s[4] = val;
460 if (fd&0x04) s[5] = val;
461 if (fd&0x02) s[6] = val;
462 if (fd&0x01) s[7] = val;
463 }
464 }
b60f2812 465}
466
69af03a2 467void pl_text_out16(int x, int y, const char *texto, ...)
b60f2812 468{
69af03a2 469 va_list args;
470 char buffer[256];
471
472 va_start(args, texto);
473 vsnprintf(buffer, sizeof(buffer), texto, args);
474 va_end(args);
475
476 pl_text_out16_(x, y, buffer);
b60f2812 477}
478
201c21e2 479static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
480{
481 *x = g_layer_x;
482 *y = g_layer_y;
483 *w = g_layer_w;
484 *h = g_layer_h;
485}
486
e64dc4c5 487struct rearmed_cbs pl_rearmed_cbs = {
201c21e2 488 pl_get_layer_pos,
76f7048e 489 pl_vout_open,
490 pl_vout_set_mode,
491 pl_vout_flip,
492 pl_vout_close,
201c21e2 493};
494
7c0f51de 495/* watchdog */
496static void *watchdog_thread(void *unused)
497{
498 int vsync_cnt_old = 0;
499 int seen_dead = 0;
500 int sleep_time = 5;
501
d3f3bf09 502#if !defined(NDEBUG) || defined(DRC_DBG)
799b0b87 503 // don't interfere with debug
504 return NULL;
505#endif
7c0f51de 506 while (1)
507 {
508 sleep(sleep_time);
509
510 if (stop) {
511 seen_dead = 0;
512 sleep_time = 5;
513 continue;
514 }
515 if (vsync_cnt != vsync_cnt_old) {
516 vsync_cnt_old = vsync_cnt;
517 seen_dead = 0;
518 sleep_time = 2;
519 continue;
520 }
521
522 seen_dead++;
523 sleep_time = 1;
524 if (seen_dead > 1)
525 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
526 if (seen_dead > 4) {
527 fprintf(stderr, "watchdog: lockup detected, aborting\n");
528 // we can't do any cleanup here really, the main thread is
529 // likely touching resources and would crash anyway
530 abort();
531 }
532 }
533}
534
535void pl_start_watchdog(void)
536{
537 pthread_attr_t attr;
538 pthread_t tid;
539 int ret;
540
541 pthread_attr_init(&attr);
542 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
543
544 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
545 if (ret != 0)
546 fprintf(stderr, "could not start watchdog: %d\n", ret);
547}
548
4c08b9e7 549void pl_init(void)
550{
24de2dd4 551 extern unsigned int hSyncCount; // from psxcounters
3ece2f0c 552 extern unsigned int frame_counter;
24de2dd4 553
ddc0a02a 554 psx_w = psx_h = pl_vout_w = pl_vout_h = 256;
555 psx_bpp = pl_vout_bpp = 16;
321ca84d 556
faf2b2aa 557 tsdev = pl_gun_ts_init();
24de2dd4 558
559 pl_rearmed_cbs.gpu_hcnt = &hSyncCount;
3ece2f0c 560 pl_rearmed_cbs.gpu_frame_count = &frame_counter;
4c08b9e7 561}