4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
14 #include <sys/types.h>
20 #include "libpicofe/fonts.h"
21 #include "libpicofe/input.h"
22 #include "libpicofe/plat.h"
23 #include "libpicofe/arm/neon_scale2x.h"
24 #include "libpicofe/arm/neon_eagle2x.h"
25 #include "plugin_lib.h"
30 #include "pl_gun_ts.h"
31 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
32 #include "../libpcsxcore/psemu_plugin_defs.h"
33 #include "../plugins/gpulib/cspace.h"
35 int in_type1, in_type2;
36 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
37 int in_adev[2] = { -1, -1 }, in_adev_axis[2][2] = {{ 0, 1 }, { 0, 1 }};
38 int in_keystate, in_state_gun;
39 int in_enable_vibration;
42 int g_layer_x, g_layer_y, g_layer_w, g_layer_h;
43 static int pl_vout_w, pl_vout_h, pl_vout_bpp; /* output display/layer */
44 static int pl_vout_scale;
45 static int psx_w, psx_h, psx_bpp;
47 static int is_pal, frame_interval, frame_interval1024;
48 static int vsync_usec_time;
51 static __attribute__((noinline)) int get_cpu_ticks(void)
53 static unsigned long last_utime;
55 unsigned long utime, ret;
59 fd = open("/proc/self/stat", O_RDONLY);
60 lseek(fd, 0, SEEK_SET);
62 read(fd, buf, sizeof(buf));
63 buf[sizeof(buf) - 1] = 0;
65 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
66 ret = utime - last_utime;
71 static void print_msg(int h, int border)
73 if (pl_vout_bpp == 16)
74 pl_text_out16(border + 2, h - 10, "%s", hud_msg);
77 static void print_fps(int h, int border)
79 if (pl_vout_bpp == 16)
80 pl_text_out16(border + 2, h - 10, "%2d %4.1f",
81 pl_rearmed_cbs.flips_per_sec, pl_rearmed_cbs.vsps_cur);
84 static void print_cpu_usage(int w, int h, int border)
86 if (pl_vout_bpp == 16)
87 pl_text_out16(w - border - 28, h - 10, "%3d", pl_rearmed_cbs.cpu_usage);
90 // draw 192x8 status of 24 sound channels
91 static __attribute__((noinline)) void draw_active_chans(int vout_w, int vout_h)
93 extern void spu_get_debug_info(int *chans_out, int *run_chans,
94 int *fmod_chans_out, int *noise_chans_out); // hack
95 int live_chans, run_chans, fmod_chans, noise_chans;
97 static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
98 unsigned short *dest = (unsigned short *)pl_vout_buf +
99 vout_w * (vout_h - 10) + vout_w / 2 - 192/2;
100 unsigned short *d, p;
103 if (pl_vout_bpp != 16)
106 spu_get_debug_info(&live_chans, &run_chans, &fmod_chans, &noise_chans);
108 for (c = 0; c < 24; c++) {
110 p = !(live_chans & (1<<c)) ? (run_chans & (1<<c) ? 0x01c0 : 0) :
111 (fmod_chans & (1<<c)) ? 0xf000 :
112 (noise_chans & (1<<c)) ? 0x001f :
114 for (y = 0; y < 8; y++, d += vout_w)
115 for (x = 0; x < 8; x++)
120 void pl_print_hud(int w, int h, int xborder)
125 if (g_opts & OPT_SHOWSPU)
126 draw_active_chans(w, h);
129 print_msg(h, xborder);
130 else if (g_opts & OPT_SHOWFPS)
131 print_fps(h, xborder);
133 if (g_opts & OPT_SHOWCPU)
134 print_cpu_usage(w, h, xborder);
137 /* update scaler target size according to user settings */
138 static void update_layer_size(int w, int h)
145 g_layer_w = w; g_layer_h = h;
149 if (h > g_menuscreen_h || (240 < h && h <= 360))
152 // 4:3 that prefers integer scaling
153 imult = g_menuscreen_h / h;
154 g_layer_w = w * imult;
155 g_layer_h = h * imult;
156 mult = (float)g_layer_w / (float)g_layer_h;
157 if (mult < 1.25f || mult > 1.666f)
158 g_layer_w = 4.0f/3.0f * (float)g_layer_h;
159 printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult);
164 mult = 240.0f / (float)h * 4.0f / 3.0f;
167 g_layer_w = mult * (float)g_menuscreen_h;
168 g_layer_h = g_menuscreen_h;
169 printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult);
172 case SCALE_FULLSCREEN:
173 g_layer_w = g_menuscreen_w;
174 g_layer_h = g_menuscreen_h;
181 g_layer_x = g_menuscreen_w / 2 - g_layer_w / 2;
182 g_layer_y = g_menuscreen_h / 2 - g_layer_h / 2;
183 if (g_layer_x < 0) g_layer_x = 0;
184 if (g_layer_y < 0) g_layer_y = 0;
185 if (g_layer_w > g_menuscreen_w) g_layer_w = g_menuscreen_w;
186 if (g_layer_h > g_menuscreen_h) g_layer_h = g_menuscreen_h;
189 // XXX: this is platform specific really
190 static int resolution_ok(int w, int h)
192 return w <= 1024 && h <= 512;
195 static void pl_vout_set_mode(int w, int h, int bpp)
197 int vout_w, vout_h, vout_bpp;
199 // special h handling, Wipeout likes to change it by 1-6
200 static int vsync_cnt_ms_prev;
201 if ((unsigned int)(vsync_cnt - vsync_cnt_ms_prev) < 5*60)
203 vsync_cnt_ms_prev = vsync_cnt;
207 vout_bpp = psx_bpp = bpp;
212 if (resolution_ok(w * 2, h * 2) && bpp == 16) {
218 // filter unavailable
224 if (pl_vout_buf != NULL && vout_w == pl_vout_w && vout_h == pl_vout_h
225 && vout_bpp == pl_vout_bpp)
228 update_layer_size(vout_w, vout_h);
230 pl_vout_buf = plat_gvideo_set_mode(&vout_w, &vout_h, &vout_bpp);
231 if (pl_vout_buf == NULL)
232 fprintf(stderr, "failed to set mode %dx%d@%d\n",
233 psx_w, psx_h, psx_bpp);
237 pl_vout_bpp = vout_bpp;
240 menu_notify_mode_change(pl_vout_w, pl_vout_h, pl_vout_bpp);
243 static void pl_vout_flip(const void *vram, int stride, int bgr24, int w, int h)
245 static int doffs_old, clear_counter;
246 unsigned char *dest = pl_vout_buf;
247 const unsigned short *src = vram;
248 int dstride = pl_vout_w, h1 = h;
251 pcnt_start(PCNT_BLIT);
258 memset(pl_vout_buf, 0, dstride * pl_vout_h * pl_vout_bpp / 8);
263 doffs = (dstride - w * pl_vout_scale) / 2 & ~1;
266 if (doffs > doffs_old)
270 if (clear_counter > 0) {
271 memset(pl_vout_buf, 0, dstride * pl_vout_h * pl_vout_bpp / 8);
277 if (pl_rearmed_cbs.only_16bpp) {
278 for (; h1-- > 0; dest += dstride * 2, src += stride)
280 bgr888_to_rgb565(dest, src, w * 3);
285 dest += (doffs / 8) * 24;
287 for (; h1-- > 0; dest += dstride * 3, src += stride)
289 bgr888_to_rgb888(dest, src, w * 3);
294 else if (soft_filter == SOFT_FILTER_SCALE2X && pl_vout_scale == 2)
296 neon_scale2x_16_16(src, (void *)dest, w,
297 stride * 2, dstride * 2, h1);
299 else if (soft_filter == SOFT_FILTER_EAGLE2X && pl_vout_scale == 2)
301 neon_eagle2x_16_16(src, (void *)dest, w,
302 stride * 2, dstride * 2, h1);
307 for (; h1-- > 0; dest += dstride * 2, src += stride)
309 bgr555_to_rgb565(dest, src, w * 2);
313 pl_print_hud(w * pl_vout_scale, h * pl_vout_scale, 0);
319 pl_vout_buf = plat_gvideo_flip();
320 pl_rearmed_cbs.flip_cnt++;
323 static int pl_vout_open(void)
327 // force mode update on pl_vout_set_mode() call from gpulib/vout_pl
330 plat_gvideo_open(is_pal);
332 gettimeofday(&now, 0);
333 vsync_usec_time = now.tv_usec;
334 while (vsync_usec_time >= frame_interval)
335 vsync_usec_time -= frame_interval;
340 static void pl_vout_close(void)
345 static void pl_set_gpu_caps(int caps)
347 pl_rearmed_cbs.gpu_caps = caps;
350 void *pl_prepare_screenshot(int *w, int *h, int *bpp)
352 void *ret = plat_prepare_screenshot(w, h, bpp);
363 /* display/redering mode switcher */
364 static int dispmode_default(void)
366 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
367 soft_filter = SOFT_FILTER_NONE;
368 snprintf(hud_msg, sizeof(hud_msg), "default mode");
372 int dispmode_doubleres(void)
374 if (!(pl_rearmed_cbs.gpu_caps & GPU_CAP_SUPPORTS_2X)
375 || !resolution_ok(psx_w * 2, psx_h * 2) || psx_bpp != 16)
379 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
380 snprintf(hud_msg, sizeof(hud_msg), "double resolution");
384 int dispmode_scale2x(void)
390 soft_filter = SOFT_FILTER_SCALE2X;
391 snprintf(hud_msg, sizeof(hud_msg), "scale2x");
395 int dispmode_eagle2x(void)
401 soft_filter = SOFT_FILTER_EAGLE2X;
402 snprintf(hud_msg, sizeof(hud_msg), "eagle2x");
406 static int (*dispmode_switchers[])(void) = {
415 static int dispmode_current;
417 void pl_switch_dispmode(void)
419 if (pl_rearmed_cbs.gpu_caps & GPU_CAP_OWNS_DISPLAY)
424 if (dispmode_current >=
425 sizeof(dispmode_switchers) / sizeof(dispmode_switchers[0]))
426 dispmode_current = 0;
427 if (dispmode_switchers[dispmode_current]())
433 static void update_analogs(void)
435 int *nubp[2] = { in_a1, in_a2 };
438 for (i = 0; i < 2; i++)
443 for (a = 0; a < 2; a++) {
446 ret = in_update_analog(in_adev[i], in_adev_axis[i][a], &v);
448 v = v / (IN_ABS_RANGE / 128) + 127;
449 nubp[i][a] = v < 0 ? 0 : v;
453 //printf("%4d %4d %4d %4d\n", in_a1[0], in_a1[1], in_a2[0], in_a2[1]);
456 static void update_input(void)
458 int actions[IN_BINDTYPE_COUNT] = { 0, };
459 unsigned int emu_act;
462 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD)
464 emu_act = actions[IN_BINDTYPE_EMU];
465 in_state_gun = (emu_act & SACTION_GUN_MASK) >> SACTION_GUN_TRIGGER;
467 emu_act &= ~SACTION_GUN_MASK;
470 for (; !(emu_act & 1); emu_act >>= 1, which++)
474 emu_set_action(emu_act);
476 in_keystate = actions[IN_BINDTYPE_PLAYER12];
478 extern int x11_update_keys(unsigned int *action);
479 in_keystate |= x11_update_keys(&emu_act);
480 emu_set_action(emu_act);
484 static void update_input(void)
489 void pl_update_gun(int *xn, int *xres, int *y, int *in)
492 pl_gun_ts_update(tsdev, xn, y, in);
495 *y = *y * pl_vout_h >> 10;
498 #define MAX_LAG_FRAMES 3
500 #define tvdiff(tv, tv_old) \
501 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
503 /* called on every vsync */
504 void pl_frame_limit(void)
506 static struct timeval tv_old, tv_expect;
507 static int vsync_cnt_prev, drc_active_vsyncs;
513 /* doing input here because the pad is polled
514 * thousands of times per frame for some reason */
518 gettimeofday(&now, 0);
520 if (now.tv_sec != tv_old.tv_sec) {
521 diff = tvdiff(now, tv_old);
522 pl_rearmed_cbs.vsps_cur = 0.0f;
523 if (0 < diff && diff < 2000000)
524 pl_rearmed_cbs.vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
525 vsync_cnt_prev = vsync_cnt;
527 if (g_opts & OPT_SHOWFPS)
528 pl_rearmed_cbs.flips_per_sec = pl_rearmed_cbs.flip_cnt;
529 pl_rearmed_cbs.flip_cnt = 0;
530 if (g_opts & OPT_SHOWCPU)
531 pl_rearmed_cbs.cpu_usage = get_cpu_ticks();
533 if (hud_new_msg > 0) {
535 if (hud_new_msg == 0)
541 static int ya_vsync_count;
542 if (++ya_vsync_count == PCNT_FRAMES) {
543 pcnt_print(pl_rearmed_cbs.vsps_cur);
548 // tv_expect uses usec*1024 units instead of usecs for better accuracy
549 tv_expect.tv_usec += frame_interval1024;
550 if (tv_expect.tv_usec >= (1000000 << 10)) {
551 tv_expect.tv_usec -= (1000000 << 10);
554 diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 + (tv_expect.tv_usec >> 10) - now.tv_usec;
556 if (diff > MAX_LAG_FRAMES * frame_interval || diff < -MAX_LAG_FRAMES * frame_interval) {
557 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, frame_interval);
560 // try to align with vsync
561 usadj = vsync_usec_time;
562 while (usadj < tv_expect.tv_usec - frame_interval)
563 usadj += frame_interval;
564 tv_expect.tv_usec = usadj << 10;
567 if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) {
568 // yay for working usleep on pandora!
569 //printf("usleep %d\n", diff - frame_interval / 2);
570 usleep(diff - frame_interval);
573 if (pl_rearmed_cbs.frameskip) {
574 if (diff < -frame_interval)
575 pl_rearmed_cbs.fskip_advice = 1;
577 pl_rearmed_cbs.fskip_advice = 0;
579 // recompilation is not that fast and may cause frame skip on
580 // loading screens and such, resulting in flicker or glitches
581 if (new_dynarec_did_compile) {
582 if (drc_active_vsyncs < 32)
583 pl_rearmed_cbs.fskip_advice = 0;
587 drc_active_vsyncs = 0;
588 new_dynarec_did_compile = 0;
591 pcnt_start(PCNT_ALL);
594 void pl_timing_prepare(int is_pal_)
596 pl_rearmed_cbs.fskip_advice = 0;
597 pl_rearmed_cbs.flips_per_sec = 0;
598 pl_rearmed_cbs.cpu_usage = 0;
601 frame_interval = is_pal ? 20000 : 16667;
602 frame_interval1024 = is_pal ? 20000*1024 : 17066667;
604 // used by P.E.Op.S. frameskip code
605 pl_rearmed_cbs.gpu_peops.fFrameRateHz = is_pal ? 50.0f : 59.94f;
606 pl_rearmed_cbs.gpu_peops.dwFrameRateTicks =
607 (100000*100 / (unsigned long)(pl_rearmed_cbs.gpu_peops.fFrameRateHz*100));
610 static void pl_text_out16_(int x, int y, const char *text)
612 int i, l, w = pl_vout_w;
613 unsigned short *screen;
614 unsigned short val = 0xffff;
617 screen = (unsigned short *)pl_vout_buf + x + y * w;
618 for (i = 0; ; i++, screen += 8)
626 for (l = 0; l < 8; l++)
628 unsigned char fd = fontdata8x8[c * 8 + l];
629 unsigned short *s = screen + l * w;
630 unsigned int *s32 = (void *)s;
632 s32[0] = (s32[0] >> 1) & 0x7bef7bef;
633 s32[1] = (s32[1] >> 1) & 0x7bef7bef;
634 s32[2] = (s32[2] >> 1) & 0x7bef7bef;
635 s32[3] = (s32[3] >> 1) & 0x7bef7bef;
637 if (fd&0x80) s[0] = val;
638 if (fd&0x40) s[1] = val;
639 if (fd&0x20) s[2] = val;
640 if (fd&0x10) s[3] = val;
641 if (fd&0x08) s[4] = val;
642 if (fd&0x04) s[5] = val;
643 if (fd&0x02) s[6] = val;
644 if (fd&0x01) s[7] = val;
649 void pl_text_out16(int x, int y, const char *texto, ...)
654 va_start(args, texto);
655 vsnprintf(buffer, sizeof(buffer), texto, args);
658 pl_text_out16_(x, y, buffer);
661 static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
669 static void *pl_mmap(unsigned int size)
671 return plat_mmap(0, size, 0, 0);
674 static void pl_munmap(void *ptr, unsigned int size)
676 plat_munmap(ptr, size);
679 struct rearmed_cbs pl_rearmed_cbs = {
688 .pl_set_gpu_caps = pl_set_gpu_caps,
692 static void *watchdog_thread(void *unused)
694 int vsync_cnt_old = 0;
698 #if !defined(NDEBUG) || defined(DRC_DBG)
699 // don't interfere with debug
711 if (vsync_cnt != vsync_cnt_old) {
712 vsync_cnt_old = vsync_cnt;
721 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
723 fprintf(stderr, "watchdog: lockup detected, aborting\n");
724 // we can't do any cleanup here really, the main thread is
725 // likely touching resources and would crash anyway
731 void pl_start_watchdog(void)
737 pthread_attr_init(&attr);
738 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
740 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
742 fprintf(stderr, "could not start watchdog: %d\n", ret);
747 extern unsigned int hSyncCount; // from psxcounters
748 extern unsigned int frame_counter;
750 psx_w = psx_h = pl_vout_w = pl_vout_h = 256;
751 psx_bpp = pl_vout_bpp = 16;
753 tsdev = pl_gun_ts_init();
755 pl_rearmed_cbs.gpu_hcnt = &hSyncCount;
756 pl_rearmed_cbs.gpu_frame_count = &frame_counter;