linux port fixed; some cleanups
[libpicofe.git] / gp2x / emu.c
index 585c1d5..46dc3aa 100644 (file)
@@ -1,4 +1,4 @@
-// (c) Copyright 2006-2007 notaz, All rights reserved.\r
+// (c) Copyright 2006-2009 notaz, All rights reserved.\r
 // Free for non-commercial use.\r
 \r
 // For commercial use, separate licencing terms must be obtained.\r
@@ -8,15 +8,11 @@
 #include <sys/time.h>\r
 #include <sys/stat.h>\r
 #include <sys/types.h>\r
-#include <linux/limits.h>\r
-#include <ctype.h>\r
-#include <unistd.h>\r
-\r
 #include <stdarg.h>\r
 \r
-#include "emu.h"\r
-#include "gp2x.h"\r
+#include "plat_gp2x.h"\r
 #include "soc.h"\r
+#include "../common/plat.h"\r
 #include "../common/menu.h"\r
 #include "../common/arm_utils.h"\r
 #include "../common/fonts.h"\r
@@ -24,7 +20,6 @@
 #include "../common/config.h"\r
 #include "../common/input.h"\r
 #include "../linux/sndout_oss.h"\r
-#include "cpuctrl.h"\r
 #include "version.h"\r
 \r
 #include <pico/pico_int.h>\r
@@ -102,12 +97,16 @@ void emu_Init(void)
 \r
 static void scaling_update(void)\r
 {\r
-       PicoOpt &= ~0x4100;\r
+       PicoOpt &= ~(POPT_DIS_32C_BORDER|POPT_EN_SOFTSCALE);\r
        switch (currentConfig.scaling) {\r
-               default: break; // off\r
-               case 1:  // hw hor\r
-               case 2:  PicoOpt |=  0x0100; break; // hw hor+vert\r
-               case 3:  PicoOpt |=  0x4000; break; // sw hor\r
+               default:break;\r
+               case EOPT_SCALE_HW_H:\r
+               case EOPT_SCALE_HW_HV:\r
+                       PicoOpt |= POPT_DIS_32C_BORDER;\r
+                       break;\r
+               case EOPT_SCALE_SW_H:\r
+                       PicoOpt |= POPT_EN_SOFTSCALE;\r
+                       break;\r
        }\r
 }\r
 \r
@@ -129,27 +128,33 @@ void emu_Deinit(void)
 \r
        // restore gamma\r
        if (gp2x_old_gamma != 100)\r
-               set_gamma(100, 0);\r
+               set_lcd_gamma(100, 0);\r
 }\r
 \r
 void emu_prepareDefaultConfig(void)\r
 {\r
+       gp2x_soc_t soc;\r
+\r
        memset(&defaultConfig, 0, sizeof(defaultConfig));\r
-       defaultConfig.EmuOpt    = 0x9d | 0x00700; // | <- ram_tmng, confirm_save, cd_leds\r
-       defaultConfig.s_PicoOpt = 0x0f | POPT_EXT_FM|POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES;\r
+       defaultConfig.EmuOpt    = 0x9d | EOPT_RAM_TIMINGS | 0x600; // | <- confirm_save, cd_leds\r
+       defaultConfig.s_PicoOpt = 0x0f | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC|POPT_ACC_SPRITES;\r
        defaultConfig.s_PsndRate = 44100;\r
        defaultConfig.s_PicoRegion = 0; // auto\r
        defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP\r
        defaultConfig.s_PicoCDBuffers = 0;\r
        defaultConfig.Frameskip = -1; // auto\r
-       defaultConfig.CPUclock = 200;\r
+       defaultConfig.CPUclock = default_cpu_clock;\r
        defaultConfig.volume = 50;\r
        defaultConfig.gamma = 100;\r
        defaultConfig.scaling = 0;\r
        defaultConfig.turbo_rate = 15;\r
+\r
+       soc = soc_detect();\r
+       if (soc == SOCID_MMSP2)\r
+               defaultConfig.s_PicoOpt |= POPT_EXT_FM;\r
 }\r
 \r
-void osd_text(int x, int y, const char *text)\r
+static void osd_text(int x, int y, const char *text)\r
 {\r
        int len = strlen(text)*8;\r
        int *p, i, h, offs;\r
@@ -296,8 +301,10 @@ static void blit(const char *fps, const char *notice)
 \r
        if (notice || (emu_opt & 2)) {\r
                int h = 232;\r
-               if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8)) h -= 8;\r
-               if (notice) osd_text(4, h, notice);\r
+               if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(Pico.video.reg[1]&8))\r
+                       h -= 8;\r
+               if (notice)\r
+                       osd_text(4, h, notice);\r
                if (emu_opt & 2)\r
                        osd_text(osd_fps_x, h, fps);\r
        }\r
@@ -363,7 +370,7 @@ static void vidResetMode(void)
        }\r
        Pico.m.dirtyPal = 1;\r
        // reset scaling\r
-       if (currentConfig.scaling == 2 && !(Pico.video.reg[1]&8))\r
+       if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(Pico.video.reg[1]&8))\r
             gp2x_video_RGB_setscaling(8, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 224);\r
        else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);\r
 }\r
@@ -762,7 +769,7 @@ static void tga_dump(void)
 \r
 void emu_Loop(void)\r
 {\r
-       static int gp2x_old_clock = 200, EmuOpt_old = 0;\r
+       static int gp2x_old_clock = -1, EmuOpt_old = 0;\r
        char fpsbuff[24]; // fps count c string\r
        struct timeval tval; // timing\r
        int pframes_done, pframes_shown, pthissec; // "period" frames, used for sync\r
@@ -772,25 +779,38 @@ void emu_Loop(void)
 \r
        printf("entered emu_Loop()\n");\r
 \r
+       if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_RAM_TIMINGS) {\r
+               if (currentConfig.EmuOpt & EOPT_RAM_TIMINGS)\r
+                       set_ram_timings();\r
+               else\r
+                       unset_ram_timings();\r
+       }\r
+\r
+       if (gp2x_old_clock < 0)\r
+               gp2x_old_clock = default_cpu_clock;\r
        if (gp2x_old_clock != currentConfig.CPUclock) {\r
                printf("changing clock to %i...", currentConfig.CPUclock); fflush(stdout);\r
-               set_FCLK(currentConfig.CPUclock);\r
+               gp2x_set_cpuclk(currentConfig.CPUclock);\r
                gp2x_old_clock = currentConfig.CPUclock;\r
                printf(" done\n");\r
        }\r
 \r
        if (gp2x_old_gamma != currentConfig.gamma || (EmuOpt_old&0x1000) != (currentConfig.EmuOpt&0x1000)) {\r
-               set_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
+               set_lcd_gamma(currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
                gp2x_old_gamma = currentConfig.gamma;\r
                printf("updated gamma to %i, A_SN's curve: %i\n", currentConfig.gamma, !!(currentConfig.EmuOpt&0x1000));\r
        }\r
 \r
-       if ((EmuOpt_old&0x2000) != (currentConfig.EmuOpt&0x2000)) {\r
-               if (currentConfig.EmuOpt&0x2000)\r
-                    set_LCD_custom_rate(Pico.m.pal ? LCDR_100 : LCDR_120);\r
-               else unset_LCD_custom_rate();\r
+       if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_PSYNC) {\r
+               if (currentConfig.EmuOpt & EOPT_PSYNC)\r
+                       set_lcd_custom_rate(Pico.m.pal);\r
+               else\r
+                       unset_lcd_custom_rate();\r
        }\r
 \r
+       if ((EmuOpt_old ^ currentConfig.EmuOpt) & EOPT_MMUHACK)\r
+               gp2x_make_fb_bufferable(currentConfig.EmuOpt & EOPT_MMUHACK);\r
+\r
        EmuOpt_old = currentConfig.EmuOpt;\r
        fpsbuff[0] = 0;\r
 \r
@@ -870,7 +890,8 @@ void emu_Loop(void)
                                        vidCpyM2 = vidCpyM2_32col;\r
                                }\r
                        }\r
-                       if (currentConfig.scaling == 2 && !(modes&8)) // want vertical scaling and game is not in 240 line mode\r
+                       /* want vertical scaling and game is not in 240 line mode */\r
+                       if (currentConfig.scaling == EOPT_SCALE_HW_HV && !(modes&8))\r
                             gp2x_video_RGB_setscaling(8, scalex, 224);\r
                        else gp2x_video_RGB_setscaling(0, scalex, 240);\r
                        oldmodes = modes;\r