4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
13 #include <sys/types.h>
20 #include "plugin_lib.h"
24 #include "../libpcsxcore/misc.h"
25 #include "../libpcsxcore/cheat.h"
26 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
27 #include "../plugins/cdrcimg/cdrcimg.h"
31 #include "libpicofe/input.h"
32 #include "libpicofe/plat.h"
33 #include "libpicofe/readpng.h"
35 static void toggle_fast_forward(int force_off);
38 #define BOOT_MSG "Booting up..."
41 // don't include debug.h - it breaks ARM build (R1 redefined)
46 extern int iUseReverb;
47 extern int iUseInterpolation;
51 int ready_to_go, g_resetting;
52 unsigned long gpuDisp;
53 char cfgfile_basename[MAXPATHLEN];
55 enum sched_action emu_action, emu_action_old;
59 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
62 snprintf(buf, size, ".%s%s", dir, fname);
64 snprintf(buf, size, ".%s", dir);
66 #define MAKE_PATH(buf, dir, fname) \
67 make_path(buf, sizeof(buf), dir, fname)
69 static void create_profile_dir(const char *directory) {
70 char path[MAXPATHLEN];
72 MAKE_PATH(path, directory, NULL);
73 mkdir(path, S_IRWXU | S_IRWXG);
76 static void CheckSubDir() {
77 // make sure that ~/.pcsx exists
78 create_profile_dir(PCSX_DOT_DIR);
80 create_profile_dir(BIOS_DIR);
81 create_profile_dir(MEMCARD_DIR);
82 create_profile_dir(STATES_DIR);
83 create_profile_dir(PLUGINS_DIR);
84 create_profile_dir(PLUGINS_CFG_DIR);
85 create_profile_dir(CHEATS_DIR);
86 create_profile_dir(PATCHES_DIR);
87 create_profile_dir(PCSX_DOT_DIR "cfg");
88 create_profile_dir("/screenshots/");
91 static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
95 strncpy(trimlabel, CdromLabel, 32);
97 for (j = 31; j >= 0; j--)
98 if (trimlabel[j] == ' ')
103 snprintf(buf, size, fmt, trimlabel, CdromId, i);
108 void set_cd_image(const char *fname)
110 const char *ext = NULL;
113 ext = strrchr(fname, '.');
116 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
117 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
119 cdrcimg_set_fname(fname);
120 strcpy(Config.Cdr, "builtin_cdrcimg");
123 strcpy(Config.Cdr, "builtin_cdr");
127 static void set_default_paths(void)
129 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
130 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
131 strcpy(Config.BiosDir, "bios");
133 strcpy(Config.PluginsDir, "plugins");
134 strcpy(Config.Gpu, "builtin_gpu");
135 strcpy(Config.Spu, "builtin_spu");
136 strcpy(Config.Cdr, "builtin_cdr");
137 strcpy(Config.Pad1, "builtin_pad");
138 strcpy(Config.Pad2, "builtin_pad");
139 strcpy(Config.Net, "Disabled");
141 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
144 void emu_set_default_config(void)
146 // try to set sane config on which most games work
147 Config.Xa = Config.Cdda = Config.Sio =
148 Config.SpuIrq = Config.RCntFix = Config.VSyncWA = 0;
151 pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
152 pl_rearmed_cbs.gpu_neon.enhancement_enable =
153 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
154 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
155 pl_rearmed_cbs.gpu_peops.dwActFixes = 1<<7;
156 pl_rearmed_cbs.gpu_unai.abe_hack =
157 pl_rearmed_cbs.gpu_unai.no_light =
158 pl_rearmed_cbs.gpu_unai.no_blend = 0;
159 memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
160 pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
161 pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
164 iUseInterpolation = 1;
167 #ifndef __ARM_ARCH_7A__ /* XXX */
169 iUseInterpolation = 0;
171 new_dynarec_hacks = 0;
172 cycle_multiplier = 200;
174 in_type1 = PSE_PAD_TYPE_STANDARD;
175 in_type2 = PSE_PAD_TYPE_STANDARD;
178 static void check_memcards(void)
180 char buf[MAXPATHLEN];
184 for (i = 1; i <= 9; i++) {
185 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
187 f = fopen(buf, "rb");
189 SysPrintf("Creating memcard: %s\n", buf);
197 void do_emu_action(void)
201 emu_action_old = emu_action;
203 switch (emu_action) {
204 case SACTION_LOAD_STATE:
205 ret = emu_load_state(state_slot);
206 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
208 case SACTION_SAVE_STATE:
209 ret = emu_save_state(state_slot);
210 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
213 case SACTION_ENTER_MENU:
214 toggle_fast_forward(1);
217 case SACTION_NEXT_SSLOT:
222 case SACTION_PREV_SSLOT:
227 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
228 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
230 SysPrintf("* %s\n", hud_msg);
232 case SACTION_TOGGLE_FSKIP:
233 pl_rearmed_cbs.fskip_advice = 0;
234 pl_rearmed_cbs.frameskip++;
235 if (pl_rearmed_cbs.frameskip > 1)
236 pl_rearmed_cbs.frameskip = -1;
237 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
238 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
239 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
240 plugin_call_rearmed_cbs();
242 case SACTION_SWITCH_DISPMODE:
243 pl_switch_dispmode();
244 plugin_call_rearmed_cbs();
245 if (GPU_open != NULL && GPU_close != NULL) {
247 GPU_open(&gpuDisp, "PCSX", NULL);
250 case SACTION_FAST_FORWARD:
251 toggle_fast_forward(0);
252 plugin_call_rearmed_cbs();
254 case SACTION_TOGGLE_FPS:
255 if ((g_opts & (OPT_SHOWFPS|OPT_SHOWCPU))
256 == (OPT_SHOWFPS|OPT_SHOWCPU))
257 g_opts &= ~(OPT_SHOWFPS|OPT_SHOWCPU);
258 else if (g_opts & OPT_SHOWFPS)
259 g_opts |= OPT_SHOWCPU;
261 g_opts |= OPT_SHOWFPS;
263 case SACTION_TOGGLE_FULLSCREEN:
264 plat_target.vout_fullscreen = !plat_target.vout_fullscreen;
265 if (GPU_open != NULL && GPU_close != NULL) {
267 GPU_open(&gpuDisp, "PCSX", NULL);
270 case SACTION_SCREENSHOT:
272 char buf[MAXPATHLEN];
275 time_t t = time(NULL);
276 struct tm *tb = localtime(&t);
277 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
278 tb->tm_min * 100 + tb->tm_sec;
280 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
281 get_gameid_filename(buf, sizeof(buf),
282 "screenshots/%.32s-%.9s.%d.png", ti);
284 if (scrbuf != 0 && bpp == 16)
285 ret = writepng(buf, scrbuf, w, h);
287 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
290 case SACTION_VOLUME_UP:
291 case SACTION_VOLUME_DOWN:
292 plat_target_step_volume(emu_action == SACTION_VOLUME_UP);
294 case SACTION_MINIMIZE:
295 if (GPU_close != NULL)
300 if (GPU_open != NULL) {
301 ret = GPU_open(&gpuDisp, "PCSX", NULL);
303 SysMessage("GPU_open returned %d", ret);
314 static char basic_lcase(char c)
316 if ('A' <= c && c <= 'Z')
317 return c - 'A' + 'a';
321 static int cdidcmp(const char *id1, const char *id2)
323 while (*id1 != 0 && *id2 != 0) {
324 if (*id1 == '_') { id1++; continue; }
325 if (*id2 == '_') { id2++; continue; }
326 if (basic_lcase(*id1) != basic_lcase(*id2))
335 static void parse_cwcheat(void)
337 char line[256], buf[64], name[64], *p;
342 f = fopen("cheatpops.db", "r");
347 while (fgets(line, sizeof(line), f)) {
348 if (sscanf(line, "_S %63s", buf) != 1)
350 if (cdidcmp(buf, CdromId) == 0)
357 SysPrintf("cwcheat section found for %s\n", CdromId);
358 while (fgets(line, sizeof(line), f))
360 p = line + strlen(line);
361 for (p--; p >= line && (*p == '\r' || *p == '\n' || *p == ' '); p--)
363 if (*p == 0 || *p == '#' || *p == ';')
366 if (strncmp(line, "_S", 2) == 0)
368 if (strncmp(line, "_G", 2) == 0) {
369 SysPrintf(" cwcheat game name: '%s'\n", line + 3);
372 if (strncmp(line, "_C0", 3) == 0) {
373 if (!newcheat && Cheats[NumCheats - 1].n == 0) {
374 SysPrintf("cheat '%s' failed to parse\n", name);
375 free(Cheats[NumCheats - 1].Descr);
378 snprintf(name, sizeof(name), "%s", line + 4);
382 if (sscanf(line, "_L %x %x", &a, &v) != 2) {
383 SysPrintf("line failed to parse: '%s'\n", line);
388 if (NumCheats >= NumCheatsAllocated) {
389 NumCheatsAllocated += 16;
390 Cheats = realloc(Cheats, sizeof(Cheat) *
395 Cheats[NumCheats].Descr = strdup(name);
396 Cheats[NumCheats].Enabled = 0;
397 Cheats[NumCheats].WasEnabled = 0;
398 Cheats[NumCheats].First = NumCodes;
399 Cheats[NumCheats].n = 0;
404 if (NumCodes >= NumCodesAllocated) {
405 NumCodesAllocated += 16;
406 CheatCodes = realloc(CheatCodes, sizeof(CheatCode) *
408 if (CheatCodes == NULL)
411 CheatCodes[NumCodes].Addr = a;
412 CheatCodes[NumCodes].Val = v;
414 Cheats[NumCheats - 1].n++;
421 void emu_on_new_cd(int show_hud_msg)
427 SysPrintf("note: running with HLE BIOS, expect compatibility problems\n");
428 SysPrintf("----------------------------------------------------------\n");
432 snprintf(hud_msg, sizeof(hud_msg), BOOT_MSG);
437 int emu_core_preinit(void)
439 // what is the name of the config file?
440 // it may be redefined by -cfg on the command line
441 strcpy(cfgfile_basename, "pcsx.cfg");
446 memset(&Config, 0, sizeof(Config));
449 emu_set_default_config();
450 strcpy(Config.Bios, "HLE");
455 int emu_core_init(void)
457 SysPrintf("Starting PCSX-ReARMed " REV "\n");
462 if (EmuInit() == -1) {
463 SysPrintf("PSX emulator couldn't be initialized.\n");
467 LoadMcds(Config.Mcd1, Config.Mcd2);
477 int main(int argc, char *argv[])
479 char file[MAXPATHLEN] = "";
480 char path[MAXPATHLEN];
481 const char *cdfile = NULL;
482 const char *loadst_f = NULL;
489 // read command line options
490 for (i = 1; i < argc; i++) {
491 if (!strcmp(argv[i], "-psxout")) psxout = 1;
492 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
493 else if (!strcmp(argv[i], "-cfg")) {
494 if (i+1 >= argc) break;
495 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
496 SysPrintf("Using config file %s.\n", cfgfile_basename);
498 else if (!strcmp(argv[i], "-cdfile")) {
499 char isofilename[MAXPATHLEN];
501 if (i+1 >= argc) break;
502 strncpy(isofilename, argv[++i], MAXPATHLEN);
503 if (isofilename[0] != '/') {
504 getcwd(path, MAXPATHLEN);
505 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
507 strcat(path, isofilename);
508 strcpy(isofilename, path);
513 cdfile = isofilename;
515 else if (!strcmp(argv[i], "-loadf")) {
516 if (i+1 >= argc) break;
517 loadst_f = argv[++i];
519 else if (!strcmp(argv[i], "-h") ||
520 !strcmp(argv[i], "-help") ||
521 !strcmp(argv[i], "--help")) {
522 printf("PCSX-ReARMed " REV "\n");
524 " pcsx [options] [file]\n"
526 "\t-cdfile FILE\tRuns a CD image file\n"
527 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
528 "\t-psxout\t\tEnable PSX output\n"
529 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
530 "\t-h -help\tDisplay this message\n"
531 "\tfile\t\tLoads a PSX EXE file\n"));
534 strncpy(file, argv[i], MAXPATHLEN);
535 if (file[0] != '/') {
536 getcwd(path, MAXPATHLEN);
537 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
548 set_cd_image(cdfile);
551 // init input but leave probing to platform code,
552 // they add input drivers and may need to modify them after probe
556 menu_init(); // loads config
558 if (emu_core_init() != 0)
564 if (LoadPlugins() == -1) {
565 // FIXME: this recovery doesn't work, just delete bad config and bail out
566 // SysMessage("could not load plugins, retrying with defaults\n");
568 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
570 SysMessage("Failed loading plugins!");
575 if (OpenPlugins() == -1) {
578 plugin_call_rearmed_cbs();
583 if (file[0] != '\0') {
584 if (Load(file) != -1)
588 if (LoadCdrom() == -1) {
590 SysPrintf(_("Could not load CD-ROM!\n"));
593 emu_on_new_cd(!loadst);
599 int ret = LoadState(loadst_f);
600 SysPrintf("%s state file: %s\n",
601 ret ? "failed to load" : "loaded", loadst_f);
602 ready_to_go |= ret == 0;
608 // If a state has been specified, then load that
610 int ret = emu_load_state(loadst - 1);
611 SysPrintf("%s state %d\n",
612 ret ? "failed to load" : "loaded", loadst);
623 emu_action = SACTION_NONE;
626 if (emu_action != SACTION_NONE)
633 static void toggle_fast_forward(int force_off)
635 static int fast_forward;
636 static int normal_g_opts;
637 static int normal_frameskip;
638 static int normal_enhancement_enable;
640 if (force_off && !fast_forward)
643 fast_forward = !fast_forward;
645 normal_g_opts = g_opts;
646 normal_frameskip = pl_rearmed_cbs.frameskip;
647 normal_enhancement_enable =
648 pl_rearmed_cbs.gpu_neon.enhancement_enable;
650 g_opts |= OPT_NO_FRAMELIM;
651 pl_rearmed_cbs.frameskip = 3;
652 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
654 g_opts = normal_g_opts;
655 pl_rearmed_cbs.frameskip = normal_frameskip;
656 pl_rearmed_cbs.gpu_neon.enhancement_enable =
657 normal_enhancement_enable;
659 pl_timing_prepare(Config.PsxType);
663 snprintf(hud_msg, sizeof(hud_msg), "FAST FORWARD %s",
664 fast_forward ? "ON" : "OFF");
669 printf("SysRunGui\n");
672 static void dummy_lace()
677 // rearmed hack: EmuReset() runs some code when real BIOS is used,
678 // but we usually do reset from menu while GPU is not open yet,
679 // so we need to prevent updateLace() call..
680 void *real_lace = GPU_updateLace;
681 GPU_updateLace = dummy_lace;
684 // reset can run code, timing must be set
685 pl_timing_prepare(Config.PsxType);
689 // hmh core forgets this
692 GPU_updateLace = real_lace;
702 if (emuLog != NULL) fclose(emuLog);
709 printf("OnFile_Exit\n");
718 int get_state_filename(char *buf, int size, int i) {
719 return get_gameid_filename(buf, size,
720 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
723 int emu_check_state(int slot)
725 char fname[MAXPATHLEN];
728 ret = get_state_filename(fname, sizeof(fname), slot);
732 return CheckState(fname);
735 int emu_save_state(int slot)
737 char fname[MAXPATHLEN];
740 ret = get_state_filename(fname, sizeof(fname), slot);
744 ret = SaveState(fname);
745 #ifndef __ARM_ARCH_7A__ /* XXX */
748 SysPrintf("* %s \"%s\" [%d]\n",
749 ret == 0 ? "saved" : "failed to save", fname, slot);
753 int emu_load_state(int slot)
755 char fname[MAXPATHLEN];
760 ret = get_state_filename(fname, sizeof(fname), slot);
764 return LoadState(fname);
769 void SysPrintf(const char *fmt, ...) {
773 vfprintf(emuLog, fmt, list);
779 #include <android/log.h>
781 void SysPrintf(const char *fmt, ...) {
785 __android_log_vprint(ANDROID_LOG_INFO, "PCSX", fmt, list);
791 void SysMessage(const char *fmt, ...) {
797 ret = vsnprintf(msg, sizeof(msg), fmt, list);
800 if (ret < sizeof(msg) && msg[ret - 1] == '\n')
803 SysPrintf("%s\n", msg);
806 static void SignalExit(int sig) {
811 #define PARSEPATH(dst, src) \
812 ptr = src + strlen(src); \
813 while (*ptr != '\\' && ptr != src) ptr--; \
815 strcpy(dst, ptr+1); \
818 static int _OpenPlugins(void) {
821 signal(SIGINT, SignalExit);
822 signal(SIGPIPE, SignalExit);
824 GPU_clearDynarec(clearDynarec);
827 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
829 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
830 SPU_registerCallback(SPUirq);
831 // pcsx-rearmed: we handle gpu elsewhere
832 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
833 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
834 ret = PAD1_open(&gpuDisp);
835 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
836 ret = PAD2_open(&gpuDisp);
837 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
839 if (Config.UseNet && !NetOpened) {
841 char path[MAXPATHLEN];
842 char dotdir[MAXPATHLEN];
844 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
846 strcpy(info.EmuName, "PCSX");
847 strncpy(info.CdromID, CdromId, 9);
848 strncpy(info.CdromLabel, CdromLabel, 9);
850 info.GPU_showScreenPic = GPU_showScreenPic;
851 info.GPU_displayText = GPU_displayText;
852 info.GPU_showScreenPic = GPU_showScreenPic;
853 info.PAD_setSensitive = PAD1_setSensitive;
854 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
855 strcpy(info.BIOSpath, path);
856 strcpy(info.MCD1path, Config.Mcd1);
857 strcpy(info.MCD2path, Config.Mcd2);
858 sprintf(path, "%s%s", dotdir, Config.Gpu);
859 strcpy(info.GPUpath, path);
860 sprintf(path, "%s%s", dotdir, Config.Spu);
861 strcpy(info.SPUpath, path);
862 sprintf(path, "%s%s", dotdir, Config.Cdr);
863 strcpy(info.CDRpath, path);
866 ret = NET_open(&gpuDisp);
869 // -2 is returned when something in the info
870 // changed and needs to be synced
873 PARSEPATH(Config.Bios, info.BIOSpath);
874 PARSEPATH(Config.Gpu, info.GPUpath);
875 PARSEPATH(Config.Spu, info.SPUpath);
876 PARSEPATH(Config.Cdr, info.CDRpath);
878 strcpy(Config.Mcd1, info.MCD1path);
879 strcpy(Config.Mcd2, info.MCD2path);
882 Config.UseNet = FALSE;
885 if (NET_queryPlayer() == 1) {
886 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
888 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
892 } else if (Config.UseNet) {
902 while ((ret = _OpenPlugins()) == -2) {
904 LoadMcds(Config.Mcd1, Config.Mcd2);
905 if (LoadPlugins() == -1) return -1;
910 void ClosePlugins() {
913 signal(SIGINT, SIG_DFL);
914 signal(SIGPIPE, SIG_DFL);
916 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
918 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
920 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
922 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
923 // pcsx-rearmed: we handle gpu elsewhere
925 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
932 /* we hook statically linked plugins here */
933 static const char *builtin_plugins[] = {
934 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
938 static const int builtin_plugin_ids[] = {
939 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
943 void *SysLoadLibrary(const char *lib) {
944 const char *tmp = strrchr(lib, '/');
948 SysPrintf("plugin: %s\n", lib);
952 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
953 if (strcmp(tmp, builtin_plugins[i]) == 0)
954 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
957 ret = dlopen(lib, RTLD_NOW);
959 SysMessage("dlopen: %s", dlerror());
963 void *SysLoadSym(void *lib, const char *sym) {
964 unsigned int plugid = (unsigned int)(long)lib;
966 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
967 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
969 return dlsym(lib, sym);
972 const char *SysLibError() {
976 void SysCloseLibrary(void *lib) {
977 unsigned int plugid = (unsigned int)(long)lib;
979 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))