spu: threaded implementation
[pcsx_rearmed.git] / frontend / main.c
... / ...
CommitLineData
1/*
2 * (C) notaz, 2010-2012
3 *
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
8#include <stdio.h>
9#include <string.h>
10#include <stdarg.h>
11#include <unistd.h>
12#include <signal.h>
13#include <time.h>
14#ifndef _WIN32
15#include <dlfcn.h>
16#endif
17
18#include "main.h"
19#include "plugin.h"
20#include "plugin_lib.h"
21#include "pcnt.h"
22#include "menu.h"
23#include "plat.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"
28#include "../plugins/dfsound/spu_config.h"
29#include "revision.h"
30
31#ifndef NO_FRONTEND
32#include "libpicofe/input.h"
33#include "libpicofe/plat.h"
34#include "libpicofe/readpng.h"
35
36static void toggle_fast_forward(int force_off);
37static void check_profile(void);
38static void check_memcards(void);
39#endif
40#ifndef BOOT_MSG
41#define BOOT_MSG "Booting up..."
42#endif
43
44// don't include debug.h - it breaks ARM build (R1 redefined)
45void StartDebugger();
46void StopDebugger();
47
48int ready_to_go, g_emu_want_quit, g_emu_resetting;
49unsigned long gpuDisp;
50char cfgfile_basename[MAXPATHLEN];
51int state_slot;
52enum sched_action emu_action, emu_action_old;
53char hud_msg[64];
54int hud_new_msg;
55
56static void make_path(char *buf, size_t size, const char *dir, const char *fname)
57{
58 if (fname)
59 snprintf(buf, size, ".%s%s", dir, fname);
60 else
61 snprintf(buf, size, ".%s", dir);
62}
63#define MAKE_PATH(buf, dir, fname) \
64 make_path(buf, sizeof(buf), dir, fname)
65
66static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
67 char trimlabel[33];
68 int j;
69
70 strncpy(trimlabel, CdromLabel, 32);
71 trimlabel[32] = 0;
72 for (j = 31; j >= 0; j--)
73 if (trimlabel[j] == ' ')
74 trimlabel[j] = 0;
75 else
76 continue;
77
78 snprintf(buf, size, fmt, trimlabel, CdromId, i);
79
80 return 0;
81}
82
83void set_cd_image(const char *fname)
84{
85 const char *ext = NULL;
86
87 if (fname != NULL)
88 ext = strrchr(fname, '.');
89
90 if (ext && (
91 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
92 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
93 SetIsoFile(NULL);
94 cdrcimg_set_fname(fname);
95 strcpy(Config.Cdr, "builtin_cdrcimg");
96 } else {
97 SetIsoFile(fname);
98 strcpy(Config.Cdr, "builtin_cdr");
99 }
100}
101
102static void set_default_paths(void)
103{
104#ifndef NO_FRONTEND
105 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
106 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
107 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
108 strcpy(Config.BiosDir, "bios");
109#endif
110
111 strcpy(Config.PluginsDir, "plugins");
112 strcpy(Config.Gpu, "builtin_gpu");
113 strcpy(Config.Spu, "builtin_spu");
114 strcpy(Config.Cdr, "builtin_cdr");
115 strcpy(Config.Pad1, "builtin_pad");
116 strcpy(Config.Pad2, "builtin_pad");
117 strcpy(Config.Net, "Disabled");
118}
119
120void emu_set_default_config(void)
121{
122 // try to set sane config on which most games work
123 Config.Xa = Config.Cdda = Config.Sio =
124 Config.SpuIrq = Config.RCntFix = Config.VSyncWA = 0;
125 Config.PsxAuto = 1;
126
127 pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
128 pl_rearmed_cbs.gpu_neon.enhancement_enable =
129 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
130 pl_rearmed_cbs.gpu_peops.iUseDither = 0;
131 pl_rearmed_cbs.gpu_peops.dwActFixes = 1<<7;
132 pl_rearmed_cbs.gpu_unai.abe_hack =
133 pl_rearmed_cbs.gpu_unai.no_light =
134 pl_rearmed_cbs.gpu_unai.no_blend = 0;
135 memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
136 pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
137 pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
138
139 spu_config.iUseReverb = 1;
140 spu_config.iUseInterpolation = 1;
141 spu_config.iXAPitch = 0;
142 spu_config.iVolume = 768;
143 spu_config.iTempo = 0;
144 spu_config.iUseThread = 1; // no effect if only 1 core is detected
145#if defined(__arm__) && !defined(__ARM_ARCH_7A__) /* XXX GPH hack */
146 spu_config.iUseReverb = 0;
147 spu_config.iUseInterpolation = 0;
148 spu_config.iTempo = 1;
149#endif
150 new_dynarec_hacks = 0;
151 cycle_multiplier = 200;
152
153 in_type1 = PSE_PAD_TYPE_STANDARD;
154 in_type2 = PSE_PAD_TYPE_STANDARD;
155}
156
157void do_emu_action(void)
158{
159 int ret;
160
161 emu_action_old = emu_action;
162
163 switch (emu_action) {
164 case SACTION_LOAD_STATE:
165 ret = emu_load_state(state_slot);
166 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
167 break;
168 case SACTION_SAVE_STATE:
169 ret = emu_save_state(state_slot);
170 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
171 break;
172#ifndef NO_FRONTEND
173 case SACTION_ENTER_MENU:
174 toggle_fast_forward(1);
175 menu_loop();
176 return;
177 case SACTION_NEXT_SSLOT:
178 state_slot++;
179 if (state_slot > 9)
180 state_slot = 0;
181 goto do_state_slot;
182 case SACTION_PREV_SSLOT:
183 state_slot--;
184 if (state_slot < 0)
185 state_slot = 9;
186do_state_slot:
187 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
188 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
189 hud_new_msg = 3;
190 SysPrintf("* %s\n", hud_msg);
191 break;
192 case SACTION_TOGGLE_FSKIP:
193 pl_rearmed_cbs.fskip_advice = 0;
194 pl_rearmed_cbs.frameskip++;
195 if (pl_rearmed_cbs.frameskip > 1)
196 pl_rearmed_cbs.frameskip = -1;
197 snprintf(hud_msg, sizeof(hud_msg), "FRAMESKIP: %s",
198 pl_rearmed_cbs.frameskip == -1 ? "AUTO" :
199 pl_rearmed_cbs.frameskip == 0 ? "OFF" : "1" );
200 plugin_call_rearmed_cbs();
201 break;
202 case SACTION_SWITCH_DISPMODE:
203 pl_switch_dispmode();
204 plugin_call_rearmed_cbs();
205 if (GPU_open != NULL && GPU_close != NULL) {
206 GPU_close();
207 GPU_open(&gpuDisp, "PCSX", NULL);
208 }
209 break;
210 case SACTION_FAST_FORWARD:
211 toggle_fast_forward(0);
212 plugin_call_rearmed_cbs();
213 break;
214 case SACTION_TOGGLE_FPS:
215 if ((g_opts & (OPT_SHOWFPS|OPT_SHOWCPU))
216 == (OPT_SHOWFPS|OPT_SHOWCPU))
217 g_opts &= ~(OPT_SHOWFPS|OPT_SHOWCPU);
218 else if (g_opts & OPT_SHOWFPS)
219 g_opts |= OPT_SHOWCPU;
220 else
221 g_opts |= OPT_SHOWFPS;
222 break;
223 case SACTION_TOGGLE_FULLSCREEN:
224 plat_target.vout_fullscreen = !plat_target.vout_fullscreen;
225 if (GPU_open != NULL && GPU_close != NULL) {
226 GPU_close();
227 GPU_open(&gpuDisp, "PCSX", NULL);
228 }
229 break;
230 case SACTION_SCREENSHOT:
231 {
232 char buf[MAXPATHLEN];
233 void *scrbuf;
234 int w, h, bpp;
235 time_t t = time(NULL);
236 struct tm *tb = localtime(&t);
237 int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 +
238 tb->tm_min * 100 + tb->tm_sec;
239
240 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
241 get_gameid_filename(buf, sizeof(buf),
242 "screenshots/%.32s-%.9s.%d.png", ti);
243 ret = -1;
244 if (scrbuf != 0 && bpp == 16)
245 ret = writepng(buf, scrbuf, w, h);
246 if (ret == 0)
247 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
248 break;
249 }
250 case SACTION_VOLUME_UP:
251 case SACTION_VOLUME_DOWN:
252 {
253 static int volume;
254 plat_target_step_volume(&volume,
255 emu_action == SACTION_VOLUME_UP ? 1 : -1);
256 }
257 return;
258 case SACTION_MINIMIZE:
259 if (GPU_close != NULL)
260 GPU_close();
261
262 plat_minimize();
263
264 if (GPU_open != NULL) {
265 ret = GPU_open(&gpuDisp, "PCSX", NULL);
266 if (ret)
267 SysMessage("GPU_open returned %d", ret);
268 }
269 return;
270#endif
271 default:
272 return;
273 }
274
275 hud_new_msg = 3;
276}
277
278static char basic_lcase(char c)
279{
280 if ('A' <= c && c <= 'Z')
281 return c - 'A' + 'a';
282 return c;
283}
284
285static int cdidcmp(const char *id1, const char *id2)
286{
287 while (*id1 != 0 && *id2 != 0) {
288 if (*id1 == '_') { id1++; continue; }
289 if (*id2 == '_') { id2++; continue; }
290 if (basic_lcase(*id1) != basic_lcase(*id2))
291 break;
292 id1++;
293 id2++;
294 }
295
296 return *id1 - *id2;
297}
298
299static void parse_cwcheat(void)
300{
301 char line[256], buf[64], name[64], *p;
302 int newcheat = 1;
303 u32 a, v;
304 FILE *f;
305
306 f = fopen("cheatpops.db", "r");
307 if (f == NULL)
308 return;
309
310 /* find the game */
311 while (fgets(line, sizeof(line), f)) {
312 if (sscanf(line, "_S %63s", buf) != 1)
313 continue;
314 if (cdidcmp(buf, CdromId) == 0)
315 break;
316 }
317
318 if (feof(f))
319 goto out;
320
321 SysPrintf("cwcheat section found for %s\n", CdromId);
322 while (fgets(line, sizeof(line), f))
323 {
324 p = line + strlen(line);
325 for (p--; p >= line && (*p == '\r' || *p == '\n' || *p == ' '); p--)
326 *p = 0;
327 if (*p == 0 || *p == '#' || *p == ';')
328 continue;
329
330 if (strncmp(line, "_S", 2) == 0)
331 break;
332 if (strncmp(line, "_G", 2) == 0) {
333 SysPrintf(" cwcheat game name: '%s'\n", line + 3);
334 continue;
335 }
336 if (strncmp(line, "_C0", 3) == 0) {
337 if (!newcheat && Cheats[NumCheats - 1].n == 0) {
338 SysPrintf("cheat '%s' failed to parse\n", name);
339 free(Cheats[NumCheats - 1].Descr);
340 NumCheats--;
341 }
342 snprintf(name, sizeof(name), "%s", line + 4);
343 newcheat = 1;
344 continue;
345 }
346 if (sscanf(line, "_L %x %x", &a, &v) != 2) {
347 SysPrintf("line failed to parse: '%s'\n", line);
348 continue;
349 }
350
351 if (newcheat) {
352 if (NumCheats >= NumCheatsAllocated) {
353 NumCheatsAllocated += 16;
354 Cheats = realloc(Cheats, sizeof(Cheat) *
355 NumCheatsAllocated);
356 if (Cheats == NULL)
357 break;
358 }
359 Cheats[NumCheats].Descr = strdup(name);
360 Cheats[NumCheats].Enabled = 0;
361 Cheats[NumCheats].WasEnabled = 0;
362 Cheats[NumCheats].First = NumCodes;
363 Cheats[NumCheats].n = 0;
364 NumCheats++;
365 newcheat = 0;
366 }
367
368 if (NumCodes >= NumCodesAllocated) {
369 NumCodesAllocated += 16;
370 CheatCodes = realloc(CheatCodes, sizeof(CheatCode) *
371 NumCodesAllocated);
372 if (CheatCodes == NULL)
373 break;
374 }
375 CheatCodes[NumCodes].Addr = a;
376 CheatCodes[NumCodes].Val = v;
377 NumCodes++;
378 Cheats[NumCheats - 1].n++;
379 }
380
381out:
382 fclose(f);
383}
384
385void emu_on_new_cd(int show_hud_msg)
386{
387 ClearAllCheats();
388 parse_cwcheat();
389
390 if (Config.HLE) {
391 SysPrintf("note: running with HLE BIOS, expect compatibility problems\n");
392 SysPrintf("----------------------------------------------------------\n");
393 }
394
395 if (show_hud_msg) {
396 snprintf(hud_msg, sizeof(hud_msg), BOOT_MSG);
397 hud_new_msg = 3;
398 }
399}
400
401int emu_core_preinit(void)
402{
403 // what is the name of the config file?
404 // it may be redefined by -cfg on the command line
405 strcpy(cfgfile_basename, "pcsx.cfg");
406
407#ifdef IOS
408 emuLog = fopen("/User/Documents/pcsxr.log", "w");
409 if (emuLog == NULL)
410 emuLog = fopen("pcsxr.log", "w");
411 if (emuLog == NULL)
412#endif
413 emuLog = stdout;
414
415 SetIsoFile(NULL);
416
417 memset(&Config, 0, sizeof(Config));
418
419 set_default_paths();
420 emu_set_default_config();
421 strcpy(Config.Bios, "HLE");
422
423 return 0;
424}
425
426int emu_core_init(void)
427{
428 SysPrintf("Starting PCSX-ReARMed " REV "\n");
429
430#ifndef NO_FRONTEND
431 check_profile();
432 check_memcards();
433#endif
434
435 if (EmuInit() == -1) {
436 SysPrintf("PSX emulator couldn't be initialized.\n");
437 return -1;
438 }
439
440 LoadMcds(Config.Mcd1, Config.Mcd2);
441
442 if (Config.Debug) {
443 StartDebugger();
444 }
445
446 return 0;
447}
448
449void emu_core_ask_exit(void)
450{
451 stop = 1;
452 g_emu_want_quit = 1;
453}
454
455#ifndef NO_FRONTEND
456
457#include <sys/stat.h>
458#include <sys/types.h>
459
460static void create_profile_dir(const char *directory) {
461 char path[MAXPATHLEN];
462
463 MAKE_PATH(path, directory, NULL);
464 mkdir(path, S_IRWXU | S_IRWXG);
465}
466
467static void check_profile(void) {
468 // make sure that ~/.pcsx exists
469 create_profile_dir(PCSX_DOT_DIR);
470
471 create_profile_dir(BIOS_DIR);
472 create_profile_dir(MEMCARD_DIR);
473 create_profile_dir(STATES_DIR);
474 create_profile_dir(PLUGINS_DIR);
475 create_profile_dir(PLUGINS_CFG_DIR);
476 create_profile_dir(CHEATS_DIR);
477 create_profile_dir(PATCHES_DIR);
478 create_profile_dir(PCSX_DOT_DIR "cfg");
479 create_profile_dir("/screenshots/");
480}
481
482static void check_memcards(void)
483{
484 char buf[MAXPATHLEN];
485 FILE *f;
486 int i;
487
488 for (i = 1; i <= 9; i++) {
489 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
490
491 f = fopen(buf, "rb");
492 if (f == NULL) {
493 SysPrintf("Creating memcard: %s\n", buf);
494 CreateMcd(buf);
495 }
496 else
497 fclose(f);
498 }
499}
500
501int main(int argc, char *argv[])
502{
503 char file[MAXPATHLEN] = "";
504 char path[MAXPATHLEN];
505 const char *cdfile = NULL;
506 const char *loadst_f = NULL;
507 int psxout = 0;
508 int loadst = 0;
509 int i;
510
511 emu_core_preinit();
512
513 // read command line options
514 for (i = 1; i < argc; i++) {
515 if (!strcmp(argv[i], "-psxout")) psxout = 1;
516 else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]);
517 else if (!strcmp(argv[i], "-cfg")) {
518 if (i+1 >= argc) break;
519 strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */
520 SysPrintf("Using config file %s.\n", cfgfile_basename);
521 }
522 else if (!strcmp(argv[i], "-cdfile")) {
523 char isofilename[MAXPATHLEN];
524
525 if (i+1 >= argc) break;
526 strncpy(isofilename, argv[++i], MAXPATHLEN);
527 if (isofilename[0] != '/') {
528 getcwd(path, MAXPATHLEN);
529 if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) {
530 strcat(path, "/");
531 strcat(path, isofilename);
532 strcpy(isofilename, path);
533 } else
534 isofilename[0] = 0;
535 }
536
537 cdfile = isofilename;
538 }
539 else if (!strcmp(argv[i], "-loadf")) {
540 if (i+1 >= argc) break;
541 loadst_f = argv[++i];
542 }
543 else if (!strcmp(argv[i], "-h") ||
544 !strcmp(argv[i], "-help") ||
545 !strcmp(argv[i], "--help")) {
546 printf("PCSX-ReARMed " REV "\n");
547 printf("%s\n", _(
548 " pcsx [options] [file]\n"
549 "\toptions:\n"
550 "\t-cdfile FILE\tRuns a CD image file\n"
551 "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n"
552 "\t-psxout\t\tEnable PSX output\n"
553 "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n"
554 "\t-h -help\tDisplay this message\n"
555 "\tfile\t\tLoads a PSX EXE file\n"));
556 return 0;
557 } else {
558 strncpy(file, argv[i], MAXPATHLEN);
559 if (file[0] != '/') {
560 getcwd(path, MAXPATHLEN);
561 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
562 strcat(path, "/");
563 strcat(path, file);
564 strcpy(file, path);
565 } else
566 file[0] = 0;
567 }
568 }
569 }
570
571 if (cdfile)
572 set_cd_image(cdfile);
573
574 // frontend stuff
575 // init input but leave probing to platform code,
576 // they add input drivers and may need to modify them after probe
577 in_init();
578 pl_init();
579 plat_init();
580 menu_init(); // loads config
581
582 if (emu_core_init() != 0)
583 return 1;
584
585 if (psxout)
586 Config.PsxOut = 1;
587
588 if (LoadPlugins() == -1) {
589 // FIXME: this recovery doesn't work, just delete bad config and bail out
590 // SysMessage("could not load plugins, retrying with defaults\n");
591 set_default_paths();
592 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
593 remove(path);
594 SysMessage("Failed loading plugins!");
595 return 1;
596 }
597 pcnt_hook_plugins();
598
599 if (OpenPlugins() == -1) {
600 return 1;
601 }
602 plugin_call_rearmed_cbs();
603
604 CheckCdrom();
605 SysReset();
606
607 if (file[0] != '\0') {
608 if (Load(file) != -1)
609 ready_to_go = 1;
610 } else {
611 if (cdfile) {
612 if (LoadCdrom() == -1) {
613 ClosePlugins();
614 SysPrintf(_("Could not load CD-ROM!\n"));
615 return -1;
616 }
617 emu_on_new_cd(!loadst);
618 ready_to_go = 1;
619 }
620 }
621
622 if (loadst_f) {
623 int ret = LoadState(loadst_f);
624 SysPrintf("%s state file: %s\n",
625 ret ? "failed to load" : "loaded", loadst_f);
626 ready_to_go |= ret == 0;
627 }
628
629 if (ready_to_go) {
630 menu_prepare_emu();
631
632 // If a state has been specified, then load that
633 if (loadst) {
634 int ret = emu_load_state(loadst - 1);
635 SysPrintf("%s state %d\n",
636 ret ? "failed to load" : "loaded", loadst);
637 }
638 }
639 else
640 menu_loop();
641
642 pl_start_watchdog();
643
644 while (!g_emu_want_quit)
645 {
646 stop = 0;
647 emu_action = SACTION_NONE;
648
649 psxCpu->Execute();
650 if (emu_action != SACTION_NONE)
651 do_emu_action();
652 }
653
654 printf("Exit..\n");
655 ClosePlugins();
656 SysClose();
657 menu_finish();
658 plat_finish();
659
660 return 0;
661}
662
663static void toggle_fast_forward(int force_off)
664{
665 static int fast_forward;
666 static int normal_g_opts;
667 static int normal_frameskip;
668 static int normal_enhancement_enable;
669
670 if (force_off && !fast_forward)
671 return;
672
673 fast_forward = !fast_forward;
674 if (fast_forward) {
675 normal_g_opts = g_opts;
676 normal_frameskip = pl_rearmed_cbs.frameskip;
677 normal_enhancement_enable =
678 pl_rearmed_cbs.gpu_neon.enhancement_enable;
679
680 g_opts |= OPT_NO_FRAMELIM;
681 pl_rearmed_cbs.frameskip = 3;
682 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
683 } else {
684 g_opts = normal_g_opts;
685 pl_rearmed_cbs.frameskip = normal_frameskip;
686 pl_rearmed_cbs.gpu_neon.enhancement_enable =
687 normal_enhancement_enable;
688
689 pl_timing_prepare(Config.PsxType);
690 }
691
692 if (!force_off)
693 snprintf(hud_msg, sizeof(hud_msg), "FAST FORWARD %s",
694 fast_forward ? "ON" : "OFF");
695}
696
697static void SignalExit(int sig) {
698 // only to restore framebuffer/resolution on some devices
699 plat_finish();
700 exit(1);
701}
702#endif
703
704void SysRunGui() {
705 printf("SysRunGui\n");
706}
707
708static void dummy_lace()
709{
710}
711
712void SysReset() {
713 // rearmed hack: EmuReset() runs some code when real BIOS is used,
714 // but we usually do reset from menu while GPU is not open yet,
715 // so we need to prevent updateLace() call..
716 void *real_lace = GPU_updateLace;
717 GPU_updateLace = dummy_lace;
718 g_emu_resetting = 1;
719
720 // reset can run code, timing must be set
721 pl_timing_prepare(Config.PsxType);
722
723 EmuReset();
724
725 // hmh core forgets this
726 CDR_stop();
727
728 GPU_updateLace = real_lace;
729 g_emu_resetting = 0;
730}
731
732void SysClose() {
733 EmuShutdown();
734 ReleasePlugins();
735
736 StopDebugger();
737
738 if (emuLog != NULL && emuLog != stdout && emuLog != stderr) {
739 fclose(emuLog);
740 emuLog = NULL;
741 }
742}
743
744void SysUpdate() {
745}
746
747int get_state_filename(char *buf, int size, int i) {
748 return get_gameid_filename(buf, size,
749 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
750}
751
752int emu_check_state(int slot)
753{
754 char fname[MAXPATHLEN];
755 int ret;
756
757 ret = get_state_filename(fname, sizeof(fname), slot);
758 if (ret != 0)
759 return ret;
760
761 return CheckState(fname);
762}
763
764int emu_save_state(int slot)
765{
766 char fname[MAXPATHLEN];
767 int ret;
768
769 ret = get_state_filename(fname, sizeof(fname), slot);
770 if (ret != 0)
771 return ret;
772
773 ret = SaveState(fname);
774#if defined(__arm__) && !defined(__ARM_ARCH_7A__) /* XXX GPH hack */
775 sync();
776#endif
777 SysPrintf("* %s \"%s\" [%d]\n",
778 ret == 0 ? "saved" : "failed to save", fname, slot);
779 return ret;
780}
781
782int emu_load_state(int slot)
783{
784 char fname[MAXPATHLEN];
785 int ret;
786
787 hud_msg[0] = 0;
788
789 ret = get_state_filename(fname, sizeof(fname), slot);
790 if (ret != 0)
791 return ret;
792
793 return LoadState(fname);
794}
795
796#ifndef ANDROID
797
798void SysPrintf(const char *fmt, ...) {
799 va_list list;
800
801 va_start(list, fmt);
802 vfprintf(emuLog, fmt, list);
803 va_end(list);
804 fflush(emuLog);
805}
806
807#else
808
809#include <android/log.h>
810
811void SysPrintf(const char *fmt, ...) {
812 va_list list;
813
814 va_start(list, fmt);
815 __android_log_vprint(ANDROID_LOG_INFO, "PCSX", fmt, list);
816 va_end(list);
817}
818
819#endif
820
821void SysMessage(const char *fmt, ...) {
822 va_list list;
823 char msg[512];
824 int ret;
825
826 va_start(list, fmt);
827 ret = vsnprintf(msg, sizeof(msg), fmt, list);
828 va_end(list);
829
830 if (ret < sizeof(msg) && msg[ret - 1] == '\n')
831 msg[ret - 1] = 0;
832
833 SysPrintf("%s\n", msg);
834}
835
836#define PARSEPATH(dst, src) \
837 ptr = src + strlen(src); \
838 while (*ptr != '\\' && ptr != src) ptr--; \
839 if (ptr != src) { \
840 strcpy(dst, ptr+1); \
841 }
842
843static int _OpenPlugins(void) {
844 int ret;
845
846#ifndef NO_FRONTEND
847 signal(SIGINT, SignalExit);
848 signal(SIGPIPE, SignalExit);
849#endif
850
851 GPU_clearDynarec(clearDynarec);
852
853 ret = CDR_open();
854 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
855 ret = SPU_open();
856 if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; }
857 SPU_registerCallback(SPUirq);
858 SPU_registerScheduleCb(SPUschedule);
859 // pcsx-rearmed: we handle gpu elsewhere
860 //ret = GPU_open(&gpuDisp, "PCSX", NULL);
861 //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; }
862 ret = PAD1_open(&gpuDisp);
863 if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; }
864 ret = PAD2_open(&gpuDisp);
865 if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; }
866
867 if (Config.UseNet && !NetOpened) {
868 netInfo info;
869 char path[MAXPATHLEN];
870 char dotdir[MAXPATHLEN];
871
872 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
873
874 strcpy(info.EmuName, "PCSX");
875 strncpy(info.CdromID, CdromId, 9);
876 strncpy(info.CdromLabel, CdromLabel, 9);
877 info.psxMem = psxM;
878 info.GPU_showScreenPic = GPU_showScreenPic;
879 info.GPU_displayText = GPU_displayText;
880 info.GPU_showScreenPic = GPU_showScreenPic;
881 info.PAD_setSensitive = PAD1_setSensitive;
882 sprintf(path, "%s%s", Config.BiosDir, Config.Bios);
883 strcpy(info.BIOSpath, path);
884 strcpy(info.MCD1path, Config.Mcd1);
885 strcpy(info.MCD2path, Config.Mcd2);
886 sprintf(path, "%s%s", dotdir, Config.Gpu);
887 strcpy(info.GPUpath, path);
888 sprintf(path, "%s%s", dotdir, Config.Spu);
889 strcpy(info.SPUpath, path);
890 sprintf(path, "%s%s", dotdir, Config.Cdr);
891 strcpy(info.CDRpath, path);
892 NET_setInfo(&info);
893
894 ret = NET_open(&gpuDisp);
895 if (ret < 0) {
896 if (ret == -2) {
897 // -2 is returned when something in the info
898 // changed and needs to be synced
899 char *ptr;
900
901 PARSEPATH(Config.Bios, info.BIOSpath);
902 PARSEPATH(Config.Gpu, info.GPUpath);
903 PARSEPATH(Config.Spu, info.SPUpath);
904 PARSEPATH(Config.Cdr, info.CDRpath);
905
906 strcpy(Config.Mcd1, info.MCD1path);
907 strcpy(Config.Mcd2, info.MCD2path);
908 return -2;
909 } else {
910 Config.UseNet = FALSE;
911 }
912 } else {
913 if (NET_queryPlayer() == 1) {
914 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
915 } else {
916 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
917 }
918 }
919 NetOpened = TRUE;
920 } else if (Config.UseNet) {
921 NET_resume();
922 }
923
924 return 0;
925}
926
927int OpenPlugins() {
928 int ret;
929
930 while ((ret = _OpenPlugins()) == -2) {
931 ReleasePlugins();
932 LoadMcds(Config.Mcd1, Config.Mcd2);
933 if (LoadPlugins() == -1) return -1;
934 }
935 return ret;
936}
937
938void ClosePlugins() {
939 int ret;
940
941#ifndef NO_FRONTEND
942 signal(SIGINT, SIG_DFL);
943 signal(SIGPIPE, SIG_DFL);
944#endif
945
946 ret = CDR_close();
947 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
948 ret = SPU_close();
949 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
950 ret = PAD1_close();
951 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
952 ret = PAD2_close();
953 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
954 // pcsx-rearmed: we handle gpu elsewhere
955 //ret = GPU_close();
956 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
957
958 if (Config.UseNet) {
959 NET_pause();
960 }
961}
962
963/* we hook statically linked plugins here */
964static const char *builtin_plugins[] = {
965 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
966 "builtin_cdrcimg",
967};
968
969static const int builtin_plugin_ids[] = {
970 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
971 PLUGIN_CDRCIMG,
972};
973
974void *SysLoadLibrary(const char *lib) {
975 const char *tmp = strrchr(lib, '/');
976 void *ret = NULL;
977 int i;
978
979 SysPrintf("plugin: %s\n", lib);
980
981 if (tmp != NULL) {
982 tmp++;
983 for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++)
984 if (strcmp(tmp, builtin_plugins[i]) == 0)
985 return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]);
986 }
987
988#ifndef _WIN32
989 ret = dlopen(lib, RTLD_NOW);
990 if (ret == NULL)
991 SysMessage("dlopen: %s", dlerror());
992#else
993 /* no external plugin support, abi is no longer
994 * compatible with psemu/pcsx anyway */
995#endif
996 return ret;
997}
998
999void *SysLoadSym(void *lib, const char *sym) {
1000 unsigned int plugid = (unsigned int)(long)lib;
1001
1002 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
1003 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
1004
1005#ifndef _WIN32
1006 return dlsym(lib, sym);
1007#else
1008 return NULL;
1009#endif
1010}
1011
1012const char *SysLibError() {
1013#ifndef _WIN32
1014 return dlerror();
1015#else
1016 return "not supported";
1017#endif
1018}
1019
1020void SysCloseLibrary(void *lib) {
1021 unsigned int plugid = (unsigned int)(long)lib;
1022
1023 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
1024 return;
1025
1026#ifndef _WIN32
1027 dlclose(lib);
1028#endif
1029}
1030