platform ps2, handle audio similar to psp
[picodrive.git] / platform / common / emu.c
index 6a8c940..051f8b4 100644 (file)
@@ -1,6 +1,7 @@
 /*\r
  * PicoDrive\r
  * (C) notaz, 2007-2010\r
+ * (C) irixxxx, 2019-2024\r
  *\r
  * This work is licensed under the terms of MAME license.\r
  * See COPYING file in the top-level directory.\r
@@ -19,6 +20,7 @@
 #include "../libpicofe/fonts.h"\r
 #include "../libpicofe/sndout.h"\r
 #include "../libpicofe/lprintf.h"\r
+#include "../libpicofe/readpng.h"\r
 #include "../libpicofe/plat.h"\r
 #include "emu.h"\r
 #include "input_pico.h"\r
 #include <pico/pico_int.h>\r
 #include <pico/patch.h>\r
 \r
+#if defined(__GNUC__) && __GNUC__ >= 7\r
+#pragma GCC diagnostic ignored "-Wformat-truncation"\r
+#endif\r
+\r
 #ifndef _WIN32\r
 #define PATH_SEP      "/"\r
 #define PATH_SEP_C    '/'\r
@@ -42,6 +48,7 @@ void *g_screen_ptr;
 \r
 int g_screen_width  = 320;\r
 int g_screen_height = 240;\r
+int g_screen_ppitch = 320; // pitch in pixels\r
 \r
 const char *PicoConfigFile = "config2.cfg";\r
 currentConfig_t currentConfig, defaultConfig;\r
@@ -52,7 +59,7 @@ int pico_inp_mode;
 int flip_after_sync;\r
 int engineState = PGS_Menu;\r
 \r
-static short __attribute__((aligned(4))) sndBuffer[2*44100/50];\r
+static short __attribute__((aligned(4))) sndBuffer[2*54000/50];\r
 \r
 /* tmp buff to reduce stack usage for plats with small stack */\r
 static char static_buff[512];\r
@@ -122,8 +129,8 @@ static void fname_ext(char *dst, int dstlen, const char *prefix, const char *ext
        strncpy(dst + prefix_len, p, dstlen - prefix_len - 1);\r
 \r
        dst[dstlen - 8] = 0;\r
-       if (dst[strlen(dst) - 4] == '.')\r
-               dst[strlen(dst) - 4] = 0;\r
+       if ((p = strrchr(dst, '.')) != NULL)\r
+               dst[p-dst] = 0;\r
        if (ext)\r
                strcat(dst, ext);\r
 }\r
@@ -171,12 +178,27 @@ static const char *find_bios(int *region, const char *cd_fname)
        ret = emu_read_config(cd_fname, 0);\r
        if (!ret) emu_read_config(NULL, 0);\r
 \r
-       if (PicoRegionOverride) {\r
-               *region = PicoRegionOverride;\r
+       if (PicoIn.regionOverride) {\r
+               *region = PicoIn.regionOverride;\r
                lprintf("override region to %s\n", *region != 4 ?\r
                        (*region == 8 ? "EU" : "JAP") : "USA");\r
        }\r
 \r
+       // look for MSU.MD rom file. XXX another extension list? ugh...\r
+       static const char *md_exts[] = { "gen", "smd", "md", "32x" };\r
+       char *ext = strrchr(cd_fname, '.');\r
+       int extpos = ext ? ext-cd_fname : strlen(cd_fname);\r
+       strcpy(static_buff, cd_fname);\r
+       static_buff[extpos++] = '.';\r
+       for (i = 0; i < ARRAY_SIZE(md_exts); i++) {\r
+               strcpy(static_buff+extpos, md_exts[i]);\r
+               if (access(static_buff, R_OK) == 0) {\r
+                       printf("found MSU rom: %s\n",static_buff);\r
+                       return static_buff;\r
+               }\r
+       }\r
+\r
+       // locate BIOS file\r
        if (*region == 4) { // US\r
                files = biosfiles_us;\r
                count = sizeof(biosfiles_us) / sizeof(char *);\r
@@ -201,6 +223,16 @@ static const char *find_bios(int *region, const char *cd_fname)
                strcat(static_buff, ".zip");\r
                f = fopen(static_buff, "rb");\r
                if (f) break;\r
+\r
+               strcpy(static_buff, files[i]);\r
+               strcat(static_buff, ".bin");\r
+               f = fopen(static_buff, "rb");\r
+               if (f) break;\r
+\r
+               static_buff[strlen(static_buff) - 4] = 0;\r
+               strcat(static_buff, ".zip");\r
+               f = fopen(static_buff, "rb");\r
+               if (f) break;\r
        }\r
 \r
        if (f) {\r
@@ -266,16 +298,16 @@ static char *emu_make_rom_id(const char *fname)
        static char id_string[3+0xe*3+0x3*3+0x30*3+3];\r
        int pos, swab = 1;\r
 \r
-       if (PicoAHW & PAHW_MCD) {\r
+       if (PicoIn.AHW & PAHW_MCD) {\r
                strcpy(id_string, "CD|");\r
                swab = 0;\r
        }\r
-       else if (PicoAHW & PAHW_SMS)\r
+       else if (PicoIn.AHW & PAHW_SMS)\r
                strcpy(id_string, "MS|");\r
        else    strcpy(id_string, "MD|");\r
        pos = 3;\r
 \r
-       if (!(PicoAHW & PAHW_SMS)) {\r
+       if (!(PicoIn.AHW & PAHW_SMS)) {\r
                pos += extract_text(id_string + pos, media_id_header + 0x80, 0x0e, swab); // serial\r
                id_string[pos] = '|'; pos++;\r
                pos += extract_text(id_string + pos, media_id_header + 0xf0, 0x03, swab); // region\r
@@ -296,7 +328,7 @@ static char *emu_make_rom_id(const char *fname)
 // buffer must be at least 150 byte long\r
 void emu_get_game_name(char *str150)\r
 {\r
-       int ret, swab = (PicoAHW & PAHW_MCD) ? 0 : 1;\r
+       int ret, swab = (PicoIn.AHW & PAHW_MCD) ? 0 : 1;\r
        char *s, *d;\r
 \r
        ret = extract_text(str150, media_id_header + 0x50, 0x30, swab); // overseas name\r
@@ -315,25 +347,33 @@ static void system_announce(void)
        const char *sys_name, *tv_standard, *extra = "";\r
        int fps;\r
 \r
-       if (PicoAHW & PAHW_SMS) {\r
+       if (PicoIn.AHW & PAHW_SMS) {\r
                sys_name = "Master System";\r
+               if (PicoIn.AHW & PAHW_GG)\r
+                       sys_name = "Game Gear";\r
+               else if (PicoIn.AHW & PAHW_SG)\r
+                       sys_name = "SG-1000";\r
+               else if (PicoIn.AHW & PAHW_SC)\r
+                       sys_name = "SC-3000";\r
+               else if (Pico.m.hardware & PMS_HW_JAP)\r
+                       sys_name = "Mark III";\r
 #ifdef NO_SMS\r
                extra = " [no support]";\r
 #endif\r
-       } else if (PicoAHW & PAHW_PICO) {\r
+       } else if (PicoIn.AHW & PAHW_PICO) {\r
                sys_name = "Pico";\r
-       } else if ((PicoAHW & (PAHW_32X|PAHW_MCD)) == (PAHW_32X|PAHW_MCD)) {\r
+       } else if ((PicoIn.AHW & (PAHW_32X|PAHW_MCD)) == (PAHW_32X|PAHW_MCD)) {\r
                sys_name = "32X + Mega CD";\r
                if ((Pico.m.hardware & 0xc0) == 0x80)\r
                        sys_name = "32X + Sega CD";\r
-       } else if (PicoAHW & PAHW_MCD) {\r
+       } else if (PicoIn.AHW & PAHW_MCD) {\r
                sys_name = "Mega CD";\r
                if ((Pico.m.hardware & 0xc0) == 0x80)\r
                        sys_name = "Sega CD";\r
-       } else if (PicoAHW & PAHW_32X) {\r
+       } else if (PicoIn.AHW & PAHW_32X) {\r
                sys_name = "32X";\r
        } else {\r
-               sys_name = "MegaDrive";\r
+               sys_name = "Mega Drive";\r
                if ((Pico.m.hardware & 0xc0) == 0x80)\r
                        sys_name = "Genesis";\r
        }\r
@@ -376,7 +416,7 @@ int emu_reload_rom(const char *rom_fname_in)
                movie_data = 0;\r
        }\r
 \r
-       if (!strcmp(ext, ".gmv"))\r
+       if (!strcasecmp(ext, ".gmv"))\r
        {\r
                // check for both gmv and rom\r
                int dummy;\r
@@ -413,7 +453,7 @@ int emu_reload_rom(const char *rom_fname_in)
                get_ext(rom_fname, ext);\r
                lprintf("gmv loaded for %s\n", rom_fname);\r
        }\r
-       else if (!strcmp(ext, ".pat"))\r
+       else if (!strcasecmp(ext, ".pat"))\r
        {\r
                int dummy;\r
                PicoPatchLoad(rom_fname);\r
@@ -430,7 +470,7 @@ int emu_reload_rom(const char *rom_fname_in)
 \r
        emu_make_path(carthw_path, "carthw.cfg", sizeof(carthw_path));\r
 \r
-       media_type = PicoLoadMedia(rom_fname, carthw_path,\r
+       media_type = PicoLoadMedia(rom_fname, NULL, 0, carthw_path,\r
                        find_bios, do_region_override);\r
 \r
        switch (media_type) {\r
@@ -451,7 +491,7 @@ int emu_reload_rom(const char *rom_fname_in)
        }\r
 \r
        // make quirks visible in UI\r
-       if (PicoQuirks & PQUIRK_FORCE_6BTN)\r
+       if (PicoIn.quirks & PQUIRK_FORCE_6BTN)\r
                currentConfig.input_dev0 = PICO_INPUT_PAD_6BTN;\r
 \r
        menu_romload_end();\r
@@ -470,12 +510,12 @@ int emu_reload_rom(const char *rom_fname_in)
                PicoSetInputDevice(0, indev);\r
                PicoSetInputDevice(1, indev);\r
 \r
-               PicoOpt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing\r
+               PicoIn.opt |= POPT_DIS_VDP_FIFO; // no VDP fifo timing\r
                if (movie_data[0xF] >= 'A') {\r
                        if (movie_data[0x16] & 0x80) {\r
-                               PicoRegionOverride = 8;\r
+                               PicoIn.regionOverride = 8;\r
                        } else {\r
-                               PicoRegionOverride = 4;\r
+                               PicoIn.regionOverride = 4;\r
                        }\r
                        PicoReset();\r
                        // TODO: bits 6 & 5\r
@@ -485,8 +525,11 @@ int emu_reload_rom(const char *rom_fname_in)
        }\r
        else\r
        {\r
+               PicoSetInputDevice(0, currentConfig.input_dev0);\r
+               PicoSetInputDevice(1, currentConfig.input_dev1);\r
+\r
                system_announce();\r
-               PicoOpt &= ~POPT_DIS_VDP_FIFO;\r
+               PicoIn.opt &= ~POPT_DIS_VDP_FIFO;\r
        }\r
 \r
        strncpy(rom_fname_loaded, rom_fname, sizeof(rom_fname_loaded)-1);\r
@@ -530,11 +573,11 @@ out:
 \r
 int emu_swap_cd(const char *fname)\r
 {\r
-       enum cd_img_type cd_type;\r
+       enum cd_track_type cd_type;\r
        int ret = -1;\r
 \r
        cd_type = PicoCdCheck(fname, NULL);\r
-       if (cd_type != CIT_NOT_CD)\r
+       if (cd_type != CT_UNKNOWN)\r
                ret = cdd_load(fname, cd_type);\r
        if (ret != 0) {\r
                menu_update_msg("Load failed, invalid CD image?");\r
@@ -576,15 +619,19 @@ static void make_config_cfg(char *cfg_buff_512)
 void emu_prep_defconfig(void)\r
 {\r
        memset(&defaultConfig, 0, sizeof(defaultConfig));\r
-       defaultConfig.EmuOpt    = 0x9d | EOPT_EN_CD_LEDS;\r
-       defaultConfig.s_PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 |\r
+       defaultConfig.EmuOpt    = EOPT_EN_SRAM | EOPT_EN_SOUND | EOPT_16BPP |\r
+                                 EOPT_EN_CD_LEDS | EOPT_GZIP_SAVES | EOPT_PICO_PEN;\r
+       defaultConfig.s_PicoOpt = POPT_EN_SNDFILTER|POPT_EN_GG_LCD|POPT_EN_YM2413 |\r
+                                 POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 |\r
                                  POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX |\r
                                  POPT_EN_DRC|POPT_ACC_SPRITES |\r
                                  POPT_EN_32X|POPT_EN_PWM;\r
        defaultConfig.s_PsndRate = 44100;\r
        defaultConfig.s_PicoRegion = 0; // auto\r
        defaultConfig.s_PicoAutoRgnOrder = 0x184; // US, EU, JP\r
+       defaultConfig.s_hwSelect = PHWS_AUTO;\r
        defaultConfig.s_PicoCDBuffers = 0;\r
+       defaultConfig.s_PicoSndFilterAlpha = 0x10000 * 60 / 100;\r
        defaultConfig.confirm_save = EOPT_CONFIRM_SAVE;\r
        defaultConfig.Frameskip = -1; // auto\r
        defaultConfig.input_dev0 = PICO_INPUT_PAD_3BTN;\r
@@ -595,6 +642,7 @@ void emu_prep_defconfig(void)
        defaultConfig.turbo_rate = 15;\r
        defaultConfig.msh2_khz = PICO_MSH2_HZ / 1000;\r
        defaultConfig.ssh2_khz = PICO_SSH2_HZ / 1000;\r
+       defaultConfig.max_skip = 4;\r
 \r
        // platform specific overrides\r
        pemu_prep_defconfig();\r
@@ -603,10 +651,12 @@ void emu_prep_defconfig(void)
 void emu_set_defconfig(void)\r
 {\r
        memcpy(&currentConfig, &defaultConfig, sizeof(currentConfig));\r
-       PicoOpt = currentConfig.s_PicoOpt;\r
-       PsndRate = currentConfig.s_PsndRate;\r
-       PicoRegionOverride = currentConfig.s_PicoRegion;\r
-       PicoAutoRgnOrder = currentConfig.s_PicoAutoRgnOrder;\r
+       PicoIn.opt = currentConfig.s_PicoOpt;\r
+       PicoIn.sndRate = currentConfig.s_PsndRate;\r
+       PicoIn.regionOverride = currentConfig.s_PicoRegion;\r
+       PicoIn.autoRgnOrder = currentConfig.s_PicoAutoRgnOrder;\r
+       PicoIn.hwSelect = currentConfig.s_hwSelect;\r
+       PicoIn.sndFilterAlpha = currentConfig.s_PicoSndFilterAlpha;\r
 }\r
 \r
 int emu_read_config(const char *rom_fname, int no_defaults)\r
@@ -652,14 +702,9 @@ int emu_read_config(const char *rom_fname, int no_defaults)
        }\r
 \r
        pemu_validate_config();\r
+       PicoIn.overclockM68k = currentConfig.overclock_68k;\r
 \r
        // some sanity checks\r
-#ifdef PSP\r
-       /* TODO: mv to plat_validate_config() */\r
-       if (currentConfig.CPUclock < 10 || currentConfig.CPUclock > 4096) currentConfig.CPUclock = 200;\r
-       if (currentConfig.gamma < -4 || currentConfig.gamma >  16) currentConfig.gamma = 0;\r
-       if (currentConfig.gamma2 < 0 || currentConfig.gamma2 > 2)  currentConfig.gamma2 = 0;\r
-#endif\r
        if (currentConfig.volume < 0 || currentConfig.volume > 99)\r
                currentConfig.volume = 50;\r
 \r
@@ -729,12 +774,12 @@ void name(int x, int y, const char *text)                         \
        }                                                               \\r
 }\r
 \r
-mk_text_out(emu_text_out8,      unsigned char,    0xf0, g_screen_ptr, 1, g_screen_width)\r
-mk_text_out(emu_text_out16,     unsigned short, 0xffff, g_screen_ptr, 1, g_screen_width)\r
+mk_text_out(emu_text_out8,      unsigned char,    0xf0, g_screen_ptr, 1, g_screen_ppitch)\r
+mk_text_out(emu_text_out16,     unsigned short, 0xffff, g_screen_ptr, 1, g_screen_ppitch)\r
 mk_text_out(emu_text_out8_rot,  unsigned char,    0xf0,\r
-       (char *)g_screen_ptr  + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)\r
+       (char *)g_screen_ptr  + (g_screen_ppitch - 1) * g_screen_height, -g_screen_height, 1)\r
 mk_text_out(emu_text_out16_rot, unsigned short, 0xffff,\r
-       (short *)g_screen_ptr + (g_screen_width - 1) * g_screen_height, -g_screen_height, 1)\r
+       (short *)g_screen_ptr + (g_screen_ppitch - 1) * g_screen_height, -g_screen_height, 1)\r
 \r
 #undef mk_text_out\r
 \r
@@ -750,7 +795,7 @@ void emu_osd_text16(int x, int y, const char *text)
        for (h = 0; h < 8; h++) {\r
                unsigned short *p;\r
                p = (unsigned short *)g_screen_ptr\r
-                       + x + g_screen_width * (y + h);\r
+                       + x + g_screen_ppitch * (y + h);\r
                for (i = len; i > 0; i--, p++)\r
                        *p = (*p >> 2) & 0x39e7;\r
        }\r
@@ -767,20 +812,20 @@ static void update_movie(void)
                lprintf("END OF MOVIE.\n");\r
        } else {\r
                // MXYZ SACB RLDU\r
-               PicoPad[0] = ~movie_data[offs]   & 0x8f; // ! SCBA RLDU\r
-               if(!(movie_data[offs]   & 0x10)) PicoPad[0] |= 0x40; // C\r
-               if(!(movie_data[offs]   & 0x20)) PicoPad[0] |= 0x10; // A\r
-               if(!(movie_data[offs]   & 0x40)) PicoPad[0] |= 0x20; // B\r
-               PicoPad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU\r
-               if(!(movie_data[offs+1] & 0x10)) PicoPad[1] |= 0x40; // C\r
-               if(!(movie_data[offs+1] & 0x20)) PicoPad[1] |= 0x10; // A\r
-               if(!(movie_data[offs+1] & 0x40)) PicoPad[1] |= 0x20; // B\r
-               PicoPad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX\r
-               if(!(movie_data[offs+2] & 0x01)) PicoPad[0] |= 0x0400; // X\r
-               if(!(movie_data[offs+2] & 0x04)) PicoPad[0] |= 0x0100; // Z\r
-               PicoPad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX\r
-               if(!(movie_data[offs+2] & 0x10)) PicoPad[1] |= 0x0400; // X\r
-               if(!(movie_data[offs+2] & 0x40)) PicoPad[1] |= 0x0100; // Z\r
+               PicoIn.pad[0] = ~movie_data[offs]   & 0x8f; // ! SCBA RLDU\r
+               if(!(movie_data[offs]   & 0x10)) PicoIn.pad[0] |= 0x40; // C\r
+               if(!(movie_data[offs]   & 0x20)) PicoIn.pad[0] |= 0x10; // A\r
+               if(!(movie_data[offs]   & 0x40)) PicoIn.pad[0] |= 0x20; // B\r
+               PicoIn.pad[1] = ~movie_data[offs+1] & 0x8f; // ! SCBA RLDU\r
+               if(!(movie_data[offs+1] & 0x10)) PicoIn.pad[1] |= 0x40; // C\r
+               if(!(movie_data[offs+1] & 0x20)) PicoIn.pad[1] |= 0x10; // A\r
+               if(!(movie_data[offs+1] & 0x40)) PicoIn.pad[1] |= 0x20; // B\r
+               PicoIn.pad[0] |= (~movie_data[offs+2] & 0x0A) << 8; // ! MZYX\r
+               if(!(movie_data[offs+2] & 0x01)) PicoIn.pad[0] |= 0x0400; // X\r
+               if(!(movie_data[offs+2] & 0x04)) PicoIn.pad[0] |= 0x0100; // Z\r
+               PicoIn.pad[1] |= (~movie_data[offs+2] & 0xA0) << 4; // ! MZYX\r
+               if(!(movie_data[offs+2] & 0x10)) PicoIn.pad[1] |= 0x0400; // X\r
+               if(!(movie_data[offs+2] & 0x40)) PicoIn.pad[1] |= 0x0100; // Z\r
        }\r
 }\r
 \r
@@ -809,9 +854,9 @@ char *emu_get_save_fname(int load, int is_sram, int slot, int *time)
 \r
        if (is_sram)\r
        {\r
-               strcpy(ext, (PicoAHW & PAHW_MCD) ? ".brm" : ".srm");\r
+               strcpy(ext, (PicoIn.AHW & PAHW_MCD) ? ".brm" : ".srm");\r
                romfname_ext(saveFname, sizeof(static_buff),\r
-                       (PicoAHW & PAHW_MCD) ? "brm"PATH_SEP : "srm"PATH_SEP, ext);\r
+                       (PicoIn.AHW & PAHW_MCD) ? "brm"PATH_SEP : "srm"PATH_SEP, ext);\r
                if (!load)\r
                        return saveFname;\r
 \r
@@ -885,24 +930,24 @@ int emu_save_load_game(int load, int sram)
                int sram_size;\r
                unsigned char *sram_data;\r
                int truncate = 1;\r
-               if (PicoAHW & PAHW_MCD)\r
+               if (PicoIn.AHW & PAHW_MCD)\r
                {\r
-                       if (PicoOpt & POPT_EN_MCD_RAMCART) {\r
+                       if (PicoIn.opt & POPT_EN_MCD_RAMCART) {\r
                                sram_size = 0x12000;\r
-                               sram_data = SRam.data;\r
+                               sram_data = Pico.sv.data;\r
                                if (sram_data)\r
-                                       memcpy32((int *)sram_data, (int *)Pico_mcd->bram, 0x2000/4);\r
+                                       memcpy(sram_data, Pico_mcd->bram, 0x2000);\r
                        } else {\r
                                sram_size = 0x2000;\r
                                sram_data = Pico_mcd->bram;\r
                                truncate  = 0; // the .brm may contain RAM cart data after normal brm\r
                        }\r
                } else {\r
-                       sram_size = SRam.size;\r
-                       sram_data = SRam.data;\r
+                       sram_size = Pico.sv.size;\r
+                       sram_data = Pico.sv.data;\r
                }\r
                if (sram_data == NULL)\r
-                       return 0; // SRam forcefully disabled for this game\r
+                       return 0; // cart saves forcefully disabled for this game\r
 \r
                if (load)\r
                {\r
@@ -912,8 +957,8 @@ int emu_save_load_game(int load, int sram)
                        ret = fread(sram_data, 1, sram_size, sramFile);\r
                        ret = ret > 0 ? 0 : -1;\r
                        fclose(sramFile);\r
-                       if ((PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_RAMCART))\r
-                               memcpy32((int *)Pico_mcd->bram, (int *)sram_data, 0x2000/4);\r
+                       if ((PicoIn.AHW & PAHW_MCD) && (PicoIn.opt&POPT_EN_MCD_RAMCART))\r
+                               memcpy(Pico_mcd->bram, sram_data, 0x2000);\r
                } else {\r
                        // sram save needs some special processing\r
                        // see if we have anything to save\r
@@ -957,25 +1002,22 @@ void emu_set_fastforward(int set_on)
        static int set_Frameskip, set_EmuOpt, is_on = 0;\r
 \r
        if (set_on && !is_on) {\r
-               set_PsndOut = PsndOut;\r
+               set_PsndOut = PicoIn.sndOut;\r
                set_Frameskip = currentConfig.Frameskip;\r
                set_EmuOpt = currentConfig.EmuOpt;\r
-               PsndOut = NULL;\r
+               PicoIn.sndOut = NULL;\r
                currentConfig.Frameskip = 8;\r
-               currentConfig.EmuOpt &= ~4;\r
-               currentConfig.EmuOpt |= 0x40000;\r
+               currentConfig.EmuOpt &= ~EOPT_EN_SOUND;\r
+               currentConfig.EmuOpt |= EOPT_NO_FRMLIMIT;\r
                is_on = 1;\r
                emu_status_msg("FAST FORWARD");\r
        }\r
        else if (!set_on && is_on) {\r
-               PsndOut = set_PsndOut;\r
+               PicoIn.sndOut = set_PsndOut;\r
                currentConfig.Frameskip = set_Frameskip;\r
                currentConfig.EmuOpt = set_EmuOpt;\r
                PsndRerate(1);\r
                is_on = 0;\r
-               // mainly to unbreak pcm\r
-               if (PicoAHW & PAHW_MCD)\r
-                       pcd_state_loaded();\r
        }\r
 }\r
 \r
@@ -1001,22 +1043,76 @@ void emu_reset_game(void)
        reset_timing = 1;\r
 }\r
 \r
-void run_events_pico(unsigned int events)\r
-{\r
-       int lim_x;\r
+static int pico_page;\r
+static int pico_w, pico_h;\r
+static u16 *pico_overlay;\r
 \r
-       if (events & PEV_PICO_SWINP) {\r
-               pico_inp_mode++;\r
-               if (pico_inp_mode > 2)\r
-                       pico_inp_mode = 0;\r
-               switch (pico_inp_mode) {\r
-                       case 2: emu_status_msg("Input: Pen on Pad"); break;\r
-                       case 1: emu_status_msg("Input: Pen on Storyware"); break;\r
-                       case 0: emu_status_msg("Input: Joystick");\r
-                               PicoPicohw.pen_pos[0] = PicoPicohw.pen_pos[1] = 0x8000;\r
-                               break;\r
+static u16 *load_pico_overlay(int page, int w, int h)\r
+{\r
+       static const char *pic_exts[] = { "png", "PNG" };\r
+       char *ext, *fname = NULL;\r
+       int extpos, i;\r
+\r
+       if (pico_page == page && pico_w == w && pico_h == h)\r
+               return pico_overlay;\r
+       pico_page = page;\r
+       pico_w = w, pico_h = h;\r
+\r
+       ext = strrchr(rom_fname_loaded, '.');\r
+       extpos = ext ? ext-rom_fname_loaded : strlen(rom_fname_loaded);\r
+       strcpy(static_buff, rom_fname_loaded);\r
+       static_buff[extpos++] = '_';\r
+       if (page < 0) {\r
+               static_buff[extpos++] = 'p';\r
+               static_buff[extpos++] = 'a';\r
+               static_buff[extpos++] = 'd';\r
+       } else\r
+               static_buff[extpos++] = '0'+PicoPicohw.page;\r
+       static_buff[extpos++] = '.';\r
+\r
+       for (i = 0; i < ARRAY_SIZE(pic_exts); i++) {\r
+               strcpy(static_buff+extpos, pic_exts[i]);\r
+               if (access(static_buff, R_OK) == 0) {\r
+                       printf("found Pico file: %s\n", static_buff);\r
+                       fname = static_buff;\r
+                       break;\r
                }\r
        }\r
+\r
+       pico_overlay = realloc(pico_overlay, w*h*2);\r
+       memset(pico_overlay, 0, w*h*2);\r
+       if (!fname || !pico_overlay || readpng(pico_overlay, fname, READPNG_SCALE, w, h)) {\r
+               if (pico_overlay)\r
+                       free(pico_overlay);\r
+               pico_overlay = NULL;\r
+       }\r
+\r
+       return pico_overlay;\r
+}\r
+\r
+void emu_pico_overlay(u16 *pd, int w, int h, int pitch)\r
+{\r
+       u16 *overlay = NULL;\r
+       int y, oh = h;\r
+\r
+       // get overlay\r
+       if (pico_inp_mode == 1) {\r
+               oh = (w/2 < h ? w/2 : h); // storyware has squished h\r
+               overlay = load_pico_overlay(PicoPicohw.page, w, oh);\r
+       } else if (pico_inp_mode == 2)\r
+               overlay = load_pico_overlay(-1, w, oh);\r
+\r
+       // copy overlay onto buffer\r
+       if (overlay) {\r
+               for (y = 0; y < oh; y++)\r
+                       memcpy(pd + y*pitch, overlay + y*w, w*2);\r
+               if (y < h)\r
+                       memset(pd + y*pitch, 0, w*2);\r
+       }\r
+}\r
+\r
+void run_events_pico(unsigned int events)\r
+{\r
        if (events & PEV_PICO_PPREV) {\r
                PicoPicohw.page--;\r
                if (PicoPicohw.page < 0)\r
@@ -1029,35 +1125,62 @@ void run_events_pico(unsigned int events)
                        PicoPicohw.page = 6;\r
                emu_status_msg("Page %i", PicoPicohw.page);\r
        }\r
+       if (events & PEV_PICO_STORY) {\r
+               if (pico_inp_mode == 1) {\r
+                       pico_inp_mode = 0;\r
+                       emu_status_msg("Input: D-Pad");\r
+               } else {\r
+                       pico_inp_mode = 1;\r
+                       emu_status_msg("Input: Pen on Storyware");\r
+               }\r
+       }\r
+       if (events & PEV_PICO_PAD) {\r
+               if (pico_inp_mode == 2) {\r
+                       pico_inp_mode = 0;\r
+                       emu_status_msg("Input: D-Pad");\r
+               } else {\r
+                       pico_inp_mode = 2;\r
+                       emu_status_msg("Input: Pen on Pad");\r
+               }\r
+       }\r
+       if (events & PEV_PICO_PENST) {\r
+               PicoPicohw.pen_pos[0] ^= 0x8000;\r
+               PicoPicohw.pen_pos[1] ^= 0x8000;\r
+               emu_status_msg("Pen %s", PicoPicohw.pen_pos[0] & 0x8000 ? "Up" : "Down");\r
+       }\r
 \r
+       if ((currentConfig.EmuOpt & EOPT_PICO_PEN) &&\r
+                       (PicoIn.pad[0]&0x20) && pico_inp_mode && pico_overlay) {\r
+               pico_inp_mode = 0;\r
+               emu_status_msg("Input: D-Pad");\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
+       if (PicoIn.pad[0] & 1) pico_pen_y--;\r
+       if (PicoIn.pad[0] & 2) pico_pen_y++;\r
+       if (PicoIn.pad[0] & 4) pico_pen_x--;\r
+       if (PicoIn.pad[0] & 8) pico_pen_x++;\r
+       PicoIn.pad[0] &= ~0x0f; // release UDLR\r
+\r
+       /* cursor position, cursor drawing must not cross screen borders */\r
+       if (pico_pen_y < PICO_PEN_ADJUST_Y)\r
+               pico_pen_y = PICO_PEN_ADJUST_Y;\r
+       if (pico_pen_y > 223-1 - PICO_PEN_ADJUST_Y)\r
+               pico_pen_y = 223-1 - PICO_PEN_ADJUST_Y;\r
+       if (pico_pen_x < PICO_PEN_ADJUST_X)\r
+               pico_pen_x = PICO_PEN_ADJUST_X;\r
+       if (pico_pen_x > 319-1 - PICO_PEN_ADJUST_X)\r
+               pico_pen_x = 319-1 - PICO_PEN_ADJUST_X;\r
+\r
+       PicoPicohw.pen_pos[0] &= 0x8000;\r
+       PicoPicohw.pen_pos[1] &= 0x8000;\r
+       PicoPicohw.pen_pos[0] |= 0x03c + pico_pen_x;\r
+       PicoPicohw.pen_pos[1] |= (pico_inp_mode == 1 ? 0x2f8 : 0x1fc) + pico_pen_y;\r
 }\r
 \r
-static void do_turbo(int *pad, int acts)\r
+static void do_turbo(unsigned short *pad, int acts)\r
 {\r
        static int turbo_pad = 0;\r
        static unsigned char turbo_cnt[3] = { 0, 0, 0 };\r
@@ -1114,6 +1237,7 @@ static void run_events_ui(unsigned int which)
                        while (in_menu_wait_any(NULL, 50) & (PBTN_MOK | PBTN_MBACK))\r
                                ;\r
                        in_set_config_int(0, IN_CFG_BLOCKING, 0);\r
+                       plat_status_msg_clear();\r
                }\r
                if (do_it) {\r
                        plat_status_msg_busy_first((which & PEV_STATE_LOAD) ? "LOADING STATE" : "SAVING STATE");\r
@@ -1151,21 +1275,29 @@ void emu_update_input(void)
 {\r
        static int prev_events = 0;\r
        int actions[IN_BINDTYPE_COUNT] = { 0, };\r
-       int pl_actions[2];\r
+       int pl_actions[4];\r
        int events;\r
 \r
        in_update(actions);\r
 \r
        pl_actions[0] = actions[IN_BINDTYPE_PLAYER12];\r
        pl_actions[1] = actions[IN_BINDTYPE_PLAYER12] >> 16;\r
+       pl_actions[2] = actions[IN_BINDTYPE_PLAYER34];\r
+       pl_actions[3] = actions[IN_BINDTYPE_PLAYER34] >> 16;\r
 \r
-       PicoPad[0] = pl_actions[0] & 0xfff;\r
-       PicoPad[1] = pl_actions[1] & 0xfff;\r
+       PicoIn.pad[0] = pl_actions[0] & 0xfff;\r
+       PicoIn.pad[1] = pl_actions[1] & 0xfff;\r
+       PicoIn.pad[2] = pl_actions[2] & 0xfff;\r
+       PicoIn.pad[3] = pl_actions[3] & 0xfff;\r
 \r
        if (pl_actions[0] & 0x7000)\r
-               do_turbo(&PicoPad[0], pl_actions[0]);\r
+               do_turbo(&PicoIn.pad[0], pl_actions[0]);\r
        if (pl_actions[1] & 0x7000)\r
-               do_turbo(&PicoPad[1], pl_actions[1]);\r
+               do_turbo(&PicoIn.pad[1], pl_actions[1]);\r
+       if (pl_actions[2] & 0x7000)\r
+               do_turbo(&PicoIn.pad[2], pl_actions[2]);\r
+       if (pl_actions[3] & 0x7000)\r
+               do_turbo(&PicoIn.pad[3], pl_actions[3]);\r
 \r
        events = actions[IN_BINDTYPE_EMU] & PEV_MASK;\r
 \r
@@ -1181,7 +1313,7 @@ void emu_update_input(void)
 \r
        events &= ~prev_events;\r
 \r
-       if (PicoAHW == PAHW_PICO)\r
+       if (PicoIn.AHW == PAHW_PICO)\r
                run_events_pico(events);\r
        if (events)\r
                run_events_ui(events);\r
@@ -1196,29 +1328,33 @@ static void mkdir_path(char *path_with_reserve, int pos, const char *name)
        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
+       if (mkdir(path_with_reserve, 0755) < 0)\r
                lprintf("failed to create: %s\n", path_with_reserve);\r
 }\r
 \r
-void emu_cmn_forced_frame(int no_scale, int do_emu)\r
+void emu_cmn_forced_frame(int no_scale, int do_emu, void *buf)\r
 {\r
-       int po_old = PicoOpt;\r
+       int po_old = PicoIn.opt;\r
+       int y;\r
 \r
-       memset32(g_screen_ptr, 0, g_screen_width * g_screen_height * 2 / 4);\r
+       for (y = 0; y < g_screen_height; y++)\r
+               memset32((short *)g_screen_ptr + g_screen_ppitch * y, 0,\r
+                        g_screen_width * 2 / 4);\r
 \r
-       PicoOpt &= ~POPT_ALT_RENDERER;\r
-       PicoOpt |= POPT_ACC_SPRITES;\r
-       if (!no_scale)\r
-               PicoOpt |= POPT_EN_SOFTSCALE;\r
+       PicoIn.opt &= ~(POPT_ALT_RENDERER|POPT_EN_SOFTSCALE);\r
+       PicoIn.opt |= POPT_ACC_SPRITES;\r
+       if (!no_scale && currentConfig.scaling)\r
+               PicoIn.opt |= POPT_EN_SOFTSCALE;\r
 \r
        PicoDrawSetOutFormat(PDF_RGB555, 1);\r
+       PicoDrawSetOutBuf(buf, g_screen_ppitch * 2);\r
        Pico.m.dirtyPal = 1;\r
        if (do_emu)\r
                PicoFrame();\r
        else\r
                PicoFrameDrawOnly();\r
 \r
-       PicoOpt = po_old;\r
+       PicoIn.opt = po_old;\r
 }\r
 \r
 void emu_init(void)\r
@@ -1252,9 +1388,9 @@ void emu_init(void)
        config_readlrom(path);\r
 \r
        PicoInit();\r
-       PicoMessage = plat_status_msg_busy_next;\r
-       PicoMCDopenTray = emu_tray_open;\r
-       PicoMCDcloseTray = emu_tray_close;\r
+       PicoIn.osdMessage = plat_status_msg_busy_next;\r
+       PicoIn.mcdTrayOpen = emu_tray_open;\r
+       PicoIn.mcdTrayClose = emu_tray_close;\r
 \r
        sndout_init();\r
 }\r
@@ -1262,9 +1398,9 @@ void emu_init(void)
 void emu_finish(void)\r
 {\r
        // save SRAM\r
-       if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r
+       if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && Pico.sv.changed) {\r
                emu_save_load_game(0, 1);\r
-               SRam.changed = 0;\r
+               Pico.sv.changed = 0;\r
        }\r
 \r
        if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG)) {\r
@@ -1284,26 +1420,30 @@ void emu_finish(void)
 \r
 static void snd_write_nonblocking(int len)\r
 {\r
-       sndout_write_nb(PsndOut, len);\r
+       sndout_write_nb(PicoIn.sndOut, len);\r
 }\r
 \r
 void emu_sound_start(void)\r
 {\r
-       PsndOut = NULL;\r
+       PicoIn.sndOut = NULL;\r
 \r
+       // auto-select rate?\r
+       if (PicoIn.sndRate > 52000 && PicoIn.sndRate < 54000)\r
+               PicoIn.sndRate = YM2612_NATIVE_RATE();\r
        if (currentConfig.EmuOpt & EOPT_EN_SOUND)\r
        {\r
-               int is_stereo = (PicoOpt & POPT_EN_STEREO) ? 1 : 0;\r
+               int is_stereo = (PicoIn.opt & POPT_EN_STEREO) ? 1 : 0;\r
 \r
+               memset(sndBuffer, 0, sizeof(sndBuffer));\r
+               PicoIn.sndOut = sndBuffer;\r
                PsndRerate(Pico.m.frame_count ? 1 : 0);\r
 \r
                printf("starting audio: %i len: %i stereo: %i, pal: %i\n",\r
-                       PsndRate, PsndLen, is_stereo, Pico.m.pal);\r
-               sndout_start(PsndRate, is_stereo);\r
-               PicoWriteSound = snd_write_nonblocking;\r
+                       PicoIn.sndRate, Pico.snd.len, is_stereo, Pico.m.pal);\r
+\r
+               sndout_start(PicoIn.sndRate, is_stereo);\r
+               PicoIn.writeSound = snd_write_nonblocking;\r
                plat_update_volume(0, 0);\r
-               memset(sndBuffer, 0, sizeof(sndBuffer));\r
-               PsndOut = sndBuffer;\r
        }\r
 }\r
 \r
@@ -1341,16 +1481,17 @@ static void emu_loop_prep(void)
 }\r
 \r
 /* our tick here is 1 us right now */\r
-#define ms_to_ticks(x) (unsigned int)(x * 1000)\r
-#define get_ticks() plat_get_ticks_us()\r
+#define ms_to_ticks(x) (int)(x * 1000)\r
+#define get_ticks()    plat_get_ticks_us()\r
+#define vsync_delay    ms_to_ticks(1)\r
 \r
 void emu_loop(void)\r
 {\r
        int frames_done, frames_shown;  /* actual frames for fps counter */\r
-       int target_frametime_x3;\r
-       unsigned int timestamp_x3 = 0;\r
-       unsigned int timestamp_aim_x3 = 0;\r
-       unsigned int timestamp_fps_x3 = 0;\r
+       int target_frametime;\r
+       unsigned int timestamp = 0;\r
+       unsigned int timestamp_aim = 0;\r
+       unsigned int timestamp_fps = 0;\r
        char *notice_msg = NULL;\r
        char fpsbuff[24];\r
        int fskip_cnt = 0;\r
@@ -1365,9 +1506,9 @@ void emu_loop(void)
 \r
        /* number of ticks per frame */\r
        if (Pico.m.pal)\r
-               target_frametime_x3 = 3 * ms_to_ticks(1000) / 50;\r
+               target_frametime = ms_to_ticks(1000) / 50;\r
        else\r
-               target_frametime_x3 = 3 * ms_to_ticks(1000) / 60;\r
+               target_frametime = ms_to_ticks(1000) / 60;\r
 \r
        reset_timing = 1;\r
        frames_done = frames_shown = 0;\r
@@ -1383,28 +1524,26 @@ void emu_loop(void)
                if (reset_timing) {\r
                        reset_timing = 0;\r
                        plat_video_wait_vsync();\r
-                       timestamp_aim_x3 = get_ticks() * 3;\r
-                       timestamp_fps_x3 = timestamp_aim_x3;\r
+                       timestamp_aim = get_ticks();\r
+                       timestamp_fps = timestamp_aim;\r
                        fskip_cnt = 0;\r
                }\r
                else if (currentConfig.EmuOpt & EOPT_NO_FRMLIMIT) {\r
-                       timestamp_aim_x3 = get_ticks() * 3;\r
+                       timestamp_aim = get_ticks();\r
                }\r
 \r
-               timestamp_x3 = get_ticks() * 3;\r
+               timestamp = get_ticks();\r
 \r
                // show notice_msg message?\r
                if (notice_msg_time != 0)\r
                {\r
                        static int noticeMsgSum;\r
-                       if (timestamp_x3 - ms_to_ticks(notice_msg_time) * 3\r
-                            > ms_to_ticks(STATUS_MSG_TIMEOUT) * 3)\r
+                       if (timestamp - ms_to_ticks(notice_msg_time)\r
+                            > ms_to_ticks(STATUS_MSG_TIMEOUT))\r
                        {\r
                                notice_msg_time = 0;\r
-                               plat_status_msg_clear();\r
-                               plat_video_flip();\r
-                               plat_status_msg_clear(); /* Do it again in case of double buffering */\r
                                notice_msg = NULL;\r
+                               plat_status_msg_clear();\r
                        }\r
                        else {\r
                                int sum = noticeMsg[0] + noticeMsg[1] + noticeMsg[2];\r
@@ -1417,7 +1556,7 @@ void emu_loop(void)
                }\r
 \r
                // second changed?\r
-               if (timestamp_x3 - timestamp_fps_x3 >= ms_to_ticks(1000) * 3)\r
+               if (timestamp - timestamp_fps >= ms_to_ticks(1000))\r
                {\r
 #ifdef BENCHMARK\r
                        static int bench = 0, bench_fps = 0, bench_fps_s = 0, bfp = 0, bf[4];\r
@@ -1435,13 +1574,13 @@ void emu_loop(void)
                                snprintf(fpsbuff, 8, "%02i/%02i  ", frames_shown, frames_done);\r
 #endif\r
                        frames_shown = frames_done = 0;\r
-                       timestamp_fps_x3 += ms_to_ticks(1000) * 3;\r
+                       timestamp_fps += ms_to_ticks(1000);\r
                }\r
 #ifdef PFRAMES\r
                sprintf(fpsbuff, "%i", Pico.m.frame_count);\r
 #endif\r
 \r
-               diff = timestamp_aim_x3 - timestamp_x3;\r
+               diff = timestamp_aim - timestamp;\r
 \r
                if (currentConfig.Frameskip >= 0) // frameskip enabled (or 0)\r
                {\r
@@ -1453,26 +1592,32 @@ void emu_loop(void)
                                fskip_cnt = 0;\r
                        }\r
                }\r
-               else if (diff < -target_frametime_x3)\r
+               else if (diff < -target_frametime)\r
                {\r
                        /* no time left for this frame - skip */\r
-                       /* limit auto frameskip to 8 */\r
-                       if (frames_done / 8 <= frames_shown)\r
+                       /* limit auto frameskip to max_skip */\r
+                       if (fskip_cnt < currentConfig.max_skip) {\r
+                               fskip_cnt++;\r
                                skip = 1;\r
-               }\r
+                       }\r
+                       else {\r
+                               fskip_cnt = 0;\r
+                       }\r
+               } else\r
+                       fskip_cnt = 0;\r
 \r
                // don't go in debt too much\r
-               while (diff < -target_frametime_x3 * 3) {\r
-                       timestamp_aim_x3 += target_frametime_x3;\r
-                       diff = timestamp_aim_x3 - timestamp_x3;\r
+               while (diff < -target_frametime * 3) {\r
+                       timestamp_aim += target_frametime;\r
+                       diff = timestamp_aim - timestamp;\r
                }\r
 \r
                emu_update_input();\r
                if (skip) {\r
-                       int do_audio = diff > -target_frametime_x3 * 2;\r
-                       PicoSkipFrame = do_audio ? 1 : 2;\r
+                       int do_audio = diff > -target_frametime * 2;\r
+                       PicoIn.skipFrame = do_audio ? 1 : 2;\r
                        PicoFrame();\r
-                       PicoSkipFrame = 0;\r
+                       PicoIn.skipFrame = 0;\r
                }\r
                else {\r
                        PicoFrame();\r
@@ -1480,7 +1625,7 @@ void emu_loop(void)
                        frames_shown++;\r
                }\r
                frames_done++;\r
-               timestamp_aim_x3 += target_frametime_x3;\r
+               timestamp_aim += target_frametime;\r
 \r
                if (!skip && !flip_after_sync)\r
                        plat_video_flip();\r
@@ -1490,18 +1635,18 @@ void emu_loop(void)
                    && !(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT)))\r
                {\r
                        unsigned int timestamp = get_ticks();\r
-                       diff = timestamp_aim_x3 - timestamp * 3;\r
+                       diff = timestamp_aim - timestamp;\r
 \r
                        // sleep or vsync if we are still too fast\r
-                       if (diff > target_frametime_x3 && (currentConfig.EmuOpt & EOPT_VSYNC)) {\r
+                       if (diff > target_frametime + vsync_delay && (currentConfig.EmuOpt & EOPT_VSYNC)) {\r
                                // we are too fast\r
                                plat_video_wait_vsync();\r
                                timestamp = get_ticks();\r
-                               diff = timestamp * 3 - timestamp_aim_x3;\r
+                               diff = timestamp_aim - timestamp;\r
                        }\r
-                       if (diff > target_frametime_x3) {\r
+                       if (diff > target_frametime + vsync_delay) {\r
                                // still too fast\r
-                               plat_wait_till_us(timestamp + (diff - target_frametime_x3) / 3);\r
+                               plat_wait_till_us(timestamp + (diff - target_frametime));\r
                        }\r
                }\r
 \r
@@ -1514,10 +1659,10 @@ void emu_loop(void)
        emu_set_fastforward(0);\r
 \r
        // save SRAM\r
-       if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && SRam.changed) {\r
+       if ((currentConfig.EmuOpt & EOPT_EN_SRAM) && Pico.sv.changed) {\r
                plat_status_msg_busy_first("Writing SRAM/BRAM...");\r
                emu_save_load_game(0, 1);\r
-               SRam.changed = 0;\r
+               Pico.sv.changed = 0;\r
        }\r
 \r
        pemu_loop_end();\r