+ if (src != NULL) {
+ // copy
+ 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 * bpp / 8);
+ }
+ }
+ else {
+ // fill. Assume the pattern is cleared and bg color is used
+ u32 bgc = blitter.patbackcolor & 0xffff;
+ 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) {
+ fb_sync_thread_futex = 1;
+ g_futex_raw(&fb_sync_thread_futex, FUTEX_WAKE, 1, NULL);
+ }
+ return;
+
+bad_blit:
+ err("blit %08x->%08x %dx%d translated to %p->%p\n",
+ blitter.srcaddr, blitter.dstaddr, w, h, src, dst);
+ dump_blitter();
+}
+
+// FIXME: pass real dimensions to blitters
+static void mlc_flip(void *src, int bpp, int stride)
+{
+ static int old_bpp;
+
+ // only pass pal to host if it's dirty
+ if (bpp <= 8 && mmsp2.v.dirty_pal) {
+ if (mmsp2.v.dirty_pal == DIRTY_PAL_MMSP2)
+ host_video_update_pal32(mmsp2.mlc_stl_pallt_d32);
+ else
+ host_video_update_pal16(mmsp2.mlcpalette);
+ mmsp2.v.dirty_pal = 0;
+ }
+
+ if (bpp != old_bpp) {
+ host_video_change_bpp(bpp);
+ old_bpp = bpp;
+ }
+
+ switch (bpp) {
+ case 4:
+ host_video_blit4(src, 320, 240, stride);
+ break;
+
+ case 8:
+ host_video_blit8(src, 320, 240, stride);
+ break;
+
+ case 16:
+ host_video_blit16(src, 320, 240, stride);
+ break;
+
+ case 24:
+ // TODO
+ break;
+ }
+}
+
+static void *fb_sync_thread(void *arg)
+{
+ unsigned long sigmask[2] = { ~0ul, ~0ul };
+ struct timespec ts = { 0, 0 };
+ int invalid_fb_addr = 1;
+ int manual_refresh = 0;
+ int frame_counter = 0;
+ int wait_ret;
+
+ // this thread can't run any signal handlers since the
+ // app's stack/tls stuff will never be set up here
+ sigmask[0] &= ~(1ul << (SIGSEGV - 1));
+ g_rt_sigprocmask_raw(SIG_SETMASK, sigmask, NULL, sizeof(sigmask));
+
+ //ret = setpriority(PRIO_PROCESS, 0, -1);
+ //log("setpriority %d\n", ret);
+
+ // tell the main thread we're done init
+ fb_sync_thread_futex = 0;
+ g_futex_raw(&fb_sync_thread_futex, FUTEX_WAKE, 1, NULL);
+
+ while (1) {
+ u8 *gp2x_fb, *gp2x_fb_end;
+
+ wait_ret = g_futex_raw(&fb_sync_thread_futex, FUTEX_WAIT, 0, &ts);
+
+ // this is supposed to be done atomically, but to make life
+ // easier ignore it for now, race impact is low anyway
+ fb_sync_thread_futex = 0;
+
+ if (wait_ret != 0 && wait_ret != -EWOULDBLOCK
+ && wait_ret != -ETIMEDOUT)
+ {
+ err("fb_thread: futex error: %d\n", wait_ret);
+ sleep(1);
+ goto check_keys;
+ }
+ if (fb_sync_thread_paused) {
+ ts.tv_nsec = 100000000;
+ goto check_keys;
+ }
+
+ if (wait_ret == 0) {
+ ts.tv_nsec = 50000000;
+ manual_refresh++;
+ if (manual_refresh == 2)
+ dbg("fb_thread: switch to manual refresh\n");
+ } else {
+ ts.tv_nsec = 16666667;
+ if (manual_refresh > 1)
+ dbg("fb_thread: switch to auto refresh\n");
+ manual_refresh = 0;
+ }
+
+ gp2x_fb = uppermem_lookup(mmsp2.mlc_stl_adr, &gp2x_fb_end);
+ if (gp2x_fb == NULL || gp2x_fb + 320*240 * mmsp2.v.bpp / 8 > gp2x_fb_end) {
+ if (!invalid_fb_addr) {
+ err("fb_thread: %08x is out of range\n", mmsp2.mlc_stl_adr);
+ invalid_fb_addr = 1;
+ }
+ continue;
+ }
+
+ invalid_fb_addr = 0;
+ mlc_flip(gp2x_fb, mmsp2.v.bpp, mmsp2.v.stride);
+
+ frame_counter++;
+ if (frame_counter & 0x0f)
+ continue;
+
+check_keys:
+ // this is to check for kill key, in case main thread hung
+ // or something else went wrong.
+ pollux.btn_state = host_read_btns();
+ }
+}
+
+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 xread32_io_cmn(u32 a, u32 *handled)
+{
+ u32 d = 0;
+
+ *handled = 1;
+ switch (a) {
+ // Wiz stuff
+ case 0x402c: // MLCVSTRIDE0
+ case 0x4060: // MLCVSTRIDE1
+ d = pollux.v.stride;
+ break;
+ case 0x4038: // MLCADDRESS0
+ case 0x406c: // MLCADDRESS1
+ d = pollux.mlc_stl_adr;
+ break;
+ // wiz_lib reads:
+ // ???? ???? YXBA DURiLe ???? VdVuMS LR?? ????
+ // | GPIOC[31:16] | GPIOB[31:16] |
+ case 0xa058: // GPIOBPAD
+ d = (pollux.btn_state >> 1) & 0x0100;
+ d |= (pollux.btn_state << 1) & 0x0200;
+ d |= (pollux.btn_state >> 3) & 0x0080;
+ d |= (pollux.btn_state >> 5) & 0x0040;
+ d |= (pollux.btn_state >> 6) & 0x0c00;
+ d <<= 16;
+ d = ~d;
+ break;
+ case 0xa098: // GPIOCPAD
+ pollux.btn_state = host_read_btns();
+ d = (pollux.btn_state >> 8) & 0x00f0;
+ d |= (pollux.btn_state >> 1) & 0x0008;
+ d |= (pollux.btn_state << 2) & 0x0004;
+ d |= (pollux.btn_state >> 5) & 0x0002;
+ d |= (pollux.btn_state >> 2) & 0x0001;
+ d <<= 16;
+ d = ~d;
+ break;
+ default:
+ *handled = 0;
+ break;