spu: stop at 0 volume
[pcsx_rearmed.git] / frontend / main.c
CommitLineData
e906c010 1/*
dde7da71 2 * (C) notaz, 2010-2012
e906c010 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
80c2304e 8#include <stdio.h>
9#include <string.h>
10#include <stdarg.h>
11#include <dlfcn.h>
12#include <sys/stat.h>
13#include <sys/types.h>
14#include <unistd.h>
c5061935 15#include <signal.h>
29a8c4f3 16#include <time.h>
80c2304e 17
c5061935 18#include "main.h"
e906c010 19#include "plugin.h"
7c0f51de 20#include "plugin_lib.h"
14dffdb7 21#include "pcnt.h"
3c70c47b 22#include "menu.h"
47821672 23#include "plat.h"
80c2304e 24#include "../libpcsxcore/misc.h"
9c27c205 25#include "../libpcsxcore/cheat.h"
799b0b87 26#include "../libpcsxcore/new_dynarec/new_dynarec.h"
47bf65ab 27#include "../plugins/cdrcimg/cdrcimg.h"
38c2028e 28#include "revision.h"
80c2304e 29
cc56203b 30#ifndef NO_FRONTEND
31#include "libpicofe/input.h"
32#include "libpicofe/plat.h"
33#include "libpicofe/readpng.h"
34#endif
a1b44e36 35#ifndef BOOT_MSG
36#define BOOT_MSG "Booting up..."
37#endif
cc56203b 38
fc8145b7 39// don't include debug.h - it breaks ARM build (R1 redefined)
40void StartDebugger();
41void StopDebugger();
42
33400707 43// sound plugin
44extern int iUseReverb;
45extern int iUseInterpolation;
46extern int iXAPitch;
33400707 47extern int iVolume;
48
69af03a2 49int ready_to_go;
c5061935 50unsigned long gpuDisp;
51char cfgfile_basename[MAXPATHLEN];
8f892648 52int state_slot;
53enum sched_action emu_action, emu_action_old;
54char hud_msg[64];
55int hud_new_msg;
56
456f1b86 57static void toggle_fast_forward(int force_off);
58
80c2304e 59static void make_path(char *buf, size_t size, const char *dir, const char *fname)
60{
61 if (fname)
62 snprintf(buf, size, ".%s%s", dir, fname);
63 else
64 snprintf(buf, size, ".%s", dir);
65}
66#define MAKE_PATH(buf, dir, fname) \
67 make_path(buf, sizeof(buf), dir, fname)
68
69static void create_profile_dir(const char *directory) {
70 char path[MAXPATHLEN];
71
72 MAKE_PATH(path, directory, NULL);
73 mkdir(path, S_IRWXU | S_IRWXG);
74}
75
76static void CheckSubDir() {
77 // make sure that ~/.pcsx exists
78 create_profile_dir(PCSX_DOT_DIR);
79
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);
cd6e8d0f 87 create_profile_dir(PCSX_DOT_DIR "cfg");
29a8c4f3 88 create_profile_dir("/screenshots/");
89}
90
91static int get_gameid_filename(char *buf, int size, const char *fmt, int i) {
92 char trimlabel[33];
93 int j;
94
95 strncpy(trimlabel, CdromLabel, 32);
96 trimlabel[32] = 0;
97 for (j = 31; j >= 0; j--)
98 if (trimlabel[j] == ' ')
99 trimlabel[j] = 0;
100 else
101 continue;
102
103 snprintf(buf, size, fmt, trimlabel, CdromId, i);
104
105 return 0;
80c2304e 106}
107
47bf65ab 108void set_cd_image(const char *fname)
109{
e16a7e51 110 const char *ext = NULL;
47bf65ab 111
33716956 112 if (fname != NULL)
113 ext = strrchr(fname, '.');
47bf65ab 114
33716956 115 if (ext && (
116 strcasecmp(ext, ".z") == 0 || strcasecmp(ext, ".bz") == 0 ||
9a92bffb 117 strcasecmp(ext, ".znx") == 0 /*|| strcasecmp(ext, ".pbp") == 0*/)) {
47bf65ab 118 SetIsoFile(NULL);
119 cdrcimg_set_fname(fname);
120 strcpy(Config.Cdr, "builtin_cdrcimg");
121 } else {
122 SetIsoFile(fname);
123 strcpy(Config.Cdr, "builtin_cdr");
124 }
125}
126
6fe1f056 127static void set_default_paths(void)
128{
129 MAKE_PATH(Config.Mcd1, MEMCARD_DIR, "card1.mcd");
130 MAKE_PATH(Config.Mcd2, MEMCARD_DIR, "card2.mcd");
131 strcpy(Config.BiosDir, "bios");
132
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");
6fe1f056 140
141 snprintf(Config.PatchesDir, sizeof(Config.PatchesDir), "." PATCHES_DIR);
142}
143
33400707 144void emu_set_default_config(void)
145{
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;
150 Config.PsxAuto = 1;
151
746fee51 152 pl_rearmed_cbs.gpu_neon.allow_interlace = 2; // auto
0b02eb77 153 pl_rearmed_cbs.gpu_neon.enhancement_enable =
154 pl_rearmed_cbs.gpu_neon.enhancement_no_main = 0;
33400707 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;
e60da159 160 memset(&pl_rearmed_cbs.gpu_peopsgl, 0, sizeof(pl_rearmed_cbs.gpu_peopsgl));
746fee51 161 pl_rearmed_cbs.gpu_peopsgl.iVRamSize = 64;
cbb26048 162 pl_rearmed_cbs.gpu_peopsgl.iTexGarbageCollection = 1;
33400707 163
164 iUseReverb = 2;
165 iUseInterpolation = 1;
166 iXAPitch = 0;
33400707 167 iVolume = 768;
168#ifndef __ARM_ARCH_7A__ /* XXX */
169 iUseReverb = 0;
170 iUseInterpolation = 0;
171#endif
172 new_dynarec_hacks = 0;
173 cycle_multiplier = 200;
174
175 in_type1 = PSE_PAD_TYPE_STANDARD;
176 in_type2 = PSE_PAD_TYPE_STANDARD;
177}
178
51f77282 179static void check_memcards(void)
180{
181 char buf[MAXPATHLEN];
182 FILE *f;
183 int i;
184
185 for (i = 1; i <= 9; i++) {
186 snprintf(buf, sizeof(buf), ".%scard%d.mcd", MEMCARD_DIR, i);
187
188 f = fopen(buf, "rb");
189 if (f == NULL) {
190 printf("Creating memcard: %s\n", buf);
191 CreateMcd(buf);
192 }
193 else
194 fclose(f);
195 }
196}
197
8f892648 198void do_emu_action(void)
199{
200 int ret;
201
202 emu_action_old = emu_action;
203
204 switch (emu_action) {
8f892648 205 case SACTION_LOAD_STATE:
206 ret = emu_load_state(state_slot);
207 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "LOADED" : "FAIL!");
208 break;
209 case SACTION_SAVE_STATE:
210 ret = emu_save_state(state_slot);
211 snprintf(hud_msg, sizeof(hud_msg), ret == 0 ? "SAVED" : "FAIL!");
212 break;
38c2028e 213#ifndef NO_FRONTEND
214 case SACTION_ENTER_MENU:
456f1b86 215 toggle_fast_forward(1);
38c2028e 216 menu_loop();
217 return;
8f892648 218 case SACTION_NEXT_SSLOT:
219 state_slot++;
220 if (state_slot > 9)
221 state_slot = 0;
222 goto do_state_slot;
223 case SACTION_PREV_SSLOT:
224 state_slot--;
225 if (state_slot < 0)
226 state_slot = 9;
38c2028e 227do_state_slot:
228 snprintf(hud_msg, sizeof(hud_msg), "STATE SLOT %d [%s]", state_slot,
229 emu_check_state(state_slot) == 0 ? "USED" : "FREE");
230 hud_new_msg = 3;
231 printf("* %s\n", hud_msg);
232 break;
8f892648 233 case SACTION_TOGGLE_FSKIP:
ea4a16e7 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();
8f892648 242 break;
fa56d360 243 case SACTION_SWITCH_DISPMODE:
244 pl_switch_dispmode();
d81b8e97 245 plugin_call_rearmed_cbs();
fa56d360 246 if (GPU_open != NULL && GPU_close != NULL) {
247 GPU_close();
248 GPU_open(&gpuDisp, "PCSX", NULL);
249 }
d81b8e97 250 break;
456f1b86 251 case SACTION_FAST_FORWARD:
252 toggle_fast_forward(0);
253 plugin_call_rearmed_cbs();
254 break;
dde7da71 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;
261 else
262 g_opts |= OPT_SHOWFPS;
263 break;
29a8c4f3 264 case SACTION_SCREENSHOT:
265 {
38c2028e 266 char buf[MAXPATHLEN];
29a8c4f3 267 void *scrbuf;
268 int w, h, bpp;
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;
273
274 scrbuf = pl_prepare_screenshot(&w, &h, &bpp);
275 get_gameid_filename(buf, sizeof(buf),
276 "screenshots/%.32s-%.9s.%d.png", ti);
277 ret = -1;
278 if (scrbuf != 0 && bpp == 16)
279 ret = writepng(buf, scrbuf, w, h);
280 if (ret == 0)
281 snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN");
282 break;
283 }
221be40d 284 case SACTION_VOLUME_UP:
285 case SACTION_VOLUME_DOWN:
cc56203b 286 plat_target_step_volume(emu_action == SACTION_VOLUME_UP);
221be40d 287 return;
a805c855 288 case SACTION_MINIMIZE:
36dfb787 289 if (GPU_close != NULL)
290 GPU_close();
291
a805c855 292 plat_minimize();
36dfb787 293
294 if (GPU_open != NULL) {
295 ret = GPU_open(&gpuDisp, "PCSX", NULL);
296 if (ret)
297 fprintf(stderr, "GPU_open returned %d\n", ret);
298 }
a805c855 299 return;
38c2028e 300#endif
4c08b9e7 301 default:
302 return;
8f892648 303 }
8f892648 304
8f892648 305 hud_new_msg = 3;
306}
307
9c27c205 308static int cdidcmp(const char *id1, const char *id2)
309{
310 while (*id1 != 0 && *id2 != 0) {
311 if (*id1 == '_') { id1++; continue; }
312 if (*id2 == '_') { id2++; continue; }
313 if (*id1 != *id2)
314 break;
315 id1++;
316 id2++;
317 }
318
319 return *id1 - *id2;
320}
321
322static void parse_cwcheat(void)
323{
324 char line[256], buf[64], name[64], *p;
325 int newcheat = 1;
326 u32 a, v;
327 FILE *f;
328
329 f = fopen("cheatpops.db", "r");
330 if (f == NULL)
331 return;
332
333 /* find the game */
334 while (fgets(line, sizeof(line), f)) {
335 if (sscanf(line, "_S %63s", buf) != 1)
336 continue;
337 if (cdidcmp(buf, CdromId) == 0)
338 break;
339 }
340
341 if (feof(f))
342 goto out;
343
344 printf("cwcheat section found for %s\n", CdromId);
345 while (fgets(line, sizeof(line), f))
346 {
347 p = line + strlen(line);
348 for (p--; p >= line && (*p == '\r' || *p == '\n' || *p == ' '); p--)
349 *p = 0;
350 if (*p == 0 || *p == '#' || *p == ';')
351 continue;
352
353 if (strncmp(line, "_S", 2) == 0)
354 break;
355 if (strncmp(line, "_G", 2) == 0) {
356 printf(" cwcheat game name: '%s'\n", line + 3);
357 continue;
358 }
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);
363 NumCheats--;
364 }
365 snprintf(name, sizeof(name), "%s", line + 4);
366 newcheat = 1;
367 continue;
368 }
369 if (sscanf(line, "_L %x %x", &a, &v) != 2) {
370 printf("line failed to parse: '%s'\n", line);
371 continue;
372 }
373
374 if (newcheat) {
375 if (NumCheats >= NumCheatsAllocated) {
376 NumCheatsAllocated += 16;
377 Cheats = realloc(Cheats, sizeof(Cheat) *
378 NumCheatsAllocated);
379 if (Cheats == NULL)
380 break;
381 }
382 Cheats[NumCheats].Descr = strdup(name);
383 Cheats[NumCheats].Enabled = 0;
2c843d96 384 Cheats[NumCheats].WasEnabled = 0;
9c27c205 385 Cheats[NumCheats].First = NumCodes;
386 Cheats[NumCheats].n = 0;
387 NumCheats++;
388 newcheat = 0;
389 }
390
391 if (NumCodes >= NumCodesAllocated) {
392 NumCodesAllocated += 16;
393 CheatCodes = realloc(CheatCodes, sizeof(CheatCode) *
394 NumCodesAllocated);
395 if (CheatCodes == NULL)
396 break;
397 }
398 CheatCodes[NumCodes].Addr = a;
399 CheatCodes[NumCodes].Val = v;
400 NumCodes++;
401 Cheats[NumCheats - 1].n++;
402 }
403
404out:
405 fclose(f);
406}
407
79f216e3 408void emu_on_new_cd(int show_hud_msg)
9c27c205 409{
410 ClearAllCheats();
411 parse_cwcheat();
412
413 if (Config.HLE) {
414 printf("note: running with HLE BIOS, expect compatibility problems\n");
415 printf("----------------------------------------------------------\n");
416 }
417
79f216e3 418 if (show_hud_msg) {
419 snprintf(hud_msg, sizeof(hud_msg), BOOT_MSG);
420 hud_new_msg = 3;
421 }
9c27c205 422}
423
38c2028e 424int emu_core_preinit(void)
80c2304e 425{
80c2304e 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");
429
e906c010 430 emuLog = stdout;
80c2304e 431 SetIsoFile(NULL);
80c2304e 432
e0c692d9 433 memset(&Config, 0, sizeof(Config));
434
e0c692d9 435 set_default_paths();
33400707 436 emu_set_default_config();
e0c692d9 437 strcpy(Config.Bios, "HLE");
438
38c2028e 439 return 0;
440}
441
442int emu_core_init(void)
443{
444 CheckSubDir();
445 check_memcards();
446
447 if (EmuInit() == -1) {
448 printf("PSX emulator couldn't be initialized.\n");
449 return -1;
450 }
451
452 LoadMcds(Config.Mcd1, Config.Mcd2);
453
454 if (Config.Debug) {
455 StartDebugger();
456 }
457
458 return 0;
459}
460
461#ifndef NO_FRONTEND
462int main(int argc, char *argv[])
463{
e0c692d9 464 char file[MAXPATHLEN] = "";
465 char path[MAXPATHLEN];
466 const char *cdfile = NULL;
de910c6b 467 const char *loadst_f = NULL;
0a151868 468 int psxout = 0;
e0c692d9 469 int loadst = 0;
470 int i;
471
38c2028e 472 emu_core_preinit();
473
80c2304e 474 // read command line options
475 for (i = 1; i < argc; i++) {
0a151868 476 if (!strcmp(argv[i], "-psxout")) psxout = 1;
80c2304e 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);
482 }
483 else if (!strcmp(argv[i], "-cdfile")) {
484 char isofilename[MAXPATHLEN];
485
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) {
491 strcat(path, "/");
492 strcat(path, isofilename);
493 strcpy(isofilename, path);
494 } else
495 isofilename[0] = 0;
496 }
497
47bf65ab 498 cdfile = isofilename;
80c2304e 499 }
de910c6b 500 else if (!strcmp(argv[i], "-loadf")) {
501 if (i+1 >= argc) break;
502 loadst_f = argv[++i];
503 }
80c2304e 504 else if (!strcmp(argv[i], "-h") ||
505 !strcmp(argv[i], "-help") ||
506 !strcmp(argv[i], "--help")) {
38c2028e 507 printf("PCSX-ReARMed " REV "\n");
80c2304e 508 printf("%s\n", _(
509 " pcsx [options] [file]\n"
510 "\toptions:\n"
511 "\t-cdfile FILE\tRuns a CD image file\n"
80c2304e 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"
38c2028e 516 "\tfile\t\tLoads a PSX EXE file\n"));
80c2304e 517 return 0;
518 } else {
519 strncpy(file, argv[i], MAXPATHLEN);
520 if (file[0] != '/') {
521 getcwd(path, MAXPATHLEN);
522 if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) {
523 strcat(path, "/");
524 strcat(path, file);
525 strcpy(file, path);
526 } else
527 file[0] = 0;
528 }
529 }
530 }
531
47bf65ab 532 if (cdfile)
533 set_cd_image(cdfile);
534
69af03a2 535 // frontend stuff
b07c18e8 536 // init input but leave probing to platform code,
537 // they add input drivers and may need to modify them after probe
69af03a2 538 in_init();
59f1c85c 539 pl_init();
69af03a2 540 plat_init();
0a151868 541 menu_init(); // loads config
542
38c2028e 543 emu_core_init();
544
0a151868 545 if (psxout)
546 Config.PsxOut = 1;
80c2304e 547
69af03a2 548 if (LoadPlugins() == -1) {
6fe1f056 549 // FIXME: this recovery doesn't work, just delete bad config and bail out
550 // SysMessage("could not load plugins, retrying with defaults\n");
551 set_default_paths();
552 snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename);
553 remove(path);
69af03a2 554 SysMessage("Failed loading plugins!");
555 return 1;
556 }
557 pcnt_hook_plugins();
80c2304e 558
69af03a2 559 if (OpenPlugins() == -1) {
560 return 1;
561 }
201c21e2 562 plugin_call_rearmed_cbs();
80c2304e 563
69af03a2 564 CheckCdrom();
bbd837c6 565 SysReset();
69af03a2 566
567 if (file[0] != '\0') {
568 if (Load(file) != -1)
569 ready_to_go = 1;
570 } else {
47bf65ab 571 if (cdfile) {
69af03a2 572 if (LoadCdrom() == -1) {
573 ClosePlugins();
574 printf(_("Could not load CD-ROM!\n"));
575 return -1;
80c2304e 576 }
79f216e3 577 emu_on_new_cd(!loadst);
69af03a2 578 ready_to_go = 1;
80c2304e 579 }
69af03a2 580 }
80c2304e 581
45d45c1e 582 if (ready_to_go) {
3c70c47b 583 menu_prepare_emu();
45d45c1e 584
585 // If a state has been specified, then load that
586 if (loadst) {
587 int ret = emu_load_state(loadst - 1);
588 printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst);
589 }
de910c6b 590 if (loadst_f) {
591 int ret = LoadState(loadst_f);
592 printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f);
593 }
45d45c1e 594 }
3c70c47b 595 else
69af03a2 596 menu_loop();
597
7c0f51de 598 pl_start_watchdog();
599
69af03a2 600 while (1)
601 {
799b0b87 602 stop = 0;
8f892648 603 emu_action = SACTION_NONE;
604
80c2304e 605 psxCpu->Execute();
8f892648 606 if (emu_action != SACTION_NONE)
607 do_emu_action();
80c2304e 608 }
609
80c2304e 610 return 0;
611}
456f1b86 612
613static void toggle_fast_forward(int force_off)
614{
615 static int fast_forward;
616 static int normal_g_opts;
617 static int normal_frameskip;
618 static int normal_enhancement_enable;
619
620 if (force_off && !fast_forward)
621 return;
622
623 fast_forward = !fast_forward;
624 if (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;
629
630 g_opts |= OPT_NO_FRAMELIM;
631 pl_rearmed_cbs.frameskip = 3;
632 pl_rearmed_cbs.gpu_neon.enhancement_enable = 0;
633 } else {
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;
638
639 pl_timing_prepare(Config.PsxType);
640 }
641
642 if (!force_off)
643 snprintf(hud_msg, sizeof(hud_msg), "FAST FORWARD %s",
644 fast_forward ? "ON" : "OFF");
645}
38c2028e 646#endif
80c2304e 647
648void SysRunGui() {
649 printf("SysRunGui\n");
650}
651
e6eb2066 652static void dummy_lace()
653{
654}
655
80c2304e 656void SysReset() {
e6eb2066 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;
662
33400707 663 // reset can run code, timing must be set
664 pl_timing_prepare(Config.PsxType);
665
80c2304e 666 EmuReset();
bd6267e6 667
668 // hmh core forgets this
669 CDR_stop();
e6eb2066 670
671 GPU_updateLace = real_lace;
80c2304e 672}
673
674void SysClose() {
675 EmuShutdown();
676 ReleasePlugins();
677
678 StopDebugger();
679
680 if (emuLog != NULL) fclose(emuLog);
681}
682
683void SysUpdate() {
80c2304e 684}
685
686void OnFile_Exit() {
bd6267e6 687 printf("OnFile_Exit\n");
bd6267e6 688 SysClose();
38c2028e 689#ifndef NO_FRONTEND
690 menu_finish();
2c886904 691 plat_finish();
80c2304e 692 exit(0);
38c2028e 693#endif
80c2304e 694}
695
c5061935 696int get_state_filename(char *buf, int size, int i) {
29a8c4f3 697 return get_gameid_filename(buf, size,
698 "." STATES_DIR "%.32s-%.9s.%3.3d", i);
80c2304e 699}
700
8f892648 701int emu_check_state(int slot)
702{
703 char fname[MAXPATHLEN];
704 int ret;
705
706 ret = get_state_filename(fname, sizeof(fname), slot);
707 if (ret != 0)
708 return ret;
709
710 return CheckState(fname);
711}
712
713int emu_save_state(int slot)
714{
715 char fname[MAXPATHLEN];
716 int ret;
717
718 ret = get_state_filename(fname, sizeof(fname), slot);
719 if (ret != 0)
720 return ret;
721
90f1d26c 722 ret = SaveState(fname);
0b1dbe6b 723#ifndef __ARM_ARCH_7A__ /* XXX */
724 sync();
725#endif
90f1d26c 726 printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot);
727 return ret;
8f892648 728}
729
730int emu_load_state(int slot)
731{
732 char fname[MAXPATHLEN];
733 int ret;
734
dde7da71 735 hud_msg[0] = 0;
736
8f892648 737 ret = get_state_filename(fname, sizeof(fname), slot);
738 if (ret != 0)
739 return ret;
740
741 return LoadState(fname);
742}
743
80c2304e 744void SysPrintf(const char *fmt, ...) {
745 va_list list;
746 char msg[512];
747
748 va_start(list, fmt);
749 vsprintf(msg, fmt, list);
750 va_end(list);
751
80c2304e 752 fprintf(emuLog, "%s", msg);
753}
754
755void SysMessage(const char *fmt, ...) {
756 va_list list;
757 char msg[512];
758
759 va_start(list, fmt);
760 vsprintf(msg, fmt, list);
761 va_end(list);
762
763 if (msg[strlen(msg) - 1] == '\n')
764 msg[strlen(msg) - 1] = 0;
765
766 fprintf(stderr, "%s\n", msg);
767}
768
c5061935 769static void SignalExit(int sig) {
770 ClosePlugins();
771 OnFile_Exit();
772}
773
774#define PARSEPATH(dst, src) \
775 ptr = src + strlen(src); \
776 while (*ptr != '\\' && ptr != src) ptr--; \
777 if (ptr != src) { \
778 strcpy(dst, ptr+1); \
779 }
780
781static int _OpenPlugins(void) {
782 int ret;
783
784 signal(SIGINT, SignalExit);
785 signal(SIGPIPE, SignalExit);
786
787 GPU_clearDynarec(clearDynarec);
788
789 ret = CDR_open();
790 if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; }
791 ret = SPU_open();
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; }
801
802 if (Config.UseNet && !NetOpened) {
803 netInfo info;
804 char path[MAXPATHLEN];
805 char dotdir[MAXPATHLEN];
806
807 MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL);
808
38c2028e 809 strcpy(info.EmuName, "PCSX");
c5061935 810 strncpy(info.CdromID, CdromId, 9);
811 strncpy(info.CdromLabel, CdromLabel, 9);
812 info.psxMem = psxM;
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);
827 NET_setInfo(&info);
828
829 ret = NET_open(&gpuDisp);
830 if (ret < 0) {
831 if (ret == -2) {
832 // -2 is returned when something in the info
833 // changed and needs to be synced
834 char *ptr;
835
836 PARSEPATH(Config.Bios, info.BIOSpath);
837 PARSEPATH(Config.Gpu, info.GPUpath);
838 PARSEPATH(Config.Spu, info.SPUpath);
839 PARSEPATH(Config.Cdr, info.CDRpath);
840
841 strcpy(Config.Mcd1, info.MCD1path);
842 strcpy(Config.Mcd2, info.MCD2path);
843 return -2;
844 } else {
845 Config.UseNet = FALSE;
846 }
847 } else {
848 if (NET_queryPlayer() == 1) {
849 if (SendPcsxInfo() == -1) Config.UseNet = FALSE;
850 } else {
851 if (RecvPcsxInfo() == -1) Config.UseNet = FALSE;
852 }
853 }
854 NetOpened = TRUE;
855 } else if (Config.UseNet) {
856 NET_resume();
857 }
858
859 return 0;
860}
861
862int OpenPlugins() {
863 int ret;
864
865 while ((ret = _OpenPlugins()) == -2) {
866 ReleasePlugins();
867 LoadMcds(Config.Mcd1, Config.Mcd2);
868 if (LoadPlugins() == -1) return -1;
869 }
870 return ret;
871}
872
873void ClosePlugins() {
874 int ret;
875
876 signal(SIGINT, SIG_DFL);
877 signal(SIGPIPE, SIG_DFL);
878 ret = CDR_close();
879 if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; }
880 ret = SPU_close();
881 if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; }
882 ret = PAD1_close();
883 if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; }
884 ret = PAD2_close();
885 if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; }
886 // pcsx-rearmed: we handle gpu elsewhere
887 //ret = GPU_close();
888 //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; }
889
890 if (Config.UseNet) {
891 NET_pause();
892 }
893}
894
e906c010 895/* we hook statically linked plugins here */
896static const char *builtin_plugins[] = {
47bf65ab 897 "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad",
898 "builtin_cdrcimg",
e906c010 899};
900
901static const int builtin_plugin_ids[] = {
902 PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD,
47bf65ab 903 PLUGIN_CDRCIMG,
e906c010 904};
905
80c2304e 906void *SysLoadLibrary(const char *lib) {
e906c010 907 const char *tmp = strrchr(lib, '/');
bbd837c6 908 void *ret;
e906c010 909 int i;
910
bbd837c6 911 printf("plugin: %s\n", lib);
912
e906c010 913 if (tmp != NULL) {
914 tmp++;
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]);
918 }
919
bbd837c6 920 ret = dlopen(lib, RTLD_NOW);
921 if (ret == NULL)
922 fprintf(stderr, "dlopen: %s\n", dlerror());
923 return ret;
80c2304e 924}
925
926void *SysLoadSym(void *lib, const char *sym) {
e906c010 927 unsigned int plugid = (unsigned int)(long)lib;
928
929 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
930 return plugin_link(plugid - PLUGIN_DL_BASE, sym);
931
80c2304e 932 return dlsym(lib, sym);
933}
934
935const char *SysLibError() {
936 return dlerror();
937}
938
939void SysCloseLibrary(void *lib) {
e906c010 940 unsigned int plugid = (unsigned int)(long)lib;
941
942 if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins))
943 return;
944
80c2304e 945 dlclose(lib);
946}
947