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>
36 #include "../common/config.h"
37 #include "../common/args.h"
38 #include "../common/unixdsp.h"
39 #include "../common/cheat.h"
41 #include "../../fce.h"
42 #include "../../cart.h"
47 void CleanSurface(void);
50 extern char lastLoadedGameName[2048];
51 extern uint8 Exit; // exit emu loop flag
54 FCEUGI *fceugi = NULL;
55 int ntsccol=0,ntschue=-1,ntsctint=-1;
59 int srendlinev[2]={8,0};
60 int erendlinev[2]={231,239};
61 int srendline,erendline;
64 static char BaseDirectory[2048];
68 static void DriverKill(void);
69 static int DriverInitialize(void);
74 static void ParseGI(FCEUGI *gi)
78 InputType[0]=UsrInputType[0];
79 InputType[1]=UsrInputType[1];
80 InputTypeFC=UsrInputTypeFC;
83 InputType[0]=gi->input[0];
85 InputType[1]=gi->input[1];
87 InputTypeFC=gi->inputfc;
90 void FCEUD_PrintError(char *s)
96 void LoadCPalette(void)
101 if(!(fp=fopen(cpalette,"rb")))
103 printf(" Error loading custom palette from file: %s\n",cpalette);
108 fread(tmpp,1,192,fp);
109 FCEUI_SetPaletteArray((uint8 *)tmpp);
113 static CFGSTRUCT fceuconfig[]={
126 ADDCFGSTRUCT(DriverConfig),
130 void SaveConfig(const char *name)
134 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
135 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
136 FCEUI_GetNTSCTH(&ntsctint, &ntschue);
137 SaveFCEUConfig(tdir,fceuconfig);
140 static void LoadConfig(const char *name)
144 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
145 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
146 FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
147 LoadFCEUConfig(tdir,fceuconfig);
148 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
151 static void LoadLLGN(void)
156 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
160 len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
161 lastLoadedGameName[len] = 0;
166 static void SaveLLGN(void)
168 // save last loaded game name
169 if (lastLoadedGameName[0])
173 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
177 fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
185 static void CreateDirs(void)
187 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
191 mkdir(BaseDirectory,S_IRWXU);
192 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
194 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
199 static void SetSignals(void (*t)(int))
201 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
207 static void CloseStuff(int signum)
210 printf("\nSignal %d has been caught and dealt with...\n",signum);
213 case SIGINT:printf("How DARE you interrupt me!\n");break;
214 case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
215 case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
216 case SIGPIPE:printf("The pipe has broken! Better watch out for floods...\n");break;
217 case SIGSEGV:printf("Iyeeeeeeeee!!! A segmentation fault has occurred. Have a fluffy day.\n");break;
219 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
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;
225 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
230 static int DoArgs(int argc, char *argv[])
232 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
233 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
234 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
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};
240 static char *inputa[2]={0,0};
241 static char *fcexp=0;
242 static int docheckie[4];
244 static ARGPSTRUCT FCEUArgs[]={
245 {"-soundvol",0,&soundvol,0},
246 {"-cpalette",0,&cpalette,0x4001},
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},
253 {"-gg",0,&eoptions,0x8000|EO_GG},
254 {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
255 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
256 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
257 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
258 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
259 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
260 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
261 {0,(void *)DriverArgs,0,0},
265 memset(docheckie,0,sizeof(docheckie));
266 ret=ParseArguments(argc, argv, FCEUArgs);
277 Settings.region_force=2;
278 FCEUI_SetGameGenie(eoptions&EO_GG);
279 FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);
280 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
284 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
285 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
288 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
289 FCEUI_SetSoundVolume(soundvol);
297 if(!strncmp(fccortab[y],fcexp,8))
299 UsrInputTypeFC=fccortabi[y];
314 if(!strncmp(cortab[y],inputa[x],8))
316 UsrInputType[x]=cortabi[y];
319 powerpadside&=~(1<<x);
320 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
332 int CLImain(int argc, char *argv[])
341 if(!DriverInitialize())
346 if(!FCEUI_Initialize())
348 GetBaseDirectory(BaseDirectory);
349 FCEUI_SetBaseDirectory(BaseDirectory);
350 lastLoadedGameName[0] = 0;
354 last_arg_parsed=DoArgs(argc-1,&argv[1]);
356 gp2x_cpuclock_gamma_update();
358 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
364 if (argc > 1 && !last_arg_parsed)
366 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
367 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
381 fceugi=FCEUI_LoadGame(lastLoadedGameName);
385 LoadConfig(lastLoadedGameName);
386 if (Settings.region_force)
387 FCEUI_SetVidSystem(Settings.region_force - 1);
391 if ((eoptions&EO_GG) && geniestage == 0) {
392 strcpy(infostring, "gg.rom is missing, GG disabled");
394 FCEUI_SetGameGenie(0);
396 GameInterface(GI_INFOSTRING, infostring);
397 FCEU_DispMessage("%s", infostring);
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;
412 ret = gp2x_menu_do();
413 if (ret == 1) break; // exit emu
414 if (ret == 2) { // reload ROM
421 RefreshThrottleFPS();
422 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
423 gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
424 switch (Settings.scaling & 3) {
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;
431 gp2x_start_sound(Settings.sound_rate, 16, 0);
443 static int DriverInitialize(void)
445 SetSignals((void *)CloseStuff);
447 if(!InitVideo()) return 0;
452 static void DriverKill(void)
454 // SaveConfig(NULL); // done explicitly in menu now
457 if(cpalette) free(cpalette);
467 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
469 if(!(eoptions&EO_NOTHROTTLE))
472 WriteSound(Buffer,Count);
475 FCEUD_UpdateInput(); // must update input before blitting because of save confirmation stuff