move emu loop to common; redo timing; add pollux timer
[libpicofe.git] / pandora / emu.c
index 16fc7cd..a9a2119 100644 (file)
@@ -1,4 +1,4 @@
-// (c) Copyright 2006-2008 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
@@ -6,12 +6,6 @@
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #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 "../common/arm_utils.h"\r
 #endif\r
 \r
 \r
-int select_exits = 0;\r
-\r
 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
-static struct timeval noticeMsgTime = { 0, 0 };        // when started showing\r
 static int osd_fps_x;\r
 unsigned char *PicoDraw2FB = NULL;  // temporary buffer for alt renderer\r
-int reset_timing = 0;\r
 \r
 #define PICO_PEN_ADJUST_X 4\r
 #define PICO_PEN_ADJUST_Y 2\r
 static int pico_pen_x = 0, 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
-{\r
-       gettimeofday(&noticeMsgTime, 0);\r
-}\r
 \r
-int emu_getMainDir(char *dst, int len)\r
+int plat_get_root_dir(char *dst, int len)\r
 {\r
        extern char **g_argv;\r
        int j;\r
@@ -75,49 +57,7 @@ int emu_getMainDir(char *dst, int len)
        return j + 1;\r
 }\r
 \r
-void emu_Init(void)\r
-{\r
-       // make temp buffer for alt renderer\r
-       PicoDraw2FB = malloc((8+320)*(8+240+8));\r
-       if (!PicoDraw2FB)\r
-       {\r
-               printf("PicoDraw2FB == 0\n");\r
-       }\r
-\r
-       // make dirs for saves, cfgs, etc.\r
-       mkdir("mds", 0777);\r
-       mkdir("srm", 0777);\r
-       mkdir("brm", 0777);\r
-       mkdir("cfg", 0777);\r
-\r
-       PicoInit();\r
-       PicoMessage = emu_msg_cb;\r
-       PicoMCDopenTray = emu_msg_tray_open;\r
-       PicoMCDcloseTray = menu_loop_tray;\r
-}\r
-\r
-\r
-void emu_Deinit(void)\r
-{\r
-       // save SRAM\r
-       if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
-               emu_SaveLoadGame(0, 1);\r
-               SRam.changed = 0;\r
-       }\r
-\r
-       if (!(currentConfig.EmuOpt & 0x20)) {\r
-               config_writelrom(PicoConfigFile);\r
-#ifndef NO_SYNC\r
-               sync();\r
-#endif\r
-       }\r
-\r
-       free(PicoDraw2FB);\r
-\r
-       PicoExit();\r
-}\r
-\r
-void emu_prepareDefaultConfig(void)\r
+void pemu_prep_defconfig(void)\r
 {\r
        memset(&defaultConfig, 0, sizeof(defaultConfig));\r
        defaultConfig.EmuOpt    = 0x8f | 0x00600; // | <- confirm_save, cd_leds\r
@@ -170,7 +110,7 @@ static void textOut16(int x, int y, const char *text)
 }\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
 \r
@@ -182,7 +122,7 @@ void osd_text(int x, int y, const char *text)
                        p = (int *) ((unsigned char *) g_screen_ptr+x+g_screen_width*(y+h));\r
                        for (i = len; i; i--, p++) *p = 0xe0e0e0e0;\r
                }\r
-               emu_textOut8(x, y, text);\r
+               emu_text_out8(x, y, text);\r
        } else {\r
                int *p, i, h;\r
                x &= ~1; // align x\r
@@ -191,7 +131,7 @@ void osd_text(int x, int y, const char *text)
                        p = (int *) ((unsigned short *) g_screen_ptr+x+g_screen_width*(y+h));\r
                        for (i = len; i; i--, p++) *p = 0;//(*p>>2)&0x39e7;\r
                }\r
-               textOut16(x, y, text);\r
+               text_out16(x, y, text);\r
        }\r
 }\r
 \r
@@ -427,95 +367,6 @@ static void vidResetMode(void)
 }\r
 \r
 \r
-static void emu_msg_cb(const char *msg)\r
-{\r
-/*\r
-       if ((PicoOpt&0x10)||!(currentConfig.EmuOpt&0x80)) {\r
-               // 8-bit renderers\r
-               gp2x_memset_all_buffers(g_screen_width*(g_screen_height-16), 0xe0, g_screen_width*16);\r
-               osd_text(4, g_screen_height-16, msg);\r
-               gp2x_memcpy_all_buffers((char *)g_screen_ptr+g_screen_width*(g_screen_height-16),\r
-                       g_screen_width*(g_screen_height-16), g_screen_width*16);\r
-       } else {\r
-               // 16bit accurate renderer\r
-               gp2x_memset_all_buffers(g_screen_width*(g_screen_height-16)*2, 0, g_screen_width*16*2);\r
-               osd_text(4, g_screen_height-16, msg);\r
-               gp2x_memcpy_all_buffers((char *)g_screen_ptr+g_screen_width*(g_screen_height-16)*2,\r
-                       g_screen_width*(g_screen_height-16)*2, g_screen_width*16*2);\r
-       }\r
-       gettimeofday(&noticeMsgTime, 0);\r
-       noticeMsgTime.tv_sec -= 2;\r
-*/\r
-       /* assumption: emu_msg_cb gets called only when something slow is about to happen */\r
-       reset_timing = 1;\r
-}\r
-\r
-static void emu_state_cb(const char *str)\r
-{\r
-       clearArea(0);\r
-       blit("", str);\r
-}\r
-\r
-static void emu_msg_tray_open(void)\r
-{\r
-       strcpy(noticeMsg, "CD tray opened");\r
-       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
-       static int pdown_frames = 0;\r
-\r
-       emu_RunEventsPico(events);\r
-\r
-       if (pico_inp_mode == 0) return;\r
-\r
-       // for F200\r
-       ret = gp2x_touchpad_read(&px, &py);\r
-       if (ret >= 0)\r
-       {\r
-               if (ret > 35000)\r
-               {\r
-                       if (pdown_frames++ > 5)\r
-                               PicoPad[0] |= 0x20;\r
-\r
-                       pico_pen_x = px;\r
-                       pico_pen_y = py;\r
-                       if (!(Pico.video.reg[12]&1)) {\r
-                               pico_pen_x -= 32;\r
-                               if (pico_pen_x <   0) pico_pen_x = 0;\r
-                               if (pico_pen_x > 248) pico_pen_x = 248;\r
-                       }\r
-                       if (pico_pen_y > 224) pico_pen_y = 224;\r
-               }\r
-               else\r
-                       pdown_frames = 0;\r
-\r
-               //if (ret == 0)\r
-               //      PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r
-       }\r
-\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
-       if (pico_pen_y > 224-PICO_PEN_ADJUST_Y) pico_pen_y = 224-PICO_PEN_ADJUST_Y;\r
-       if (pico_pen_x < 0) pico_pen_x = 0;\r
-       if (pico_pen_x > lim_x-PICO_PEN_ADJUST_X) pico_pen_x = lim_x-PICO_PEN_ADJUST_X;\r
-\r
-       PicoPicohw.pen_pos[0] = pico_pen_x;\r
-       if (!(Pico.video.reg[12]&1)) PicoPicohw.pen_pos[0] += pico_pen_x/4;\r
-       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
        static int prev_frame = 0, wait_frames = 0;\r
@@ -550,108 +401,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
-#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
-                               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
-#endif\r
-               }\r
-               if (do_it) {\r
-                       osd_text(4, g_screen_height-16, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");\r
-                       PicoStateProgressCB = emu_state_cb;\r
-                       //gp2x_memcpy_all_buffers(g_screen_ptr, 0, g_screen_width*g_screen_height*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
-               gettimeofday(&noticeMsgTime, 0);\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
-               gettimeofday(&noticeMsgTime, 0);\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: combos, 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
-/*\r
-       if (PicoAHW == PAHW_PICO)\r
-               RunEventsPico(events, keys);\r
-*/\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
@@ -671,7 +420,7 @@ static void SkipFrame(int do_audio)
 }\r
 \r
 \r
-void emu_forcedFrame(int opts)\r
+void pemu_forced_frame(int opts)\r
 {\r
        int po_old = PicoOpt;\r
        int eo_old = currentConfig.EmuOpt;\r
@@ -705,9 +454,8 @@ void emu_forcedFrame(int opts)
        currentConfig.EmuOpt = eo_old;\r
 }\r
 \r
-void emu_platformDebugCat(char *str)\r
+void plat_debug_cat(char *str)\r
 {\r
-       // nothing\r
 }\r
 \r
 static void simpleWait(int thissec, int lim_time)\r
@@ -726,7 +474,7 @@ static void simpleWait(int thissec, int lim_time)
        }\r
 }\r
 \r
-void emu_startSound(void)\r
+void pemu_sound_start(void)\r
 {\r
        static int PsndRate_old = 0, PicoOpt_old = 0, pal_old = 0;\r
        int target_fps = Pico.m.pal ? 50 : 60;\r
@@ -754,17 +502,16 @@ void emu_startSound(void)
        }\r
 }\r
 \r
-void emu_endSound(void)\r
+void pemu_sound_stop(void)\r
 {\r
 }\r
 \r
-/* wait until we can write more sound */\r
-void emu_waitSound(void)\r
+void pemu_sound_wait(void)\r
 {\r
        // don't need to do anything, writes will block by themselves\r
 }\r
 \r
-void emu_Loop(void)\r
+void pemu_loop(void)\r
 {\r
        char fpsbuff[24]; // fps count c string\r
        struct timeval tval; // timing\r
@@ -787,7 +534,7 @@ void emu_Loop(void)
        target_frametime = 1000000/target_fps;\r
        reset_timing = 1;\r
 \r
-       emu_startSound();\r
+       pemu_sound_start();\r
 \r
        // prepare CD buffer\r
        if (PicoAHW & PAHW_MCD) PicoCDBufferInit();\r
@@ -894,7 +641,7 @@ void emu_Loop(void)
                                // 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
+                                       emu_update_input();\r
                                        SkipFrame(1); pframes_done++;\r
                                }\r
 \r
@@ -909,7 +656,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
@@ -930,12 +677,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
@@ -968,26 +715,20 @@ void emu_Loop(void)
                 frames_done++;  frames_shown++;\r
        }\r
 \r
-       emu_changeFastForward(0);\r
+       emu_set_fastforward(0);\r
 \r
        if (PicoAHW & PAHW_MCD) PicoCDBufferFree();\r
 \r
        // save SRAM\r
-       if((currentConfig.EmuOpt & 1) && SRam.changed) {\r
+       if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r
+               /* FIXME: plat_status_msg_busy_first */\r
                emu_state_cb("Writing SRAM/BRAM..");\r
-               emu_SaveLoadGame(0, 1);\r
+               emu_save_load_game(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
-               emu_forcedFrame(POPT_EN_SOFTSCALE);\r
-}\r
-\r
-\r
-void emu_ResetGame(void)\r
-{\r
-       PicoReset();\r
-       reset_timing = 1;\r
+               pemu_forced_frame(POPT_EN_SOFTSCALE);\r
 }\r
 \r