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