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