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