wip, some dynamic stuff works
[ginge.git] / loader / emu.c
index 389103e..0d908d7 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)
+#define LOG_IO_UNK
+#endif
+#if (dbg & 4)
+#define LOG_IO
+#endif
 //#define LOG_SEGV
 
 #ifdef LOG_IO
@@ -103,7 +113,7 @@ static struct {
   u16 host_pal[256];
   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;
@@ -189,7 +199,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 +209,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 +237,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 +248,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)
@@ -342,6 +366,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);
@@ -468,6 +493,14 @@ out:
 static u32 xread32(u32 a)
 {
   u32 d = 0;
+  if ((a & 0xfff00000) == 0x7f000000) {
+    u32 a_ = a & 0xffff;
+    switch (a_) {
+    case 0x0a00: // TCOUNT, 1/7372800s
+      // TODO
+      break;
+    }
+  }
   if ((a & 0xfff00000) == 0x7f100000) {
     u32 *bl = &blitter.dstctrl;
     u32 a_ = a & 0xfff;
@@ -731,7 +764,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 +772,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++)
+      err(" r%d=%08x r%2d=%08x\n", i, regs[i], i+8, regs[i+8]);
     signal(num, SIG_DFL);
     raise(num);
     return;
@@ -785,7 +820,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,
   };
@@ -799,7 +834,8 @@ void emu_init(void *map_bottom)
   *((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, -1, 0);
   if (pret != g_linkpage) {
     perror(PFX "mmap linkpage");
     exit(1);
@@ -810,7 +846,7 @@ void emu_init(void *map_bottom)
   // host stuff
   ret = host_video_init(&host_stride, 0);
   if (ret != 0) {
-    err("can't alloc screen\n");
+    err("can't init video\n");
     exit(1);
   }
   host_screen = host_video_flip();
@@ -824,6 +860,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);
@@ -843,7 +880,12 @@ int emu_read_gpiodev(void *buf, int count)
   return 4;
 }
 
-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];
@@ -887,6 +929,7 @@ void *emu_mmap_dev(unsigned int length, int prot, int flags, unsigned int offset
     err("failed, giving up\n");
     close(fd);
     free(umem);
+    errno = EINVAL;
     return MAP_FAILED;
   }
 
@@ -897,3 +940,178 @@ done:
   return umem->mem;
 }
 
+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/" },
+};
+
+// FIXME: threads..
+static const char *wrap_path(const char *path)
+{
+  static char tmp_path[512];
+  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) {
+      snprintf(tmp_path, sizeof(tmp_path), "%s%s", path_map[i].to, path + len);
+      dbg("mapped path \"%s\" -> \"%s\"\n", path, tmp_path);
+      return tmp_path;
+    }
+  }
+
+  return path;
+}
+
+void *emu_do_fopen(const char *path, const char *mode)
+{
+  return fopen(wrap_path(path), mode);
+}
+
+int emu_do_system(const char *command)
+{
+  static char tmp_path[512];
+  char *p, *p2;
+
+  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);
+
+  snprintf(p, sizeof(tmp_path) - (p - tmp_path), " %s", wrap_path(command));
+  dbg("system: \"%s\"\n", tmp_path);
+  return system(tmp_path);
+}
+