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