random fixes (root path and mmap svp tcache)
[libpicofe.git] / common / emu.c
index c5686fd..08efdb2 100644 (file)
@@ -112,8 +112,7 @@ static int find_bios(int region, char **bios_file)
 \r
        for (i = 0; i < count; i++)\r
        {\r
-               plat_get_root_dir(bios_path, sizeof(bios_path));\r
-               strcat(bios_path, files[i]);\r
+               emu_make_path(bios_path, files[i], sizeof(bios_path) - 4);\r
                strcat(bios_path, ".bin");\r
                f = fopen(bios_path, "rb");\r
                if (f) break;\r
@@ -533,18 +532,29 @@ static void romfname_ext(char *dst, const char *prefix, const char *ext)
        if (ext) strcat(dst, ext);\r
 }\r
 \r
-static void make_config_cfg(char *cfg)\r
+void emu_make_path(char *buff, const char *end, int size)\r
 {\r
-       int len;\r
-       len = plat_get_root_dir(cfg, 512);\r
-       strncpy(cfg + len, PicoConfigFile, 512-6-1-len);\r
+       int pos, end_len;\r
+\r
+       end_len = strlen(end);\r
+       pos = plat_get_root_dir(buff, size);\r
+       strncpy(buff + pos, end, size - pos);\r
+       buff[size - 1] = 0;\r
+       if (pos + end_len > size - 1)\r
+               lprintf("Warning: path truncated: %s\n", buff);\r
+}\r
+\r
+static void make_config_cfg(char *cfg_buff_512)\r
+{\r
+       emu_make_path(cfg_buff_512, PicoConfigFile, 512-6);\r
        if (config_slot != 0)\r
        {\r
-               char *p = strrchr(cfg, '.');\r
-               if (p == NULL) p = cfg + strlen(cfg);\r
+               char *p = strrchr(cfg_buff_512, '.');\r
+               if (p == NULL)\r
+                       p = cfg_buff_512 + strlen(cfg_buff_512);\r
                sprintf(p, ".%i.cfg", config_slot);\r
        }\r
-       cfg[511] = 0;\r
+       cfg_buff_512[511] = 0;\r
 }\r
 \r
 static void emu_setDefaultConfig(void)\r
@@ -650,16 +660,6 @@ int emu_write_config(int is_game)
 }\r
 \r
 \r
-void emu_writelrom(void)\r
-{\r
-       char cfg[512];\r
-       make_config_cfg(cfg);\r
-       config_writelrom(cfg);\r
-#ifndef NO_SYNC\r
-       sync();\r
-#endif\r
-}\r
-\r
 /* always using built-in font */\r
 \r
 #define mk_text_out(name, type, val) \\r
@@ -1152,14 +1152,17 @@ static void mkdir_path(char *path_with_reserve, int pos, const char *name)
 \r
 void emu_init(void)\r
 {\r
-       char dir[256];\r
+       char path[512];\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
+       pos = plat_get_root_dir(path, sizeof(path) - 4);\r
+       mkdir_path(path, pos, "mds");\r
+       mkdir_path(path, pos, "srm");\r
+       mkdir_path(path, pos, "brm");\r
+\r
+       make_config_cfg(path);\r
+       config_readlrom(path);\r
 \r
        PicoInit();\r
        PicoMessage = plat_status_msg_busy_next;\r
@@ -1175,8 +1178,14 @@ void emu_finish(void)
                SRam.changed = 0;\r
        }\r
 \r
-       if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG))\r
-               emu_writelrom();\r
+       if (!(currentConfig.EmuOpt & EOPT_NO_AUTOSVCFG)) {\r
+               char cfg[512];\r
+               make_config_cfg(cfg);\r
+               config_writelrom(cfg);\r
+#ifndef NO_SYNC\r
+               sync();\r
+#endif\r
+       }\r
 \r
        PicoExit();\r
 }\r