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