1 /* FCE Ultra - NES/Famicom Emulator
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.
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.
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
20 #include <sys/types.h>
41 #include "../../fce.h"
42 #include "../../cart.h"
45 // these are now here to try to make compatible configs
46 // between different versions of the emu
48 CFGSTRUCT DriverConfig[]={
49 AC(Settings.turbo_rate_add),
50 AC(Settings.sound_rate),
53 AC(Settings.frameskip),
54 AC(Settings.sstate_confirm),
55 AC(Settings.region_force),
56 AC(Settings.cpuclock),
58 AC(Settings.ramtimings),
60 AC(Settings.perfect_vsync),
61 AC(Settings.accurate_mode),
65 void CleanSurface(void);
68 extern uint8 Exit; // exit emu loop flag
72 FCEUGI *fceugi = NULL;
73 int ntsccol=0,ntschue=-1,ntsctint=-1;
77 int srendlinev[2]={8,0};
78 int erendlinev[2]={231,239};
79 int srendline,erendline;
82 static char BaseDirectory[2048];
86 static void DriverKill(void);
87 static int DriverInitialize(void);
92 static void ParseGI(FCEUGI *gi)
96 InputType[0]=UsrInputType[0];
97 InputType[1]=UsrInputType[1];
98 InputTypeFC=UsrInputTypeFC;
101 InputType[0]=gi->input[0];
103 InputType[1]=gi->input[1];
105 InputTypeFC=gi->inputfc;
108 void FCEUD_PrintError(char *s)
114 void LoadCPalette(void)
119 if(!(fp=fopen(cpalette,"rb")))
121 printf(" Error loading custom palette from file: %s\n",cpalette);
126 fread(tmpp,1,192,fp);
127 FCEUI_SetPaletteArray((uint8 *)tmpp);
131 static CFGSTRUCT fceuconfig[]={
144 ADDCFGSTRUCT(DriverConfig),
148 static const char *skip_path(const char *path)
151 if (path == NULL) return NULL;
152 for (p = path+strlen(path)-1; p > path && *p != '/'; p--);
157 int SaveConfig(const char *llgn_path)
159 const char *name = skip_path(llgn_path);
163 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
164 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
165 printf("saving cfg to %s ... ", tdir); fflush(stdout);
166 FCEUI_GetNTSCTH(&ntsctint, &ntschue);
167 ret=SaveFCEUConfig(tdir,fceuconfig);
168 printf(ret == 0 ? "done\n" : "failed\n");
172 static int LoadConfig(const char *llgn_path)
174 const char *name = skip_path(llgn_path);
178 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
179 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
180 printf("loading cfg from %s ... ", tdir); fflush(stdout);
181 FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
182 ret=LoadFCEUConfig(tdir,fceuconfig);
183 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
184 printf(ret == 0 ? "done\n" : "failed\n");
188 static void LoadLLGN(void)
193 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
197 len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
198 lastLoadedGameName[len] = 0;
203 static void SaveLLGN(void)
205 // save last loaded game name
206 if (lastLoadedGameName[0] && !(eoptions&EO_NOAUTOWRITE))
210 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
214 fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
222 static void CreateDirs(void)
224 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
228 mkdir(BaseDirectory,S_IRWXU);
229 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
231 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
236 static void SetSignals(void (*t)(int))
238 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
244 static void CloseStuff(int signum)
247 printf("\nSignal %d has been caught and dealt with...\n",signum);
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;
256 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
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;
262 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
267 static int DoArgs(int argc, char *argv[])
269 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
270 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
271 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
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};
277 static char *inputa[2]={0,0};
278 static char *fcexp=0;
279 static int docheckie[4];
281 static int docheckie2[2]={0,0};
284 static ARGPSTRUCT FCEUArgs[]={
285 {"-soundvol",0,&soundvol,0},
286 {"-cpalette",0,&cpalette,0x4001},
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},
293 {"-gg",0,&eoptions,0x8000|EO_GG},
294 {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
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},
299 {"-noautowrite",0,&eoptions,0x8000|EO_NOAUTOWRITE},
300 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
301 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
302 {"-sound",0,&Settings.sound_rate,0},
303 {"-showfps",0,&Settings.showfps,0},
305 {"-connect",&docheckie2[0],&netplayhost,0x4001},
306 {"-server",&docheckie2[1],0,0},
307 {"-netport",0,&Port,0},
309 {0,(void *)DriverArgs,0,0},
313 memset(docheckie,0,sizeof(docheckie));
314 ret=ParseArguments(argc, argv, FCEUArgs);
325 Settings.region_force=2;
326 FCEUI_SetGameGenie(eoptions&EO_GG);
327 FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);
328 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
332 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
333 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
336 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
337 FCEUI_SetSoundVolume(80);
341 else if(docheckie2[1])
345 FCEUI_SetNetworkPlay(netplay);
354 if(!strncmp(fccortab[y],fcexp,8))
356 UsrInputTypeFC=fccortabi[y];
371 if(!strncmp(cortab[y],inputa[x],8))
373 UsrInputType[x]=cortabi[y];
376 powerpadside&=~(1<<x);
377 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
389 int main(int argc, char *argv[])
391 int last_arg_parsed, ret;
399 if(!DriverInitialize())
404 if(!FCEUI_Initialize())
406 GetBaseDirectory(BaseDirectory);
407 FCEUI_SetBaseDirectory(BaseDirectory);
408 lastLoadedGameName[0] = 0;
414 last_arg_parsed=DoArgs(argc-1,&argv[1]);
415 platform_late_init();
418 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
424 if (argc > 1 && !last_arg_parsed)
426 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
427 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
441 ret=LoadConfig(lastLoadedGameName);
446 FCEUI_SetEmuMode(Settings.accurate_mode);
447 fceugi=FCEUI_LoadGame(lastLoadedGameName);
451 if (Settings.region_force)
452 FCEUI_SetVidSystem(Settings.region_force - 1);
456 if ((eoptions&EO_GG) && geniestage == 0) {
457 strcpy(infostring, "gg.rom is missing, GG disabled");
459 FCEUI_SetGameGenie(0);
461 GameInterface(GI_INFOSTRING, infostring);
462 FCEU_DispMessage("%s", infostring);
466 switch(LoadGameLastError) {
467 default: menu_update_msg("failed to load ROM"); break;
468 case 2: menu_update_msg("Can't find a ROM for ips/movie"); break;
469 case 10: menu_update_msg("FDS BIOS ROM is missing, read docs"); break;
470 case 11: menu_update_msg("Error reading auxillary FDS file"); break;
478 if (ret == 1) break; // exit emu
479 if (ret == 2) { // reload ROM
486 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
487 platform_apply_config();
490 RefreshThrottleFPS();
504 static int DriverInitialize(void)
506 SetSignals((void *)CloseStuff);
508 if(!InitVideo()) return 0;
513 static void DriverKill(void)
515 // SaveConfig(NULL); // done explicitly in menu now
518 if(cpalette) free(cpalette);
528 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
530 FCEUD_UpdateInput(); // must update input before blitting because of save confirmation stuff
531 BlitPrepare(xbuf == NULL);
532 if(!(eoptions&EO_NOTHROTTLE))
535 WriteSound(Buffer,Count);
538 BlitScreen(xbuf == NULL);
539 // make sure last frame won't get skipped, because we need it for menu bg