some Pico input support
[libpicofe.git] / gp2x / emu.c
index 3ee96f2..f5b8ce3 100644 (file)
@@ -53,6 +53,8 @@ char noticeMsg[64];                   // notice msg to draw
 unsigned char *PicoDraw2FB = NULL;  // temporary buffer for alt renderer\r
 int reset_timing = 0;\r
 \r
+static int pico_pen_x = 0, pico_pen_y = 0, pico_inp_mode = 0;\r
+\r
 static void emu_msg_cb(const char *msg);\r
 static void emu_msg_tray_open(void);\r
 \r
@@ -141,7 +143,7 @@ void emu_prepareDefaultConfig(void)
        defaultConfig.s_PsndRate = 44100;\r
        defaultConfig.s_PicoRegion = 0; // auto\r
        defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP\r
-       defaultConfig.s_PicoCDBuffers = 64;\r
+       defaultConfig.s_PicoCDBuffers = 0;\r
        defaultConfig.Frameskip = -1; // auto\r
        defaultConfig.CPUclock = 200;\r
        defaultConfig.volume = 50;\r
@@ -225,18 +227,18 @@ static void cd_leds(void)
        }\r
 }\r
 \r
-static int EmuScan16(unsigned int num, void *sdata)\r
+static int EmuScanBegin16(unsigned int num)\r
 {\r
        if (!(Pico.video.reg[1]&8)) num += 8;\r
-       DrawLineDest = (unsigned short *) gp2x_screen + 320*(num+1);\r
+       DrawLineDest = (unsigned short *) gp2x_screen + 320 * num;\r
 \r
        return 0;\r
 }\r
 \r
-static int EmuScan8(unsigned int num, void *sdata)\r
+static int EmuScanBegin8(unsigned int num)\r
 {\r
        if (!(Pico.video.reg[1]&8)) num += 8;\r
-       DrawLineDest = (unsigned char *)  gp2x_screen + 320*(num+1);\r
+       DrawLineDest = (unsigned char *)  gp2x_screen + 320 * num;\r
 \r
        return 0;\r
 }\r
@@ -266,7 +268,8 @@ static void blit(const char *fps, const char *notice)
        else if (!(emu_opt&0x80))\r
        {\r
                // 8bit accurate renderer\r
-               if (Pico.m.dirtyPal) {\r
+               if (Pico.m.dirtyPal)\r
+               {\r
                        Pico.m.dirtyPal = 0;\r
                        if(Pico.video.reg[0xC]&8) { // shadow/hilight mode\r
                                vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
@@ -278,7 +281,7 @@ static void blit(const char *fps, const char *notice)
                                localPal[0xe0] = 0x00000000; // reserved pixels for OSD\r
                                localPal[0xf0] = 0x00ffffff;\r
                                gp2x_video_setpalette(localPal, 0x100);\r
-                       } else if (rendstatus & 0x20) { // mid-frame palette changes\r
+                       } else if (rendstatus & PDRAW_SONIC_MODE) { // mid-frame palette changes\r
                                vidConvCpyRGB32(localPal, Pico.cram, 0x40);\r
                                vidConvCpyRGB32(localPal+0x40, HighPal, 0x40);\r
                                vidConvCpyRGB32(localPal+0x80, HighPal+0x40, 0x40);\r
@@ -297,7 +300,7 @@ static void blit(const char *fps, const char *notice)
                if (emu_opt & 2)\r
                        osd_text(osd_fps_x, h, fps);\r
        }\r
-       if ((emu_opt & 0x400) && (PicoMCD & 1))\r
+       if ((emu_opt & 0x400) && (PicoAHW & PAHW_MCD))\r
                cd_leds();\r
 \r
        //gp2x_video_wait_vsync();\r
@@ -339,13 +342,11 @@ static void vidResetMode(void)
        } else if (currentConfig.EmuOpt&0x80) {\r
                gp2x_video_changemode(16);\r
                PicoDrawSetColorFormat(1);\r
-               PicoScan = EmuScan16;\r
-               PicoScan(0, 0);\r
+               PicoScanBegin = EmuScanBegin16;\r
        } else {\r
                gp2x_video_changemode(8);\r
                PicoDrawSetColorFormat(2);\r
-               PicoScan = EmuScan8;\r
-               PicoScan(0, 0);\r
+               PicoScanBegin = EmuScanBegin8;\r
        }\r
        if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
                // setup pal for 8-bit modes\r
@@ -397,6 +398,41 @@ static void emu_msg_tray_open(void)
        gettimeofday(&noticeMsgTime, 0);\r
 }\r
 \r
+static void RunEventsPico(unsigned int events, unsigned int gp2x_keys)\r
+{\r
+       if (events & (1 << 3)) {\r
+               pico_inp_mode++;\r
+               if (pico_inp_mode > 2) pico_inp_mode = 0;\r
+               switch (pico_inp_mode) {\r
+                       case 0: strcpy(noticeMsg, "Input: Joytick         "); break;\r
+                       case 1: strcpy(noticeMsg, "Input: Pen on Storyware"); break;\r
+                       case 2: strcpy(noticeMsg, "Input: Pen on Pad      "); break;\r
+               }\r
+               gettimeofday(&noticeMsgTime, 0);\r
+       }\r
+       if (events & (1 << 4)) {\r
+               PicoPicoPage--;\r
+               if (PicoPicoPage < 0) PicoPicoPage = 0;\r
+               sprintf(noticeMsg, "Page %i                 ", PicoPicoPage);\r
+               gettimeofday(&noticeMsgTime, 0);\r
+       }\r
+       if (events & (1 << 5)) {\r
+               PicoPicoPage++;\r
+               if (PicoPicoPage > 6) PicoPicoPage = 6;\r
+               sprintf(noticeMsg, "Page %i                 ", PicoPicoPage);\r
+               gettimeofday(&noticeMsgTime, 0);\r
+       }\r
+       if (pico_inp_mode != 0) {\r
+               PicoPad[0] &= ~0x0f; // release UDLR\r
+               if (gp2x_keys & GP2X_UP)   { pico_pen_y--; if (pico_pen_y <   0) pico_pen_y =   0; }\r
+               if (gp2x_keys & GP2X_DOWN) { pico_pen_y++; if (pico_pen_y > 251) pico_pen_y = 251; }\r
+               if (gp2x_keys & GP2X_LEFT) { pico_pen_x--; if (pico_pen_x <   0) pico_pen_x =   0; }\r
+               if (gp2x_keys & GP2X_RIGHT){ pico_pen_x++; if (pico_pen_x > 353) pico_pen_x = 353; }\r
+               PicoPicoPenPos[0] = 0x03c + pico_pen_x;\r
+               PicoPicoPenPos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);\r
+       }\r
+}\r
+\r
 static void update_volume(int has_changed, int is_up)\r
 {\r
        static int prev_frame = 0, wait_frames = 0;\r
@@ -466,9 +502,11 @@ static void RunEvents(unsigned int which)
                                 (!(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
+                       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
                        clearArea(0);\r
                }\r
                if (do_it) {\r
@@ -519,8 +557,8 @@ static void RunEvents(unsigned int which)
 \r
 static void updateKeys(void)\r
 {\r
-       unsigned long keys, allActions[2] = { 0, 0 }, events;\r
-       static unsigned long prevEvents = 0;\r
+       unsigned int keys, keys2, allActions[2] = { 0, 0 }, events;\r
+       static unsigned int prevEvents = 0;\r
        int joy, i;\r
 \r
        keys = gp2x_joystick_read(0);\r
@@ -531,10 +569,11 @@ static void updateKeys(void)
        }\r
 \r
        keys &= CONFIGURABLE_KEYS;\r
+       keys2 = keys;\r
 \r
        for (i = 0; i < 32; i++)\r
        {\r
-               if (keys & (1 << i))\r
+               if (keys2 & (1 << i))\r
                {\r
                        int pl, acts = currentConfig.KeyBinds[i];\r
                        if (!acts) continue;\r
@@ -545,9 +584,9 @@ static void updateKeys(void)
                                // let's try to find the other one\r
                                if (acts_c) {\r
                                        for (; u < 32; u++)\r
-                                               if ( (keys & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {\r
+                                               if ( (keys2 & (1 << u)) && (currentConfig.KeyBinds[u] & acts_c) ) {\r
                                                        allActions[pl] |= acts_c & currentConfig.KeyBinds[u];\r
-                                                       keys &= ~((1 << i) | (1 << u));\r
+                                                       keys2 &= ~((1 << i) | (1 << u));\r
                                                        break;\r
                                                }\r
                                }\r
@@ -565,9 +604,9 @@ static void updateKeys(void)
        {\r
                gp2x_usbjoy_update();\r
                for (joy = 0; joy < num_of_joys; joy++) {\r
-                       int keys = gp2x_usbjoy_check2(joy);\r
+                       int btns = gp2x_usbjoy_check2(joy);\r
                        for (i = 0; i < 32; i++) {\r
-                               if (keys & (1 << i)) {\r
+                               if (btns & (1 << i)) {\r
                                        int acts = currentConfig.JoyBinds[joy][i];\r
                                        int pl = (acts >> 16) & 1;\r
                                        allActions[pl] |= acts;\r
@@ -589,6 +628,10 @@ static void updateKeys(void)
                change_fast_forward(events & 0x40);\r
 \r
        events &= ~prevEvents;\r
+\r
+       if (PicoAHW == PAHW_PICO)\r
+               RunEventsPico(events, keys);\r
+\r
        if (events) RunEvents(events);\r
        if (movie_data) emu_updateMovie();\r
 \r
@@ -625,8 +668,7 @@ void emu_forcedFrame(void)
 \r
        //vidResetMode();\r
        PicoDrawSetColorFormat(1);\r
-       PicoScan = EmuScan16;\r
-       PicoScan(0, 0);\r
+       PicoScanBegin = EmuScanBegin16;\r
        Pico.m.dirtyPal = 1;\r
        PicoFrameDrawOnly();\r
 \r
@@ -782,7 +824,7 @@ void emu_Loop(void)
        }\r
 \r
        // prepare CD buffer\r
-       if (PicoMCD & 1) PicoCDBufferInit();\r
+       if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r
 \r
        // calc vsync offset to sync timing code with vsync\r
        if (currentConfig.EmuOpt&0x2000) {\r
@@ -865,9 +907,10 @@ void emu_Loop(void)
                        bench_fps += frames_shown;\r
                        sprintf(fpsbuff, "%02i/%02i/%02i", frames_shown, bench_fps_s, (bf[0]+bf[1]+bf[2]+bf[3])>>2);\r
 #else\r
-                       if (currentConfig.EmuOpt & 2)\r
+                       if (currentConfig.EmuOpt & 2) {\r
                                sprintf(fpsbuff, "%02i/%02i", frames_shown, frames_done);\r
-                       if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r
+                               if (fpsbuff[5] == 0) { fpsbuff[5] = fpsbuff[6] = ' '; fpsbuff[7] = 0; }\r
+                       }\r
 #endif\r
                        frames_shown = frames_done = 0;\r
                        thissec = tval.tv_sec;\r
@@ -959,7 +1002,7 @@ void emu_Loop(void)
 \r
        change_fast_forward(0);\r
 \r
-       if (PicoMCD & 1) PicoCDBufferFree();\r
+       if (PicoAHW & PAHW_MCD) PicoCDBufferFree();\r
 \r
        // save SRAM\r
        if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
@@ -976,7 +1019,7 @@ void emu_Loop(void)
 \r
 void emu_ResetGame(void)\r
 {\r
-       PicoReset(0);\r
+       PicoReset();\r
        reset_timing = 1;\r
 }\r
 \r