wiz blitters, fb restore, tweaks
[ginge.git] / loader / emu.c
index 389103e..993ed55 100644 (file)
 #include <errno.h>
 #include <time.h>
 #include <sys/resource.h>
+#include <sys/ioctl.h>
+#include <linux/soundcard.h>
+#include <linux/fb.h>
 
 #include "header.h"
+#include "../common/host_fb.h"
+#include "../common/cmn.h"
 #include "sys_cacheflush.h"
+#include "realfuncs.h"
 
-//#define LOG_IO
-//#define LOG_IO_UNK
+#if (DBG & 2) && !(DBG & 4)
+#define LOG_IO_UNK
+#endif
+#if (DBG & 4)
+#define LOG_IO
+#endif
 //#define LOG_SEGV
 
 #ifdef LOG_IO
@@ -45,6 +55,7 @@
 #include "mmsp2-regs.h"
 #endif
 
+typedef unsigned long long u64;
 typedef unsigned int   u32;
 typedef unsigned short u16;
 typedef unsigned char  u8;
@@ -52,15 +63,6 @@ typedef unsigned char  u8;
 static pthread_mutex_t fb_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t fb_cond = PTHREAD_COND_INITIALIZER;
 
-struct uppermem_block {
-  u32 addr; // physical
-  u32 size;
-  void *mem;
-  struct uppermem_block *next;
-};
-
-static struct uppermem_block *upper_mem;
-
 static struct {
   u32 dstctrl;
   u32 dstaddr;
@@ -100,15 +102,12 @@ static struct {
   };
 
   // state
-  u16 host_pal[256];
+  void *umem;
   u32 old_mlc_stl_adr;
   u32 btn_state; // as seen through /dev/GPIO
-  u16 dirty_pal:1;
+  u32 dirty_pal:1;
 } mmsp2;
 
-static u16 *host_screen;
-static int host_stride;
-
 
 #if defined(LOG_IO) || defined(LOG_IO_UNK)
 static void log_io(const char *pfx, u32 a, u32 d, int size)
@@ -172,16 +171,12 @@ static void blt_tr(void *dst, void *src, u32 trc, int w)
 
 static void *uppermem_lookup(u32 addr, u8 **mem_end)
 {
-  struct uppermem_block *ub;
+  // XXX: maybe support mirroring?
+  if ((addr & 0xfe000000) != 0x02000000)
+    return NULL;
 
-  for (ub = upper_mem; ub != NULL; ub = ub->next) {
-    if (ub->addr <= addr && addr < ub->addr + ub->size) {
-      *mem_end = (u8 *)ub->mem + ub->size;
-      return (u8 *)ub->mem + addr - ub->addr;
-    }
-  }
-
-  return NULL;
+  *mem_end = (u8 *)mmsp2.umem + 0x02000000;
+  return (u8 *)mmsp2.umem - 0x02000000 + addr;
 }
 
 static void blitter_do(void)
@@ -189,7 +184,7 @@ static void blitter_do(void)
   u8 *dst, *dste, *src = NULL, *srce = NULL;
   int w, h, sstrd, dstrd;
   int to_screen = 0;
-  u32 addr;
+  u32 bpp, addr;
 
   w = blitter.size & 0x7ff;
   h = (blitter.size >> 16) & 0x7ff;
@@ -199,8 +194,13 @@ static void blitter_do(void)
   // XXX: need to confirm this..
   addr = (blitter.dstaddr & ~3) | ((blitter.dstctrl & 0x1f) >> 3);
 
+  // use dst bpp.. How does it do blits with different src bpp?
+  bpp = (blitter.dstctrl & 0x20) ? 16 : 8;
+
   // maybe the screen?
-  if (w == 320 && h == 240 && mmsp2.mlc_stl_adr <= addr && addr < mmsp2.mlc_stl_adr + 320*240*2)
+  if (((w == 320 && h == 240) || // blit whole screen
+       (w * h >= 320*240/2)) &&  // ..or at least half of the area
+       mmsp2.mlc_stl_adr <= addr && addr < mmsp2.mlc_stl_adr + 320*240*2)
     to_screen = 1;
 
   dst = uppermem_lookup(addr, &dste);
@@ -222,6 +222,9 @@ static void blitter_do(void)
     }
   }
 
+  if (dst == NULL)
+    goto bad_blit;
+
   if (dst + dstrd * h > dste) {
     err("blit %08x->%08x %dx%d did not fit dst\n",
       blitter.srcaddr, blitter.dstaddr, w, h);
@@ -230,21 +233,27 @@ static void blitter_do(void)
 
   if (src != NULL) {
     // copy
-    if (blitter.ctrl & CTRL_TRANSPARENCYENB) {
+    if (bpp == 16 && (blitter.ctrl & CTRL_TRANSPARENCYENB)) {
       u32 trc = blitter.ctrl >> 16;
       for (; h > 0; h--, dst += dstrd, src += sstrd)
         blt_tr(dst, src, trc, w);
     }
     else {
       for (; h > 0; h--, dst += dstrd, src += sstrd)
-        memcpy(dst, src, w * 2);
+        memcpy(dst, src, w * bpp / 8);
     }
   }
   else {
     // fill. Assume the pattern is cleared and bg color is used
     u32 bgc = blitter.patbackcolor & 0xffff;
-    for (; h > 0; h--, dst += dstrd)
-      memset16(dst, bgc, w);
+    if (bpp == 16) {
+      for (; h > 0; h--, dst += dstrd)
+        memset16(dst, bgc, w);
+    }
+    else {
+      for (; h > 0; h--, dst += dstrd)
+        memset(dst, bgc, w); // bgc?
+    }
   }
 
   if (to_screen)
@@ -257,56 +266,39 @@ bad_blit:
   dump_blitter();
 }
 
-// TODO: hw scaler stuff
-static void mlc_flip(u8 *src, int bpp)
+// FIXME: pass real dimensions to blitters
+static void mlc_flip(void *src, int bpp)
 {
-  u16 *dst = host_screen;
-  u16 *hpal = mmsp2.host_pal;
-  int i, u;
+  static int old_bpp;
 
+  // only pass pal to host if it's dirty
   if (bpp <= 8 && mmsp2.dirty_pal) {
-    u32 *srcp = mmsp2.mlc_stl_pallt_d32;
-    u16 *dstp = hpal;
-
-    for (i = 0; i < 256; i++, srcp++, dstp++) {
-      u32 t = *srcp;
-      *dstp = ((t >> 8) & 0xf800) | ((t >> 5) & 0x07e0) | ((t >> 3) & 0x001f);
-    }
+    host_video_update_pal(mmsp2.mlc_stl_pallt_d32);
     mmsp2.dirty_pal = 0;
   }
 
+  if (bpp != old_bpp) {
+    host_video_change_bpp(bpp);
+    old_bpp = bpp;
+  }
+
   switch (bpp) {
   case  4:
-    for (i = 0; i < 240; i++, dst += host_stride / 2 - 320) {
-      for (u = 320 / 2; u > 0; u--, src++) {
-        *dst++ = hpal[*src >> 4];
-        *dst++ = hpal[*src & 0x0f];
-      }
-    }
+    host_video_blit4(src, 320, 240);
     break;
 
   case  8:
-    for (i = 0; i < 240; i++, dst += host_stride / 2 - 320) {
-      for (u = 320 / 4; u > 0; u--) {
-        *dst++ = hpal[*src++];
-        *dst++ = hpal[*src++];
-        *dst++ = hpal[*src++];
-        *dst++ = hpal[*src++];
-      }
-    }
+    host_video_blit8(src, 320, 240);
     break;
 
   case 16:
-    for (i = 0; i < 240; i++, dst += host_stride / 2, src += 320*2)
-      memcpy(dst, src, 320*2);
+    host_video_blit16(src, 320, 240);
     break;
 
   case 24:
     // TODO
     break;
   }
-
-  host_screen = host_video_flip();
 }
 
 #define ts_add_nsec(ts, ns) { \
@@ -342,6 +334,7 @@ static void *fb_sync_thread(void *arg)
     ret =  pthread_mutex_lock(&fb_mutex);
     wait_ret = pthread_cond_timedwait(&fb_cond, &fb_mutex, &ts);
     ret |= pthread_mutex_unlock(&fb_mutex);
+
     if (ret != 0) {
       err("fb_thread: mutex error: %d\n", ret);
       sleep(1);
@@ -358,11 +351,11 @@ static void *fb_sync_thread(void *arg)
       ts_add_nsec(ts, 50000000);
       manual_refresh++;
       if (manual_refresh == 2)
-        log("fb_thread: switch to manual refresh\n");
+        dbg("fb_thread: switch to manual refresh\n");
     } else {
       ts_add_nsec(ts, 16666667);
       if (manual_refresh > 1)
-        log("fb_thread: switch to auto refresh\n");
+        dbg("fb_thread: switch to auto refresh\n");
       manual_refresh = 0;
     }
 
@@ -468,6 +461,21 @@ out:
 static u32 xread32(u32 a)
 {
   u32 d = 0;
+  if ((a & 0xfff00000) == 0x7f000000) {
+    u32 a_ = a & 0xffff;
+    struct timespec ts;
+    u64 t64;
+
+    switch (a_) {
+    case 0x0a00: // TCOUNT, 1/7372800s
+      clock_gettime(CLOCK_REALTIME, &ts);
+      t64 = (u64)ts.tv_sec * 1000000000 + ts.tv_nsec;
+      // t * 7372800.0 / 1000000000 * 0x100000000 ~= t * 31665935
+      t64 *= 31665935;
+      d = t64 >> 32;
+      break;
+    }
+  }
   if ((a & 0xfff00000) == 0x7f100000) {
     u32 *bl = &blitter.dstctrl;
     u32 a_ = a & 0xfff;
@@ -554,25 +562,25 @@ struct op_context {
   u32 code[0];
 };
 
-struct linkpage {
-  u32 saved_regs[15];
-  u32 cpsr;
-  u32 *handler_stack;
+struct op_linkpage {
   void (*handler)(struct op_context *op_ctx);
   u32 code[0];
 };
 
-static struct linkpage *g_linkpage;
+struct op_stackframe {
+  u32 saved_regs[15];
+  u32 cpsr;
+};
+
+static struct op_linkpage *g_linkpage;
 static u32 *g_code_ptr;
 static int g_linkpage_count;
 
-static void *g_handler_stack_end;
-
 #define BIT_SET(v, b) (v & (1 << (b)))
 
-static void handle_op(struct op_context *op_ctx)
+void emu_handle_op(struct op_context *op_ctx, struct op_stackframe *sframe)
 {
-  u32 *regs = g_linkpage->saved_regs;
+  u32 *regs = sframe->saved_regs;
   u32 op = op_ctx->op;
   u32 t, shift, ret, addr;
   int rn, rd;
@@ -699,30 +707,8 @@ static void emit_op_io(u32 op, u32 *target)
 
 static void init_linkpage(void)
 {
-  g_linkpage->handler = handle_op;
-  g_linkpage->handler_stack = g_handler_stack_end;
+  g_linkpage->handler = emu_call_handle_op;
   g_code_ptr = g_linkpage->code;
-
-  // common_code.
-  // r0 and r14 must be saved by caller, r0 is arg for handle_op
-  // on return everything is restored except lr, which is used to return
-  emit_op_io(0xe50f1000, &g_linkpage->saved_regs[1]);  // str r1, [->saved_regs[1]] @ save r1
-  emit_op   (0xe24f1000 +                              // sub r1, pc, =offs(saved_regs[2])
-    (g_code_ptr - &g_linkpage->saved_regs[2] + 2) * 4);
-  emit_op   (0xe8813ffc);                              // stmia r1, {r2-r13}
-  emit_op_io(0xe51fd000,                               // ldr sp, [->handler_stack]
-    (u32 *)&g_linkpage->handler_stack);
-  emit_op   (0xe2414008);                              // sub r4, r1, #4*2
-  emit_op   (0xe10f1000);                              // mrs r1, cpsr
-  emit_op_io(0xe50f1000, &g_linkpage->cpsr);           // str r1, [->cpsr]
-  emit_op   (0xe1a0500e);                              // mov r5, lr
-  emit_op   (0xe1a0e00f);                              // mov lr, pc
-  emit_op_io(0xe51ff000, (u32 *)&g_linkpage->handler); // ldr pc, =handle_op
-  emit_op_io(0xe51f1000, &g_linkpage->cpsr);           // ldr r1, [->cpsr]
-  emit_op   (0xe128f001);                              // msr cpsr_f, r1
-  emit_op   (0xe1a0e005);                              // mov lr, r5
-  emit_op   (0xe8943fff);                              // ldmia r4, {r0-r13}
-  emit_op   (0xe12fff1e);                              // bx lr @ return
 }
 
 static void segv_sigaction(int num, siginfo_t *info, void *ctx)
@@ -731,7 +717,7 @@ static void segv_sigaction(int num, siginfo_t *info, void *ctx)
   u32 *regs = (u32 *)&context->uc_mcontext.arm_r0;
   u32 *pc = (u32 *)regs[15];
   struct op_context *op_ctx;
-  int lp_size;
+  int i, lp_size;
 
   if (((regs[15] ^ (u32)&segv_sigaction) & 0xff000000) == 0 ||         // PC is in our segment or
       (((regs[15] ^ (u32)g_linkpage) & ~(LINKPAGE_ALLOC - 1)) == 0) || // .. in linkpage
@@ -739,6 +725,8 @@ static void segv_sigaction(int num, siginfo_t *info, void *ctx)
   {
     // real crash - time to die
     err("segv %d %p @ %08x\n", info->si_code, info->si_addr, regs[15]);
+    for (i = 0; i < 8; i++)
+      dbg(" r%d=%08x r%2d=%08x\n", i, regs[i], i+8, regs[i+8]);
     signal(num, SIG_DFL);
     raise(num);
     return;
@@ -755,12 +743,12 @@ static void segv_sigaction(int num, siginfo_t *info, void *ctx)
   *pc = make_jmp(pc, g_code_ptr, 0);
 
   // generate code:
-  // TODO: multithreading
-  emit_op_io(0xe50f0000, &g_linkpage->saved_regs[0]);            // str r0,  [->saved_regs[0]] @ save r0
-  emit_op_io(0xe50fe000, &g_linkpage->saved_regs[14]);           // str r14, [->saved_regs[14]]
+  emit_op   (0xe50d0000 + 0xf00 - 4 * 0);                        // str r0, [sp, #(-0xf00 + r0_offs)]
+  emit_op   (0xe50de000 + 0xf00 - 4 * 14);                       // str lr, [sp, #(-0xf00 + lr_offs)]
   emit_op   (0xe24f0000 + (g_code_ptr - (u32 *)op_ctx + 2) * 4); // sub r0, pc, #op_ctx
-  emit_op   (make_jmp(g_code_ptr, &g_linkpage->code[0], 1));     // bl common_code
-  emit_op_io(0xe51fe000, &g_linkpage->saved_regs[14]);           // ldr r14, [->saved_regs[14]]
+  emit_op   (0xe1a0e00f);                                        // mov lr, pc
+  emit_op_io(0xe51ff000, (u32 *)&g_linkpage->handler);           // ldr pc, =handle_op
+  emit_op   (0xe51de000 + 0xf00 - 4 * 14);                       // ldr lr, [sp, #(-0xf00 + lr_offs)]
   emit_op   (make_jmp(g_code_ptr, pc + 1, 0));                   // jmp <back>
 
   // sync caches
@@ -785,7 +773,7 @@ static void segv_sigaction(int num, siginfo_t *info, void *ctx)
 
 void emu_init(void *map_bottom)
 {
-  struct sigaction segv_action = {
+  sigaction_t segv_action = {
     .sa_sigaction = segv_sigaction,
     .sa_flags = SA_SIGINFO,
   };
@@ -793,13 +781,9 @@ void emu_init(void *map_bottom)
   void *pret;
   int ret;
 
-  g_handler_stack_end = (void *)((long)alloca(1536 * 1024) & ~0xffff);
-  log("handler stack @ %p (current %p)\n", g_handler_stack_end, &ret);
-  // touch it now. If we crash now we'll know why
-  *((char *)g_handler_stack_end - 4096) = 1;
-
   g_linkpage = (void *)(((u32)map_bottom - LINKPAGE_ALLOC) & ~0xfff);
-  pret = mmap(g_linkpage, LINKPAGE_ALLOC, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+  pret = mmap(g_linkpage, LINKPAGE_ALLOC, PROT_READ|PROT_WRITE,
+              MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
   if (pret != g_linkpage) {
     perror(PFX "mmap linkpage");
     exit(1);
@@ -808,12 +792,30 @@ void emu_init(void *map_bottom)
   init_linkpage();
 
   // host stuff
-  ret = host_video_init(&host_stride, 0);
+  ret = host_init();
+  if (ret != 0) {
+    err("can't init host\n");
+    exit(1);
+  }
+
+  ret = host_video_init(NULL, 0);
   if (ret != 0) {
-    err("can't alloc screen\n");
+    err("can't init host video\n");
+    exit(1);
+  }
+
+#ifdef WIZ
+  // we are short on memmory on Wiz, need special handling
+  extern void *host_mmap_upper(void);
+  mmsp2.umem = host_mmap_upper();
+#else
+  mmsp2.umem = mmap(NULL, 0x2000000, PROT_READ|PROT_WRITE|PROT_EXEC,
+                    MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+#endif
+  if (mmsp2.umem == MAP_FAILED) {
+    perror(PFX "mmap upper mem");
     exit(1);
   }
-  host_screen = host_video_flip();
 
   ret = pthread_create(&tid, NULL, fb_sync_thread, NULL);
   if (ret != 0) {
@@ -824,6 +826,7 @@ void emu_init(void *map_bottom)
 
   // mmsp2 defaults
   mmsp2.mlc_stl_adr = 0x03101000; // fb2 is at 0x03381000
+  mmsp2.mlc_stl_cntl = 0x4ab; // 16bpp, region 1 active
 
   sigemptyset(&segv_action.sa_mask);
   sigaction(SIGSEGV, &segv_action, NULL);
@@ -831,23 +834,26 @@ void emu_init(void *map_bottom)
 
 int emu_read_gpiodev(void *buf, int count)
 {
-  unsigned int btns;
-
-  if (count < 4) {
+  if (count <= 0) {
     err("gpiodev read %d?\n", count);
     return -1;
   }
+  if (count > 4)
+    count = 4;
 
-  btns = host_read_btns();
-  memcpy(buf, &btns, 4);
-  return 4;
+  mmsp2.btn_state = host_read_btns();
+  memcpy(buf, &mmsp2.btn_state, count);
+  return count;
 }
 
-void *emu_mmap_dev(unsigned int length, int prot, int flags, unsigned int offset)
+struct dev_fd_t emu_interesting_fds[] = {
+  [IFD_SOUND] = { "/dev/dsp", -1 },
+  { NULL, 0 },
+};
+
+static void *emu_mmap_dev(unsigned int length, int prot, int flags, unsigned int offset)
 {
-  struct uppermem_block *umem;
-  char name[32];
-  int fd;
+  u8 *umem, *umem_end;
 
   // SoC regs
   if ((offset & ~0xffff) == 0xc0000000) {
@@ -860,40 +866,208 @@ void *emu_mmap_dev(unsigned int length, int prot, int flags, unsigned int offset
       MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_NORESERVE, -1, 0);
   }
   // upper mem
-  if ((offset & 0xfe000000) != 0x02000000)
+  if ((offset & 0xfe000000) != 0x02000000) {
     err("unexpected devmem mmap @ %08x\n", offset);
-
-  umem = calloc(1, sizeof(*umem));
-  if (umem == NULL) {
-    err("OOM\n");
+    errno = EINVAL;
     return MAP_FAILED;
   }
 
-  umem->addr = offset;
-  umem->size = length;
-  umem->mem = mmap(NULL, length, prot, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
-  if (umem->mem != MAP_FAILED)
-    goto done;
-
-  log("upper mem @ %08x %d mmap fail, trying backing file\n", offset, length);
-  sprintf(name, "m%08x", offset);
-  fd = open(name, O_CREAT|O_RDWR, 0644);
-  lseek(fd, length - 1, SEEK_SET);
-  name[0] = 0;
-  write(fd, name, 1);
-
-  umem->mem = mmap(NULL, length, prot, MAP_SHARED, fd, 0);
-  if (umem->mem == MAP_FAILED) {
-    err("failed, giving up\n");
-    close(fd);
-    free(umem);
-    return MAP_FAILED;
+  umem = uppermem_lookup(offset, &umem_end);
+  if (umem + length > umem_end)
+    err("warning: uppermem @ %08x overflows by %d bytes\n",
+        offset, umem + length - umem_end);
+
+  dbg("upper mem @ %08x %d\n", offset, length);
+  return umem;
+}
+
+void *emu_do_mmap(unsigned int length, int prot, int flags, int fd, unsigned int offset)
+{
+  if (fd == FAKEDEV_MEM)
+    return emu_mmap_dev(length, prot, flags, offset);
+
+  if (fd == FAKEDEV_FB0)
+    return emu_mmap_dev(length, prot, flags, offset + 0x03101000);
+
+  if (fd == FAKEDEV_FB1)
+    return emu_mmap_dev(length, prot, flags, offset + 0x03381000);
+
+  err("bad/ni mmap(?, %d, %x, %x, %d, %08x)\n", length, prot, flags, fd, offset);
+  errno = EINVAL;
+  return MAP_FAILED;
+}
+
+static int emu_sound_ioctl(int fd, int request, void *argp)
+{
+  int *arg = argp;
+
+#if 0
+  dbg("snd ioctl(%d, %08x, %p)", fd, request, argp);
+  if (arg != NULL)
+    dbg_c(" [%d]", *arg);
+  dbg_c("\n");
+#endif
+
+  /* People set strange frag settings on GP2X, which even manage
+   * to break audio on pandora (causes writes to fail).
+   * Catch this and set to something that works. */
+  if (request == SNDCTL_DSP_SPEED) {
+    int ret, bsize, frag;
+
+    // ~4ms. gpSP wants small buffers or else it stutters
+    // because of it's audio thread sync stuff
+    bsize = *arg / 250 * 4;
+    for (frag = 0; bsize; bsize >>= 1, frag++)
+      ;
+
+    frag |= 16 << 16;       // fragment count
+    ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
+    if (ret != 0) {
+      err("snd ioctl SETFRAGMENT %08x: ", frag);
+      perror(NULL);
+    }
+  }
+  else if (request == SNDCTL_DSP_SETFRAGMENT)
+    return 0;
+
+  return ioctl(fd, request, argp);
+}
+
+int emu_do_ioctl(int fd, int request, void *argp)
+{
+  if (fd == emu_interesting_fds[IFD_SOUND].fd)
+    return emu_sound_ioctl(fd, request, argp);
+
+  switch (fd) {
+  /* *********************** */
+  case FAKEDEV_FB0:
+  case FAKEDEV_FB1:
+    if (argp == NULL)
+      goto fail;
+
+    switch (request) {
+      case FBIOGET_FSCREENINFO: {
+        struct fb_fix_screeninfo *fix = argp;
+
+        memset(fix, 0, sizeof(*fix));
+        strcpy(fix->id, "mmsp2_RGB0");
+        fix->type         = FB_TYPE_PACKED_PIXELS;
+        fix->accel        = FB_ACCEL_NONE;
+        fix->visual       = FB_VISUAL_TRUECOLOR;
+        fix->line_length  = 320*2;
+        fix->smem_start   = (fd == FAKEDEV_FB0) ? 0x03101000 : 0x03381000;
+        fix->smem_len     = 320*240*2;
+        return 0;
+      }
+      case FBIOGET_VSCREENINFO: {
+        struct fb_var_screeninfo *var = argp;
+        static const struct fb_bitfield fbb_red   = { offset: 11, length: 5, };
+        static const struct fb_bitfield fbb_green = { offset:  5, length: 6, };
+        static const struct fb_bitfield fbb_blue  = { offset:  0, length: 5, };
+
+        memset(var, 0, sizeof(*var));
+        var->activate     = FB_ACTIVATE_NOW;
+        var->xres         =
+        var->xres_virtual = 320;
+        var->yres         =
+        var->yres_virtual = 240;
+        var->width        =
+        var->height       = -1;
+        var->vmode        = FB_VMODE_NONINTERLACED;
+        var->bits_per_pixel = 16;
+        var->red          = fbb_red;
+        var->green        = fbb_green;
+        var->blue         = fbb_blue;
+        return 0;
+      }
+      case FBIOPUT_VSCREENINFO: {
+        struct fb_var_screeninfo *var = argp;
+        dbg(" put vscreen: %dx%d@%d\n", var->xres, var->yres, var->bits_per_pixel);
+        if (var->xres != 320 || var->yres != 240 || var->bits_per_pixel != 16)
+          return -1;
+        return 0;
+      }
+    }
+
+  /* *********************** */
+  case FAKEDEV_TTY0:
+    // fake tty0 to make GPH SDL happy
+    if (request == 0x4b46) // KDGKBENT
+      return -1;
+    return 0;
+  }
+
+fail:
+  err("bad/ni ioctl(%d, %08x, %p)\n", fd, request, argp);
+  errno = EINVAL;
+  return -1;
+}
+
+static const struct {
+  const char *from;
+  const char *to;
+} path_map[] = {
+  { "/mnt/tmp/", "/tmp/" },
+};
+
+static const char *wrap_path(const char *path)
+{
+  char *buff;
+  size_t size;
+  int i, len;
+
+  // do only path mapping for now
+  for (i = 0; i < ARRAY_SIZE(path_map); i++) {
+    len = strlen(path_map[i].from);
+    if (strncmp(path, path_map[i].from, len) == 0) {
+      size = strlen(path) + strlen(path_map[i].to) + 1;
+      buff = malloc(size);
+      if (buff == NULL)
+        break;
+      snprintf(buff, size, "%s%s", path_map[i].to, path + len);
+      dbg("mapped path \"%s\" -> \"%s\"\n", path, buff);
+      return buff;
+    }
   }
 
-done:
-  log("upper mem @ %08x %d\n", offset, length);
-  umem->next = upper_mem;
-  upper_mem = umem;
-  return umem->mem;
+  return path;
+}
+
+void *emu_do_fopen(const char *path, const char *mode)
+{
+  const char *w_path = wrap_path(path);
+  FILE *ret;
+  ret = fopen(w_path, mode);
+  if (w_path != path)
+    free((void *)w_path);
+  return ret;
+}
+
+// FIXME: threads..
+int emu_do_system(const char *command)
+{
+  static char tmp_path[512];
+  const char *p2;
+  char *p;
+
+  if (command == NULL)
+    return -1;
+
+  // pass through stuff in PATH
+  p = strchr(command, ' ');
+  p2 = strchr(command, '/');
+  if (p2 == NULL || (p != NULL && p2 > p))
+    return system(command);
+
+  make_local_path(tmp_path, sizeof(tmp_path), "ginge_prep");
+  p = tmp_path + strlen(tmp_path);
+
+  p2 = wrap_path(command);
+  snprintf(p, sizeof(tmp_path) - (p - tmp_path), " %s", p2);
+  if (p2 != command)
+    free((void *)p2);
+
+  dbg("system: \"%s\"\n", tmp_path);
+  return system(tmp_path);
 }