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> |
308c6e67 |
19 | #include <assert.h> |
b60f2812 |
20 | |
cc56203b |
21 | #include "libpicofe/fonts.h" |
22 | #include "libpicofe/input.h" |
23 | #include "libpicofe/plat.h" |
24 | #include "libpicofe/arm/neon_scale2x.h" |
25 | #include "libpicofe/arm/neon_eagle2x.h" |
72228559 |
26 | #include "plugin_lib.h" |
3c70c47b |
27 | #include "menu.h" |
8f892648 |
28 | #include "main.h" |
47821672 |
29 | #include "plat.h" |
72228559 |
30 | #include "pcnt.h" |
4c08b9e7 |
31 | #include "pl_gun_ts.h" |
c82f907a |
32 | #include "cspace.h" |
da710571 |
33 | #include "psemu_plugin_defs.h" |
69af03a2 |
34 | #include "../libpcsxcore/new_dynarec/new_dynarec.h" |
87e5b45f |
35 | #include "../libpcsxcore/psxmem_map.h" |
abf09485 |
36 | #include "../libpcsxcore/gpu.h" |
fb640dd9 |
37 | #include "../libpcsxcore/r3000a.h" |
4c8f1c25 |
38 | #include "../libpcsxcore/psxcounters.h" |
f49a4c48 |
39 | #include "arm_features.h" |
b60f2812 |
40 | |
92a5fe88 |
41 | #define HUD_HEIGHT 10 |
42 | |
7a8d521f |
43 | int in_type[8]; |
44 | int multitap1; |
45 | int multitap2; |
46 | int in_analog_left[8][2] = {{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 }}; |
47 | int in_analog_right[8][2] = {{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 },{ 127, 127 }}; |
3a998763 |
48 | int in_adev[2] = { -1, -1 }; |
49 | int in_adev_axis[2][2] = |
50 | #ifdef PANDORA |
51 | {{ 0, 1 }, { 0, 1 }}; |
52 | #else |
53 | {{ 0, 1 }, { 2, 3 }}; |
54 | #endif |
2c5d0a55 |
55 | int in_adev_is_nublike[2]; |
7a8d521f |
56 | unsigned short in_keystate[8]; |
57 | int in_mouse[8][2]; |
b944a30e |
58 | int in_enable_vibration; |
faf2b2aa |
59 | void *tsdev; |
76f7048e |
60 | void *pl_vout_buf; |
6469a8c4 |
61 | int g_layer_x, g_layer_y, g_layer_w, g_layer_h; |
ddc0a02a |
62 | static int pl_vout_w, pl_vout_h, pl_vout_bpp; /* output display/layer */ |
81023939 |
63 | static int pl_vout_scale_w, pl_vout_scale_h; |
ddc0a02a |
64 | static int psx_w, psx_h, psx_bpp; |
a92f6af1 |
65 | static int vsync_cnt; |
ab423939 |
66 | static int is_pal, frame_interval, frame_interval1024; |
67 | static int vsync_usec_time; |
72228559 |
68 | |
c9099d02 |
69 | // platform hooks |
70 | void (*pl_plat_clear)(void); |
71 | void (*pl_plat_blit)(int doffs, const void *src, int w, int h, |
72 | int sstride, int bgr24); |
73 | void (*pl_plat_hud_print)(int x, int y, const char *str, int bpp); |
74 | |
4c08b9e7 |
75 | |
76 | static __attribute__((noinline)) int get_cpu_ticks(void) |
72228559 |
77 | { |
78 | static unsigned long last_utime; |
79 | static int fd; |
80 | unsigned long utime, ret; |
81 | char buf[128]; |
82 | |
83 | if (fd == 0) |
84 | fd = open("/proc/self/stat", O_RDONLY); |
85 | lseek(fd, 0, SEEK_SET); |
86 | buf[0] = 0; |
87 | read(fd, buf, sizeof(buf)); |
88 | buf[sizeof(buf) - 1] = 0; |
89 | |
90 | sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime); |
91 | ret = utime - last_utime; |
c9099d02 |
92 | if (ret > 200) |
93 | ret = 0; |
72228559 |
94 | last_utime = utime; |
95 | return ret; |
96 | } |
97 | |
c9099d02 |
98 | static void hud_print(void *fb, int w, int x, int y, const char *text) |
99 | { |
100 | if (pl_plat_hud_print) |
101 | pl_plat_hud_print(x, y, text, pl_vout_bpp); |
102 | else if (pl_vout_bpp == 16) |
103 | basic_text_out16_nf(fb, w, x, y, text); |
104 | } |
105 | |
106 | static void hud_printf(void *fb, int w, int x, int y, const char *texto, ...) |
107 | { |
108 | va_list args; |
109 | char buffer[256]; |
110 | |
111 | va_start(args, texto); |
112 | vsnprintf(buffer, sizeof(buffer), texto, args); |
113 | va_end(args); |
114 | |
115 | hud_print(fb, w, x, y, buffer); |
116 | } |
117 | |
41f55c9f |
118 | static void print_msg(int h, int border) |
8f892648 |
119 | { |
92a5fe88 |
120 | hud_print(pl_vout_buf, pl_vout_w, border + 2, h - HUD_HEIGHT, hud_msg); |
8f892648 |
121 | } |
122 | |
41f55c9f |
123 | static void print_fps(int h, int border) |
72228559 |
124 | { |
92a5fe88 |
125 | hud_printf(pl_vout_buf, pl_vout_w, border + 2, h - HUD_HEIGHT, |
c9099d02 |
126 | "%2d %4.1f", pl_rearmed_cbs.flips_per_sec, |
127 | pl_rearmed_cbs.vsps_cur); |
72228559 |
128 | } |
129 | |
308c6e67 |
130 | static void print_cpu_usage(int x, int h) |
72228559 |
131 | { |
308c6e67 |
132 | hud_printf(pl_vout_buf, pl_vout_w, x - 28, |
92a5fe88 |
133 | h - HUD_HEIGHT, "%3d", pl_rearmed_cbs.cpu_usage); |
72228559 |
134 | } |
b60f2812 |
135 | |
90f1d26c |
136 | // draw 192x8 status of 24 sound channels |
55b0eeea |
137 | static __attribute__((noinline)) void draw_active_chans(int vout_w, int vout_h) |
90f1d26c |
138 | { |
174c454a |
139 | extern void spu_get_debug_info(int *chans_out, int *run_chans, |
140 | int *fmod_chans_out, int *noise_chans_out); // hack |
141 | int live_chans, run_chans, fmod_chans, noise_chans; |
90f1d26c |
142 | |
143 | static const unsigned short colors[2] = { 0x1fe3, 0x0700 }; |
76f7048e |
144 | unsigned short *dest = (unsigned short *)pl_vout_buf + |
2da2fc76 |
145 | pl_vout_w * (vout_h - HUD_HEIGHT) + pl_vout_w / 2 - 192/2; |
90f1d26c |
146 | unsigned short *d, p; |
147 | int c, x, y; |
148 | |
ba8d6a2d |
149 | if (pl_vout_buf == NULL || pl_vout_bpp != 16) |
90f1d26c |
150 | return; |
151 | |
174c454a |
152 | spu_get_debug_info(&live_chans, &run_chans, &fmod_chans, &noise_chans); |
90f1d26c |
153 | |
154 | for (c = 0; c < 24; c++) { |
155 | d = dest + c * 8; |
174c454a |
156 | p = !(live_chans & (1<<c)) ? (run_chans & (1<<c) ? 0x01c0 : 0) : |
90f1d26c |
157 | (fmod_chans & (1<<c)) ? 0xf000 : |
158 | (noise_chans & (1<<c)) ? 0x001f : |
159 | colors[c & 1]; |
2da2fc76 |
160 | for (y = 0; y < 8; y++, d += pl_vout_w) |
90f1d26c |
161 | for (x = 0; x < 8; x++) |
162 | d[x] = p; |
163 | } |
164 | } |
165 | |
308c6e67 |
166 | static void print_hud(int x, int w, int h) |
55b0eeea |
167 | { |
308c6e67 |
168 | if (h < 192) |
aafcb4dd |
169 | return; |
170 | |
02783d0b |
171 | if (h > pl_vout_h) |
172 | h = pl_vout_h; |
841ba5ee |
173 | |
55b0eeea |
174 | if (g_opts & OPT_SHOWSPU) |
175 | draw_active_chans(w, h); |
176 | |
177 | if (hud_msg[0] != 0) |
308c6e67 |
178 | print_msg(h, x); |
55b0eeea |
179 | else if (g_opts & OPT_SHOWFPS) |
308c6e67 |
180 | print_fps(h, x); |
55b0eeea |
181 | |
182 | if (g_opts & OPT_SHOWCPU) |
308c6e67 |
183 | print_cpu_usage(x + w, h); |
55b0eeea |
184 | } |
185 | |
9e5ac38f |
186 | /* update scaler target size according to user settings */ |
81023939 |
187 | void pl_update_layer_size(int w, int h, int fw, int fh) |
9e5ac38f |
188 | { |
189 | float mult; |
190 | int imult; |
191 | |
192 | switch (g_scaler) { |
193 | case SCALE_1_1: |
194 | g_layer_w = w; g_layer_h = h; |
195 | break; |
196 | |
efcf1f73 |
197 | case SCALE_2_2: |
198 | g_layer_w = w; g_layer_h = h; |
81023939 |
199 | if (w * 2 <= fw) |
efcf1f73 |
200 | g_layer_w = w * 2; |
81023939 |
201 | if (h * 2 <= fh) |
efcf1f73 |
202 | g_layer_h = h * 2; |
203 | break; |
204 | |
9e5ac38f |
205 | case SCALE_4_3v2: |
81023939 |
206 | #ifdef PANDORA |
207 | if (h <= fh && !(240 < h && h <= 360)) |
208 | { |
209 | #endif |
9e5ac38f |
210 | |
211 | // 4:3 that prefers integer scaling |
81023939 |
212 | imult = fh / h; |
213 | if (imult < 1) |
214 | imult = 1; |
9e5ac38f |
215 | g_layer_w = w * imult; |
216 | g_layer_h = h * imult; |
217 | mult = (float)g_layer_w / (float)g_layer_h; |
218 | if (mult < 1.25f || mult > 1.666f) |
219 | g_layer_w = 4.0f/3.0f * (float)g_layer_h; |
81023939 |
220 | //printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult); |
9e5ac38f |
221 | break; |
81023939 |
222 | #ifdef PANDORA |
223 | } |
224 | #endif |
9e5ac38f |
225 | |
9e5ac38f |
226 | case SCALE_4_3: |
227 | mult = 240.0f / (float)h * 4.0f / 3.0f; |
228 | if (h > 256) |
229 | mult *= 2.0f; |
81023939 |
230 | g_layer_w = mult * (float)fh; |
231 | g_layer_h = fh; |
232 | //printf(" -> %dx%d %.1f\n", g_layer_w, g_layer_h, mult); |
9e5ac38f |
233 | break; |
234 | |
235 | case SCALE_FULLSCREEN: |
81023939 |
236 | g_layer_w = fw; |
237 | g_layer_h = fh; |
9e5ac38f |
238 | break; |
239 | |
240 | default: |
241 | break; |
242 | } |
243 | |
96867f0e |
244 | if (g_scaler != SCALE_CUSTOM) { |
81023939 |
245 | g_layer_x = fw / 2 - g_layer_w / 2; |
246 | g_layer_y = fh / 2 - g_layer_h / 2; |
96867f0e |
247 | } |
81023939 |
248 | if (g_layer_w > fw * 2) g_layer_w = fw * 2; |
249 | if (g_layer_h > fh * 2) g_layer_h = fh * 2; |
9e5ac38f |
250 | } |
251 | |
fa56d360 |
252 | // XXX: this is platform specific really |
7cfc0c66 |
253 | static inline int resolution_ok(int w, int h) |
69af03a2 |
254 | { |
fa56d360 |
255 | return w <= 1024 && h <= 512; |
256 | } |
257 | |
e4c83ca6 |
258 | static void pl_vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp) |
fa56d360 |
259 | { |
260 | int vout_w, vout_h, vout_bpp; |
261 | |
a185be70 |
262 | // special h handling, Wipeout likes to change it by 1-6 |
321ca84d |
263 | static int vsync_cnt_ms_prev; |
264 | if ((unsigned int)(vsync_cnt - vsync_cnt_ms_prev) < 5*60) |
265 | h = (h + 7) & ~7; |
266 | vsync_cnt_ms_prev = vsync_cnt; |
a185be70 |
267 | |
e4c83ca6 |
268 | psx_w = raw_w; |
269 | psx_h = raw_h; |
468072a1 |
270 | psx_bpp = bpp; |
e4c83ca6 |
271 | vout_w = w; |
272 | vout_h = h; |
468072a1 |
273 | vout_bpp = bpp; |
274 | if (pl_rearmed_cbs.only_16bpp) |
275 | vout_bpp = 16; |
fa56d360 |
276 | |
308c6e67 |
277 | assert(vout_h >= 192); |
0fed3dc3 |
278 | |
7cfc0c66 |
279 | pl_vout_scale_w = pl_vout_scale_h = 1; |
f49a4c48 |
280 | #ifdef HAVE_NEON32 |
fa56d360 |
281 | if (soft_filter) { |
282 | if (resolution_ok(w * 2, h * 2) && bpp == 16) { |
7cfc0c66 |
283 | pl_vout_scale_w = 2; |
284 | pl_vout_scale_h = 2; |
fa56d360 |
285 | } |
286 | else { |
287 | // filter unavailable |
288 | hud_msg[0] = 0; |
289 | } |
290 | } |
7cfc0c66 |
291 | else if (scanlines != 0 && scanline_level != 100 && bpp == 16) { |
292 | if (h <= 256) |
293 | pl_vout_scale_h = 2; |
294 | } |
fa56d360 |
295 | #endif |
7cfc0c66 |
296 | vout_w *= pl_vout_scale_w; |
297 | vout_h *= pl_vout_scale_h; |
b60f2812 |
298 | |
81023939 |
299 | pl_update_layer_size(vout_w, vout_h, g_menuscreen_w, g_menuscreen_h); |
9e5ac38f |
300 | |
fa56d360 |
301 | pl_vout_buf = plat_gvideo_set_mode(&vout_w, &vout_h, &vout_bpp); |
c9099d02 |
302 | if (pl_vout_buf == NULL && pl_plat_blit == NULL) |
ddc0a02a |
303 | fprintf(stderr, "failed to set mode %dx%d@%d\n", |
468072a1 |
304 | vout_w, vout_h, vout_bpp); |
fa56d360 |
305 | else { |
306 | pl_vout_w = vout_w; |
307 | pl_vout_h = vout_h; |
308 | pl_vout_bpp = vout_bpp; |
309 | } |
a185be70 |
310 | |
ddc0a02a |
311 | menu_notify_mode_change(pl_vout_w, pl_vout_h, pl_vout_bpp); |
69af03a2 |
312 | } |
313 | |
2da2fc76 |
314 | static int flip_clear_counter; |
315 | |
316 | void pl_force_clear(void) |
317 | { |
318 | flip_clear_counter = 2; |
319 | } |
320 | |
cdc1d78f |
321 | static void pl_vout_flip(const void *vram_, int vram_ofs, int bgr24, |
308c6e67 |
322 | int x, int y, int w, int h, int dims_changed) |
69af03a2 |
323 | { |
fa56d360 |
324 | unsigned char *dest = pl_vout_buf; |
cdc1d78f |
325 | const unsigned char *vram = vram_; |
fa56d360 |
326 | int dstride = pl_vout_w, h1 = h; |
81023939 |
327 | int h_full = pl_vout_h; |
a0392833 |
328 | int enhres = w > psx_w; |
308c6e67 |
329 | int xoffs = 0, doffs; |
cdc1d78f |
330 | int hwrapped; |
fa56d360 |
331 | |
cc56203b |
332 | pcnt_start(PCNT_BLIT); |
333 | |
fa56d360 |
334 | if (vram == NULL) { |
335 | // blanking |
c9099d02 |
336 | if (pl_plat_clear) |
337 | pl_plat_clear(); |
338 | else |
339 | memset(pl_vout_buf, 0, |
af486d6e |
340 | dstride * h_full * pl_vout_bpp / 8); |
c9099d02 |
341 | goto out_hud; |
fa56d360 |
342 | } |
343 | |
308c6e67 |
344 | assert(x + w <= pl_vout_w); |
345 | assert(y + h <= pl_vout_h); |
346 | |
347 | // offset |
348 | xoffs = x * pl_vout_scale_w; |
16b099b4 |
349 | doffs = xoffs + y * pl_vout_scale_h * dstride; |
fa56d360 |
350 | |
308c6e67 |
351 | if (dims_changed) |
42dde520 |
352 | flip_clear_counter = 3; |
fa56d360 |
353 | |
2da2fc76 |
354 | if (flip_clear_counter > 0) { |
c9099d02 |
355 | if (pl_plat_clear) |
356 | pl_plat_clear(); |
357 | else |
358 | memset(pl_vout_buf, 0, |
af486d6e |
359 | dstride * h_full * pl_vout_bpp / 8); |
2da2fc76 |
360 | flip_clear_counter--; |
fa56d360 |
361 | } |
362 | |
c9099d02 |
363 | if (pl_plat_blit) |
364 | { |
cdc1d78f |
365 | pl_plat_blit(doffs, vram + vram_ofs, w, h, 1024, bgr24); |
c9099d02 |
366 | goto out_hud; |
367 | } |
368 | |
369 | if (dest == NULL) |
370 | goto out; |
371 | |
372 | dest += doffs * 2; |
373 | |
fa56d360 |
374 | if (bgr24) |
375 | { |
cdc1d78f |
376 | hwrapped = (vram_ofs & 2047) + w * 3 - 2048; |
fa56d360 |
377 | if (pl_rearmed_cbs.only_16bpp) { |
cdc1d78f |
378 | for (; h1-- > 0; dest += dstride * 2) { |
379 | bgr888_to_rgb565(dest, vram + vram_ofs, w * 3); |
380 | vram_ofs = (vram_ofs + 2048) & 0xfffff; |
381 | } |
382 | |
383 | if (hwrapped > 0) { |
384 | // this is super-rare so just fix-up |
385 | vram_ofs = (vram_ofs - h * 2048) & 0xff800; |
386 | dest -= dstride * 2 * h; |
387 | dest += (w - hwrapped / 3) * 2; |
388 | for (h1 = h; h1-- > 0; dest += dstride * 2) { |
389 | bgr888_to_rgb565(dest, vram + vram_ofs, hwrapped); |
390 | vram_ofs = (vram_ofs + 2048) & 0xfffff; |
391 | } |
fa56d360 |
392 | } |
393 | } |
394 | else { |
395 | dest -= doffs * 2; |
396 | dest += (doffs / 8) * 24; |
397 | |
cdc1d78f |
398 | for (; h1-- > 0; dest += dstride * 3) { |
399 | bgr888_to_rgb888(dest, vram + vram_ofs, w * 3); |
400 | vram_ofs = (vram_ofs + 2048) & 0xfffff; |
401 | } |
402 | |
403 | if (hwrapped > 0) { |
404 | vram_ofs = (vram_ofs - h * 2048) & 0xff800; |
405 | dest -= dstride * 3 * h; |
406 | dest += w * 3 - hwrapped; |
407 | for (h1 = h; h1-- > 0; dest += dstride * 3) { |
408 | bgr888_to_rgb888(dest, vram + vram_ofs, hwrapped); |
409 | vram_ofs = (vram_ofs + 2048) & 0xfffff; |
410 | } |
fa56d360 |
411 | } |
412 | } |
413 | } |
f49a4c48 |
414 | #ifdef HAVE_NEON32 |
7cfc0c66 |
415 | else if (soft_filter == SOFT_FILTER_SCALE2X && pl_vout_scale_w == 2) |
fa56d360 |
416 | { |
cdc1d78f |
417 | neon_scale2x_16_16((const void *)(vram + vram_ofs), (void *)dest, w, |
418 | 2048, dstride * 2, h); |
fa56d360 |
419 | } |
7cfc0c66 |
420 | else if (soft_filter == SOFT_FILTER_EAGLE2X && pl_vout_scale_w == 2) |
fa56d360 |
421 | { |
cdc1d78f |
422 | neon_eagle2x_16_16((const void *)(vram + vram_ofs), (void *)dest, w, |
423 | 2048, dstride * 2, h); |
fa56d360 |
424 | } |
35d3fd2e |
425 | else if (scanlines != 0 && scanline_level != 100) |
426 | { |
2f70bda7 |
427 | int h2, l = scanline_level * 2048 / 100; |
cdc1d78f |
428 | int stride_0 = pl_vout_scale_h >= 2 ? 0 : 2048; |
35d3fd2e |
429 | |
7cfc0c66 |
430 | h1 *= pl_vout_scale_h; |
2f70bda7 |
431 | while (h1 > 0) |
35d3fd2e |
432 | { |
2f70bda7 |
433 | for (h2 = scanlines; h2 > 0 && h1 > 0; h2--, h1--) { |
cdc1d78f |
434 | bgr555_to_rgb565(dest, vram + vram_ofs, w * 2); |
435 | vram_ofs = (vram_ofs + stride_0) & 0xfffff; |
436 | dest += dstride * 2; |
2f70bda7 |
437 | } |
35d3fd2e |
438 | |
2f70bda7 |
439 | for (h2 = scanlines; h2 > 0 && h1 > 0; h2--, h1--) { |
cdc1d78f |
440 | bgr555_to_rgb565_b(dest, vram + vram_ofs, w * 2, l); |
441 | vram_ofs = (vram_ofs + 2048) & 0xfffff; |
442 | dest += dstride * 2; |
2f70bda7 |
443 | } |
35d3fd2e |
444 | } |
445 | } |
fa56d360 |
446 | #endif |
447 | else |
448 | { |
a0392833 |
449 | unsigned int vram_mask = enhres ? ~0 : 0xfffff; |
cdc1d78f |
450 | for (; h1-- > 0; dest += dstride * 2) { |
451 | bgr555_to_rgb565(dest, vram + vram_ofs, w * 2); |
a0392833 |
452 | vram_ofs = (vram_ofs + 2048) & vram_mask; |
cdc1d78f |
453 | } |
454 | |
455 | hwrapped = (vram_ofs & 2047) + w * 2 - 2048; |
a0392833 |
456 | if (!enhres && hwrapped > 0) { |
cdc1d78f |
457 | vram_ofs = (vram_ofs - h * 2048) & 0xff800; |
458 | dest -= dstride * 2 * h; |
459 | dest += w * 2 - hwrapped; |
460 | for (h1 = h; h1-- > 0; dest += dstride * 2) { |
461 | bgr555_to_rgb565(dest, vram + vram_ofs, hwrapped); |
462 | vram_ofs = (vram_ofs + 2048) & 0xfffff; |
463 | } |
fa56d360 |
464 | } |
465 | } |
8f892648 |
466 | |
c9099d02 |
467 | out_hud: |
308c6e67 |
468 | print_hud(xoffs, w * pl_vout_scale_w, (y + h) * pl_vout_scale_h); |
72228559 |
469 | |
fa56d360 |
470 | out: |
cc56203b |
471 | pcnt_end(PCNT_BLIT); |
472 | |
69af03a2 |
473 | // let's flip now |
6469a8c4 |
474 | pl_vout_buf = plat_gvideo_flip(); |
0fed3dc3 |
475 | |
fa56d360 |
476 | pl_rearmed_cbs.flip_cnt++; |
b60f2812 |
477 | } |
478 | |
76f7048e |
479 | static int pl_vout_open(void) |
6d1a1ac2 |
480 | { |
cefe86b7 |
481 | struct timeval now; |
482 | |
fa56d360 |
483 | // force mode update on pl_vout_set_mode() call from gpulib/vout_pl |
484 | pl_vout_buf = NULL; |
cefe86b7 |
485 | |
ab423939 |
486 | plat_gvideo_open(is_pal); |
76f7048e |
487 | |
cefe86b7 |
488 | gettimeofday(&now, 0); |
489 | vsync_usec_time = now.tv_usec; |
76f7048e |
490 | while (vsync_usec_time >= frame_interval) |
491 | vsync_usec_time -= frame_interval; |
cefe86b7 |
492 | |
6d1a1ac2 |
493 | return 0; |
494 | } |
495 | |
76f7048e |
496 | static void pl_vout_close(void) |
b60f2812 |
497 | { |
6469a8c4 |
498 | plat_gvideo_close(); |
b60f2812 |
499 | } |
500 | |
fa56d360 |
501 | static void pl_set_gpu_caps(int caps) |
502 | { |
503 | pl_rearmed_cbs.gpu_caps = caps; |
504 | } |
505 | |
29a8c4f3 |
506 | void *pl_prepare_screenshot(int *w, int *h, int *bpp) |
507 | { |
6469a8c4 |
508 | void *ret = plat_prepare_screenshot(w, h, bpp); |
509 | if (ret != NULL) |
510 | return ret; |
511 | |
76f7048e |
512 | *w = pl_vout_w; |
513 | *h = pl_vout_h; |
514 | *bpp = pl_vout_bpp; |
29a8c4f3 |
515 | |
76f7048e |
516 | return pl_vout_buf; |
29a8c4f3 |
517 | } |
518 | |
fa56d360 |
519 | /* display/redering mode switcher */ |
520 | static int dispmode_default(void) |
521 | { |
522 | pl_rearmed_cbs.gpu_neon.enhancement_enable = 0; |
523 | soft_filter = SOFT_FILTER_NONE; |
524 | snprintf(hud_msg, sizeof(hud_msg), "default mode"); |
525 | return 1; |
526 | } |
527 | |
7cfc0c66 |
528 | static int dispmode_doubleres(void) |
fa56d360 |
529 | { |
530 | if (!(pl_rearmed_cbs.gpu_caps & GPU_CAP_SUPPORTS_2X) |
531 | || !resolution_ok(psx_w * 2, psx_h * 2) || psx_bpp != 16) |
532 | return 0; |
533 | |
534 | dispmode_default(); |
535 | pl_rearmed_cbs.gpu_neon.enhancement_enable = 1; |
536 | snprintf(hud_msg, sizeof(hud_msg), "double resolution"); |
537 | return 1; |
538 | } |
539 | |
f49a4c48 |
540 | #ifdef HAVE_NEON32 |
7cfc0c66 |
541 | static int dispmode_scale2x(void) |
fa56d360 |
542 | { |
e4c83ca6 |
543 | if (!resolution_ok(psx_w * 2, psx_h * 2) || psx_bpp != 16) |
fa56d360 |
544 | return 0; |
545 | |
546 | dispmode_default(); |
547 | soft_filter = SOFT_FILTER_SCALE2X; |
548 | snprintf(hud_msg, sizeof(hud_msg), "scale2x"); |
549 | return 1; |
550 | } |
551 | |
7cfc0c66 |
552 | static int dispmode_eagle2x(void) |
fa56d360 |
553 | { |
e4c83ca6 |
554 | if (!resolution_ok(psx_w * 2, psx_h * 2) || psx_bpp != 16) |
fa56d360 |
555 | return 0; |
556 | |
557 | dispmode_default(); |
558 | soft_filter = SOFT_FILTER_EAGLE2X; |
559 | snprintf(hud_msg, sizeof(hud_msg), "eagle2x"); |
560 | return 1; |
561 | } |
7cfc0c66 |
562 | #endif |
fa56d360 |
563 | |
564 | static int (*dispmode_switchers[])(void) = { |
565 | dispmode_default, |
fa56d360 |
566 | dispmode_doubleres, |
f49a4c48 |
567 | #ifdef HAVE_NEON32 |
fa56d360 |
568 | dispmode_scale2x, |
569 | dispmode_eagle2x, |
570 | #endif |
571 | }; |
572 | |
573 | static int dispmode_current; |
574 | |
575 | void pl_switch_dispmode(void) |
576 | { |
577 | if (pl_rearmed_cbs.gpu_caps & GPU_CAP_OWNS_DISPLAY) |
578 | return; |
579 | |
580 | while (1) { |
581 | dispmode_current++; |
582 | if (dispmode_current >= |
583 | sizeof(dispmode_switchers) / sizeof(dispmode_switchers[0])) |
584 | dispmode_current = 0; |
585 | if (dispmode_switchers[dispmode_current]()) |
586 | break; |
587 | } |
588 | } |
589 | |
1b0c5139 |
590 | #ifndef MAEMO |
2c5d0a55 |
591 | /* adjust circle-like analog inputs to better match |
592 | * more square-like analogs in PSX */ |
593 | static void update_analog_nub_adjust(int *x_, int *y_) |
594 | { |
e4c83ca6 |
595 | #define d 16 |
cc8ae53e |
596 | static const int scale[] = |
597 | { 0 - d*2, 0 - d*2, 0 - d*2, 12 - d*2, |
598 | 30 - d*2, 60 - d*2, 75 - d*2, 60 - d*2, 60 - d*2 }; |
599 | int x = abs(*x_); |
600 | int y = abs(*y_); |
601 | int scale_x = scale[y / 16]; |
602 | int scale_y = scale[x / 16]; |
603 | |
604 | if (x) { |
605 | x += d + (x * scale_x >> 8); |
606 | if (*x_ < 0) |
607 | x = -x; |
608 | } |
609 | if (y) { |
610 | y += d + (y * scale_y >> 8); |
611 | if (*y_ < 0) |
612 | y = -y; |
613 | } |
2c5d0a55 |
614 | |
615 | *x_ = x; |
616 | *y_ = y; |
e4c83ca6 |
617 | #undef d |
2c5d0a55 |
618 | } |
619 | |
1b0c5139 |
620 | static void update_analogs(void) |
621 | { |
7a8d521f |
622 | int *nubp[2] = { in_analog_left[0], in_analog_right[0] }; |
2c5d0a55 |
623 | int vals[2]; |
1b0c5139 |
624 | int i, a, v, ret; |
625 | |
626 | for (i = 0; i < 2; i++) |
627 | { |
628 | if (in_adev[i] < 0) |
629 | continue; |
630 | |
631 | for (a = 0; a < 2; a++) { |
2c5d0a55 |
632 | vals[a] = 0; |
1b0c5139 |
633 | |
634 | ret = in_update_analog(in_adev[i], in_adev_axis[i][a], &v); |
2c5d0a55 |
635 | if (ret == 0) |
636 | vals[a] = 128 * v / IN_ABS_RANGE; |
1b0c5139 |
637 | } |
2c5d0a55 |
638 | |
639 | if (in_adev_is_nublike[i]) |
640 | update_analog_nub_adjust(&vals[0], &vals[1]); |
641 | |
642 | for (a = 0; a < 2; a++) { |
643 | v = vals[a] + 127; |
644 | if (v < 0) v = 0; |
645 | else if (v > 255) v = 255; |
646 | nubp[i][a] = v; |
647 | } |
648 | |
1b0c5139 |
649 | } |
1b0c5139 |
650 | } |
651 | |
c87406ff |
652 | static void emu_set_action(enum sched_action action_) |
653 | { |
654 | extern enum sched_action emu_action, emu_action_old; |
655 | |
656 | if (action_ == SACTION_NONE) |
657 | emu_action_old = 0; |
658 | else if (action_ != emu_action_old) |
659 | psxRegs.stop++; |
660 | emu_action = action_; |
661 | } |
662 | |
15d46930 |
663 | static void update_input(void) |
664 | { |
665 | int actions[IN_BINDTYPE_COUNT] = { 0, }; |
8f892648 |
666 | unsigned int emu_act; |
fb640dd9 |
667 | int in_state_gun; |
668 | int i; |
15d46930 |
669 | |
670 | in_update(actions); |
7a8d521f |
671 | if (in_type[0] == PSE_PAD_TYPE_ANALOGJOY || in_type[0] == PSE_PAD_TYPE_ANALOGPAD) |
1b0c5139 |
672 | update_analogs(); |
8f892648 |
673 | emu_act = actions[IN_BINDTYPE_EMU]; |
fb640dd9 |
674 | in_state_gun = emu_act & SACTION_GUN_MASK; |
4c08b9e7 |
675 | |
676 | emu_act &= ~SACTION_GUN_MASK; |
8f892648 |
677 | if (emu_act) { |
678 | int which = 0; |
679 | for (; !(emu_act & 1); emu_act >>= 1, which++) |
680 | ; |
681 | emu_act = which; |
682 | } |
683 | emu_set_action(emu_act); |
684 | |
fb640dd9 |
685 | in_keystate[0] = actions[IN_BINDTYPE_PLAYER12] & 0xffff; |
686 | in_keystate[1] = (actions[IN_BINDTYPE_PLAYER12] >> 16) & 0xffff; |
2db412ad |
687 | |
fb640dd9 |
688 | if (tsdev) for (i = 0; i < 2; i++) { |
689 | int in = 0, x = 0, y = 0, trigger;; |
690 | if (in_type[i] != PSE_PAD_TYPE_GUN |
691 | && in_type[i] != PSE_PAD_TYPE_GUNCON) |
692 | continue; |
693 | trigger = in_type[i] == PSE_PAD_TYPE_GUN |
694 | ? (1 << DKEY_SQUARE) : (1 << DKEY_CIRCLE); |
695 | |
696 | pl_gun_ts_update(tsdev, &x, &y, &in); |
697 | in_analog_left[i][0] = 65536; |
698 | in_analog_left[i][1] = 65536; |
699 | if (in && !(in_state_gun & (1 << SACTION_GUN_TRIGGER2))) { |
700 | in_analog_left[i][0] = x; |
701 | in_analog_left[i][1] = y; |
702 | if (!(g_opts & OPT_TSGUN_NOTRIGGER)) |
703 | in_state_gun |= (1 << SACTION_GUN_TRIGGER); |
704 | } |
705 | in_keystate[i] = 0; |
706 | if (in_state_gun & ((1 << SACTION_GUN_TRIGGER) |
707 | | (1 << SACTION_GUN_TRIGGER2))) |
708 | in_keystate[i] |= trigger; |
709 | if (in_state_gun & (1 << SACTION_GUN_A)) |
710 | in_keystate[i] |= (1 << DKEY_START); |
711 | if (in_state_gun & (1 << SACTION_GUN_B)) |
712 | in_keystate[i] |= (1 << DKEY_CROSS); |
713 | } |
15d46930 |
714 | } |
1b0c5139 |
715 | #else /* MAEMO */ |
7010034e |
716 | extern void update_input(void); |
1b0c5139 |
717 | #endif |
15d46930 |
718 | |
2db412ad |
719 | void pl_gun_byte2(int port, unsigned char byte) |
4c08b9e7 |
720 | { |
fb640dd9 |
721 | if (!tsdev || in_type[port] != PSE_PAD_TYPE_GUN || !(byte & 0x10)) |
722 | return; |
723 | if (in_analog_left[port][0] == 65536) |
724 | return; |
725 | |
726 | psxScheduleIrq10(4, in_analog_left[port][0] * 1629 / 1024, |
727 | in_analog_left[port][1] * psx_h / 1024); |
4c08b9e7 |
728 | } |
729 | |
bce6b056 |
730 | #define MAX_LAG_FRAMES 3 |
731 | |
732 | #define tvdiff(tv, tv_old) \ |
733 | ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec) |
bce6b056 |
734 | |
72228559 |
735 | /* called on every vsync */ |
736 | void pl_frame_limit(void) |
737 | { |
bce6b056 |
738 | static struct timeval tv_old, tv_expect; |
2f546f9a |
739 | static int vsync_cnt_prev, drc_active_vsyncs; |
bce6b056 |
740 | struct timeval now; |
cefe86b7 |
741 | int diff, usadj; |
bce6b056 |
742 | |
69e482e3 |
743 | if (g_emu_resetting) |
81edd2b3 |
744 | return; |
745 | |
bce6b056 |
746 | vsync_cnt++; |
72228559 |
747 | |
15d46930 |
748 | /* doing input here because the pad is polled |
749 | * thousands of times per frame for some reason */ |
750 | update_input(); |
751 | |
72228559 |
752 | pcnt_end(PCNT_ALL); |
bce6b056 |
753 | gettimeofday(&now, 0); |
72228559 |
754 | |
bce6b056 |
755 | if (now.tv_sec != tv_old.tv_sec) { |
756 | diff = tvdiff(now, tv_old); |
a92f6af1 |
757 | pl_rearmed_cbs.vsps_cur = 0.0f; |
bce6b056 |
758 | if (0 < diff && diff < 2000000) |
a92f6af1 |
759 | pl_rearmed_cbs.vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff; |
7c0f51de |
760 | vsync_cnt_prev = vsync_cnt; |
a92f6af1 |
761 | |
762 | if (g_opts & OPT_SHOWFPS) |
763 | pl_rearmed_cbs.flips_per_sec = pl_rearmed_cbs.flip_cnt; |
764 | pl_rearmed_cbs.flip_cnt = 0; |
bd6267e6 |
765 | if (g_opts & OPT_SHOWCPU) |
a92f6af1 |
766 | pl_rearmed_cbs.cpu_usage = get_cpu_ticks(); |
8f892648 |
767 | |
768 | if (hud_new_msg > 0) { |
769 | hud_new_msg--; |
770 | if (hud_new_msg == 0) |
771 | hud_msg[0] = 0; |
772 | } |
a92f6af1 |
773 | tv_old = now; |
ece032e6 |
774 | //new_dynarec_print_stats(); |
72228559 |
775 | } |
776 | #ifdef PCNT |
777 | static int ya_vsync_count; |
778 | if (++ya_vsync_count == PCNT_FRAMES) { |
a92f6af1 |
779 | pcnt_print(pl_rearmed_cbs.vsps_cur); |
72228559 |
780 | ya_vsync_count = 0; |
781 | } |
782 | #endif |
783 | |
76f7048e |
784 | // tv_expect uses usec*1024 units instead of usecs for better accuracy |
785 | tv_expect.tv_usec += frame_interval1024; |
786 | if (tv_expect.tv_usec >= (1000000 << 10)) { |
787 | tv_expect.tv_usec -= (1000000 << 10); |
788 | tv_expect.tv_sec++; |
789 | } |
790 | diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 + (tv_expect.tv_usec >> 10) - now.tv_usec; |
791 | |
792 | if (diff > MAX_LAG_FRAMES * frame_interval || diff < -MAX_LAG_FRAMES * frame_interval) { |
793 | //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, frame_interval); |
c89cd762 |
794 | tv_expect = now; |
795 | diff = 0; |
cefe86b7 |
796 | // try to align with vsync |
797 | usadj = vsync_usec_time; |
76f7048e |
798 | while (usadj < tv_expect.tv_usec - frame_interval) |
799 | usadj += frame_interval; |
800 | tv_expect.tv_usec = usadj << 10; |
c89cd762 |
801 | } |
802 | |
76f7048e |
803 | if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) { |
c89cd762 |
804 | // yay for working usleep on pandora! |
76f7048e |
805 | //printf("usleep %d\n", diff - frame_interval / 2); |
47f37583 |
806 | usleep(diff - frame_interval); |
c89cd762 |
807 | } |
808 | |
e64dc4c5 |
809 | if (pl_rearmed_cbs.frameskip) { |
76f7048e |
810 | if (diff < -frame_interval) |
e64dc4c5 |
811 | pl_rearmed_cbs.fskip_advice = 1; |
812 | else if (diff >= 0) |
813 | pl_rearmed_cbs.fskip_advice = 0; |
2f546f9a |
814 | |
815 | // recompilation is not that fast and may cause frame skip on |
816 | // loading screens and such, resulting in flicker or glitches |
0b1633d7 |
817 | if (ndrc_g.did_compile) { |
2f546f9a |
818 | if (drc_active_vsyncs < 32) |
819 | pl_rearmed_cbs.fskip_advice = 0; |
820 | drc_active_vsyncs++; |
821 | } |
822 | else |
823 | drc_active_vsyncs = 0; |
0b1633d7 |
824 | ndrc_g.did_compile = 0; |
bce6b056 |
825 | } |
72228559 |
826 | |
827 | pcnt_start(PCNT_ALL); |
828 | } |
829 | |
ab423939 |
830 | void pl_timing_prepare(int is_pal_) |
76f7048e |
831 | { |
4c8f1c25 |
832 | double fps; |
76f7048e |
833 | pl_rearmed_cbs.fskip_advice = 0; |
a92f6af1 |
834 | pl_rearmed_cbs.flips_per_sec = 0; |
835 | pl_rearmed_cbs.cpu_usage = 0; |
76f7048e |
836 | |
ab423939 |
837 | is_pal = is_pal_; |
4c8f1c25 |
838 | fps = psxGetFps(); |
839 | frame_interval = (int)(1000000.0 / fps); |
840 | frame_interval1024 = (int)(1000000.0 * 1024.0 / fps); |
76f7048e |
841 | |
842 | // used by P.E.Op.S. frameskip code |
4c8f1c25 |
843 | pl_rearmed_cbs.gpu_peops.fFrameRateHz = (float)fps; |
76f7048e |
844 | pl_rearmed_cbs.gpu_peops.dwFrameRateTicks = |
4c8f1c25 |
845 | (100000*100 / (int)(pl_rearmed_cbs.gpu_peops.fFrameRateHz*100)); |
76f7048e |
846 | } |
847 | |
201c21e2 |
848 | static void pl_get_layer_pos(int *x, int *y, int *w, int *h) |
849 | { |
850 | *x = g_layer_x; |
851 | *y = g_layer_y; |
852 | *w = g_layer_w; |
853 | *h = g_layer_h; |
854 | } |
855 | |
87e5b45f |
856 | static void *pl_mmap(unsigned int size); |
857 | static void pl_munmap(void *ptr, unsigned int size); |
9ee0fd5b |
858 | |
e64dc4c5 |
859 | struct rearmed_cbs pl_rearmed_cbs = { |
201c21e2 |
860 | pl_get_layer_pos, |
76f7048e |
861 | pl_vout_open, |
862 | pl_vout_set_mode, |
863 | pl_vout_flip, |
864 | pl_vout_close, |
fa56d360 |
865 | |
866 | .mmap = pl_mmap, |
867 | .munmap = pl_munmap, |
868 | .pl_set_gpu_caps = pl_set_gpu_caps, |
abf09485 |
869 | .gpu_state_change = gpu_state_change, |
201c21e2 |
870 | }; |
871 | |
7c0f51de |
872 | /* watchdog */ |
873 | static void *watchdog_thread(void *unused) |
874 | { |
875 | int vsync_cnt_old = 0; |
876 | int seen_dead = 0; |
877 | int sleep_time = 5; |
878 | |
879 | while (1) |
880 | { |
881 | sleep(sleep_time); |
882 | |
c87406ff |
883 | if (psxRegs.stop) { |
7c0f51de |
884 | seen_dead = 0; |
885 | sleep_time = 5; |
886 | continue; |
887 | } |
888 | if (vsync_cnt != vsync_cnt_old) { |
889 | vsync_cnt_old = vsync_cnt; |
890 | seen_dead = 0; |
891 | sleep_time = 2; |
892 | continue; |
893 | } |
894 | |
895 | seen_dead++; |
896 | sleep_time = 1; |
897 | if (seen_dead > 1) |
898 | fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead); |
899 | if (seen_dead > 4) { |
900 | fprintf(stderr, "watchdog: lockup detected, aborting\n"); |
627a6557 |
901 | fflush(stderr); |
7c0f51de |
902 | // we can't do any cleanup here really, the main thread is |
903 | // likely touching resources and would crash anyway |
904 | abort(); |
905 | } |
906 | } |
907 | } |
908 | |
909 | void pl_start_watchdog(void) |
910 | { |
627a6557 |
911 | #if defined(NDEBUG) && !defined(DRC_DBG) |
7c0f51de |
912 | pthread_attr_t attr; |
913 | pthread_t tid; |
914 | int ret; |
627a6557 |
915 | #ifdef __linux__ |
916 | int tpid = 0; |
917 | char buf[256]; |
918 | FILE *f = fopen("/proc/self/status", "r"); |
919 | if (f) { |
920 | while (fgets(buf, sizeof(buf), f)) |
921 | if (buf[0] == 'T' && sscanf(buf, "TracerPid: %d", &tpid) == 1) |
922 | break; |
923 | fclose(f); |
924 | } |
925 | if (tpid) { |
926 | printf("no watchdog to tracer %d\n", tpid); |
927 | return; |
928 | } |
929 | #endif |
7c0f51de |
930 | |
931 | pthread_attr_init(&attr); |
932 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
933 | |
934 | ret = pthread_create(&tid, &attr, watchdog_thread, NULL); |
935 | if (ret != 0) |
936 | fprintf(stderr, "could not start watchdog: %d\n", ret); |
627a6557 |
937 | #endif |
938 | (void)watchdog_thread; |
7c0f51de |
939 | } |
940 | |
4284a818 |
941 | static void *pl_emu_mmap(unsigned long addr, size_t size, |
942 | enum psxMapTag tag, int *can_retry_addr) |
87e5b45f |
943 | { |
4284a818 |
944 | *can_retry_addr = 1; |
7e9c3060 |
945 | return plat_mmap(addr, size, 0, 0); |
87e5b45f |
946 | } |
947 | |
948 | static void pl_emu_munmap(void *ptr, size_t size, enum psxMapTag tag) |
949 | { |
950 | plat_munmap(ptr, size); |
951 | } |
952 | |
953 | static void *pl_mmap(unsigned int size) |
954 | { |
4284a818 |
955 | int can_retry_addr; |
956 | return psxMapHook(0, size, MAP_TAG_VRAM, &can_retry_addr); |
87e5b45f |
957 | } |
958 | |
959 | static void pl_munmap(void *ptr, unsigned int size) |
960 | { |
961 | psxUnmapHook(ptr, size, MAP_TAG_VRAM); |
962 | } |
963 | |
4c08b9e7 |
964 | void pl_init(void) |
965 | { |
24de2dd4 |
966 | extern unsigned int hSyncCount; // from psxcounters |
3ece2f0c |
967 | extern unsigned int frame_counter; |
24de2dd4 |
968 | |
ddc0a02a |
969 | psx_w = psx_h = pl_vout_w = pl_vout_h = 256; |
970 | psx_bpp = pl_vout_bpp = 16; |
321ca84d |
971 | |
faf2b2aa |
972 | tsdev = pl_gun_ts_init(); |
24de2dd4 |
973 | |
974 | pl_rearmed_cbs.gpu_hcnt = &hSyncCount; |
3ece2f0c |
975 | pl_rearmed_cbs.gpu_frame_count = &frame_counter; |
87e5b45f |
976 | |
977 | psxMapHook = pl_emu_mmap; |
978 | psxUnmapHook = pl_emu_munmap; |
4c08b9e7 |
979 | } |