drc: merge part of old Ari64's patch: 09_tlb_offset
[pcsx_rearmed.git] / frontend / plugin_lib.c
CommitLineData
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"
69af03a2 28#include "../libpcsxcore/new_dynarec/new_dynarec.h"
799b0b87 29#include "../libpcsxcore/psemu_plugin_defs.h"
b60f2812 30
b60f2812 31void *pl_fbdev_buf;
bce6b056 32int pl_frame_interval;
3e215238 33int in_type, in_keystate, in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
72228559 34static int pl_fbdev_w, pl_fbdev_h, pl_fbdev_bpp;
bce6b056 35static int flip_cnt, vsync_cnt, flips_per_sec, tick_per_sec;
36static float vsps_cur;
cefe86b7 37static int vsync_usec_time;
72228559 38
39static int get_cpu_ticks(void)
40{
41 static unsigned long last_utime;
42 static int fd;
43 unsigned long utime, ret;
44 char buf[128];
45
46 if (fd == 0)
47 fd = open("/proc/self/stat", O_RDONLY);
48 lseek(fd, 0, SEEK_SET);
49 buf[0] = 0;
50 read(fd, buf, sizeof(buf));
51 buf[sizeof(buf) - 1] = 0;
52
53 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
54 ret = utime - last_utime;
55 last_utime = utime;
56 return ret;
57}
58
8f892648 59static void print_hud(void)
60{
61 if (pl_fbdev_bpp == 16)
62 pl_text_out16(2, pl_fbdev_h - 10, "%s", hud_msg);
63}
64
72228559 65static void print_fps(void)
66{
67 if (pl_fbdev_bpp == 16)
bce6b056 68 pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
72228559 69}
70
71static void print_cpu_usage(void)
72{
73 if (pl_fbdev_bpp == 16)
74 pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec);
75}
b60f2812 76
297b3d63 77void *pl_fbdev_set_mode(int w, int h, int bpp)
69af03a2 78{
d352cde2 79 void *ret;
b60f2812 80
72228559 81 if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp)
7947ab55 82 return pl_fbdev_buf;
72228559 83
69af03a2 84 pl_fbdev_w = w;
72228559 85 pl_fbdev_h = h;
86 pl_fbdev_bpp = bpp;
d352cde2 87
88 vout_fbdev_clear(layer_fb);
69af03a2 89 ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
d352cde2 90 if (ret == NULL)
69af03a2 91 fprintf(stderr, "failed to set mode\n");
d352cde2 92 else
93 pl_fbdev_buf = ret;
94
bd6267e6 95 menu_notify_mode_change(w, h, bpp);
3c70c47b 96
297b3d63 97 return pl_fbdev_buf;
69af03a2 98}
99
ffd0d743 100void *pl_fbdev_flip(void)
69af03a2 101{
72228559 102 flip_cnt++;
297b3d63 103
104 if (pl_fbdev_buf != NULL) {
8f892648 105 if (hud_msg[0] != 0)
106 print_hud();
107 else if (g_opts & OPT_SHOWFPS)
297b3d63 108 print_fps();
8f892648 109
297b3d63 110 if (g_opts & OPT_SHOWCPU)
111 print_cpu_usage();
112 }
72228559 113
69af03a2 114 // let's flip now
115 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
ffd0d743 116 return pl_fbdev_buf;
b60f2812 117}
118
6d1a1ac2 119int pl_fbdev_open(void)
120{
cefe86b7 121 struct timeval now;
122
6d1a1ac2 123 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
124 omap_enable_layer(1);
cefe86b7 125
126 // try to align redraws to vsync
127 vout_fbdev_wait_vsync(layer_fb);
128 gettimeofday(&now, 0);
129 vsync_usec_time = now.tv_usec;
130 while (vsync_usec_time >= pl_frame_interval)
131 vsync_usec_time -= pl_frame_interval;
132
6d1a1ac2 133 return 0;
134}
135
136void pl_fbdev_close(void)
b60f2812 137{
6d1a1ac2 138 omap_enable_layer(0);
b60f2812 139}
140
29a8c4f3 141void *pl_prepare_screenshot(int *w, int *h, int *bpp)
142{
143 *w = pl_fbdev_w;
144 *h = pl_fbdev_h;
145 *bpp = pl_fbdev_bpp;
146
147 return pl_fbdev_buf;
148}
149
15d46930 150static void update_input(void)
151{
152 int actions[IN_BINDTYPE_COUNT] = { 0, };
8f892648 153 unsigned int emu_act;
15d46930 154
155 in_update(actions);
799b0b87 156 if (in_type == PSE_PAD_TYPE_ANALOGPAD)
157 in_update_analogs();
8f892648 158 emu_act = actions[IN_BINDTYPE_EMU];
159 if (emu_act) {
160 int which = 0;
161 for (; !(emu_act & 1); emu_act >>= 1, which++)
162 ;
163 emu_act = which;
164 }
165 emu_set_action(emu_act);
166
799b0b87 167 in_keystate = actions[IN_BINDTYPE_PLAYER12];
447783f8 168
169#ifdef X11
4218954f 170 extern int x11_update_keys(unsigned int *action);
171 in_keystate |= x11_update_keys(&emu_act);
172 emu_set_action(emu_act);
447783f8 173#endif
15d46930 174}
175
bce6b056 176#define MAX_LAG_FRAMES 3
177
178#define tvdiff(tv, tv_old) \
179 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
180// assumes us < 1000000
181#define tvadd(tv, us) { \
182 tv.tv_usec += us; \
183 if (tv.tv_usec >= 1000000) { \
184 tv.tv_usec -= 1000000; \
185 tv.tv_sec++; \
186 } \
187}
188
72228559 189/* called on every vsync */
190void pl_frame_limit(void)
191{
bce6b056 192 static struct timeval tv_old, tv_expect;
7c0f51de 193 static int vsync_cnt_prev;
bce6b056 194 struct timeval now;
cefe86b7 195 int diff, usadj;
bce6b056 196
197 vsync_cnt++;
72228559 198
15d46930 199 /* doing input here because the pad is polled
200 * thousands of times per frame for some reason */
201 update_input();
202
72228559 203 pcnt_end(PCNT_ALL);
bce6b056 204 gettimeofday(&now, 0);
72228559 205
bce6b056 206 if (now.tv_sec != tv_old.tv_sec) {
207 diff = tvdiff(now, tv_old);
208 vsps_cur = 0.0f;
209 if (0 < diff && diff < 2000000)
7c0f51de 210 vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
211 vsync_cnt_prev = vsync_cnt;
72228559 212 flips_per_sec = flip_cnt;
7c0f51de 213 flip_cnt = 0;
bce6b056 214 tv_old = now;
bd6267e6 215 if (g_opts & OPT_SHOWCPU)
216 tick_per_sec = get_cpu_ticks();
8f892648 217
218 if (hud_new_msg > 0) {
219 hud_new_msg--;
220 if (hud_new_msg == 0)
221 hud_msg[0] = 0;
222 }
72228559 223 }
224#ifdef PCNT
225 static int ya_vsync_count;
226 if (++ya_vsync_count == PCNT_FRAMES) {
bce6b056 227 pcnt_print(vsps_cur);
72228559 228 ya_vsync_count = 0;
229 }
230#endif
231
c89cd762 232 tvadd(tv_expect, pl_frame_interval);
233 diff = tvdiff(tv_expect, now);
234 if (diff > MAX_LAG_FRAMES * pl_frame_interval || diff < -MAX_LAG_FRAMES * pl_frame_interval) {
235 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, pl_frame_interval);
236 tv_expect = now;
237 diff = 0;
cefe86b7 238 // try to align with vsync
239 usadj = vsync_usec_time;
240 while (usadj < tv_expect.tv_usec - pl_frame_interval)
241 usadj += pl_frame_interval;
242 tv_expect.tv_usec = usadj;
c89cd762 243 }
244
245 if (!(g_opts & OPT_NO_FRAMELIM) && diff > pl_frame_interval) {
246 // yay for working usleep on pandora!
247 //printf("usleep %d\n", diff - pl_frame_interval / 2);
248 usleep(diff - pl_frame_interval / 2);
249 }
250
e64dc4c5 251 if (pl_rearmed_cbs.frameskip) {
252 if (diff < -pl_frame_interval)
253 pl_rearmed_cbs.fskip_advice = 1;
254 else if (diff >= 0)
255 pl_rearmed_cbs.fskip_advice = 0;
bce6b056 256 }
72228559 257
258 pcnt_start(PCNT_ALL);
259}
260
69af03a2 261static void pl_text_out16_(int x, int y, const char *text)
b60f2812 262{
69af03a2 263 int i, l, len = strlen(text), w = pl_fbdev_w;
264 unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w;
265 unsigned short val = 0xffff;
266
267 for (i = 0; i < len; i++, screen += 8)
268 {
269 for (l = 0; l < 8; l++)
270 {
271 unsigned char fd = fontdata8x8[text[i] * 8 + l];
272 unsigned short *s = screen + l * w;
273 if (fd&0x80) s[0] = val;
274 if (fd&0x40) s[1] = val;
275 if (fd&0x20) s[2] = val;
276 if (fd&0x10) s[3] = val;
277 if (fd&0x08) s[4] = val;
278 if (fd&0x04) s[5] = val;
279 if (fd&0x02) s[6] = val;
280 if (fd&0x01) s[7] = val;
281 }
282 }
b60f2812 283}
284
69af03a2 285void pl_text_out16(int x, int y, const char *texto, ...)
b60f2812 286{
69af03a2 287 va_list args;
288 char buffer[256];
289
290 va_start(args, texto);
291 vsnprintf(buffer, sizeof(buffer), texto, args);
292 va_end(args);
293
294 pl_text_out16_(x, y, buffer);
b60f2812 295}
296
201c21e2 297static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
298{
299 *x = g_layer_x;
300 *y = g_layer_y;
301 *w = g_layer_w;
302 *h = g_layer_h;
303}
304
e64dc4c5 305struct rearmed_cbs pl_rearmed_cbs = {
201c21e2 306 pl_get_layer_pos,
ffd0d743 307 pl_fbdev_open,
308 pl_fbdev_set_mode,
309 pl_fbdev_flip,
310 pl_fbdev_close,
201c21e2 311};
312
7c0f51de 313/* watchdog */
314static void *watchdog_thread(void *unused)
315{
316 int vsync_cnt_old = 0;
317 int seen_dead = 0;
318 int sleep_time = 5;
319
799b0b87 320#ifndef NDEBUG
321 // don't interfere with debug
322 return NULL;
323#endif
7c0f51de 324 while (1)
325 {
326 sleep(sleep_time);
327
328 if (stop) {
329 seen_dead = 0;
330 sleep_time = 5;
331 continue;
332 }
333 if (vsync_cnt != vsync_cnt_old) {
334 vsync_cnt_old = vsync_cnt;
335 seen_dead = 0;
336 sleep_time = 2;
337 continue;
338 }
339
340 seen_dead++;
341 sleep_time = 1;
342 if (seen_dead > 1)
343 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
344 if (seen_dead > 4) {
345 fprintf(stderr, "watchdog: lockup detected, aborting\n");
346 // we can't do any cleanup here really, the main thread is
347 // likely touching resources and would crash anyway
348 abort();
349 }
350 }
351}
352
353void pl_start_watchdog(void)
354{
355 pthread_attr_t attr;
356 pthread_t tid;
357 int ret;
358
359 pthread_attr_init(&attr);
360 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
361
362 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
363 if (ret != 0)
364 fprintf(stderr, "could not start watchdog: %d\n", ret);
365}
366