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"
29 #include "pl_gun_ts.h"
30 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
31 #include "../libpcsxcore/psemu_plugin_defs.h"
33 int in_type1, in_type2;
34 int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
35 int in_adev[2] = { -1, -1 }, in_adev_axis[2][2] = {{ 0, 1 }, { 0, 1 }};
36 int in_keystate, in_state_gun;
37 int in_enable_vibration;
40 static int pl_vout_w, pl_vout_h, pl_vout_bpp;
42 static int frame_interval, frame_interval1024, vsync_usec_time;
45 static __attribute__((noinline)) int get_cpu_ticks(void)
47 static unsigned long last_utime;
49 unsigned long utime, ret;
53 fd = open("/proc/self/stat", O_RDONLY);
54 lseek(fd, 0, SEEK_SET);
56 read(fd, buf, sizeof(buf));
57 buf[sizeof(buf) - 1] = 0;
59 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
60 ret = utime - last_utime;
65 static void print_msg(int h, int border)
67 if (pl_vout_bpp == 16)
68 pl_text_out16(border + 2, h - 10, "%s", hud_msg);
71 static void print_fps(int h, int border)
73 if (pl_vout_bpp == 16)
74 pl_text_out16(border + 2, h - 10, "%2d %4.1f",
75 pl_rearmed_cbs.flips_per_sec, pl_rearmed_cbs.vsps_cur);
78 static void print_cpu_usage(int w, int h, int border)
80 if (pl_vout_bpp == 16)
81 pl_text_out16(w - border - 28, h - 10, "%3d", pl_rearmed_cbs.cpu_usage);
84 // draw 192x8 status of 24 sound channels
85 static __attribute__((noinline)) void draw_active_chans(int vout_w, int vout_h)
87 extern void spu_get_debug_info(int *chans_out, int *run_chans,
88 int *fmod_chans_out, int *noise_chans_out); // hack
89 int live_chans, run_chans, fmod_chans, noise_chans;
91 static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
92 unsigned short *dest = (unsigned short *)pl_vout_buf +
93 vout_w * (vout_h - 10) + vout_w / 2 - 192/2;
97 if (pl_vout_bpp != 16)
100 spu_get_debug_info(&live_chans, &run_chans, &fmod_chans, &noise_chans);
102 for (c = 0; c < 24; c++) {
104 p = !(live_chans & (1<<c)) ? (run_chans & (1<<c) ? 0x01c0 : 0) :
105 (fmod_chans & (1<<c)) ? 0xf000 :
106 (noise_chans & (1<<c)) ? 0x001f :
108 for (y = 0; y < 8; y++, d += vout_w)
109 for (x = 0; x < 8; x++)
114 void pl_print_hud(int w, int h, int xborder)
116 pl_vout_w = w; // used by pollux
119 if (g_opts & OPT_SHOWSPU)
120 draw_active_chans(w, h);
123 print_msg(h, xborder);
124 else if (g_opts & OPT_SHOWFPS)
125 print_fps(h, xborder);
127 if (g_opts & OPT_SHOWCPU)
128 print_cpu_usage(w, h, xborder);
131 static void *pl_vout_set_mode(int w, int h, int bpp)
133 // special h handling, Wipeout likes to change it by 1-6
134 static int vsync_cnt_ms_prev;
135 if ((unsigned int)(vsync_cnt - vsync_cnt_ms_prev) < 5*60)
137 vsync_cnt_ms_prev = vsync_cnt;
139 if (w == pl_vout_w && h == pl_vout_h && bpp == pl_vout_bpp)
146 #if defined(VOUT_FBDEV)
147 vout_fbdev_clear(layer_fb);
148 pl_vout_buf = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
150 extern void *hildon_set_mode(int w, int h);
151 pl_vout_buf = hildon_set_mode(w, h);
154 if (pl_vout_buf == NULL)
155 fprintf(stderr, "failed to set mode\n");
157 // menu decides on layer size, we commit it
158 menu_notify_mode_change(w, h, bpp);
159 omap_enable_layer(1);
164 static void *pl_vout_flip(void)
166 pl_rearmed_cbs.flip_cnt++;
168 if (pl_vout_buf != NULL)
169 pl_print_hud(pl_vout_w, pl_vout_h, 0);
172 #if defined(VOUT_FBDEV)
173 pl_vout_buf = vout_fbdev_flip(layer_fb);
175 extern void *hildon_flip(void);
176 pl_vout_buf = hildon_flip();
181 static int pl_vout_open(void)
185 omap_enable_layer(1);
186 #if defined(VOUT_FBDEV)
190 pl_vout_buf = pl_vout_set_mode(pl_vout_w, h, pl_vout_bpp);
192 // try to align redraws to vsync
193 vout_fbdev_wait_vsync(layer_fb);
195 extern void *hildon_flip(void);
196 pl_vout_buf = hildon_flip();
199 gettimeofday(&now, 0);
200 vsync_usec_time = now.tv_usec;
201 while (vsync_usec_time >= frame_interval)
202 vsync_usec_time -= frame_interval;
207 static void pl_vout_close(void)
209 omap_enable_layer(0);
212 void *pl_prepare_screenshot(int *w, int *h, int *bpp)
214 #if defined(VOUT_FBDEV)
221 return plat_prepare_screenshot(w, h, bpp);
226 static void update_analogs(void)
228 int *nubp[2] = { in_a1, in_a2 };
231 for (i = 0; i < 2; i++)
236 for (a = 0; a < 2; a++) {
239 ret = in_update_analog(in_adev[i], in_adev_axis[i][a], &v);
241 v = v / (IN_ABS_RANGE / 128) + 127;
242 nubp[i][a] = v < 0 ? 0 : v;
246 //printf("%4d %4d %4d %4d\n", in_a1[0], in_a1[1], in_a2[0], in_a2[1]);
249 static void update_input(void)
251 int actions[IN_BINDTYPE_COUNT] = { 0, };
252 unsigned int emu_act;
255 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD)
257 emu_act = actions[IN_BINDTYPE_EMU];
258 in_state_gun = (emu_act & SACTION_GUN_MASK) >> SACTION_GUN_TRIGGER;
260 emu_act &= ~SACTION_GUN_MASK;
263 for (; !(emu_act & 1); emu_act >>= 1, which++)
267 emu_set_action(emu_act);
269 in_keystate = actions[IN_BINDTYPE_PLAYER12];
271 extern int x11_update_keys(unsigned int *action);
272 in_keystate |= x11_update_keys(&emu_act);
273 emu_set_action(emu_act);
277 static void update_input(void)
282 void pl_update_gun(int *xn, int *xres, int *y, int *in)
285 pl_gun_ts_update(tsdev, xn, y, in);
288 *y = *y * pl_vout_h >> 10;
291 #define MAX_LAG_FRAMES 3
293 #define tvdiff(tv, tv_old) \
294 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
296 /* called on every vsync */
297 void pl_frame_limit(void)
299 static struct timeval tv_old, tv_expect;
300 static int vsync_cnt_prev, drc_active_vsyncs;
306 /* doing input here because the pad is polled
307 * thousands of times per frame for some reason */
311 gettimeofday(&now, 0);
313 if (now.tv_sec != tv_old.tv_sec) {
314 diff = tvdiff(now, tv_old);
315 pl_rearmed_cbs.vsps_cur = 0.0f;
316 if (0 < diff && diff < 2000000)
317 pl_rearmed_cbs.vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
318 vsync_cnt_prev = vsync_cnt;
320 if (g_opts & OPT_SHOWFPS)
321 pl_rearmed_cbs.flips_per_sec = pl_rearmed_cbs.flip_cnt;
322 pl_rearmed_cbs.flip_cnt = 0;
323 if (g_opts & OPT_SHOWCPU)
324 pl_rearmed_cbs.cpu_usage = get_cpu_ticks();
326 if (hud_new_msg > 0) {
328 if (hud_new_msg == 0)
334 static int ya_vsync_count;
335 if (++ya_vsync_count == PCNT_FRAMES) {
336 pcnt_print(pl_rearmed_cbs.vsps_cur);
341 // tv_expect uses usec*1024 units instead of usecs for better accuracy
342 tv_expect.tv_usec += frame_interval1024;
343 if (tv_expect.tv_usec >= (1000000 << 10)) {
344 tv_expect.tv_usec -= (1000000 << 10);
347 diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 + (tv_expect.tv_usec >> 10) - now.tv_usec;
349 if (diff > MAX_LAG_FRAMES * frame_interval || diff < -MAX_LAG_FRAMES * frame_interval) {
350 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, frame_interval);
353 // try to align with vsync
354 usadj = vsync_usec_time;
355 while (usadj < tv_expect.tv_usec - frame_interval)
356 usadj += frame_interval;
357 tv_expect.tv_usec = usadj << 10;
360 if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) {
361 // yay for working usleep on pandora!
362 //printf("usleep %d\n", diff - frame_interval / 2);
363 usleep(diff - frame_interval);
366 if (pl_rearmed_cbs.frameskip) {
367 if (diff < -frame_interval)
368 pl_rearmed_cbs.fskip_advice = 1;
370 pl_rearmed_cbs.fskip_advice = 0;
372 // recompilation is not that fast and may cause frame skip on
373 // loading screens and such, resulting in flicker or glitches
374 if (new_dynarec_did_compile) {
375 if (drc_active_vsyncs < 32)
376 pl_rearmed_cbs.fskip_advice = 0;
380 drc_active_vsyncs = 0;
381 new_dynarec_did_compile = 0;
384 pcnt_start(PCNT_ALL);
387 void pl_timing_prepare(int is_pal)
389 pl_rearmed_cbs.fskip_advice = 0;
390 pl_rearmed_cbs.flips_per_sec = 0;
391 pl_rearmed_cbs.cpu_usage = 0;
393 frame_interval = is_pal ? 20000 : 16667;
394 frame_interval1024 = is_pal ? 20000*1024 : 17066667;
396 // used by P.E.Op.S. frameskip code
397 pl_rearmed_cbs.gpu_peops.fFrameRateHz = is_pal ? 50.0f : 59.94f;
398 pl_rearmed_cbs.gpu_peops.dwFrameRateTicks =
399 (100000*100 / (unsigned long)(pl_rearmed_cbs.gpu_peops.fFrameRateHz*100));
402 static void pl_text_out16_(int x, int y, const char *text)
404 int i, l, len = strlen(text), w = pl_vout_w;
405 unsigned short *screen = (unsigned short *)pl_vout_buf + x + y * w;
406 unsigned short val = 0xffff;
408 for (i = 0; i < len; i++, screen += 8)
410 for (l = 0; l < 8; l++)
412 unsigned char fd = fontdata8x8[text[i] * 8 + l];
413 unsigned short *s = screen + l * w;
414 if (fd&0x80) s[0] = val;
415 if (fd&0x40) s[1] = val;
416 if (fd&0x20) s[2] = val;
417 if (fd&0x10) s[3] = val;
418 if (fd&0x08) s[4] = val;
419 if (fd&0x04) s[5] = val;
420 if (fd&0x02) s[6] = val;
421 if (fd&0x01) s[7] = val;
426 void pl_text_out16(int x, int y, const char *texto, ...)
431 va_start(args, texto);
432 vsnprintf(buffer, sizeof(buffer), texto, args);
435 pl_text_out16_(x, y, buffer);
438 static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
446 struct rearmed_cbs pl_rearmed_cbs = {
455 static void *watchdog_thread(void *unused)
457 int vsync_cnt_old = 0;
461 #if !defined(NDEBUG) || defined(DRC_DBG)
462 // don't interfere with debug
474 if (vsync_cnt != vsync_cnt_old) {
475 vsync_cnt_old = vsync_cnt;
484 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
486 fprintf(stderr, "watchdog: lockup detected, aborting\n");
487 // we can't do any cleanup here really, the main thread is
488 // likely touching resources and would crash anyway
494 void pl_start_watchdog(void)
500 pthread_attr_init(&attr);
501 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
503 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
505 fprintf(stderr, "could not start watchdog: %d\n", ret);
510 extern unsigned int hSyncCount; // from psxcounters
511 extern unsigned int frame_counter;
513 pl_vout_w = pl_vout_h = 256;
516 tsdev = pl_gun_ts_init();
518 pl_rearmed_cbs.gpu_hcnt = &hSyncCount;
519 pl_rearmed_cbs.gpu_frame_count = &frame_counter;