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