1 // vim:shiftwidth=2:expandtab
4 #error loader is meant to be OABI!
11 #include <sys/types.h>
15 #include <sys/types.h>
18 #include <asm/ucontext.h>
22 #include <sys/resource.h>
23 #include <sys/ioctl.h>
24 #include <linux/soundcard.h>
28 #include "../common/host_fb.h"
29 #include "../common/cmn.h"
30 #include "sys_cacheflush.h"
31 #include "realfuncs.h"
33 #if (DBG & 2) && !(DBG & 4)
48 #define iolog_unh log_io
50 #define iolog_unh(...)
54 #define segvlog printf
59 #if defined(LOG_IO) || defined(LOG_IO_UNK)
60 #include "mmsp2-regs.h"
63 typedef unsigned long long u64;
64 typedef unsigned int u32;
65 typedef unsigned short u16;
66 typedef unsigned char u8;
68 static pthread_mutex_t fb_mutex = PTHREAD_MUTEX_INITIALIZER;
69 static pthread_cond_t fb_cond = PTHREAD_COND_INITIALIZER;
90 #define SRCCTRL_INVIDEO (1 << 8)
91 #define SRCCTRL_SRCENB (1 << 7)
92 #define CTRL_TRANSPARENCYENB (1 << 11)
98 u32 mlc_stl_adr; // mlcaddress for pollux
106 u16 mlc_stl_pallt_d[256*2];
107 u32 mlc_stl_pallt_d32[256];
117 u32 btn_state; // as seen through /dev/GPIO: 0PVdVu YXBA RLSeSt 0Ri0Dn 0Le0Up
125 #define pollux mmsp2 // so that code doesn't look that weird
128 DIRTY_PAL_POLLUX = 2,
132 #if defined(LOG_IO) || defined(LOG_IO_UNK)
133 static void log_io(const char *pfx, u32 a, u32 d, int size)
135 const char *fmt, *reg = "";
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;
142 if ((a & ~0xffff) == 0x7f000000)
143 reg = regnames[a & 0xffff];
145 printf(fmt, pfx, a, d, reg);
149 static void memset16(void *dst, u32 pattern, int count)
154 d = (u16 *)((long)dst & ~1);
160 pattern |= pattern << 16;
167 *(u16 *)dl = pattern;
170 static void blt_tr(void *dst, void *src, u32 trc, int w)
172 u16 *d = (u16 *)((long)dst & ~1);
173 u16 *s = (u16 *)((long)src & ~1);
176 for (; w > 0; d++, s++, w--)
181 #define dump_blitter() \
183 u32 *r = &blitter.dstctrl; \
185 for (i = 0; i < 4*4; i++, r++) { \
186 printf("%08x ", *r); \
192 static void *uppermem_lookup(u32 addr, u8 **mem_end)
194 // XXX: maybe support mirroring?
195 if ((addr & 0xfe000000) != 0x02000000)
198 *mem_end = (u8 *)mmsp2.umem + 0x02000000;
199 return (u8 *)mmsp2.umem - 0x02000000 + addr;
202 static void blitter_do(void)
204 u8 *dst, *dste, *src = NULL, *srce = NULL;
205 int w, h, sstrd, dstrd;
209 w = blitter.size & 0x7ff;
210 h = (blitter.size >> 16) & 0x7ff;
211 sstrd = blitter.srcstride;
212 dstrd = blitter.dststride;
214 // XXX: need to confirm this..
215 addr = (blitter.dstaddr & ~3) | ((blitter.dstctrl & 0x1f) >> 3);
217 // use dst bpp.. How does it do blits with different src bpp?
218 bpp = (blitter.dstctrl & 0x20) ? 16 : 8;
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)
226 dst = uppermem_lookup(addr, &dste);
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))
233 addr = (blitter.srcaddr & ~3) | ((blitter.srcctrl & 0x1f) >> 3);
234 src = uppermem_lookup(addr, &srce);
238 if (src + sstrd * h > srce) {
239 err("blit %08x->%08x %dx%d did not fit src\n",
240 blitter.srcaddr, blitter.dstaddr, w, h);
241 h = (srce - src) / sstrd;
248 if (dst + dstrd * h > dste) {
249 err("blit %08x->%08x %dx%d did not fit dst\n",
250 blitter.srcaddr, blitter.dstaddr, w, h);
251 h = (dste - dst) / dstrd;
256 if (bpp == 16 && (blitter.ctrl & CTRL_TRANSPARENCYENB)) {
257 u32 trc = blitter.ctrl >> 16;
258 for (; h > 0; h--, dst += dstrd, src += sstrd)
259 blt_tr(dst, src, trc, w);
262 for (; h > 0; h--, dst += dstrd, src += sstrd)
263 memcpy(dst, src, w * bpp / 8);
267 // fill. Assume the pattern is cleared and bg color is used
268 u32 bgc = blitter.patbackcolor & 0xffff;
270 for (; h > 0; h--, dst += dstrd)
271 memset16(dst, bgc, w);
274 for (; h > 0; h--, dst += dstrd)
275 memset(dst, bgc, w); // bgc?
280 pthread_cond_signal(&fb_cond);
284 err("blit %08x->%08x %dx%d translated to %p->%p\n",
285 blitter.srcaddr, blitter.dstaddr, w, h, src, dst);
289 // FIXME: pass real dimensions to blitters
290 static void mlc_flip(void *src, int bpp, int stride)
294 // only pass pal to host if it's dirty
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);
299 host_video_update_pal16(mmsp2.mlcpalette);
300 mmsp2.v.dirty_pal = 0;
303 if (bpp != old_bpp) {
304 host_video_change_bpp(bpp);
310 host_video_blit4(src, 320, 240, stride);
314 host_video_blit8(src, 320, 240, stride);
318 host_video_blit16(src, 320, 240, stride);
327 #define ts_add_nsec(ts, ns) { \
329 if (ts.tv_nsec >= 1000000000) { \
331 ts.tv_nsec -= 1000000000; \
335 static int fb_sync_thread_paused;
337 static void *fb_sync_thread(void *arg)
339 int invalid_fb_addr = 1;
340 int manual_refresh = 0;
341 int frame_counter = 0;
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);
350 ret = clock_gettime(CLOCK_REALTIME, &ts);
352 perror(PFX "clock_gettime");
357 u8 *gp2x_fb, *gp2x_fb_end;
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);
364 err("fb_thread: mutex error: %d\n", ret);
368 if (wait_ret != 0 && wait_ret != ETIMEDOUT) {
369 err("fb_thread: cond error: %d\n", wait_ret);
373 if (fb_sync_thread_paused) {
374 ts_add_nsec(ts, 100000000);
378 if (wait_ret != ETIMEDOUT) {
379 clock_gettime(CLOCK_REALTIME, &ts);
380 ts_add_nsec(ts, 50000000);
382 if (manual_refresh == 2)
383 dbg("fb_thread: switch to manual refresh\n");
385 ts_add_nsec(ts, 16666667);
386 if (manual_refresh > 1)
387 dbg("fb_thread: switch to auto refresh\n");
391 gp2x_fb = uppermem_lookup(mmsp2.mlc_stl_adr, &gp2x_fb_end);
392 if (gp2x_fb == NULL || gp2x_fb + 320*240 * mmsp2.v.bpp / 8 > gp2x_fb_end) {
393 if (!invalid_fb_addr) {
394 err("fb_thread: %08x is out of range\n", mmsp2.mlc_stl_adr);
401 mlc_flip(gp2x_fb, mmsp2.v.bpp, mmsp2.v.stride);
404 if (frame_counter & 0x0f)
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();
414 static void fb_thread_pause(void)
416 fb_sync_thread_paused = 1;
417 // wait until it finishes last refresh
418 // that it might be doing now
422 static void fb_thread_resume(void)
424 fb_sync_thread_paused = 0;
427 static u32 xread32_io_cmn(u32 a, u32 *handled)
434 case 0x402c: // MLCVSTRIDE0
435 case 0x4060: // MLCVSTRIDE1
438 case 0x4038: // MLCADDRESS0
439 case 0x406c: // MLCADDRESS1
440 d = pollux.mlc_stl_adr;
443 // ???? ???? YXBA DURiLe ???? VdVuMS LR?? ????
444 // | GPIOC[31:16] | GPIOB[31:16] |
445 case 0xa058: // GPIOBPAD
446 d = (pollux.btn_state >> 1) & 0x0100;
447 d |= (pollux.btn_state << 1) & 0x0200;
448 d |= (pollux.btn_state >> 3) & 0x0080;
449 d |= (pollux.btn_state >> 5) & 0x0040;
450 d |= (pollux.btn_state >> 6) & 0x0c00;
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;
472 static u32 xread8(u32 a)
474 iolog("r8 ", a, 0, 8);
475 iolog_unh("r8 ", a, 0, 8);
479 static u32 xread16(u32 a)
481 static u32 fudge, old_a;
484 if ((a & 0xffff0000) == 0x7f000000) {
492 // 0000 P000 VuVd00 0000 YXBA RLSeSt 0Ri0D 0Le0U
493 // | GPIOD |GPIOC[8:15]|GPIOM[0:7] |
496 case 0x1184: // GPIOC
497 d = ~mmsp2.btn_state & 0xff00;
500 case 0x1186: // GPIOD
501 t = ~mmsp2.btn_state;
502 d = (t >> 9) & 0x0080;
503 d |= (t >> 11) & 0x0040;
504 d |= (t >> 7) & 0x0800;
507 case 0x1198: // GPIOM
508 mmsp2.btn_state = host_read_btns();
509 d = ~mmsp2.btn_state & 0xff;
512 case 0x1836: // reserved
515 case 0x2816: // DPC_X_MAX
518 case 0x2818: // DPC_Y_MAX
522 d = mmsp2.mlc_stl_cntl;
526 d = mmsp2.mlc_stl_adrl;
530 d = mmsp2.mlc_stl_adrh;
533 d = mmsp2.mlc_stl_pallt_a;
537 d = xread32_io_cmn(a_, &t);
553 iolog_unh("r16", a, d & 0xffff, 16);
557 iolog("r16", a, d, 16);
561 static u32 xread32(u32 a)
564 if ((a & 0xfff00000) == 0x7f000000) {
571 case 0x0a00: // TCOUNT, 1/7372800s
572 clock_gettime(CLOCK_REALTIME, &ts);
573 t64 = (u64)ts.tv_sec * 1000000000 + ts.tv_nsec;
574 // t * 7372800.0 / 1000000000 * 0x100000000 ~= t * 31665935
580 d = xread32_io_cmn(a_, &t);
587 if ((a & 0xfff00000) == 0x7f100000) {
588 u32 *bl = &blitter.dstctrl;
599 iolog_unh("r32", a, d, 32);
602 iolog("r32", a, d, 32);
606 static void xwrite8(u32 a, u32 d)
608 iolog("w8 ", a, d, 8);
609 iolog_unh("w8 ", a, d, 8);
612 static void xwrite16(u32 a, u32 d)
614 iolog("w16", a, d, 16);
615 if ((a & 0xfff00000) == 0x7f000000) {
620 mmsp2.mlc_stl_cntl = d | 0xaa;
622 mmsp2.v.bpp = mode ? mode * 8 : 4;
630 // odd addresses don't affect LCD. What about TV?
633 mmsp2.mlc_stl_adrl = d;
636 mmsp2.mlc_stl_adrh = d;
637 if (mmsp2.mlc_stl_adr != mmsp2.old_mlc_stl_adr)
639 pthread_cond_signal(&fb_cond);
640 mmsp2.old_mlc_stl_adr = mmsp2.mlc_stl_adr;
643 mmsp2.mlc_stl_pallt_a = d & 0x1ff;
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;
652 iolog_unh("w16", a, d, 16);
655 static void xwrite32(u32 a, u32 d)
657 iolog("w32", a, d, 32);
659 if ((a & 0xfff00000) == 0x7f000000) {
663 case 0x4024: // MLCCONTROL0
664 case 0x4058: // MLCCONTROL1
665 pollux.mlccontrol = d;
667 return; // layer not enabled
668 if ((d >> 16) == 0x443A)
673 case 0x402c: // MLCVSTRIDE0
674 case 0x4060: // MLCVSTRIDE1
677 case 0x4038: // MLCADDRESS0
678 case 0x406c: // MLCADDRESS1
679 pollux.mlc_stl_adr = d;
680 if (d != mmsp2.old_mlc_stl_adr)
682 pthread_cond_signal(&fb_cond);
683 mmsp2.old_mlc_stl_adr = d;
685 case 0x403c: // MLCPALETTE0
686 case 0x4070: // MLCPALETTE1
687 pollux.mlcpalette[d >> 24] = d;
688 pollux.v.dirty_pal = DIRTY_PAL_POLLUX;
692 if ((a & 0xfff00000) == 0x7f100000) {
693 u32 *bl = &blitter.dstctrl;
697 if (a_ == 0x34 && (d & 1))
702 iolog_unh("w32", a, d, 32);
705 #define LINKPAGE_SIZE 0x1000
706 #define LINKPAGE_COUNT 4
707 #define LINKPAGE_ALLOC (LINKPAGE_SIZE * LINKPAGE_COUNT)
716 void (*handler)(struct op_context *op_ctx);
720 struct op_stackframe {
725 static struct op_linkpage *g_linkpage;
726 static u32 *g_code_ptr;
727 static int g_linkpage_count;
729 #define BIT_SET(v, b) (v & (1 << (b)))
731 void emu_handle_op(struct op_context *op_ctx, struct op_stackframe *sframe)
733 u32 *regs = sframe->saved_regs;
735 u32 t, shift, ret, addr;
738 rd = (op & 0x0000f000) >> 12;
739 rn = (op & 0x000f0000) >> 16;
741 if ((op & 0x0f200090) == 0x01000090) { // AM3: LDRH, STRH
742 if (!BIT_SET(op, 5)) // !H
744 if (BIT_SET(op, 6) && !BIT_SET(op, 20)) // S && !L
747 if (BIT_SET(op, 22)) // imm offset
748 t = ((op & 0xf00) >> 4) | (op & 0x0f);
750 t = regs[op & 0x000f];
752 if (!BIT_SET(op, 23))
756 if (BIT_SET(op, 20)) { // Load
758 if (BIT_SET(op, 6)) { // S
760 ret = (signed int)ret >> 16;
765 xwrite16(addr, regs[rd]);
767 else if ((op & 0x0d200000) == 0x05000000) { // AM2: LDR[B], STR[B]
768 if (BIT_SET(op, 25)) { // reg offs
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
784 if (!BIT_SET(op, 23))
788 if (BIT_SET(op, 20)) { // Load
789 if (BIT_SET(op, 22)) // Byte
796 if (BIT_SET(op, 22)) // Byte
797 xwrite8(addr, regs[rd]);
799 xwrite32(addr, regs[rd]);
806 if (addr != addr_check) {
807 fprintf(stderr, "bad calculated addr: %08x vs %08x\n", addr, addr_check);
814 err("unhandled IO op %08x @ %08x\n", op, op_ctx->pc);
817 static u32 make_offset12(u32 *pc, u32 *target)
821 lp_offs = (char *)target - (char *)pc - 2*4;
826 if (lp_offs >= LINKPAGE_SIZE) {
827 err("linkpage too far: %d\n", lp_offs);
831 return (u << 23) | lp_offs;
834 static u32 make_jmp(u32 *pc, u32 *target, int bl)
838 jmp_val = target - pc - 2;
839 if (jmp_val < (int)0xff000000 || jmp_val > 0x00ffffff) {
840 err("jump out of range (%p -> %p)\n", pc, target);
844 return 0xea000000 | (bl << 24) | (jmp_val & 0x00ffffff);
847 static void emit_op(u32 op)
852 static void emit_op_io(u32 op, u32 *target)
854 op |= make_offset12(g_code_ptr, target);
858 static void init_linkpage(void)
860 g_linkpage->handler = emu_call_handle_op;
861 g_code_ptr = g_linkpage->code;
864 static void segv_sigaction(int num, siginfo_t *info, void *ctx)
866 struct ucontext *context = ctx;
867 u32 *regs = (u32 *)&context->uc_mcontext.arm_r0;
868 u32 *pc = (u32 *)regs[15];
869 struct op_context *op_ctx;
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
876 // real crash - time to die
877 err("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]);
878 for (i = 0; i < 8; i++)
879 dbg(" r%d=%08x r%2d=%08x\n", i, regs[i], i+8, regs[i+8]);
880 signal(num, SIG_DFL);
884 segvlog("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]);
887 op_ctx = (void *)g_code_ptr;
888 op_ctx->pc = (u32)pc;
890 g_code_ptr = &op_ctx->code[0];
892 // emit jump to code ptr
893 *pc = make_jmp(pc, g_code_ptr, 0);
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)]
898 emit_op (0xe24f0000 + (g_code_ptr - (u32 *)op_ctx + 2) * 4); // sub r0, pc, #op_ctx
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)]
902 emit_op (make_jmp(g_code_ptr, pc + 1, 0)); // jmp <back>
905 sys_cacheflush(pc, pc + 1);
906 sys_cacheflush(g_linkpage, g_code_ptr);
908 lp_size = (char *)g_code_ptr - (char *)g_linkpage;
909 segvlog("code #%d %d/%d\n", g_linkpage_count, lp_size, LINKPAGE_SIZE);
911 if (lp_size + 13*4 > LINKPAGE_SIZE) {
913 if (g_linkpage_count >= LINKPAGE_COUNT) {
914 err("too many linkpages needed\n");
917 g_linkpage = (void *)((char *)g_linkpage + LINKPAGE_SIZE);
920 //handle_op(regs[15], op, regs, (u32)info->si_addr);
924 void emu_init(void *map_bottom)
926 sigaction_t segv_action = {
927 .sa_sigaction = segv_sigaction,
928 .sa_flags = SA_SIGINFO,
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");
942 g_linkpage = (void *)(((u32)map_bottom - LINKPAGE_ALLOC) & ~0xfff);
943 pret = mmap(g_linkpage, LINKPAGE_ALLOC, PROT_READ|PROT_WRITE,
944 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
945 if (pret != g_linkpage) {
946 perror(PFX "mmap linkpage");
949 log("linkpages @ %p\n", g_linkpage);
955 err("can't init host\n");
959 ret = host_video_init(NULL, 0);
961 err("can't init host video\n");
965 // TODO: check if this really fails on Wiz..
966 mmsp2.umem = mmap(NULL, 0x2000000, PROT_READ|PROT_WRITE|PROT_EXEC,
967 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
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();
975 if (mmsp2.umem == MAP_FAILED) {
976 perror(PFX "mmap upper mem");
980 ret = pthread_create(&tid, NULL, fb_sync_thread, NULL);
982 err("failed to create fb_sync_thread: %d\n", ret);
988 mmsp2.mlc_stl_adr = 0x03101000; // fb2 is at 0x03381000
989 mmsp2.mlc_stl_cntl = 0x4ab; // 16bpp, region 1 active
991 mmsp2.v.height = 240;
992 mmsp2.v.stride = 320*2;
994 mmsp2.v.dirty_pal = 1;
996 sigemptyset(&segv_action.sa_mask);
997 sigaction(SIGSEGV, &segv_action, NULL);
1000 int emu_read_gpiodev(void *buf, int count)
1003 err("gpiodev read %d?\n", count);
1009 mmsp2.btn_state = host_read_btns();
1010 memcpy(buf, &mmsp2.btn_state, count);
1014 static void *emu_mmap_dev(unsigned int length, int prot, int flags, unsigned int offset)
1016 u8 *umem, *umem_end;
1019 if ((offset & ~0x1ffff) == 0xc0000000) {
1020 return mmap((void *)0x7f000000, length, PROT_NONE,
1021 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0);
1024 if ((offset & ~0xffff) == 0xe0020000) {
1025 return mmap((void *)0x7f100000, length, PROT_NONE,
1026 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0);
1029 if ((offset & 0xfe000000) != 0x02000000) {
1030 err("unexpected devmem mmap @ %08x\n", offset);
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);
1040 dbg("upper mem @ %08x %d\n", offset, length);
1044 void *emu_do_mmap(unsigned int length, int prot, int flags, int fd, unsigned int offset)
1046 if (fd == FAKEDEV_MEM)
1047 return emu_mmap_dev(length, prot, flags, offset);
1049 if (fd == FAKEDEV_FB0)
1050 return emu_mmap_dev(length, prot, flags, offset + 0x03101000);
1052 if (fd == FAKEDEV_FB1)
1053 return emu_mmap_dev(length, prot, flags, offset + 0x03381000);
1055 err("bad/ni mmap(?, %d, %x, %x, %d, %08x)\n", length, prot, flags, fd, offset);
1060 static void emu_sound_open(int fd)
1065 // set default buffer size to 16 * 1K
1066 frag = (16<<16) | 10; // 16K
1067 ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
1069 err("snd ioctl SETFRAGMENT %08x: ", frag);
1075 static int emu_sound_ioctl(int fd, int request, void *argp)
1080 dbg("snd ioctl(%d, %08x, %p)", fd, request, argp);
1082 dbg_c(" [%d]", *arg);
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. */
1090 case SNDCTL_DSP_SETFRAGMENT: {
1091 int ret, bsize, frag, frag_cnt;
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) {
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.
1107 bsize = 44100 / 250 * 4;
1109 for (frag = 0; bsize; bsize >>= 1, frag++)
1115 frag |= frag_cnt << 16;
1116 ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
1118 err("snd ioctl SETFRAGMENT %08x: ", frag);
1121 // indicate success even if we fail (because of ALSA mostly),
1122 // things like MikMod will bail out otherwise.
1125 case SNDCTL_DSP_SYNC:
1126 // Franxis tends to use sync/write loops, bad idea under ALSA
1132 return ioctl(fd, request, argp);
1135 int emu_do_ioctl(int fd, int request, void *argp)
1137 if (fd == emu_interesting_fds[IFD_SOUND].fd)
1138 return emu_sound_ioctl(fd, request, argp);
1141 /* *********************** */
1148 case FBIOGET_FSCREENINFO: {
1149 struct fb_fix_screeninfo *fix = argp;
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;
1155 fix->visual = FB_VISUAL_TRUECOLOR;
1156 fix->line_length = 320*2;
1157 fix->smem_start = (fd == FAKEDEV_FB0) ? 0x03101000 : 0x03381000;
1158 fix->smem_len = 320*240*2;
1161 case FBIOGET_VSCREENINFO: {
1162 struct fb_var_screeninfo *var = argp;
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, };
1167 memset(var, 0, sizeof(*var));
1168 var->activate = FB_ACTIVATE_NOW;
1170 var->xres_virtual = 320;
1172 var->yres_virtual = 240;
1175 var->vmode = FB_VMODE_NONINTERLACED;
1176 var->bits_per_pixel = 16;
1178 var->green = fbb_green;
1179 var->blue = fbb_blue;
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)
1191 /* *********************** */
1193 // fake tty0 to make GPH SDL happy
1194 if (request == 0x4b46) // KDGKBENT
1200 err("bad/ni ioctl(%d, %08x, %p)\n", fd, request, argp);
1205 struct dev_fd_t emu_interesting_fds[] = {
1206 [IFD_SOUND] = { "/dev/dsp", -1, emu_sound_open },
1210 static const struct {
1214 { "/mnt/tmp", "./tmp" },
1217 static const char *wrap_path(const char *path)
1223 // do only path mapping for now
1224 for (i = 0; i < ARRAY_SIZE(path_map); i++) {
1225 p = strstr(path, path_map[i].from);
1227 size = strlen(path) + strlen(path_map[i].to) + 1;
1228 buff = malloc(size);
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));
1235 dbg("mapped path \"%s\" -> \"%s\"\n", path, buff);
1243 static void wrap_path_free(const char *w_path, const char *old_path)
1245 if (w_path != old_path)
1246 free((void *)w_path);
1249 void *emu_do_fopen(const char *path, const char *mode)
1254 w_path = wrap_path(path);
1255 ret = fopen(w_path, mode);
1256 wrap_path_free(w_path, path);
1262 int emu_do_system(const char *command)
1264 static char tmp_path[512];
1270 if (command == NULL)
1273 for (p2 = command; *p2 && isspace(*p2); p2++)
1276 if (*p2 == '.') // relative path?
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
1283 p2 = wrap_path(command);
1285 make_local_path(tmp_path, sizeof(tmp_path), "ginge_prep");
1286 p = tmp_path + strlen(tmp_path);
1288 snprintf(p, sizeof(tmp_path) - (p - tmp_path), " --nomenu %s", p2);
1291 snprintf(tmp_path, sizeof(tmp_path), "%s", p2);
1292 wrap_path_free(p2, command);
1294 dbg("system: \"%s\"\n", tmp_path);
1296 // the app might want the screen too..
1298 ret = system(tmp_path);
1303 int emu_do_execve(const char *filename, char *const argv[], char *const envp[])
1305 const char **new_argv;
1309 if (filename == NULL)
1312 if (strstr(filename, "/gp2xmenu") != NULL)
1315 for (i = 0; argv[i] != NULL; i++)
1319 new_argv = calloc(argc + 2, sizeof(new_argv[0]));
1320 if (new_argv == NULL)
1323 prep_path = malloc(512);
1324 if (prep_path == NULL)
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);
1332 if (argv[0] != NULL)
1333 for (i = 1; argv[i] != NULL; i++)
1334 new_argv[i + 2] = argv[i];
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);