frontend: fix screenshot functionality for pollux
[pcsx_rearmed.git] / frontend / plat_pollux.c
index d710e8f..fd9c325 100644 (file)
 #include "warm/warm.h"
 #include "plugin_lib.h"
 #include "cspace.h"
 #include "warm/warm.h"
 #include "plugin_lib.h"
 #include "cspace.h"
+#include "blit320.h"
+#include "in_tsbutton.h"
 #include "main.h"
 #include "menu.h"
 #include "plat.h"
 #include "main.h"
 #include "menu.h"
 #include "plat.h"
+#include "pcnt.h"
 
 static int fbdev = -1, memdev = -1, battdev = -1;
 static volatile unsigned short *memregs;
 
 static int fbdev = -1, memdev = -1, battdev = -1;
 static volatile unsigned short *memregs;
@@ -31,12 +34,15 @@ static volatile unsigned int   *memregl;
 static void *fb_vaddrs[2];
 static unsigned int fb_paddrs[2];
 static int fb_work_buf;
 static void *fb_vaddrs[2];
 static unsigned int fb_paddrs[2];
 static int fb_work_buf;
-static int cpu_clock_allowed;
+static int cpu_clock_allowed, have_warm;
+static unsigned int saved_video_regs[2][6];
+#define FB_VRAM_SIZE (320*240*2*2*2) // 2 buffers with space for 24bpp mode
 
 static unsigned short *psx_vram;
 static unsigned int psx_vram_padds[512];
 
 static unsigned short *psx_vram;
 static unsigned int psx_vram_padds[512];
-static int psx_offset, psx_step, psx_width, psx_height, psx_bpp;
-static int fb_offset;
+static int psx_step, psx_width, psx_height, psx_bpp;
+static int psx_offset_x, psx_offset_y;
+static int fb_offset_x, fb_offset_y;
 
 // TODO: get rid of this
 struct vout_fbdev;
 
 // TODO: get rid of this
 struct vout_fbdev;
@@ -50,8 +56,9 @@ int omap_enable_layer(int enabled)
 
 static void *fb_flip(void)
 {
 
 static void *fb_flip(void)
 {
-       memregl[0x406C>>2] = fb_paddrs[fb_work_buf];
+       memregl[0x406C>>2] = memregl[0x446C>>2] = fb_paddrs[fb_work_buf];
        memregl[0x4058>>2] |= 0x10;
        memregl[0x4058>>2] |= 0x10;
+       memregl[0x4458>>2] |= 0x10;
        fb_work_buf ^= 1;
        return fb_vaddrs[fb_work_buf];
 }
        fb_work_buf ^= 1;
        return fb_vaddrs[fb_work_buf];
 }
@@ -85,12 +92,17 @@ static void pollux_changemode(int bpp, int is_bgr)
                        return;
        }
 
                        return;
        }
 
-       memregl[0x405c>>2] = bytes;
-       memregl[0x4060>>2] = 320 * bytes;
+       // program both MLCs so that TV-out works
+       memregl[0x405c>>2] = memregl[0x445c>>2] = bytes;
+       memregl[0x4060>>2] = memregl[0x4460>>2] = 320 * bytes;
 
        r = memregl[0x4058>>2];
        r = (r & 0xffff) | (code << 16) | 0x10;
        memregl[0x4058>>2] = r;
 
        r = memregl[0x4058>>2];
        r = (r & 0xffff) | (code << 16) | 0x10;
        memregl[0x4058>>2] = r;
+
+       r = memregl[0x4458>>2];
+       r = (r & 0xffff) | (code << 16) | 0x10;
+       memregl[0x4458>>2] = r;
 }
 
 /* note: both PLLs are programmed the same way,
 }
 
 /* note: both PLLs are programmed the same way,
@@ -217,7 +229,6 @@ void plat_video_menu_end(void)
 
 void plat_video_menu_leave(void)
 {
 
 void plat_video_menu_leave(void)
 {
-       pollux_changemode(psx_bpp, 1);
        if (psx_vram == NULL) {
                fprintf(stderr, "GPU plugin did not provide vram\n");
                exit(1);
        if (psx_vram == NULL) {
                fprintf(stderr, "GPU plugin did not provide vram\n");
                exit(1);
@@ -225,6 +236,21 @@ void plat_video_menu_leave(void)
 
        in_set_config_int(in_name_to_id("evdev:pollux-analog"),
                        IN_CFG_ABS_DEAD_ZONE, analog_deadzone);
 
        in_set_config_int(in_name_to_id("evdev:pollux-analog"),
                        IN_CFG_ABS_DEAD_ZONE, analog_deadzone);
+
+       memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
+       g_menuscreen_ptr = fb_flip();
+       memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
+
+       pollux_changemode(psx_bpp, 1);
+}
+
+void *plat_prepare_screenshot(int *w, int *h, int *bpp)
+{
+       bgr555_to_rgb565(pl_vout_buf, pl_vout_buf, 320*240*2);
+       *w = 320;
+       *h = 240;
+       *bpp = psx_bpp;
+       return pl_vout_buf;
 }
 
 static void pl_vout_set_raw_vram(void *vram)
 }
 
 static void pl_vout_set_raw_vram(void *vram)
@@ -245,46 +271,6 @@ static void pl_vout_set_raw_vram(void *vram)
        }
 }
 
        }
 }
 
-static void *pl_vout_set_mode(int w, int h, int bpp)
-{
-       static int old_w, old_h, old_bpp;
-       int fboff_w, fboff_h;
-       int poff_w, poff_h;
-
-       if (!w || !h || !bpp || (w == old_w && h == old_h && bpp == old_bpp))
-               return NULL;
-
-       printf("psx mode: %dx%d@%d\n", w, h, bpp);
-
-       psx_step = 1;
-       if (h > 256) {
-               psx_step = 2;
-               h /= 2;
-       }
-
-       poff_w = poff_h = 0;
-       if (w > 320) {
-               poff_w = w / 2 - 320/2;
-               w = 320;
-       }
-       if (h > 240) {
-               poff_h = h / 2 - 240/2;
-               h = 240;
-       }
-       fboff_w = 320/2 - w / 2;
-       fboff_h = 240/2 - h / 2;
-
-       psx_offset = poff_h * 1024 + poff_w;
-       psx_width = w;
-       psx_height = h;
-       psx_bpp = bpp;
-       fb_offset = fboff_h * 320 + fboff_w;
-
-       pollux_changemode(bpp, 1);
-
-       return NULL;
-}
-
 static void spend_cycles(int loops)
 {
        asm volatile (
 static void spend_cycles(int loops)
 {
        asm volatile (
@@ -300,15 +286,16 @@ static void spend_cycles(int loops)
 /* this takes ~1.5ms, while ldm/stm ~1.95ms */
 static void raw_flip_dma(int x, int y)
 {
 /* this takes ~1.5ms, while ldm/stm ~1.95ms */
 static void raw_flip_dma(int x, int y)
 {
-       unsigned int dst = fb_paddrs[fb_work_buf] + fb_offset * psx_bpp / 8;
-       int spsx_line = y + (psx_offset >> 10);
-       int spsx_offset = (x + psx_offset) & 0x3f8;
+       unsigned int dst = fb_paddrs[fb_work_buf] +
+                       (fb_offset_y * 320 + fb_offset_x) * psx_bpp / 8;
+       int spsx_line = y + psx_offset_y;
+       int spsx_offset = (x + psx_offset_x) & 0x3f8;
        int dst_stride = 320 * psx_bpp / 8;
        int len = psx_width * psx_bpp / 8;
        int dst_stride = 320 * psx_bpp / 8;
        int len = psx_width * psx_bpp / 8;
-       //unsigned int st = timer_get();
        int i;
 
        warm_cache_op_all(WOP_D_CLEAN);
        int i;
 
        warm_cache_op_all(WOP_D_CLEAN);
+       pcnt_start(PCNT_BLIT);
 
        dst &= ~7;
        len &= ~7;
 
        dst &= ~7;
        len &= ~7;
@@ -334,38 +321,140 @@ static void raw_flip_dma(int x, int y)
                DMA_REG(0x1c) = 0x80000;        // go
        }
 
                DMA_REG(0x1c) = 0x80000;        // go
        }
 
-       //printf("d %d\n", timer_get() - st);
-
        if (psx_bpp == 16) {
                pl_vout_buf = g_menuscreen_ptr;
        if (psx_bpp == 16) {
                pl_vout_buf = g_menuscreen_ptr;
-               pl_print_hud(psx_width, psx_height);
+               pl_print_hud(320, fb_offset_y + psx_height, fb_offset_x);
        }
 
        g_menuscreen_ptr = fb_flip();
        pl_flip_cnt++;
        }
 
        g_menuscreen_ptr = fb_flip();
        pl_flip_cnt++;
+
+       pcnt_end(PCNT_BLIT);
 }
 
 }
 
-static void raw_flip_soft(int x, int y)
+#define make_flip_func(name, blitfunc)                                                  \
+static void name(int x, int y)                                                          \
+{                                                                                       \
+        unsigned short *vram = psx_vram;                                                \
+        unsigned char *dst = (unsigned char *)g_menuscreen_ptr +                        \
+                        (fb_offset_y * 320 + fb_offset_x) * psx_bpp / 8;                \
+        unsigned int src = (y + psx_offset_y) * 1024 + x + psx_offset_x;                \
+        int dst_stride = 320 * psx_bpp / 8;                                             \
+        int len = psx_width * psx_bpp / 8;                                              \
+        int i;                                                                          \
+                                                                                        \
+        pcnt_start(PCNT_BLIT);                                                          \
+                                                                                        \
+        for (i = psx_height; i > 0; i--, src += psx_step * 1024, dst += dst_stride) {   \
+                src &= 1024*512-1;                                                      \
+                blitfunc(dst, vram + src, len);                                         \
+        }                                                                               \
+                                                                                        \
+        if (psx_bpp == 16) {                                                            \
+                pl_vout_buf = g_menuscreen_ptr;                                         \
+                pl_print_hud(320, fb_offset_y + psx_height, fb_offset_x);               \
+        }                                                                               \
+                                                                                        \
+        g_menuscreen_ptr = fb_flip();                                                   \
+        pl_flip_cnt++;                                                                  \
+                                                                                        \
+        pcnt_end(PCNT_BLIT);                                                            \
+}
+
+make_flip_func(raw_flip_soft, memcpy)
+make_flip_func(raw_flip_soft_368, blit320_368)
+make_flip_func(raw_flip_soft_512, blit320_512)
+make_flip_func(raw_flip_soft_640, blit320_640)
+
+static void *pl_vout_set_mode(int w, int h, int bpp)
 {
 {
-       unsigned short *src = psx_vram + y * 1024 + x + psx_offset;
-       unsigned char *dst = (unsigned char *)g_menuscreen_ptr + fb_offset * psx_bpp / 8;
-       int dst_stride = 320 * psx_bpp / 8;
-       int len = psx_width * psx_bpp / 8;
-       //unsigned int st = timer_get();
-       int i;
+       static int old_w, old_h, old_bpp;
+       int poff_w, poff_h, w_max;
 
 
-       for (i = psx_height; i > 0; i--, src += psx_step * 1024, dst += dst_stride)
-               memcpy(dst, src, len);
+       if (!w || !h || !bpp || (w == old_w && h == old_h && bpp == old_bpp))
+               return NULL;
 
 
-       //printf("s %d\n", timer_get() - st);
+       printf("psx mode: %dx%d@%d\n", w, h, bpp);
 
 
-       if (psx_bpp == 16) {
-               pl_vout_buf = g_menuscreen_ptr;
-               pl_print_hud(psx_width, psx_height);
+       switch (w + (bpp != 16)) {
+       case 640:
+               pl_rearmed_cbs.pl_vout_raw_flip = raw_flip_soft_640;
+               w_max = 640;
+               break;
+       case 512:
+               pl_rearmed_cbs.pl_vout_raw_flip = raw_flip_soft_512;
+               w_max = 512;
+               break;
+       case 384:
+       case 368:
+               pl_rearmed_cbs.pl_vout_raw_flip = raw_flip_soft_368;
+               w_max = 368;
+               break;
+       default:
+               pl_rearmed_cbs.pl_vout_raw_flip = have_warm ? raw_flip_dma : raw_flip_soft;
+               w_max = 320;
+               break;
        }
 
        }
 
-       g_menuscreen_ptr = fb_flip();
-       pl_flip_cnt++;
+       psx_step = 1;
+       if (h > 256) {
+               psx_step = 2;
+               h /= 2;
+       }
+
+       poff_w = poff_h = 0;
+       if (w > w_max) {
+               poff_w = w / 2 - w_max / 2;
+               w = w_max;
+       }
+       fb_offset_x = 0;
+       if (w < 320)
+               fb_offset_x = 320/2 - w / 2;
+       if (h > 240) {
+               poff_h = h / 2 - 240/2;
+               h = 240;
+       }
+       fb_offset_y = 240/2 - h / 2;
+
+       psx_offset_x = poff_w;
+       psx_offset_y = poff_h;
+       psx_width = w;
+       psx_height = h;
+       psx_bpp = bpp;
+
+       if (fb_offset_x || fb_offset_y) {
+               // not fullscreen, must clear borders
+               memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
+               g_menuscreen_ptr = fb_flip();
+               memset(g_menuscreen_ptr, 0, 320*240 * psx_bpp/8);
+       }
+
+       pollux_changemode(bpp, 1);
+
+       return NULL;
+}
+
+static void *pl_vout_flip(void)
+{
+       return NULL;
+}
+
+static void save_multiple_regs(unsigned int *dest, int base, int count)
+{
+       const volatile unsigned int *regs = memregl + base / 4;
+       int i;
+
+       for (i = 0; i < count; i++)
+               dest[i] = regs[i];
+}
+
+static void restore_multiple_regs(int base, const unsigned int *src, int count)
+{
+       volatile unsigned int *regs = memregl + base / 4;
+       int i;
+
+       for (i = 0; i < count; i++)
+               regs[i] = src[i];
 }
 
 void plat_init(void)
 }
 
 void plat_init(void)
@@ -388,6 +477,10 @@ void plat_init(void)
        }
        memregl = (volatile void *)memregs;
 
        }
        memregl = (volatile void *)memregs;
 
+       // save video regs of both MLCs
+       save_multiple_regs(saved_video_regs[0], 0x4058, ARRAY_SIZE(saved_video_regs[0]));
+       save_multiple_regs(saved_video_regs[1], 0x4458, ARRAY_SIZE(saved_video_regs[1]));
+
        fbdev = open(main_fb_name, O_RDWR);
        if (fbdev == -1) {
                fprintf(stderr, "%s: ", main_fb_name);
        fbdev = open(main_fb_name, O_RDWR);
        if (fbdev == -1) {
                fprintf(stderr, "%s: ", main_fb_name);
@@ -404,7 +497,7 @@ void plat_init(void)
        fb_paddrs[0] = fbfix.smem_start;
        fb_paddrs[1] = fb_paddrs[0] + 320*240*4; // leave space for 24bpp
 
        fb_paddrs[0] = fbfix.smem_start;
        fb_paddrs[1] = fb_paddrs[0] + 320*240*4; // leave space for 24bpp
 
-       fb_vaddrs[0] = mmap(0, 320*240*2*4, PROT_READ|PROT_WRITE,
+       fb_vaddrs[0] = mmap(0, FB_VRAM_SIZE, PROT_READ|PROT_WRITE,
                                MAP_SHARED, memdev, fb_paddrs[0]);
        if (fb_vaddrs[0] == MAP_FAILED) {
                perror("mmap(fb_vaddrs) failed");
                                MAP_SHARED, memdev, fb_paddrs[0]);
        if (fb_vaddrs[0] == MAP_FAILED) {
                perror("mmap(fb_vaddrs) failed");
@@ -424,6 +517,7 @@ void plat_init(void)
        }
 
        warm_ret = warm_init();
        }
 
        warm_ret = warm_init();
+       have_warm = warm_ret == 0;
        warm_change_cb_upper(WCB_B_BIT, 1);
 
        /* some firmwares have sys clk on PLL0, we can't adjust CPU clock
        warm_change_cb_upper(WCB_B_BIT, 1);
 
        /* some firmwares have sys clk on PLL0, we can't adjust CPU clock
@@ -471,30 +565,33 @@ void plat_init(void)
        if (battdev < 0)
                perror("Warning: could't open pollux_batt");
 
        if (battdev < 0)
                perror("Warning: could't open pollux_batt");
 
-       // hmh
-       plat_rescan_inputs();
-
-       pl_rearmed_cbs.pl_vout_raw_flip = warm_ret == 0 ? raw_flip_dma : raw_flip_soft;
+       pl_rearmed_cbs.pl_vout_flip = pl_vout_flip;
+       pl_rearmed_cbs.pl_vout_raw_flip = have_warm ? raw_flip_dma : raw_flip_soft;
        pl_rearmed_cbs.pl_vout_set_mode = pl_vout_set_mode;
        pl_rearmed_cbs.pl_vout_set_raw_vram = pl_vout_set_raw_vram;
 
        psx_width = 320;
        psx_height = 240;
        psx_bpp = 16;
        pl_rearmed_cbs.pl_vout_set_mode = pl_vout_set_mode;
        pl_rearmed_cbs.pl_vout_set_raw_vram = pl_vout_set_raw_vram;
 
        psx_width = 320;
        psx_height = 240;
        psx_bpp = 16;
+
+       in_tsbutton_init();
 }
 
 void plat_finish(void)
 {
        warm_finish();
        timer_cleanup();
 }
 
 void plat_finish(void)
 {
        warm_finish();
        timer_cleanup();
-       pollux_changemode(16, 0);
-       fb_work_buf = 0;
-       fb_flip();
+
+       memset(fb_vaddrs[0], 0, FB_VRAM_SIZE);
+       restore_multiple_regs(0x4058, saved_video_regs[0], ARRAY_SIZE(saved_video_regs[0]));
+       restore_multiple_regs(0x4458, saved_video_regs[1], ARRAY_SIZE(saved_video_regs[1]));
+       memregl[0x4058>>2] |= 0x10;
+       memregl[0x4458>>2] |= 0x10;
+       munmap(fb_vaddrs[0], FB_VRAM_SIZE);
+       close(fbdev);
 
        if (battdev >= 0)
                close(battdev);
 
        if (battdev >= 0)
                close(battdev);
-       munmap(fb_vaddrs[0], 320*240*2*2);
-       close(fbdev);
        munmap((void *)memregs, 0x20000);
        close(memdev);
 }
        munmap((void *)memregs, 0x20000);
        close(memdev);
 }