35868d35 |
1 | /* FCE Ultra - NES/Famicom Emulator |
35868d35 |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License |
14 | * along with this program; if not, write to the Free Software |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 | */ |
17 | |
35868d35 |
18 | |
19 | #include <unistd.h> |
20 | #include <sys/types.h> |
21 | #include <signal.h> |
22 | #include <sys/time.h> |
23 | #include <sys/stat.h> |
24 | #include <string.h> |
25 | #include <strings.h> |
26 | #include <errno.h> |
27 | #include <stdio.h> |
28 | #include <stdlib.h> |
29 | |
4a2a617a |
30 | #include "main.h" |
35868d35 |
31 | #include "throttle.h" |
4a2a617a |
32 | #include "config.h" |
33 | #include "args.h" |
34 | #include "unixdsp.h" |
35 | #include "cheat.h" |
36 | #include "dface.h" |
37 | #include "platform.h" |
b2b95d2e |
38 | #include "menu.h" |
4a2a617a |
39 | #include "settings.h" |
35868d35 |
40 | |
21afaa36 |
41 | #include "../../fce.h" |
9b451455 |
42 | #include "../../cart.h" |
21afaa36 |
43 | |
35868d35 |
44 | |
4a2a617a |
45 | // these are now here to try to make compatible configs |
46 | // between different versions of the emu |
47 | DSETTINGS Settings; |
48 | CFGSTRUCT DriverConfig[]={ |
49 | AC(Settings.turbo_rate_add), |
50 | AC(Settings.sound_rate), |
51 | AC(Settings.showfps), |
52 | AC(Settings.scaling), |
53 | AC(Settings.frameskip), |
54 | AC(Settings.sstate_confirm), |
55 | AC(Settings.region_force), |
56 | AC(Settings.cpuclock), |
57 | AC(Settings.mmuhack), |
58 | AC(Settings.ramtimings), |
59 | AC(Settings.gamma), |
60 | AC(Settings.perfect_vsync), |
61 | AC(Settings.accurate_mode), |
62 | ENDCFGSTRUCT |
63 | }; |
21afaa36 |
64 | |
21afaa36 |
65 | void CleanSurface(void); |
6587f346 |
66 | |
b2b95d2e |
67 | // internals |
b2b95d2e |
68 | extern uint8 Exit; // exit emu loop flag |
7b356ee3 |
69 | extern int FSkip; |
b2b95d2e |
70 | void CloseGame(void); |
71 | |
72 | FCEUGI *fceugi = NULL; |
21afaa36 |
73 | int ntsccol=0,ntschue=-1,ntsctint=-1; |
35868d35 |
74 | int soundvol=70; |
75 | int inited=0; |
35868d35 |
76 | |
21afaa36 |
77 | int srendlinev[2]={8,0}; |
78 | int erendlinev[2]={231,239}; |
35868d35 |
79 | int srendline,erendline; |
80 | |
81 | |
82 | static char BaseDirectory[2048]; |
83 | |
84 | int eoptions=0; |
85 | |
86 | static void DriverKill(void); |
87 | static int DriverInitialize(void); |
88 | |
89 | static int gametype; |
90 | #include "input.c" |
91 | |
92 | static void ParseGI(FCEUGI *gi) |
93 | { |
94 | gametype=gi->type; |
95 | |
96 | InputType[0]=UsrInputType[0]; |
97 | InputType[1]=UsrInputType[1]; |
98 | InputTypeFC=UsrInputTypeFC; |
99 | |
100 | if(gi->input[0]>=0) |
101 | InputType[0]=gi->input[0]; |
102 | if(gi->input[1]>=0) |
103 | InputType[1]=gi->input[1]; |
104 | if(gi->inputfc>=0) |
105 | InputTypeFC=gi->inputfc; |
35868d35 |
106 | } |
107 | |
108 | void FCEUD_PrintError(char *s) |
109 | { |
110 | puts(s); |
111 | } |
112 | |
6f6bc6fa |
113 | char *cpalette=0; |
114 | void LoadCPalette(void) |
35868d35 |
115 | { |
116 | char tmpp[192]; |
117 | FILE *fp; |
118 | |
119 | if(!(fp=fopen(cpalette,"rb"))) |
120 | { |
121 | printf(" Error loading custom palette from file: %s\n",cpalette); |
6f6bc6fa |
122 | free(cpalette); |
123 | cpalette=0; |
35868d35 |
124 | return; |
125 | } |
126 | fread(tmpp,1,192,fp); |
127 | FCEUI_SetPaletteArray((uint8 *)tmpp); |
128 | fclose(fp); |
129 | } |
130 | |
131 | static CFGSTRUCT fceuconfig[]={ |
132 | AC(soundvol), |
133 | ACS(cpalette), |
134 | AC(ntsctint), |
135 | AC(ntschue), |
136 | AC(ntsccol), |
137 | AC(UsrInputTypeFC), |
138 | ACA(UsrInputType), |
139 | AC(powerpadside), |
140 | AC(powerpadsc), |
141 | AC(eoptions), |
142 | ACA(srendlinev), |
143 | ACA(erendlinev), |
144 | ADDCFGSTRUCT(DriverConfig), |
145 | ENDCFGSTRUCT |
146 | }; |
147 | |
642070a9 |
148 | static const char *skip_path(const char *path) |
35868d35 |
149 | { |
642070a9 |
150 | const char *p; |
151 | if (path == NULL) return NULL; |
152 | for (p = path+strlen(path)-1; p > path && *p != '/'; p--); |
153 | if (*p == '/') p++; |
154 | return p; |
155 | } |
156 | |
157 | int SaveConfig(const char *llgn_path) |
158 | { |
159 | const char *name = skip_path(llgn_path); |
35868d35 |
160 | char tdir[2048]; |
642070a9 |
161 | int ret; |
b547bda7 |
162 | if (name) |
163 | sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name); |
164 | else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory); |
642070a9 |
165 | printf("saving cfg to %s ... ", tdir); fflush(stdout); |
21afaa36 |
166 | FCEUI_GetNTSCTH(&ntsctint, &ntschue); |
642070a9 |
167 | ret=SaveFCEUConfig(tdir,fceuconfig); |
168 | printf(ret == 0 ? "done\n" : "failed\n"); |
169 | return ret; |
35868d35 |
170 | } |
171 | |
642070a9 |
172 | static int LoadConfig(const char *llgn_path) |
35868d35 |
173 | { |
642070a9 |
174 | const char *name = skip_path(llgn_path); |
35868d35 |
175 | char tdir[2048]; |
642070a9 |
176 | int ret; |
b547bda7 |
177 | if (name) |
178 | sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name); |
179 | else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory); |
642070a9 |
180 | printf("loading cfg from %s ... ", tdir); fflush(stdout); |
21afaa36 |
181 | FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */ |
642070a9 |
182 | ret=LoadFCEUConfig(tdir,fceuconfig); |
21afaa36 |
183 | FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue); |
642070a9 |
184 | printf(ret == 0 ? "done\n" : "failed\n"); |
185 | return ret; |
35868d35 |
186 | } |
187 | |
989672f4 |
188 | static void LoadLLGN(void) |
189 | { |
190 | char tdir[2048]; |
191 | FILE *f; |
192 | int len; |
193 | sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory); |
194 | f=fopen(tdir, "r"); |
195 | if(f) |
196 | { |
197 | len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f); |
198 | lastLoadedGameName[len] = 0; |
199 | fclose(f); |
200 | } |
201 | } |
202 | |
203 | static void SaveLLGN(void) |
204 | { |
205 | // save last loaded game name |
7b356ee3 |
206 | if (lastLoadedGameName[0] && !(eoptions&EO_NOAUTOWRITE)) |
989672f4 |
207 | { |
208 | char tdir[2048]; |
209 | FILE *f; |
210 | sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory); |
211 | f=fopen(tdir, "w"); |
212 | if(f) |
213 | { |
214 | fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f); |
215 | fclose(f); |
216 | sync(); |
217 | } |
218 | } |
219 | } |
220 | |
221 | |
35868d35 |
222 | static void CreateDirs(void) |
223 | { |
6f6bc6fa |
224 | char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"}; |
35868d35 |
225 | char tdir[2048]; |
226 | int x; |
227 | |
228 | mkdir(BaseDirectory,S_IRWXU); |
b547bda7 |
229 | for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++) |
35868d35 |
230 | { |
231 | sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]); |
232 | mkdir(tdir,S_IRWXU); |
233 | } |
234 | } |
235 | |
236 | static void SetSignals(void (*t)(int)) |
237 | { |
238 | int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2}; |
239 | int x; |
240 | for(x=0;x<11;x++) |
241 | signal(sigs[x],t); |
242 | } |
243 | |
244 | static void CloseStuff(int signum) |
245 | { |
246 | DriverKill(); |
247 | printf("\nSignal %d has been caught and dealt with...\n",signum); |
248 | switch(signum) |
249 | { |
250 | case SIGINT:printf("How DARE you interrupt me!\n");break; |
251 | case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break; |
252 | case SIGHUP:printf("Reach out and hang-up on someone.\n");break; |
253 | case SIGPIPE:printf("The pipe has broken! Better watch out for floods...\n");break; |
254 | case SIGSEGV:printf("Iyeeeeeeeee!!! A segmentation fault has occurred. Have a fluffy day.\n");break; |
35868d35 |
255 | #if(SIGBUS!=SIGSEGV) |
256 | case SIGBUS:printf("I told you to be nice to the driver.\n");break; |
257 | #endif |
35868d35 |
258 | case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break; |
259 | case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break; |
260 | case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break; |
261 | case SIGUSR1: |
262 | case SIGUSR2:printf("Killing your processes is not nice.\n");break; |
263 | } |
264 | exit(1); |
265 | } |
266 | |
b2b95d2e |
267 | static int DoArgs(int argc, char *argv[]) |
35868d35 |
268 | { |
269 | static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"}; |
270 | static int cortabi[5]={SI_NONE,SI_GAMEPAD, |
d97315ac |
271 | SI_ZAPPER,SI_POWERPADA,SI_ARKANOID}; |
35868d35 |
272 | static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"}; |
273 | static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW, |
274 | SIFC_4PLAYER,SIFC_FKB}; |
275 | |
b2b95d2e |
276 | int x, ret; |
35868d35 |
277 | static char *inputa[2]={0,0}; |
278 | static char *fcexp=0; |
279 | static int docheckie[4]; |
4a2a617a |
280 | #ifdef NETWORK |
281 | static int docheckie2[2]={0,0}; |
282 | #endif |
35868d35 |
283 | |
284 | static ARGPSTRUCT FCEUArgs[]={ |
285 | {"-soundvol",0,&soundvol,0}, |
286 | {"-cpalette",0,&cpalette,0x4001}, |
287 | |
288 | {"-ntsccol",0,&ntsccol,0}, |
289 | {"-pal",&docheckie[0],0,0}, |
290 | {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001}, |
291 | {"-fcexp",0,&fcexp,0x4001}, |
292 | |
9b451455 |
293 | {"-gg",0,&eoptions,0x8000|EO_GG}, |
6f6bc6fa |
294 | {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM}, |
35868d35 |
295 | {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME}, |
296 | {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE}, |
297 | {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES}, |
298 | {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE}, |
7b356ee3 |
299 | {"-noautowrite",0,&eoptions,0x8000|EO_NOAUTOWRITE}, |
35868d35 |
300 | {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0}, |
301 | {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0}, |
4a2a617a |
302 | {"-sound",0,&Settings.sound_rate,0}, |
303 | {"-showfps",0,&Settings.showfps,0}, |
304 | #ifdef NETWORK |
305 | {"-connect",&docheckie2[0],&netplayhost,0x4001}, |
306 | {"-server",&docheckie2[1],0,0}, |
307 | {"-netport",0,&Port,0}, |
308 | #endif |
309 | {0,(void *)DriverArgs,0,0}, |
310 | {0,0,0,0} |
35868d35 |
311 | }; |
312 | |
313 | memset(docheckie,0,sizeof(docheckie)); |
b2b95d2e |
314 | ret=ParseArguments(argc, argv, FCEUArgs); |
35868d35 |
315 | if(cpalette) |
316 | { |
317 | if(cpalette[0]=='0') |
318 | if(cpalette[1]==0) |
319 | { |
320 | free(cpalette); |
321 | cpalette=0; |
322 | } |
323 | } |
324 | if(docheckie[0]) |
989672f4 |
325 | Settings.region_force=2; |
9b451455 |
326 | FCEUI_SetGameGenie(eoptions&EO_GG); |
6f6bc6fa |
327 | FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM); |
35868d35 |
328 | FCEUI_SetSnapName(eoptions&EO_SNAPNAME); |
329 | |
330 | for(x=0;x<2;x++) |
331 | { |
332 | if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0; |
333 | if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239; |
334 | } |
335 | |
35868d35 |
336 | FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]); |
7b356ee3 |
337 | FCEUI_SetSoundVolume(80); |
4a2a617a |
338 | #ifdef NETWORK |
339 | if(docheckie2[0]) |
340 | netplay=2; |
341 | else if(docheckie2[1]) |
342 | netplay=1; |
343 | |
344 | if(netplay) |
345 | FCEUI_SetNetworkPlay(netplay); |
346 | #endif |
35868d35 |
347 | DoDriverArgs(); |
348 | |
349 | if(fcexp) |
350 | { |
351 | int y; |
352 | for(y=0;y<5;y++) |
353 | { |
354 | if(!strncmp(fccortab[y],fcexp,8)) |
355 | { |
356 | UsrInputTypeFC=fccortabi[y]; |
357 | break; |
358 | } |
359 | } |
360 | free(fcexp); |
361 | } |
362 | for(x=0;x<2;x++) |
363 | { |
364 | int y; |
365 | |
366 | if(!inputa[x]) |
367 | continue; |
368 | |
369 | for(y=0;y<5;y++) |
370 | { |
371 | if(!strncmp(cortab[y],inputa[x],8)) |
372 | { |
373 | UsrInputType[x]=cortabi[y]; |
374 | if(y==3) |
375 | { |
376 | powerpadside&=~(1<<x); |
377 | powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x; |
378 | } |
379 | free(inputa[x]); |
380 | } |
381 | } |
382 | } |
b2b95d2e |
383 | return ret; |
35868d35 |
384 | } |
385 | |
b547bda7 |
386 | |
35868d35 |
387 | #include "usage.h" |
388 | |
4a2a617a |
389 | int main(int argc, char *argv[]) |
35868d35 |
390 | { |
642070a9 |
391 | int last_arg_parsed, ret; |
b2b95d2e |
392 | /* TODO if(argc<=1) |
937bf65b |
393 | { |
394 | ShowUsage(argv[0]); |
395 | return 1; |
b2b95d2e |
396 | }*/ |
937bf65b |
397 | |
4a2a617a |
398 | platform_init(); |
937bf65b |
399 | if(!DriverInitialize()) |
400 | { |
401 | return 1; |
402 | } |
403 | |
b2b95d2e |
404 | if(!FCEUI_Initialize()) |
35868d35 |
405 | return(1); |
406 | GetBaseDirectory(BaseDirectory); |
407 | FCEUI_SetBaseDirectory(BaseDirectory); |
b2b95d2e |
408 | lastLoadedGameName[0] = 0; |
4a2a617a |
409 | menu_init(); |
410 | in_init(); |
35868d35 |
411 | |
412 | CreateDirs(); |
b547bda7 |
413 | LoadConfig(NULL); |
b2b95d2e |
414 | last_arg_parsed=DoArgs(argc-1,&argv[1]); |
4a2a617a |
415 | platform_late_init(); |
416 | in_probe(); |
417 | |
989672f4 |
418 | LoadLLGN(); |
21afaa36 |
419 | FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue); |
35868d35 |
420 | if(cpalette) |
6f6bc6fa |
421 | LoadCPalette(); |
35868d35 |
422 | if(InitSound()) |
423 | inited|=1; |
424 | |
b2b95d2e |
425 | if (argc > 1 && !last_arg_parsed) |
6587f346 |
426 | { |
b2b95d2e |
427 | strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName)); |
428 | lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0; |
429 | Exit = 0; |
430 | } |
431 | else |
432 | { |
b2b95d2e |
433 | Exit = 1; |
6587f346 |
434 | } |
435 | |
b2b95d2e |
436 | while (1) |
437 | { |
438 | if(!Exit) |
439 | { |
440 | if (fceugi) |
441 | CloseGame(); |
642070a9 |
442 | ret=LoadConfig(lastLoadedGameName); |
443 | if (ret != 0) |
444 | { |
445 | LoadConfig(NULL); |
446 | } |
6244011f |
447 | FCEUI_SetEmuMode(Settings.accurate_mode); |
b2b95d2e |
448 | fceugi=FCEUI_LoadGame(lastLoadedGameName); |
449 | if (fceugi) |
450 | { |
c4980f9e |
451 | char infostring[32]; |
989672f4 |
452 | if (Settings.region_force) |
453 | FCEUI_SetVidSystem(Settings.region_force - 1); |
b2b95d2e |
454 | ParseGI(fceugi); |
b2b95d2e |
455 | InitOtherInput(); |
456 | |
9b451455 |
457 | if ((eoptions&EO_GG) && geniestage == 0) { |
458 | strcpy(infostring, "gg.rom is missing, GG disabled"); |
459 | eoptions&=~EO_GG; |
460 | FCEUI_SetGameGenie(0); |
461 | } else |
462 | GameInterface(GI_INFOSTRING, infostring); |
c4980f9e |
463 | FCEU_DispMessage("%s", infostring); |
b2b95d2e |
464 | } |
465 | else |
989672f4 |
466 | { |
467 | switch(LoadGameLastError) { |
4a2a617a |
468 | default: menu_update_msg("failed to load ROM"); break; |
469 | case 2: menu_update_msg("Can't find a ROM for ips/movie"); break; |
470 | case 10: menu_update_msg("FDS BIOS ROM is missing, read docs"); break; |
471 | case 11: menu_update_msg("Error reading auxillary FDS file"); break; |
989672f4 |
472 | } |
473 | } |
b2b95d2e |
474 | } |
475 | if(Exit || !fceugi) |
476 | { |
477 | int ret; |
4a2a617a |
478 | ret = menu_loop(); |
b2b95d2e |
479 | if (ret == 1) break; // exit emu |
480 | if (ret == 2) { // reload ROM |
481 | Exit = 0; |
482 | continue; |
483 | } |
484 | } |
485 | |
971a1d07 |
486 | PrepareOtherInput(); |
21afaa36 |
487 | FCEUI_GetCurrentVidSystem(&srendline,&erendline); |
4a2a617a |
488 | platform_apply_config(); |
21afaa36 |
489 | CleanSurface(); |
4a2a617a |
490 | StartSound(); |
7b356ee3 |
491 | RefreshThrottleFPS(); |
b2b95d2e |
492 | FCEUI_Emulate(); |
493 | } |
35868d35 |
494 | |
b547bda7 |
495 | if (fceugi) |
496 | CloseGame(); |
497 | |
989672f4 |
498 | SaveLLGN(); |
6244011f |
499 | FCEUI_Kill(); |
35868d35 |
500 | DriverKill(); |
4a2a617a |
501 | platform_finish(); |
502 | return 0; |
35868d35 |
503 | } |
504 | |
505 | static int DriverInitialize(void) |
506 | { |
507 | SetSignals((void *)CloseStuff); |
508 | |
35868d35 |
509 | if(!InitVideo()) return 0; |
510 | inited|=4; |
35868d35 |
511 | return 1; |
512 | } |
513 | |
514 | static void DriverKill(void) |
515 | { |
989672f4 |
516 | // SaveConfig(NULL); // done explicitly in menu now |
cc981841 |
517 | SetSignals(SIG_DFL); |
35868d35 |
518 | |
6f6bc6fa |
519 | if(cpalette) free(cpalette); |
520 | cpalette=0; |
521 | |
35868d35 |
522 | if(inited&4) |
523 | KillVideo(); |
524 | if(inited&1) |
525 | KillSound(); |
35868d35 |
526 | inited=0; |
527 | } |
528 | |
937bf65b |
529 | void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count) |
35868d35 |
530 | { |
7b356ee3 |
531 | FCEUD_UpdateInput(); // must update input before blitting because of save confirmation stuff |
532 | BlitPrepare(xbuf == NULL); |
0bb3fe12 |
533 | if(!(eoptions&EO_NOTHROTTLE)) |
534 | { |
535 | if(Count) |
536 | WriteSound(Buffer,Count); |
35868d35 |
537 | SpeedThrottle(); |
0bb3fe12 |
538 | } |
7b356ee3 |
539 | BlitScreen(xbuf == NULL); |
540 | // make sure last frame won't get skipped, because we need it for menu bg |
541 | if (Exit) FSkip=0; |
35868d35 |
542 | } |
543 | |
4a2a617a |
544 | |