wiz r1 relese
[ginge.git] / loader / emu.c
index 0d908d7..5c24120 100644 (file)
 #include "sys_cacheflush.h"
 #include "realfuncs.h"
 
-#if (dbg & 2)
+#if (DBG & 2) && !(DBG & 4)
 #define LOG_IO_UNK
 #endif
-#if (dbg & 4)
+#if (DBG & 4)
 #define LOG_IO
 #endif
 //#define LOG_SEGV
@@ -55,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;
@@ -62,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;
@@ -110,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
   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)
@@ -182,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;
-
-  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;
-    }
-  }
+  // XXX: maybe support mirroring?
+  if ((addr & 0xfe000000) != 0x02000000)
+    return NULL;
 
-  return NULL;
+  *mem_end = (u8 *)mmsp2.umem + 0x02000000;
+  return (u8 *)mmsp2.umem - 0x02000000 + addr;
 }
 
 static void blitter_do(void)
@@ -281,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) { \
@@ -341,6 +309,8 @@ static void mlc_flip(u8 *src, int bpp)
   } \
 }
 
+static int fb_sync_thread_paused;
+
 static void *fb_sync_thread(void *arg)
 {
   int invalid_fb_addr = 1;
@@ -377,17 +347,21 @@ static void *fb_sync_thread(void *arg)
       sleep(1);
       continue;
     }
+    if (fb_sync_thread_paused) {
+      ts_add_nsec(ts, 100000000);
+      continue;
+    }
 
     if (wait_ret != ETIMEDOUT) {
       clock_gettime(CLOCK_REALTIME, &ts);
       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;
     }
 
@@ -407,6 +381,19 @@ static void *fb_sync_thread(void *arg)
   }
 }
 
+static void fb_thread_pause(void)
+{
+  fb_sync_thread_paused = 1;
+  // wait until it finishes last refresh
+  // that it might be doing now
+  usleep(10000);
+}
+
+static void fb_thread_resume(void)
+{
+  fb_sync_thread_paused = 0;
+}
+
 static u32 xread8(u32 a)
 {
   iolog("r8 ", a, 0, 8);
@@ -495,9 +482,16 @@ 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
-      // TODO
+      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;
     }
   }
@@ -587,25 +581,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;
@@ -732,30 +726,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)
@@ -773,7 +745,7 @@ 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]);
+      dbg(" r%d=%08x r%2d=%08x\n", i, regs[i], i+8, regs[i+8]);
     signal(num, SIG_DFL);
     raise(num);
     return;
@@ -790,12 +762,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
@@ -828,14 +800,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);
+              MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
   if (pret != g_linkpage) {
     perror(PFX "mmap linkpage");
     exit(1);
@@ -844,12 +811,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 video\n");
+    err("can't init host\n");
+    exit(1);
+  }
+
+  ret = host_video_init(NULL, 0);
+  if (ret != 0) {
+    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) {
@@ -868,16 +853,16 @@ 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;
 }
 
 struct dev_fd_t emu_interesting_fds[] = {
@@ -887,9 +872,7 @@ struct dev_fd_t emu_interesting_fds[] = {
 
 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) {
@@ -902,42 +885,19 @@ static void *emu_mmap_dev(unsigned int length, int prot, int flags, unsigned int
       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");
-    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);
     errno = EINVAL;
     return MAP_FAILED;
   }
 
-done:
-  log("upper mem @ %08x %d\n", offset, length);
-  umem->next = upper_mem;
-  upper_mem = umem;
-  return umem->mem;
+  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)
@@ -1069,19 +1029,23 @@ static const struct {
   { "/mnt/tmp/", "/tmp/" },
 };
 
-// FIXME: threads..
 static const char *wrap_path(const char *path)
 {
-  static char tmp_path[512];
+  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) {
-      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;
+      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;
     }
   }
 
@@ -1090,13 +1054,21 @@ static const char *wrap_path(const char *path)
 
 void *emu_do_fopen(const char *path, const char *mode)
 {
-  return fopen(wrap_path(path), 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];
-  char *p, *p2;
+  const char *p2;
+  char *p;
+  int ret;
 
   if (command == NULL)
     return -1;
@@ -1110,8 +1082,17 @@ int emu_do_system(const char *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));
+  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);
+
+  // the app might want the screen too..
+  fb_thread_pause();
+  ret = system(tmp_path);
+  fb_thread_resume();
+  return ret;
 }