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