more unification; replace some magic bits to defines
[libpicofe.git] / gp2x / emu.c
index 46dc3aa..f27b285 100644 (file)
@@ -18,7 +18,6 @@
 #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 "version.h"\r
 \r
@@ -42,6 +41,7 @@ static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
 static struct timeval noticeMsgTime = { 0, 0 };        // when started showing\r
 static int osd_fps_x;\r
 static int gp2x_old_gamma = 100;\r
+static char noticeMsg[40];\r
 unsigned char *PicoDraw2FB = NULL;  // temporary buffer for alt renderer\r
 int reset_timing = 0;\r
 \r
@@ -49,12 +49,17 @@ int reset_timing = 0;
 #define PICO_PEN_ADJUST_Y 2\r
 static int pico_pen_x = 320/2, pico_pen_y = 240/2;\r
 \r
-static void emu_msg_cb(const char *msg);\r
 static void emu_msg_tray_open(void);\r
 \r
 \r
-void emu_noticeMsgUpdated(void)\r
+void plat_status_msg(const char *format, ...)\r
 {\r
+       va_list vl;\r
+\r
+       va_start(vl, format);\r
+       vsnprintf(noticeMsg, sizeof(noticeMsg), format, vl);\r
+       va_end(vl);\r
+\r
        gettimeofday(&noticeMsgTime, 0);\r
 }\r
 \r
@@ -89,7 +94,7 @@ void emu_Init(void)
        mkdir("cfg", 0777);\r
 \r
        PicoInit();\r
-       PicoMessage = emu_msg_cb;\r
+       PicoMessage = plat_status_msg_busy_next;\r
        PicoMCDopenTray = emu_msg_tray_open;\r
        PicoMCDcloseTray = menu_loop_tray;\r
 }\r
@@ -182,12 +187,10 @@ static void osd_text(int x, int y, const char *text)
 \r
 static void draw_cd_leds(void)\r
 {\r
-//     static\r
        int old_reg;\r
-//     if (!((Pico_mcd->s68k_regs[0] ^ old_reg) & 3)) return; // no change // mmu hack problems?\r
        old_reg = Pico_mcd->s68k_regs[0];\r
 \r
-       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
+       if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP)) {\r
                // 8-bit modes\r
                unsigned int col_g = (old_reg & 2) ? 0xc0c0c0c0 : 0xe0e0e0e0;\r
                unsigned int col_r = (old_reg & 1) ? 0xd0d0d0d0 : 0xe0e0e0e0;\r
@@ -250,7 +253,7 @@ static void blit(const char *fps, const char *notice)
 {\r
        int emu_opt = currentConfig.EmuOpt;\r
 \r
-       if (PicoOpt&0x10)\r
+       if (PicoOpt & POPT_ALT_RENDERER)\r
        {\r
                // 8bit fast renderer\r
                if (Pico.m.dirtyPal) {\r
@@ -265,7 +268,7 @@ static void blit(const char *fps, const char *notice)
                // do actual copy\r
                vidCpyM2((unsigned char *)g_screen_ptr+320*8, PicoDraw2FB+328*8);\r
        }\r
-       else if (!(emu_opt&0x80))\r
+       else if (!(emu_opt & EOPT_16BPP))\r
        {\r
                // 8bit accurate renderer\r
                if (Pico.m.dirtyPal)\r
@@ -313,23 +316,20 @@ static void blit(const char *fps, const char *notice)
        if (PicoAHW & PAHW_PICO)\r
                draw_pico_ptr();\r
 \r
-       //gp2x_video_wait_vsync();\r
        gp2x_video_flip();\r
 \r
-       if (!(PicoOpt&0x10)) {\r
+       if (!(PicoOpt & POPT_ALT_RENDERER)) {\r
                if (!(Pico.video.reg[1]&8)) {\r
-                       if (currentConfig.EmuOpt&0x80) {\r
+                       if (currentConfig.EmuOpt & EOPT_16BPP)\r
                                DrawLineDest = (unsigned short *) g_screen_ptr + 320*8;\r
-                       } else {\r
+                       else\r
                                DrawLineDest = (unsigned char  *) g_screen_ptr + 320*8;\r
-                       }\r
                } else {\r
                        DrawLineDest = g_screen_ptr;\r
                }\r
        }\r
 }\r
 \r
-\r
 // clears whole screen or just the notice area (in all buffers)\r
 static void clearArea(int full)\r
 {\r
@@ -344,12 +344,27 @@ static void clearArea(int full)
        }\r
 }\r
 \r
+void plat_status_msg_busy_next(const char *msg)\r
+{\r
+       clearArea(0);\r
+       blit("", msg);\r
+\r
+       /* assumption: msg_busy_next gets called only when\r
+        * something slow is about to happen */\r
+       reset_timing = 1;\r
+}\r
+\r
+void plat_status_msg_busy_first(const char *msg)\r
+{\r
+       gp2x_memcpy_all_buffers(g_screen_ptr, 0, 320*240*2);\r
+       plat_status_msg_busy_next(msg);\r
+}\r
 \r
 static void vidResetMode(void)\r
 {\r
-       if (PicoOpt&0x10) {\r
+       if (PicoOpt & POPT_ALT_RENDERER) {\r
                gp2x_video_changemode(8);\r
-       } else if (currentConfig.EmuOpt&0x80) {\r
+       } else if (currentConfig.EmuOpt & EOPT_16BPP) {\r
                gp2x_video_changemode(16);\r
                PicoDrawSetColorFormat(1);\r
                PicoScanBegin = EmuScanBegin16;\r
@@ -358,7 +373,7 @@ static void vidResetMode(void)
                PicoDrawSetColorFormat(2);\r
                PicoScanBegin = EmuScanBegin8;\r
        }\r
-       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
+       if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP)) {\r
                // setup pal for 8-bit modes\r
                localPal[0xc0] = 0x0000c000; // MCD LEDs\r
                localPal[0xd0] = 0x00c00000;\r
@@ -375,37 +390,30 @@ static void vidResetMode(void)
        else gp2x_video_RGB_setscaling(0, (PicoOpt&0x100)&&!(Pico.video.reg[12]&1) ? 256 : 320, 240);\r
 }\r
 \r
-\r
-static void emu_msg_cb(const char *msg)\r
+void plat_video_toggle_renderer(void)\r
 {\r
-       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
-               // 8-bit renderers\r
-               gp2x_memset_all_buffers(320*232, 0xe0, 320*8);\r
-               osd_text(4, 232, msg);\r
-               gp2x_memcpy_all_buffers((char *)g_screen_ptr+320*232, 320*232, 320*8);\r
-       } else {\r
-               // 16bit accurate renderer\r
-               gp2x_memset_all_buffers(320*232*2, 0, 320*8*2);\r
-               osd_text(4, 232, msg);\r
-               gp2x_memcpy_all_buffers((char *)g_screen_ptr+320*232*2, 320*232*2, 320*8*2);\r
-       }\r
-       gettimeofday(&noticeMsgTime, 0);\r
-       noticeMsgTime.tv_sec -= 2;\r
+       if (PicoOpt & POPT_ALT_RENDERER) {\r
+               PicoOpt &= ~POPT_ALT_RENDERER;\r
+               currentConfig.EmuOpt |= EOPT_16BPP;\r
+       } else if (!(currentConfig.EmuOpt & EOPT_16BPP))\r
+               PicoOpt |= POPT_ALT_RENDERER;\r
+       else\r
+               currentConfig.EmuOpt &= ~EOPT_16BPP;\r
 \r
-       /* assumption: emu_msg_cb gets called only when something slow is about to happen */\r
-       reset_timing = 1;\r
-}\r
+       vidResetMode();\r
 \r
-static void emu_state_cb(const char *str)\r
-{\r
-       clearArea(0);\r
-       blit("", str);\r
+       if (PicoOpt & POPT_ALT_RENDERER) {\r
+               plat_status_msg(" 8bit fast renderer");\r
+       } else if (currentConfig.EmuOpt & EOPT_16BPP) {\r
+               plat_status_msg("16bit accurate renderer");\r
+       } else {\r
+               plat_status_msg(" 8bit accurate renderer");\r
+       }\r
 }\r
 \r
 static void emu_msg_tray_open(void)\r
 {\r
-       strcpy(noticeMsg, "CD tray opened");\r
-       gettimeofday(&noticeMsgTime, 0);\r
+       plat_status_msg("CD tray opened");\r
 }\r
 \r
 static void RunEventsPico(unsigned int events)\r
@@ -460,14 +468,20 @@ static void RunEventsPico(unsigned int events)
        PicoPicohw.pen_pos[1] = pico_inp_mode == 1 ? (0x2f8 + pico_pen_y) : (0x1fc + pico_pen_y);\r
 }\r
 \r
-static void update_volume(int has_changed, int is_up)\r
+void plat_update_volume(int has_changed, int is_up)\r
 {\r
        static int prev_frame = 0, wait_frames = 0;\r
        int vol = currentConfig.volume;\r
+       int need_low_volume = 0;\r
+       gp2x_soc_t soc;\r
+\r
+       soc = soc_detect();\r
+       if ((PicoOpt & POPT_EN_STEREO) && soc == SOCID_MMSP2)\r
+               need_low_volume = 1;\r
 \r
        if (has_changed)\r
        {\r
-               if (vol < 5 && (PicoOpt&8) && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r
+               if (need_low_volume && vol < 5 && prev_frame == Pico.m.frame_count - 1 && wait_frames < 12)\r
                        wait_frames++;\r
                else {\r
                        if (is_up) {\r
@@ -479,13 +493,14 @@ static void update_volume(int has_changed, int is_up)
                        sndout_oss_setvol(vol, vol);\r
                        currentConfig.volume = vol;\r
                }\r
-               sprintf(noticeMsg, "VOL: %02i", vol);\r
-               gettimeofday(&noticeMsgTime, 0);\r
+               plat_status_msg("VOL: %02i", vol);\r
                prev_frame = Pico.m.frame_count;\r
        }\r
 \r
-       // set the right mixer func\r
-       if (!(PicoOpt&8)) return; // just use defaults for mono\r
+       if (need_low_volume)\r
+               return;\r
+\r
+       /* set the right mixer func */\r
        if (vol >= 5)\r
                PsndMix_32_to_16l = mix_32_to_16l_stereo;\r
        else {\r
@@ -494,120 +509,6 @@ static void update_volume(int has_changed, int is_up)
        }\r
 }\r
 \r
-static void RunEvents(unsigned int which)\r
-{\r
-       if (which & 0x1800) // save or load (but not both)\r
-       {\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
-               {\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
-                       osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r
-                       PicoStateProgressCB = emu_state_cb;\r
-                       gp2x_memcpy_all_buffers(g_screen_ptr, 0, 320*240*2);\r
-                       emu_SaveLoadGame((which & 0x1000) >> 12, 0);\r
-                       PicoStateProgressCB = NULL;\r
-               }\r
-\r
-               reset_timing = 1;\r
-       }\r
-       if (which & 0x0400) // switch renderer\r
-       {\r
-               if      (  PicoOpt&0x10)             { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }\r
-               else if (!(currentConfig.EmuOpt&0x80)) PicoOpt|= 0x10;\r
-               else   currentConfig.EmuOpt &= ~0x80;\r
-\r
-               vidResetMode();\r
-\r
-               if (PicoOpt&0x10) {\r
-                       strcpy(noticeMsg, " 8bit fast renderer");\r
-               } else if (currentConfig.EmuOpt&0x80) {\r
-                       strcpy(noticeMsg, "16bit accurate renderer");\r
-               } else {\r
-                       strcpy(noticeMsg, " 8bit accurate renderer");\r
-               }\r
-\r
-               emu_noticeMsgUpdated();\r
-       }\r
-       if (which & 0x0300)\r
-       {\r
-               if(which&0x0200) {\r
-                       state_slot -= 1;\r
-                       if(state_slot < 0) state_slot = 9;\r
-               } else {\r
-                       state_slot += 1;\r
-                       if(state_slot > 9) state_slot = 0;\r
-               }\r
-               sprintf(noticeMsg, "SAVE SLOT %i [%s]", state_slot, emu_checkSaveFile(state_slot) ? "USED" : "FREE");\r
-               emu_noticeMsgUpdated();\r
-       }\r
-       if (which & 0x0080) {\r
-               engineState = PGS_Menu;\r
-       }\r
-}\r
-\r
-static void updateKeys(void)\r
-{\r
-       unsigned int allActions[2] = { 0, 0 }, events;\r
-       static unsigned int prevEvents = 0;\r
-\r
-       /* FIXME: player2 */\r
-       allActions[0] = in_update();\r
-\r
-       PicoPad[0] = allActions[0] & 0xfff;\r
-       PicoPad[1] = allActions[1] & 0xfff;\r
-\r
-       if (allActions[0] & 0x7000) emu_DoTurbo(&PicoPad[0], allActions[0]);\r
-       if (allActions[1] & 0x7000) emu_DoTurbo(&PicoPad[1], allActions[1]);\r
-\r
-       events = (allActions[0] | allActions[1]) >> 16;\r
-\r
-       // volume is treated in special way and triggered every frame\r
-       if (events & 0x6000)\r
-               update_volume(1, events & 0x2000);\r
-\r
-       if ((events ^ prevEvents) & 0x40) {\r
-               emu_changeFastForward(events & 0x40);\r
-               update_volume(0, 0);\r
-               reset_timing = 1;\r
-       }\r
-\r
-       events &= ~prevEvents;\r
-\r
-       if (PicoAHW == PAHW_PICO)\r
-               RunEventsPico(events);\r
-       if (events) RunEvents(events);\r
-       if (movie_data) emu_updateMovie();\r
-\r
-       prevEvents = (allActions[0] | allActions[1]) >> 16;\r
-}\r
-\r
 \r
 static void updateSound(int len)\r
 {\r
@@ -639,7 +540,7 @@ void emu_startSound(void)
                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
+               plat_update_volume(0, 0);\r
                memset(sndBuffer, 0, sizeof(sndBuffer));\r
                PsndOut = sndBuffer;\r
                PsndRate_old = PsndRate;\r
@@ -931,8 +832,8 @@ void emu_Loop(void)
                        } else {\r
                                // it is quite common for this implementation to leave 1 fame unfinished\r
                                // when second changes, but we don't want buffer to starve.\r
-                               if(PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {\r
-                                       updateKeys();\r
+                               if (PsndOut && pframes_done < target_fps && pframes_done > target_fps-5) {\r
+                                       emu_update_input();\r
                                        SkipFrame(1); pframes_done++;\r
                                }\r
 \r
@@ -947,7 +848,7 @@ void emu_Loop(void)
                if (currentConfig.Frameskip >= 0) // frameskip enabled\r
                {\r
                        for(i = 0; i < currentConfig.Frameskip; i++) {\r
-                               updateKeys();\r
+                               emu_update_input();\r
                                SkipFrame(1); pframes_done++; frames_done++;\r
                                if (PsndOut && !reset_timing) { // do framelimitting if sound is enabled\r
                                        gettimeofday(&tval, 0);\r
@@ -968,12 +869,12 @@ void emu_Loop(void)
                                reset_timing = 1;\r
                                continue;\r
                        }\r
-                       updateKeys();\r
+                       emu_update_input();\r
                        SkipFrame(tval.tv_usec < lim_time+target_frametime*2); pframes_done++; frames_done++;\r
                        continue;\r
                }\r
 \r
-               updateKeys();\r
+               emu_update_input();\r
                PicoFrame();\r
 \r
                // check time\r
@@ -1007,17 +908,18 @@ void emu_Loop(void)
 \r
        emu_changeFastForward(0);\r
 \r
-       if (PicoAHW & PAHW_MCD) PicoCDBufferFree();\r
+       if (PicoAHW & PAHW_MCD)\r
+               PicoCDBufferFree();\r
 \r
        // save SRAM\r
-       if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
-               emu_state_cb("Writing SRAM/BRAM..");\r
+       if ((currentConfig.EmuOpt & EOPT_USE_SRAM) && SRam.changed) {\r
+               plat_status_msg_busy_first("Writing SRAM/BRAM...");\r
                emu_SaveLoadGame(0, 1);\r
                SRam.changed = 0;\r
        }\r
 \r
        // if in 8bit mode, generate 16bit image for menu background\r
-       if ((PicoOpt&0x10) || !(currentConfig.EmuOpt&0x80))\r
+       if ((PicoOpt & POPT_ALT_RENDERER) || !(currentConfig.EmuOpt & EOPT_16BPP))\r
                emu_forcedFrame(POPT_EN_SOFTSCALE);\r
 }\r
 \r