2 * GINGE - GINGE Is Not Gp2x Emulator
3 * (C) notaz, 2010-2011,2016
5 * This work is licensed under the MAME license, see COPYING file for details.
9 #error loader is meant to be OABI!
16 #include <sys/types.h>
20 #include <sys/types.h>
23 #include <asm/ucontext.h>
27 #include <sys/resource.h>
28 #include <sys/ioctl.h>
29 #include <linux/soundcard.h>
33 #include "../common/host_fb.h"
34 #include "../common/cmn.h"
35 #include "sys_cacheflush.h"
36 #include "realfuncs.h"
38 #if (DBG & 2) && !(DBG & 4)
53 #define iolog_unh log_io
55 #define iolog_unh(...)
59 #define segvlog printf
64 #if defined(LOG_IO) || defined(LOG_IO_UNK)
65 #include "mmsp2-regs.h"
68 typedef unsigned long long u64;
69 typedef unsigned int u32;
70 typedef unsigned short u16;
71 typedef unsigned char u8;
73 static pthread_mutex_t fb_mutex = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_cond_t fb_cond = PTHREAD_COND_INITIALIZER;
95 #define SRCCTRL_INVIDEO (1 << 8)
96 #define SRCCTRL_SRCENB (1 << 7)
97 #define CTRL_TRANSPARENCYENB (1 << 11)
103 u32 mlc_stl_adr; // mlcaddress for pollux
111 u16 mlc_stl_pallt_d[256*2];
112 u32 mlc_stl_pallt_d32[256];
122 u32 btn_state; // as seen through /dev/GPIO: 0PVdVu YXBA RLSeSt 0Ri0Dn 0Le0Up
130 #define pollux mmsp2 // so that code doesn't look that weird
133 DIRTY_PAL_POLLUX = 2,
137 #if defined(LOG_IO) || defined(LOG_IO_UNK)
138 static void log_io(const char *pfx, u32 a, u32 d, int size)
140 const char *fmt, *reg = "";
142 case 8: fmt = "%s %08x %02x %s\n"; d &= 0xff; break;
143 case 32: fmt = "%s %08x %08x %s\n"; break;
144 default: fmt = "%s %08x %04x %s\n"; d &= 0xffff; break;
147 if ((a & ~0xffff) == 0x7f000000)
148 reg = regnames[a & 0xffff];
150 printf(fmt, pfx, a, d, reg);
154 static void memset16(void *dst, u32 pattern, int count)
159 d = (u16 *)((long)dst & ~1);
165 pattern |= pattern << 16;
172 *(u16 *)dl = pattern;
175 static void blt_tr(void *dst, void *src, u32 trc, int w)
177 u16 *d = (u16 *)((long)dst & ~1);
178 u16 *s = (u16 *)((long)src & ~1);
181 for (; w > 0; d++, s++, w--)
186 #define dump_blitter() \
188 u32 *r = &blitter.dstctrl; \
190 for (i = 0; i < 4*4; i++, r++) { \
191 printf("%08x ", *r); \
197 static void *uppermem_lookup(u32 addr, u8 **mem_end)
199 // XXX: maybe support mirroring?
200 if ((addr & 0xfe000000) != 0x02000000)
203 *mem_end = (u8 *)mmsp2.umem + 0x02000000;
204 return (u8 *)mmsp2.umem - 0x02000000 + addr;
207 static void blitter_do(void)
209 u8 *dst, *dste, *src = NULL, *srce = NULL;
210 int w, h, sstrd, dstrd;
214 w = blitter.size & 0x7ff;
215 h = (blitter.size >> 16) & 0x7ff;
216 sstrd = blitter.srcstride;
217 dstrd = blitter.dststride;
219 // XXX: need to confirm this..
220 addr = (blitter.dstaddr & ~3) | ((blitter.dstctrl & 0x1f) >> 3);
222 // use dst bpp.. How does it do blits with different src bpp?
223 bpp = (blitter.dstctrl & 0x20) ? 16 : 8;
226 if (((w == 320 && h == 240) || // blit whole screen
227 (w * h >= 320*240/2)) && // ..or at least half of the area
228 mmsp2.mlc_stl_adr <= addr && addr < mmsp2.mlc_stl_adr + 320*240*2)
231 dst = uppermem_lookup(addr, &dste);
233 // XXX: assume fill if no SRCENB, but it could be pattern blit..
234 if (blitter.srcctrl & SRCCTRL_SRCENB) {
235 if (!(blitter.srcctrl & SRCCTRL_INVIDEO))
238 addr = (blitter.srcaddr & ~3) | ((blitter.srcctrl & 0x1f) >> 3);
239 src = uppermem_lookup(addr, &srce);
243 if (src + sstrd * h > srce) {
244 err("blit %08x->%08x %dx%d did not fit src\n",
245 blitter.srcaddr, blitter.dstaddr, w, h);
246 h = (srce - src) / sstrd;
253 if (dst + dstrd * h > dste) {
254 err("blit %08x->%08x %dx%d did not fit dst\n",
255 blitter.srcaddr, blitter.dstaddr, w, h);
256 h = (dste - dst) / dstrd;
261 if (bpp == 16 && (blitter.ctrl & CTRL_TRANSPARENCYENB)) {
262 u32 trc = blitter.ctrl >> 16;
263 for (; h > 0; h--, dst += dstrd, src += sstrd)
264 blt_tr(dst, src, trc, w);
267 for (; h > 0; h--, dst += dstrd, src += sstrd)
268 memcpy(dst, src, w * bpp / 8);
272 // fill. Assume the pattern is cleared and bg color is used
273 u32 bgc = blitter.patbackcolor & 0xffff;
275 for (; h > 0; h--, dst += dstrd)
276 memset16(dst, bgc, w);
279 for (; h > 0; h--, dst += dstrd)
280 memset(dst, bgc, w); // bgc?
285 pthread_cond_signal(&fb_cond);
289 err("blit %08x->%08x %dx%d translated to %p->%p\n",
290 blitter.srcaddr, blitter.dstaddr, w, h, src, dst);
294 // FIXME: pass real dimensions to blitters
295 static void mlc_flip(void *src, int bpp, int stride)
299 // only pass pal to host if it's dirty
300 if (bpp <= 8 && mmsp2.v.dirty_pal) {
301 if (mmsp2.v.dirty_pal == DIRTY_PAL_MMSP2)
302 host_video_update_pal32(mmsp2.mlc_stl_pallt_d32);
304 host_video_update_pal16(mmsp2.mlcpalette);
305 mmsp2.v.dirty_pal = 0;
308 if (bpp != old_bpp) {
309 host_video_change_bpp(bpp);
315 host_video_blit4(src, 320, 240, stride);
319 host_video_blit8(src, 320, 240, stride);
323 host_video_blit16(src, 320, 240, stride);
332 #define ts_add_nsec(ts, ns) { \
334 if (ts.tv_nsec >= 1000000000) { \
336 ts.tv_nsec -= 1000000000; \
340 static int fb_sync_thread_paused;
342 static void *fb_sync_thread(void *arg)
344 int invalid_fb_addr = 1;
345 int manual_refresh = 0;
346 int frame_counter = 0;
350 //ret = pthread_setschedprio(pthread_self(), -1);
351 //log("pthread_setschedprio %d\n", ret);
352 //ret = setpriority(PRIO_PROCESS, 0, -1);
353 //log("setpriority %d\n", ret);
355 ret = clock_gettime(CLOCK_REALTIME, &ts);
357 perror(PFX "clock_gettime");
362 u8 *gp2x_fb, *gp2x_fb_end;
364 ret = pthread_mutex_lock(&fb_mutex);
365 wait_ret = pthread_cond_timedwait(&fb_cond, &fb_mutex, &ts);
366 ret |= pthread_mutex_unlock(&fb_mutex);
369 err("fb_thread: mutex error: %d\n", ret);
373 if (wait_ret != 0 && wait_ret != ETIMEDOUT) {
374 err("fb_thread: cond error: %d\n", wait_ret);
378 if (fb_sync_thread_paused) {
379 ts_add_nsec(ts, 100000000);
383 if (wait_ret != ETIMEDOUT) {
384 clock_gettime(CLOCK_REALTIME, &ts);
385 ts_add_nsec(ts, 50000000);
387 if (manual_refresh == 2)
388 dbg("fb_thread: switch to manual refresh\n");
390 ts_add_nsec(ts, 16666667);
391 if (manual_refresh > 1)
392 dbg("fb_thread: switch to auto refresh\n");
396 gp2x_fb = uppermem_lookup(mmsp2.mlc_stl_adr, &gp2x_fb_end);
397 if (gp2x_fb == NULL || gp2x_fb + 320*240 * mmsp2.v.bpp / 8 > gp2x_fb_end) {
398 if (!invalid_fb_addr) {
399 err("fb_thread: %08x is out of range\n", mmsp2.mlc_stl_adr);
406 mlc_flip(gp2x_fb, mmsp2.v.bpp, mmsp2.v.stride);
409 if (frame_counter & 0x0f)
413 // this is to check for kill key, in case main thread hung
414 // or something else went wrong.
415 pollux.btn_state = host_read_btns();
419 static void fb_thread_pause(void)
421 fb_sync_thread_paused = 1;
422 // wait until it finishes last refresh
423 // that it might be doing now
427 static void fb_thread_resume(void)
429 fb_sync_thread_paused = 0;
432 static u32 xread32_io_cmn(u32 a, u32 *handled)
439 case 0x402c: // MLCVSTRIDE0
440 case 0x4060: // MLCVSTRIDE1
443 case 0x4038: // MLCADDRESS0
444 case 0x406c: // MLCADDRESS1
445 d = pollux.mlc_stl_adr;
448 // ???? ???? YXBA DURiLe ???? VdVuMS LR?? ????
449 // | GPIOC[31:16] | GPIOB[31:16] |
450 case 0xa058: // GPIOBPAD
451 d = (pollux.btn_state >> 1) & 0x0100;
452 d |= (pollux.btn_state << 1) & 0x0200;
453 d |= (pollux.btn_state >> 3) & 0x0080;
454 d |= (pollux.btn_state >> 5) & 0x0040;
455 d |= (pollux.btn_state >> 6) & 0x0c00;
459 case 0xa098: // GPIOCPAD
460 pollux.btn_state = host_read_btns();
461 d = (pollux.btn_state >> 8) & 0x00f0;
462 d |= (pollux.btn_state >> 1) & 0x0008;
463 d |= (pollux.btn_state << 2) & 0x0004;
464 d |= (pollux.btn_state >> 5) & 0x0002;
465 d |= (pollux.btn_state >> 2) & 0x0001;
477 static u32 xread8(u32 a)
479 iolog("r8 ", a, 0, 8);
480 iolog_unh("r8 ", a, 0, 8);
484 static u32 xread16(u32 a)
486 static u32 fudge, old_a;
489 if ((a & 0xffff0000) == 0x7f000000) {
497 // 0000 P000 VuVd00 0000 YXBA RLSeSt 0Ri0D 0Le0U
498 // | GPIOD |GPIOC[8:15]|GPIOM[0:7] |
501 case 0x1184: // GPIOC
502 d = ~mmsp2.btn_state & 0xff00;
505 case 0x1186: // GPIOD
506 t = ~mmsp2.btn_state;
507 d = (t >> 9) & 0x0080;
508 d |= (t >> 11) & 0x0040;
509 d |= (t >> 7) & 0x0800;
512 case 0x1198: // GPIOM
513 mmsp2.btn_state = host_read_btns();
514 d = ~mmsp2.btn_state & 0xff;
517 case 0x1836: // reserved
520 case 0x2816: // DPC_X_MAX
523 case 0x2818: // DPC_Y_MAX
527 d = mmsp2.mlc_stl_cntl;
531 d = mmsp2.mlc_stl_adrl;
535 d = mmsp2.mlc_stl_adrh;
538 d = mmsp2.mlc_stl_pallt_a;
542 d = xread32_io_cmn(a_, &t);
558 iolog_unh("r16", a, d & 0xffff, 16);
562 iolog("r16", a, d, 16);
566 static u32 xread32(u32 a)
569 if ((a & 0xfff00000) == 0x7f000000) {
576 case 0x0a00: // TCOUNT, 1/7372800s
577 clock_gettime(CLOCK_REALTIME, &ts);
578 t64 = (u64)ts.tv_sec * 1000000000 + ts.tv_nsec;
579 // t * 7372800.0 / 1000000000 * 0x100000000 ~= t * 31665935
585 d = xread32_io_cmn(a_, &t);
592 if ((a & 0xfff00000) == 0x7f100000) {
593 u32 *bl = &blitter.dstctrl;
604 iolog_unh("r32", a, d, 32);
607 iolog("r32", a, d, 32);
611 static void xwrite8(u32 a, u32 d)
613 iolog("w8 ", a, d, 8);
614 iolog_unh("w8 ", a, d, 8);
617 static void xwrite16(u32 a, u32 d)
619 iolog("w16", a, d, 16);
620 if ((a & 0xfff00000) == 0x7f000000) {
625 mmsp2.mlc_stl_cntl = d | 0xaa;
627 mmsp2.v.bpp = mode ? mode * 8 : 4;
635 // odd addresses don't affect LCD. What about TV?
638 mmsp2.mlc_stl_adrl = d;
641 mmsp2.mlc_stl_adrh = d;
642 if (mmsp2.mlc_stl_adr != mmsp2.old_mlc_stl_adr)
644 pthread_cond_signal(&fb_cond);
645 mmsp2.old_mlc_stl_adr = mmsp2.mlc_stl_adr;
648 mmsp2.mlc_stl_pallt_a = d & 0x1ff;
651 mmsp2.mlc_stl_pallt_d[mmsp2.mlc_stl_pallt_a++] = d;
652 mmsp2.mlc_stl_pallt_a &= 0x1ff;
653 mmsp2.v.dirty_pal = DIRTY_PAL_MMSP2;
657 iolog_unh("w16", a, d, 16);
660 static void xwrite32(u32 a, u32 d)
662 iolog("w32", a, d, 32);
664 if ((a & 0xfff00000) == 0x7f000000) {
668 case 0x4024: // MLCCONTROL0
669 case 0x4058: // MLCCONTROL1
670 pollux.mlccontrol = d;
672 return; // layer not enabled
673 if ((d >> 16) == 0x443A)
678 case 0x402c: // MLCVSTRIDE0
679 case 0x4060: // MLCVSTRIDE1
682 case 0x4038: // MLCADDRESS0
683 case 0x406c: // MLCADDRESS1
684 pollux.mlc_stl_adr = d;
685 if (d != mmsp2.old_mlc_stl_adr)
687 pthread_cond_signal(&fb_cond);
688 mmsp2.old_mlc_stl_adr = d;
690 case 0x403c: // MLCPALETTE0
691 case 0x4070: // MLCPALETTE1
692 pollux.mlcpalette[d >> 24] = d;
693 pollux.v.dirty_pal = DIRTY_PAL_POLLUX;
697 if ((a & 0xfff00000) == 0x7f100000) {
698 u32 *bl = &blitter.dstctrl;
702 if (a_ == 0x34 && (d & 1))
707 iolog_unh("w32", a, d, 32);
710 #define LINKPAGE_SIZE 0x1000
711 #define LINKPAGE_COUNT 4
712 #define LINKPAGE_ALLOC (LINKPAGE_SIZE * LINKPAGE_COUNT)
721 void (*handler)(struct op_context *op_ctx);
725 struct op_stackframe {
730 static struct op_linkpage *g_linkpage;
731 static u32 *g_code_ptr;
732 static int g_linkpage_count;
734 #define BIT_SET(v, b) (v & (1 << (b)))
736 void emu_handle_op(struct op_context *op_ctx, struct op_stackframe *sframe)
738 u32 *regs = sframe->saved_regs;
740 u32 t, shift, ret, addr;
743 cond = (op & 0xf0000000) >> 28;
744 rd = (op & 0x0000f000) >> 12;
745 rn = (op & 0x000f0000) >> 16;
748 goto unhandled; // TODO
750 if ((op & 0x0f200090) == 0x01000090) { // AM3: LDRH, STRH
751 if (!BIT_SET(op, 5)) // !H
753 if (BIT_SET(op, 6) && !BIT_SET(op, 20)) // S && !L
756 if (BIT_SET(op, 22)) // imm offset
757 t = ((op & 0xf00) >> 4) | (op & 0x0f);
759 t = regs[op & 0x000f];
761 if (!BIT_SET(op, 23))
765 if (BIT_SET(op, 20)) { // Load
767 if (BIT_SET(op, 6)) { // S
769 ret = (signed int)ret >> 16;
774 xwrite16(addr, regs[rd]);
776 else if ((op & 0x0c000000) == 0x04000000) { // load/store word/byte
778 goto unhandled; // unprivileged
779 if (BIT_SET(op, 25)) { // reg offs
781 goto unhandled; // nah it's media
783 t = regs[op & 0x000f];
784 shift = (op & 0x0f80) >> 7;
785 switch ((op & 0x0060) >> 5) {
786 case 0: t = t << shift; break;
787 case 1: t = t >> (shift + 1); break;
788 case 2: t = (signed int)t >> (shift + 1); break;
789 case 3: goto unhandled; // I'm just lazy
795 if (!BIT_SET(op, 23))
799 if (BIT_SET(op, 24)) // pre-indexed
801 if (!BIT_SET(op, 24) || BIT_SET(op, 21))
802 regs[rn] += t; // writeback
804 if (BIT_SET(op, 20)) { // Load
805 if (BIT_SET(op, 22)) // Byte
812 if (BIT_SET(op, 22)) // Byte
813 xwrite8(addr, regs[rd]);
815 xwrite32(addr, regs[rd]);
822 if (addr != addr_check) {
823 fprintf(stderr, "bad calculated addr: %08x vs %08x\n", addr, addr_check);
830 err("unhandled IO op %08x @ %08x\n", op, op_ctx->pc);
834 static u32 make_offset12(u32 *pc, u32 *target)
838 lp_offs = (char *)target - (char *)pc - 2*4;
843 if (lp_offs >= LINKPAGE_SIZE) {
844 err("linkpage too far: %d\n", lp_offs);
848 return (u << 23) | lp_offs;
851 static u32 make_jmp(u32 *pc, u32 *target, int bl)
855 jmp_val = target - pc - 2;
856 if (jmp_val < (int)0xff000000 || jmp_val > 0x00ffffff) {
857 err("jump out of range (%p -> %p)\n", pc, target);
861 return 0xea000000 | (bl << 24) | (jmp_val & 0x00ffffff);
864 static void emit_op(u32 op)
869 static void emit_op_io(u32 op, u32 *target)
871 op |= make_offset12(g_code_ptr, target);
875 static void init_linkpage(void)
877 g_linkpage->handler = emu_call_handle_op;
878 g_code_ptr = g_linkpage->code;
881 static void segv_sigaction(int num, siginfo_t *info, void *ctx)
883 struct ucontext *context = ctx;
884 u32 *regs = (u32 *)&context->uc_mcontext.arm_r0;
885 u32 *pc = (u32 *)regs[15];
886 struct op_context *op_ctx;
889 if (((regs[15] ^ (u32)&segv_sigaction) & 0xff000000) == 0 || // PC is in our segment or
890 (((regs[15] ^ (u32)g_linkpage) & ~(LINKPAGE_ALLOC - 1)) == 0) || // .. in linkpage
891 ((long)info->si_addr & 0xffe00000) != 0x7f000000) // faulting not where expected
893 // real crash - time to die
894 err("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]);
895 for (i = 0; i < 8; i++)
896 dbg(" r%d=%08x r%2d=%08x\n", i, regs[i], i+8, regs[i+8]);
897 signal(num, SIG_DFL);
901 segvlog("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]);
904 op_ctx = (void *)g_code_ptr;
905 op_ctx->pc = (u32)pc;
907 g_code_ptr = &op_ctx->code[0];
909 // emit jump to code ptr
910 *pc = make_jmp(pc, g_code_ptr, 0);
913 emit_op (0xe50d0000 + 0xf00 - 4 * 0); // str r0, [sp, #(-0xf00 + r0_offs)]
914 emit_op (0xe50de000 + 0xf00 - 4 * 14); // str lr, [sp, #(-0xf00 + lr_offs)]
915 emit_op (0xe24f0000 + (g_code_ptr - (u32 *)op_ctx + 2) * 4); // sub r0, pc, #op_ctx
916 emit_op (0xe1a0e00f); // mov lr, pc
917 emit_op_io(0xe51ff000, (u32 *)&g_linkpage->handler); // ldr pc, =handle_op
918 emit_op (0xe51de000 + 0xf00 - 4 * 14); // ldr lr, [sp, #(-0xf00 + lr_offs)]
919 emit_op (make_jmp(g_code_ptr, pc + 1, 0)); // jmp <back>
922 sys_cacheflush(pc, pc + 1);
923 sys_cacheflush(g_linkpage, g_code_ptr);
925 lp_size = (char *)g_code_ptr - (char *)g_linkpage;
926 segvlog("code #%d %d/%d\n", g_linkpage_count, lp_size, LINKPAGE_SIZE);
928 if (lp_size + 13*4 > LINKPAGE_SIZE) {
930 if (g_linkpage_count >= LINKPAGE_COUNT) {
931 err("too many linkpages needed\n");
934 g_linkpage = (void *)((char *)g_linkpage + LINKPAGE_SIZE);
937 //handle_op(regs[15], op, regs, (u32)info->si_addr);
941 void emu_init(void *map_bottom)
943 sigaction_t segv_action = {
944 .sa_sigaction = segv_sigaction,
945 .sa_flags = SA_SIGINFO,
952 if (geteuid() == 0) {
953 fprintf(stderr, "don't try to run as root, device registers or memory "
954 "might get trashed crashing the OS or even damaging the device.\n");
959 g_linkpage = (void *)(((u32)map_bottom - LINKPAGE_ALLOC) & ~0xfff);
960 pret = mmap(g_linkpage, LINKPAGE_ALLOC, PROT_READ|PROT_WRITE,
961 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
962 if (pret != g_linkpage) {
963 perror(PFX "mmap linkpage");
966 log("linkpages @ %p\n", g_linkpage);
972 err("can't init host\n");
976 ret = host_video_init(NULL, 0);
978 err("can't init host video\n");
982 // TODO: check if this really fails on Wiz..
983 mmsp2.umem = mmap(NULL, 0x2000000, PROT_READ|PROT_WRITE|PROT_EXEC,
984 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
986 if (mmsp2.umem == MAP_FAILED) {
987 // we are short on memmory on Wiz, need special handling
988 extern void *host_mmap_upper(void);
989 mmsp2.umem = host_mmap_upper();
992 if (mmsp2.umem == MAP_FAILED) {
993 perror(PFX "mmap upper mem");
997 ret = pthread_create(&tid, NULL, fb_sync_thread, NULL);
999 err("failed to create fb_sync_thread: %d\n", ret);
1002 pthread_detach(tid);
1005 mmsp2.mlc_stl_adr = 0x03101000; // fb2 is at 0x03381000
1006 mmsp2.mlc_stl_cntl = 0x4ab; // 16bpp, region 1 active
1007 mmsp2.v.width = 320;
1008 mmsp2.v.height = 240;
1009 mmsp2.v.stride = 320*2;
1011 mmsp2.v.dirty_pal = 1;
1013 sigemptyset(&segv_action.sa_mask);
1014 sigaction(SIGSEGV, &segv_action, NULL);
1017 int emu_read_gpiodev(void *buf, int count)
1020 err("gpiodev read %d?\n", count);
1026 mmsp2.btn_state = host_read_btns();
1027 memcpy(buf, &mmsp2.btn_state, count);
1031 static void *emu_mmap_dev(unsigned int length, int prot, int flags, unsigned int offset)
1033 u8 *umem, *umem_end;
1036 if ((offset & ~0x1ffff) == 0xc0000000) {
1037 return mmap((void *)0x7f000000, length, PROT_NONE,
1038 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0);
1041 if ((offset & ~0xffff) == 0xe0020000) {
1042 return mmap((void *)0x7f100000, length, PROT_NONE,
1043 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0);
1046 if ((offset & 0xfe000000) != 0x02000000) {
1047 err("unexpected devmem mmap @ %08x\n", offset);
1052 umem = uppermem_lookup(offset, &umem_end);
1053 if (umem + length > umem_end)
1054 err("warning: uppermem @ %08x overflows by %d bytes\n",
1055 offset, umem + length - umem_end);
1057 dbg("upper mem @ %08x %d\n", offset, length);
1061 void *emu_do_mmap(unsigned int length, int prot, int flags, int fd, unsigned int offset)
1063 if (fd == FAKEDEV_MEM)
1064 return emu_mmap_dev(length, prot, flags, offset);
1066 if (fd == FAKEDEV_FB0)
1067 return emu_mmap_dev(length, prot, flags, offset + 0x03101000);
1069 if (fd == FAKEDEV_FB1)
1070 return emu_mmap_dev(length, prot, flags, offset + 0x03381000);
1072 err("bad/ni mmap(?, %d, %x, %x, %d, %08x)\n", length, prot, flags, fd, offset);
1077 static void emu_sound_open(int fd)
1082 // set default buffer size to 16 * 1K
1083 frag = (16<<16) | 10; // 16K
1084 ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
1086 err("snd ioctl SETFRAGMENT %08x: ", frag);
1092 static int emu_sound_ioctl(int fd, int request, void *argp)
1097 dbg("snd ioctl(%d, %08x, %p)", fd, request, argp);
1099 dbg_c(" [%d]", *arg);
1103 /* People set strange frag settings on GP2X, which even manage
1104 * to break audio on pandora (causes writes to fail).
1105 * Catch this and set to something that works. */
1107 case SNDCTL_DSP_SETFRAGMENT: {
1108 int ret, bsize, frag, frag_cnt;
1112 frag = *arg & 0xffff;
1113 frag_cnt = *arg >> 16;
1114 bsize = frag_cnt << frag;
1115 if (frag < 10 || bsize < 4096*4 || bsize > 4096*4*2) {
1117 * ~4ms. gpSP wants small buffers or else it stutters
1118 * because of it's audio thread sync stuff
1119 * XXX: hardcoding, as low samplerates will result in small fragment size,
1120 * which itself causes ALSA stall and hangs the program.
1121 * Also some apps change samplerate without reopening /dev/dsp,
1122 * which causes ALSA to reject SNDCTL_DSP_SETFRAGMENT.
1124 bsize = 44100 / 250 * 4;
1126 for (frag = 0; bsize; bsize >>= 1, frag++)
1132 frag |= frag_cnt << 16;
1133 ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
1135 err("snd ioctl SETFRAGMENT %08x: ", frag);
1138 // indicate success even if we fail (because of ALSA mostly),
1139 // things like MikMod will bail out otherwise.
1142 case SNDCTL_DSP_SYNC:
1143 // Franxis tends to use sync/write loops, bad idea under ALSA
1149 return ioctl(fd, request, argp);
1152 int emu_do_ioctl(int fd, int request, void *argp)
1154 if (fd == emu_interesting_fds[IFD_SOUND].fd)
1155 return emu_sound_ioctl(fd, request, argp);
1158 /* *********************** */
1165 case FBIOGET_FSCREENINFO: {
1166 struct fb_fix_screeninfo *fix = argp;
1168 memset(fix, 0, sizeof(*fix));
1169 strcpy(fix->id, "mmsp2_RGB0");
1170 fix->type = FB_TYPE_PACKED_PIXELS;
1171 fix->accel = FB_ACCEL_NONE;
1172 fix->visual = FB_VISUAL_TRUECOLOR;
1173 fix->line_length = 320*2;
1174 fix->smem_start = (fd == FAKEDEV_FB0) ? 0x03101000 : 0x03381000;
1175 fix->smem_len = 320*240*2;
1178 case FBIOGET_VSCREENINFO: {
1179 struct fb_var_screeninfo *var = argp;
1180 static const struct fb_bitfield fbb_red = { offset: 11, length: 5, };
1181 static const struct fb_bitfield fbb_green = { offset: 5, length: 6, };
1182 static const struct fb_bitfield fbb_blue = { offset: 0, length: 5, };
1184 memset(var, 0, sizeof(*var));
1185 var->activate = FB_ACTIVATE_NOW;
1187 var->xres_virtual = 320;
1189 var->yres_virtual = 240;
1192 var->vmode = FB_VMODE_NONINTERLACED;
1193 var->bits_per_pixel = 16;
1195 var->green = fbb_green;
1196 var->blue = fbb_blue;
1199 case FBIOPUT_VSCREENINFO: {
1200 struct fb_var_screeninfo *var = argp;
1201 dbg(" put vscreen: %dx%d@%d\n", var->xres, var->yres, var->bits_per_pixel);
1202 if (var->xres != 320 || var->yres != 240 || var->bits_per_pixel != 16)
1208 /* *********************** */
1210 // fake tty0 to make GPH SDL happy
1211 if (request == 0x4b46) // KDGKBENT
1217 err("bad/ni ioctl(%d, %08x, %p)\n", fd, request, argp);
1222 struct dev_fd_t emu_interesting_fds[] = {
1223 [IFD_SOUND] = { "/dev/dsp", -1, emu_sound_open },
1227 static const struct {
1231 { "/mnt/tmp", "./tmp" },
1234 static const char *wrap_path(const char *path)
1240 // do only path mapping for now
1241 for (i = 0; i < ARRAY_SIZE(path_map); i++) {
1242 p = strstr(path, path_map[i].from);
1244 size = strlen(path) + strlen(path_map[i].to) + 1;
1245 buff = malloc(size);
1249 strncpy(buff, path, len);
1250 snprintf(buff + len, size - len, "%s%s", path_map[i].to,
1251 path + len + strlen(path_map[i].from));
1252 dbg("mapped path \"%s\" -> \"%s\"\n", path, buff);
1260 static void wrap_path_free(const char *w_path, const char *old_path)
1262 if (w_path != old_path)
1263 free((void *)w_path);
1266 void *emu_do_fopen(const char *path, const char *mode)
1271 w_path = wrap_path(path);
1272 ret = fopen(w_path, mode);
1273 wrap_path_free(w_path, path);
1279 int emu_do_system(const char *command)
1281 static char tmp_path[512];
1287 if (command == NULL)
1290 for (p2 = command; *p2 && isspace(*p2); p2++)
1293 if (*p2 == '.') // relative path?
1295 else if (*p2 == '/' && strncmp(p2, "/bin", 4) && strncmp(p2, "/lib", 4)
1296 && strncmp(p2, "/sbin", 4) && strncmp(p2, "/usr", 4))
1297 // absolute path, but not a system command
1300 p2 = wrap_path(command);
1302 make_local_path(tmp_path, sizeof(tmp_path), "ginge_prep");
1303 p = tmp_path + strlen(tmp_path);
1305 snprintf(p, sizeof(tmp_path) - (p - tmp_path), " --nomenu %s", p2);
1308 snprintf(tmp_path, sizeof(tmp_path), "%s", p2);
1309 wrap_path_free(p2, command);
1311 dbg("system: \"%s\"\n", tmp_path);
1313 // the app might want the screen too..
1315 ret = system(tmp_path);
1320 int emu_do_execve(const char *filename, char *const argv[], char *const envp[])
1322 const char **new_argv;
1326 if (filename == NULL)
1329 if (strstr(filename, "/gp2xmenu") != NULL)
1332 for (i = 0; argv[i] != NULL; i++)
1336 new_argv = calloc(argc + 2, sizeof(new_argv[0]));
1337 if (new_argv == NULL)
1340 prep_path = malloc(512);
1341 if (prep_path == NULL)
1344 make_local_path(prep_path, 512, "ginge_prep");
1345 new_argv[0] = prep_path;
1346 new_argv[1] = "--nomenu";
1347 new_argv[2] = wrap_path(filename);
1349 if (argv[0] != NULL)
1350 for (i = 1; argv[i] != NULL; i++)
1351 new_argv[i + 2] = argv[i];
1353 dbg("execve \"%s\" %s \"%s\"\n", new_argv[0], new_argv[1], new_argv[2]);
1354 ret = execve(new_argv[0], (char **)new_argv, envp);
1359 // vim:shiftwidth=2:expandtab