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