frontend: update caanoo port
[pcsx_rearmed.git] / frontend / plugin_lib.c
CommitLineData
b60f2812 1/*
8f892648 2 * (C) notaz, 2010-2011
b60f2812 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>
69af03a2 10#include <string.h>
11#include <stdarg.h>
799b0b87 12#include <stdint.h>
72228559 13#include <sys/time.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <fcntl.h>
17#include <unistd.h>
7c0f51de 18#include <pthread.h>
b60f2812 19
cc56203b 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"
72228559 25#include "plugin_lib.h"
3c70c47b 26#include "menu.h"
8f892648 27#include "main.h"
47821672 28#include "plat.h"
72228559 29#include "pcnt.h"
4c08b9e7 30#include "pl_gun_ts.h"
69af03a2 31#include "../libpcsxcore/new_dynarec/new_dynarec.h"
799b0b87 32#include "../libpcsxcore/psemu_plugin_defs.h"
fa56d360 33#include "../plugins/gpulib/cspace.h"
e4c83ca6 34#include "../plugins/dfinput/externals.h"
b60f2812 35
4c08b9e7 36int in_type1, in_type2;
37int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
1b0c5139 38int in_adev[2] = { -1, -1 }, in_adev_axis[2][2] = {{ 0, 1 }, { 0, 1 }};
2c5d0a55 39int in_adev_is_nublike[2];
4c08b9e7 40int in_keystate, in_state_gun;
b944a30e 41int in_enable_vibration;
faf2b2aa 42void *tsdev;
76f7048e 43void *pl_vout_buf;
6469a8c4 44int g_layer_x, g_layer_y, g_layer_w, g_layer_h;
ddc0a02a 45static int pl_vout_w, pl_vout_h, pl_vout_bpp; /* output display/layer */
fa56d360 46static int pl_vout_scale;
ddc0a02a 47static int psx_w, psx_h, psx_bpp;
a92f6af1 48static int vsync_cnt;
ab423939 49static int is_pal, frame_interval, frame_interval1024;
50static int vsync_usec_time;
72228559 51
c9099d02 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
4c08b9e7 58
59static __attribute__((noinline)) int get_cpu_ticks(void)
72228559 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;
c9099d02 75 if (ret > 200)
76 ret = 0;
72228559 77 last_utime = utime;
78 return ret;
79}
80
c9099d02 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
41f55c9f 101static void print_msg(int h, int border)
8f892648 102{
c9099d02 103 hud_print(pl_vout_buf, pl_vout_w, border + 2, h - 10, hud_msg);
8f892648 104}
105
41f55c9f 106static void print_fps(int h, int border)
72228559 107{
c9099d02 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);
72228559 111}
112
41f55c9f 113static void print_cpu_usage(int w, int h, int border)
72228559 114{
c9099d02 115 hud_printf(pl_vout_buf, pl_vout_w, pl_vout_w - border - 28, h - 10,
116 "%3d", pl_rearmed_cbs.cpu_usage);
72228559 117}
b60f2812 118
90f1d26c 119// draw 192x8 status of 24 sound channels
55b0eeea 120static __attribute__((noinline)) void draw_active_chans(int vout_w, int vout_h)
90f1d26c 121{
174c454a 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;
90f1d26c 125
126 static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
76f7048e 127 unsigned short *dest = (unsigned short *)pl_vout_buf +
55b0eeea 128 vout_w * (vout_h - 10) + vout_w / 2 - 192/2;
90f1d26c 129 unsigned short *d, p;
130 int c, x, y;
131
c9099d02 132 if (dest == NULL || pl_vout_bpp != 16)
90f1d26c 133 return;
134
174c454a 135 spu_get_debug_info(&live_chans, &run_chans, &fmod_chans, &noise_chans);
90f1d26c 136
137 for (c = 0; c < 24; c++) {
138 d = dest + c * 8;
174c454a 139 p = !(live_chans & (1<<c)) ? (run_chans & (1<<c) ? 0x01c0 : 0) :
90f1d26c 140 (fmod_chans & (1<<c)) ? 0xf000 :
141 (noise_chans & (1<<c)) ? 0x001f :
142 colors[c & 1];
55b0eeea 143 for (y = 0; y < 8; y++, d += vout_w)
90f1d26c 144 for (x = 0; x < 8; x++)
145 d[x] = p;
146 }
147}
148
02783d0b 149static void print_hud(int w, int h, int xborder)
55b0eeea 150{
aafcb4dd 151 if (h < 16)
152 return;
153
02783d0b 154 if (w < pl_vout_w)
155 xborder += (pl_vout_w - w) / 2;
156 if (h > pl_vout_h)
157 h = pl_vout_h;
841ba5ee 158
55b0eeea 159 if (g_opts & OPT_SHOWSPU)
160 draw_active_chans(w, h);
161
162 if (hud_msg[0] != 0)
41f55c9f 163 print_msg(h, xborder);
55b0eeea 164 else if (g_opts & OPT_SHOWFPS)
41f55c9f 165 print_fps(h, xborder);
55b0eeea 166
167 if (g_opts & OPT_SHOWCPU)
41f55c9f 168 print_cpu_usage(w, h, xborder);
55b0eeea 169}
170
9e5ac38f 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
fa56d360 223// XXX: this is platform specific really
224static int resolution_ok(int w, int h)
69af03a2 225{
fa56d360 226 return w <= 1024 && h <= 512;
227}
228
e4c83ca6 229static void pl_vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
fa56d360 230{
231 int vout_w, vout_h, vout_bpp;
232
a185be70 233 // special h handling, Wipeout likes to change it by 1-6
321ca84d 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;
a185be70 238
e4c83ca6 239 psx_w = raw_w;
240 psx_h = raw_h;
241 vout_w = w;
242 vout_h = h;
fa56d360 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
b60f2812 259
fa56d360 260 if (pl_vout_buf != NULL && vout_w == pl_vout_w && vout_h == pl_vout_h
261 && vout_bpp == pl_vout_bpp)
262 return;
d352cde2 263
fa56d360 264 update_layer_size(vout_w, vout_h);
9e5ac38f 265
fa56d360 266 pl_vout_buf = plat_gvideo_set_mode(&vout_w, &vout_h, &vout_bpp);
c9099d02 267 if (pl_vout_buf == NULL && pl_plat_blit == NULL)
ddc0a02a 268 fprintf(stderr, "failed to set mode %dx%d@%d\n",
e4c83ca6 269 vout_w, vout_h, psx_bpp);
fa56d360 270 else {
271 pl_vout_w = vout_w;
272 pl_vout_h = vout_h;
273 pl_vout_bpp = vout_bpp;
274 }
a185be70 275
ddc0a02a 276 menu_notify_mode_change(pl_vout_w, pl_vout_h, pl_vout_bpp);
69af03a2 277}
278
fa56d360 279static void pl_vout_flip(const void *vram, int stride, int bgr24, int w, int h)
69af03a2 280{
fa56d360 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
cc56203b 287 pcnt_start(PCNT_BLIT);
288
fa56d360 289 if (vram == NULL) {
290 // blanking
c9099d02 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;
fa56d360 297 }
298
299 // borders
300 doffs = (dstride - w * pl_vout_scale) / 2 & ~1;
fa56d360 301
302 if (doffs > doffs_old)
303 clear_counter = 2;
304 doffs_old = doffs;
305
306 if (clear_counter > 0) {
c9099d02 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);
fa56d360 312 clear_counter--;
313 }
314
c9099d02 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
fa56d360 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,
c9099d02 348 stride * 2, dstride * 2, h);
fa56d360 349 }
350 else if (soft_filter == SOFT_FILTER_EAGLE2X && pl_vout_scale == 2)
351 {
352 neon_eagle2x_16_16(src, (void *)dest, w,
c9099d02 353 stride * 2, dstride * 2, h);
fa56d360 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 }
8f892648 363
c9099d02 364out_hud:
02783d0b 365 print_hud(w * pl_vout_scale, h * pl_vout_scale, 0);
72228559 366
fa56d360 367out:
cc56203b 368 pcnt_end(PCNT_BLIT);
369
69af03a2 370 // let's flip now
6469a8c4 371 pl_vout_buf = plat_gvideo_flip();
fa56d360 372 pl_rearmed_cbs.flip_cnt++;
b60f2812 373}
374
76f7048e 375static int pl_vout_open(void)
6d1a1ac2 376{
cefe86b7 377 struct timeval now;
378
fa56d360 379 // force mode update on pl_vout_set_mode() call from gpulib/vout_pl
380 pl_vout_buf = NULL;
cefe86b7 381
ab423939 382 plat_gvideo_open(is_pal);
76f7048e 383
cefe86b7 384 gettimeofday(&now, 0);
385 vsync_usec_time = now.tv_usec;
76f7048e 386 while (vsync_usec_time >= frame_interval)
387 vsync_usec_time -= frame_interval;
cefe86b7 388
6d1a1ac2 389 return 0;
390}
391
76f7048e 392static void pl_vout_close(void)
b60f2812 393{
6469a8c4 394 plat_gvideo_close();
b60f2812 395}
396
fa56d360 397static void pl_set_gpu_caps(int caps)
398{
399 pl_rearmed_cbs.gpu_caps = caps;
400}
401
29a8c4f3 402void *pl_prepare_screenshot(int *w, int *h, int *bpp)
403{
6469a8c4 404 void *ret = plat_prepare_screenshot(w, h, bpp);
405 if (ret != NULL)
406 return ret;
407
76f7048e 408 *w = pl_vout_w;
409 *h = pl_vout_h;
410 *bpp = pl_vout_bpp;
29a8c4f3 411
76f7048e 412 return pl_vout_buf;
29a8c4f3 413}
414
fa56d360 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{
e4c83ca6 438 if (!resolution_ok(psx_w * 2, psx_h * 2) || psx_bpp != 16)
fa56d360 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{
e4c83ca6 449 if (!resolution_ok(psx_w * 2, psx_h * 2) || psx_bpp != 16)
fa56d360 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
1b0c5139 484#ifndef MAEMO
2c5d0a55 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{
e4c83ca6 489 #define d 16
cc8ae53e 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 }
2c5d0a55 508
509 *x_ = x;
510 *y_ = y;
e4c83ca6 511 #undef d
2c5d0a55 512}
513
1b0c5139 514static void update_analogs(void)
515{
516 int *nubp[2] = { in_a1, in_a2 };
2c5d0a55 517 int vals[2];
1b0c5139 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++) {
2c5d0a55 526 vals[a] = 0;
1b0c5139 527
528 ret = in_update_analog(in_adev[i], in_adev_axis[i][a], &v);
2c5d0a55 529 if (ret == 0)
530 vals[a] = 128 * v / IN_ABS_RANGE;
1b0c5139 531 }
2c5d0a55 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
1b0c5139 543 }
544 //printf("%4d %4d %4d %4d\n", in_a1[0], in_a1[1], in_a2[0], in_a2[1]);
545}
546
15d46930 547static void update_input(void)
548{
549 int actions[IN_BINDTYPE_COUNT] = { 0, };
8f892648 550 unsigned int emu_act;
15d46930 551
552 in_update(actions);
4c08b9e7 553 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD)
1b0c5139 554 update_analogs();
8f892648 555 emu_act = actions[IN_BINDTYPE_EMU];
4c08b9e7 556 in_state_gun = (emu_act & SACTION_GUN_MASK) >> SACTION_GUN_TRIGGER;
557
558 emu_act &= ~SACTION_GUN_MASK;
8f892648 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
799b0b87 567 in_keystate = actions[IN_BINDTYPE_PLAYER12];
15d46930 568}
1b0c5139 569#else /* MAEMO */
570static void update_input(void)
571{
572}
573#endif
15d46930 574
e4c83ca6 575void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
4c08b9e7 576{
faf2b2aa 577 if (tsdev)
e4c83ca6 578 pl_gun_ts_update(tsdev, xn, yn, in);
4c08b9e7 579
e4c83ca6 580 *xres = psx_w;
581 *yres = psx_h;
4c08b9e7 582}
583
bce6b056 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)
bce6b056 588
72228559 589/* called on every vsync */
590void pl_frame_limit(void)
591{
bce6b056 592 static struct timeval tv_old, tv_expect;
2f546f9a 593 static int vsync_cnt_prev, drc_active_vsyncs;
bce6b056 594 struct timeval now;
cefe86b7 595 int diff, usadj;
bce6b056 596
597 vsync_cnt++;
72228559 598
15d46930 599 /* doing input here because the pad is polled
600 * thousands of times per frame for some reason */
601 update_input();
602
72228559 603 pcnt_end(PCNT_ALL);
bce6b056 604 gettimeofday(&now, 0);
72228559 605
bce6b056 606 if (now.tv_sec != tv_old.tv_sec) {
607 diff = tvdiff(now, tv_old);
a92f6af1 608 pl_rearmed_cbs.vsps_cur = 0.0f;
bce6b056 609 if (0 < diff && diff < 2000000)
a92f6af1 610 pl_rearmed_cbs.vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
7c0f51de 611 vsync_cnt_prev = vsync_cnt;
a92f6af1 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;
bd6267e6 616 if (g_opts & OPT_SHOWCPU)
a92f6af1 617 pl_rearmed_cbs.cpu_usage = get_cpu_ticks();
8f892648 618
619 if (hud_new_msg > 0) {
620 hud_new_msg--;
621 if (hud_new_msg == 0)
622 hud_msg[0] = 0;
623 }
a92f6af1 624 tv_old = now;
72228559 625 }
626#ifdef PCNT
627 static int ya_vsync_count;
628 if (++ya_vsync_count == PCNT_FRAMES) {
a92f6af1 629 pcnt_print(pl_rearmed_cbs.vsps_cur);
72228559 630 ya_vsync_count = 0;
631 }
632#endif
633
76f7048e 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);
c89cd762 644 tv_expect = now;
645 diff = 0;
cefe86b7 646 // try to align with vsync
647 usadj = vsync_usec_time;
76f7048e 648 while (usadj < tv_expect.tv_usec - frame_interval)
649 usadj += frame_interval;
650 tv_expect.tv_usec = usadj << 10;
c89cd762 651 }
652
76f7048e 653 if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) {
c89cd762 654 // yay for working usleep on pandora!
76f7048e 655 //printf("usleep %d\n", diff - frame_interval / 2);
47f37583 656 usleep(diff - frame_interval);
c89cd762 657 }
658
e64dc4c5 659 if (pl_rearmed_cbs.frameskip) {
76f7048e 660 if (diff < -frame_interval)
e64dc4c5 661 pl_rearmed_cbs.fskip_advice = 1;
662 else if (diff >= 0)
663 pl_rearmed_cbs.fskip_advice = 0;
2f546f9a 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;
bce6b056 675 }
72228559 676
677 pcnt_start(PCNT_ALL);
678}
679
ab423939 680void pl_timing_prepare(int is_pal_)
76f7048e 681{
682 pl_rearmed_cbs.fskip_advice = 0;
a92f6af1 683 pl_rearmed_cbs.flips_per_sec = 0;
684 pl_rearmed_cbs.cpu_usage = 0;
76f7048e 685
ab423939 686 is_pal = is_pal_;
76f7048e 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
201c21e2 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
9ee0fd5b 704static void *pl_mmap(unsigned int size)
705{
a2ad8cc5 706 return plat_mmap(0, size, 0, 0);
9ee0fd5b 707}
708
709static void pl_munmap(void *ptr, unsigned int size)
710{
711 plat_munmap(ptr, size);
712}
713
e64dc4c5 714struct rearmed_cbs pl_rearmed_cbs = {
201c21e2 715 pl_get_layer_pos,
76f7048e 716 pl_vout_open,
717 pl_vout_set_mode,
718 pl_vout_flip,
719 pl_vout_close,
fa56d360 720
721 .mmap = pl_mmap,
722 .munmap = pl_munmap,
723 .pl_set_gpu_caps = pl_set_gpu_caps,
201c21e2 724};
725
7c0f51de 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
d3f3bf09 733#if !defined(NDEBUG) || defined(DRC_DBG)
799b0b87 734 // don't interfere with debug
735 return NULL;
736#endif
7c0f51de 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
4c08b9e7 780void pl_init(void)
781{
24de2dd4 782 extern unsigned int hSyncCount; // from psxcounters
3ece2f0c 783 extern unsigned int frame_counter;
24de2dd4 784
ddc0a02a 785 psx_w = psx_h = pl_vout_w = pl_vout_h = 256;
786 psx_bpp = pl_vout_bpp = 16;
321ca84d 787
faf2b2aa 788 tsdev = pl_gun_ts_init();
24de2dd4 789
790 pl_rearmed_cbs.gpu_hcnt = &hSyncCount;
3ece2f0c 791 pl_rearmed_cbs.gpu_frame_count = &frame_counter;
4c08b9e7 792}