further unification and refactoring
[picodrive.git] / platform / common / emu.c
index ff17e3b..2535abe 100644 (file)
@@ -16,6 +16,7 @@
 #include "config.h"\r
 #include "plat.h"\r
 #include "input.h"\r
+#include "posix.h"\r
 \r
 #include <pico/pico_int.h>\r
 #include <pico/patch.h>\r
@@ -34,6 +35,7 @@ char *PicoConfigFile = "config.cfg";
 currentConfig_t currentConfig, defaultConfig;\r
 int state_slot = 0;\r
 int config_slot = 0, config_slot_current = 0;\r
+int pico_pen_x = 320/2, pico_pen_y = 240/2;\r
 int pico_inp_mode = 0;\r
 int engineState = PGS_Menu;\r
 \r
@@ -41,6 +43,7 @@ int engineState = PGS_Menu;
 char rom_fname_reload[512] = { 0, };\r
 char rom_fname_loaded[512] = { 0, };\r
 int rom_loaded = 0;\r
+int reset_timing = 0;\r
 \r
 unsigned char *movie_data = NULL;\r
 static int movie_size = 0;\r
@@ -109,7 +112,7 @@ int emu_findBios(int region, char **bios_file)
 \r
        for (i = 0; i < count; i++)\r
        {\r
-               emu_getMainDir(bios_path, sizeof(bios_path));\r
+               plat_get_root_dir(bios_path, sizeof(bios_path));\r
                strcat(bios_path, files[i]);\r
                strcat(bios_path, ".bin");\r
                f = fopen(bios_path, "rb");\r
@@ -519,7 +522,7 @@ static void romfname_ext(char *dst, const char *prefix, const char *ext)
        for (; p >= rom_fname_loaded && *p != PATH_SEP_C; p--); p++;\r
        *dst = 0;\r
        if (prefix) {\r
-               int len = emu_getMainDir(dst, 512);\r
+               int len = plat_get_root_dir(dst, 512);\r
                strcpy(dst + len, prefix);\r
                prefix_len = len + strlen(prefix);\r
        }\r
@@ -536,7 +539,7 @@ static void romfname_ext(char *dst, const char *prefix, const char *ext)
 static void make_config_cfg(char *cfg)\r
 {\r
        int len;\r
-       len = emu_getMainDir(cfg, 512);\r
+       len = plat_get_root_dir(cfg, 512);\r
        strncpy(cfg + len, PicoConfigFile, 512-6-1-len);\r
        if (config_slot != 0)\r
        {\r
@@ -957,9 +960,22 @@ void emu_changeFastForward(int set_on)
        }\r
 }\r
 \r
-void emu_RunEventsPico(unsigned int events)\r
+static void emu_msg_tray_open(void)\r
 {\r
-       if (events & (1 << 3)) {\r
+       plat_status_msg("CD tray opened");\r
+}\r
+\r
+void emu_reset_game(void)\r
+{\r
+       PicoReset();\r
+       reset_timing = 1;\r
+}\r
+\r
+void run_events_pico(unsigned int events)\r
+{\r
+       int lim_x;\r
+\r
+       if (events & PEV_PICO_SWINP) {\r
                pico_inp_mode++;\r
                if (pico_inp_mode > 2)\r
                        pico_inp_mode = 0;\r
@@ -971,18 +987,44 @@ void emu_RunEventsPico(unsigned int events)
                                break;\r
                }\r
        }\r
-       if (events & (1 << 4)) {\r
+       if (events & PEV_PICO_PPREV) {\r
                PicoPicohw.page--;\r
                if (PicoPicohw.page < 0)\r
                        PicoPicohw.page = 0;\r
                plat_status_msg("Page %i", PicoPicohw.page);\r
        }\r
-       if (events & (1 << 5)) {\r
+       if (events & PEV_PICO_PNEXT) {\r
                PicoPicohw.page++;\r
                if (PicoPicohw.page > 6)\r
                        PicoPicohw.page = 6;\r
                plat_status_msg("Page %i", PicoPicohw.page);\r
        }\r
+\r
+       if (pico_inp_mode == 0)\r
+               return;\r
+\r
+       /* handle other input modes */\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
+\r
+       lim_x = (Pico.video.reg[12]&1) ? 319 : 255;\r
+       if (pico_pen_y < 8)\r
+               pico_pen_y = 8;\r
+       if (pico_pen_y > 224 - PICO_PEN_ADJUST_Y)\r
+               pico_pen_y = 224 - PICO_PEN_ADJUST_Y;\r
+       if (pico_pen_x < 0)\r
+               pico_pen_x = 0;\r
+       if (pico_pen_x > lim_x - PICO_PEN_ADJUST_X)\r
+               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))\r
+               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
 \r
 static void do_turbo(int *pad, int acts)\r
@@ -1009,7 +1051,7 @@ static void do_turbo(int *pad, int acts)
        *pad |= turbo_pad & (acts >> 8);\r
 }\r
 \r
-static void run_ui_events(unsigned int which)\r
+static void run_events_ui(unsigned int which)\r
 {\r
        if (which & (PEV_STATE_LOAD|PEV_STATE_SAVE))\r
        {\r
@@ -1096,18 +1138,58 @@ void emu_update_input(void)
        if ((events ^ prevEvents) & PEV_FF) {\r
                emu_changeFastForward(events & PEV_FF);\r
                plat_update_volume(0, 0);\r
-//             reset_timing = 1;\r
+               reset_timing = 1;\r
        }\r
 \r
        events &= ~prevEvents;\r
 \r
-// TODO        if (PicoAHW == PAHW_PICO)\r
-//             RunEventsPico(events);\r
+       if (PicoAHW == PAHW_PICO)\r
+               run_events_pico(events);\r
        if (events)\r
-               run_ui_events(events);\r
+               run_events_ui(events);\r
        if (movie_data)\r
                update_movie();\r
 \r
        prevEvents = (allActions[0] | allActions[1]) & PEV_MASK;\r
 }\r
 \r
+static void mkdir_path(char *path_with_reserve, int pos, const char *name)\r
+{\r
+       strcpy(path_with_reserve + pos, name);\r
+       if (plat_is_dir(path_with_reserve))\r
+               return;\r
+       if (mkdir(path_with_reserve, 0777) < 0)\r
+               lprintf("failed to create: %s\n", path_with_reserve);\r
+}\r
+\r
+void emu_init(void)\r
+{\r
+       char dir[256];\r
+       int pos;\r
+\r
+       /* make dirs for saves */\r
+       pos = plat_get_root_dir(dir, sizeof(dir) - 4);\r
+       mkdir_path(dir, pos, "mds");\r
+       mkdir_path(dir, pos, "srm");\r
+       mkdir_path(dir, pos, "brm");\r
+\r
+       PicoInit();\r
+       PicoMessage = plat_status_msg_busy_next;\r
+       PicoMCDopenTray = emu_msg_tray_open;\r
+       PicoMCDcloseTray = menu_loop_tray;\r
+}\r
+\r
+void emu_finish(void)\r
+{\r
+       // save SRAM\r
+       if ((currentConfig.EmuOpt & EOPT_USE_SRAM) && SRam.changed) {\r
+               emu_SaveLoadGame(0, 1);\r
+               SRam.changed = 0;\r
+       }\r
+\r
+       if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG))\r
+               emu_writelrom();\r
+\r
+       PicoExit();\r
+}\r
+\r