cdrom: change pause timing again
[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>
308c6e67 19#include <assert.h>
b60f2812 20
cc56203b 21#include "libpicofe/fonts.h"
22#include "libpicofe/input.h"
23#include "libpicofe/plat.h"
24#include "libpicofe/arm/neon_scale2x.h"
25#include "libpicofe/arm/neon_eagle2x.h"
72228559 26#include "plugin_lib.h"
3c70c47b 27#include "menu.h"
8f892648 28#include "main.h"
47821672 29#include "plat.h"
72228559 30#include "pcnt.h"
4c08b9e7 31#include "pl_gun_ts.h"
c82f907a 32#include "cspace.h"
da710571 33#include "psemu_plugin_defs.h"
69af03a2 34#include "../libpcsxcore/new_dynarec/new_dynarec.h"
87e5b45f 35#include "../libpcsxcore/psxmem_map.h"
abf09485 36#include "../libpcsxcore/gpu.h"
fb640dd9 37#include "../libpcsxcore/r3000a.h"
4c8f1c25 38#include "../libpcsxcore/psxcounters.h"
b60f2812 39
92a5fe88 40#define HUD_HEIGHT 10
41
7a8d521f 42int in_type[8];
43int multitap1;
44int multitap2;
45int in_analog_left[8][2] = {{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 }};
46int in_analog_right[8][2] = {{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 }};
1b0c5139 47int in_adev[2] = { -1, -1 }, in_adev_axis[2][2] = {{ 0, 1 }, { 0, 1 }};
2c5d0a55 48int in_adev_is_nublike[2];
7a8d521f 49unsigned short in_keystate[8];
50int in_mouse[8][2];
b944a30e 51int in_enable_vibration;
faf2b2aa 52void *tsdev;
76f7048e 53void *pl_vout_buf;
6469a8c4 54int g_layer_x, g_layer_y, g_layer_w, g_layer_h;
ddc0a02a 55static int pl_vout_w, pl_vout_h, pl_vout_bpp; /* output display/layer */
7cfc0c66 56static int pl_vout_scale_w, pl_vout_scale_h, pl_vout_yoffset;
ddc0a02a 57static int psx_w, psx_h, psx_bpp;
a92f6af1 58static int vsync_cnt;
ab423939 59static int is_pal, frame_interval, frame_interval1024;
60static int vsync_usec_time;
72228559 61
c9099d02 62// platform hooks
63void (*pl_plat_clear)(void);
64void (*pl_plat_blit)(int doffs, const void *src, int w, int h,
65 int sstride, int bgr24);
66void (*pl_plat_hud_print)(int x, int y, const char *str, int bpp);
67
4c08b9e7 68
69static __attribute__((noinline)) int get_cpu_ticks(void)
72228559 70{
71 static unsigned long last_utime;
72 static int fd;
73 unsigned long utime, ret;
74 char buf[128];
75
76 if (fd == 0)
77 fd = open("/proc/self/stat", O_RDONLY);
78 lseek(fd, 0, SEEK_SET);
79 buf[0] = 0;
80 read(fd, buf, sizeof(buf));
81 buf[sizeof(buf) - 1] = 0;
82
83 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
84 ret = utime - last_utime;
c9099d02 85 if (ret > 200)
86 ret = 0;
72228559 87 last_utime = utime;
88 return ret;
89}
90
c9099d02 91static void hud_print(void *fb, int w, int x, int y, const char *text)
92{
93 if (pl_plat_hud_print)
94 pl_plat_hud_print(x, y, text, pl_vout_bpp);
95 else if (pl_vout_bpp == 16)
96 basic_text_out16_nf(fb, w, x, y, text);
97}
98
99static void hud_printf(void *fb, int w, int x, int y, const char *texto, ...)
100{
101 va_list args;
102 char buffer[256];
103
104 va_start(args, texto);
105 vsnprintf(buffer, sizeof(buffer), texto, args);
106 va_end(args);
107
108 hud_print(fb, w, x, y, buffer);
109}
110
41f55c9f 111static void print_msg(int h, int border)
8f892648 112{
92a5fe88 113 hud_print(pl_vout_buf, pl_vout_w, border + 2, h - HUD_HEIGHT, hud_msg);
8f892648 114}
115
41f55c9f 116static void print_fps(int h, int border)
72228559 117{
92a5fe88 118 hud_printf(pl_vout_buf, pl_vout_w, border + 2, h - HUD_HEIGHT,
c9099d02 119 "%2d %4.1f", pl_rearmed_cbs.flips_per_sec,
120 pl_rearmed_cbs.vsps_cur);
72228559 121}
122
308c6e67 123static void print_cpu_usage(int x, int h)
72228559 124{
308c6e67 125 hud_printf(pl_vout_buf, pl_vout_w, x - 28,
92a5fe88 126 h - HUD_HEIGHT, "%3d", pl_rearmed_cbs.cpu_usage);
72228559 127}
b60f2812 128
90f1d26c 129// draw 192x8 status of 24 sound channels
55b0eeea 130static __attribute__((noinline)) void draw_active_chans(int vout_w, int vout_h)
90f1d26c 131{
174c454a 132 extern void spu_get_debug_info(int *chans_out, int *run_chans,
133 int *fmod_chans_out, int *noise_chans_out); // hack
134 int live_chans, run_chans, fmod_chans, noise_chans;
90f1d26c 135
136 static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
76f7048e 137 unsigned short *dest = (unsigned short *)pl_vout_buf +
2da2fc76 138 pl_vout_w * (vout_h - HUD_HEIGHT) + pl_vout_w / 2 - 192/2;
90f1d26c 139 unsigned short *d, p;
140 int c, x, y;
141
ba8d6a2d 142 if (pl_vout_buf == NULL || pl_vout_bpp != 16)
90f1d26c 143 return;
144
174c454a 145 spu_get_debug_info(&live_chans, &run_chans, &fmod_chans, &noise_chans);
90f1d26c 146
147 for (c = 0; c < 24; c++) {
148 d = dest + c * 8;
174c454a 149 p = !(live_chans & (1<<c)) ? (run_chans & (1<<c) ? 0x01c0 : 0) :
90f1d26c 150 (fmod_chans & (1<<c)) ? 0xf000 :
151 (noise_chans & (1<<c)) ? 0x001f :
152 colors[c & 1];
2da2fc76 153 for (y = 0; y < 8; y++, d += pl_vout_w)
90f1d26c 154 for (x = 0; x < 8; x++)
155 d[x] = p;
156 }
157}
158
308c6e67 159static void print_hud(int x, int w, int h)
55b0eeea 160{
308c6e67 161 if (h < 192)
aafcb4dd 162 return;
163
02783d0b 164 if (h > pl_vout_h)
165 h = pl_vout_h;
841ba5ee 166
55b0eeea 167 if (g_opts & OPT_SHOWSPU)
168 draw_active_chans(w, h);
169
170 if (hud_msg[0] != 0)
308c6e67 171 print_msg(h, x);
55b0eeea 172 else if (g_opts & OPT_SHOWFPS)
308c6e67 173 print_fps(h, x);
55b0eeea 174
175 if (g_opts & OPT_SHOWCPU)
308c6e67 176 print_cpu_usage(x + w, h);
55b0eeea 177}
178
9e5ac38f 179/* update scaler target size according to user settings */
180static void update_layer_size(int w, int h)
181{
182 float mult;
183 int imult;
184
185 switch (g_scaler) {
186 case SCALE_1_1:
187 g_layer_w = w; g_layer_h = h;
188 break;
189
efcf1f73 190 case SCALE_2_2:
191 g_layer_w = w; g_layer_h = h;
192 if (w * 2 <= g_menuscreen_w)
193 g_layer_w = w * 2;
194 if (h * 2 <= g_menuscreen_h)
195 g_layer_h = h * 2;
196 break;
197
9e5ac38f 198 case SCALE_4_3v2:
199 if (h > g_menuscreen_h || (240 < h && h <= 360))
200 goto fractional_4_3;
201
202 // 4:3 that prefers integer scaling
203 imult = g_menuscreen_h / h;
204 g_layer_w = w * imult;
205 g_layer_h = h * imult;
206 mult = (float)g_layer_w / (float)g_layer_h;
207 if (mult < 1.25f || mult > 1.666f)
208 g_layer_w = 4.0f/3.0f * (float)g_layer_h;
209 printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult);
210 break;
211
212 fractional_4_3:
213 case SCALE_4_3:
214 mult = 240.0f / (float)h * 4.0f / 3.0f;
215 if (h > 256)
216 mult *= 2.0f;
217 g_layer_w = mult * (float)g_menuscreen_h;
218 g_layer_h = g_menuscreen_h;
219 printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult);
220 break;
221
222 case SCALE_FULLSCREEN:
223 g_layer_w = g_menuscreen_w;
224 g_layer_h = g_menuscreen_h;
225 break;
226
227 default:
228 break;
229 }
230
96867f0e 231 if (g_scaler != SCALE_CUSTOM) {
232 g_layer_x = g_menuscreen_w / 2 - g_layer_w / 2;
233 g_layer_y = g_menuscreen_h / 2 - g_layer_h / 2;
234 }
235 if (g_layer_w > g_menuscreen_w * 2) g_layer_w = g_menuscreen_w * 2;
236 if (g_layer_h > g_menuscreen_h * 2) g_layer_h = g_menuscreen_h * 2;
9e5ac38f 237}
238
fa56d360 239// XXX: this is platform specific really
7cfc0c66 240static inline int resolution_ok(int w, int h)
69af03a2 241{
fa56d360 242 return w <= 1024 && h <= 512;
243}
244
e4c83ca6 245static void pl_vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
fa56d360 246{
247 int vout_w, vout_h, vout_bpp;
0fed3dc3 248 int buf_yoffset = 0;
fa56d360 249
a185be70 250 // special h handling, Wipeout likes to change it by 1-6
321ca84d 251 static int vsync_cnt_ms_prev;
252 if ((unsigned int)(vsync_cnt - vsync_cnt_ms_prev) < 5*60)
253 h = (h + 7) & ~7;
254 vsync_cnt_ms_prev = vsync_cnt;
a185be70 255
e4c83ca6 256 psx_w = raw_w;
257 psx_h = raw_h;
468072a1 258 psx_bpp = bpp;
e4c83ca6 259 vout_w = w;
260 vout_h = h;
468072a1 261 vout_bpp = bpp;
262 if (pl_rearmed_cbs.only_16bpp)
263 vout_bpp = 16;
fa56d360 264
308c6e67 265 assert(vout_h >= 192);
0fed3dc3 266
7cfc0c66 267 pl_vout_scale_w = pl_vout_scale_h = 1;
fa56d360 268#ifdef __ARM_NEON__
269 if (soft_filter) {
270 if (resolution_ok(w * 2, h * 2) && bpp == 16) {
7cfc0c66 271 pl_vout_scale_w = 2;
272 pl_vout_scale_h = 2;
fa56d360 273 }
274 else {
275 // filter unavailable
276 hud_msg[0] = 0;
277 }
278 }
7cfc0c66 279 else if (scanlines != 0 && scanline_level != 100 && bpp == 16) {
280 if (h <= 256)
281 pl_vout_scale_h = 2;
282 }
fa56d360 283#endif
7cfc0c66 284 vout_w *= pl_vout_scale_w;
285 vout_h *= pl_vout_scale_h;
b60f2812 286
fa56d360 287 update_layer_size(vout_w, vout_h);
9e5ac38f 288
fa56d360 289 pl_vout_buf = plat_gvideo_set_mode(&vout_w, &vout_h, &vout_bpp);
c9099d02 290 if (pl_vout_buf == NULL && pl_plat_blit == NULL)
ddc0a02a 291 fprintf(stderr, "failed to set mode %dx%d@%d\n",
468072a1 292 vout_w, vout_h, vout_bpp);
fa56d360 293 else {
294 pl_vout_w = vout_w;
295 pl_vout_h = vout_h;
296 pl_vout_bpp = vout_bpp;
0fed3dc3 297 pl_vout_yoffset = buf_yoffset;
fa56d360 298 }
0fed3dc3 299 if (pl_vout_buf != NULL)
300 pl_vout_buf = (char *)pl_vout_buf
301 + pl_vout_yoffset * pl_vout_w * pl_vout_bpp / 8;
a185be70 302
ddc0a02a 303 menu_notify_mode_change(pl_vout_w, pl_vout_h, pl_vout_bpp);
69af03a2 304}
305
2da2fc76 306static int flip_clear_counter;
307
308void pl_force_clear(void)
309{
310 flip_clear_counter = 2;
311}
312
308c6e67 313static void pl_vout_flip(const void *vram, int stride, int bgr24,
314 int x, int y, int w, int h, int dims_changed)
69af03a2 315{
fa56d360 316 unsigned char *dest = pl_vout_buf;
317 const unsigned short *src = vram;
318 int dstride = pl_vout_w, h1 = h;
af486d6e 319 int h_full = pl_vout_h - pl_vout_yoffset;
308c6e67 320 int xoffs = 0, doffs;
fa56d360 321
cc56203b 322 pcnt_start(PCNT_BLIT);
323
fa56d360 324 if (vram == NULL) {
325 // blanking
c9099d02 326 if (pl_plat_clear)
327 pl_plat_clear();
328 else
329 memset(pl_vout_buf, 0,
af486d6e 330 dstride * h_full * pl_vout_bpp / 8);
c9099d02 331 goto out_hud;
fa56d360 332 }
333
308c6e67 334 assert(x + w <= pl_vout_w);
335 assert(y + h <= pl_vout_h);
336
337 // offset
338 xoffs = x * pl_vout_scale_w;
339 doffs = xoffs + y * dstride;
fa56d360 340
308c6e67 341 if (dims_changed)
42dde520 342 flip_clear_counter = 3;
fa56d360 343
2da2fc76 344 if (flip_clear_counter > 0) {
c9099d02 345 if (pl_plat_clear)
346 pl_plat_clear();
347 else
348 memset(pl_vout_buf, 0,
af486d6e 349 dstride * h_full * pl_vout_bpp / 8);
2da2fc76 350 flip_clear_counter--;
fa56d360 351 }
352
c9099d02 353 if (pl_plat_blit)
354 {
355 pl_plat_blit(doffs, src, w, h, stride, bgr24);
356 goto out_hud;
357 }
358
359 if (dest == NULL)
360 goto out;
361
362 dest += doffs * 2;
363
fa56d360 364 if (bgr24)
365 {
366 if (pl_rearmed_cbs.only_16bpp) {
367 for (; h1-- > 0; dest += dstride * 2, src += stride)
368 {
369 bgr888_to_rgb565(dest, src, w * 3);
370 }
371 }
372 else {
373 dest -= doffs * 2;
374 dest += (doffs / 8) * 24;
375
376 for (; h1-- > 0; dest += dstride * 3, src += stride)
377 {
378 bgr888_to_rgb888(dest, src, w * 3);
379 }
380 }
381 }
382#ifdef __ARM_NEON__
7cfc0c66 383 else if (soft_filter == SOFT_FILTER_SCALE2X && pl_vout_scale_w == 2)
fa56d360 384 {
385 neon_scale2x_16_16(src, (void *)dest, w,
c9099d02 386 stride * 2, dstride * 2, h);
fa56d360 387 }
7cfc0c66 388 else if (soft_filter == SOFT_FILTER_EAGLE2X && pl_vout_scale_w == 2)
fa56d360 389 {
390 neon_eagle2x_16_16(src, (void *)dest, w,
c9099d02 391 stride * 2, dstride * 2, h);
fa56d360 392 }
35d3fd2e 393 else if (scanlines != 0 && scanline_level != 100)
394 {
2f70bda7 395 int h2, l = scanline_level * 2048 / 100;
7cfc0c66 396 int stride_0 = pl_vout_scale_h >= 2 ? 0 : stride;
35d3fd2e 397
7cfc0c66 398 h1 *= pl_vout_scale_h;
2f70bda7 399 while (h1 > 0)
35d3fd2e 400 {
2f70bda7 401 for (h2 = scanlines; h2 > 0 && h1 > 0; h2--, h1--) {
402 bgr555_to_rgb565(dest, src, w * 2);
403 dest += dstride * 2, src += stride_0;
404 }
35d3fd2e 405
2f70bda7 406 for (h2 = scanlines; h2 > 0 && h1 > 0; h2--, h1--) {
407 bgr555_to_rgb565_b(dest, src, w * 2, l);
408 dest += dstride * 2, src += stride;
409 }
35d3fd2e 410 }
411 }
fa56d360 412#endif
413 else
414 {
415 for (; h1-- > 0; dest += dstride * 2, src += stride)
416 {
417 bgr555_to_rgb565(dest, src, w * 2);
418 }
419 }
8f892648 420
c9099d02 421out_hud:
308c6e67 422 print_hud(xoffs, w * pl_vout_scale_w, (y + h) * pl_vout_scale_h);
72228559 423
fa56d360 424out:
cc56203b 425 pcnt_end(PCNT_BLIT);
426
69af03a2 427 // let's flip now
6469a8c4 428 pl_vout_buf = plat_gvideo_flip();
0fed3dc3 429 if (pl_vout_buf != NULL)
430 pl_vout_buf = (char *)pl_vout_buf
431 + pl_vout_yoffset * pl_vout_w * pl_vout_bpp / 8;
432
fa56d360 433 pl_rearmed_cbs.flip_cnt++;
b60f2812 434}
435
76f7048e 436static int pl_vout_open(void)
6d1a1ac2 437{
cefe86b7 438 struct timeval now;
439
fa56d360 440 // force mode update on pl_vout_set_mode() call from gpulib/vout_pl
441 pl_vout_buf = NULL;
cefe86b7 442
ab423939 443 plat_gvideo_open(is_pal);
76f7048e 444
cefe86b7 445 gettimeofday(&now, 0);
446 vsync_usec_time = now.tv_usec;
76f7048e 447 while (vsync_usec_time >= frame_interval)
448 vsync_usec_time -= frame_interval;
cefe86b7 449
6d1a1ac2 450 return 0;
451}
452
76f7048e 453static void pl_vout_close(void)
b60f2812 454{
6469a8c4 455 plat_gvideo_close();
b60f2812 456}
457
fa56d360 458static void pl_set_gpu_caps(int caps)
459{
460 pl_rearmed_cbs.gpu_caps = caps;
461}
462
29a8c4f3 463void *pl_prepare_screenshot(int *w, int *h, int *bpp)
464{
6469a8c4 465 void *ret = plat_prepare_screenshot(w, h, bpp);
466 if (ret != NULL)
467 return ret;
468
76f7048e 469 *w = pl_vout_w;
470 *h = pl_vout_h;
471 *bpp = pl_vout_bpp;
29a8c4f3 472
76f7048e 473 return pl_vout_buf;
29a8c4f3 474}
475
fa56d360 476/* display/redering mode switcher */
477static int dispmode_default(void)
478{
479 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
480 soft_filter = SOFT_FILTER_NONE;
481 snprintf(hud_msg, sizeof(hud_msg), "default mode");
482 return 1;
483}
484
57467c77 485#ifdef BUILTIN_GPU_NEON
7cfc0c66 486static int dispmode_doubleres(void)
fa56d360 487{
488 if (!(pl_rearmed_cbs.gpu_caps & GPU_CAP_SUPPORTS_2X)
489 || !resolution_ok(psx_w * 2, psx_h * 2) || psx_bpp != 16)
490 return 0;
491
492 dispmode_default();
493 pl_rearmed_cbs.gpu_neon.enhancement_enable = 1;
494 snprintf(hud_msg, sizeof(hud_msg), "double resolution");
495 return 1;
496}
57467c77 497#endif
fa56d360 498
57467c77 499#ifdef __ARM_NEON__
7cfc0c66 500static int dispmode_scale2x(void)
fa56d360 501{
e4c83ca6 502 if (!resolution_ok(psx_w * 2, psx_h * 2) || psx_bpp != 16)
fa56d360 503 return 0;
504
505 dispmode_default();
506 soft_filter = SOFT_FILTER_SCALE2X;
507 snprintf(hud_msg, sizeof(hud_msg), "scale2x");
508 return 1;
509}
510
7cfc0c66 511static int dispmode_eagle2x(void)
fa56d360 512{
e4c83ca6 513 if (!resolution_ok(psx_w * 2, psx_h * 2) || psx_bpp != 16)
fa56d360 514 return 0;
515
516 dispmode_default();
517 soft_filter = SOFT_FILTER_EAGLE2X;
518 snprintf(hud_msg, sizeof(hud_msg), "eagle2x");
519 return 1;
520}
7cfc0c66 521#endif
fa56d360 522
523static int (*dispmode_switchers[])(void) = {
524 dispmode_default,
57467c77 525#ifdef BUILTIN_GPU_NEON
fa56d360 526 dispmode_doubleres,
57467c77 527#endif
528#ifdef __ARM_NEON__
fa56d360 529 dispmode_scale2x,
530 dispmode_eagle2x,
531#endif
532};
533
534static int dispmode_current;
535
536void pl_switch_dispmode(void)
537{
538 if (pl_rearmed_cbs.gpu_caps & GPU_CAP_OWNS_DISPLAY)
539 return;
540
541 while (1) {
542 dispmode_current++;
543 if (dispmode_current >=
544 sizeof(dispmode_switchers) / sizeof(dispmode_switchers[0]))
545 dispmode_current = 0;
546 if (dispmode_switchers[dispmode_current]())
547 break;
548 }
549}
550
1b0c5139 551#ifndef MAEMO
2c5d0a55 552/* adjust circle-like analog inputs to better match
553 * more square-like analogs in PSX */
554static void update_analog_nub_adjust(int *x_, int *y_)
555{
e4c83ca6 556 #define d 16
cc8ae53e 557 static const int scale[] =
558 { 0 - d*2, 0 - d*2, 0 - d*2, 12 - d*2,
559 30 - d*2, 60 - d*2, 75 - d*2, 60 - d*2, 60 - d*2 };
560 int x = abs(*x_);
561 int y = abs(*y_);
562 int scale_x = scale[y / 16];
563 int scale_y = scale[x / 16];
564
565 if (x) {
566 x += d + (x * scale_x >> 8);
567 if (*x_ < 0)
568 x = -x;
569 }
570 if (y) {
571 y += d + (y * scale_y >> 8);
572 if (*y_ < 0)
573 y = -y;
574 }
2c5d0a55 575
576 *x_ = x;
577 *y_ = y;
e4c83ca6 578 #undef d
2c5d0a55 579}
580
1b0c5139 581static void update_analogs(void)
582{
7a8d521f 583 int *nubp[2] = { in_analog_left[0], in_analog_right[0] };
2c5d0a55 584 int vals[2];
1b0c5139 585 int i, a, v, ret;
586
587 for (i = 0; i < 2; i++)
588 {
589 if (in_adev[i] < 0)
590 continue;
591
592 for (a = 0; a < 2; a++) {
2c5d0a55 593 vals[a] = 0;
1b0c5139 594
595 ret = in_update_analog(in_adev[i], in_adev_axis[i][a], &v);
2c5d0a55 596 if (ret == 0)
597 vals[a] = 128 * v / IN_ABS_RANGE;
1b0c5139 598 }
2c5d0a55 599
600 if (in_adev_is_nublike[i])
601 update_analog_nub_adjust(&vals[0], &vals[1]);
602
603 for (a = 0; a < 2; a++) {
604 v = vals[a] + 127;
605 if (v < 0) v = 0;
606 else if (v > 255) v = 255;
607 nubp[i][a] = v;
608 }
609
1b0c5139 610 }
1b0c5139 611}
612
15d46930 613static void update_input(void)
614{
615 int actions[IN_BINDTYPE_COUNT] = { 0, };
8f892648 616 unsigned int emu_act;
fb640dd9 617 int in_state_gun;
618 int i;
15d46930 619
620 in_update(actions);
7a8d521f 621 if (in_type[0] == PSE_PAD_TYPE_ANALOGJOY || in_type[0] == PSE_PAD_TYPE_ANALOGPAD)
1b0c5139 622 update_analogs();
8f892648 623 emu_act = actions[IN_BINDTYPE_EMU];
fb640dd9 624 in_state_gun = emu_act & SACTION_GUN_MASK;
4c08b9e7 625
626 emu_act &= ~SACTION_GUN_MASK;
8f892648 627 if (emu_act) {
628 int which = 0;
629 for (; !(emu_act & 1); emu_act >>= 1, which++)
630 ;
631 emu_act = which;
632 }
633 emu_set_action(emu_act);
634
fb640dd9 635 in_keystate[0] = actions[IN_BINDTYPE_PLAYER12] & 0xffff;
636 in_keystate[1] = (actions[IN_BINDTYPE_PLAYER12] >> 16) & 0xffff;
2db412ad 637
fb640dd9 638 if (tsdev) for (i = 0; i < 2; i++) {
639 int in = 0, x = 0, y = 0, trigger;;
640 if (in_type[i] != PSE_PAD_TYPE_GUN
641 && in_type[i] != PSE_PAD_TYPE_GUNCON)
642 continue;
643 trigger = in_type[i] == PSE_PAD_TYPE_GUN
644 ? (1 << DKEY_SQUARE) : (1 << DKEY_CIRCLE);
645
646 pl_gun_ts_update(tsdev, &x, &y, &in);
647 in_analog_left[i][0] = 65536;
648 in_analog_left[i][1] = 65536;
649 if (in && !(in_state_gun & (1 << SACTION_GUN_TRIGGER2))) {
650 in_analog_left[i][0] = x;
651 in_analog_left[i][1] = y;
652 if (!(g_opts & OPT_TSGUN_NOTRIGGER))
653 in_state_gun |= (1 << SACTION_GUN_TRIGGER);
654 }
655 in_keystate[i] = 0;
656 if (in_state_gun & ((1 << SACTION_GUN_TRIGGER)
657 | (1 << SACTION_GUN_TRIGGER2)))
658 in_keystate[i] |= trigger;
659 if (in_state_gun & (1 << SACTION_GUN_A))
660 in_keystate[i] |= (1 << DKEY_START);
661 if (in_state_gun & (1 << SACTION_GUN_B))
662 in_keystate[i] |= (1 << DKEY_CROSS);
663 }
15d46930 664}
1b0c5139 665#else /* MAEMO */
7010034e 666extern void update_input(void);
1b0c5139 667#endif
15d46930 668
2db412ad 669void pl_gun_byte2(int port, unsigned char byte)
4c08b9e7 670{
fb640dd9 671 if (!tsdev || in_type[port] != PSE_PAD_TYPE_GUN || !(byte & 0x10))
672 return;
673 if (in_analog_left[port][0] == 65536)
674 return;
675
676 psxScheduleIrq10(4, in_analog_left[port][0] * 1629 / 1024,
677 in_analog_left[port][1] * psx_h / 1024);
4c08b9e7 678}
679
bce6b056 680#define MAX_LAG_FRAMES 3
681
682#define tvdiff(tv, tv_old) \
683 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
bce6b056 684
72228559 685/* called on every vsync */
686void pl_frame_limit(void)
687{
bce6b056 688 static struct timeval tv_old, tv_expect;
2f546f9a 689 static int vsync_cnt_prev, drc_active_vsyncs;
bce6b056 690 struct timeval now;
cefe86b7 691 int diff, usadj;
bce6b056 692
69e482e3 693 if (g_emu_resetting)
81edd2b3 694 return;
695
bce6b056 696 vsync_cnt++;
72228559 697
15d46930 698 /* doing input here because the pad is polled
699 * thousands of times per frame for some reason */
700 update_input();
701
72228559 702 pcnt_end(PCNT_ALL);
bce6b056 703 gettimeofday(&now, 0);
72228559 704
bce6b056 705 if (now.tv_sec != tv_old.tv_sec) {
706 diff = tvdiff(now, tv_old);
a92f6af1 707 pl_rearmed_cbs.vsps_cur = 0.0f;
bce6b056 708 if (0 < diff && diff < 2000000)
a92f6af1 709 pl_rearmed_cbs.vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
7c0f51de 710 vsync_cnt_prev = vsync_cnt;
a92f6af1 711
712 if (g_opts & OPT_SHOWFPS)
713 pl_rearmed_cbs.flips_per_sec = pl_rearmed_cbs.flip_cnt;
714 pl_rearmed_cbs.flip_cnt = 0;
bd6267e6 715 if (g_opts & OPT_SHOWCPU)
a92f6af1 716 pl_rearmed_cbs.cpu_usage = get_cpu_ticks();
8f892648 717
718 if (hud_new_msg > 0) {
719 hud_new_msg--;
720 if (hud_new_msg == 0)
721 hud_msg[0] = 0;
722 }
a92f6af1 723 tv_old = now;
ece032e6 724 //new_dynarec_print_stats();
72228559 725 }
726#ifdef PCNT
727 static int ya_vsync_count;
728 if (++ya_vsync_count == PCNT_FRAMES) {
a92f6af1 729 pcnt_print(pl_rearmed_cbs.vsps_cur);
72228559 730 ya_vsync_count = 0;
731 }
732#endif
733
76f7048e 734 // tv_expect uses usec*1024 units instead of usecs for better accuracy
735 tv_expect.tv_usec += frame_interval1024;
736 if (tv_expect.tv_usec >= (1000000 << 10)) {
737 tv_expect.tv_usec -= (1000000 << 10);
738 tv_expect.tv_sec++;
739 }
740 diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 + (tv_expect.tv_usec >> 10) - now.tv_usec;
741
742 if (diff > MAX_LAG_FRAMES * frame_interval || diff < -MAX_LAG_FRAMES * frame_interval) {
743 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, frame_interval);
c89cd762 744 tv_expect = now;
745 diff = 0;
cefe86b7 746 // try to align with vsync
747 usadj = vsync_usec_time;
76f7048e 748 while (usadj < tv_expect.tv_usec - frame_interval)
749 usadj += frame_interval;
750 tv_expect.tv_usec = usadj << 10;
c89cd762 751 }
752
76f7048e 753 if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) {
c89cd762 754 // yay for working usleep on pandora!
76f7048e 755 //printf("usleep %d\n", diff - frame_interval / 2);
47f37583 756 usleep(diff - frame_interval);
c89cd762 757 }
758
e64dc4c5 759 if (pl_rearmed_cbs.frameskip) {
76f7048e 760 if (diff < -frame_interval)
e64dc4c5 761 pl_rearmed_cbs.fskip_advice = 1;
762 else if (diff >= 0)
763 pl_rearmed_cbs.fskip_advice = 0;
2f546f9a 764
765 // recompilation is not that fast and may cause frame skip on
766 // loading screens and such, resulting in flicker or glitches
767 if (new_dynarec_did_compile) {
768 if (drc_active_vsyncs < 32)
769 pl_rearmed_cbs.fskip_advice = 0;
770 drc_active_vsyncs++;
771 }
772 else
773 drc_active_vsyncs = 0;
774 new_dynarec_did_compile = 0;
bce6b056 775 }
72228559 776
777 pcnt_start(PCNT_ALL);
778}
779
ab423939 780void pl_timing_prepare(int is_pal_)
76f7048e 781{
4c8f1c25 782 double fps;
76f7048e 783 pl_rearmed_cbs.fskip_advice = 0;
a92f6af1 784 pl_rearmed_cbs.flips_per_sec = 0;
785 pl_rearmed_cbs.cpu_usage = 0;
76f7048e 786
ab423939 787 is_pal = is_pal_;
4c8f1c25 788 fps = psxGetFps();
789 frame_interval = (int)(1000000.0 / fps);
790 frame_interval1024 = (int)(1000000.0 * 1024.0 / fps);
76f7048e 791
792 // used by P.E.Op.S. frameskip code
4c8f1c25 793 pl_rearmed_cbs.gpu_peops.fFrameRateHz = (float)fps;
76f7048e 794 pl_rearmed_cbs.gpu_peops.dwFrameRateTicks =
4c8f1c25 795 (100000*100 / (int)(pl_rearmed_cbs.gpu_peops.fFrameRateHz*100));
76f7048e 796}
797
201c21e2 798static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
799{
800 *x = g_layer_x;
801 *y = g_layer_y;
802 *w = g_layer_w;
803 *h = g_layer_h;
804}
805
87e5b45f 806static void *pl_mmap(unsigned int size);
807static void pl_munmap(void *ptr, unsigned int size);
9ee0fd5b 808
e64dc4c5 809struct rearmed_cbs pl_rearmed_cbs = {
201c21e2 810 pl_get_layer_pos,
76f7048e 811 pl_vout_open,
812 pl_vout_set_mode,
813 pl_vout_flip,
814 pl_vout_close,
fa56d360 815
816 .mmap = pl_mmap,
817 .munmap = pl_munmap,
818 .pl_set_gpu_caps = pl_set_gpu_caps,
abf09485 819 .gpu_state_change = gpu_state_change,
201c21e2 820};
821
7c0f51de 822/* watchdog */
823static void *watchdog_thread(void *unused)
824{
825 int vsync_cnt_old = 0;
826 int seen_dead = 0;
827 int sleep_time = 5;
828
d3f3bf09 829#if !defined(NDEBUG) || defined(DRC_DBG)
799b0b87 830 // don't interfere with debug
831 return NULL;
832#endif
7c0f51de 833 while (1)
834 {
835 sleep(sleep_time);
836
837 if (stop) {
838 seen_dead = 0;
839 sleep_time = 5;
840 continue;
841 }
842 if (vsync_cnt != vsync_cnt_old) {
843 vsync_cnt_old = vsync_cnt;
844 seen_dead = 0;
845 sleep_time = 2;
846 continue;
847 }
848
849 seen_dead++;
850 sleep_time = 1;
851 if (seen_dead > 1)
852 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
853 if (seen_dead > 4) {
854 fprintf(stderr, "watchdog: lockup detected, aborting\n");
855 // we can't do any cleanup here really, the main thread is
856 // likely touching resources and would crash anyway
857 abort();
858 }
859 }
860}
861
862void pl_start_watchdog(void)
863{
864 pthread_attr_t attr;
865 pthread_t tid;
866 int ret;
867
868 pthread_attr_init(&attr);
869 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
870
871 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
872 if (ret != 0)
873 fprintf(stderr, "could not start watchdog: %d\n", ret);
874}
875
87e5b45f 876static void *pl_emu_mmap(unsigned long addr, size_t size, int is_fixed,
877 enum psxMapTag tag)
878{
879 return plat_mmap(addr, size, 0, is_fixed);
880}
881
882static void pl_emu_munmap(void *ptr, size_t size, enum psxMapTag tag)
883{
884 plat_munmap(ptr, size);
885}
886
887static void *pl_mmap(unsigned int size)
888{
889 return psxMapHook(0, size, 0, MAP_TAG_VRAM);
890}
891
892static void pl_munmap(void *ptr, unsigned int size)
893{
894 psxUnmapHook(ptr, size, MAP_TAG_VRAM);
895}
896
4c08b9e7 897void pl_init(void)
898{
24de2dd4 899 extern unsigned int hSyncCount; // from psxcounters
3ece2f0c 900 extern unsigned int frame_counter;
24de2dd4 901
ddc0a02a 902 psx_w = psx_h = pl_vout_w = pl_vout_h = 256;
903 psx_bpp = pl_vout_bpp = 16;
321ca84d 904
faf2b2aa 905 tsdev = pl_gun_ts_init();
24de2dd4 906
907 pl_rearmed_cbs.gpu_hcnt = &hSyncCount;
3ece2f0c 908 pl_rearmed_cbs.gpu_frame_count = &frame_counter;
87e5b45f 909
910 psxMapHook = pl_emu_mmap;
911 psxUnmapHook = pl_emu_munmap;
4c08b9e7 912}