11913091 |
1 | // vim:shiftwidth=2:expandtab |
830ba1bf |
2 | // a "gentle" reminder |
3 | #ifdef __ARM_EABI__ |
4 | #error loader is meant to be OABI! |
5 | #endif |
11913091 |
6 | #include <stdio.h> |
7 | #include <stdlib.h> |
3d295a9f |
8 | #include <string.h> |
c1ffd5ba |
9 | #include <alloca.h> |
158ddd8d |
10 | #include <ctype.h> |
11913091 |
11 | #include <sys/types.h> |
12 | #include <sys/stat.h> |
13 | #include <fcntl.h> |
14 | #include <sys/mman.h> |
15 | #include <sys/types.h> |
16 | #include <unistd.h> |
17 | #include <signal.h> |
18 | #include <asm/ucontext.h> |
c1ffd5ba |
19 | #include <pthread.h> |
20 | #include <errno.h> |
21 | #include <time.h> |
22 | #include <sys/resource.h> |
7fd42181 |
23 | #include <sys/ioctl.h> |
24 | #include <linux/soundcard.h> |
25 | #include <linux/fb.h> |
11913091 |
26 | |
27 | #include "header.h" |
7fd42181 |
28 | #include "../common/host_fb.h" |
29 | #include "../common/cmn.h" |
86418a84 |
30 | #include "sys_cacheflush.h" |
7fd42181 |
31 | #include "realfuncs.h" |
11913091 |
32 | |
3d3fa63b |
33 | #if (DBG & 2) && !(DBG & 4) |
7fd42181 |
34 | #define LOG_IO_UNK |
35 | #endif |
3d3fa63b |
36 | #if (DBG & 4) |
7fd42181 |
37 | #define LOG_IO |
38 | #endif |
6720e4e4 |
39 | //#define LOG_SEGV |
40 | |
41 | #ifdef LOG_IO |
42 | #define iolog log_io |
43 | #else |
86418a84 |
44 | #define iolog(...) |
6720e4e4 |
45 | #endif |
46 | |
c1ffd5ba |
47 | #ifdef LOG_IO_UNK |
6720e4e4 |
48 | #define iolog_unh log_io |
49 | #else |
50 | #define iolog_unh(...) |
51 | #endif |
52 | |
53 | #ifdef LOG_SEGV |
54 | #define segvlog printf |
55 | #else |
0881206b |
56 | #define segvlog(...) |
6720e4e4 |
57 | #endif |
58 | |
c1ffd5ba |
59 | #if defined(LOG_IO) || defined(LOG_IO_UNK) |
6720e4e4 |
60 | #include "mmsp2-regs.h" |
61 | #endif |
11913091 |
62 | |
3d3fa63b |
63 | typedef unsigned long long u64; |
11913091 |
64 | typedef unsigned int u32; |
65 | typedef unsigned short u16; |
66 | typedef unsigned char u8; |
67 | |
c1ffd5ba |
68 | static pthread_mutex_t fb_mutex = PTHREAD_MUTEX_INITIALIZER; |
69 | static pthread_cond_t fb_cond = PTHREAD_COND_INITIALIZER; |
70 | |
3d295a9f |
71 | static struct { |
72 | u32 dstctrl; |
73 | u32 dstaddr; |
74 | u32 dststride; |
75 | u32 srcctrl; |
76 | u32 srcaddr; // |
77 | u32 srcstride; |
78 | u32 srcforcolor; |
79 | u32 srcbackcolor; |
80 | u32 patctrl; // |
81 | u32 patforcolor; |
82 | u32 patbackcolor; |
83 | u32 size; |
84 | u32 ctrl; // |
85 | u32 run; |
86 | u32 intc; |
87 | u32 srcfifo; |
88 | } blitter; |
89 | |
0881206b |
90 | #define SRCCTRL_INVIDEO (1 << 8) |
91 | #define SRCCTRL_SRCENB (1 << 7) |
92 | #define CTRL_TRANSPARENCYENB (1 << 11) |
93 | |
3d295a9f |
94 | static struct { |
7000b522 |
95 | // mmsp2 |
6720e4e4 |
96 | u16 mlc_stl_cntl; |
3d295a9f |
97 | union { |
7000b522 |
98 | u32 mlc_stl_adr; // mlcaddress for pollux |
3d295a9f |
99 | struct { |
6720e4e4 |
100 | u16 mlc_stl_adrl; |
101 | u16 mlc_stl_adrh; |
3d295a9f |
102 | }; |
103 | }; |
6720e4e4 |
104 | u16 mlc_stl_pallt_a; |
105 | union { |
106 | u16 mlc_stl_pallt_d[256*2]; |
107 | u32 mlc_stl_pallt_d32[256]; |
108 | }; |
109 | |
7000b522 |
110 | // pollux |
111 | u32 mlccontrol; |
112 | u16 mlcpalette[256]; |
113 | |
6720e4e4 |
114 | // state |
3d3fa63b |
115 | void *umem; |
6720e4e4 |
116 | u32 old_mlc_stl_adr; |
7000b522 |
117 | u32 btn_state; // as seen through /dev/GPIO: 0PVdVu YXBA RLSeSt 0Ri0Dn 0Le0Up |
118 | struct { |
119 | u32 width, height; |
120 | u32 stride; |
121 | u32 bpp; |
122 | u32 dirty_pal:2; |
123 | } v; |
3d295a9f |
124 | } mmsp2; |
7000b522 |
125 | #define pollux mmsp2 // so that code doesn't look that weird |
126 | enum { |
127 | DIRTY_PAL_MMSP2 = 1, |
128 | DIRTY_PAL_POLLUX = 2, |
129 | }; |
3d295a9f |
130 | |
11913091 |
131 | |
c1ffd5ba |
132 | #if defined(LOG_IO) || defined(LOG_IO_UNK) |
6720e4e4 |
133 | static void log_io(const char *pfx, u32 a, u32 d, int size) |
134 | { |
135 | const char *fmt, *reg = ""; |
136 | switch (size) { |
c1ffd5ba |
137 | case 8: fmt = "%s %08x %02x %s\n"; d &= 0xff; break; |
138 | case 32: fmt = "%s %08x %08x %s\n"; break; |
139 | default: fmt = "%s %08x %04x %s\n"; d &= 0xffff; break; |
6720e4e4 |
140 | } |
141 | |
142 | if ((a & ~0xffff) == 0x7f000000) |
143 | reg = regnames[a & 0xffff]; |
144 | |
145 | printf(fmt, pfx, a, d, reg); |
146 | } |
147 | #endif |
148 | |
0881206b |
149 | static void memset16(void *dst, u32 pattern, int count) |
150 | { |
151 | u32 *dl; |
152 | u16 *d; |
153 | |
154 | d = (u16 *)((long)dst & ~1); |
155 | if ((long)d & 2) { |
156 | *d++ = pattern; |
157 | count--; |
158 | } |
159 | dl = (void *)d; |
160 | pattern |= pattern << 16; |
161 | |
162 | while (count >= 2) { |
163 | *dl++ = pattern; |
164 | count -= 2; |
165 | } |
166 | if (count) |
167 | *(u16 *)dl = pattern; |
168 | } |
169 | |
170 | static void blt_tr(void *dst, void *src, u32 trc, int w) |
171 | { |
172 | u16 *d = (u16 *)((long)dst & ~1); |
173 | u16 *s = (u16 *)((long)src & ~1); |
174 | |
175 | // XXX: optimize |
176 | for (; w > 0; d++, s++, w--) |
177 | if (*s != trc) |
178 | *d = *s; |
179 | } |
180 | |
181 | #define dump_blitter() \ |
182 | { \ |
183 | u32 *r = &blitter.dstctrl; \ |
184 | int i; \ |
185 | for (i = 0; i < 4*4; i++, r++) { \ |
186 | printf("%08x ", *r); \ |
187 | if ((i & 3) == 3) \ |
188 | printf("\n"); \ |
189 | } \ |
190 | } |
191 | |
6720e4e4 |
192 | static void *uppermem_lookup(u32 addr, u8 **mem_end) |
11913091 |
193 | { |
3d3fa63b |
194 | // XXX: maybe support mirroring? |
195 | if ((addr & 0xfe000000) != 0x02000000) |
196 | return NULL; |
3d295a9f |
197 | |
3d3fa63b |
198 | *mem_end = (u8 *)mmsp2.umem + 0x02000000; |
199 | return (u8 *)mmsp2.umem - 0x02000000 + addr; |
11913091 |
200 | } |
201 | |
3d295a9f |
202 | static void blitter_do(void) |
11913091 |
203 | { |
0881206b |
204 | u8 *dst, *dste, *src = NULL, *srce = NULL; |
3d295a9f |
205 | int w, h, sstrd, dstrd; |
6720e4e4 |
206 | int to_screen = 0; |
dc90c02d |
207 | u32 bpp, addr; |
3d295a9f |
208 | |
209 | w = blitter.size & 0x7ff; |
210 | h = (blitter.size >> 16) & 0x7ff; |
211 | sstrd = blitter.srcstride; |
212 | dstrd = blitter.dststride; |
213 | |
0881206b |
214 | // XXX: need to confirm this.. |
215 | addr = (blitter.dstaddr & ~3) | ((blitter.dstctrl & 0x1f) >> 3); |
c1ffd5ba |
216 | |
dc90c02d |
217 | // use dst bpp.. How does it do blits with different src bpp? |
218 | bpp = (blitter.dstctrl & 0x20) ? 16 : 8; |
219 | |
c1ffd5ba |
220 | // maybe the screen? |
dc90c02d |
221 | if (((w == 320 && h == 240) || // blit whole screen |
222 | (w * h >= 320*240/2)) && // ..or at least half of the area |
223 | mmsp2.mlc_stl_adr <= addr && addr < mmsp2.mlc_stl_adr + 320*240*2) |
c1ffd5ba |
224 | to_screen = 1; |
225 | |
226 | dst = uppermem_lookup(addr, &dste); |
0881206b |
227 | |
228 | // XXX: assume fill if no SRCENB, but it could be pattern blit.. |
229 | if (blitter.srcctrl & SRCCTRL_SRCENB) { |
230 | if (!(blitter.srcctrl & SRCCTRL_INVIDEO)) |
231 | goto bad_blit; |
232 | |
233 | addr = (blitter.srcaddr & ~3) | ((blitter.srcctrl & 0x1f) >> 3); |
c1ffd5ba |
234 | src = uppermem_lookup(addr, &srce); |
0881206b |
235 | if (src == NULL) |
236 | goto bad_blit; |
237 | |
238 | if (src + sstrd * h > srce) { |
6720e4e4 |
239 | err("blit %08x->%08x %dx%d did not fit src\n", |
0881206b |
240 | blitter.srcaddr, blitter.dstaddr, w, h); |
241 | h = (srce - src) / sstrd; |
242 | } |
3d295a9f |
243 | } |
244 | |
dc90c02d |
245 | if (dst == NULL) |
246 | goto bad_blit; |
247 | |
3d295a9f |
248 | if (dst + dstrd * h > dste) { |
6720e4e4 |
249 | err("blit %08x->%08x %dx%d did not fit dst\n", |
3d295a9f |
250 | blitter.srcaddr, blitter.dstaddr, w, h); |
251 | h = (dste - dst) / dstrd; |
252 | } |
253 | |
0881206b |
254 | if (src != NULL) { |
255 | // copy |
dc90c02d |
256 | if (bpp == 16 && (blitter.ctrl & CTRL_TRANSPARENCYENB)) { |
0881206b |
257 | u32 trc = blitter.ctrl >> 16; |
258 | for (; h > 0; h--, dst += dstrd, src += sstrd) |
259 | blt_tr(dst, src, trc, w); |
260 | } |
261 | else { |
262 | for (; h > 0; h--, dst += dstrd, src += sstrd) |
dc90c02d |
263 | memcpy(dst, src, w * bpp / 8); |
0881206b |
264 | } |
3d295a9f |
265 | } |
0881206b |
266 | else { |
267 | // fill. Assume the pattern is cleared and bg color is used |
268 | u32 bgc = blitter.patbackcolor & 0xffff; |
dc90c02d |
269 | if (bpp == 16) { |
270 | for (; h > 0; h--, dst += dstrd) |
271 | memset16(dst, bgc, w); |
272 | } |
273 | else { |
274 | for (; h > 0; h--, dst += dstrd) |
275 | memset(dst, bgc, w); // bgc? |
276 | } |
0881206b |
277 | } |
6720e4e4 |
278 | |
279 | if (to_screen) |
c1ffd5ba |
280 | pthread_cond_signal(&fb_cond); |
0881206b |
281 | return; |
3d295a9f |
282 | |
0881206b |
283 | bad_blit: |
6720e4e4 |
284 | err("blit %08x->%08x %dx%d translated to %p->%p\n", |
0881206b |
285 | blitter.srcaddr, blitter.dstaddr, w, h, src, dst); |
286 | dump_blitter(); |
3d295a9f |
287 | } |
288 | |
4d045184 |
289 | // FIXME: pass real dimensions to blitters |
7000b522 |
290 | static void mlc_flip(void *src, int bpp, int stride) |
6720e4e4 |
291 | { |
6ca08393 |
292 | static int old_bpp; |
6720e4e4 |
293 | |
4d045184 |
294 | // only pass pal to host if it's dirty |
7000b522 |
295 | if (bpp <= 8 && mmsp2.v.dirty_pal) { |
296 | if (mmsp2.v.dirty_pal == DIRTY_PAL_MMSP2) |
297 | host_video_update_pal32(mmsp2.mlc_stl_pallt_d32); |
298 | else |
299 | host_video_update_pal16(mmsp2.mlcpalette); |
300 | mmsp2.v.dirty_pal = 0; |
6720e4e4 |
301 | } |
302 | |
6ca08393 |
303 | if (bpp != old_bpp) { |
304 | host_video_change_bpp(bpp); |
305 | old_bpp = bpp; |
306 | } |
307 | |
6720e4e4 |
308 | switch (bpp) { |
309 | case 4: |
7000b522 |
310 | host_video_blit4(src, 320, 240, stride); |
6720e4e4 |
311 | break; |
312 | |
313 | case 8: |
7000b522 |
314 | host_video_blit8(src, 320, 240, stride); |
6720e4e4 |
315 | break; |
316 | |
317 | case 16: |
7000b522 |
318 | host_video_blit16(src, 320, 240, stride); |
6720e4e4 |
319 | break; |
320 | |
321 | case 24: |
322 | // TODO |
323 | break; |
324 | } |
6720e4e4 |
325 | } |
326 | |
c1ffd5ba |
327 | #define ts_add_nsec(ts, ns) { \ |
328 | ts.tv_nsec += ns; \ |
329 | if (ts.tv_nsec >= 1000000000) { \ |
330 | ts.tv_sec++; \ |
331 | ts.tv_nsec -= 1000000000; \ |
332 | } \ |
333 | } |
334 | |
db9191ed |
335 | static int fb_sync_thread_paused; |
336 | |
c1ffd5ba |
337 | static void *fb_sync_thread(void *arg) |
338 | { |
339 | int invalid_fb_addr = 1; |
340 | int manual_refresh = 0; |
7000b522 |
341 | int frame_counter = 0; |
c1ffd5ba |
342 | struct timespec ts; |
343 | int ret, wait_ret; |
344 | |
345 | //ret = pthread_setschedprio(pthread_self(), -1); |
346 | //log("pthread_setschedprio %d\n", ret); |
347 | //ret = setpriority(PRIO_PROCESS, 0, -1); |
348 | //log("setpriority %d\n", ret); |
349 | |
350 | ret = clock_gettime(CLOCK_REALTIME, &ts); |
351 | if (ret != 0) { |
352 | perror(PFX "clock_gettime"); |
353 | exit(1); |
354 | } |
355 | |
356 | while (1) { |
357 | u8 *gp2x_fb, *gp2x_fb_end; |
c1ffd5ba |
358 | |
359 | ret = pthread_mutex_lock(&fb_mutex); |
360 | wait_ret = pthread_cond_timedwait(&fb_cond, &fb_mutex, &ts); |
361 | ret |= pthread_mutex_unlock(&fb_mutex); |
dc90c02d |
362 | |
c1ffd5ba |
363 | if (ret != 0) { |
364 | err("fb_thread: mutex error: %d\n", ret); |
365 | sleep(1); |
7000b522 |
366 | goto check_keys; |
c1ffd5ba |
367 | } |
368 | if (wait_ret != 0 && wait_ret != ETIMEDOUT) { |
369 | err("fb_thread: cond error: %d\n", wait_ret); |
370 | sleep(1); |
7000b522 |
371 | goto check_keys; |
c1ffd5ba |
372 | } |
db9191ed |
373 | if (fb_sync_thread_paused) { |
374 | ts_add_nsec(ts, 100000000); |
7000b522 |
375 | goto check_keys; |
db9191ed |
376 | } |
c1ffd5ba |
377 | |
378 | if (wait_ret != ETIMEDOUT) { |
379 | clock_gettime(CLOCK_REALTIME, &ts); |
380 | ts_add_nsec(ts, 50000000); |
381 | manual_refresh++; |
382 | if (manual_refresh == 2) |
4d045184 |
383 | dbg("fb_thread: switch to manual refresh\n"); |
c1ffd5ba |
384 | } else { |
385 | ts_add_nsec(ts, 16666667); |
386 | if (manual_refresh > 1) |
4d045184 |
387 | dbg("fb_thread: switch to auto refresh\n"); |
c1ffd5ba |
388 | manual_refresh = 0; |
389 | } |
390 | |
c1ffd5ba |
391 | gp2x_fb = uppermem_lookup(mmsp2.mlc_stl_adr, &gp2x_fb_end); |
7000b522 |
392 | if (gp2x_fb == NULL || gp2x_fb + 320*240 * mmsp2.v.bpp / 8 > gp2x_fb_end) { |
c1ffd5ba |
393 | if (!invalid_fb_addr) { |
394 | err("fb_thread: %08x is out of range\n", mmsp2.mlc_stl_adr); |
395 | invalid_fb_addr = 1; |
396 | } |
397 | continue; |
398 | } |
399 | |
7000b522 |
400 | invalid_fb_addr = 0; |
401 | mlc_flip(gp2x_fb, mmsp2.v.bpp, mmsp2.v.stride); |
402 | |
403 | frame_counter++; |
404 | if (frame_counter & 0x0f) |
405 | continue; |
406 | |
407 | check_keys: |
408 | // this is to check for kill key, in case main thread hung |
409 | // or something else went wrong. |
410 | pollux.btn_state = host_read_btns(); |
c1ffd5ba |
411 | } |
412 | } |
413 | |
db9191ed |
414 | static void fb_thread_pause(void) |
415 | { |
416 | fb_sync_thread_paused = 1; |
417 | // wait until it finishes last refresh |
418 | // that it might be doing now |
419 | usleep(10000); |
420 | } |
421 | |
422 | static void fb_thread_resume(void) |
423 | { |
424 | fb_sync_thread_paused = 0; |
425 | } |
426 | |
7000b522 |
427 | static u32 xread32_io_cmn(u32 a, u32 *handled) |
428 | { |
429 | u32 d = 0; |
430 | |
431 | *handled = 1; |
432 | switch (a) { |
433 | // Wiz stuff |
434 | case 0x402c: // MLCVSTRIDE0 |
435 | case 0x4060: // MLCVSTRIDE1 |
436 | d = pollux.v.stride; |
437 | break; |
438 | case 0x4038: // MLCADDRESS0 |
439 | case 0x406c: // MLCADDRESS1 |
440 | d = pollux.mlc_stl_adr; |
441 | break; |
442 | // wiz_lib reads: |
443 | // ???? ???? YXBA DURiLe ???? VdVuMS LR?? ???? |
444 | // | GPIOC[31:16] | GPIOB[31:16] | |
445 | case 0xa058: // GPIOBPAD |
830ba1bf |
446 | d = (pollux.btn_state >> 1) & 0x0100; |
447 | d |= (pollux.btn_state << 1) & 0x0200; |
7000b522 |
448 | d |= (pollux.btn_state >> 3) & 0x0080; |
449 | d |= (pollux.btn_state >> 5) & 0x0040; |
450 | d |= (pollux.btn_state >> 6) & 0x0c00; |
451 | d <<= 16; |
452 | d = ~d; |
453 | break; |
454 | case 0xa098: // GPIOCPAD |
455 | pollux.btn_state = host_read_btns(); |
456 | d = (pollux.btn_state >> 8) & 0x00f0; |
457 | d |= (pollux.btn_state >> 1) & 0x0008; |
458 | d |= (pollux.btn_state << 2) & 0x0004; |
459 | d |= (pollux.btn_state >> 5) & 0x0002; |
460 | d |= (pollux.btn_state >> 2) & 0x0001; |
461 | d <<= 16; |
462 | d = ~d; |
463 | break; |
464 | default: |
465 | *handled = 0; |
466 | break; |
467 | } |
468 | |
469 | return d; |
470 | } |
471 | |
3d295a9f |
472 | static u32 xread8(u32 a) |
473 | { |
6720e4e4 |
474 | iolog("r8 ", a, 0, 8); |
475 | iolog_unh("r8 ", a, 0, 8); |
3d295a9f |
476 | return 0; |
11913091 |
477 | } |
478 | |
479 | static u32 xread16(u32 a) |
480 | { |
6720e4e4 |
481 | static u32 fudge, old_a; |
482 | u32 d = 0, t; |
483 | |
484 | if ((a & 0xffff0000) == 0x7f000000) { |
485 | u32 a_ = a & 0xffff; |
486 | switch (a_) { |
487 | case 0x0910: // FPLL |
488 | case 0x0912: |
489 | d = 0x9407; |
490 | break; |
491 | // minilib reads as: |
7000b522 |
492 | // 0000 P000 VuVd00 0000 YXBA RLSeSt 0Ri0D 0Le0U |
493 | // | GPIOD |GPIOC[8:15]|GPIOM[0:7] | |
6720e4e4 |
494 | // /dev/GPIO: |
495 | // ... 0PVdVu ... |
496 | case 0x1184: // GPIOC |
497 | d = ~mmsp2.btn_state & 0xff00; |
498 | d |= 0x00ff; |
499 | break; |
500 | case 0x1186: // GPIOD |
501 | t = ~mmsp2.btn_state; |
502 | d = (t >> 9) & 0x0080; |
503 | d |= (t >> 11) & 0x0040; |
504 | d |= (t >> 7) & 0x0800; |
505 | d |= 0x373b; |
506 | break; |
507 | case 0x1198: // GPIOM |
508 | mmsp2.btn_state = host_read_btns(); |
509 | d = ~mmsp2.btn_state & 0xff; |
510 | d |= 0x01aa; |
511 | break; |
c1ffd5ba |
512 | case 0x1836: // reserved |
513 | d = 0x2330; |
514 | break; |
515 | case 0x2816: // DPC_X_MAX |
516 | d = 319; |
517 | break; |
518 | case 0x2818: // DPC_Y_MAX |
519 | d = 239; |
520 | break; |
6720e4e4 |
521 | case 0x28da: |
522 | d = mmsp2.mlc_stl_cntl; |
523 | break; |
c1ffd5ba |
524 | case 0x290e: |
525 | case 0x2912: |
526 | d = mmsp2.mlc_stl_adrl; |
527 | break; |
528 | case 0x2910: |
529 | case 0x2914: |
530 | d = mmsp2.mlc_stl_adrh; |
531 | break; |
6720e4e4 |
532 | case 0x2958: |
533 | d = mmsp2.mlc_stl_pallt_a; |
534 | break; |
7000b522 |
535 | |
6720e4e4 |
536 | default: |
7000b522 |
537 | d = xread32_io_cmn(a_, &t); |
538 | if (!t) |
539 | goto unk; |
540 | if (!(a_ & 2)) |
541 | d >>= 16; |
542 | break; |
6720e4e4 |
543 | } |
544 | goto out; |
545 | } |
546 | |
7000b522 |
547 | unk: |
6720e4e4 |
548 | if (a == old_a) { |
549 | d = fudge; |
550 | fudge = ~fudge; |
551 | } |
552 | old_a = a; |
553 | iolog_unh("r16", a, d & 0xffff, 16); |
554 | |
555 | out: |
556 | d &= 0xffff; |
557 | iolog("r16", a, d, 16); |
558 | return d; |
11913091 |
559 | } |
560 | |
561 | static u32 xread32(u32 a) |
562 | { |
3d295a9f |
563 | u32 d = 0; |
dc90c02d |
564 | if ((a & 0xfff00000) == 0x7f000000) { |
565 | u32 a_ = a & 0xffff; |
3d3fa63b |
566 | struct timespec ts; |
567 | u64 t64; |
7000b522 |
568 | u32 t; |
3d3fa63b |
569 | |
dc90c02d |
570 | switch (a_) { |
571 | case 0x0a00: // TCOUNT, 1/7372800s |
3d3fa63b |
572 | clock_gettime(CLOCK_REALTIME, &ts); |
573 | t64 = (u64)ts.tv_sec * 1000000000 + ts.tv_nsec; |
574 | // t * 7372800.0 / 1000000000 * 0x100000000 ~= t * 31665935 |
575 | t64 *= 31665935; |
576 | d = t64 >> 32; |
dc90c02d |
577 | break; |
7000b522 |
578 | |
579 | default: |
580 | d = xread32_io_cmn(a_, &t); |
581 | if (!t) |
582 | goto unh; |
583 | break; |
dc90c02d |
584 | } |
7000b522 |
585 | goto out; |
dc90c02d |
586 | } |
3d295a9f |
587 | if ((a & 0xfff00000) == 0x7f100000) { |
588 | u32 *bl = &blitter.dstctrl; |
6720e4e4 |
589 | u32 a_ = a & 0xfff; |
590 | if (a_ < 0x40) { |
591 | d = bl[a_ / 4]; |
592 | if (a_ == 0x34) |
593 | d = 0; // not busy |
594 | goto out; |
595 | } |
3d295a9f |
596 | } |
7000b522 |
597 | |
598 | unh: |
6720e4e4 |
599 | iolog_unh("r32", a, d, 32); |
600 | |
601 | out: |
602 | iolog("r32", a, d, 32); |
3d295a9f |
603 | return d; |
11913091 |
604 | } |
605 | |
606 | static void xwrite8(u32 a, u32 d) |
607 | { |
6720e4e4 |
608 | iolog("w8 ", a, d, 8); |
609 | iolog_unh("w8 ", a, d, 8); |
11913091 |
610 | } |
611 | |
612 | static void xwrite16(u32 a, u32 d) |
613 | { |
6720e4e4 |
614 | iolog("w16", a, d, 16); |
3d295a9f |
615 | if ((a & 0xfff00000) == 0x7f000000) { |
6720e4e4 |
616 | u32 a_ = a & 0xffff; |
617 | switch (a_) { |
7000b522 |
618 | case 0x28da: { |
619 | int mode; |
620 | mmsp2.mlc_stl_cntl = d | 0xaa; |
621 | mode = (d >> 9) & 3; |
622 | mmsp2.v.bpp = mode ? mode * 8 : 4; |
623 | break; |
624 | } |
625 | case 0x290c: |
626 | mmsp2.v.stride = d; |
627 | return; |
628 | case 0x290e: |
629 | case 0x2910: |
630 | // odd addresses don't affect LCD. What about TV? |
631 | return; |
632 | case 0x2912: |
633 | mmsp2.mlc_stl_adrl = d; |
634 | return; |
635 | case 0x2914: |
636 | mmsp2.mlc_stl_adrh = d; |
637 | if (mmsp2.mlc_stl_adr != mmsp2.old_mlc_stl_adr) |
638 | // ask for refresh |
639 | pthread_cond_signal(&fb_cond); |
640 | mmsp2.old_mlc_stl_adr = mmsp2.mlc_stl_adr; |
641 | return; |
642 | case 0x2958: |
643 | mmsp2.mlc_stl_pallt_a = d & 0x1ff; |
644 | return; |
645 | case 0x295a: |
646 | mmsp2.mlc_stl_pallt_d[mmsp2.mlc_stl_pallt_a++] = d; |
647 | mmsp2.mlc_stl_pallt_a &= 0x1ff; |
648 | mmsp2.v.dirty_pal = DIRTY_PAL_MMSP2; |
649 | return; |
3d295a9f |
650 | } |
3d295a9f |
651 | } |
6720e4e4 |
652 | iolog_unh("w16", a, d, 16); |
11913091 |
653 | } |
654 | |
655 | static void xwrite32(u32 a, u32 d) |
656 | { |
6720e4e4 |
657 | iolog("w32", a, d, 32); |
658 | |
7000b522 |
659 | if ((a & 0xfff00000) == 0x7f000000) { |
660 | u32 a_ = a & 0xffff; |
661 | switch (a_) { |
662 | // Wiz |
663 | case 0x4024: // MLCCONTROL0 |
664 | case 0x4058: // MLCCONTROL1 |
665 | pollux.mlccontrol = d; |
666 | if (!(d & 0x20)) |
667 | return; // layer not enabled |
668 | if ((d >> 16) == 0x443A) |
669 | pollux.v.bpp = 8; |
670 | else |
671 | pollux.v.bpp = 16; |
672 | return; |
673 | case 0x402c: // MLCVSTRIDE0 |
674 | case 0x4060: // MLCVSTRIDE1 |
675 | pollux.v.stride = d; |
676 | return; |
677 | case 0x4038: // MLCADDRESS0 |
678 | case 0x406c: // MLCADDRESS1 |
679 | pollux.mlc_stl_adr = d; |
680 | if (d != mmsp2.old_mlc_stl_adr) |
681 | // ask for refresh |
682 | pthread_cond_signal(&fb_cond); |
683 | mmsp2.old_mlc_stl_adr = d; |
684 | return; |
685 | case 0x403c: // MLCPALETTE0 |
686 | case 0x4070: // MLCPALETTE1 |
687 | pollux.mlcpalette[d >> 24] = d; |
688 | pollux.v.dirty_pal = DIRTY_PAL_POLLUX; |
689 | return; |
690 | } |
691 | } |
3d295a9f |
692 | if ((a & 0xfff00000) == 0x7f100000) { |
693 | u32 *bl = &blitter.dstctrl; |
6720e4e4 |
694 | u32 a_ = a & 0xfff; |
695 | if (a_ < 0x40) { |
696 | bl[a_ / 4] = d; |
697 | if (a_ == 0x34 && (d & 1)) |
698 | blitter_do(); |
699 | return; |
700 | } |
3d295a9f |
701 | } |
6720e4e4 |
702 | iolog_unh("w32", a, d, 32); |
11913091 |
703 | } |
704 | |
6720e4e4 |
705 | #define LINKPAGE_SIZE 0x1000 |
706 | #define LINKPAGE_COUNT 4 |
707 | #define LINKPAGE_ALLOC (LINKPAGE_SIZE * LINKPAGE_COUNT) |
708 | |
709 | struct op_context { |
710 | u32 pc; |
711 | u32 op; |
712 | u32 code[0]; |
713 | }; |
714 | |
3d3fa63b |
715 | struct op_linkpage { |
6720e4e4 |
716 | void (*handler)(struct op_context *op_ctx); |
717 | u32 code[0]; |
718 | }; |
719 | |
3d3fa63b |
720 | struct op_stackframe { |
721 | u32 saved_regs[15]; |
722 | u32 cpsr; |
723 | }; |
724 | |
725 | static struct op_linkpage *g_linkpage; |
6720e4e4 |
726 | static u32 *g_code_ptr; |
727 | static int g_linkpage_count; |
728 | |
11913091 |
729 | #define BIT_SET(v, b) (v & (1 << (b))) |
730 | |
3d3fa63b |
731 | void emu_handle_op(struct op_context *op_ctx, struct op_stackframe *sframe) |
11913091 |
732 | { |
3d3fa63b |
733 | u32 *regs = sframe->saved_regs; |
6720e4e4 |
734 | u32 op = op_ctx->op; |
11913091 |
735 | u32 t, shift, ret, addr; |
736 | int rn, rd; |
737 | |
738 | rd = (op & 0x0000f000) >> 12; |
739 | rn = (op & 0x000f0000) >> 16; |
740 | |
741 | if ((op & 0x0f200090) == 0x01000090) { // AM3: LDRH, STRH |
86418a84 |
742 | if (!BIT_SET(op, 5)) // !H |
743 | goto unhandled; |
744 | if (BIT_SET(op, 6) && !BIT_SET(op, 20)) // S && !L |
11913091 |
745 | goto unhandled; |
746 | |
747 | if (BIT_SET(op, 22)) // imm offset |
748 | t = ((op & 0xf00) >> 4) | (op & 0x0f); |
749 | else // reg offset |
750 | t = regs[op & 0x000f]; |
751 | |
752 | if (!BIT_SET(op, 23)) |
753 | t = -t; |
754 | addr = regs[rn] + t; |
755 | |
756 | if (BIT_SET(op, 20)) { // Load |
757 | ret = xread16(addr); |
86418a84 |
758 | if (BIT_SET(op, 6)) { // S |
759 | ret <<= 16; |
760 | ret = (signed int)ret >> 16; |
761 | } |
11913091 |
762 | regs[rd] = ret; |
763 | } |
764 | else |
765 | xwrite16(addr, regs[rd]); |
766 | } |
767 | else if ((op & 0x0d200000) == 0x05000000) { // AM2: LDR[B], STR[B] |
768 | if (BIT_SET(op, 25)) { // reg offs |
769 | if (BIT_SET(op, 4)) |
770 | goto unhandled; |
771 | |
772 | t = regs[op & 0x000f]; |
773 | shift = (op & 0x0f80) >> 7; |
774 | switch ((op & 0x0060) >> 5) { |
775 | case 0: t = t << shift; break; |
776 | case 1: t = t >> (shift + 1); break; |
777 | case 2: t = (signed int)t >> (shift + 1); break; |
778 | case 3: goto unhandled; // I'm just lazy |
779 | } |
780 | } |
781 | else // imm offs |
782 | t = op & 0x0fff; |
783 | |
784 | if (!BIT_SET(op, 23)) |
785 | t = -t; |
786 | addr = regs[rn] + t; |
787 | |
788 | if (BIT_SET(op, 20)) { // Load |
789 | if (BIT_SET(op, 22)) // Byte |
790 | ret = xread8(addr); |
791 | else |
792 | ret = xread32(addr); |
793 | regs[rd] = ret; |
794 | } |
795 | else { |
796 | if (BIT_SET(op, 22)) // Byte |
797 | xwrite8(addr, regs[rd]); |
798 | else |
799 | xwrite32(addr, regs[rd]); |
800 | } |
801 | } |
802 | else |
803 | goto unhandled; |
804 | |
86418a84 |
805 | #if 0 |
11913091 |
806 | if (addr != addr_check) { |
807 | fprintf(stderr, "bad calculated addr: %08x vs %08x\n", addr, addr_check); |
808 | abort(); |
809 | } |
86418a84 |
810 | #endif |
11913091 |
811 | return; |
812 | |
813 | unhandled: |
6720e4e4 |
814 | err("unhandled IO op %08x @ %08x\n", op, op_ctx->pc); |
86418a84 |
815 | } |
816 | |
817 | static u32 make_offset12(u32 *pc, u32 *target) |
818 | { |
819 | int lp_offs, u = 1; |
820 | |
821 | lp_offs = (char *)target - (char *)pc - 2*4; |
822 | if (lp_offs < 0) { |
823 | lp_offs = -lp_offs; |
824 | u = 0; |
825 | } |
826 | if (lp_offs >= LINKPAGE_SIZE) { |
6720e4e4 |
827 | err("linkpage too far: %d\n", lp_offs); |
86418a84 |
828 | abort(); |
829 | } |
830 | |
831 | return (u << 23) | lp_offs; |
832 | } |
833 | |
6720e4e4 |
834 | static u32 make_jmp(u32 *pc, u32 *target, int bl) |
86418a84 |
835 | { |
836 | int jmp_val; |
837 | |
838 | jmp_val = target - pc - 2; |
839 | if (jmp_val < (int)0xff000000 || jmp_val > 0x00ffffff) { |
6720e4e4 |
840 | err("jump out of range (%p -> %p)\n", pc, target); |
86418a84 |
841 | abort(); |
842 | } |
843 | |
6720e4e4 |
844 | return 0xea000000 | (bl << 24) | (jmp_val & 0x00ffffff); |
86418a84 |
845 | } |
846 | |
847 | static void emit_op(u32 op) |
848 | { |
849 | *g_code_ptr++ = op; |
850 | } |
851 | |
852 | static void emit_op_io(u32 op, u32 *target) |
853 | { |
854 | op |= make_offset12(g_code_ptr, target); |
855 | emit_op(op); |
856 | } |
857 | |
6720e4e4 |
858 | static void init_linkpage(void) |
859 | { |
3d3fa63b |
860 | g_linkpage->handler = emu_call_handle_op; |
6720e4e4 |
861 | g_code_ptr = g_linkpage->code; |
6720e4e4 |
862 | } |
863 | |
86418a84 |
864 | static void segv_sigaction(int num, siginfo_t *info, void *ctx) |
865 | { |
866 | struct ucontext *context = ctx; |
867 | u32 *regs = (u32 *)&context->uc_mcontext.arm_r0; |
868 | u32 *pc = (u32 *)regs[15]; |
6720e4e4 |
869 | struct op_context *op_ctx; |
7fd42181 |
870 | int i, lp_size; |
86418a84 |
871 | |
6720e4e4 |
872 | if (((regs[15] ^ (u32)&segv_sigaction) & 0xff000000) == 0 || // PC is in our segment or |
873 | (((regs[15] ^ (u32)g_linkpage) & ~(LINKPAGE_ALLOC - 1)) == 0) || // .. in linkpage |
874 | ((long)info->si_addr & 0xffe00000) != 0x7f000000) // faulting not where expected |
86418a84 |
875 | { |
876 | // real crash - time to die |
6720e4e4 |
877 | err("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]); |
7fd42181 |
878 | for (i = 0; i < 8; i++) |
4d045184 |
879 | dbg(" r%d=%08x r%2d=%08x\n", i, regs[i], i+8, regs[i+8]); |
86418a84 |
880 | signal(num, SIG_DFL); |
881 | raise(num); |
c1ffd5ba |
882 | return; |
86418a84 |
883 | } |
0881206b |
884 | segvlog("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]); |
86418a84 |
885 | |
886 | // spit PC and op |
6720e4e4 |
887 | op_ctx = (void *)g_code_ptr; |
888 | op_ctx->pc = (u32)pc; |
889 | op_ctx->op = *pc; |
890 | g_code_ptr = &op_ctx->code[0]; |
86418a84 |
891 | |
892 | // emit jump to code ptr |
6720e4e4 |
893 | *pc = make_jmp(pc, g_code_ptr, 0); |
86418a84 |
894 | |
895 | // generate code: |
3d3fa63b |
896 | emit_op (0xe50d0000 + 0xf00 - 4 * 0); // str r0, [sp, #(-0xf00 + r0_offs)] |
897 | emit_op (0xe50de000 + 0xf00 - 4 * 14); // str lr, [sp, #(-0xf00 + lr_offs)] |
6720e4e4 |
898 | emit_op (0xe24f0000 + (g_code_ptr - (u32 *)op_ctx + 2) * 4); // sub r0, pc, #op_ctx |
3d3fa63b |
899 | emit_op (0xe1a0e00f); // mov lr, pc |
900 | emit_op_io(0xe51ff000, (u32 *)&g_linkpage->handler); // ldr pc, =handle_op |
901 | emit_op (0xe51de000 + 0xf00 - 4 * 14); // ldr lr, [sp, #(-0xf00 + lr_offs)] |
6720e4e4 |
902 | emit_op (make_jmp(g_code_ptr, pc + 1, 0)); // jmp <back> |
86418a84 |
903 | |
904 | // sync caches |
905 | sys_cacheflush(pc, pc + 1); |
906 | sys_cacheflush(g_linkpage, g_code_ptr); |
907 | |
908 | lp_size = (char *)g_code_ptr - (char *)g_linkpage; |
0881206b |
909 | segvlog("code #%d %d/%d\n", g_linkpage_count, lp_size, LINKPAGE_SIZE); |
86418a84 |
910 | |
911 | if (lp_size + 13*4 > LINKPAGE_SIZE) { |
912 | g_linkpage_count++; |
913 | if (g_linkpage_count >= LINKPAGE_COUNT) { |
6720e4e4 |
914 | err("too many linkpages needed\n"); |
86418a84 |
915 | abort(); |
916 | } |
917 | g_linkpage = (void *)((char *)g_linkpage + LINKPAGE_SIZE); |
918 | init_linkpage(); |
919 | } |
920 | //handle_op(regs[15], op, regs, (u32)info->si_addr); |
921 | //regs[15] += 4; |
922 | } |
11913091 |
923 | |
924 | void emu_init(void *map_bottom) |
925 | { |
7fd42181 |
926 | sigaction_t segv_action = { |
11913091 |
927 | .sa_sigaction = segv_sigaction, |
928 | .sa_flags = SA_SIGINFO, |
929 | }; |
c1ffd5ba |
930 | pthread_t tid; |
6720e4e4 |
931 | void *pret; |
932 | int ret; |
11913091 |
933 | |
830ba1bf |
934 | #ifdef PND |
935 | if (geteuid() == 0) { |
936 | fprintf(stderr, "don't try to run as root, device registers or memory " |
937 | "might get trashed crashing the OS or even damaging the device.\n"); |
938 | exit(1); |
939 | } |
940 | #endif |
941 | |
86418a84 |
942 | g_linkpage = (void *)(((u32)map_bottom - LINKPAGE_ALLOC) & ~0xfff); |
7fd42181 |
943 | pret = mmap(g_linkpage, LINKPAGE_ALLOC, PROT_READ|PROT_WRITE, |
4d045184 |
944 | MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0); |
6720e4e4 |
945 | if (pret != g_linkpage) { |
946 | perror(PFX "mmap linkpage"); |
11913091 |
947 | exit(1); |
948 | } |
c1ffd5ba |
949 | log("linkpages @ %p\n", g_linkpage); |
86418a84 |
950 | init_linkpage(); |
11913091 |
951 | |
4d045184 |
952 | // host stuff |
953 | ret = host_init(); |
954 | if (ret != 0) { |
955 | err("can't init host\n"); |
3d3fa63b |
956 | exit(1); |
957 | } |
958 | |
4d045184 |
959 | ret = host_video_init(NULL, 0); |
2ce69bdf |
960 | if (ret != 0) { |
4d045184 |
961 | err("can't init host video\n"); |
962 | exit(1); |
963 | } |
964 | |
5853ddbd |
965 | // TODO: check if this really fails on Wiz.. |
4d045184 |
966 | mmsp2.umem = mmap(NULL, 0x2000000, PROT_READ|PROT_WRITE|PROT_EXEC, |
967 | MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
5853ddbd |
968 | #ifdef WIZ |
969 | if (mmsp2.umem == MAP_FAILED) { |
970 | // we are short on memmory on Wiz, need special handling |
971 | extern void *host_mmap_upper(void); |
972 | mmsp2.umem = host_mmap_upper(); |
973 | } |
4d045184 |
974 | #endif |
975 | if (mmsp2.umem == MAP_FAILED) { |
976 | perror(PFX "mmap upper mem"); |
3d295a9f |
977 | exit(1); |
978 | } |
c1ffd5ba |
979 | |
980 | ret = pthread_create(&tid, NULL, fb_sync_thread, NULL); |
981 | if (ret != 0) { |
982 | err("failed to create fb_sync_thread: %d\n", ret); |
983 | exit(1); |
984 | } |
985 | pthread_detach(tid); |
986 | |
7000b522 |
987 | // defaults |
c1ffd5ba |
988 | mmsp2.mlc_stl_adr = 0x03101000; // fb2 is at 0x03381000 |
7fd42181 |
989 | mmsp2.mlc_stl_cntl = 0x4ab; // 16bpp, region 1 active |
7000b522 |
990 | mmsp2.v.width = 320; |
991 | mmsp2.v.height = 240; |
992 | mmsp2.v.stride = 320*2; |
993 | mmsp2.v.bpp = 16; |
994 | mmsp2.v.dirty_pal = 1; |
c1ffd5ba |
995 | |
996 | sigemptyset(&segv_action.sa_mask); |
997 | sigaction(SIGSEGV, &segv_action, NULL); |
3d295a9f |
998 | } |
999 | |
1000 | int emu_read_gpiodev(void *buf, int count) |
1001 | { |
4d045184 |
1002 | if (count <= 0) { |
6720e4e4 |
1003 | err("gpiodev read %d?\n", count); |
3d295a9f |
1004 | return -1; |
1005 | } |
4d045184 |
1006 | if (count > 4) |
1007 | count = 4; |
3d295a9f |
1008 | |
4d045184 |
1009 | mmsp2.btn_state = host_read_btns(); |
1010 | memcpy(buf, &mmsp2.btn_state, count); |
1011 | return count; |
11913091 |
1012 | } |
1013 | |
dc90c02d |
1014 | static void *emu_mmap_dev(unsigned int length, int prot, int flags, unsigned int offset) |
11913091 |
1015 | { |
3d3fa63b |
1016 | u8 *umem, *umem_end; |
11913091 |
1017 | |
3d295a9f |
1018 | // SoC regs |
7000b522 |
1019 | if ((offset & ~0x1ffff) == 0xc0000000) { |
11913091 |
1020 | return mmap((void *)0x7f000000, length, PROT_NONE, |
1021 | MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0); |
1022 | } |
7000b522 |
1023 | // MMSP2 blitter |
11913091 |
1024 | if ((offset & ~0xffff) == 0xe0020000) { |
1025 | return mmap((void *)0x7f100000, length, PROT_NONE, |
1026 | MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0); |
1027 | } |
3d295a9f |
1028 | // upper mem |
3d3fa63b |
1029 | if ((offset & 0xfe000000) != 0x02000000) { |
6720e4e4 |
1030 | err("unexpected devmem mmap @ %08x\n", offset); |
dc90c02d |
1031 | errno = EINVAL; |
3d295a9f |
1032 | return MAP_FAILED; |
1033 | } |
1034 | |
3d3fa63b |
1035 | umem = uppermem_lookup(offset, &umem_end); |
1036 | if (umem + length > umem_end) |
1037 | err("warning: uppermem @ %08x overflows by %d bytes\n", |
1038 | offset, umem + length - umem_end); |
1039 | |
1040 | dbg("upper mem @ %08x %d\n", offset, length); |
1041 | return umem; |
11913091 |
1042 | } |
1043 | |
dc90c02d |
1044 | void *emu_do_mmap(unsigned int length, int prot, int flags, int fd, unsigned int offset) |
1045 | { |
1046 | if (fd == FAKEDEV_MEM) |
1047 | return emu_mmap_dev(length, prot, flags, offset); |
1048 | |
1049 | if (fd == FAKEDEV_FB0) |
1050 | return emu_mmap_dev(length, prot, flags, offset + 0x03101000); |
1051 | |
1052 | if (fd == FAKEDEV_FB1) |
1053 | return emu_mmap_dev(length, prot, flags, offset + 0x03381000); |
1054 | |
7fd42181 |
1055 | err("bad/ni mmap(?, %d, %x, %x, %d, %08x)\n", length, prot, flags, fd, offset); |
dc90c02d |
1056 | errno = EINVAL; |
1057 | return MAP_FAILED; |
1058 | } |
1059 | |
7000b522 |
1060 | static void emu_sound_open(int fd) |
1061 | { |
1062 | #ifdef PND |
1063 | int ret, frag; |
1064 | |
1065 | // set default buffer size to 16 * 1K |
1066 | frag = (16<<16) | 10; // 16K |
1067 | ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag); |
1068 | if (ret != 0) { |
1069 | err("snd ioctl SETFRAGMENT %08x: ", frag); |
1070 | perror(NULL); |
1071 | } |
1072 | #endif |
1073 | } |
1074 | |
dc90c02d |
1075 | static int emu_sound_ioctl(int fd, int request, void *argp) |
1076 | { |
1077 | int *arg = argp; |
1078 | |
1079 | #if 0 |
1080 | dbg("snd ioctl(%d, %08x, %p)", fd, request, argp); |
1081 | if (arg != NULL) |
1082 | dbg_c(" [%d]", *arg); |
1083 | dbg_c("\n"); |
1084 | #endif |
1085 | |
1086 | /* People set strange frag settings on GP2X, which even manage |
1087 | * to break audio on pandora (causes writes to fail). |
1088 | * Catch this and set to something that works. */ |
7000b522 |
1089 | switch(request) { |
1090 | case SNDCTL_DSP_SETFRAGMENT: { |
1091 | int ret, bsize, frag, frag_cnt; |
1092 | if (arg == NULL) |
1093 | break; |
dc90c02d |
1094 | |
7000b522 |
1095 | frag = *arg & 0xffff; |
1096 | frag_cnt = *arg >> 16; |
1097 | bsize = frag_cnt << frag; |
1098 | if (frag < 10 || bsize < 4096*4 || bsize > 4096*4*2) { |
1099 | /* |
1100 | * ~4ms. gpSP wants small buffers or else it stutters |
1101 | * because of it's audio thread sync stuff |
1102 | * XXX: hardcoding, as low samplerates will result in small fragment size, |
1103 | * which itself causes ALSA stall and hangs the program. |
1104 | * Also some apps change samplerate without reopening /dev/dsp, |
1105 | * which causes ALSA to reject SNDCTL_DSP_SETFRAGMENT. |
1106 | */ |
1107 | bsize = 44100 / 250 * 4; |
1108 | |
1109 | for (frag = 0; bsize; bsize >>= 1, frag++) |
1110 | ; |
1111 | |
1112 | frag_cnt = 16; |
1113 | } |
dc90c02d |
1114 | |
7000b522 |
1115 | frag |= frag_cnt << 16; |
1116 | ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag); |
1117 | if (ret != 0) { |
1118 | err("snd ioctl SETFRAGMENT %08x: ", frag); |
1119 | perror(NULL); |
1120 | } |
1121 | // indicate success even if we fail (because of ALSA mostly), |
1122 | // things like MikMod will bail out otherwise. |
1123 | return 0; |
dc90c02d |
1124 | } |
7000b522 |
1125 | case SNDCTL_DSP_SYNC: |
1126 | // Franxis tends to use sync/write loops, bad idea under ALSA |
1127 | return 0; |
1128 | default: |
1129 | break; |
dc90c02d |
1130 | } |
dc90c02d |
1131 | |
1132 | return ioctl(fd, request, argp); |
1133 | } |
1134 | |
dc90c02d |
1135 | int emu_do_ioctl(int fd, int request, void *argp) |
1136 | { |
1137 | if (fd == emu_interesting_fds[IFD_SOUND].fd) |
1138 | return emu_sound_ioctl(fd, request, argp); |
1139 | |
7fd42181 |
1140 | switch (fd) { |
1141 | /* *********************** */ |
1142 | case FAKEDEV_FB0: |
1143 | case FAKEDEV_FB1: |
1144 | if (argp == NULL) |
1145 | goto fail; |
dc90c02d |
1146 | |
dc90c02d |
1147 | switch (request) { |
1148 | case FBIOGET_FSCREENINFO: { |
1149 | struct fb_fix_screeninfo *fix = argp; |
1150 | |
1151 | memset(fix, 0, sizeof(*fix)); |
1152 | strcpy(fix->id, "mmsp2_RGB0"); |
1153 | fix->type = FB_TYPE_PACKED_PIXELS; |
1154 | fix->accel = FB_ACCEL_NONE; |
7fd42181 |
1155 | fix->visual = FB_VISUAL_TRUECOLOR; |
1156 | fix->line_length = 320*2; |
dc90c02d |
1157 | fix->smem_start = (fd == FAKEDEV_FB0) ? 0x03101000 : 0x03381000; |
1158 | fix->smem_len = 320*240*2; |
1159 | return 0; |
1160 | } |
1161 | case FBIOGET_VSCREENINFO: { |
1162 | struct fb_var_screeninfo *var = argp; |
7fd42181 |
1163 | static const struct fb_bitfield fbb_red = { offset: 11, length: 5, }; |
1164 | static const struct fb_bitfield fbb_green = { offset: 5, length: 6, }; |
1165 | static const struct fb_bitfield fbb_blue = { offset: 0, length: 5, }; |
dc90c02d |
1166 | |
1167 | memset(var, 0, sizeof(*var)); |
1168 | var->activate = FB_ACTIVATE_NOW; |
1169 | var->xres = |
1170 | var->xres_virtual = 320; |
1171 | var->yres = |
1172 | var->yres_virtual = 240; |
1173 | var->width = |
1174 | var->height = -1; |
1175 | var->vmode = FB_VMODE_NONINTERLACED; |
1176 | var->bits_per_pixel = 16; |
1177 | var->red = fbb_red; |
1178 | var->green = fbb_green; |
1179 | var->blue = fbb_blue; |
1180 | return 0; |
1181 | } |
7fd42181 |
1182 | case FBIOPUT_VSCREENINFO: { |
1183 | struct fb_var_screeninfo *var = argp; |
1184 | dbg(" put vscreen: %dx%d@%d\n", var->xres, var->yres, var->bits_per_pixel); |
1185 | if (var->xres != 320 || var->yres != 240 || var->bits_per_pixel != 16) |
1186 | return -1; |
1187 | return 0; |
1188 | } |
dc90c02d |
1189 | } |
7fd42181 |
1190 | |
1191 | /* *********************** */ |
1192 | case FAKEDEV_TTY0: |
1193 | // fake tty0 to make GPH SDL happy |
1194 | if (request == 0x4b46) // KDGKBENT |
1195 | return -1; |
1196 | return 0; |
dc90c02d |
1197 | } |
1198 | |
1199 | fail: |
7fd42181 |
1200 | err("bad/ni ioctl(%d, %08x, %p)\n", fd, request, argp); |
dc90c02d |
1201 | errno = EINVAL; |
1202 | return -1; |
1203 | } |
1204 | |
7000b522 |
1205 | struct dev_fd_t emu_interesting_fds[] = { |
1206 | [IFD_SOUND] = { "/dev/dsp", -1, emu_sound_open }, |
1207 | { NULL, 0, NULL }, |
1208 | }; |
1209 | |
7fd42181 |
1210 | static const struct { |
1211 | const char *from; |
1212 | const char *to; |
1213 | } path_map[] = { |
158ddd8d |
1214 | { "/mnt/tmp", "./tmp" }, |
7fd42181 |
1215 | }; |
1216 | |
7fd42181 |
1217 | static const char *wrap_path(const char *path) |
1218 | { |
158ddd8d |
1219 | char *buff, *p; |
3d3fa63b |
1220 | size_t size; |
7fd42181 |
1221 | int i, len; |
1222 | |
1223 | // do only path mapping for now |
1224 | for (i = 0; i < ARRAY_SIZE(path_map); i++) { |
158ddd8d |
1225 | p = strstr(path, path_map[i].from); |
1226 | if (p != NULL) { |
3d3fa63b |
1227 | size = strlen(path) + strlen(path_map[i].to) + 1; |
1228 | buff = malloc(size); |
1229 | if (buff == NULL) |
1230 | break; |
158ddd8d |
1231 | len = p - path; |
1232 | strncpy(buff, path, len); |
1233 | snprintf(buff + len, size - len, "%s%s", path_map[i].to, |
1234 | path + len + strlen(path_map[i].from)); |
3d3fa63b |
1235 | dbg("mapped path \"%s\" -> \"%s\"\n", path, buff); |
1236 | return buff; |
7fd42181 |
1237 | } |
1238 | } |
1239 | |
1240 | return path; |
1241 | } |
1242 | |
7000b522 |
1243 | static void wrap_path_free(const char *w_path, const char *old_path) |
1244 | { |
1245 | if (w_path != old_path) |
1246 | free((void *)w_path); |
1247 | } |
1248 | |
7fd42181 |
1249 | void *emu_do_fopen(const char *path, const char *mode) |
1250 | { |
7000b522 |
1251 | const char *w_path; |
3d3fa63b |
1252 | FILE *ret; |
7000b522 |
1253 | |
1254 | w_path = wrap_path(path); |
3d3fa63b |
1255 | ret = fopen(w_path, mode); |
7000b522 |
1256 | wrap_path_free(w_path, path); |
1257 | |
3d3fa63b |
1258 | return ret; |
7fd42181 |
1259 | } |
1260 | |
3d3fa63b |
1261 | // FIXME: threads.. |
7fd42181 |
1262 | int emu_do_system(const char *command) |
1263 | { |
1264 | static char tmp_path[512]; |
158ddd8d |
1265 | int need_ginge = 0; |
3d3fa63b |
1266 | const char *p2; |
1267 | char *p; |
db9191ed |
1268 | int ret; |
7fd42181 |
1269 | |
1270 | if (command == NULL) |
1271 | return -1; |
1272 | |
158ddd8d |
1273 | for (p2 = command; *p2 && isspace(*p2); p2++) |
1274 | ; |
7fd42181 |
1275 | |
158ddd8d |
1276 | if (*p2 == '.') // relative path? |
1277 | need_ginge = 1; |
1278 | else if (*p2 == '/' && strncmp(p2, "/bin", 4) && strncmp(p2, "/lib", 4) |
1279 | && strncmp(p2, "/sbin", 4) && strncmp(p2, "/usr", 4)) |
1280 | // absolute path, but not a system command |
1281 | need_ginge = 1; |
7fd42181 |
1282 | |
3d3fa63b |
1283 | p2 = wrap_path(command); |
158ddd8d |
1284 | if (need_ginge) { |
1285 | make_local_path(tmp_path, sizeof(tmp_path), "ginge_prep"); |
1286 | p = tmp_path + strlen(tmp_path); |
1287 | |
1288 | snprintf(p, sizeof(tmp_path) - (p - tmp_path), " --nomenu %s", p2); |
1289 | } |
1290 | else |
1291 | snprintf(tmp_path, sizeof(tmp_path), "%s", p2); |
7000b522 |
1292 | wrap_path_free(p2, command); |
3d3fa63b |
1293 | |
7fd42181 |
1294 | dbg("system: \"%s\"\n", tmp_path); |
db9191ed |
1295 | |
1296 | // the app might want the screen too.. |
1297 | fb_thread_pause(); |
1298 | ret = system(tmp_path); |
1299 | fb_thread_resume(); |
1300 | return ret; |
7fd42181 |
1301 | } |
1302 | |
7000b522 |
1303 | int emu_do_execve(const char *filename, char *const argv[], char *const envp[]) |
1304 | { |
1305 | const char **new_argv; |
1306 | char *prep_path; |
1307 | int i, ret, argc; |
1308 | |
1309 | if (filename == NULL) |
1310 | return -1; |
1311 | |
1312 | if (strstr(filename, "/gp2xmenu") != NULL) |
1313 | exit(0); |
1314 | |
1315 | for (i = 0; argv[i] != NULL; i++) |
1316 | ; |
1317 | argc = i + 1; |
1318 | |
1319 | new_argv = calloc(argc + 2, sizeof(new_argv[0])); |
1320 | if (new_argv == NULL) |
1321 | return -1; |
1322 | |
1323 | prep_path = malloc(512); |
1324 | if (prep_path == NULL) |
1325 | return -1; |
1326 | |
1327 | make_local_path(prep_path, 512, "ginge_prep"); |
1328 | new_argv[0] = prep_path; |
1329 | new_argv[1] = "--nomenu"; |
1330 | new_argv[2] = wrap_path(filename); |
1331 | |
1332 | if (argv[0] != NULL) |
1333 | for (i = 1; argv[i] != NULL; i++) |
1334 | new_argv[i + 2] = argv[i]; |
1335 | |
1336 | dbg("execve \"%s\" %s \"%s\"\n", new_argv[0], new_argv[1], new_argv[2]); |
1337 | ret = execve(new_argv[0], (char **)new_argv, envp); |
1338 | perror("execve"); |
1339 | return ret; |
1340 | } |
1341 | |