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>
35 #include "../common/config.h"
36 #include "../common/args.h"
37 #include "../common/unixdsp.h"
38 #include "../common/cheat.h"
42 // just for printing some iNES info for user..
43 #include "../../fce.h"
44 #include "../../ines.h"
47 extern char lastLoadedGameName[2048];
48 extern uint8 Exit; // exit emu loop flag
51 FCEUGI *fceugi = NULL;
52 static int ntsccol=0,ntschue=-1,ntsctint=-1;
56 int srendlinev[2]={0,0};
57 int erendlinev[2]={239,239};
58 int srendline,erendline;
61 static char BaseDirectory[2048];
65 static void DriverKill(void);
66 static int DriverInitialize(void);
71 static void ParseGI(FCEUGI *gi)
75 InputType[0]=UsrInputType[0];
76 InputType[1]=UsrInputType[1];
77 InputTypeFC=UsrInputTypeFC;
80 InputType[0]=gi->input[0];
82 InputType[1]=gi->input[1];
84 InputTypeFC=gi->inputfc;
85 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
88 void FCEUD_PrintError(char *s)
93 static char *cpalette=0;
94 static void LoadCPalette(void)
99 if(!(fp=fopen(cpalette,"rb")))
101 printf(" Error loading custom palette from file: %s\n",cpalette);
104 fread(tmpp,1,192,fp);
105 FCEUI_SetPaletteArray((uint8 *)tmpp);
109 static CFGSTRUCT fceuconfig[]={
122 ACA(lastLoadedGameName),
123 ADDCFGSTRUCT(DriverConfig),
127 void SaveConfig(const char *name)
131 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
132 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
133 DriverInterface(DES_GETNTSCTINT,&ntsctint);
134 DriverInterface(DES_GETNTSCHUE,&ntschue);
135 SaveFCEUConfig(tdir,fceuconfig);
138 static void LoadConfig(const char *name)
142 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
143 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
144 LoadFCEUConfig(tdir,fceuconfig);
145 if(ntsctint>=0) DriverInterface(DES_SETNTSCTINT,&ntsctint);
146 if(ntschue>=0) DriverInterface(DES_SETNTSCHUE,&ntschue);
149 static void CreateDirs(void)
151 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg"};
155 mkdir(BaseDirectory,S_IRWXU);
156 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
158 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
163 static void SetSignals(void (*t)(int))
165 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
171 static void CloseStuff(int signum)
174 printf("\nSignal %d has been caught and dealt with...\n",signum);
177 case SIGINT:printf("How DARE you interrupt me!\n");break;
178 case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
179 case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
180 case SIGPIPE:printf("The pipe has broken! Better watch out for floods...\n");break;
181 case SIGSEGV:printf("Iyeeeeeeeee!!! A segmentation fault has occurred. Have a fluffy day.\n");break;
182 /* So much SIGBUS evil. */
185 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
188 case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break;
189 case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
190 case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
192 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
197 static int DoArgs(int argc, char *argv[])
199 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
200 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
201 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
202 static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
203 static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
204 SIFC_4PLAYER,SIFC_FKB};
207 static char *inputa[2]={0,0};
208 static char *fcexp=0;
209 static int docheckie[4];
211 static ARGPSTRUCT FCEUArgs[]={
212 {"-soundvol",0,&soundvol,0},
213 {"-cpalette",0,&cpalette,0x4001},
215 {"-ntsccol",0,&ntsccol,0},
216 {"-pal",&docheckie[0],0,0},
217 {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
218 {"-fcexp",0,&fcexp,0x4001},
220 {"-gg",&docheckie[1],0,0},
221 {"-no8lim",0,&eoptions,0x8001},
222 {"-subase",0,&eoptions,0x8002},
223 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
224 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
225 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
226 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
227 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
228 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
229 {0,(void *)DriverArgs,0,0},
233 memset(docheckie,0,sizeof(docheckie));
234 ret=ParseArguments(argc, argv, FCEUArgs);
245 FCEUI_SetVidSystem(1);
247 FCEUI_SetGameGenie(1);
248 FCEUI_DisableSpriteLimitation(1);
249 FCEUI_SaveExtraDataUnderBase(eoptions&2);
250 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
254 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
255 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
258 printf("main() setrendered lines: %d, %d, %d, %d\n",srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
259 printf("main() clip sides %d\n", eoptions&EO_CLIPSIDES);
261 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
262 FCEUI_SetRenderedLines(0,erendlinev[0],srendlinev[1],erendlinev[1]);
263 FCEUI_SetSoundVolume(soundvol);
264 DriverInterface(DES_NTSCCOL,&ntsccol);
272 if(!strncmp(fccortab[y],fcexp,8))
274 UsrInputTypeFC=fccortabi[y];
289 if(!strncmp(cortab[y],inputa[x],8))
291 UsrInputType[x]=cortabi[y];
294 powerpadside&=~(1<<x);
295 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
307 int CLImain(int argc, char *argv[])
316 if(!DriverInitialize())
321 if(!FCEUI_Initialize())
323 GetBaseDirectory(BaseDirectory);
324 FCEUI_SetBaseDirectory(BaseDirectory);
325 lastLoadedGameName[0] = 0;
329 last_arg_parsed=DoArgs(argc-1,&argv[1]);
331 gp2x_cpuclock_update();
337 if (argc > 1 && !last_arg_parsed)
339 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
340 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
354 fceugi=FCEUI_LoadGame(lastLoadedGameName);
357 LoadConfig(lastLoadedGameName);
359 //RefreshThrottleFPS();
362 // additional print for gpfce
363 // TODO: handlers for other formats then iNES
366 iNES_HEADER *head = iNESGetHead();
367 MapperNo = (head->ROM_type>>4);
368 MapperNo|=(head->ROM_type2&0xF0);
369 FCEU_DispMessage("%s, Mapper: %d%s%s", PAL?"PAL":"NTSC", MapperNo, (head->ROM_type&2)?", BB":"", (head->ROM_type&4)?", T":"");
373 strcpy(menuErrorMsg, "failed to load ROM");
378 ret = gp2x_menu_do();
379 if (ret == 1) break; // exit emu
380 if (ret == 2) { // reload ROM
386 gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
387 gp2x_video_RGB_setscaling(0, 320, 240);
388 gp2x_start_sound(22050, 16, 0);
399 static int DriverInitialize(void)
401 SetSignals((void *)CloseStuff);
405 if(!InitVideo()) return 0;
407 if(!InitKeyboard()) return 0;
412 static void DriverKill(void)
430 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
432 if(!Count && !NoWaiting && !(eoptions&EO_NOTHROTTLE))
435 if(Count && !NoWaiting && !(eoptions&EO_NOTHROTTLE))
436 WriteSound(Buffer,Count);