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