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" |
87e5b45f |
34 | |
35 | static void toggle_fast_forward(int force_off); |
cc56203b |
36 | #endif |
a1b44e36 |
37 | #ifndef BOOT_MSG |
38 | #define BOOT_MSG "Booting up..." |
39 | #endif |
cc56203b |
40 | |
fc8145b7 |
41 | // don't include debug.h - it breaks ARM build (R1 redefined) |
42 | void StartDebugger(); |
43 | void StopDebugger(); |
44 | |
33400707 |
45 | // sound plugin |
46 | extern int iUseReverb; |
47 | extern int iUseInterpolation; |
48 | extern int iXAPitch; |
33400707 |
49 | extern int iVolume; |
50 | |
69af03a2 |
51 | int ready_to_go; |
c5061935 |
52 | unsigned long gpuDisp; |
53 | char cfgfile_basename[MAXPATHLEN]; |
8f892648 |
54 | int state_slot; |
55 | enum sched_action emu_action, emu_action_old; |
56 | char hud_msg[64]; |
57 | int hud_new_msg; |
58 | |
80c2304e |
59 | static 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 | |
69 | static 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 | |
76 | static 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 | |
91 | static 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 |
108 | void 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 |
127 | static 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 |
144 | void 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 |
179 | static 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 |
198 | void 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 |
227 | do_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; |
a8376201 |
264 | case SACTION_TOGGLE_FULLSCREEN: |
265 | g_fullscreen = !g_fullscreen; |
c9099d02 |
266 | if (GPU_open != NULL && GPU_close != NULL) { |
267 | GPU_close(); |
268 | GPU_open(&gpuDisp, "PCSX", NULL); |
269 | } |
a8376201 |
270 | break; |
29a8c4f3 |
271 | case SACTION_SCREENSHOT: |
272 | { |
38c2028e |
273 | char buf[MAXPATHLEN]; |
29a8c4f3 |
274 | void *scrbuf; |
275 | int w, h, bpp; |
276 | time_t t = time(NULL); |
277 | struct tm *tb = localtime(&t); |
278 | int ti = tb->tm_yday * 1000000 + tb->tm_hour * 10000 + |
279 | tb->tm_min * 100 + tb->tm_sec; |
280 | |
281 | scrbuf = pl_prepare_screenshot(&w, &h, &bpp); |
282 | get_gameid_filename(buf, sizeof(buf), |
283 | "screenshots/%.32s-%.9s.%d.png", ti); |
284 | ret = -1; |
285 | if (scrbuf != 0 && bpp == 16) |
286 | ret = writepng(buf, scrbuf, w, h); |
287 | if (ret == 0) |
288 | snprintf(hud_msg, sizeof(hud_msg), "SCREENSHOT TAKEN"); |
289 | break; |
290 | } |
221be40d |
291 | case SACTION_VOLUME_UP: |
292 | case SACTION_VOLUME_DOWN: |
cc56203b |
293 | plat_target_step_volume(emu_action == SACTION_VOLUME_UP); |
221be40d |
294 | return; |
a805c855 |
295 | case SACTION_MINIMIZE: |
36dfb787 |
296 | if (GPU_close != NULL) |
297 | GPU_close(); |
298 | |
a805c855 |
299 | plat_minimize(); |
36dfb787 |
300 | |
301 | if (GPU_open != NULL) { |
302 | ret = GPU_open(&gpuDisp, "PCSX", NULL); |
303 | if (ret) |
304 | fprintf(stderr, "GPU_open returned %d\n", ret); |
305 | } |
a805c855 |
306 | return; |
38c2028e |
307 | #endif |
4c08b9e7 |
308 | default: |
309 | return; |
8f892648 |
310 | } |
8f892648 |
311 | |
8f892648 |
312 | hud_new_msg = 3; |
313 | } |
314 | |
9c27c205 |
315 | static int cdidcmp(const char *id1, const char *id2) |
316 | { |
317 | while (*id1 != 0 && *id2 != 0) { |
318 | if (*id1 == '_') { id1++; continue; } |
319 | if (*id2 == '_') { id2++; continue; } |
320 | if (*id1 != *id2) |
321 | break; |
322 | id1++; |
323 | id2++; |
324 | } |
325 | |
326 | return *id1 - *id2; |
327 | } |
328 | |
329 | static void parse_cwcheat(void) |
330 | { |
331 | char line[256], buf[64], name[64], *p; |
332 | int newcheat = 1; |
333 | u32 a, v; |
334 | FILE *f; |
335 | |
336 | f = fopen("cheatpops.db", "r"); |
337 | if (f == NULL) |
338 | return; |
339 | |
340 | /* find the game */ |
341 | while (fgets(line, sizeof(line), f)) { |
342 | if (sscanf(line, "_S %63s", buf) != 1) |
343 | continue; |
344 | if (cdidcmp(buf, CdromId) == 0) |
345 | break; |
346 | } |
347 | |
348 | if (feof(f)) |
349 | goto out; |
350 | |
351 | printf("cwcheat section found for %s\n", CdromId); |
352 | while (fgets(line, sizeof(line), f)) |
353 | { |
354 | p = line + strlen(line); |
355 | for (p--; p >= line && (*p == '\r' || *p == '\n' || *p == ' '); p--) |
356 | *p = 0; |
357 | if (*p == 0 || *p == '#' || *p == ';') |
358 | continue; |
359 | |
360 | if (strncmp(line, "_S", 2) == 0) |
361 | break; |
362 | if (strncmp(line, "_G", 2) == 0) { |
363 | printf(" cwcheat game name: '%s'\n", line + 3); |
364 | continue; |
365 | } |
366 | if (strncmp(line, "_C0", 3) == 0) { |
367 | if (!newcheat && Cheats[NumCheats - 1].n == 0) { |
368 | printf("cheat '%s' failed to parse\n", name); |
369 | free(Cheats[NumCheats - 1].Descr); |
370 | NumCheats--; |
371 | } |
372 | snprintf(name, sizeof(name), "%s", line + 4); |
373 | newcheat = 1; |
374 | continue; |
375 | } |
376 | if (sscanf(line, "_L %x %x", &a, &v) != 2) { |
377 | printf("line failed to parse: '%s'\n", line); |
378 | continue; |
379 | } |
380 | |
381 | if (newcheat) { |
382 | if (NumCheats >= NumCheatsAllocated) { |
383 | NumCheatsAllocated += 16; |
384 | Cheats = realloc(Cheats, sizeof(Cheat) * |
385 | NumCheatsAllocated); |
386 | if (Cheats == NULL) |
387 | break; |
388 | } |
389 | Cheats[NumCheats].Descr = strdup(name); |
390 | Cheats[NumCheats].Enabled = 0; |
2c843d96 |
391 | Cheats[NumCheats].WasEnabled = 0; |
9c27c205 |
392 | Cheats[NumCheats].First = NumCodes; |
393 | Cheats[NumCheats].n = 0; |
394 | NumCheats++; |
395 | newcheat = 0; |
396 | } |
397 | |
398 | if (NumCodes >= NumCodesAllocated) { |
399 | NumCodesAllocated += 16; |
400 | CheatCodes = realloc(CheatCodes, sizeof(CheatCode) * |
401 | NumCodesAllocated); |
402 | if (CheatCodes == NULL) |
403 | break; |
404 | } |
405 | CheatCodes[NumCodes].Addr = a; |
406 | CheatCodes[NumCodes].Val = v; |
407 | NumCodes++; |
408 | Cheats[NumCheats - 1].n++; |
409 | } |
410 | |
411 | out: |
412 | fclose(f); |
413 | } |
414 | |
79f216e3 |
415 | void emu_on_new_cd(int show_hud_msg) |
9c27c205 |
416 | { |
417 | ClearAllCheats(); |
418 | parse_cwcheat(); |
419 | |
420 | if (Config.HLE) { |
421 | printf("note: running with HLE BIOS, expect compatibility problems\n"); |
422 | printf("----------------------------------------------------------\n"); |
423 | } |
424 | |
79f216e3 |
425 | if (show_hud_msg) { |
426 | snprintf(hud_msg, sizeof(hud_msg), BOOT_MSG); |
427 | hud_new_msg = 3; |
428 | } |
9c27c205 |
429 | } |
430 | |
38c2028e |
431 | int emu_core_preinit(void) |
80c2304e |
432 | { |
80c2304e |
433 | // what is the name of the config file? |
434 | // it may be redefined by -cfg on the command line |
435 | strcpy(cfgfile_basename, "pcsx.cfg"); |
436 | |
e906c010 |
437 | emuLog = stdout; |
80c2304e |
438 | SetIsoFile(NULL); |
80c2304e |
439 | |
e0c692d9 |
440 | memset(&Config, 0, sizeof(Config)); |
441 | |
e0c692d9 |
442 | set_default_paths(); |
33400707 |
443 | emu_set_default_config(); |
e0c692d9 |
444 | strcpy(Config.Bios, "HLE"); |
445 | |
38c2028e |
446 | return 0; |
447 | } |
448 | |
449 | int emu_core_init(void) |
450 | { |
451 | CheckSubDir(); |
452 | check_memcards(); |
453 | |
454 | if (EmuInit() == -1) { |
455 | printf("PSX emulator couldn't be initialized.\n"); |
456 | return -1; |
457 | } |
458 | |
459 | LoadMcds(Config.Mcd1, Config.Mcd2); |
460 | |
461 | if (Config.Debug) { |
462 | StartDebugger(); |
463 | } |
464 | |
465 | return 0; |
466 | } |
467 | |
468 | #ifndef NO_FRONTEND |
469 | int main(int argc, char *argv[]) |
470 | { |
e0c692d9 |
471 | char file[MAXPATHLEN] = ""; |
472 | char path[MAXPATHLEN]; |
473 | const char *cdfile = NULL; |
de910c6b |
474 | const char *loadst_f = NULL; |
0a151868 |
475 | int psxout = 0; |
e0c692d9 |
476 | int loadst = 0; |
477 | int i; |
478 | |
38c2028e |
479 | emu_core_preinit(); |
480 | |
80c2304e |
481 | // read command line options |
482 | for (i = 1; i < argc; i++) { |
0a151868 |
483 | if (!strcmp(argv[i], "-psxout")) psxout = 1; |
80c2304e |
484 | else if (!strcmp(argv[i], "-load")) loadst = atol(argv[++i]); |
485 | else if (!strcmp(argv[i], "-cfg")) { |
486 | if (i+1 >= argc) break; |
487 | strncpy(cfgfile_basename, argv[++i], MAXPATHLEN-100); /* TODO buffer overruns */ |
488 | printf("Using config file %s.\n", cfgfile_basename); |
489 | } |
490 | else if (!strcmp(argv[i], "-cdfile")) { |
491 | char isofilename[MAXPATHLEN]; |
492 | |
493 | if (i+1 >= argc) break; |
494 | strncpy(isofilename, argv[++i], MAXPATHLEN); |
495 | if (isofilename[0] != '/') { |
496 | getcwd(path, MAXPATHLEN); |
497 | if (strlen(path) + strlen(isofilename) + 1 < MAXPATHLEN) { |
498 | strcat(path, "/"); |
499 | strcat(path, isofilename); |
500 | strcpy(isofilename, path); |
501 | } else |
502 | isofilename[0] = 0; |
503 | } |
504 | |
47bf65ab |
505 | cdfile = isofilename; |
80c2304e |
506 | } |
de910c6b |
507 | else if (!strcmp(argv[i], "-loadf")) { |
508 | if (i+1 >= argc) break; |
509 | loadst_f = argv[++i]; |
510 | } |
80c2304e |
511 | else if (!strcmp(argv[i], "-h") || |
512 | !strcmp(argv[i], "-help") || |
513 | !strcmp(argv[i], "--help")) { |
38c2028e |
514 | printf("PCSX-ReARMed " REV "\n"); |
80c2304e |
515 | printf("%s\n", _( |
516 | " pcsx [options] [file]\n" |
517 | "\toptions:\n" |
518 | "\t-cdfile FILE\tRuns a CD image file\n" |
80c2304e |
519 | "\t-cfg FILE\tLoads desired configuration file (default: ~/.pcsx/pcsx.cfg)\n" |
520 | "\t-psxout\t\tEnable PSX output\n" |
521 | "\t-load STATENUM\tLoads savestate STATENUM (1-5)\n" |
522 | "\t-h -help\tDisplay this message\n" |
38c2028e |
523 | "\tfile\t\tLoads a PSX EXE file\n")); |
80c2304e |
524 | return 0; |
525 | } else { |
526 | strncpy(file, argv[i], MAXPATHLEN); |
527 | if (file[0] != '/') { |
528 | getcwd(path, MAXPATHLEN); |
529 | if (strlen(path) + strlen(file) + 1 < MAXPATHLEN) { |
530 | strcat(path, "/"); |
531 | strcat(path, file); |
532 | strcpy(file, path); |
533 | } else |
534 | file[0] = 0; |
535 | } |
536 | } |
537 | } |
538 | |
47bf65ab |
539 | if (cdfile) |
540 | set_cd_image(cdfile); |
541 | |
69af03a2 |
542 | // frontend stuff |
b07c18e8 |
543 | // init input but leave probing to platform code, |
544 | // they add input drivers and may need to modify them after probe |
69af03a2 |
545 | in_init(); |
59f1c85c |
546 | pl_init(); |
69af03a2 |
547 | plat_init(); |
0a151868 |
548 | menu_init(); // loads config |
549 | |
38c2028e |
550 | emu_core_init(); |
551 | |
0a151868 |
552 | if (psxout) |
553 | Config.PsxOut = 1; |
80c2304e |
554 | |
69af03a2 |
555 | if (LoadPlugins() == -1) { |
6fe1f056 |
556 | // FIXME: this recovery doesn't work, just delete bad config and bail out |
557 | // SysMessage("could not load plugins, retrying with defaults\n"); |
558 | set_default_paths(); |
559 | snprintf(path, sizeof(path), "." PCSX_DOT_DIR "%s", cfgfile_basename); |
560 | remove(path); |
69af03a2 |
561 | SysMessage("Failed loading plugins!"); |
562 | return 1; |
563 | } |
564 | pcnt_hook_plugins(); |
80c2304e |
565 | |
69af03a2 |
566 | if (OpenPlugins() == -1) { |
567 | return 1; |
568 | } |
201c21e2 |
569 | plugin_call_rearmed_cbs(); |
80c2304e |
570 | |
69af03a2 |
571 | CheckCdrom(); |
bbd837c6 |
572 | SysReset(); |
69af03a2 |
573 | |
574 | if (file[0] != '\0') { |
575 | if (Load(file) != -1) |
576 | ready_to_go = 1; |
577 | } else { |
47bf65ab |
578 | if (cdfile) { |
69af03a2 |
579 | if (LoadCdrom() == -1) { |
580 | ClosePlugins(); |
581 | printf(_("Could not load CD-ROM!\n")); |
582 | return -1; |
80c2304e |
583 | } |
79f216e3 |
584 | emu_on_new_cd(!loadst); |
69af03a2 |
585 | ready_to_go = 1; |
80c2304e |
586 | } |
69af03a2 |
587 | } |
80c2304e |
588 | |
ea66e884 |
589 | if (loadst_f) { |
590 | int ret = LoadState(loadst_f); |
591 | printf("%s state file: %s\n", ret ? "failed to load" : "loaded", loadst_f); |
592 | ready_to_go |= ret == 0; |
593 | } |
594 | |
45d45c1e |
595 | if (ready_to_go) { |
3c70c47b |
596 | menu_prepare_emu(); |
45d45c1e |
597 | |
598 | // If a state has been specified, then load that |
599 | if (loadst) { |
600 | int ret = emu_load_state(loadst - 1); |
601 | printf("%s state %d\n", ret ? "failed to load" : "loaded", loadst); |
602 | } |
603 | } |
3c70c47b |
604 | else |
69af03a2 |
605 | menu_loop(); |
606 | |
7c0f51de |
607 | pl_start_watchdog(); |
608 | |
69af03a2 |
609 | while (1) |
610 | { |
799b0b87 |
611 | stop = 0; |
8f892648 |
612 | emu_action = SACTION_NONE; |
613 | |
80c2304e |
614 | psxCpu->Execute(); |
8f892648 |
615 | if (emu_action != SACTION_NONE) |
616 | do_emu_action(); |
80c2304e |
617 | } |
618 | |
80c2304e |
619 | return 0; |
620 | } |
456f1b86 |
621 | |
622 | static void toggle_fast_forward(int force_off) |
623 | { |
624 | static int fast_forward; |
625 | static int normal_g_opts; |
626 | static int normal_frameskip; |
627 | static int normal_enhancement_enable; |
628 | |
629 | if (force_off && !fast_forward) |
630 | return; |
631 | |
632 | fast_forward = !fast_forward; |
633 | if (fast_forward) { |
634 | normal_g_opts = g_opts; |
635 | normal_frameskip = pl_rearmed_cbs.frameskip; |
636 | normal_enhancement_enable = |
637 | pl_rearmed_cbs.gpu_neon.enhancement_enable; |
638 | |
639 | g_opts |= OPT_NO_FRAMELIM; |
640 | pl_rearmed_cbs.frameskip = 3; |
641 | pl_rearmed_cbs.gpu_neon.enhancement_enable = 0; |
642 | } else { |
643 | g_opts = normal_g_opts; |
644 | pl_rearmed_cbs.frameskip = normal_frameskip; |
645 | pl_rearmed_cbs.gpu_neon.enhancement_enable = |
646 | normal_enhancement_enable; |
647 | |
648 | pl_timing_prepare(Config.PsxType); |
649 | } |
650 | |
651 | if (!force_off) |
652 | snprintf(hud_msg, sizeof(hud_msg), "FAST FORWARD %s", |
653 | fast_forward ? "ON" : "OFF"); |
654 | } |
38c2028e |
655 | #endif |
80c2304e |
656 | |
657 | void SysRunGui() { |
658 | printf("SysRunGui\n"); |
659 | } |
660 | |
e6eb2066 |
661 | static void dummy_lace() |
662 | { |
663 | } |
664 | |
80c2304e |
665 | void SysReset() { |
e6eb2066 |
666 | // rearmed hack: EmuReset() runs some code when real BIOS is used, |
667 | // but we usually do reset from menu while GPU is not open yet, |
668 | // so we need to prevent updateLace() call.. |
669 | void *real_lace = GPU_updateLace; |
670 | GPU_updateLace = dummy_lace; |
671 | |
33400707 |
672 | // reset can run code, timing must be set |
673 | pl_timing_prepare(Config.PsxType); |
674 | |
80c2304e |
675 | EmuReset(); |
bd6267e6 |
676 | |
677 | // hmh core forgets this |
678 | CDR_stop(); |
e6eb2066 |
679 | |
680 | GPU_updateLace = real_lace; |
80c2304e |
681 | } |
682 | |
683 | void SysClose() { |
684 | EmuShutdown(); |
685 | ReleasePlugins(); |
686 | |
687 | StopDebugger(); |
688 | |
689 | if (emuLog != NULL) fclose(emuLog); |
690 | } |
691 | |
692 | void SysUpdate() { |
80c2304e |
693 | } |
694 | |
695 | void OnFile_Exit() { |
bd6267e6 |
696 | printf("OnFile_Exit\n"); |
bd6267e6 |
697 | SysClose(); |
38c2028e |
698 | #ifndef NO_FRONTEND |
699 | menu_finish(); |
2c886904 |
700 | plat_finish(); |
80c2304e |
701 | exit(0); |
38c2028e |
702 | #endif |
80c2304e |
703 | } |
704 | |
c5061935 |
705 | int get_state_filename(char *buf, int size, int i) { |
29a8c4f3 |
706 | return get_gameid_filename(buf, size, |
707 | "." STATES_DIR "%.32s-%.9s.%3.3d", i); |
80c2304e |
708 | } |
709 | |
8f892648 |
710 | int emu_check_state(int slot) |
711 | { |
712 | char fname[MAXPATHLEN]; |
713 | int ret; |
714 | |
715 | ret = get_state_filename(fname, sizeof(fname), slot); |
716 | if (ret != 0) |
717 | return ret; |
718 | |
719 | return CheckState(fname); |
720 | } |
721 | |
722 | int emu_save_state(int slot) |
723 | { |
724 | char fname[MAXPATHLEN]; |
725 | int ret; |
726 | |
727 | ret = get_state_filename(fname, sizeof(fname), slot); |
728 | if (ret != 0) |
729 | return ret; |
730 | |
90f1d26c |
731 | ret = SaveState(fname); |
0b1dbe6b |
732 | #ifndef __ARM_ARCH_7A__ /* XXX */ |
733 | sync(); |
734 | #endif |
90f1d26c |
735 | printf("* %s \"%s\" [%d]\n", ret == 0 ? "saved" : "failed to save", fname, slot); |
736 | return ret; |
8f892648 |
737 | } |
738 | |
739 | int emu_load_state(int slot) |
740 | { |
741 | char fname[MAXPATHLEN]; |
742 | int ret; |
743 | |
dde7da71 |
744 | hud_msg[0] = 0; |
745 | |
8f892648 |
746 | ret = get_state_filename(fname, sizeof(fname), slot); |
747 | if (ret != 0) |
748 | return ret; |
749 | |
750 | return LoadState(fname); |
751 | } |
752 | |
80c2304e |
753 | void SysPrintf(const char *fmt, ...) { |
754 | va_list list; |
755 | char msg[512]; |
756 | |
757 | va_start(list, fmt); |
758 | vsprintf(msg, fmt, list); |
759 | va_end(list); |
760 | |
80c2304e |
761 | fprintf(emuLog, "%s", msg); |
762 | } |
763 | |
764 | void SysMessage(const char *fmt, ...) { |
765 | va_list list; |
766 | char msg[512]; |
767 | |
768 | va_start(list, fmt); |
769 | vsprintf(msg, fmt, list); |
770 | va_end(list); |
771 | |
772 | if (msg[strlen(msg) - 1] == '\n') |
773 | msg[strlen(msg) - 1] = 0; |
774 | |
775 | fprintf(stderr, "%s\n", msg); |
776 | } |
777 | |
c5061935 |
778 | static void SignalExit(int sig) { |
779 | ClosePlugins(); |
780 | OnFile_Exit(); |
781 | } |
782 | |
783 | #define PARSEPATH(dst, src) \ |
784 | ptr = src + strlen(src); \ |
785 | while (*ptr != '\\' && ptr != src) ptr--; \ |
786 | if (ptr != src) { \ |
787 | strcpy(dst, ptr+1); \ |
788 | } |
789 | |
790 | static int _OpenPlugins(void) { |
791 | int ret; |
792 | |
793 | signal(SIGINT, SignalExit); |
794 | signal(SIGPIPE, SignalExit); |
795 | |
796 | GPU_clearDynarec(clearDynarec); |
797 | |
798 | ret = CDR_open(); |
799 | if (ret < 0) { SysMessage(_("Error opening CD-ROM plugin!")); return -1; } |
800 | ret = SPU_open(); |
801 | if (ret < 0) { SysMessage(_("Error opening SPU plugin!")); return -1; } |
802 | SPU_registerCallback(SPUirq); |
803 | // pcsx-rearmed: we handle gpu elsewhere |
804 | //ret = GPU_open(&gpuDisp, "PCSX", NULL); |
805 | //if (ret < 0) { SysMessage(_("Error opening GPU plugin!")); return -1; } |
806 | ret = PAD1_open(&gpuDisp); |
807 | if (ret < 0) { SysMessage(_("Error opening Controller 1 plugin!")); return -1; } |
808 | ret = PAD2_open(&gpuDisp); |
809 | if (ret < 0) { SysMessage(_("Error opening Controller 2 plugin!")); return -1; } |
810 | |
811 | if (Config.UseNet && !NetOpened) { |
812 | netInfo info; |
813 | char path[MAXPATHLEN]; |
814 | char dotdir[MAXPATHLEN]; |
815 | |
816 | MAKE_PATH(dotdir, "/.pcsx/plugins/", NULL); |
817 | |
38c2028e |
818 | strcpy(info.EmuName, "PCSX"); |
c5061935 |
819 | strncpy(info.CdromID, CdromId, 9); |
820 | strncpy(info.CdromLabel, CdromLabel, 9); |
821 | info.psxMem = psxM; |
822 | info.GPU_showScreenPic = GPU_showScreenPic; |
823 | info.GPU_displayText = GPU_displayText; |
824 | info.GPU_showScreenPic = GPU_showScreenPic; |
825 | info.PAD_setSensitive = PAD1_setSensitive; |
826 | sprintf(path, "%s%s", Config.BiosDir, Config.Bios); |
827 | strcpy(info.BIOSpath, path); |
828 | strcpy(info.MCD1path, Config.Mcd1); |
829 | strcpy(info.MCD2path, Config.Mcd2); |
830 | sprintf(path, "%s%s", dotdir, Config.Gpu); |
831 | strcpy(info.GPUpath, path); |
832 | sprintf(path, "%s%s", dotdir, Config.Spu); |
833 | strcpy(info.SPUpath, path); |
834 | sprintf(path, "%s%s", dotdir, Config.Cdr); |
835 | strcpy(info.CDRpath, path); |
836 | NET_setInfo(&info); |
837 | |
838 | ret = NET_open(&gpuDisp); |
839 | if (ret < 0) { |
840 | if (ret == -2) { |
841 | // -2 is returned when something in the info |
842 | // changed and needs to be synced |
843 | char *ptr; |
844 | |
845 | PARSEPATH(Config.Bios, info.BIOSpath); |
846 | PARSEPATH(Config.Gpu, info.GPUpath); |
847 | PARSEPATH(Config.Spu, info.SPUpath); |
848 | PARSEPATH(Config.Cdr, info.CDRpath); |
849 | |
850 | strcpy(Config.Mcd1, info.MCD1path); |
851 | strcpy(Config.Mcd2, info.MCD2path); |
852 | return -2; |
853 | } else { |
854 | Config.UseNet = FALSE; |
855 | } |
856 | } else { |
857 | if (NET_queryPlayer() == 1) { |
858 | if (SendPcsxInfo() == -1) Config.UseNet = FALSE; |
859 | } else { |
860 | if (RecvPcsxInfo() == -1) Config.UseNet = FALSE; |
861 | } |
862 | } |
863 | NetOpened = TRUE; |
864 | } else if (Config.UseNet) { |
865 | NET_resume(); |
866 | } |
867 | |
868 | return 0; |
869 | } |
870 | |
871 | int OpenPlugins() { |
872 | int ret; |
873 | |
874 | while ((ret = _OpenPlugins()) == -2) { |
875 | ReleasePlugins(); |
876 | LoadMcds(Config.Mcd1, Config.Mcd2); |
877 | if (LoadPlugins() == -1) return -1; |
878 | } |
879 | return ret; |
880 | } |
881 | |
882 | void ClosePlugins() { |
883 | int ret; |
884 | |
885 | signal(SIGINT, SIG_DFL); |
886 | signal(SIGPIPE, SIG_DFL); |
887 | ret = CDR_close(); |
888 | if (ret < 0) { SysMessage(_("Error closing CD-ROM plugin!")); return; } |
889 | ret = SPU_close(); |
890 | if (ret < 0) { SysMessage(_("Error closing SPU plugin!")); return; } |
891 | ret = PAD1_close(); |
892 | if (ret < 0) { SysMessage(_("Error closing Controller 1 Plugin!")); return; } |
893 | ret = PAD2_close(); |
894 | if (ret < 0) { SysMessage(_("Error closing Controller 2 plugin!")); return; } |
895 | // pcsx-rearmed: we handle gpu elsewhere |
896 | //ret = GPU_close(); |
897 | //if (ret < 0) { SysMessage(_("Error closing GPU plugin!")); return; } |
898 | |
899 | if (Config.UseNet) { |
900 | NET_pause(); |
901 | } |
902 | } |
903 | |
e906c010 |
904 | /* we hook statically linked plugins here */ |
905 | static const char *builtin_plugins[] = { |
47bf65ab |
906 | "builtin_gpu", "builtin_spu", "builtin_cdr", "builtin_pad", |
907 | "builtin_cdrcimg", |
e906c010 |
908 | }; |
909 | |
910 | static const int builtin_plugin_ids[] = { |
911 | PLUGIN_GPU, PLUGIN_SPU, PLUGIN_CDR, PLUGIN_PAD, |
47bf65ab |
912 | PLUGIN_CDRCIMG, |
e906c010 |
913 | }; |
914 | |
80c2304e |
915 | void *SysLoadLibrary(const char *lib) { |
e906c010 |
916 | const char *tmp = strrchr(lib, '/'); |
bbd837c6 |
917 | void *ret; |
e906c010 |
918 | int i; |
919 | |
bbd837c6 |
920 | printf("plugin: %s\n", lib); |
921 | |
e906c010 |
922 | if (tmp != NULL) { |
923 | tmp++; |
924 | for (i = 0; i < ARRAY_SIZE(builtin_plugins); i++) |
925 | if (strcmp(tmp, builtin_plugins[i]) == 0) |
926 | return (void *)(long)(PLUGIN_DL_BASE + builtin_plugin_ids[i]); |
927 | } |
928 | |
bbd837c6 |
929 | ret = dlopen(lib, RTLD_NOW); |
930 | if (ret == NULL) |
931 | fprintf(stderr, "dlopen: %s\n", dlerror()); |
932 | return ret; |
80c2304e |
933 | } |
934 | |
935 | void *SysLoadSym(void *lib, const char *sym) { |
e906c010 |
936 | unsigned int plugid = (unsigned int)(long)lib; |
937 | |
938 | if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins)) |
939 | return plugin_link(plugid - PLUGIN_DL_BASE, sym); |
940 | |
80c2304e |
941 | return dlsym(lib, sym); |
942 | } |
943 | |
944 | const char *SysLibError() { |
945 | return dlerror(); |
946 | } |
947 | |
948 | void SysCloseLibrary(void *lib) { |
e906c010 |
949 | unsigned int plugid = (unsigned int)(long)lib; |
950 | |
951 | if (PLUGIN_DL_BASE <= plugid && plugid < PLUGIN_DL_BASE + ARRAY_SIZE(builtin_plugins)) |
952 | return; |
953 | |
80c2304e |
954 | dlclose(lib); |
955 | } |
956 | |