gpu_neon: handle 0xEx cmds while frameskiping
[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 "plugin_lib.h"
21#include "linux/fbdev.h"
22#include "common/fonts.h"
23#include "common/input.h"
24#include "omap.h"
25#include "menu.h"
26#include "main.h"
27#include "pcnt.h"
28#include "pl_gun_ts.h"
29#include "../libpcsxcore/new_dynarec/new_dynarec.h"
30#include "../libpcsxcore/psemu_plugin_defs.h"
31
32int in_type1, in_type2;
33int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
34int in_keystate, in_state_gun;
35static void *ts;
36void *pl_vout_buf;
37static int pl_vout_w, pl_vout_h, pl_vout_bpp;
38static int flip_cnt, vsync_cnt, flips_per_sec, tick_per_sec;
39static float vsps_cur;
40static int frame_interval, frame_interval1024, vsync_usec_time;
41
42
43static __attribute__((noinline)) int get_cpu_ticks(void)
44{
45 static unsigned long last_utime;
46 static int fd;
47 unsigned long utime, ret;
48 char buf[128];
49
50 if (fd == 0)
51 fd = open("/proc/self/stat", O_RDONLY);
52 lseek(fd, 0, SEEK_SET);
53 buf[0] = 0;
54 read(fd, buf, sizeof(buf));
55 buf[sizeof(buf) - 1] = 0;
56
57 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
58 ret = utime - last_utime;
59 last_utime = utime;
60 return ret;
61}
62
63static void print_hud(void)
64{
65 if (pl_vout_bpp == 16)
66 pl_text_out16(2, pl_vout_h - 10, "%s", hud_msg);
67}
68
69static void print_fps(void)
70{
71 if (pl_vout_bpp == 16)
72 pl_text_out16(2, pl_vout_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
73}
74
75static void print_cpu_usage(void)
76{
77 if (pl_vout_bpp == 16)
78 pl_text_out16(pl_vout_w - 28, pl_vout_h - 10, "%3d", tick_per_sec);
79}
80
81// draw 192x8 status of 24 sound channels
82static __attribute__((noinline)) void draw_active_chans(void)
83{
84 extern void spu_get_debug_info(int *chans_out, int *fmod_chans_out, int *noise_chans_out); // hack
85 int live_chans, fmod_chans, noise_chans;
86
87 static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
88 unsigned short *dest = (unsigned short *)pl_vout_buf +
89 pl_vout_w * (pl_vout_h - 10) + pl_vout_w / 2 - 192/2;
90 unsigned short *d, p;
91 int c, x, y;
92
93 if (pl_vout_bpp != 16)
94 return;
95
96 spu_get_debug_info(&live_chans, &fmod_chans, &noise_chans);
97
98 for (c = 0; c < 24; c++) {
99 d = dest + c * 8;
100 p = !(live_chans & (1<<c)) ? 0 :
101 (fmod_chans & (1<<c)) ? 0xf000 :
102 (noise_chans & (1<<c)) ? 0x001f :
103 colors[c & 1];
104 for (y = 0; y < 8; y++, d += pl_vout_w)
105 for (x = 0; x < 8; x++)
106 d[x] = p;
107 }
108}
109
110static void *pl_vout_set_mode(int w, int h, int bpp)
111{
112 if (w == pl_vout_w && h == pl_vout_h && bpp == pl_vout_bpp)
113 return pl_vout_buf;
114
115 pl_vout_w = w;
116 pl_vout_h = h;
117 pl_vout_bpp = bpp;
118
119#if defined(VOUT_FBDEV)
120 vout_fbdev_clear(layer_fb);
121 pl_vout_buf = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
122#elif defined(MAEMO)
123 extern void *hildon_set_mode(int w, int h);
124 pl_vout_buf = hildon_set_mode(w, h);
125#endif
126
127 if (pl_vout_buf == NULL)
128 fprintf(stderr, "failed to set mode\n");
129 menu_notify_mode_change(w, h, bpp);
130
131 return pl_vout_buf;
132}
133
134static void *pl_vout_flip(void)
135{
136 flip_cnt++;
137
138 if (pl_vout_buf != NULL) {
139 if (g_opts & OPT_SHOWSPU)
140 draw_active_chans();
141
142 if (hud_msg[0] != 0)
143 print_hud();
144 else if (g_opts & OPT_SHOWFPS)
145 print_fps();
146
147 if (g_opts & OPT_SHOWCPU)
148 print_cpu_usage();
149 }
150
151 // let's flip now
152#if defined(VOUT_FBDEV)
153 pl_vout_buf = vout_fbdev_flip(layer_fb);
154#elif defined(MAEMO)
155 extern void *hildon_flip(void);
156 pl_vout_buf = hildon_flip();
157#endif
158 return pl_vout_buf;
159}
160
161static int pl_vout_open(void)
162{
163 struct timeval now;
164
165 omap_enable_layer(1);
166#if defined(VOUT_FBDEV)
167 pl_vout_buf = vout_fbdev_flip(layer_fb);
168
169 // try to align redraws to vsync
170 vout_fbdev_wait_vsync(layer_fb);
171#elif defined(MAEMO)
172 extern void *hildon_flip(void);
173 pl_vout_buf = hildon_flip();
174#endif
175
176 gettimeofday(&now, 0);
177 vsync_usec_time = now.tv_usec;
178 while (vsync_usec_time >= frame_interval)
179 vsync_usec_time -= frame_interval;
180
181 return 0;
182}
183
184static void pl_vout_close(void)
185{
186 omap_enable_layer(0);
187}
188
189void *pl_prepare_screenshot(int *w, int *h, int *bpp)
190{
191 *w = pl_vout_w;
192 *h = pl_vout_h;
193 *bpp = pl_vout_bpp;
194
195 return pl_vout_buf;
196}
197
198static void update_input(void)
199{
200#ifndef MAEMO
201 int actions[IN_BINDTYPE_COUNT] = { 0, };
202 unsigned int emu_act;
203
204 in_update(actions);
205 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD)
206 in_update_analogs();
207 emu_act = actions[IN_BINDTYPE_EMU];
208 in_state_gun = (emu_act & SACTION_GUN_MASK) >> SACTION_GUN_TRIGGER;
209
210 emu_act &= ~SACTION_GUN_MASK;
211 if (emu_act) {
212 int which = 0;
213 for (; !(emu_act & 1); emu_act >>= 1, which++)
214 ;
215 emu_act = which;
216 }
217 emu_set_action(emu_act);
218
219 in_keystate = actions[IN_BINDTYPE_PLAYER12];
220#endif
221#ifdef X11
222 extern int x11_update_keys(unsigned int *action);
223 in_keystate |= x11_update_keys(&emu_act);
224 emu_set_action(emu_act);
225#endif
226}
227
228void pl_update_gun(int *xn, int *xres, int *y, int *in)
229{
230 if (ts)
231 pl_gun_ts_update(ts, xn, y, in);
232
233 *xres = pl_vout_w;
234 *y = *y * pl_vout_h >> 10;
235}
236
237#define MAX_LAG_FRAMES 3
238
239#define tvdiff(tv, tv_old) \
240 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
241
242/* called on every vsync */
243void pl_frame_limit(void)
244{
245 static struct timeval tv_old, tv_expect;
246 static int vsync_cnt_prev;
247 struct timeval now;
248 int diff, usadj;
249
250 vsync_cnt++;
251
252 /* doing input here because the pad is polled
253 * thousands of times per frame for some reason */
254 update_input();
255
256 pcnt_end(PCNT_ALL);
257 gettimeofday(&now, 0);
258
259 if (now.tv_sec != tv_old.tv_sec) {
260 diff = tvdiff(now, tv_old);
261 vsps_cur = 0.0f;
262 if (0 < diff && diff < 2000000)
263 vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
264 vsync_cnt_prev = vsync_cnt;
265 flips_per_sec = flip_cnt;
266 flip_cnt = 0;
267 tv_old = now;
268 if (g_opts & OPT_SHOWCPU)
269 tick_per_sec = get_cpu_ticks();
270
271 if (hud_new_msg > 0) {
272 hud_new_msg--;
273 if (hud_new_msg == 0)
274 hud_msg[0] = 0;
275 }
276 }
277#ifdef PCNT
278 static int ya_vsync_count;
279 if (++ya_vsync_count == PCNT_FRAMES) {
280 pcnt_print(vsps_cur);
281 ya_vsync_count = 0;
282 }
283#endif
284
285 // tv_expect uses usec*1024 units instead of usecs for better accuracy
286 tv_expect.tv_usec += frame_interval1024;
287 if (tv_expect.tv_usec >= (1000000 << 10)) {
288 tv_expect.tv_usec -= (1000000 << 10);
289 tv_expect.tv_sec++;
290 }
291 diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 + (tv_expect.tv_usec >> 10) - now.tv_usec;
292
293 if (diff > MAX_LAG_FRAMES * frame_interval || diff < -MAX_LAG_FRAMES * frame_interval) {
294 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, frame_interval);
295 tv_expect = now;
296 diff = 0;
297 // try to align with vsync
298 usadj = vsync_usec_time;
299 while (usadj < tv_expect.tv_usec - frame_interval)
300 usadj += frame_interval;
301 tv_expect.tv_usec = usadj << 10;
302 }
303
304 if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) {
305 // yay for working usleep on pandora!
306 //printf("usleep %d\n", diff - frame_interval / 2);
307 usleep(diff - frame_interval / 2);
308 }
309
310 if (pl_rearmed_cbs.frameskip) {
311 if (diff < -frame_interval)
312 pl_rearmed_cbs.fskip_advice = 1;
313 else if (diff >= 0)
314 pl_rearmed_cbs.fskip_advice = 0;
315 }
316
317 pcnt_start(PCNT_ALL);
318}
319
320void pl_timing_prepare(int is_pal)
321{
322 pl_rearmed_cbs.fskip_advice = 0;
323
324 frame_interval = is_pal ? 20000 : 16667;
325 frame_interval1024 = is_pal ? 20000*1024 : 17066667;
326
327 // used by P.E.Op.S. frameskip code
328 pl_rearmed_cbs.gpu_peops.fFrameRateHz = is_pal ? 50.0f : 59.94f;
329 pl_rearmed_cbs.gpu_peops.dwFrameRateTicks =
330 (100000*100 / (unsigned long)(pl_rearmed_cbs.gpu_peops.fFrameRateHz*100));
331}
332
333static void pl_text_out16_(int x, int y, const char *text)
334{
335 int i, l, len = strlen(text), w = pl_vout_w;
336 unsigned short *screen = (unsigned short *)pl_vout_buf + x + y * w;
337 unsigned short val = 0xffff;
338
339 for (i = 0; i < len; i++, screen += 8)
340 {
341 for (l = 0; l < 8; l++)
342 {
343 unsigned char fd = fontdata8x8[text[i] * 8 + l];
344 unsigned short *s = screen + l * w;
345 if (fd&0x80) s[0] = val;
346 if (fd&0x40) s[1] = val;
347 if (fd&0x20) s[2] = val;
348 if (fd&0x10) s[3] = val;
349 if (fd&0x08) s[4] = val;
350 if (fd&0x04) s[5] = val;
351 if (fd&0x02) s[6] = val;
352 if (fd&0x01) s[7] = val;
353 }
354 }
355}
356
357void pl_text_out16(int x, int y, const char *texto, ...)
358{
359 va_list args;
360 char buffer[256];
361
362 va_start(args, texto);
363 vsnprintf(buffer, sizeof(buffer), texto, args);
364 va_end(args);
365
366 pl_text_out16_(x, y, buffer);
367}
368
369static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
370{
371 *x = g_layer_x;
372 *y = g_layer_y;
373 *w = g_layer_w;
374 *h = g_layer_h;
375}
376
377struct rearmed_cbs pl_rearmed_cbs = {
378 pl_get_layer_pos,
379 pl_vout_open,
380 pl_vout_set_mode,
381 pl_vout_flip,
382 pl_vout_close,
383};
384
385/* watchdog */
386static void *watchdog_thread(void *unused)
387{
388 int vsync_cnt_old = 0;
389 int seen_dead = 0;
390 int sleep_time = 5;
391
392#ifndef NDEBUG
393 // don't interfere with debug
394 return NULL;
395#endif
396 while (1)
397 {
398 sleep(sleep_time);
399
400 if (stop) {
401 seen_dead = 0;
402 sleep_time = 5;
403 continue;
404 }
405 if (vsync_cnt != vsync_cnt_old) {
406 vsync_cnt_old = vsync_cnt;
407 seen_dead = 0;
408 sleep_time = 2;
409 continue;
410 }
411
412 seen_dead++;
413 sleep_time = 1;
414 if (seen_dead > 1)
415 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
416 if (seen_dead > 4) {
417 fprintf(stderr, "watchdog: lockup detected, aborting\n");
418 // we can't do any cleanup here really, the main thread is
419 // likely touching resources and would crash anyway
420 abort();
421 }
422 }
423}
424
425void pl_start_watchdog(void)
426{
427 pthread_attr_t attr;
428 pthread_t tid;
429 int ret;
430
431 pthread_attr_init(&attr);
432 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
433
434 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
435 if (ret != 0)
436 fprintf(stderr, "could not start watchdog: %d\n", ret);
437}
438
439void pl_init(void)
440{
441 ts = pl_gun_ts_init();
442}