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