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 toggle_fast_forward(int force_off);
56 static void make_path(char *buf, size_t size, const char *dir, const char *fname)
59 snprintf(buf, size, ".%s%s", dir, fname);
61 snprintf(buf, size, ".%s", dir);
63 #define MAKE_PATH(buf, dir, fname) \
64 make_path(buf, sizeof(buf), dir, fname)
66 static void create_profile_dir(const char *directory) {
67 char path[MAXPATHLEN];
69 MAKE_PATH(path, directory, NULL);
70 mkdir(path, S_IRWXU | S_IRWXG);
73 static void CheckSubDir() {
74 // make sure that ~/.pcsx exists
75 create_profile_dir(PCSX_DOT_DIR);
77 create_profile_dir(BIOS_DIR);
78 create_profile_dir(MEMCARD_DIR);
79 create_profile_dir(STATES_DIR);
80 create_profile_dir(PLUGINS_DIR);
81 create_profile_dir(PLUGINS_CFG_DIR);
82 create_profile_dir(CHEATS_DIR);
83 create_profile_dir(PATCHES_DIR);
84 create_profile_dir(PCSX_DOT_DIR "cfg");
85 create_profile_dir("/screenshots/");
88 static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
92 strncpy(trimlabel, CdromLabel, 32);
94 for (j = 31; j >= 0; j--)
95 if (trimlabel[j] == ' ')
100 snprintf(buf, size, fmt, trimlabel, CdromId, i);
105 void set_cd_image(const char *fname)
107 const char *ext = NULL;
110 ext = strrchr(fname, '.');
113 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
114 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
116 cdrcimg_set_fname(fname);
117 strcpy(Config.Cdr, "builtin_cdrcimg");
120 strcpy(Config.Cdr, "builtin_cdr");
124 static void set_default_paths(void)
126 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
127 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
128 strcpy(Config.BiosDir, "bios");
130 strcpy(Config.PluginsDir, "plugins");
131 strcpy(Config.Gpu, "builtin_gpu");
132 strcpy(Config.Spu, "builtin_spu");
133 strcpy(Config.Cdr, "builtin_cdr");
134 strcpy(Config.Pad1, "builtin_pad");
135 strcpy(Config.Pad2, "builtin_pad");
136 strcpy(Config.Net, "Disabled");
138 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
141 void emu_set_default_config(void)
143 // try to set sane config on which most games work
144 Config.Xa = Config.Cdda = Config.Sio =
145 Config.SpuIrq = Config.RCntFix = Config.VSyncWA = 0;
146 Config.CdrReschedule = 0;
149 pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
150 pl_rearmed_cbs.gpu_neon.enhancement_enable =
151 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
152 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
153 pl_rearmed_cbs.gpu_peops.dwActFixes = 1<<7;
154 pl_rearmed_cbs.gpu_unai.abe_hack =
155 pl_rearmed_cbs.gpu_unai.no_light =
156 pl_rearmed_cbs.gpu_unai.no_blend = 0;
157 memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
158 pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
159 pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
162 iUseInterpolation = 1;
165 #ifndef __ARM_ARCH_7A__ /* XXX */
167 iUseInterpolation = 0;
169 new_dynarec_hacks = 0;
170 cycle_multiplier = 200;
172 in_type1 = PSE_PAD_TYPE_STANDARD;
173 in_type2 = PSE_PAD_TYPE_STANDARD;
176 static void check_memcards(void)
178 char buf[MAXPATHLEN];
182 for (i = 1; i <= 9; i++) {
183 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
185 f = fopen(buf, "rb");
187 printf("Creating memcard: %s\n", buf);
195 void do_emu_action(void)
199 emu_action_old = emu_action;
201 switch (emu_action) {
202 case SACTION_LOAD_STATE:
203 ret = emu_load_state(state_slot);
204 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
206 case SACTION_SAVE_STATE:
207 ret = emu_save_state(state_slot);
208 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
211 case SACTION_ENTER_MENU:
212 toggle_fast_forward(1);
215 case SACTION_NEXT_SSLOT:
220 case SACTION_PREV_SSLOT:
225 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
226 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
228 printf("* %s\n", hud_msg);
230 case SACTION_TOGGLE_FSKIP:
231 pl_rearmed_cbs.fskip_advice = 0;
232 pl_rearmed_cbs.frameskip++;
233 if (pl_rearmed_cbs.frameskip > 1)
234 pl_rearmed_cbs.frameskip = -1;
235 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
236 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
237 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
238 plugin_call_rearmed_cbs();
240 case SACTION_SWITCH_DISPMODE:
241 pl_switch_dispmode();
242 plugin_call_rearmed_cbs();
243 if (GPU_open != NULL && GPU_close != NULL) {
245 GPU_open(&gpuDisp, "PCSX", NULL);
248 case SACTION_FAST_FORWARD:
249 toggle_fast_forward(0);
250 plugin_call_rearmed_cbs();
252 case SACTION_SCREENSHOT:
254 char buf[MAXPATHLEN];
257 time_t t = time(NULL);
258 struct tm *tb = localtime(&t);
259 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
260 tb->tm_min * 100 + tb->tm_sec;
262 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
263 get_gameid_filename(buf, sizeof(buf),
264 "screenshots/%.32s-%.9s.%d.png", ti);
266 if (scrbuf != 0 && bpp == 16)
267 ret = writepng(buf, scrbuf, w, h);
269 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
272 case SACTION_VOLUME_UP:
273 case SACTION_VOLUME_DOWN:
274 plat_target_step_volume(emu_action == SACTION_VOLUME_UP);
276 case SACTION_MINIMIZE:
277 if (GPU_close != NULL)
282 if (GPU_open != NULL) {
283 ret = GPU_open(&gpuDisp, "PCSX", NULL);
285 fprintf(stderr, "GPU_open returned %d\n", ret);
296 static int cdidcmp(const char *id1, const char *id2)
298 while (*id1 != 0 && *id2 != 0) {
299 if (*id1 == '_') { id1++; continue; }
300 if (*id2 == '_') { id2++; continue; }
310 static void parse_cwcheat(void)
312 char line[256], buf[64], name[64], *p;
317 f = fopen("cheatpops.db", "r");
322 while (fgets(line, sizeof(line), f)) {
323 if (sscanf(line, "_S %63s", buf) != 1)
325 if (cdidcmp(buf, CdromId) == 0)
332 printf("cwcheat section found for %s\n", CdromId);
333 while (fgets(line, sizeof(line), f))
335 p = line + strlen(line);
336 for (p--; p >= line && (*p == '\r' || *p == '\n' || *p == ' '); p--)
338 if (*p == 0 || *p == '#' || *p == ';')
341 if (strncmp(line, "_S", 2) == 0)
343 if (strncmp(line, "_G", 2) == 0) {
344 printf(" cwcheat game name: '%s'\n", line + 3);
347 if (strncmp(line, "_C0", 3) == 0) {
348 if (!newcheat && Cheats[NumCheats - 1].n == 0) {
349 printf("cheat '%s' failed to parse\n", name);
350 free(Cheats[NumCheats - 1].Descr);
353 snprintf(name, sizeof(name), "%s", line + 4);
357 if (sscanf(line, "_L %x %x", &a, &v) != 2) {
358 printf("line failed to parse: '%s'\n", line);
363 if (NumCheats >= NumCheatsAllocated) {
364 NumCheatsAllocated += 16;
365 Cheats = realloc(Cheats, sizeof(Cheat) *
370 Cheats[NumCheats].Descr = strdup(name);
371 Cheats[NumCheats].Enabled = 0;
372 Cheats[NumCheats].WasEnabled = 0;
373 Cheats[NumCheats].First = NumCodes;
374 Cheats[NumCheats].n = 0;
379 if (NumCodes >= NumCodesAllocated) {
380 NumCodesAllocated += 16;
381 CheatCodes = realloc(CheatCodes, sizeof(CheatCode) *
383 if (CheatCodes == NULL)
386 CheatCodes[NumCodes].Addr = a;
387 CheatCodes[NumCodes].Val = v;
389 Cheats[NumCheats - 1].n++;
396 void emu_on_new_cd(void)
402 printf("note: running with HLE BIOS, expect compatibility problems\n");
403 printf("----------------------------------------------------------\n");
406 snprintf(hud_msg, sizeof(hud_msg), "Booting up...");
410 int emu_core_preinit(void)
412 // what is the name of the config file?
413 // it may be redefined by -cfg on the command line
414 strcpy(cfgfile_basename, "pcsx.cfg");
419 memset(&Config, 0, sizeof(Config));
422 emu_set_default_config();
423 strcpy(Config.Bios, "HLE");
428 int emu_core_init(void)
433 if (EmuInit() == -1) {
434 printf("PSX emulator couldn't be initialized.\n");
438 LoadMcds(Config.Mcd1, Config.Mcd2);
448 int main(int argc, char *argv[])
450 char file[MAXPATHLEN] = "";
451 char path[MAXPATHLEN];
452 const char *cdfile = NULL;
453 const char *loadst_f = NULL;
460 // read command line options
461 for (i = 1; i < argc; i++) {
462 if (!strcmp(argv[i], "-psxout")) psxout = 1;
463 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
464 else if (!strcmp(argv[i], "-cfg")) {
465 if (i+1 >= argc) break;
466 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
467 printf("Using config file %s.\n", cfgfile_basename);
469 else if (!strcmp(argv[i], "-cdfile")) {
470 char isofilename[MAXPATHLEN];
472 if (i+1 >= argc) break;
473 strncpy(isofilename, argv[++i], MAXPATHLEN);
474 if (isofilename[0] != '/') {
475 getcwd(path, MAXPATHLEN);
476 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
478 strcat(path, isofilename);
479 strcpy(isofilename, path);
484 cdfile = isofilename;
486 else if (!strcmp(argv[i], "-loadf")) {
487 if (i+1 >= argc) break;
488 loadst_f = argv[++i];
490 else if (!strcmp(argv[i], "-h") ||
491 !strcmp(argv[i], "-help") ||
492 !strcmp(argv[i], "--help")) {
493 printf("PCSX-ReARMed " REV "\n");
495 " pcsx [options] [file]\n"
497 "\t-cdfile FILE\tRuns a CD image file\n"
498 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
499 "\t-psxout\t\tEnable PSX output\n"
500 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
501 "\t-h -help\tDisplay this message\n"
502 "\tfile\t\tLoads a PSX EXE file\n"));
505 strncpy(file, argv[i], MAXPATHLEN);
506 if (file[0] != '/') {
507 getcwd(path, MAXPATHLEN);
508 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
519 set_cd_image(cdfile);
522 // init input but leave probing to platform code,
523 // they add input drivers and may need to modify them after probe
527 menu_init(); // loads config
534 if (LoadPlugins() == -1) {
535 // FIXME: this recovery doesn't work, just delete bad config and bail out
536 // SysMessage("could not load plugins, retrying with defaults\n");
538 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
540 SysMessage("Failed loading plugins!");
545 if (OpenPlugins() == -1) {
548 plugin_call_rearmed_cbs();
553 if (file[0] != '\0') {
554 if (Load(file) != -1)
558 if (LoadCdrom() == -1) {
560 printf(_("Could not load CD-ROM!\n"));
571 // If a state has been specified, then load that
573 int ret = emu_load_state(loadst - 1);
574 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
577 int ret = LoadState(loadst_f);
578 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
589 emu_action = SACTION_NONE;
592 if (emu_action != SACTION_NONE)
599 static void toggle_fast_forward(int force_off)
601 static int fast_forward;
602 static int normal_g_opts;
603 static int normal_frameskip;
604 static int normal_enhancement_enable;
606 if (force_off && !fast_forward)
609 fast_forward = !fast_forward;
611 normal_g_opts = g_opts;
612 normal_frameskip = pl_rearmed_cbs.frameskip;
613 normal_enhancement_enable =
614 pl_rearmed_cbs.gpu_neon.enhancement_enable;
616 g_opts |= OPT_NO_FRAMELIM;
617 pl_rearmed_cbs.frameskip = 3;
618 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
620 g_opts = normal_g_opts;
621 pl_rearmed_cbs.frameskip = normal_frameskip;
622 pl_rearmed_cbs.gpu_neon.enhancement_enable =
623 normal_enhancement_enable;
625 pl_timing_prepare(Config.PsxType);
629 snprintf(hud_msg, sizeof(hud_msg), "FAST FORWARD %s",
630 fast_forward ? "ON" : "OFF");
635 printf("SysRunGui\n");
638 static void dummy_lace()
643 // rearmed hack: EmuReset() runs some code when real BIOS is used,
644 // but we usually do reset from menu while GPU is not open yet,
645 // so we need to prevent updateLace() call..
646 void *real_lace = GPU_updateLace;
647 GPU_updateLace = dummy_lace;
649 // reset can run code, timing must be set
650 pl_timing_prepare(Config.PsxType);
654 // hmh core forgets this
657 GPU_updateLace = real_lace;
666 if (emuLog != NULL) fclose(emuLog);
673 printf("OnFile_Exit\n");
682 int get_state_filename(char *buf, int size, int i) {
683 return get_gameid_filename(buf, size,
684 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
687 int emu_check_state(int slot)
689 char fname[MAXPATHLEN];
692 ret = get_state_filename(fname, sizeof(fname), slot);
696 return CheckState(fname);
699 int emu_save_state(int slot)
701 char fname[MAXPATHLEN];
704 ret = get_state_filename(fname, sizeof(fname), slot);
708 ret = SaveState(fname);
709 #ifndef __ARM_ARCH_7A__ /* XXX */
712 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
716 int emu_load_state(int slot)
718 char fname[MAXPATHLEN];
721 ret = get_state_filename(fname, sizeof(fname), slot);
725 return LoadState(fname);
728 void SysPrintf(const char *fmt, ...) {
733 vsprintf(msg, fmt, list);
736 fprintf(emuLog, "%s", msg);
739 void SysMessage(const char *fmt, ...) {
744 vsprintf(msg, fmt, list);
747 if (msg[strlen(msg) - 1] == '\n')
748 msg[strlen(msg) - 1] = 0;
750 fprintf(stderr, "%s\n", msg);
753 static void SignalExit(int sig) {
758 #define PARSEPATH(dst, src) \
759 ptr = src + strlen(src); \
760 while (*ptr != '\\' && ptr != src) ptr--; \
762 strcpy(dst, ptr+1); \
765 static int _OpenPlugins(void) {
768 signal(SIGINT, SignalExit);
769 signal(SIGPIPE, SignalExit);
771 GPU_clearDynarec(clearDynarec);
774 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
776 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
777 SPU_registerCallback(SPUirq);
778 // pcsx-rearmed: we handle gpu elsewhere
779 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
780 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
781 ret = PAD1_open(&gpuDisp);
782 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
783 ret = PAD2_open(&gpuDisp);
784 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
786 if (Config.UseNet && !NetOpened) {
788 char path[MAXPATHLEN];
789 char dotdir[MAXPATHLEN];
791 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
793 strcpy(info.EmuName, "PCSX");
794 strncpy(info.CdromID, CdromId, 9);
795 strncpy(info.CdromLabel, CdromLabel, 9);
797 info.GPU_showScreenPic = GPU_showScreenPic;
798 info.GPU_displayText = GPU_displayText;
799 info.GPU_showScreenPic = GPU_showScreenPic;
800 info.PAD_setSensitive = PAD1_setSensitive;
801 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
802 strcpy(info.BIOSpath, path);
803 strcpy(info.MCD1path, Config.Mcd1);
804 strcpy(info.MCD2path, Config.Mcd2);
805 sprintf(path, "%s%s", dotdir, Config.Gpu);
806 strcpy(info.GPUpath, path);
807 sprintf(path, "%s%s", dotdir, Config.Spu);
808 strcpy(info.SPUpath, path);
809 sprintf(path, "%s%s", dotdir, Config.Cdr);
810 strcpy(info.CDRpath, path);
813 ret = NET_open(&gpuDisp);
816 // -2 is returned when something in the info
817 // changed and needs to be synced
820 PARSEPATH(Config.Bios, info.BIOSpath);
821 PARSEPATH(Config.Gpu, info.GPUpath);
822 PARSEPATH(Config.Spu, info.SPUpath);
823 PARSEPATH(Config.Cdr, info.CDRpath);
825 strcpy(Config.Mcd1, info.MCD1path);
826 strcpy(Config.Mcd2, info.MCD2path);
829 Config.UseNet = FALSE;
832 if (NET_queryPlayer() == 1) {
833 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
835 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
839 } else if (Config.UseNet) {
849 while ((ret = _OpenPlugins()) == -2) {
851 LoadMcds(Config.Mcd1, Config.Mcd2);
852 if (LoadPlugins() == -1) return -1;
857 void ClosePlugins() {
860 signal(SIGINT, SIG_DFL);
861 signal(SIGPIPE, SIG_DFL);
863 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
865 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
867 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
869 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
870 // pcsx-rearmed: we handle gpu elsewhere
872 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
879 /* we hook statically linked plugins here */
880 static const char *builtin_plugins[] = {
881 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
885 static const int builtin_plugin_ids[] = {
886 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
890 void *SysLoadLibrary(const char *lib) {
891 const char *tmp = strrchr(lib, '/');
895 printf("plugin: %s\n", lib);
899 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
900 if (strcmp(tmp, builtin_plugins[i]) == 0)
901 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
904 ret = dlopen(lib, RTLD_NOW);
906 fprintf(stderr, "dlopen: %s\n", dlerror());
910 void *SysLoadSym(void *lib, const char *sym) {
911 unsigned int plugid = (unsigned int)(long)lib;
913 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
914 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
916 return dlsym(lib, sym);
919 const char *SysLibError() {
923 void SysCloseLibrary(void *lib) {
924 unsigned int plugid = (unsigned int)(long)lib;
926 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))