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"
36 #define BOOT_MSG "Booting up..."
39 // don't include debug.h - it breaks ARM build (R1 redefined)
44 extern int iUseReverb;
45 extern int iUseInterpolation;
50 unsigned long gpuDisp;
51 char cfgfile_basename[MAXPATHLEN];
53 enum sched_action emu_action, emu_action_old;
57 static void toggle_fast_forward(int force_off);
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;
149 Config.CdrReschedule = 0;
152 pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
153 pl_rearmed_cbs.gpu_neon.enhancement_enable =
154 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
155 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
156 pl_rearmed_cbs.gpu_peops.dwActFixes = 1<<7;
157 pl_rearmed_cbs.gpu_unai.abe_hack =
158 pl_rearmed_cbs.gpu_unai.no_light =
159 pl_rearmed_cbs.gpu_unai.no_blend = 0;
160 memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
161 pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
162 pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
165 iUseInterpolation = 1;
168 #ifndef __ARM_ARCH_7A__ /* XXX */
170 iUseInterpolation = 0;
172 new_dynarec_hacks = 0;
173 cycle_multiplier = 200;
175 in_type1 = PSE_PAD_TYPE_STANDARD;
176 in_type2 = PSE_PAD_TYPE_STANDARD;
179 static void check_memcards(void)
181 char buf[MAXPATHLEN];
185 for (i = 1; i <= 9; i++) {
186 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
188 f = fopen(buf, "rb");
190 printf("Creating memcard: %s\n", buf);
198 void do_emu_action(void)
202 emu_action_old = emu_action;
204 switch (emu_action) {
205 case SACTION_LOAD_STATE:
206 ret = emu_load_state(state_slot);
207 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
209 case SACTION_SAVE_STATE:
210 ret = emu_save_state(state_slot);
211 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
214 case SACTION_ENTER_MENU:
215 toggle_fast_forward(1);
218 case SACTION_NEXT_SSLOT:
223 case SACTION_PREV_SSLOT:
228 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
229 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
231 printf("* %s\n", hud_msg);
233 case SACTION_TOGGLE_FSKIP:
234 pl_rearmed_cbs.fskip_advice = 0;
235 pl_rearmed_cbs.frameskip++;
236 if (pl_rearmed_cbs.frameskip > 1)
237 pl_rearmed_cbs.frameskip = -1;
238 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
239 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
240 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
241 plugin_call_rearmed_cbs();
243 case SACTION_SWITCH_DISPMODE:
244 pl_switch_dispmode();
245 plugin_call_rearmed_cbs();
246 if (GPU_open != NULL && GPU_close != NULL) {
248 GPU_open(&gpuDisp, "PCSX", NULL);
251 case SACTION_FAST_FORWARD:
252 toggle_fast_forward(0);
253 plugin_call_rearmed_cbs();
255 case SACTION_TOGGLE_FPS:
256 if ((g_opts & (OPT_SHOWFPS|OPT_SHOWCPU))
257 == (OPT_SHOWFPS|OPT_SHOWCPU))
258 g_opts &= ~(OPT_SHOWFPS|OPT_SHOWCPU);
259 else if (g_opts & OPT_SHOWFPS)
260 g_opts |= OPT_SHOWCPU;
262 g_opts |= OPT_SHOWFPS;
264 case SACTION_SCREENSHOT:
266 char buf[MAXPATHLEN];
269 time_t t = time(NULL);
270 struct tm *tb = localtime(&t);
271 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
272 tb->tm_min * 100 + tb->tm_sec;
274 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
275 get_gameid_filename(buf, sizeof(buf),
276 "screenshots/%.32s-%.9s.%d.png", ti);
278 if (scrbuf != 0 && bpp == 16)
279 ret = writepng(buf, scrbuf, w, h);
281 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
284 case SACTION_VOLUME_UP:
285 case SACTION_VOLUME_DOWN:
286 plat_target_step_volume(emu_action == SACTION_VOLUME_UP);
288 case SACTION_MINIMIZE:
289 if (GPU_close != NULL)
294 if (GPU_open != NULL) {
295 ret = GPU_open(&gpuDisp, "PCSX", NULL);
297 fprintf(stderr, "GPU_open returned %d\n", ret);
308 static int cdidcmp(const char *id1, const char *id2)
310 while (*id1 != 0 && *id2 != 0) {
311 if (*id1 == '_') { id1++; continue; }
312 if (*id2 == '_') { id2++; continue; }
322 static void parse_cwcheat(void)
324 char line[256], buf[64], name[64], *p;
329 f = fopen("cheatpops.db", "r");
334 while (fgets(line, sizeof(line), f)) {
335 if (sscanf(line, "_S %63s", buf) != 1)
337 if (cdidcmp(buf, CdromId) == 0)
344 printf("cwcheat section found for %s\n", CdromId);
345 while (fgets(line, sizeof(line), f))
347 p = line + strlen(line);
348 for (p--; p >= line && (*p == '\r' || *p == '\n' || *p == ' '); p--)
350 if (*p == 0 || *p == '#' || *p == ';')
353 if (strncmp(line, "_S", 2) == 0)
355 if (strncmp(line, "_G", 2) == 0) {
356 printf(" cwcheat game name: '%s'\n", line + 3);
359 if (strncmp(line, "_C0", 3) == 0) {
360 if (!newcheat && Cheats[NumCheats - 1].n == 0) {
361 printf("cheat '%s' failed to parse\n", name);
362 free(Cheats[NumCheats - 1].Descr);
365 snprintf(name, sizeof(name), "%s", line + 4);
369 if (sscanf(line, "_L %x %x", &a, &v) != 2) {
370 printf("line failed to parse: '%s'\n", line);
375 if (NumCheats >= NumCheatsAllocated) {
376 NumCheatsAllocated += 16;
377 Cheats = realloc(Cheats, sizeof(Cheat) *
382 Cheats[NumCheats].Descr = strdup(name);
383 Cheats[NumCheats].Enabled = 0;
384 Cheats[NumCheats].WasEnabled = 0;
385 Cheats[NumCheats].First = NumCodes;
386 Cheats[NumCheats].n = 0;
391 if (NumCodes >= NumCodesAllocated) {
392 NumCodesAllocated += 16;
393 CheatCodes = realloc(CheatCodes, sizeof(CheatCode) *
395 if (CheatCodes == NULL)
398 CheatCodes[NumCodes].Addr = a;
399 CheatCodes[NumCodes].Val = v;
401 Cheats[NumCheats - 1].n++;
408 void emu_on_new_cd(int show_hud_msg)
414 printf("note: running with HLE BIOS, expect compatibility problems\n");
415 printf("----------------------------------------------------------\n");
419 snprintf(hud_msg, sizeof(hud_msg), BOOT_MSG);
424 int emu_core_preinit(void)
426 // what is the name of the config file?
427 // it may be redefined by -cfg on the command line
428 strcpy(cfgfile_basename, "pcsx.cfg");
433 memset(&Config, 0, sizeof(Config));
436 emu_set_default_config();
437 strcpy(Config.Bios, "HLE");
442 int emu_core_init(void)
447 if (EmuInit() == -1) {
448 printf("PSX emulator couldn't be initialized.\n");
452 LoadMcds(Config.Mcd1, Config.Mcd2);
462 int main(int argc, char *argv[])
464 char file[MAXPATHLEN] = "";
465 char path[MAXPATHLEN];
466 const char *cdfile = NULL;
467 const char *loadst_f = NULL;
474 // read command line options
475 for (i = 1; i < argc; i++) {
476 if (!strcmp(argv[i], "-psxout")) psxout = 1;
477 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
478 else if (!strcmp(argv[i], "-cfg")) {
479 if (i+1 >= argc) break;
480 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
481 printf("Using config file %s.\n", cfgfile_basename);
483 else if (!strcmp(argv[i], "-cdfile")) {
484 char isofilename[MAXPATHLEN];
486 if (i+1 >= argc) break;
487 strncpy(isofilename, argv[++i], MAXPATHLEN);
488 if (isofilename[0] != '/') {
489 getcwd(path, MAXPATHLEN);
490 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
492 strcat(path, isofilename);
493 strcpy(isofilename, path);
498 cdfile = isofilename;
500 else if (!strcmp(argv[i], "-loadf")) {
501 if (i+1 >= argc) break;
502 loadst_f = argv[++i];
504 else if (!strcmp(argv[i], "-h") ||
505 !strcmp(argv[i], "-help") ||
506 !strcmp(argv[i], "--help")) {
507 printf("PCSX-ReARMed " REV "\n");
509 " pcsx [options] [file]\n"
511 "\t-cdfile FILE\tRuns a CD image file\n"
512 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
513 "\t-psxout\t\tEnable PSX output\n"
514 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
515 "\t-h -help\tDisplay this message\n"
516 "\tfile\t\tLoads a PSX EXE file\n"));
519 strncpy(file, argv[i], MAXPATHLEN);
520 if (file[0] != '/') {
521 getcwd(path, MAXPATHLEN);
522 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
533 set_cd_image(cdfile);
536 // init input but leave probing to platform code,
537 // they add input drivers and may need to modify them after probe
541 menu_init(); // loads config
548 if (LoadPlugins() == -1) {
549 // FIXME: this recovery doesn't work, just delete bad config and bail out
550 // SysMessage("could not load plugins, retrying with defaults\n");
552 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
554 SysMessage("Failed loading plugins!");
559 if (OpenPlugins() == -1) {
562 plugin_call_rearmed_cbs();
567 if (file[0] != '\0') {
568 if (Load(file) != -1)
572 if (LoadCdrom() == -1) {
574 printf(_("Could not load CD-ROM!\n"));
577 emu_on_new_cd(!loadst);
585 // If a state has been specified, then load that
587 int ret = emu_load_state(loadst - 1);
588 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
591 int ret = LoadState(loadst_f);
592 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
603 emu_action = SACTION_NONE;
606 if (emu_action != SACTION_NONE)
613 static void toggle_fast_forward(int force_off)
615 static int fast_forward;
616 static int normal_g_opts;
617 static int normal_frameskip;
618 static int normal_enhancement_enable;
620 if (force_off && !fast_forward)
623 fast_forward = !fast_forward;
625 normal_g_opts = g_opts;
626 normal_frameskip = pl_rearmed_cbs.frameskip;
627 normal_enhancement_enable =
628 pl_rearmed_cbs.gpu_neon.enhancement_enable;
630 g_opts |= OPT_NO_FRAMELIM;
631 pl_rearmed_cbs.frameskip = 3;
632 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
634 g_opts = normal_g_opts;
635 pl_rearmed_cbs.frameskip = normal_frameskip;
636 pl_rearmed_cbs.gpu_neon.enhancement_enable =
637 normal_enhancement_enable;
639 pl_timing_prepare(Config.PsxType);
643 snprintf(hud_msg, sizeof(hud_msg), "FAST FORWARD %s",
644 fast_forward ? "ON" : "OFF");
649 printf("SysRunGui\n");
652 static void dummy_lace()
657 // rearmed hack: EmuReset() runs some code when real BIOS is used,
658 // but we usually do reset from menu while GPU is not open yet,
659 // so we need to prevent updateLace() call..
660 void *real_lace = GPU_updateLace;
661 GPU_updateLace = dummy_lace;
663 // reset can run code, timing must be set
664 pl_timing_prepare(Config.PsxType);
668 // hmh core forgets this
671 GPU_updateLace = real_lace;
680 if (emuLog != NULL) fclose(emuLog);
687 printf("OnFile_Exit\n");
696 int get_state_filename(char *buf, int size, int i) {
697 return get_gameid_filename(buf, size,
698 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
701 int emu_check_state(int slot)
703 char fname[MAXPATHLEN];
706 ret = get_state_filename(fname, sizeof(fname), slot);
710 return CheckState(fname);
713 int emu_save_state(int slot)
715 char fname[MAXPATHLEN];
718 ret = get_state_filename(fname, sizeof(fname), slot);
722 ret = SaveState(fname);
723 #ifndef __ARM_ARCH_7A__ /* XXX */
726 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
730 int emu_load_state(int slot)
732 char fname[MAXPATHLEN];
737 ret = get_state_filename(fname, sizeof(fname), slot);
741 return LoadState(fname);
744 void SysPrintf(const char *fmt, ...) {
749 vsprintf(msg, fmt, list);
752 fprintf(emuLog, "%s", msg);
755 void SysMessage(const char *fmt, ...) {
760 vsprintf(msg, fmt, list);
763 if (msg[strlen(msg) - 1] == '\n')
764 msg[strlen(msg) - 1] = 0;
766 fprintf(stderr, "%s\n", msg);
769 static void SignalExit(int sig) {
774 #define PARSEPATH(dst, src) \
775 ptr = src + strlen(src); \
776 while (*ptr != '\\' && ptr != src) ptr--; \
778 strcpy(dst, ptr+1); \
781 static int _OpenPlugins(void) {
784 signal(SIGINT, SignalExit);
785 signal(SIGPIPE, SignalExit);
787 GPU_clearDynarec(clearDynarec);
790 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
792 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
793 SPU_registerCallback(SPUirq);
794 // pcsx-rearmed: we handle gpu elsewhere
795 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
796 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
797 ret = PAD1_open(&gpuDisp);
798 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
799 ret = PAD2_open(&gpuDisp);
800 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
802 if (Config.UseNet && !NetOpened) {
804 char path[MAXPATHLEN];
805 char dotdir[MAXPATHLEN];
807 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
809 strcpy(info.EmuName, "PCSX");
810 strncpy(info.CdromID, CdromId, 9);
811 strncpy(info.CdromLabel, CdromLabel, 9);
813 info.GPU_showScreenPic = GPU_showScreenPic;
814 info.GPU_displayText = GPU_displayText;
815 info.GPU_showScreenPic = GPU_showScreenPic;
816 info.PAD_setSensitive = PAD1_setSensitive;
817 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
818 strcpy(info.BIOSpath, path);
819 strcpy(info.MCD1path, Config.Mcd1);
820 strcpy(info.MCD2path, Config.Mcd2);
821 sprintf(path, "%s%s", dotdir, Config.Gpu);
822 strcpy(info.GPUpath, path);
823 sprintf(path, "%s%s", dotdir, Config.Spu);
824 strcpy(info.SPUpath, path);
825 sprintf(path, "%s%s", dotdir, Config.Cdr);
826 strcpy(info.CDRpath, path);
829 ret = NET_open(&gpuDisp);
832 // -2 is returned when something in the info
833 // changed and needs to be synced
836 PARSEPATH(Config.Bios, info.BIOSpath);
837 PARSEPATH(Config.Gpu, info.GPUpath);
838 PARSEPATH(Config.Spu, info.SPUpath);
839 PARSEPATH(Config.Cdr, info.CDRpath);
841 strcpy(Config.Mcd1, info.MCD1path);
842 strcpy(Config.Mcd2, info.MCD2path);
845 Config.UseNet = FALSE;
848 if (NET_queryPlayer() == 1) {
849 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
851 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
855 } else if (Config.UseNet) {
865 while ((ret = _OpenPlugins()) == -2) {
867 LoadMcds(Config.Mcd1, Config.Mcd2);
868 if (LoadPlugins() == -1) return -1;
873 void ClosePlugins() {
876 signal(SIGINT, SIG_DFL);
877 signal(SIGPIPE, SIG_DFL);
879 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
881 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
883 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
885 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
886 // pcsx-rearmed: we handle gpu elsewhere
888 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
895 /* we hook statically linked plugins here */
896 static const char *builtin_plugins[] = {
897 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
901 static const int builtin_plugin_ids[] = {
902 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
906 void *SysLoadLibrary(const char *lib) {
907 const char *tmp = strrchr(lib, '/');
911 printf("plugin: %s\n", lib);
915 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
916 if (strcmp(tmp, builtin_plugins[i]) == 0)
917 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
920 ret = dlopen(lib, RTLD_NOW);
922 fprintf(stderr, "dlopen: %s\n", dlerror());
926 void *SysLoadSym(void *lib, const char *sym) {
927 unsigned int plugid = (unsigned int)(long)lib;
929 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
930 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
932 return dlsym(lib, sym);
935 const char *SysLibError() {
939 void SysCloseLibrary(void *lib) {
940 unsigned int plugid = (unsigned int)(long)lib;
942 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))