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