update license in source code itself
[libpicofe.git] / gp2x / soc_mmsp2.c
index 32399d7..ee933d4 100644 (file)
@@ -1,3 +1,14 @@
+/*
+ * (C) GraÅžvydas "notaz" Ignotas, 2006-2012
+ *
+ * This work is licensed under the terms of any of these licenses
+ * (at your option):
+ *  - GNU GPL, version 2 or later.
+ *  - GNU LGPL, version 2.1 or later.
+ *  - MAME license.
+ * See the COPYING file in the top-level directory.
+ */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -11,8 +22,9 @@
 #include "soc.h"
 #include "soc_mmsp2.h"
 #include "plat_gp2x.h"
-#include "../common/emu.h"
-#include "../common/arm_utils.h"
+#include "../emu.h"
+#include "../plat.h"
+#include "../arm_utils.h"
 #include "940ctl.h"
 
 volatile unsigned short *gp2x_memregs;
@@ -20,7 +32,7 @@ volatile unsigned long  *gp2x_memregl;
 extern void *gp2x_screens[4];
 static int screensel = 0;
 
-int memdev = 0;        /* used by code940 */
+int memdev = -1;       /* used by code940 */
 static int touchdev = -1;
 static int touchcal[7] = { 6203, 0, -1501397, 0, -4200, 16132680, 65536 };
 
@@ -33,11 +45,15 @@ static int touchcal[7] = { 6203, 0, -1501397, 0, -4200, 16132680, 65536 };
 
 static const int gp2x_screenaddrs[4] = { FRAMEBUFF_ADDR0, FRAMEBUFF_ADDR1, FRAMEBUFF_ADDR2, FRAMEBUFF_ADDR3 };
 static int gp2x_screenaddrs_use[4];
-static unsigned short gp2x_screenaddr_old[4];
 
+static char gamma_was_changed = 0;
+static char cpuclk_was_changed = 0;
+static unsigned short gp2x_screenaddr_old[4];
+static unsigned short memtimex_old[2];
+static unsigned short reg0910;
 
 /* video stuff */
-void gp2x_video_flip(void)
+static void gp2x_video_flip_(void)
 {
        unsigned short lsw = (unsigned short) gp2x_screenaddrs_use[screensel&3];
        unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&3] >> 16);
@@ -52,7 +68,7 @@ void gp2x_video_flip(void)
 }
 
 /* doulblebuffered flip */
-void gp2x_video_flip2(void)
+static void gp2x_video_flip2_(void)
 {
        unsigned short msw = (unsigned short)(gp2x_screenaddrs_use[screensel&1] >> 16);
 
@@ -65,13 +81,13 @@ void gp2x_video_flip2(void)
        g_screen_ptr = gp2x_screens[++screensel&1];
 }
 
-void gp2x_video_changemode_ll(int bpp)
+static void gp2x_video_changemode_ll_(int bpp)
 {
        gp2x_memregs[0x28DA>>1] = (((bpp+1)/8)<<9)|0xAB; /*8/15/16/24bpp...*/
        gp2x_memregs[0x290C>>1] = 320*((bpp+1)/8); /*line width in bytes*/
 }
 
-void gp2x_video_setpalette(int *pal, int len)
+static void gp2x_video_setpalette_(int *pal, int len)
 {
        unsigned short *g = (unsigned short *)pal;
        volatile unsigned short *memreg = &gp2x_memregs[0x295A>>1];
@@ -83,8 +99,7 @@ void gp2x_video_setpalette(int *pal, int len)
                *memreg = *g++;
 }
 
-// TV Compatible function //
-void gp2x_video_RGB_setscaling(int ln_offs, int W, int H)
+static void gp2x_video_RGB_setscaling_(int ln_offs, int W, int H)
 {
        float escalaw, escalah;
        int bpp = (gp2x_memregs[0x28DA>>1]>>9)&0x3;
@@ -99,7 +114,7 @@ void gp2x_video_RGB_setscaling(int ln_offs, int W, int H)
        escalaw = 1024.0; // RGB Horiz LCD
        escalah = 320.0; // RGB Vert LCD
 
-       if(gp2x_memregs[0x2800>>1]&0x100) //TV-Out
+       if (gp2x_memregs[0x2800>>1]&0x100) //TV-Out
        {
                escalaw=489.0; // RGB Horiz TV (PAL, NTSC)
                if (gp2x_memregs[0x2818>>1]  == 287) //PAL
@@ -110,14 +125,15 @@ void gp2x_video_RGB_setscaling(int ln_offs, int W, int H)
 
        // scale horizontal
        scalw = (unsigned short)((float)escalaw *(W/320.0));
-       /* if there is no horizontal scaling, vertical doesn't work. Here is a nasty wrokaround... */
+       /* if there is no horizontal scaling, vertical doesn't work.
+        * Here is a nasty wrokaround... */
        if (H != 240 && W == 320) scalw--;
        gp2x_memregs[0x2906>>1]=scalw;
        // scale vertical
        gp2x_memregl[0x2908>>2]=(unsigned long)((float)escalah *bpp *(H/240.0));
 }
 
-void gp2x_video_wait_vsync(void)
+static void gp2x_video_wait_vsync_(void)
 {
        unsigned short v = gp2x_memregs[0x1182>>1];
        while (!((v ^ gp2x_memregs[0x1182>>1]) & 0x10))
@@ -148,7 +164,7 @@ void reset940(int yes, int bank)
 
 #define SYS_CLK_FREQ 7372800
 
-void gp2x_set_cpuclk(unsigned int mhz)
+static void gp2x_set_cpuclk_(unsigned int mhz)
 {
        unsigned int mdiv, pdiv, sdiv = 0;
        unsigned int v;
@@ -166,17 +182,17 @@ void gp2x_set_cpuclk(unsigned int mhz)
        for (i = 0; i < 10000; i++)
                if (!(gp2x_memregs[0x902>>1] & 1))
                        break;
+
+       cpuclk_was_changed = 1;
 }
 
 /* RAM timings */
-static unsigned short memtimex[2];
-
 #define TIMING_CHECK(t, adj, mask) \
        t += adj; \
        if (t & ~mask) \
                goto bad
 
-void set_ram_timings(int tCAS, int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD)
+static void set_ram_timing_vals(int tCAS, int tRC, int tRAS, int tWR, int tMRD, int tRFC, int tRP, int tRCD)
 {
        int i;
        TIMING_CHECK(tCAS, -2, 0x1);
@@ -206,10 +222,16 @@ bad:
        fprintf(stderr, "RAM timings invalid.\n");
 }
 
-void unset_ram_timings(void)
+static void set_ram_timings_(void)
+{
+       /* craigix: --cas 2 --trc 6 --tras 4 --twr 1 --tmrd 1 --trfc 1 --trp 2 --trcd 2 */
+       set_ram_timing_vals(2, 6, 4, 1, 1, 1, 2, 2);
+}
+
+static void unset_ram_timings_(void)
 {
-       gp2x_memregs[0x3802>>1] = memtimex[0];
-       gp2x_memregs[0x3804>>1] = memtimex[1] | 0x8000;
+       gp2x_memregs[0x3802>>1] = memtimex_old[0];
+       gp2x_memregs[0x3804>>1] = memtimex_old[1] | 0x8000;
        printf("RAM timings reset to startup values.\n");
 }
 
@@ -279,7 +301,7 @@ static void set_reg_setting(const reg_setting *set)
        }
 }
 
-void set_lcd_custom_rate(int is_pal)
+static void set_lcd_custom_rate_(int is_pal)
 {
        if (gp2x_memregs[0x2800>>1] & 0x100) // tv-out
                return;
@@ -291,13 +313,13 @@ void set_lcd_custom_rate(int is_pal)
        printf("done.\n");
 }
 
-void unset_lcd_custom_rate(void)
+static void unset_lcd_custom_rate_(void)
 {
        printf("reset to prev LCD refresh.\n");
        set_reg_setting(lcd_rate_defaults);
 }
 
-void set_lcd_gamma(int g100, int A_SNs_curve)
+static void set_lcd_gamma_(int g100, int A_SNs_curve)
 {
        float gamma = (float) g100 / 100;
        int i;
@@ -338,8 +360,14 @@ void set_lcd_gamma(int g100, int A_SNs_curve)
                gp2x_memregs[0x295E>>1]= s;
                gp2x_memregs[0x295E>>1]= g;
        }
+
+       gamma_was_changed = 1;
 }
 
+static int gp2x_read_battery_(void)
+{
+       return -1; /* TODO? */
+}
 
 /* these are not quite MMSP2 related,
  * more to GP2X F100/F200 consoles themselves. */
@@ -448,12 +476,13 @@ void mmsp2_init(void)
 
        memcpy(gp2x_screenaddrs_use, gp2x_screenaddrs, sizeof(gp2x_screenaddrs));
 
-       /* default LCD refresh */
+       /* save startup values: LCD refresh */
        get_reg_setting(lcd_rate_defaults);
 
-       /* RAM timings */
-       memtimex[0] = gp2x_memregs[0x3802>>1];
-       memtimex[1] = gp2x_memregs[0x3804>>1];
+       /* CPU and RAM timings */
+       reg0910 = gp2x_memregs[0x0910>>1];
+       memtimex_old[0] = gp2x_memregs[0x3802>>1];
+       memtimex_old[1] = gp2x_memregs[0x3804>>1];
 
        /* touchscreen */
        touchdev = open("/dev/touchscreen/wm97xx", O_RDONLY);
@@ -473,6 +502,26 @@ void mmsp2_init(void)
 
        /* code940 portion */
        sharedmem940_init();
+
+       gp2x_video_flip = gp2x_video_flip_;
+       gp2x_video_flip2 = gp2x_video_flip2_;
+       gp2x_video_changemode_ll = gp2x_video_changemode_ll_;
+       gp2x_video_setpalette = gp2x_video_setpalette_;
+       gp2x_video_RGB_setscaling = gp2x_video_RGB_setscaling_;
+       gp2x_video_wait_vsync = gp2x_video_wait_vsync_;
+
+       gp2x_set_cpuclk = gp2x_set_cpuclk_;
+
+       set_lcd_custom_rate = set_lcd_custom_rate_;
+       unset_lcd_custom_rate = unset_lcd_custom_rate_;
+       set_lcd_gamma = set_lcd_gamma_;
+
+       set_ram_timings = set_ram_timings_;
+       unset_ram_timings = unset_ram_timings_;
+       gp2x_read_battery = gp2x_read_battery_;
+
+       gp2x_get_ticks_ms = plat_get_ticks_ms_good;
+       gp2x_get_ticks_us = plat_get_ticks_us_good;
 }
 
 void mmsp2_finish(void)
@@ -481,13 +530,20 @@ void mmsp2_finish(void)
        pause940(1);
        sharedmem940_finish();
 
+       gp2x_video_RGB_setscaling_(0, 320, 240);
+       gp2x_video_changemode_ll_(16);
+
        gp2x_memregs[0x290E>>1] = gp2x_screenaddr_old[0];
        gp2x_memregs[0x2910>>1] = gp2x_screenaddr_old[1];
        gp2x_memregs[0x2912>>1] = gp2x_screenaddr_old[2];
        gp2x_memregs[0x2914>>1] = gp2x_screenaddr_old[3];
 
-       unset_ram_timings();
-       unset_lcd_custom_rate();
+       unset_lcd_custom_rate_();
+       if (gamma_was_changed)
+               set_lcd_gamma_(100, 0);
+       unset_ram_timings_();
+       if (cpuclk_was_changed)
+               gp2x_memregs[0x910>>1] = reg0910;
 
        munmap(gp2x_screens[0], FRAMEBUFF_WHOLESIZE);
        munmap((void *)gp2x_memregs, 0x10000);