continuing input framework integration
[libpicofe.git] / pandora / emu.c
index 2d873df..8bc76be 100644 (file)
 #include <stdarg.h>\r
 \r
 #include "../gp2x/emu.h"\r
-#include "../gp2x/usbjoy.h"\r
 #include "../gp2x/menu.h"\r
+#include "../gp2x/gp2x.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/common.h"\r
+#include "../common/input.h"\r
+#include "../linux/sndout_oss.h"\r
 #include "asm_utils.h"\r
 \r
-#include <Pico/PicoInt.h>\r
-#include <Pico/Patch.h>\r
-#include <Pico/sound/mix.h>\r
+#include <pico/pico_int.h>\r
+#include <pico/patch.h>\r
+#include <pico/sound/mix.h>\r
 #include <zlib/zlib.h>\r
 \r
 //#define PFRAMES\r
@@ -101,18 +103,6 @@ void emu_Init(void)
 }\r
 \r
 \r
-static void scaling_update(void)\r
-{\r
-       PicoOpt &= ~0x4100;\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
-       }\r
-}\r
-\r
-\r
 void emu_Deinit(void)\r
 {\r
        // save SRAM\r
@@ -139,7 +129,7 @@ void emu_prepareDefaultConfig(void)
        defaultConfig.EmuOpt    = 0x8f | 0x00600; // | <- confirm_save, cd_leds\r
        defaultConfig.s_PicoOpt  = 0x0f | POPT_EXT_FM|POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC;\r
        defaultConfig.s_PicoOpt |= POPT_ACC_SPRITES|POPT_EN_MCD_GFX;\r
-       defaultConfig.s_PicoOpt &= ~POPT_EN_SVP_DRC; // crashes :(\r
+//     defaultConfig.s_PicoOpt &= ~POPT_EN_SVP_DRC; // crashes :(\r
        defaultConfig.EmuOpt    &= ~8; // no save gzip\r
        defaultConfig.s_PsndRate = 44100;\r
        defaultConfig.s_PicoRegion = 0;\r
@@ -294,8 +284,8 @@ static int EmuScanEnd16(unsigned int num)
                len = 256;\r
        }\r
 \r
-       if (!sh && (rendstatus & PDRAW_ACC_SPRITES))\r
-               mask=0x3f; // accurate sprites, upper bits are priority stuff\r
+       if (!sh && (rendstatus & PDRAW_SPR_LO_ON_HI))\r
+               mask=0x3f; // messed sprites, upper bits are priority stuff\r
 \r
 #if 1\r
        clut_line(pd, ps, pal, (mask<<16) | len);\r
@@ -350,7 +340,7 @@ static void blit(const char *fps, const char *notice)
                // 8bit accurate renderer\r
                if (Pico.m.dirtyPal)\r
                {\r
-                       int pallen = 0x40;\r
+                       int pallen = 0xc0;\r
                        Pico.m.dirtyPal = 0;\r
                        if (Pico.video.reg[0xC]&8) // shadow/hilight mode\r
                        {\r
@@ -360,13 +350,6 @@ static void blit(const char *fps, const char *notice)
                                memcpy32(localPal+0xc0, localPal+0x40, 0x40);\r
                                pallen = 0x100;\r
                        }\r
-                       else if (rendstatus & PDRAW_ACC_SPRITES) {\r
-                               vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
-                               memcpy32(localPal+0x40, localPal, 0x40);\r
-                               memcpy32(localPal+0x80, localPal, 0x40);\r
-                               memcpy32(localPal+0xc0, localPal, 0x40);\r
-                               pallen = 0x100;\r
-                       }\r
                        else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r
                                vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
                                vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);\r
@@ -375,6 +358,7 @@ static void blit(const char *fps, const char *notice)
                        }\r
                        else {\r
                                vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
+                               memcpy32(localPal+0x80, localPal, 0x40);\r
                        }\r
                        if (pallen > 0xc0) {\r
                                localPal[0xc0] = 0x0000c000;\r
@@ -492,6 +476,7 @@ static void emu_msg_tray_open(void)
        gettimeofday(&noticeMsgTime, 0);\r
 }\r
 \r
+#if 0\r
 static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)\r
 {\r
        int ret, px, py, lim_x;\r
@@ -543,6 +528,7 @@ static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)
        PicoPicohw.pen_pos[0] += 0x3c;\r
        PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);\r
 }\r
+#endif\r
 \r
 static void update_volume(int has_changed, int is_up)\r
 {\r
@@ -560,7 +546,7 @@ static void update_volume(int has_changed, int is_up)
                                if (vol >  0) vol--;\r
                        }\r
                        wait_frames = 0;\r
-                       gp2x_sound_volume(vol, vol);\r
+                       sndout_oss_setvol(vol, vol);\r
                        currentConfig.volume = vol;\r
                }\r
                sprintf(noticeMsg, "VOL: %02i", vol);\r
@@ -586,6 +572,7 @@ static void RunEvents(unsigned int which)
                if ( emu_checkSaveFile(state_slot) &&\r
                                (( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) ||   // load\r
                                 (!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) { // save\r
+#if 0\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
@@ -594,6 +581,7 @@ static void RunEvents(unsigned int which)
                        while ( gp2x_joystick_read(1) & (GP2X_X|GP2X_Y) ) // wait for release\r
                                usleep(50*1024);\r
                        clearArea(0);\r
+#endif\r
                }\r
                if (do_it) {\r
                        osd_text(4, SCREEN_HEIGHT-16, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r
@@ -642,63 +630,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
-       int joy, i;\r
-\r
-       keys = gp2x_joystick_read(0);\r
-       if (keys & GP2X_SELECT) {\r
-               engineState = select_exits ? PGS_Quit : PGS_Menu;\r
-               // wait until select is released, so menu would not resume game\r
-               while (gp2x_joystick_read(1) & GP2X_SELECT) usleep(50*1000);\r
-       }\r
-\r
-       keys &= CONFIGURABLE_KEYS;\r
-       keys2 = keys;\r
-\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
 \r
-       // add joy inputs\r
-       if (num_of_joys > 0)\r
-       {\r
-               gp2x_usbjoy_update();\r
-               for (joy = 0; joy < num_of_joys; joy++) {\r
-                       int btns = gp2x_usbjoy_check2(joy);\r
-                       for (i = 0; i < 32; i++) {\r
-                               if (btns & (1 << i)) {\r
-                                       int acts = currentConfig.JoyBinds[joy][i];\r
-                                       int pl = (acts >> 16) & 1;\r
-                                       allActions[pl] |= acts;\r
-                               }\r
-                       }\r
-               }\r
-       }\r
+       /* FIXME: combos, player2 */\r
+       allActions[0] = in_update();\r
 \r
        PicoPad[0] = allActions[0] & 0xfff;\r
        PicoPad[1] = allActions[1] & 0xfff;\r
@@ -720,8 +656,10 @@ static void updateKeys(void)
 \r
        events &= ~prevEvents;\r
 \r
+/*\r
        if (PicoAHW == PAHW_PICO)\r
                RunEventsPico(events, keys);\r
+*/\r
        if (events) RunEvents(events);\r
        if (movie_data) emu_updateMovie();\r
 \r
@@ -735,7 +673,7 @@ static void updateSound(int len)
 \r
        /* avoid writing audio when lagging behind to prevent audio lag */\r
        if (PicoSkipFrame != 2)\r
-               gp2x_sound_write(PsndOut, len<<1);\r
+               sndout_oss_write(PsndOut, len<<1);\r
 }\r
 \r
 \r
@@ -781,6 +719,11 @@ void emu_forcedFrame(int opts)
        currentConfig.EmuOpt = eo_old;\r
 }\r
 \r
+void emu_platformDebugCat(char *str)\r
+{\r
+       // nothing\r
+}\r
+\r
 static void simpleWait(int thissec, int lim_time)\r
 {\r
        struct timeval tval;\r
@@ -797,10 +740,46 @@ static void simpleWait(int thissec, int lim_time)
        }\r
 }\r
 \r
+void emu_startSound(void)\r
+{\r
+       static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;\r
+       int target_fps = Pico.m.pal ? 50 : 60;\r
+\r
+       PsndOut = NULL;\r
+\r
+       if (currentConfig.EmuOpt & 4)\r
+       {\r
+               int snd_excess_add;\r
+               if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old)\r
+                       PsndRerate(Pico.m.frame_count ? 1 : 0);\r
+\r
+               snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
+               printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
+                       PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r
+               sndout_oss_start(PsndRate, 16, (PicoOpt&8)>>3);\r
+               sndout_oss_setvol(currentConfig.volume, currentConfig.volume);\r
+               PicoWriteSound = updateSound;\r
+               update_volume(0, 0);\r
+               memset(sndBuffer, 0, sizeof(sndBuffer));\r
+               PsndOut = sndBuffer;\r
+               PsndRate_old = PsndRate;\r
+               PicoOpt_old  = PicoOpt;\r
+               pal_old = Pico.m.pal;\r
+       }\r
+}\r
+\r
+void emu_endSound(void)\r
+{\r
+}\r
+\r
+/* wait until we can write more sound */\r
+void emu_waitSound(void)\r
+{\r
+       // don't need to do anything, writes will block by themselves\r
+}\r
 \r
 void emu_Loop(void)\r
 {\r
-       static int PsndRate_old = 0, PicoOpt_old = 0, pal_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
@@ -814,7 +793,6 @@ void emu_Loop(void)
 \r
        // make sure we are in correct mode\r
        vidResetMode();\r
-       scaling_update();\r
        Pico.m.dirtyPal = 1;\r
        oldmodes = ((Pico.video.reg[12]&1)<<2) ^ 0xc;\r
        emu_findKeyBindCombos();\r
@@ -824,28 +802,7 @@ void emu_Loop(void)
        target_frametime = 1000000/target_fps;\r
        reset_timing = 1;\r
 \r
-       // prepare sound stuff\r
-       if (currentConfig.EmuOpt & 4)\r
-       {\r
-               int snd_excess_add;\r
-               if (PsndRate != PsndRate_old || (PicoOpt&0x20b) != (PicoOpt_old&0x20b) || Pico.m.pal != pal_old)\r
-                       PsndRerate(Pico.m.frame_count ? 1 : 0);\r
-\r
-               snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;\r
-               printf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",\r
-                       PsndRate, PsndLen, snd_excess_add, (PicoOpt&8)>>3, Pico.m.pal);\r
-               gp2x_start_sound(PsndRate, 16, (PicoOpt&8)>>3);\r
-               gp2x_sound_volume(currentConfig.volume, currentConfig.volume);\r
-               PicoWriteSound = updateSound;\r
-               update_volume(0, 0);\r
-               memset(sndBuffer, 0, sizeof(sndBuffer));\r
-               PsndOut = sndBuffer;\r
-               PsndRate_old = PsndRate;\r
-               PicoOpt_old  = PicoOpt;\r
-               pal_old = Pico.m.pal;\r
-       } else {\r
-               PsndOut = NULL;\r
-       }\r
+       emu_startSound();\r
 \r
        // prepare CD buffer\r
        if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r
@@ -1002,7 +959,8 @@ void emu_Loop(void)
 \r
                if (currentConfig.Frameskip < 0 && tval.tv_usec - lim_time >= 300000) // slowdown detection\r
                        reset_timing = 1;\r
-/*             else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
+#if 1\r
+               else if (PsndOut != NULL || currentConfig.Frameskip < 0)\r
                {\r
                        // sleep or vsync if we are still too fast\r
                        // usleep sleeps for ~20ms minimum, so it is not a solution here\r
@@ -1018,7 +976,7 @@ void emu_Loop(void)
                                }\r
                        }\r
                }\r
-*/\r
+#endif\r
                blit(fpsbuff, notice);\r
 \r
                pframes_done++; pframes_shown++;\r