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