+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <linux/fb.h>
+
+#include "soc.h"
+#include "plat_gp2x.h"
+#include "../common/emu.h"
+#include "../common/arm_utils.h"
+#include "pollux_set.h"
+
+static volatile unsigned short *memregs;
+static volatile unsigned long *memregl;
+static int memdev = -1;
+
+extern void *gp2x_screens[4];
+
+#define fb_buf_count 4
+static unsigned int fb_paddr[fb_buf_count];
+static int fb_work_buf;
+static int fbdev = -1;
+
+
+/* video stuff */
+static void pollux_video_flip(int buf_count)
+{
+ memregl[0x406C>>2] = fb_paddr[fb_work_buf];
+ memregl[0x4058>>2] |= 0x10;
+ fb_work_buf++;
+ if (fb_work_buf >= buf_count)
+ fb_work_buf = 0;
+ g_screen_ptr = gp2x_screens[fb_work_buf];
+}
+
+static void gp2x_video_flip_(void)
+{
+ pollux_video_flip(fb_buf_count);
+}
+
+/* doulblebuffered flip */
+static void gp2x_video_flip2_(void)
+{
+ pollux_video_flip(2);
+}
+
+static void gp2x_video_changemode_ll_(int bpp)
+{
+ int code = 0, bytes = 2;
+ unsigned int r;
+ switch (bpp)
+ {
+ case 8:
+ code = 0x443a;
+ bytes = 1;
+ break;
+
+ case 15:
+ case 16:
+ code = 0x4432;
+ bytes = 2;
+ break;
+
+ default:
+ printf("unhandled bpp request: %d\n", bpp);
+ return;
+ }
+
+ memregl[0x405c>>2] = bytes;
+ memregl[0x4060>>2] = bytes * 320;
+
+ r = memregl[0x4058>>2];
+ r = (r & 0xffff) | (code << 16) | 0x10;
+ memregl[0x4058>>2] = r;
+}
+
+static void gp2x_video_setpalette_(int *pal, int len)
+{
+ /* pollux palette is 16bpp only.. */
+ int i;
+ for (i = 0; i < len; i++)
+ {
+ int c = pal[i];
+ c = ((c >> 8) & 0xf800) | ((c >> 5) & 0x07c0) | ((c >> 3) & 0x001f);
+ memregl[0x4070>>2] = (i << 24) | c;
+ }
+}
+
+static void gp2x_video_RGB_setscaling_(int ln_offs, int W, int H)
+{
+ /* maybe a job for 3d hardware? */
+}
+
+static void gp2x_video_wait_vsync_(void)
+{
+ while (!(memregl[0x308c>>2] & (1 << 10)));
+ spend_cycles(128);
+ memregl[0x308c>>2] |= 1 << 10;
+}
+
+/* CPU clock */
+static void gp2x_set_cpuclk_(unsigned int mhz)
+{
+ char buff[24];
+ snprintf(buff, sizeof(buff), "cpuclk=%u", mhz);
+ pollux_set(memregs, buff);
+}
+
+/* misc */
+static void pollux_set_fromenv(const char *env_var)
+{
+ const char *set_string;
+ set_string = getenv(env_var);
+ if (set_string)
+ pollux_set(memregs, set_string);
+ else
+ printf("env var %s not defined.\n", env_var);
+}
+
+/* RAM timings */
+static unsigned short memtimex[2];
+
+static void set_ram_timings_(void)
+{
+ pollux_set_fromenv("POLLUX_RAM_TIMINGS");
+}
+
+static void unset_ram_timings_(void)
+{
+ int i;
+
+ memregs[0x14802>>1] = memtimex[0];
+ memregs[0x14804>>1] = memtimex[1] | 0x8000;
+
+ for (i = 0; i < 0x100000; i++)
+ if (!(memregs[0x14804>>1] & 0x8000))
+ break;
+
+ printf("RAM timings reset to startup values.\n");
+}
+
+/* LCD refresh */
+static void set_lcd_custom_rate_(int is_pal)
+{
+ char buff[32];
+
+ snprintf(buff, sizeof(buff), "POLLUX_LCD_TIMINGS_%s", is_pal ? "PAL" : "NTSC");
+ pollux_set_fromenv(buff);
+}
+
+static void unset_lcd_custom_rate_(void)
+{
+}
+
+static void set_lcd_gamma_(int g100, int A_SNs_curve)
+{
+ /* hm, the LCD possibly can do it (but not POLLUX) */
+}