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