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