frontend: update caanoo port
[pcsx_rearmed.git] / frontend / plugin_lib.c
... / ...
CommitLineData
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 "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"
26#include "menu.h"
27#include "main.h"
28#include "plat.h"
29#include "pcnt.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"
34#include "../plugins/dfinput/externals.h"
35
36int in_type1, in_type2;
37int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
38int in_adev[2] = { -1, -1 }, in_adev_axis[2][2] = {{ 0, 1 }, { 0, 1 }};
39int in_adev_is_nublike[2];
40int in_keystate, in_state_gun;
41int in_enable_vibration;
42void *tsdev;
43void *pl_vout_buf;
44int g_layer_x, g_layer_y, g_layer_w, g_layer_h;
45static int pl_vout_w, pl_vout_h, pl_vout_bpp; /* output display/layer */
46static int pl_vout_scale;
47static int psx_w, psx_h, psx_bpp;
48static int vsync_cnt;
49static int is_pal, frame_interval, frame_interval1024;
50static int vsync_usec_time;
51
52// platform hooks
53void (*pl_plat_clear)(void);
54void (*pl_plat_blit)(int doffs, const void *src, int w, int h,
55 int sstride, int bgr24);
56void (*pl_plat_hud_print)(int x, int y, const char *str, int bpp);
57
58
59static __attribute__((noinline)) int get_cpu_ticks(void)
60{
61 static unsigned long last_utime;
62 static int fd;
63 unsigned long utime, ret;
64 char buf[128];
65
66 if (fd == 0)
67 fd = open("/proc/self/stat", O_RDONLY);
68 lseek(fd, 0, SEEK_SET);
69 buf[0] = 0;
70 read(fd, buf, sizeof(buf));
71 buf[sizeof(buf) - 1] = 0;
72
73 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
74 ret = utime - last_utime;
75 if (ret > 200)
76 ret = 0;
77 last_utime = utime;
78 return ret;
79}
80
81static void hud_print(void *fb, int w, int x, int y, const char *text)
82{
83 if (pl_plat_hud_print)
84 pl_plat_hud_print(x, y, text, pl_vout_bpp);
85 else if (pl_vout_bpp == 16)
86 basic_text_out16_nf(fb, w, x, y, text);
87}
88
89static void hud_printf(void *fb, int w, int x, int y, const char *texto, ...)
90{
91 va_list args;
92 char buffer[256];
93
94 va_start(args, texto);
95 vsnprintf(buffer, sizeof(buffer), texto, args);
96 va_end(args);
97
98 hud_print(fb, w, x, y, buffer);
99}
100
101static void print_msg(int h, int border)
102{
103 hud_print(pl_vout_buf, pl_vout_w, border + 2, h - 10, hud_msg);
104}
105
106static void print_fps(int h, int border)
107{
108 hud_printf(pl_vout_buf, pl_vout_w, border + 2, h - 10,
109 "%2d %4.1f", pl_rearmed_cbs.flips_per_sec,
110 pl_rearmed_cbs.vsps_cur);
111}
112
113static void print_cpu_usage(int w, int h, int border)
114{
115 hud_printf(pl_vout_buf, pl_vout_w, pl_vout_w - border - 28, h - 10,
116 "%3d", pl_rearmed_cbs.cpu_usage);
117}
118
119// draw 192x8 status of 24 sound channels
120static __attribute__((noinline)) void draw_active_chans(int vout_w, int vout_h)
121{
122 extern void spu_get_debug_info(int *chans_out, int *run_chans,
123 int *fmod_chans_out, int *noise_chans_out); // hack
124 int live_chans, run_chans, fmod_chans, noise_chans;
125
126 static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
127 unsigned short *dest = (unsigned short *)pl_vout_buf +
128 vout_w * (vout_h - 10) + vout_w / 2 - 192/2;
129 unsigned short *d, p;
130 int c, x, y;
131
132 if (dest == NULL || pl_vout_bpp != 16)
133 return;
134
135 spu_get_debug_info(&live_chans, &run_chans, &fmod_chans, &noise_chans);
136
137 for (c = 0; c < 24; c++) {
138 d = dest + c * 8;
139 p = !(live_chans & (1<<c)) ? (run_chans & (1<<c) ? 0x01c0 : 0) :
140 (fmod_chans & (1<<c)) ? 0xf000 :
141 (noise_chans & (1<<c)) ? 0x001f :
142 colors[c & 1];
143 for (y = 0; y < 8; y++, d += vout_w)
144 for (x = 0; x < 8; x++)
145 d[x] = p;
146 }
147}
148
149static void print_hud(int w, int h, int xborder)
150{
151 if (h < 16)
152 return;
153
154 if (w < pl_vout_w)
155 xborder += (pl_vout_w - w) / 2;
156 if (h > pl_vout_h)
157 h = pl_vout_h;
158
159 if (g_opts & OPT_SHOWSPU)
160 draw_active_chans(w, h);
161
162 if (hud_msg[0] != 0)
163 print_msg(h, xborder);
164 else if (g_opts & OPT_SHOWFPS)
165 print_fps(h, xborder);
166
167 if (g_opts & OPT_SHOWCPU)
168 print_cpu_usage(w, h, xborder);
169}
170
171/* update scaler target size according to user settings */
172static void update_layer_size(int w, int h)
173{
174 float mult;
175 int imult;
176
177 switch (g_scaler) {
178 case SCALE_1_1:
179 g_layer_w = w; g_layer_h = h;
180 break;
181
182 case SCALE_4_3v2:
183 if (h > g_menuscreen_h || (240 < h && h <= 360))
184 goto fractional_4_3;
185
186 // 4:3 that prefers integer scaling
187 imult = g_menuscreen_h / h;
188 g_layer_w = w * imult;
189 g_layer_h = h * imult;
190 mult = (float)g_layer_w / (float)g_layer_h;
191 if (mult < 1.25f || mult > 1.666f)
192 g_layer_w = 4.0f/3.0f * (float)g_layer_h;
193 printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult);
194 break;
195
196 fractional_4_3:
197 case SCALE_4_3:
198 mult = 240.0f / (float)h * 4.0f / 3.0f;
199 if (h > 256)
200 mult *= 2.0f;
201 g_layer_w = mult * (float)g_menuscreen_h;
202 g_layer_h = g_menuscreen_h;
203 printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult);
204 break;
205
206 case SCALE_FULLSCREEN:
207 g_layer_w = g_menuscreen_w;
208 g_layer_h = g_menuscreen_h;
209 break;
210
211 default:
212 break;
213 }
214
215 g_layer_x = g_menuscreen_w / 2 - g_layer_w / 2;
216 g_layer_y = g_menuscreen_h / 2 - g_layer_h / 2;
217 if (g_layer_x < 0) g_layer_x = 0;
218 if (g_layer_y < 0) g_layer_y = 0;
219 if (g_layer_w > g_menuscreen_w) g_layer_w = g_menuscreen_w;
220 if (g_layer_h > g_menuscreen_h) g_layer_h = g_menuscreen_h;
221}
222
223// XXX: this is platform specific really
224static int resolution_ok(int w, int h)
225{
226 return w <= 1024 && h <= 512;
227}
228
229static void pl_vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
230{
231 int vout_w, vout_h, vout_bpp;
232
233 // special h handling, Wipeout likes to change it by 1-6
234 static int vsync_cnt_ms_prev;
235 if ((unsigned int)(vsync_cnt - vsync_cnt_ms_prev) < 5*60)
236 h = (h + 7) & ~7;
237 vsync_cnt_ms_prev = vsync_cnt;
238
239 psx_w = raw_w;
240 psx_h = raw_h;
241 vout_w = w;
242 vout_h = h;
243 vout_bpp = psx_bpp = bpp;
244
245 pl_vout_scale = 1;
246#ifdef __ARM_NEON__
247 if (soft_filter) {
248 if (resolution_ok(w * 2, h * 2) && bpp == 16) {
249 vout_w *= 2;
250 vout_h *= 2;
251 pl_vout_scale = 2;
252 }
253 else {
254 // filter unavailable
255 hud_msg[0] = 0;
256 }
257 }
258#endif
259
260 if (pl_vout_buf != NULL && vout_w == pl_vout_w && vout_h == pl_vout_h
261 && vout_bpp == pl_vout_bpp)
262 return;
263
264 update_layer_size(vout_w, vout_h);
265
266 pl_vout_buf = plat_gvideo_set_mode(&vout_w, &vout_h, &vout_bpp);
267 if (pl_vout_buf == NULL && pl_plat_blit == NULL)
268 fprintf(stderr, "failed to set mode %dx%d@%d\n",
269 vout_w, vout_h, psx_bpp);
270 else {
271 pl_vout_w = vout_w;
272 pl_vout_h = vout_h;
273 pl_vout_bpp = vout_bpp;
274 }
275
276 menu_notify_mode_change(pl_vout_w, pl_vout_h, pl_vout_bpp);
277}
278
279static void pl_vout_flip(const void *vram, int stride, int bgr24, int w, int h)
280{
281 static int doffs_old, clear_counter;
282 unsigned char *dest = pl_vout_buf;
283 const unsigned short *src = vram;
284 int dstride = pl_vout_w, h1 = h;
285 int doffs;
286
287 pcnt_start(PCNT_BLIT);
288
289 if (vram == NULL) {
290 // blanking
291 if (pl_plat_clear)
292 pl_plat_clear();
293 else
294 memset(pl_vout_buf, 0,
295 dstride * pl_vout_h * pl_vout_bpp / 8);
296 goto out_hud;
297 }
298
299 // borders
300 doffs = (dstride - w * pl_vout_scale) / 2 & ~1;
301
302 if (doffs > doffs_old)
303 clear_counter = 2;
304 doffs_old = doffs;
305
306 if (clear_counter > 0) {
307 if (pl_plat_clear)
308 pl_plat_clear();
309 else
310 memset(pl_vout_buf, 0,
311 dstride * pl_vout_h * pl_vout_bpp / 8);
312 clear_counter--;
313 }
314
315 if (pl_plat_blit)
316 {
317 pl_plat_blit(doffs, src, w, h, stride, bgr24);
318 goto out_hud;
319 }
320
321 if (dest == NULL)
322 goto out;
323
324 dest += doffs * 2;
325
326 if (bgr24)
327 {
328 if (pl_rearmed_cbs.only_16bpp) {
329 for (; h1-- > 0; dest += dstride * 2, src += stride)
330 {
331 bgr888_to_rgb565(dest, src, w * 3);
332 }
333 }
334 else {
335 dest -= doffs * 2;
336 dest += (doffs / 8) * 24;
337
338 for (; h1-- > 0; dest += dstride * 3, src += stride)
339 {
340 bgr888_to_rgb888(dest, src, w * 3);
341 }
342 }
343 }
344#ifdef __ARM_NEON__
345 else if (soft_filter == SOFT_FILTER_SCALE2X && pl_vout_scale == 2)
346 {
347 neon_scale2x_16_16(src, (void *)dest, w,
348 stride * 2, dstride * 2, h);
349 }
350 else if (soft_filter == SOFT_FILTER_EAGLE2X && pl_vout_scale == 2)
351 {
352 neon_eagle2x_16_16(src, (void *)dest, w,
353 stride * 2, dstride * 2, h);
354 }
355#endif
356 else
357 {
358 for (; h1-- > 0; dest += dstride * 2, src += stride)
359 {
360 bgr555_to_rgb565(dest, src, w * 2);
361 }
362 }
363
364out_hud:
365 print_hud(w * pl_vout_scale, h * pl_vout_scale, 0);
366
367out:
368 pcnt_end(PCNT_BLIT);
369
370 // let's flip now
371 pl_vout_buf = plat_gvideo_flip();
372 pl_rearmed_cbs.flip_cnt++;
373}
374
375static int pl_vout_open(void)
376{
377 struct timeval now;
378
379 // force mode update on pl_vout_set_mode() call from gpulib/vout_pl
380 pl_vout_buf = NULL;
381
382 plat_gvideo_open(is_pal);
383
384 gettimeofday(&now, 0);
385 vsync_usec_time = now.tv_usec;
386 while (vsync_usec_time >= frame_interval)
387 vsync_usec_time -= frame_interval;
388
389 return 0;
390}
391
392static void pl_vout_close(void)
393{
394 plat_gvideo_close();
395}
396
397static void pl_set_gpu_caps(int caps)
398{
399 pl_rearmed_cbs.gpu_caps = caps;
400}
401
402void *pl_prepare_screenshot(int *w, int *h, int *bpp)
403{
404 void *ret = plat_prepare_screenshot(w, h, bpp);
405 if (ret != NULL)
406 return ret;
407
408 *w = pl_vout_w;
409 *h = pl_vout_h;
410 *bpp = pl_vout_bpp;
411
412 return pl_vout_buf;
413}
414
415/* display/redering mode switcher */
416static int dispmode_default(void)
417{
418 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
419 soft_filter = SOFT_FILTER_NONE;
420 snprintf(hud_msg, sizeof(hud_msg), "default mode");
421 return 1;
422}
423
424int dispmode_doubleres(void)
425{
426 if (!(pl_rearmed_cbs.gpu_caps & GPU_CAP_SUPPORTS_2X)
427 || !resolution_ok(psx_w * 2, psx_h * 2) || psx_bpp != 16)
428 return 0;
429
430 dispmode_default();
431 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
432 snprintf(hud_msg, sizeof(hud_msg), "double resolution");
433 return 1;
434}
435
436int dispmode_scale2x(void)
437{
438 if (!resolution_ok(psx_w * 2, psx_h * 2) || psx_bpp != 16)
439 return 0;
440
441 dispmode_default();
442 soft_filter = SOFT_FILTER_SCALE2X;
443 snprintf(hud_msg, sizeof(hud_msg), "scale2x");
444 return 1;
445}
446
447int dispmode_eagle2x(void)
448{
449 if (!resolution_ok(psx_w * 2, psx_h * 2) || psx_bpp != 16)
450 return 0;
451
452 dispmode_default();
453 soft_filter = SOFT_FILTER_EAGLE2X;
454 snprintf(hud_msg, sizeof(hud_msg), "eagle2x");
455 return 1;
456}
457
458static int (*dispmode_switchers[])(void) = {
459 dispmode_default,
460#ifdef __ARM_NEON__
461 dispmode_doubleres,
462 dispmode_scale2x,
463 dispmode_eagle2x,
464#endif
465};
466
467static int dispmode_current;
468
469void pl_switch_dispmode(void)
470{
471 if (pl_rearmed_cbs.gpu_caps & GPU_CAP_OWNS_DISPLAY)
472 return;
473
474 while (1) {
475 dispmode_current++;
476 if (dispmode_current >=
477 sizeof(dispmode_switchers) / sizeof(dispmode_switchers[0]))
478 dispmode_current = 0;
479 if (dispmode_switchers[dispmode_current]())
480 break;
481 }
482}
483
484#ifndef MAEMO
485/* adjust circle-like analog inputs to better match
486 * more square-like analogs in PSX */
487static void update_analog_nub_adjust(int *x_, int *y_)
488{
489 #define d 16
490 static const int scale[] =
491 { 0 - d*2, 0 - d*2, 0 - d*2, 12 - d*2,
492 30 - d*2, 60 - d*2, 75 - d*2, 60 - d*2, 60 - d*2 };
493 int x = abs(*x_);
494 int y = abs(*y_);
495 int scale_x = scale[y / 16];
496 int scale_y = scale[x / 16];
497
498 if (x) {
499 x += d + (x * scale_x >> 8);
500 if (*x_ < 0)
501 x = -x;
502 }
503 if (y) {
504 y += d + (y * scale_y >> 8);
505 if (*y_ < 0)
506 y = -y;
507 }
508
509 *x_ = x;
510 *y_ = y;
511 #undef d
512}
513
514static void update_analogs(void)
515{
516 int *nubp[2] = { in_a1, in_a2 };
517 int vals[2];
518 int i, a, v, ret;
519
520 for (i = 0; i < 2; i++)
521 {
522 if (in_adev[i] < 0)
523 continue;
524
525 for (a = 0; a < 2; a++) {
526 vals[a] = 0;
527
528 ret = in_update_analog(in_adev[i], in_adev_axis[i][a], &v);
529 if (ret == 0)
530 vals[a] = 128 * v / IN_ABS_RANGE;
531 }
532
533 if (in_adev_is_nublike[i])
534 update_analog_nub_adjust(&vals[0], &vals[1]);
535
536 for (a = 0; a < 2; a++) {
537 v = vals[a] + 127;
538 if (v < 0) v = 0;
539 else if (v > 255) v = 255;
540 nubp[i][a] = v;
541 }
542
543 }
544 //printf("%4d %4d %4d %4d\n", in_a1[0], in_a1[1], in_a2[0], in_a2[1]);
545}
546
547static void update_input(void)
548{
549 int actions[IN_BINDTYPE_COUNT] = { 0, };
550 unsigned int emu_act;
551
552 in_update(actions);
553 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD)
554 update_analogs();
555 emu_act = actions[IN_BINDTYPE_EMU];
556 in_state_gun = (emu_act & SACTION_GUN_MASK) >> SACTION_GUN_TRIGGER;
557
558 emu_act &= ~SACTION_GUN_MASK;
559 if (emu_act) {
560 int which = 0;
561 for (; !(emu_act & 1); emu_act >>= 1, which++)
562 ;
563 emu_act = which;
564 }
565 emu_set_action(emu_act);
566
567 in_keystate = actions[IN_BINDTYPE_PLAYER12];
568}
569#else /* MAEMO */
570static void update_input(void)
571{
572}
573#endif
574
575void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
576{
577 if (tsdev)
578 pl_gun_ts_update(tsdev, xn, yn, in);
579
580 *xres = psx_w;
581 *yres = psx_h;
582}
583
584#define MAX_LAG_FRAMES 3
585
586#define tvdiff(tv, tv_old) \
587 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
588
589/* called on every vsync */
590void pl_frame_limit(void)
591{
592 static struct timeval tv_old, tv_expect;
593 static int vsync_cnt_prev, drc_active_vsyncs;
594 struct timeval now;
595 int diff, usadj;
596
597 vsync_cnt++;
598
599 /* doing input here because the pad is polled
600 * thousands of times per frame for some reason */
601 update_input();
602
603 pcnt_end(PCNT_ALL);
604 gettimeofday(&now, 0);
605
606 if (now.tv_sec != tv_old.tv_sec) {
607 diff = tvdiff(now, tv_old);
608 pl_rearmed_cbs.vsps_cur = 0.0f;
609 if (0 < diff && diff < 2000000)
610 pl_rearmed_cbs.vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
611 vsync_cnt_prev = vsync_cnt;
612
613 if (g_opts & OPT_SHOWFPS)
614 pl_rearmed_cbs.flips_per_sec = pl_rearmed_cbs.flip_cnt;
615 pl_rearmed_cbs.flip_cnt = 0;
616 if (g_opts & OPT_SHOWCPU)
617 pl_rearmed_cbs.cpu_usage = get_cpu_ticks();
618
619 if (hud_new_msg > 0) {
620 hud_new_msg--;
621 if (hud_new_msg == 0)
622 hud_msg[0] = 0;
623 }
624 tv_old = now;
625 }
626#ifdef PCNT
627 static int ya_vsync_count;
628 if (++ya_vsync_count == PCNT_FRAMES) {
629 pcnt_print(pl_rearmed_cbs.vsps_cur);
630 ya_vsync_count = 0;
631 }
632#endif
633
634 // tv_expect uses usec*1024 units instead of usecs for better accuracy
635 tv_expect.tv_usec += frame_interval1024;
636 if (tv_expect.tv_usec >= (1000000 << 10)) {
637 tv_expect.tv_usec -= (1000000 << 10);
638 tv_expect.tv_sec++;
639 }
640 diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 + (tv_expect.tv_usec >> 10) - now.tv_usec;
641
642 if (diff > MAX_LAG_FRAMES * frame_interval || diff < -MAX_LAG_FRAMES * frame_interval) {
643 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, frame_interval);
644 tv_expect = now;
645 diff = 0;
646 // try to align with vsync
647 usadj = vsync_usec_time;
648 while (usadj < tv_expect.tv_usec - frame_interval)
649 usadj += frame_interval;
650 tv_expect.tv_usec = usadj << 10;
651 }
652
653 if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) {
654 // yay for working usleep on pandora!
655 //printf("usleep %d\n", diff - frame_interval / 2);
656 usleep(diff - frame_interval);
657 }
658
659 if (pl_rearmed_cbs.frameskip) {
660 if (diff < -frame_interval)
661 pl_rearmed_cbs.fskip_advice = 1;
662 else if (diff >= 0)
663 pl_rearmed_cbs.fskip_advice = 0;
664
665 // recompilation is not that fast and may cause frame skip on
666 // loading screens and such, resulting in flicker or glitches
667 if (new_dynarec_did_compile) {
668 if (drc_active_vsyncs < 32)
669 pl_rearmed_cbs.fskip_advice = 0;
670 drc_active_vsyncs++;
671 }
672 else
673 drc_active_vsyncs = 0;
674 new_dynarec_did_compile = 0;
675 }
676
677 pcnt_start(PCNT_ALL);
678}
679
680void pl_timing_prepare(int is_pal_)
681{
682 pl_rearmed_cbs.fskip_advice = 0;
683 pl_rearmed_cbs.flips_per_sec = 0;
684 pl_rearmed_cbs.cpu_usage = 0;
685
686 is_pal = is_pal_;
687 frame_interval = is_pal ? 20000 : 16667;
688 frame_interval1024 = is_pal ? 20000*1024 : 17066667;
689
690 // used by P.E.Op.S. frameskip code
691 pl_rearmed_cbs.gpu_peops.fFrameRateHz = is_pal ? 50.0f : 59.94f;
692 pl_rearmed_cbs.gpu_peops.dwFrameRateTicks =
693 (100000*100 / (unsigned long)(pl_rearmed_cbs.gpu_peops.fFrameRateHz*100));
694}
695
696static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
697{
698 *x = g_layer_x;
699 *y = g_layer_y;
700 *w = g_layer_w;
701 *h = g_layer_h;
702}
703
704static void *pl_mmap(unsigned int size)
705{
706 return plat_mmap(0, size, 0, 0);
707}
708
709static void pl_munmap(void *ptr, unsigned int size)
710{
711 plat_munmap(ptr, size);
712}
713
714struct rearmed_cbs pl_rearmed_cbs = {
715 pl_get_layer_pos,
716 pl_vout_open,
717 pl_vout_set_mode,
718 pl_vout_flip,
719 pl_vout_close,
720
721 .mmap = pl_mmap,
722 .munmap = pl_munmap,
723 .pl_set_gpu_caps = pl_set_gpu_caps,
724};
725
726/* watchdog */
727static void *watchdog_thread(void *unused)
728{
729 int vsync_cnt_old = 0;
730 int seen_dead = 0;
731 int sleep_time = 5;
732
733#if !defined(NDEBUG) || defined(DRC_DBG)
734 // don't interfere with debug
735 return NULL;
736#endif
737 while (1)
738 {
739 sleep(sleep_time);
740
741 if (stop) {
742 seen_dead = 0;
743 sleep_time = 5;
744 continue;
745 }
746 if (vsync_cnt != vsync_cnt_old) {
747 vsync_cnt_old = vsync_cnt;
748 seen_dead = 0;
749 sleep_time = 2;
750 continue;
751 }
752
753 seen_dead++;
754 sleep_time = 1;
755 if (seen_dead > 1)
756 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
757 if (seen_dead > 4) {
758 fprintf(stderr, "watchdog: lockup detected, aborting\n");
759 // we can't do any cleanup here really, the main thread is
760 // likely touching resources and would crash anyway
761 abort();
762 }
763 }
764}
765
766void pl_start_watchdog(void)
767{
768 pthread_attr_t attr;
769 pthread_t tid;
770 int ret;
771
772 pthread_attr_init(&attr);
773 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
774
775 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
776 if (ret != 0)
777 fprintf(stderr, "could not start watchdog: %d\n", ret);
778}
779
780void pl_init(void)
781{
782 extern unsigned int hSyncCount; // from psxcounters
783 extern unsigned int frame_counter;
784
785 psx_w = psx_h = pl_vout_w = pl_vout_h = 256;
786 psx_bpp = pl_vout_bpp = 16;
787
788 tsdev = pl_gun_ts_init();
789
790 pl_rearmed_cbs.gpu_hcnt = &hSyncCount;
791 pl_rearmed_cbs.gpu_frame_count = &frame_counter;
792}