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 // don't include debug.h - it breaks ARM build (R1 redefined)
41 extern int iUseReverb;
42 extern int iUseInterpolation;
47 unsigned long gpuDisp;
48 char cfgfile_basename[MAXPATHLEN];
50 enum sched_action emu_action, emu_action_old;
54 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
57 snprintf(buf, size, ".%s%s", dir, fname);
59 snprintf(buf, size, ".%s", dir);
61 #define MAKE_PATH(buf, dir, fname) \
62 make_path(buf, sizeof(buf), dir, fname)
64 static void create_profile_dir(const char *directory) {
65 char path[MAXPATHLEN];
67 MAKE_PATH(path, directory, NULL);
68 mkdir(path, S_IRWXU | S_IRWXG);
71 static void CheckSubDir() {
72 // make sure that ~/.pcsx exists
73 create_profile_dir(PCSX_DOT_DIR);
75 create_profile_dir(BIOS_DIR);
76 create_profile_dir(MEMCARD_DIR);
77 create_profile_dir(STATES_DIR);
78 create_profile_dir(PLUGINS_DIR);
79 create_profile_dir(PLUGINS_CFG_DIR);
80 create_profile_dir(CHEATS_DIR);
81 create_profile_dir(PATCHES_DIR);
82 create_profile_dir(PCSX_DOT_DIR "cfg");
83 create_profile_dir("/screenshots/");
86 static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
90 strncpy(trimlabel, CdromLabel, 32);
92 for (j = 31; j >= 0; j--)
93 if (trimlabel[j] == ' ')
98 snprintf(buf, size, fmt, trimlabel, CdromId, i);
103 void set_cd_image(const char *fname)
105 const char *ext = NULL;
108 ext = strrchr(fname, '.');
111 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
112 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
114 cdrcimg_set_fname(fname);
115 strcpy(Config.Cdr, "builtin_cdrcimg");
118 strcpy(Config.Cdr, "builtin_cdr");
122 static void set_default_paths(void)
124 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
125 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
126 strcpy(Config.BiosDir, "bios");
128 strcpy(Config.PluginsDir, "plugins");
129 strcpy(Config.Gpu, "builtin_gpu");
130 strcpy(Config.Spu, "builtin_spu");
131 strcpy(Config.Cdr, "builtin_cdr");
132 strcpy(Config.Pad1, "builtin_pad");
133 strcpy(Config.Pad2, "builtin_pad");
134 strcpy(Config.Net, "Disabled");
136 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
139 void emu_set_default_config(void)
141 // try to set sane config on which most games work
142 Config.Xa = Config.Cdda = Config.Sio =
143 Config.SpuIrq = Config.RCntFix = Config.VSyncWA = 0;
144 Config.CdrReschedule = 0;
147 pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
148 pl_rearmed_cbs.gpu_neon.enhancement_enable =
149 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
150 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
151 pl_rearmed_cbs.gpu_peops.dwActFixes = 1<<7;
152 pl_rearmed_cbs.gpu_unai.abe_hack =
153 pl_rearmed_cbs.gpu_unai.no_light =
154 pl_rearmed_cbs.gpu_unai.no_blend = 0;
155 memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
156 pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
157 pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
160 iUseInterpolation = 1;
163 #ifndef __ARM_ARCH_7A__ /* XXX */
165 iUseInterpolation = 0;
167 new_dynarec_hacks = 0;
168 cycle_multiplier = 200;
170 in_type1 = PSE_PAD_TYPE_STANDARD;
171 in_type2 = PSE_PAD_TYPE_STANDARD;
174 static void check_memcards(void)
176 char buf[MAXPATHLEN];
180 for (i = 1; i <= 9; i++) {
181 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
183 f = fopen(buf, "rb");
185 printf("Creating memcard: %s\n", buf);
193 void do_emu_action(void)
197 emu_action_old = emu_action;
199 switch (emu_action) {
200 case SACTION_LOAD_STATE:
201 ret = emu_load_state(state_slot);
202 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
204 case SACTION_SAVE_STATE:
205 ret = emu_save_state(state_slot);
206 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
209 case SACTION_ENTER_MENU:
212 case SACTION_NEXT_SSLOT:
217 case SACTION_PREV_SSLOT:
222 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
223 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
225 printf("* %s\n", hud_msg);
227 case SACTION_TOGGLE_FSKIP:
228 pl_rearmed_cbs.fskip_advice = 0;
229 pl_rearmed_cbs.frameskip++;
230 if (pl_rearmed_cbs.frameskip > 1)
231 pl_rearmed_cbs.frameskip = -1;
232 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
233 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
234 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
235 plugin_call_rearmed_cbs();
237 case SACTION_SWITCH_DISPMODE:
238 pl_switch_dispmode();
239 plugin_call_rearmed_cbs();
240 if (GPU_open != NULL && GPU_close != NULL) {
242 GPU_open(&gpuDisp, "PCSX", NULL);
245 case SACTION_SCREENSHOT:
247 char buf[MAXPATHLEN];
250 time_t t = time(NULL);
251 struct tm *tb = localtime(&t);
252 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
253 tb->tm_min * 100 + tb->tm_sec;
255 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
256 get_gameid_filename(buf, sizeof(buf),
257 "screenshots/%.32s-%.9s.%d.png", ti);
259 if (scrbuf != 0 && bpp == 16)
260 ret = writepng(buf, scrbuf, w, h);
262 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
265 case SACTION_VOLUME_UP:
266 case SACTION_VOLUME_DOWN:
267 plat_target_step_volume(emu_action == SACTION_VOLUME_UP);
269 case SACTION_MINIMIZE:
270 if (GPU_close != NULL)
275 if (GPU_open != NULL) {
276 ret = GPU_open(&gpuDisp, "PCSX", NULL);
278 fprintf(stderr, "GPU_open returned %d\n", ret);
289 static int cdidcmp(const char *id1, const char *id2)
291 while (*id1 != 0 && *id2 != 0) {
292 if (*id1 == '_') { id1++; continue; }
293 if (*id2 == '_') { id2++; continue; }
303 static void parse_cwcheat(void)
305 char line[256], buf[64], name[64], *p;
310 f = fopen("cheatpops.db", "r");
315 while (fgets(line, sizeof(line), f)) {
316 if (sscanf(line, "_S %63s", buf) != 1)
318 if (cdidcmp(buf, CdromId) == 0)
325 printf("cwcheat section found for %s\n", CdromId);
326 while (fgets(line, sizeof(line), f))
328 p = line + strlen(line);
329 for (p--; p >= line && (*p == '\r' || *p == '\n' || *p == ' '); p--)
331 if (*p == 0 || *p == '#' || *p == ';')
334 if (strncmp(line, "_S", 2) == 0)
336 if (strncmp(line, "_G", 2) == 0) {
337 printf(" cwcheat game name: '%s'\n", line + 3);
340 if (strncmp(line, "_C0", 3) == 0) {
341 if (!newcheat && Cheats[NumCheats - 1].n == 0) {
342 printf("cheat '%s' failed to parse\n", name);
343 free(Cheats[NumCheats - 1].Descr);
346 snprintf(name, sizeof(name), "%s", line + 4);
350 if (sscanf(line, "_L %x %x", &a, &v) != 2) {
351 printf("line failed to parse: '%s'\n", line);
356 if (NumCheats >= NumCheatsAllocated) {
357 NumCheatsAllocated += 16;
358 Cheats = realloc(Cheats, sizeof(Cheat) *
363 Cheats[NumCheats].Descr = strdup(name);
364 Cheats[NumCheats].Enabled = 0;
365 Cheats[NumCheats].WasEnabled = 0;
366 Cheats[NumCheats].First = NumCodes;
367 Cheats[NumCheats].n = 0;
372 if (NumCodes >= NumCodesAllocated) {
373 NumCodesAllocated += 16;
374 CheatCodes = realloc(CheatCodes, sizeof(CheatCode) *
376 if (CheatCodes == NULL)
379 CheatCodes[NumCodes].Addr = a;
380 CheatCodes[NumCodes].Val = v;
382 Cheats[NumCheats - 1].n++;
389 void emu_on_new_cd(void)
395 printf("note: running with HLE BIOS, expect compatibility problems\n");
396 printf("----------------------------------------------------------\n");
399 snprintf(hud_msg, sizeof(hud_msg), "Booting up...");
403 int emu_core_preinit(void)
405 // what is the name of the config file?
406 // it may be redefined by -cfg on the command line
407 strcpy(cfgfile_basename, "pcsx.cfg");
412 memset(&Config, 0, sizeof(Config));
415 emu_set_default_config();
416 strcpy(Config.Bios, "HLE");
421 int emu_core_init(void)
426 if (EmuInit() == -1) {
427 printf("PSX emulator couldn't be initialized.\n");
431 LoadMcds(Config.Mcd1, Config.Mcd2);
441 int main(int argc, char *argv[])
443 char file[MAXPATHLEN] = "";
444 char path[MAXPATHLEN];
445 const char *cdfile = NULL;
446 const char *loadst_f = NULL;
453 // read command line options
454 for (i = 1; i < argc; i++) {
455 if (!strcmp(argv[i], "-psxout")) psxout = 1;
456 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
457 else if (!strcmp(argv[i], "-cfg")) {
458 if (i+1 >= argc) break;
459 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
460 printf("Using config file %s.\n", cfgfile_basename);
462 else if (!strcmp(argv[i], "-cdfile")) {
463 char isofilename[MAXPATHLEN];
465 if (i+1 >= argc) break;
466 strncpy(isofilename, argv[++i], MAXPATHLEN);
467 if (isofilename[0] != '/') {
468 getcwd(path, MAXPATHLEN);
469 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
471 strcat(path, isofilename);
472 strcpy(isofilename, path);
477 cdfile = isofilename;
479 else if (!strcmp(argv[i], "-loadf")) {
480 if (i+1 >= argc) break;
481 loadst_f = argv[++i];
483 else if (!strcmp(argv[i], "-h") ||
484 !strcmp(argv[i], "-help") ||
485 !strcmp(argv[i], "--help")) {
486 printf("PCSX-ReARMed " REV "\n");
488 " pcsx [options] [file]\n"
490 "\t-cdfile FILE\tRuns a CD image file\n"
491 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
492 "\t-psxout\t\tEnable PSX output\n"
493 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
494 "\t-h -help\tDisplay this message\n"
495 "\tfile\t\tLoads a PSX EXE file\n"));
498 strncpy(file, argv[i], MAXPATHLEN);
499 if (file[0] != '/') {
500 getcwd(path, MAXPATHLEN);
501 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
512 set_cd_image(cdfile);
515 // init input but leave probing to platform code,
516 // they add input drivers and may need to modify them after probe
520 menu_init(); // loads config
527 if (LoadPlugins() == -1) {
528 // FIXME: this recovery doesn't work, just delete bad config and bail out
529 // SysMessage("could not load plugins, retrying with defaults\n");
531 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
533 SysMessage("Failed loading plugins!");
538 if (OpenPlugins() == -1) {
541 plugin_call_rearmed_cbs();
546 if (file[0] != '\0') {
547 if (Load(file) != -1)
551 if (LoadCdrom() == -1) {
553 printf(_("Could not load CD-ROM!\n"));
564 // If a state has been specified, then load that
566 int ret = emu_load_state(loadst - 1);
567 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
570 int ret = LoadState(loadst_f);
571 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
582 emu_action = SACTION_NONE;
585 if (emu_action != SACTION_NONE)
594 printf("SysRunGui\n");
597 static void dummy_lace()
602 // rearmed hack: EmuReset() runs some code when real BIOS is used,
603 // but we usually do reset from menu while GPU is not open yet,
604 // so we need to prevent updateLace() call..
605 void *real_lace = GPU_updateLace;
606 GPU_updateLace = dummy_lace;
608 // reset can run code, timing must be set
609 pl_timing_prepare(Config.PsxType);
613 // hmh core forgets this
616 GPU_updateLace = real_lace;
625 if (emuLog != NULL) fclose(emuLog);
632 printf("OnFile_Exit\n");
641 int get_state_filename(char *buf, int size, int i) {
642 return get_gameid_filename(buf, size,
643 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
646 int emu_check_state(int slot)
648 char fname[MAXPATHLEN];
651 ret = get_state_filename(fname, sizeof(fname), slot);
655 return CheckState(fname);
658 int emu_save_state(int slot)
660 char fname[MAXPATHLEN];
663 ret = get_state_filename(fname, sizeof(fname), slot);
667 ret = SaveState(fname);
668 #ifndef __ARM_ARCH_7A__ /* XXX */
671 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
675 int emu_load_state(int slot)
677 char fname[MAXPATHLEN];
680 ret = get_state_filename(fname, sizeof(fname), slot);
684 return LoadState(fname);
687 void SysPrintf(const char *fmt, ...) {
692 vsprintf(msg, fmt, list);
695 fprintf(emuLog, "%s", msg);
698 void SysMessage(const char *fmt, ...) {
703 vsprintf(msg, fmt, list);
706 if (msg[strlen(msg) - 1] == '\n')
707 msg[strlen(msg) - 1] = 0;
709 fprintf(stderr, "%s\n", msg);
712 static void SignalExit(int sig) {
717 #define PARSEPATH(dst, src) \
718 ptr = src + strlen(src); \
719 while (*ptr != '\\' && ptr != src) ptr--; \
721 strcpy(dst, ptr+1); \
724 static int _OpenPlugins(void) {
727 signal(SIGINT, SignalExit);
728 signal(SIGPIPE, SignalExit);
730 GPU_clearDynarec(clearDynarec);
733 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
735 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
736 SPU_registerCallback(SPUirq);
737 // pcsx-rearmed: we handle gpu elsewhere
738 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
739 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
740 ret = PAD1_open(&gpuDisp);
741 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
742 ret = PAD2_open(&gpuDisp);
743 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
745 if (Config.UseNet && !NetOpened) {
747 char path[MAXPATHLEN];
748 char dotdir[MAXPATHLEN];
750 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
752 strcpy(info.EmuName, "PCSX");
753 strncpy(info.CdromID, CdromId, 9);
754 strncpy(info.CdromLabel, CdromLabel, 9);
756 info.GPU_showScreenPic = GPU_showScreenPic;
757 info.GPU_displayText = GPU_displayText;
758 info.GPU_showScreenPic = GPU_showScreenPic;
759 info.PAD_setSensitive = PAD1_setSensitive;
760 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
761 strcpy(info.BIOSpath, path);
762 strcpy(info.MCD1path, Config.Mcd1);
763 strcpy(info.MCD2path, Config.Mcd2);
764 sprintf(path, "%s%s", dotdir, Config.Gpu);
765 strcpy(info.GPUpath, path);
766 sprintf(path, "%s%s", dotdir, Config.Spu);
767 strcpy(info.SPUpath, path);
768 sprintf(path, "%s%s", dotdir, Config.Cdr);
769 strcpy(info.CDRpath, path);
772 ret = NET_open(&gpuDisp);
775 // -2 is returned when something in the info
776 // changed and needs to be synced
779 PARSEPATH(Config.Bios, info.BIOSpath);
780 PARSEPATH(Config.Gpu, info.GPUpath);
781 PARSEPATH(Config.Spu, info.SPUpath);
782 PARSEPATH(Config.Cdr, info.CDRpath);
784 strcpy(Config.Mcd1, info.MCD1path);
785 strcpy(Config.Mcd2, info.MCD2path);
788 Config.UseNet = FALSE;
791 if (NET_queryPlayer() == 1) {
792 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
794 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
798 } else if (Config.UseNet) {
808 while ((ret = _OpenPlugins()) == -2) {
810 LoadMcds(Config.Mcd1, Config.Mcd2);
811 if (LoadPlugins() == -1) return -1;
816 void ClosePlugins() {
819 signal(SIGINT, SIG_DFL);
820 signal(SIGPIPE, SIG_DFL);
822 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
824 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
826 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
828 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
829 // pcsx-rearmed: we handle gpu elsewhere
831 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
838 /* we hook statically linked plugins here */
839 static const char *builtin_plugins[] = {
840 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
844 static const int builtin_plugin_ids[] = {
845 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
849 void *SysLoadLibrary(const char *lib) {
850 const char *tmp = strrchr(lib, '/');
854 printf("plugin: %s\n", lib);
858 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
859 if (strcmp(tmp, builtin_plugins[i]) == 0)
860 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
863 #if defined(__x86_64__) || defined(__i386__)
865 if (strstr(lib, ".x86") == NULL) {
866 char name[MAXPATHLEN];
867 snprintf(name, sizeof(name), "%s.x86_64", lib);
872 ret = dlopen(lib, RTLD_NOW);
874 fprintf(stderr, "dlopen: %s\n", dlerror());
878 void *SysLoadSym(void *lib, const char *sym) {
879 unsigned int plugid = (unsigned int)(long)lib;
881 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
882 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
884 return dlsym(lib, sym);
887 const char *SysLibError() {
891 void SysCloseLibrary(void *lib) {
892 unsigned int plugid = (unsigned int)(long)lib;
894 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))