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