gpu-gles: refactor for caanoo/wiz support
[pcsx_rearmed.git] / frontend / plat_pollux.c
index d710e8f..af12f1e 100644 (file)
 #include <unistd.h>
 #include <linux/fb.h>
 #include <sys/mman.h>
+#include <linux/soundcard.h>
 
 #include "common/input.h"
+#include "gp2x/in_gp2x.h"
 #include "common/menu.h"
 #include "warm/warm.h"
 #include "plugin_lib.h"
-#include "cspace.h"
+#include "pl_gun_ts.h"
+#include "blit320.h"
+#include "in_tsbutton.h"
 #include "main.h"
 #include "menu.h"
 #include "plat.h"
+#include "pcnt.h"
+#include "../plugins/gpulib/cspace.h"
 
-static int fbdev = -1, memdev = -1, battdev = -1;
+int gp2x_dev_id;
+
+static int fbdev = -1, memdev = -1, battdev = -1, mixerdev = -1;
 static volatile unsigned short *memregs;
 static volatile unsigned int   *memregl;
 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 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;
@@ -48,10 +59,14 @@ int omap_enable_layer(int enabled)
        return 0;
 }
 
+static void caanoo_init(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[0x4458>>2] |= 0x10;
        fb_work_buf ^= 1;
        return fb_vaddrs[fb_work_buf];
 }
@@ -85,12 +100,17 @@ static void pollux_changemode(int bpp, int is_bgr)
                        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[0x4458>>2];
+       r = (r & 0xffff) | (code << 16) | 0x10;
+       memregl[0x4458>>2] = r;
 }
 
 /* note: both PLLs are programmed the same way,
@@ -217,14 +237,33 @@ void plat_video_menu_end(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);
        }
 
-       in_set_config_int(in_name_to_id("evdev:pollux-analog"),
+       if (gp2x_dev_id == GP2X_DEV_CAANOO)
+               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;
+}
+
+void plat_minimize(void)
+{
 }
 
 static void pl_vout_set_raw_vram(void *vram)
@@ -245,46 +284,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 (
@@ -300,15 +299,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)
 {
-       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;
-       //unsigned int st = timer_get();
        int i;
 
        warm_cache_op_all(WOP_D_CLEAN);
+       pcnt_start(PCNT_BLIT);
 
        dst &= ~7;
        len &= ~7;
@@ -334,38 +334,142 @@ static void raw_flip_dma(int x, int y)
                DMA_REG(0x1c) = 0x80000;        // go
        }
 
-       //printf("d %d\n", timer_get() - st);
-
        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++;
+       pl_rearmed_cbs.flip_cnt++;
+
+       pcnt_end(PCNT_BLIT);
+}
+
+#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_rearmed_cbs.flip_cnt++;                                                      \
+                                                                                        \
+        pcnt_end(PCNT_BLIT);                                                            \
 }
 
-static void raw_flip_soft(int x, int y)
+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);
+
+       pl_set_gun_rect(fb_offset_x, fb_offset_y, w > 320 ? 320 : w, h);
+
+       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)
@@ -374,6 +478,7 @@ void plat_init(void)
        struct fb_fix_screeninfo fbfix;
        int rate, timer_div, timer_div2;
        int fbdev, ret, warm_ret;
+       FILE *f;
 
        memdev = open("/dev/mem", O_RDWR);
        if (memdev == -1) {
@@ -388,6 +493,10 @@ void plat_init(void)
        }
        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);
@@ -404,7 +513,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_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");
@@ -424,6 +533,7 @@ void plat_init(void)
        }
 
        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
@@ -471,38 +581,61 @@ void plat_init(void)
        if (battdev < 0)
                perror("Warning: could't open pollux_batt");
 
-       // hmh
-       plat_rescan_inputs();
+       f = fopen("/dev/accel", "rb");
+       if (f) {
+               printf("detected Caanoo\n");
+               gp2x_dev_id = GP2X_DEV_CAANOO;
+               fclose(f);
+       }
+       else {
+               printf("detected Wiz\n");
+               gp2x_dev_id = GP2X_DEV_WIZ;
+               in_gp2x_init();
+       }
+
+       in_tsbutton_init();
+       in_probe();
+       if (gp2x_dev_id == GP2X_DEV_CAANOO)
+               caanoo_init();
+
+       mixerdev = open("/dev/mixer", O_RDWR);
+       if (mixerdev == -1)
+               perror("open(/dev/mixer)");
 
-       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.screen_w = 320;
+       pl_rearmed_cbs.screen_h = 240;
 }
 
 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);
-       munmap(fb_vaddrs[0], 320*240*2*2);
-       close(fbdev);
+       if (mixerdev >= 0)
+               close(mixerdev);
        munmap((void *)memregs, 0x20000);
        close(memdev);
 }
 
-void in_update_analogs(void)
-{
-}
-
 /* Caanoo stuff, perhaps move later */
 #include <linux/input.h>
 
@@ -542,10 +675,147 @@ static const char * const caanoo_keys[KEY_MAX + 1] = {
        [BTN_BASE5]     = "Push",
 };
 
-int plat_rescan_inputs(void)
+struct haptic_data {
+       int count;
+       struct {
+               short time, strength;
+       } actions[120];
+};
+
+#define HAPTIC_IOCTL_MAGIC     'I'
+#define HAPTIC_PLAY_PATTERN    _IOW(HAPTIC_IOCTL_MAGIC, 4, struct haptic_data)
+#define HAPTIC_INDIVIDUAL_MODE _IOW(HAPTIC_IOCTL_MAGIC, 5, unsigned int)
+#define HAPTIC_SET_VIB_LEVEL   _IOW(HAPTIC_IOCTL_MAGIC, 9, unsigned int)
+
+static int hapticdev = -1;
+static struct haptic_data haptic_seq;
+
+static int haptic_init(void)
+{
+       int i, ret, v1, v2;
+       char buf[128], *p;
+       FILE *f;
+
+       f = fopen("haptic.txt", "r");
+       if (f == NULL) {
+               perror("fopen(haptic.txt)");
+               return -1;
+       }
+
+       for (i = 0; i < sizeof(haptic_seq.actions) / sizeof(haptic_seq.actions[0]); ) {
+               p = fgets(buf, sizeof(buf), f);
+               if (p == NULL)
+                       break;
+               while (*p != 0 && *p == ' ')
+                       p++;
+               if (*p == 0 || *p == ';' || *p == '#')
+                       continue;
+
+               ret = sscanf(buf, "%d %d", &v1, &v2);
+               if (ret != 2) {
+                       fprintf(stderr, "can't parse: %s", buf);
+                       continue;
+               }
+
+               haptic_seq.actions[i].time = v1;
+               haptic_seq.actions[i].strength = v2;
+               i++;
+       }
+       fclose(f);
+
+       if (i == 0) {
+               fprintf(stderr, "bad haptic.txt\n");
+               return -1;
+       }
+       haptic_seq.count = i;
+
+       hapticdev = open("/dev/isa1200", O_RDWR | O_NONBLOCK);
+       if (hapticdev == -1) {
+               perror("open(/dev/isa1200)");
+               return -1;
+       }
+
+       i = 0;
+       ret  = ioctl(hapticdev, HAPTIC_INDIVIDUAL_MODE, &i);    /* use 2 of them */
+       i = 3;
+       ret |= ioctl(hapticdev, HAPTIC_SET_VIB_LEVEL, &i);      /* max */
+       if (ret != 0) {
+               fprintf(stderr, "haptic ioctls failed\n");
+               close(hapticdev);
+               hapticdev = -1;
+               return -1;
+       }
+
+       return 0;
+}
+
+void plat_trigger_vibrate(void)
+{
+       int ret;
+
+       if (hapticdev == -2)
+               return; // it's broken
+       if (hapticdev < 0) {
+               ret = haptic_init();
+               if (ret < 0) {
+                       hapticdev = -2;
+                       return;
+               }
+       }
+
+       ioctl(hapticdev, HAPTIC_PLAY_PATTERN, &haptic_seq);
+}
+
+/* Wiz stuff */
+struct in_default_bind in_gp2x_defbinds[] =
+{
+       /* MXYZ SACB RLDU */
+       { GP2X_BTN_UP,          IN_BINDTYPE_PLAYER12, DKEY_UP },
+       { GP2X_BTN_DOWN,        IN_BINDTYPE_PLAYER12, DKEY_DOWN },
+       { GP2X_BTN_LEFT,        IN_BINDTYPE_PLAYER12, DKEY_LEFT },
+       { GP2X_BTN_RIGHT,       IN_BINDTYPE_PLAYER12, DKEY_RIGHT },
+       { GP2X_BTN_X,           IN_BINDTYPE_PLAYER12, DKEY_CROSS },
+       { GP2X_BTN_B,           IN_BINDTYPE_PLAYER12, DKEY_CIRCLE },
+       { GP2X_BTN_A,           IN_BINDTYPE_PLAYER12, DKEY_SQUARE },
+       { GP2X_BTN_Y,           IN_BINDTYPE_PLAYER12, DKEY_TRIANGLE },
+       { GP2X_BTN_L,           IN_BINDTYPE_PLAYER12, DKEY_L1 },
+       { GP2X_BTN_R,           IN_BINDTYPE_PLAYER12, DKEY_R1 },
+       { GP2X_BTN_START,       IN_BINDTYPE_PLAYER12, DKEY_START },
+       { GP2X_BTN_SELECT,      IN_BINDTYPE_EMU, SACTION_ENTER_MENU },
+       { GP2X_BTN_VOL_UP,      IN_BINDTYPE_EMU, SACTION_VOLUME_UP },
+       { GP2X_BTN_VOL_DOWN,    IN_BINDTYPE_EMU, SACTION_VOLUME_DOWN },
+       { 0, 0, 0 },
+};
+
+void plat_step_volume(int is_up)
+{
+       static int volume = 50;
+       int ret, val;
+
+       if (mixerdev < 0)
+               return;
+
+       if (is_up) {
+               volume += 5;
+               if (volume > 255) volume = 255;
+       }
+       else {
+               volume -= 5;
+               if (volume < 0) volume = 0;
+       }
+       val = volume;
+       val |= val << 8;
+
+       ret = ioctl(mixerdev, SOUND_MIXER_WRITE_PCM, &val);
+       if (ret == -1)
+               perror("WRITE_PCM");
+}
+
+// unused dummy for in_gp2x
+volatile unsigned short *gp2x_memregs;
+
+static void caanoo_init(void)
 {
-       in_probe();
        in_set_config(in_name_to_id("evdev:pollux-analog"), IN_CFG_KEY_NAMES,
                      caanoo_keys, sizeof(caanoo_keys));
-       return 0;
 }