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"
43 // just for printing some iNES info for user..
44 #include "../../fce.h"
45 #include "../../ines.h"
48 extern char lastLoadedGameName[2048];
49 extern uint8 Exit; // exit emu loop flag
52 FCEUGI *fceugi = NULL;
53 static int ntsccol=0,ntschue=-1,ntsctint=-1;
57 int srendlinev[2]={0,0};
58 int erendlinev[2]={239,239};
59 int srendline,erendline;
62 static char BaseDirectory[2048];
66 static void DriverKill(void);
67 static int DriverInitialize(void);
72 static void ParseGI(FCEUGI *gi)
76 InputType[0]=UsrInputType[0];
77 InputType[1]=UsrInputType[1];
78 InputTypeFC=UsrInputTypeFC;
81 InputType[0]=gi->input[0];
83 InputType[1]=gi->input[1];
85 InputTypeFC=gi->inputfc;
86 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
89 void FCEUD_PrintError(char *s)
94 static char *cpalette=0;
95 static void LoadCPalette(void)
100 if(!(fp=fopen(cpalette,"rb")))
102 printf(" Error loading custom palette from file: %s\n",cpalette);
105 fread(tmpp,1,192,fp);
106 FCEUI_SetPaletteArray((uint8 *)tmpp);
110 static CFGSTRUCT fceuconfig[]={
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 LoadLLGN(void)
154 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
158 len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
159 lastLoadedGameName[len] = 0;
164 static void SaveLLGN(void)
166 // save last loaded game name
167 if (lastLoadedGameName[0])
171 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
175 fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
183 static void CreateDirs(void)
185 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg"};
189 mkdir(BaseDirectory,S_IRWXU);
190 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
192 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
197 static void SetSignals(void (*t)(int))
199 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
205 static void CloseStuff(int signum)
208 printf("\nSignal %d has been caught and dealt with...\n",signum);
211 case SIGINT:printf("How DARE you interrupt me!\n");break;
212 case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
213 case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
214 case SIGPIPE:printf("The pipe has broken! Better watch out for floods...\n");break;
215 case SIGSEGV:printf("Iyeeeeeeeee!!! A segmentation fault has occurred. Have a fluffy day.\n");break;
216 /* So much SIGBUS evil. */
219 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
222 case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break;
223 case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
224 case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
226 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
231 static int DoArgs(int argc, char *argv[])
233 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
234 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
235 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
236 static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
237 static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
238 SIFC_4PLAYER,SIFC_FKB};
241 static char *inputa[2]={0,0};
242 static char *fcexp=0;
243 static int docheckie[4];
245 static ARGPSTRUCT FCEUArgs[]={
246 {"-soundvol",0,&soundvol,0},
247 {"-cpalette",0,&cpalette,0x4001},
249 {"-ntsccol",0,&ntsccol,0},
250 {"-pal",&docheckie[0],0,0},
251 {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
252 {"-fcexp",0,&fcexp,0x4001},
254 {"-gg",&docheckie[1],0,0},
255 {"-no8lim",0,&eoptions,0x8001},
256 {"-subase",0,&eoptions,0x8002},
257 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
258 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
259 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
260 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
261 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
262 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
263 {0,(void *)DriverArgs,0,0},
267 memset(docheckie,0,sizeof(docheckie));
268 ret=ParseArguments(argc, argv, FCEUArgs);
279 Settings.region_force=2;
281 FCEUI_SetGameGenie(1);
282 FCEUI_DisableSpriteLimitation(1);
283 FCEUI_SaveExtraDataUnderBase(eoptions&2);
284 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
288 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
289 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
292 printf("main() setrendered lines: %d, %d, %d, %d\n",srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
293 printf("main() clip sides %d\n", eoptions&EO_CLIPSIDES);
295 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
296 FCEUI_SetRenderedLines(0,erendlinev[0],srendlinev[1],erendlinev[1]);
297 FCEUI_SetSoundVolume(soundvol);
298 DriverInterface(DES_NTSCCOL,&ntsccol); // TODO
306 if(!strncmp(fccortab[y],fcexp,8))
308 UsrInputTypeFC=fccortabi[y];
323 if(!strncmp(cortab[y],inputa[x],8))
325 UsrInputType[x]=cortabi[y];
328 powerpadside&=~(1<<x);
329 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
341 int CLImain(int argc, char *argv[])
350 if(!DriverInitialize())
355 if(!FCEUI_Initialize())
357 GetBaseDirectory(BaseDirectory);
358 FCEUI_SetBaseDirectory(BaseDirectory);
359 lastLoadedGameName[0] = 0;
363 last_arg_parsed=DoArgs(argc-1,&argv[1]);
365 gp2x_cpuclock_gamma_update();
372 if (argc > 1 && !last_arg_parsed)
374 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
375 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
389 fceugi=FCEUI_LoadGame(lastLoadedGameName);
392 LoadConfig(lastLoadedGameName);
393 if (Settings.region_force)
394 FCEUI_SetVidSystem(Settings.region_force - 1);
396 //RefreshThrottleFPS();
399 // additional print for gpfce
400 // TODO: handlers for other formats then iNES
403 iNES_HEADER *head = iNESGetHead(); // TODO: ReMake
404 MapperNo = (head->ROM_type>>4);
405 MapperNo|=(head->ROM_type2&0xF0);
406 FCEU_DispMessage("%s, Mapper: %d%s%s", PAL?"PAL":"NTSC", MapperNo, (head->ROM_type&2)?", BB":"", (head->ROM_type&4)?", T":"");
411 switch(LoadGameLastError) {
412 default: strcpy(menuErrorMsg, "failed to load ROM"); break;
413 case 2: strcpy(menuErrorMsg, "Can't find a ROM for movie"); break;
414 case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
415 case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
422 ret = gp2x_menu_do();
423 if (ret == 1) break; // exit emu
424 if (ret == 2) { // reload ROM
431 gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
432 switch (Settings.scaling & 3) {
433 case 0: gp2x_video_RGB_setscaling(0, 320, 240); gp2x_video_set_offs(0); break;
434 case 1: gp2x_video_RGB_setscaling(0, 256, 240); gp2x_video_set_offs(32); break;
435 case 2: gp2x_video_RGB_setscaling(0, 256, 240); gp2x_video_set_offs(32); break; // TODO
436 case 3: gp2x_video_RGB_setscaling(0, 320, 240); gp2x_video_set_offs(32); break;
438 gp2x_start_sound(Settings.sound_rate, 16, 0);
450 static int DriverInitialize(void)
452 SetSignals((void *)CloseStuff);
454 if(!InitVideo()) return 0;
459 static void DriverKill(void)
461 // SaveConfig(NULL); // done explicitly in menu now
471 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
473 if(!Count && !(eoptions&EO_NOTHROTTLE))
476 if(Count && !(eoptions&EO_NOTHROTTLE))
477 WriteSound(Buffer,Count);