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 "plugin_lib.h"
21 #include "linux/fbdev.h"
22 #include "common/fonts.h"
23 #include "common/input.h"
28 #include "pl_gun_ts.h"
29 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
30 #include "../libpcsxcore/psemu_plugin_defs.h"
32 int in_type1, in_type2;
33 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
34 int in_keystate, in_state_gun;
37 static int pl_vout_w, pl_vout_h, pl_vout_bpp;
38 static int flip_cnt, vsync_cnt, flips_per_sec, tick_per_sec;
39 static float vsps_cur;
40 static int frame_interval, frame_interval1024, vsync_usec_time;
43 static __attribute__((noinline)) int get_cpu_ticks(void)
45 static unsigned long last_utime;
47 unsigned long utime, ret;
51 fd = open("/proc/self/stat", O_RDONLY);
52 lseek(fd, 0, SEEK_SET);
54 read(fd, buf, sizeof(buf));
55 buf[sizeof(buf) - 1] = 0;
57 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
58 ret = utime - last_utime;
63 static void print_hud(void)
65 if (pl_vout_bpp == 16)
66 pl_text_out16(2, pl_vout_h - 10, "%s", hud_msg);
69 static void print_fps(void)
71 if (pl_vout_bpp == 16)
72 pl_text_out16(2, pl_vout_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
75 static void print_cpu_usage(void)
77 if (pl_vout_bpp == 16)
78 pl_text_out16(pl_vout_w - 28, pl_vout_h - 10, "%3d", tick_per_sec);
81 // draw 192x8 status of 24 sound channels
82 static __attribute__((noinline)) void draw_active_chans(void)
84 extern void spu_get_debug_info(int *chans_out, int *run_chans,
85 int *fmod_chans_out, int *noise_chans_out); // hack
86 int live_chans, run_chans, fmod_chans, noise_chans;
88 static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
89 unsigned short *dest = (unsigned short *)pl_vout_buf +
90 pl_vout_w * (pl_vout_h - 10) + pl_vout_w / 2 - 192/2;
94 if (pl_vout_bpp != 16)
97 spu_get_debug_info(&live_chans, &run_chans, &fmod_chans, &noise_chans);
99 for (c = 0; c < 24; c++) {
101 p = !(live_chans & (1<<c)) ? (run_chans & (1<<c) ? 0x01c0 : 0) :
102 (fmod_chans & (1<<c)) ? 0xf000 :
103 (noise_chans & (1<<c)) ? 0x001f :
105 for (y = 0; y < 8; y++, d += pl_vout_w)
106 for (x = 0; x < 8; x++)
111 static void *pl_vout_set_mode(int w, int h, int bpp)
113 // special h handling, Wipeout likes to change it by 1-6
114 static int vsync_cnt_ms_prev;
115 if ((unsigned int)(vsync_cnt - vsync_cnt_ms_prev) < 5*60)
117 vsync_cnt_ms_prev = vsync_cnt;
119 if (w == pl_vout_w && h == pl_vout_h && bpp == pl_vout_bpp)
126 #if defined(VOUT_FBDEV)
127 vout_fbdev_clear(layer_fb);
128 pl_vout_buf = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
130 extern void *hildon_set_mode(int w, int h);
131 pl_vout_buf = hildon_set_mode(w, h);
134 if (pl_vout_buf == NULL)
135 fprintf(stderr, "failed to set mode\n");
137 // menu decides on layer size, we commit it
138 menu_notify_mode_change(w, h, bpp);
139 omap_enable_layer(1);
144 static void *pl_vout_flip(void)
148 if (pl_vout_buf != NULL) {
149 if (g_opts & OPT_SHOWSPU)
154 else if (g_opts & OPT_SHOWFPS)
157 if (g_opts & OPT_SHOWCPU)
162 #if defined(VOUT_FBDEV)
163 pl_vout_buf = vout_fbdev_flip(layer_fb);
165 extern void *hildon_flip(void);
166 pl_vout_buf = hildon_flip();
171 static int pl_vout_open(void)
175 omap_enable_layer(1);
176 #if defined(VOUT_FBDEV)
180 pl_vout_buf = pl_vout_set_mode(pl_vout_w, h, pl_vout_bpp);
182 // try to align redraws to vsync
183 vout_fbdev_wait_vsync(layer_fb);
185 extern void *hildon_flip(void);
186 pl_vout_buf = hildon_flip();
189 gettimeofday(&now, 0);
190 vsync_usec_time = now.tv_usec;
191 while (vsync_usec_time >= frame_interval)
192 vsync_usec_time -= frame_interval;
197 static void pl_vout_close(void)
199 omap_enable_layer(0);
202 void *pl_prepare_screenshot(int *w, int *h, int *bpp)
211 static void update_input(void)
214 int actions[IN_BINDTYPE_COUNT] = { 0, };
215 unsigned int emu_act;
218 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD)
220 emu_act = actions[IN_BINDTYPE_EMU];
221 in_state_gun = (emu_act & SACTION_GUN_MASK) >> SACTION_GUN_TRIGGER;
223 emu_act &= ~SACTION_GUN_MASK;
226 for (; !(emu_act & 1); emu_act >>= 1, which++)
230 emu_set_action(emu_act);
232 in_keystate = actions[IN_BINDTYPE_PLAYER12];
235 extern int x11_update_keys(unsigned int *action);
236 in_keystate |= x11_update_keys(&emu_act);
237 emu_set_action(emu_act);
241 void pl_update_gun(int *xn, int *xres, int *y, int *in)
244 pl_gun_ts_update(ts, xn, y, in);
247 *y = *y * pl_vout_h >> 10;
250 #define MAX_LAG_FRAMES 3
252 #define tvdiff(tv, tv_old) \
253 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
255 /* called on every vsync */
256 void pl_frame_limit(void)
258 static struct timeval tv_old, tv_expect;
259 static int vsync_cnt_prev;
265 /* doing input here because the pad is polled
266 * thousands of times per frame for some reason */
270 gettimeofday(&now, 0);
272 if (now.tv_sec != tv_old.tv_sec) {
273 diff = tvdiff(now, tv_old);
275 if (0 < diff && diff < 2000000)
276 vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
277 vsync_cnt_prev = vsync_cnt;
278 flips_per_sec = flip_cnt;
281 if (g_opts & OPT_SHOWCPU)
282 tick_per_sec = get_cpu_ticks();
284 if (hud_new_msg > 0) {
286 if (hud_new_msg == 0)
291 static int ya_vsync_count;
292 if (++ya_vsync_count == PCNT_FRAMES) {
293 pcnt_print(vsps_cur);
298 // tv_expect uses usec*1024 units instead of usecs for better accuracy
299 tv_expect.tv_usec += frame_interval1024;
300 if (tv_expect.tv_usec >= (1000000 << 10)) {
301 tv_expect.tv_usec -= (1000000 << 10);
304 diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 + (tv_expect.tv_usec >> 10) - now.tv_usec;
306 if (diff > MAX_LAG_FRAMES * frame_interval || diff < -MAX_LAG_FRAMES * frame_interval) {
307 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, frame_interval);
310 // try to align with vsync
311 usadj = vsync_usec_time;
312 while (usadj < tv_expect.tv_usec - frame_interval)
313 usadj += frame_interval;
314 tv_expect.tv_usec = usadj << 10;
317 if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) {
318 // yay for working usleep on pandora!
319 //printf("usleep %d\n", diff - frame_interval / 2);
320 usleep(diff - frame_interval);
323 if (pl_rearmed_cbs.frameskip) {
324 if (diff < -frame_interval)
325 pl_rearmed_cbs.fskip_advice = 1;
327 pl_rearmed_cbs.fskip_advice = 0;
330 pcnt_start(PCNT_ALL);
333 void pl_timing_prepare(int is_pal)
335 pl_rearmed_cbs.fskip_advice = 0;
337 frame_interval = is_pal ? 20000 : 16667;
338 frame_interval1024 = is_pal ? 20000*1024 : 17066667;
340 // used by P.E.Op.S. frameskip code
341 pl_rearmed_cbs.gpu_peops.fFrameRateHz = is_pal ? 50.0f : 59.94f;
342 pl_rearmed_cbs.gpu_peops.dwFrameRateTicks =
343 (100000*100 / (unsigned long)(pl_rearmed_cbs.gpu_peops.fFrameRateHz*100));
346 static void pl_text_out16_(int x, int y, const char *text)
348 int i, l, len = strlen(text), w = pl_vout_w;
349 unsigned short *screen = (unsigned short *)pl_vout_buf + x + y * w;
350 unsigned short val = 0xffff;
352 for (i = 0; i < len; i++, screen += 8)
354 for (l = 0; l < 8; l++)
356 unsigned char fd = fontdata8x8[text[i] * 8 + l];
357 unsigned short *s = screen + l * w;
358 if (fd&0x80) s[0] = val;
359 if (fd&0x40) s[1] = val;
360 if (fd&0x20) s[2] = val;
361 if (fd&0x10) s[3] = val;
362 if (fd&0x08) s[4] = val;
363 if (fd&0x04) s[5] = val;
364 if (fd&0x02) s[6] = val;
365 if (fd&0x01) s[7] = val;
370 void pl_text_out16(int x, int y, const char *texto, ...)
375 va_start(args, texto);
376 vsnprintf(buffer, sizeof(buffer), texto, args);
379 pl_text_out16_(x, y, buffer);
382 static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
390 struct rearmed_cbs pl_rearmed_cbs = {
399 static void *watchdog_thread(void *unused)
401 int vsync_cnt_old = 0;
406 // don't interfere with debug
418 if (vsync_cnt != vsync_cnt_old) {
419 vsync_cnt_old = vsync_cnt;
428 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
430 fprintf(stderr, "watchdog: lockup detected, aborting\n");
431 // we can't do any cleanup here really, the main thread is
432 // likely touching resources and would crash anyway
438 void pl_start_watchdog(void)
444 pthread_attr_init(&attr);
445 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
447 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
449 fprintf(stderr, "could not start watchdog: %d\n", ret);
454 pl_vout_w = pl_vout_h = 256;
457 ts = pl_gun_ts_init();