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