linux port fixed; some cleanups
[libpicofe.git] / gp2x / emu.c
index 237105a..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,22 +8,18 @@
 #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 "menu.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
 #include "../common/emu.h"\r
 #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
@@ -40,8 +36,6 @@
 #endif\r
 \r
 \r
-int select_exits = 0;\r
-\r
 extern int crashed_940;\r
 \r
 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
@@ -103,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
@@ -130,40 +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.KeyBinds[ 0] = 1<<0; // SACB RLDU\r
-       defaultConfig.KeyBinds[ 4] = 1<<1;\r
-       defaultConfig.KeyBinds[ 2] = 1<<2;\r
-       defaultConfig.KeyBinds[ 6] = 1<<3;\r
-       defaultConfig.KeyBinds[14] = 1<<4;\r
-       defaultConfig.KeyBinds[13] = 1<<5;\r
-       defaultConfig.KeyBinds[12] = 1<<6;\r
-       defaultConfig.KeyBinds[ 8] = 1<<7;\r
-       defaultConfig.KeyBinds[15] = 1<<26; // switch rend\r
-       defaultConfig.KeyBinds[10] = 1<<27; // save state\r
-       defaultConfig.KeyBinds[11] = 1<<28; // load state\r
-       defaultConfig.KeyBinds[23] = 1<<29; // vol up\r
-       defaultConfig.KeyBinds[22] = 1<<30; // vol down\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
@@ -310,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
@@ -377,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
@@ -415,7 +408,7 @@ static void emu_msg_tray_open(void)
        gettimeofday(&noticeMsgTime, 0);\r
 }\r
 \r
-static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)\r
+static void RunEventsPico(unsigned int events)\r
 {\r
        int ret, px, py, lim_x;\r
        static int pdown_frames = 0;\r
@@ -449,11 +442,11 @@ static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)
                //      PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r
        }\r
 \r
+       if (PicoPad[0] & 1) pico_pen_y--;\r
+       if (PicoPad[0] & 2) pico_pen_y++;\r
+       if (PicoPad[0] & 4) pico_pen_x--;\r
+       if (PicoPad[0] & 8) pico_pen_x++;\r
        PicoPad[0] &= ~0x0f; // release UDLR\r
-       if (gp2x_keys & GP2X_UP)    pico_pen_y--;\r
-       if (gp2x_keys & GP2X_DOWN)  pico_pen_y++;\r
-       if (gp2x_keys & GP2X_LEFT)  pico_pen_x--;\r
-       if (gp2x_keys & GP2X_RIGHT) pico_pen_x++;\r
 \r
        lim_x = (Pico.video.reg[12]&1) ? 319 : 255;\r
        if (pico_pen_y < 8) pico_pen_y = 8;\r
@@ -507,15 +500,31 @@ static void RunEvents(unsigned int which)
        {\r
                int do_it = 1;\r
                if ( emu_checkSaveFile(state_slot) &&\r
-                               (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) ||   // load\r
-                                (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save\r
-                       unsigned long keys;\r
-                       blit("", (which & 0x1000) ? "LOAD STATE? (Y=yes, X=no)" : "OVERWRITE SAVE? (Y=yes, X=no)");\r
-                       while ( !((keys = gp2x_joystick_read(1)) & (GP2X_X|GP2X_Y)) )\r
-                               usleep(50*1024);\r
-                       if (keys & GP2X_X) do_it = 0;\r
-                       while ( gp2x_joystick_read(1) & (GP2X_X|GP2X_Y) ) // wait for release\r
-                               usleep(50*1024);\r
+                               (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load\r
+                                (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save\r
+               {\r
+                       const char *nm;\r
+                       char tmp[64];\r
+                       int keys, len;\r
+\r
+                       strcpy(tmp, (which & 0x1000) ? "LOAD STATE? " : "OVERWRITE SAVE? ");\r
+                       len = strlen(tmp);\r
+                       nm = in_get_key_name(-1, -PBTN_MA3);\r
+                       snprintf(tmp + len, sizeof(tmp) - len, "(%s=yes, ", nm);\r
+                       len = strlen(tmp);\r
+                       nm = in_get_key_name(-1, -PBTN_MBACK);\r
+                       snprintf(tmp + len, sizeof(tmp) - len, "%s=no)", nm);\r
+\r
+                       blit("", tmp);\r
+\r
+                       in_set_blocking(1);\r
+                       while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK));   // wait for release\r
+                       while ( !((keys = in_menu_wait_any(50)) & (PBTN_MA3|PBTN_MBACK)) ); // .. press\r
+                       if (keys & PBTN_MBACK)\r
+                               do_it = 0;\r
+                       while (in_menu_wait_any(50) & (PBTN_MA3|PBTN_MBACK));   // .. release\r
+                       in_set_blocking(0);\r
+\r
                        clearArea(0);\r
                }\r
                if (do_it) {\r
@@ -544,7 +553,7 @@ static void RunEvents(unsigned int which)
                        strcpy(noticeMsg, " 8bit accurate renderer");\r
                }\r
 \r
-               gettimeofday(&noticeMsgTime, 0);\r
+               emu_noticeMsgUpdated();\r
        }\r
        if (which & 0x0300)\r
        {\r
@@ -556,7 +565,7 @@ static void RunEvents(unsigned int which)
                        if(state_slot > 9) state_slot = 0;\r
                }\r
                sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");\r
-               gettimeofday(&noticeMsgTime, 0);\r
+               emu_noticeMsgUpdated();\r
        }\r
        if (which & 0x0080) {\r
                engineState = PGS_Menu;\r
@@ -565,48 +574,11 @@ static void RunEvents(unsigned int which)
 \r
 static void updateKeys(void)\r
 {\r
-       unsigned int keys, keys2, allActions[2] = { 0, 0 }, events;\r
+       unsigned int allActions[2] = { 0, 0 }, events;\r
        static unsigned int prevEvents = 0;\r
 \r
-       keys = gp2x_joystick_read(0);\r
-       if (keys & GP2X_SELECT)\r
-               engineState = select_exits ? PGS_Quit : PGS_Menu;\r
-\r
-       keys &= CONFIGURABLE_KEYS;\r
-       keys2 = keys;\r
-\r
-#if 1\r
-       /* FIXME: combos, player2 */\r
+       /* FIXME: player2 */\r
        allActions[0] = in_update();\r
-#else\r
-       for (i = 0; i < 32; i++)\r
-       {\r
-               if (keys2 & (1 << i))\r
-               {\r
-                       int pl, acts = currentConfig.KeyBinds[i];\r
-                       if (!acts) continue;\r
-                       pl = (acts >> 16) & 1;\r
-                       if (kb_combo_keys & (1 << i))\r
-                       {\r
-                               int u = i+1, acts_c = acts & kb_combo_acts;\r
-                               // let's try to find the other one\r
-                               if (acts_c) {\r
-                                       for (; u < 32; u++)\r
-                                               if ( (keys2 & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {\r
-                                                       allActions[pl] |= acts_c & currentConfig.KeyBinds[u];\r
-                                                       keys2 &= ~((1 << i) | (1 << u));\r
-                                                       break;\r
-                                               }\r
-                               }\r
-                               // add non-combo actions if combo ones were not found\r
-                               if (!acts_c || u == 32)\r
-                                       allActions[pl] |= acts & ~kb_combo_acts;\r
-                       } else {\r
-                               allActions[pl] |= acts;\r
-                       }\r
-               }\r
-       }\r
-#endif\r
 \r
        PicoPad[0] = allActions[0] & 0xfff;\r
        PicoPad[1] = allActions[1] & 0xfff;\r
@@ -629,7 +601,7 @@ static void updateKeys(void)
        events &= ~prevEvents;\r
 \r
        if (PicoAHW == PAHW_PICO)\r
-               RunEventsPico(events, keys);\r
+               RunEventsPico(events);\r
        if (events) RunEvents(events);\r
        if (movie_data) emu_updateMovie();\r
 \r
@@ -797,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
@@ -807,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
@@ -834,7 +819,6 @@ void emu_Loop(void)
        scaling_update();\r
        Pico.m.dirtyPal = 1;\r
        oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
-       emu_findKeyBindCombos();\r
 \r
        // pal/ntsc might have changed, reset related stuff\r
        target_fps = Pico.m.pal ? 50 : 60;\r
@@ -906,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
@@ -1052,7 +1037,7 @@ const char *plat_get_credits(void)
                "Reesy & FluBBa: DrZ80 core\n"\r
                "MAME devs: YM2612 and SN76496 cores\n"\r
                "rlyeh and others: minimal SDK\n"\r
-               "Squidge: squidgehack\n"\r
+               "Squidge: mmuhack\n"\r
                "Dzz: ARM940 sample\n"\r
                "GnoStiC / Puck2099: USB joy code\n"\r
                "craigix: GP2X hardware\n"\r