port to 64bit. Some gcc 4.4 warning fixes
[libpicofe.git] / common / emu.c
index a8de902..e916b3f 100644 (file)
@@ -41,9 +41,11 @@ int pico_pen_x = 320/2, pico_pen_y = 240/2;
 int pico_inp_mode = 0;\r
 int engineState = PGS_Menu;\r
 \r
+/* tmp buff to reduce stack usage for plats with small stack */\r
+static char static_buff[512];\r
 /* TODO: len checking */\r
-char rom_fname_reload[512] = { 0, };\r
-char rom_fname_loaded[512] = { 0, };\r
+char rom_fname_reload[512];\r
+char rom_fname_loaded[512];\r
 int rom_loaded = 0;\r
 int reset_timing = 0;\r
 static unsigned int notice_msg_time;   /* when started showing */\r
@@ -113,7 +115,6 @@ static const char * const biosfiles_jp[] = { "jp_mcd1_9112", "jp_mcd1_9111" };
 \r
 static int find_bios(int region, char **bios_file)\r
 {\r
-       static char bios_path[1024];\r
        int i, count;\r
        const char * const *files;\r
        FILE *f = NULL;\r
@@ -133,26 +134,27 @@ static int find_bios(int region, char **bios_file)
 \r
        for (i = 0; i < count; i++)\r
        {\r
-               emu_make_path(bios_path, files[i], sizeof(bios_path) - 4);\r
-               strcat(bios_path, ".bin");\r
-               f = fopen(bios_path, "rb");\r
+               emu_make_path(static_buff, files[i], sizeof(static_buff) - 4);\r
+               strcat(static_buff, ".bin");\r
+               f = fopen(static_buff, "rb");\r
                if (f) break;\r
 \r
-               bios_path[strlen(bios_path) - 4] = 0;\r
-               strcat(bios_path, ".zip");\r
-               f = fopen(bios_path, "rb");\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
-               lprintf("using bios: %s\n", bios_path);\r
+               lprintf("using bios: %s\n", static_buff);\r
                fclose(f);\r
-               if (bios_file) *bios_file = bios_path;\r
+               if (bios_file)\r
+                       *bios_file = static_buff;\r
                return 1;\r
        } else {\r
-               sprintf(bios_path, "no %s BIOS files found, read docs",\r
+               sprintf(static_buff, "no %s BIOS files found, read docs",\r
                        region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");\r
-               me_update_msg(bios_path);\r
+               me_update_msg(static_buff);\r
                return 0;\r
        }\r
 }\r
@@ -462,25 +464,25 @@ int emu_reload_rom(char *rom_fname)
                // check for both gmv and rom\r
                int dummy;\r
                FILE *movie_file = fopen(rom_fname, "rb");\r
-               if(!movie_file) {\r
+               if (!movie_file) {\r
                        me_update_msg("Failed to open movie.");\r
                        return 0;\r
                }\r
                fseek(movie_file, 0, SEEK_END);\r
                movie_size = ftell(movie_file);\r
                fseek(movie_file, 0, SEEK_SET);\r
-               if(movie_size < 64+3) {\r
+               if (movie_size < 64+3) {\r
                        me_update_msg("Invalid GMV file.");\r
                        fclose(movie_file);\r
                        return 0;\r
                }\r
                movie_data = malloc(movie_size);\r
-               if(movie_data == NULL) {\r
+               if (movie_data == NULL) {\r
                        me_update_msg("low memory.");\r
                        fclose(movie_file);\r
                        return 0;\r
                }\r
-               fread(movie_data, 1, movie_size, movie_file);\r
+               dummy = fread(movie_data, 1, movie_size, movie_file);\r
                fclose(movie_file);\r
                if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {\r
                        me_update_msg("Invalid GMV file.");\r
@@ -560,6 +562,7 @@ int emu_reload_rom(char *rom_fname)
        }\r
 \r
        menu_romload_prepare(used_rom_name); // also CD load\r
+       used_rom_name = NULL; // uses static_buff\r
 \r
        ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoAHW & PAHW_SMS) ? 1 : 0);\r
        pm_close(rom);\r
@@ -594,7 +597,8 @@ int emu_reload_rom(char *rom_fname)
                if (!ret) emu_read_config(0, 0);\r
        }\r
 \r
-       if (PicoCartInsert(rom_data, rom_size)) {\r
+       emu_make_path(static_buff, "carthw.cfg", sizeof(static_buff));\r
+       if (PicoCartInsert(rom_data, rom_size, static_buff)) {\r
                me_update_msg("Failed to load ROM.");\r
                goto fail;\r
        }\r
@@ -700,6 +704,7 @@ static void romfname_ext(char *dst, const char *prefix, const char *ext)
        if (ext) strcat(dst, ext);\r
 }\r
 \r
+// <base dir><end>\r
 void emu_make_path(char *buff, const char *end, int size)\r
 {\r
        int pos, end_len;\r
@@ -907,7 +912,7 @@ static int try_ropen_file(const char *fname)
 \r
 char *emu_get_save_fname(int load, int is_sram, int slot)\r
 {\r
-       static char saveFname[512];\r
+       char *saveFname = static_buff;\r
        char ext[16];\r
 \r
        if (is_sram)\r
@@ -1003,13 +1008,16 @@ int emu_save_load_game(int load, int sram)
                        sram_size = SRam.size;\r
                        sram_data = SRam.data;\r
                }\r
-               if (!sram_data) return 0; // SRam forcefully disabled for this game\r
+               if (sram_data == NULL)\r
+                       return 0; // SRam forcefully disabled for this game\r
 \r
                if (load)\r
                {\r
                        sramFile = fopen(saveFname, "rb");\r
-                       if(!sramFile) return -1;\r
-                       fread(sram_data, 1, sram_size, sramFile);\r
+                       if (!sramFile)\r
+                               return -1;\r
+                       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
@@ -1482,7 +1490,7 @@ void emu_loop(void)
                                pframes_done++; frames_done++;\r
                                diff_lim += target_frametime;\r
 \r
-                               if (!(currentConfig.EmuOpt & EOPT_NO_FRMLIMIT)) {\r
+                               if (!(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT))) {\r
                                        timestamp = get_ticks();\r
                                        diff = timestamp - timestamp_base;\r
                                        if (!reset_timing && diff < diff_lim) // we are too fast\r
@@ -1508,7 +1516,7 @@ void emu_loop(void)
                PicoFrame();\r
 \r
                /* frame limiter */\r
-               if (!reset_timing && !(currentConfig.EmuOpt & EOPT_NO_FRMLIMIT))\r
+               if (!reset_timing && !(currentConfig.EmuOpt & (EOPT_NO_FRMLIMIT|EOPT_EXT_FRMLIMIT)))\r
                {\r
                        timestamp = get_ticks();\r
                        diff = timestamp - timestamp_base;\r