1 // vim:shiftwidth=2:expandtab
4 #error loader is meant to be OABI!
10 #include <sys/types.h>
14 #include <sys/types.h>
17 #include <asm/ucontext.h>
21 #include <sys/resource.h>
22 #include <sys/ioctl.h>
23 #include <linux/soundcard.h>
27 #include "../common/host_fb.h"
28 #include "../common/cmn.h"
29 #include "sys_cacheflush.h"
30 #include "realfuncs.h"
32 #if (DBG & 2) && !(DBG & 4)
47 #define iolog_unh log_io
49 #define iolog_unh(...)
53 #define segvlog printf
58 #if defined(LOG_IO) || defined(LOG_IO_UNK)
59 #include "mmsp2-regs.h"
62 typedef unsigned long long u64;
63 typedef unsigned int u32;
64 typedef unsigned short u16;
65 typedef unsigned char u8;
67 static pthread_mutex_t fb_mutex = PTHREAD_MUTEX_INITIALIZER;
68 static pthread_cond_t fb_cond = PTHREAD_COND_INITIALIZER;
89 #define SRCCTRL_INVIDEO (1 << 8)
90 #define SRCCTRL_SRCENB (1 << 7)
91 #define CTRL_TRANSPARENCYENB (1 << 11)
97 u32 mlc_stl_adr; // mlcaddress for pollux
105 u16 mlc_stl_pallt_d[256*2];
106 u32 mlc_stl_pallt_d32[256];
116 u32 btn_state; // as seen through /dev/GPIO: 0PVdVu YXBA RLSeSt 0Ri0Dn 0Le0Up
124 #define pollux mmsp2 // so that code doesn't look that weird
127 DIRTY_PAL_POLLUX = 2,
131 #if defined(LOG_IO) || defined(LOG_IO_UNK)
132 static void log_io(const char *pfx, u32 a, u32 d, int size)
134 const char *fmt, *reg = "";
136 case 8: fmt = "%s %08x %02x %s\n"; d &= 0xff; break;
137 case 32: fmt = "%s %08x %08x %s\n"; break;
138 default: fmt = "%s %08x %04x %s\n"; d &= 0xffff; break;
141 if ((a & ~0xffff) == 0x7f000000)
142 reg = regnames[a & 0xffff];
144 printf(fmt, pfx, a, d, reg);
148 static void memset16(void *dst, u32 pattern, int count)
153 d = (u16 *)((long)dst & ~1);
159 pattern |= pattern << 16;
166 *(u16 *)dl = pattern;
169 static void blt_tr(void *dst, void *src, u32 trc, int w)
171 u16 *d = (u16 *)((long)dst & ~1);
172 u16 *s = (u16 *)((long)src & ~1);
175 for (; w > 0; d++, s++, w--)
180 #define dump_blitter() \
182 u32 *r = &blitter.dstctrl; \
184 for (i = 0; i < 4*4; i++, r++) { \
185 printf("%08x ", *r); \
191 static void *uppermem_lookup(u32 addr, u8 **mem_end)
193 // XXX: maybe support mirroring?
194 if ((addr & 0xfe000000) != 0x02000000)
197 *mem_end = (u8 *)mmsp2.umem + 0x02000000;
198 return (u8 *)mmsp2.umem - 0x02000000 + addr;
201 static void blitter_do(void)
203 u8 *dst, *dste, *src = NULL, *srce = NULL;
204 int w, h, sstrd, dstrd;
208 w = blitter.size & 0x7ff;
209 h = (blitter.size >> 16) & 0x7ff;
210 sstrd = blitter.srcstride;
211 dstrd = blitter.dststride;
213 // XXX: need to confirm this..
214 addr = (blitter.dstaddr & ~3) | ((blitter.dstctrl & 0x1f) >> 3);
216 // use dst bpp.. How does it do blits with different src bpp?
217 bpp = (blitter.dstctrl & 0x20) ? 16 : 8;
220 if (((w == 320 && h == 240) || // blit whole screen
221 (w * h >= 320*240/2)) && // ..or at least half of the area
222 mmsp2.mlc_stl_adr <= addr && addr < mmsp2.mlc_stl_adr + 320*240*2)
225 dst = uppermem_lookup(addr, &dste);
227 // XXX: assume fill if no SRCENB, but it could be pattern blit..
228 if (blitter.srcctrl & SRCCTRL_SRCENB) {
229 if (!(blitter.srcctrl & SRCCTRL_INVIDEO))
232 addr = (blitter.srcaddr & ~3) | ((blitter.srcctrl & 0x1f) >> 3);
233 src = uppermem_lookup(addr, &srce);
237 if (src + sstrd * h > srce) {
238 err("blit %08x->%08x %dx%d did not fit src\n",
239 blitter.srcaddr, blitter.dstaddr, w, h);
240 h = (srce - src) / sstrd;
247 if (dst + dstrd * h > dste) {
248 err("blit %08x->%08x %dx%d did not fit dst\n",
249 blitter.srcaddr, blitter.dstaddr, w, h);
250 h = (dste - dst) / dstrd;
255 if (bpp == 16 && (blitter.ctrl & CTRL_TRANSPARENCYENB)) {
256 u32 trc = blitter.ctrl >> 16;
257 for (; h > 0; h--, dst += dstrd, src += sstrd)
258 blt_tr(dst, src, trc, w);
261 for (; h > 0; h--, dst += dstrd, src += sstrd)
262 memcpy(dst, src, w * bpp / 8);
266 // fill. Assume the pattern is cleared and bg color is used
267 u32 bgc = blitter.patbackcolor & 0xffff;
269 for (; h > 0; h--, dst += dstrd)
270 memset16(dst, bgc, w);
273 for (; h > 0; h--, dst += dstrd)
274 memset(dst, bgc, w); // bgc?
279 pthread_cond_signal(&fb_cond);
283 err("blit %08x->%08x %dx%d translated to %p->%p\n",
284 blitter.srcaddr, blitter.dstaddr, w, h, src, dst);
288 // FIXME: pass real dimensions to blitters
289 static void mlc_flip(void *src, int bpp, int stride)
293 // only pass pal to host if it's dirty
294 if (bpp <= 8 && mmsp2.v.dirty_pal) {
295 if (mmsp2.v.dirty_pal == DIRTY_PAL_MMSP2)
296 host_video_update_pal32(mmsp2.mlc_stl_pallt_d32);
298 host_video_update_pal16(mmsp2.mlcpalette);
299 mmsp2.v.dirty_pal = 0;
302 if (bpp != old_bpp) {
303 host_video_change_bpp(bpp);
309 host_video_blit4(src, 320, 240, stride);
313 host_video_blit8(src, 320, 240, stride);
317 host_video_blit16(src, 320, 240, stride);
326 #define ts_add_nsec(ts, ns) { \
328 if (ts.tv_nsec >= 1000000000) { \
330 ts.tv_nsec -= 1000000000; \
334 static int fb_sync_thread_paused;
336 static void *fb_sync_thread(void *arg)
338 int invalid_fb_addr = 1;
339 int manual_refresh = 0;
340 int frame_counter = 0;
344 //ret = pthread_setschedprio(pthread_self(), -1);
345 //log("pthread_setschedprio %d\n", ret);
346 //ret = setpriority(PRIO_PROCESS, 0, -1);
347 //log("setpriority %d\n", ret);
349 ret = clock_gettime(CLOCK_REALTIME, &ts);
351 perror(PFX "clock_gettime");
356 u8 *gp2x_fb, *gp2x_fb_end;
358 ret = pthread_mutex_lock(&fb_mutex);
359 wait_ret = pthread_cond_timedwait(&fb_cond, &fb_mutex, &ts);
360 ret |= pthread_mutex_unlock(&fb_mutex);
363 err("fb_thread: mutex error: %d\n", ret);
367 if (wait_ret != 0 && wait_ret != ETIMEDOUT) {
368 err("fb_thread: cond error: %d\n", wait_ret);
372 if (fb_sync_thread_paused) {
373 ts_add_nsec(ts, 100000000);
377 if (wait_ret != ETIMEDOUT) {
378 clock_gettime(CLOCK_REALTIME, &ts);
379 ts_add_nsec(ts, 50000000);
381 if (manual_refresh == 2)
382 dbg("fb_thread: switch to manual refresh\n");
384 ts_add_nsec(ts, 16666667);
385 if (manual_refresh > 1)
386 dbg("fb_thread: switch to auto refresh\n");
390 gp2x_fb = uppermem_lookup(mmsp2.mlc_stl_adr, &gp2x_fb_end);
391 if (gp2x_fb == NULL || gp2x_fb + 320*240 * mmsp2.v.bpp / 8 > gp2x_fb_end) {
392 if (!invalid_fb_addr) {
393 err("fb_thread: %08x is out of range\n", mmsp2.mlc_stl_adr);
400 mlc_flip(gp2x_fb, mmsp2.v.bpp, mmsp2.v.stride);
403 if (frame_counter & 0x0f)
407 // this is to check for kill key, in case main thread hung
408 // or something else went wrong.
409 pollux.btn_state = host_read_btns();
413 static void fb_thread_pause(void)
415 fb_sync_thread_paused = 1;
416 // wait until it finishes last refresh
417 // that it might be doing now
421 static void fb_thread_resume(void)
423 fb_sync_thread_paused = 0;
426 static u32 xread32_io_cmn(u32 a, u32 *handled)
433 case 0x402c: // MLCVSTRIDE0
434 case 0x4060: // MLCVSTRIDE1
437 case 0x4038: // MLCADDRESS0
438 case 0x406c: // MLCADDRESS1
439 d = pollux.mlc_stl_adr;
442 // ???? ???? YXBA DURiLe ???? VdVuMS LR?? ????
443 // | GPIOC[31:16] | GPIOB[31:16] |
444 case 0xa058: // GPIOBPAD
445 d = (pollux.btn_state >> 1) & 0x0100;
446 d |= (pollux.btn_state << 1) & 0x0200;
447 d |= (pollux.btn_state >> 3) & 0x0080;
448 d |= (pollux.btn_state >> 5) & 0x0040;
449 d |= (pollux.btn_state >> 6) & 0x0c00;
453 case 0xa098: // GPIOCPAD
454 pollux.btn_state = host_read_btns();
455 d = (pollux.btn_state >> 8) & 0x00f0;
456 d |= (pollux.btn_state >> 1) & 0x0008;
457 d |= (pollux.btn_state << 2) & 0x0004;
458 d |= (pollux.btn_state >> 5) & 0x0002;
459 d |= (pollux.btn_state >> 2) & 0x0001;
471 static u32 xread8(u32 a)
473 iolog("r8 ", a, 0, 8);
474 iolog_unh("r8 ", a, 0, 8);
478 static u32 xread16(u32 a)
480 static u32 fudge, old_a;
483 if ((a & 0xffff0000) == 0x7f000000) {
491 // 0000 P000 VuVd00 0000 YXBA RLSeSt 0Ri0D 0Le0U
492 // | GPIOD |GPIOC[8:15]|GPIOM[0:7] |
495 case 0x1184: // GPIOC
496 d = ~mmsp2.btn_state & 0xff00;
499 case 0x1186: // GPIOD
500 t = ~mmsp2.btn_state;
501 d = (t >> 9) & 0x0080;
502 d |= (t >> 11) & 0x0040;
503 d |= (t >> 7) & 0x0800;
506 case 0x1198: // GPIOM
507 mmsp2.btn_state = host_read_btns();
508 d = ~mmsp2.btn_state & 0xff;
511 case 0x1836: // reserved
514 case 0x2816: // DPC_X_MAX
517 case 0x2818: // DPC_Y_MAX
521 d = mmsp2.mlc_stl_cntl;
525 d = mmsp2.mlc_stl_adrl;
529 d = mmsp2.mlc_stl_adrh;
532 d = mmsp2.mlc_stl_pallt_a;
536 d = xread32_io_cmn(a_, &t);
552 iolog_unh("r16", a, d & 0xffff, 16);
556 iolog("r16", a, d, 16);
560 static u32 xread32(u32 a)
563 if ((a & 0xfff00000) == 0x7f000000) {
570 case 0x0a00: // TCOUNT, 1/7372800s
571 clock_gettime(CLOCK_REALTIME, &ts);
572 t64 = (u64)ts.tv_sec * 1000000000 + ts.tv_nsec;
573 // t * 7372800.0 / 1000000000 * 0x100000000 ~= t * 31665935
579 d = xread32_io_cmn(a_, &t);
586 if ((a & 0xfff00000) == 0x7f100000) {
587 u32 *bl = &blitter.dstctrl;
598 iolog_unh("r32", a, d, 32);
601 iolog("r32", a, d, 32);
605 static void xwrite8(u32 a, u32 d)
607 iolog("w8 ", a, d, 8);
608 iolog_unh("w8 ", a, d, 8);
611 static void xwrite16(u32 a, u32 d)
613 iolog("w16", a, d, 16);
614 if ((a & 0xfff00000) == 0x7f000000) {
619 mmsp2.mlc_stl_cntl = d | 0xaa;
621 mmsp2.v.bpp = mode ? mode * 8 : 4;
629 // odd addresses don't affect LCD. What about TV?
632 mmsp2.mlc_stl_adrl = d;
635 mmsp2.mlc_stl_adrh = d;
636 if (mmsp2.mlc_stl_adr != mmsp2.old_mlc_stl_adr)
638 pthread_cond_signal(&fb_cond);
639 mmsp2.old_mlc_stl_adr = mmsp2.mlc_stl_adr;
642 mmsp2.mlc_stl_pallt_a = d & 0x1ff;
645 mmsp2.mlc_stl_pallt_d[mmsp2.mlc_stl_pallt_a++] = d;
646 mmsp2.mlc_stl_pallt_a &= 0x1ff;
647 mmsp2.v.dirty_pal = DIRTY_PAL_MMSP2;
651 iolog_unh("w16", a, d, 16);
654 static void xwrite32(u32 a, u32 d)
656 iolog("w32", a, d, 32);
658 if ((a & 0xfff00000) == 0x7f000000) {
662 case 0x4024: // MLCCONTROL0
663 case 0x4058: // MLCCONTROL1
664 pollux.mlccontrol = d;
666 return; // layer not enabled
667 if ((d >> 16) == 0x443A)
672 case 0x402c: // MLCVSTRIDE0
673 case 0x4060: // MLCVSTRIDE1
676 case 0x4038: // MLCADDRESS0
677 case 0x406c: // MLCADDRESS1
678 pollux.mlc_stl_adr = d;
679 if (d != mmsp2.old_mlc_stl_adr)
681 pthread_cond_signal(&fb_cond);
682 mmsp2.old_mlc_stl_adr = d;
684 case 0x403c: // MLCPALETTE0
685 case 0x4070: // MLCPALETTE1
686 pollux.mlcpalette[d >> 24] = d;
687 pollux.v.dirty_pal = DIRTY_PAL_POLLUX;
691 if ((a & 0xfff00000) == 0x7f100000) {
692 u32 *bl = &blitter.dstctrl;
696 if (a_ == 0x34 && (d & 1))
701 iolog_unh("w32", a, d, 32);
704 #define LINKPAGE_SIZE 0x1000
705 #define LINKPAGE_COUNT 4
706 #define LINKPAGE_ALLOC (LINKPAGE_SIZE * LINKPAGE_COUNT)
715 void (*handler)(struct op_context *op_ctx);
719 struct op_stackframe {
724 static struct op_linkpage *g_linkpage;
725 static u32 *g_code_ptr;
726 static int g_linkpage_count;
728 #define BIT_SET(v, b) (v & (1 << (b)))
730 void emu_handle_op(struct op_context *op_ctx, struct op_stackframe *sframe)
732 u32 *regs = sframe->saved_regs;
734 u32 t, shift, ret, addr;
737 rd = (op & 0x0000f000) >> 12;
738 rn = (op & 0x000f0000) >> 16;
740 if ((op & 0x0f200090) == 0x01000090) { // AM3: LDRH, STRH
741 if (!BIT_SET(op, 5)) // !H
743 if (BIT_SET(op, 6) && !BIT_SET(op, 20)) // S && !L
746 if (BIT_SET(op, 22)) // imm offset
747 t = ((op & 0xf00) >> 4) | (op & 0x0f);
749 t = regs[op & 0x000f];
751 if (!BIT_SET(op, 23))
755 if (BIT_SET(op, 20)) { // Load
757 if (BIT_SET(op, 6)) { // S
759 ret = (signed int)ret >> 16;
764 xwrite16(addr, regs[rd]);
766 else if ((op & 0x0d200000) == 0x05000000) { // AM2: LDR[B], STR[B]
767 if (BIT_SET(op, 25)) { // reg offs
771 t = regs[op & 0x000f];
772 shift = (op & 0x0f80) >> 7;
773 switch ((op & 0x0060) >> 5) {
774 case 0: t = t << shift; break;
775 case 1: t = t >> (shift + 1); break;
776 case 2: t = (signed int)t >> (shift + 1); break;
777 case 3: goto unhandled; // I'm just lazy
783 if (!BIT_SET(op, 23))
787 if (BIT_SET(op, 20)) { // Load
788 if (BIT_SET(op, 22)) // Byte
795 if (BIT_SET(op, 22)) // Byte
796 xwrite8(addr, regs[rd]);
798 xwrite32(addr, regs[rd]);
805 if (addr != addr_check) {
806 fprintf(stderr, "bad calculated addr: %08x vs %08x\n", addr, addr_check);
813 err("unhandled IO op %08x @ %08x\n", op, op_ctx->pc);
816 static u32 make_offset12(u32 *pc, u32 *target)
820 lp_offs = (char *)target - (char *)pc - 2*4;
825 if (lp_offs >= LINKPAGE_SIZE) {
826 err("linkpage too far: %d\n", lp_offs);
830 return (u << 23) | lp_offs;
833 static u32 make_jmp(u32 *pc, u32 *target, int bl)
837 jmp_val = target - pc - 2;
838 if (jmp_val < (int)0xff000000 || jmp_val > 0x00ffffff) {
839 err("jump out of range (%p -> %p)\n", pc, target);
843 return 0xea000000 | (bl << 24) | (jmp_val & 0x00ffffff);
846 static void emit_op(u32 op)
851 static void emit_op_io(u32 op, u32 *target)
853 op |= make_offset12(g_code_ptr, target);
857 static void init_linkpage(void)
859 g_linkpage->handler = emu_call_handle_op;
860 g_code_ptr = g_linkpage->code;
863 static void segv_sigaction(int num, siginfo_t *info, void *ctx)
865 struct ucontext *context = ctx;
866 u32 *regs = (u32 *)&context->uc_mcontext.arm_r0;
867 u32 *pc = (u32 *)regs[15];
868 struct op_context *op_ctx;
871 if (((regs[15] ^ (u32)&segv_sigaction) & 0xff000000) == 0 || // PC is in our segment or
872 (((regs[15] ^ (u32)g_linkpage) & ~(LINKPAGE_ALLOC - 1)) == 0) || // .. in linkpage
873 ((long)info->si_addr & 0xffe00000) != 0x7f000000) // faulting not where expected
875 // real crash - time to die
876 err("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]);
877 for (i = 0; i < 8; i++)
878 dbg(" r%d=%08x r%2d=%08x\n", i, regs[i], i+8, regs[i+8]);
879 signal(num, SIG_DFL);
883 segvlog("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]);
886 op_ctx = (void *)g_code_ptr;
887 op_ctx->pc = (u32)pc;
889 g_code_ptr = &op_ctx->code[0];
891 // emit jump to code ptr
892 *pc = make_jmp(pc, g_code_ptr, 0);
895 emit_op (0xe50d0000 + 0xf00 - 4 * 0); // str r0, [sp, #(-0xf00 + r0_offs)]
896 emit_op (0xe50de000 + 0xf00 - 4 * 14); // str lr, [sp, #(-0xf00 + lr_offs)]
897 emit_op (0xe24f0000 + (g_code_ptr - (u32 *)op_ctx + 2) * 4); // sub r0, pc, #op_ctx
898 emit_op (0xe1a0e00f); // mov lr, pc
899 emit_op_io(0xe51ff000, (u32 *)&g_linkpage->handler); // ldr pc, =handle_op
900 emit_op (0xe51de000 + 0xf00 - 4 * 14); // ldr lr, [sp, #(-0xf00 + lr_offs)]
901 emit_op (make_jmp(g_code_ptr, pc + 1, 0)); // jmp <back>
904 sys_cacheflush(pc, pc + 1);
905 sys_cacheflush(g_linkpage, g_code_ptr);
907 lp_size = (char *)g_code_ptr - (char *)g_linkpage;
908 segvlog("code #%d %d/%d\n", g_linkpage_count, lp_size, LINKPAGE_SIZE);
910 if (lp_size + 13*4 > LINKPAGE_SIZE) {
912 if (g_linkpage_count >= LINKPAGE_COUNT) {
913 err("too many linkpages needed\n");
916 g_linkpage = (void *)((char *)g_linkpage + LINKPAGE_SIZE);
919 //handle_op(regs[15], op, regs, (u32)info->si_addr);
923 void emu_init(void *map_bottom)
925 sigaction_t segv_action = {
926 .sa_sigaction = segv_sigaction,
927 .sa_flags = SA_SIGINFO,
934 if (geteuid() == 0) {
935 fprintf(stderr, "don't try to run as root, device registers or memory "
936 "might get trashed crashing the OS or even damaging the device.\n");
941 g_linkpage = (void *)(((u32)map_bottom - LINKPAGE_ALLOC) & ~0xfff);
942 pret = mmap(g_linkpage, LINKPAGE_ALLOC, PROT_READ|PROT_WRITE,
943 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
944 if (pret != g_linkpage) {
945 perror(PFX "mmap linkpage");
948 log("linkpages @ %p\n", g_linkpage);
954 err("can't init host\n");
958 ret = host_video_init(NULL, 0);
960 err("can't init host video\n");
964 // TODO: check if this really fails on Wiz..
965 mmsp2.umem = mmap(NULL, 0x2000000, PROT_READ|PROT_WRITE|PROT_EXEC,
966 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
968 if (mmsp2.umem == MAP_FAILED) {
969 // we are short on memmory on Wiz, need special handling
970 extern void *host_mmap_upper(void);
971 mmsp2.umem = host_mmap_upper();
974 if (mmsp2.umem == MAP_FAILED) {
975 perror(PFX "mmap upper mem");
979 ret = pthread_create(&tid, NULL, fb_sync_thread, NULL);
981 err("failed to create fb_sync_thread: %d\n", ret);
987 mmsp2.mlc_stl_adr = 0x03101000; // fb2 is at 0x03381000
988 mmsp2.mlc_stl_cntl = 0x4ab; // 16bpp, region 1 active
990 mmsp2.v.height = 240;
991 mmsp2.v.stride = 320*2;
993 mmsp2.v.dirty_pal = 1;
995 sigemptyset(&segv_action.sa_mask);
996 sigaction(SIGSEGV, &segv_action, NULL);
999 int emu_read_gpiodev(void *buf, int count)
1002 err("gpiodev read %d?\n", count);
1008 mmsp2.btn_state = host_read_btns();
1009 memcpy(buf, &mmsp2.btn_state, count);
1013 static void *emu_mmap_dev(unsigned int length, int prot, int flags, unsigned int offset)
1015 u8 *umem, *umem_end;
1018 if ((offset & ~0x1ffff) == 0xc0000000) {
1019 return mmap((void *)0x7f000000, length, PROT_NONE,
1020 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0);
1023 if ((offset & ~0xffff) == 0xe0020000) {
1024 return mmap((void *)0x7f100000, length, PROT_NONE,
1025 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0);
1028 if ((offset & 0xfe000000) != 0x02000000) {
1029 err("unexpected devmem mmap @ %08x\n", offset);
1034 umem = uppermem_lookup(offset, &umem_end);
1035 if (umem + length > umem_end)
1036 err("warning: uppermem @ %08x overflows by %d bytes\n",
1037 offset, umem + length - umem_end);
1039 dbg("upper mem @ %08x %d\n", offset, length);
1043 void *emu_do_mmap(unsigned int length, int prot, int flags, int fd, unsigned int offset)
1045 if (fd == FAKEDEV_MEM)
1046 return emu_mmap_dev(length, prot, flags, offset);
1048 if (fd == FAKEDEV_FB0)
1049 return emu_mmap_dev(length, prot, flags, offset + 0x03101000);
1051 if (fd == FAKEDEV_FB1)
1052 return emu_mmap_dev(length, prot, flags, offset + 0x03381000);
1054 err("bad/ni mmap(?, %d, %x, %x, %d, %08x)\n", length, prot, flags, fd, offset);
1059 static void emu_sound_open(int fd)
1064 // set default buffer size to 16 * 1K
1065 frag = (16<<16) | 10; // 16K
1066 ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
1068 err("snd ioctl SETFRAGMENT %08x: ", frag);
1074 static int emu_sound_ioctl(int fd, int request, void *argp)
1079 dbg("snd ioctl(%d, %08x, %p)", fd, request, argp);
1081 dbg_c(" [%d]", *arg);
1085 /* People set strange frag settings on GP2X, which even manage
1086 * to break audio on pandora (causes writes to fail).
1087 * Catch this and set to something that works. */
1089 case SNDCTL_DSP_SETFRAGMENT: {
1090 int ret, bsize, frag, frag_cnt;
1094 frag = *arg & 0xffff;
1095 frag_cnt = *arg >> 16;
1096 bsize = frag_cnt << frag;
1097 if (frag < 10 || bsize < 4096*4 || bsize > 4096*4*2) {
1099 * ~4ms. gpSP wants small buffers or else it stutters
1100 * because of it's audio thread sync stuff
1101 * XXX: hardcoding, as low samplerates will result in small fragment size,
1102 * which itself causes ALSA stall and hangs the program.
1103 * Also some apps change samplerate without reopening /dev/dsp,
1104 * which causes ALSA to reject SNDCTL_DSP_SETFRAGMENT.
1106 bsize = 44100 / 250 * 4;
1108 for (frag = 0; bsize; bsize >>= 1, frag++)
1114 frag |= frag_cnt << 16;
1115 ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
1117 err("snd ioctl SETFRAGMENT %08x: ", frag);
1120 // indicate success even if we fail (because of ALSA mostly),
1121 // things like MikMod will bail out otherwise.
1124 case SNDCTL_DSP_SYNC:
1125 // Franxis tends to use sync/write loops, bad idea under ALSA
1131 return ioctl(fd, request, argp);
1134 int emu_do_ioctl(int fd, int request, void *argp)
1136 if (fd == emu_interesting_fds[IFD_SOUND].fd)
1137 return emu_sound_ioctl(fd, request, argp);
1140 /* *********************** */
1147 case FBIOGET_FSCREENINFO: {
1148 struct fb_fix_screeninfo *fix = argp;
1150 memset(fix, 0, sizeof(*fix));
1151 strcpy(fix->id, "mmsp2_RGB0");
1152 fix->type = FB_TYPE_PACKED_PIXELS;
1153 fix->accel = FB_ACCEL_NONE;
1154 fix->visual = FB_VISUAL_TRUECOLOR;
1155 fix->line_length = 320*2;
1156 fix->smem_start = (fd == FAKEDEV_FB0) ? 0x03101000 : 0x03381000;
1157 fix->smem_len = 320*240*2;
1160 case FBIOGET_VSCREENINFO: {
1161 struct fb_var_screeninfo *var = argp;
1162 static const struct fb_bitfield fbb_red = { offset: 11, length: 5, };
1163 static const struct fb_bitfield fbb_green = { offset: 5, length: 6, };
1164 static const struct fb_bitfield fbb_blue = { offset: 0, length: 5, };
1166 memset(var, 0, sizeof(*var));
1167 var->activate = FB_ACTIVATE_NOW;
1169 var->xres_virtual = 320;
1171 var->yres_virtual = 240;
1174 var->vmode = FB_VMODE_NONINTERLACED;
1175 var->bits_per_pixel = 16;
1177 var->green = fbb_green;
1178 var->blue = fbb_blue;
1181 case FBIOPUT_VSCREENINFO: {
1182 struct fb_var_screeninfo *var = argp;
1183 dbg(" put vscreen: %dx%d@%d\n", var->xres, var->yres, var->bits_per_pixel);
1184 if (var->xres != 320 || var->yres != 240 || var->bits_per_pixel != 16)
1190 /* *********************** */
1192 // fake tty0 to make GPH SDL happy
1193 if (request == 0x4b46) // KDGKBENT
1199 err("bad/ni ioctl(%d, %08x, %p)\n", fd, request, argp);
1204 struct dev_fd_t emu_interesting_fds[] = {
1205 [IFD_SOUND] = { "/dev/dsp", -1, emu_sound_open },
1209 static const struct {
1213 { "/mnt/tmp/", "/tmp/" },
1216 static const char *wrap_path(const char *path)
1222 // do only path mapping for now
1223 for (i = 0; i < ARRAY_SIZE(path_map); i++) {
1224 len = strlen(path_map[i].from);
1225 if (strncmp(path, path_map[i].from, len) == 0) {
1226 size = strlen(path) + strlen(path_map[i].to) + 1;
1227 buff = malloc(size);
1230 snprintf(buff, size, "%s%s", path_map[i].to, path + len);
1231 dbg("mapped path \"%s\" -> \"%s\"\n", path, buff);
1239 static void wrap_path_free(const char *w_path, const char *old_path)
1241 if (w_path != old_path)
1242 free((void *)w_path);
1245 void *emu_do_fopen(const char *path, const char *mode)
1250 w_path = wrap_path(path);
1251 ret = fopen(w_path, mode);
1252 wrap_path_free(w_path, path);
1258 int emu_do_system(const char *command)
1260 static char tmp_path[512];
1265 if (command == NULL)
1268 // pass through stuff in PATH
1269 p = strchr(command, ' ');
1270 p2 = strchr(command, '/');
1271 if (p2 == NULL || (p != NULL && p2 > p))
1272 return system(command);
1274 make_local_path(tmp_path, sizeof(tmp_path), "ginge_prep");
1275 p = tmp_path + strlen(tmp_path);
1277 p2 = wrap_path(command);
1278 snprintf(p, sizeof(tmp_path) - (p - tmp_path), " --nomenu %s", p2);
1279 wrap_path_free(p2, command);
1281 dbg("system: \"%s\"\n", tmp_path);
1283 // the app might want the screen too..
1285 ret = system(tmp_path);
1290 int emu_do_execve(const char *filename, char *const argv[], char *const envp[])
1292 const char **new_argv;
1296 if (filename == NULL)
1299 if (strstr(filename, "/gp2xmenu") != NULL)
1302 for (i = 0; argv[i] != NULL; i++)
1306 new_argv = calloc(argc + 2, sizeof(new_argv[0]));
1307 if (new_argv == NULL)
1310 prep_path = malloc(512);
1311 if (prep_path == NULL)
1314 make_local_path(prep_path, 512, "ginge_prep");
1315 new_argv[0] = prep_path;
1316 new_argv[1] = "--nomenu";
1317 new_argv[2] = wrap_path(filename);
1319 if (argv[0] != NULL)
1320 for (i = 1; argv[i] != NULL; i++)
1321 new_argv[i + 2] = argv[i];
1323 dbg("execve \"%s\" %s \"%s\"\n", new_argv[0], new_argv[1], new_argv[2]);
1324 ret = execve(new_argv[0], (char **)new_argv, envp);