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 ADDCFGSTRUCT(DriverConfig),
126 void SaveConfig(const char *name)
130 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
131 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
132 DriverInterface(DES_GETNTSCTINT,&ntsctint);
133 DriverInterface(DES_GETNTSCHUE,&ntschue);
134 SaveFCEUConfig(tdir,fceuconfig);
137 static void LoadConfig(const char *name)
141 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
142 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
143 LoadFCEUConfig(tdir,fceuconfig);
144 if(ntsctint>=0) DriverInterface(DES_SETNTSCTINT,&ntsctint);
145 if(ntschue>=0) DriverInterface(DES_SETNTSCHUE,&ntschue);
148 static void LoadLLGN(void)
153 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
157 len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
158 lastLoadedGameName[len] = 0;
163 static void SaveLLGN(void)
165 // save last loaded game name
166 if (lastLoadedGameName[0])
170 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
174 fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
182 static void CreateDirs(void)
184 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg"};
188 mkdir(BaseDirectory,S_IRWXU);
189 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
191 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
196 static void SetSignals(void (*t)(int))
198 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
204 static void CloseStuff(int signum)
207 printf("\nSignal %d has been caught and dealt with...\n",signum);
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. */
218 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",&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},
262 {0,(void *)DriverArgs,0,0},
266 memset(docheckie,0,sizeof(docheckie));
267 ret=ParseArguments(argc, argv, FCEUArgs);
278 Settings.region_force=2;
280 FCEUI_SetGameGenie(1);
281 FCEUI_DisableSpriteLimitation(1);
282 FCEUI_SaveExtraDataUnderBase(eoptions&2);
283 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
287 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
288 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
291 printf("main() setrendered lines: %d, %d, %d, %d\n",srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
292 printf("main() clip sides %d\n", eoptions&EO_CLIPSIDES);
294 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
295 FCEUI_SetRenderedLines(0,erendlinev[0],srendlinev[1],erendlinev[1]);
296 FCEUI_SetSoundVolume(soundvol);
297 DriverInterface(DES_NTSCCOL,&ntsccol);
305 if(!strncmp(fccortab[y],fcexp,8))
307 UsrInputTypeFC=fccortabi[y];
322 if(!strncmp(cortab[y],inputa[x],8))
324 UsrInputType[x]=cortabi[y];
327 powerpadside&=~(1<<x);
328 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
340 int CLImain(int argc, char *argv[])
349 if(!DriverInitialize())
354 if(!FCEUI_Initialize())
356 GetBaseDirectory(BaseDirectory);
357 FCEUI_SetBaseDirectory(BaseDirectory);
358 lastLoadedGameName[0] = 0;
362 last_arg_parsed=DoArgs(argc-1,&argv[1]);
364 gp2x_cpuclock_gamma_update();
371 if (argc > 1 && !last_arg_parsed)
373 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
374 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
388 fceugi=FCEUI_LoadGame(lastLoadedGameName);
391 LoadConfig(lastLoadedGameName);
392 if (Settings.region_force)
393 FCEUI_SetVidSystem(Settings.region_force - 1);
395 //RefreshThrottleFPS();
398 // additional print for gpfce
399 // TODO: handlers for other formats then iNES
402 iNES_HEADER *head = iNESGetHead();
403 MapperNo = (head->ROM_type>>4);
404 MapperNo|=(head->ROM_type2&0xF0);
405 FCEU_DispMessage("%s, Mapper: %d%s%s", PAL?"PAL":"NTSC", MapperNo, (head->ROM_type&2)?", BB":"", (head->ROM_type&4)?", T":"");
410 switch(LoadGameLastError) {
411 default: strcpy(menuErrorMsg, "failed to load ROM"); break;
412 case 2: strcpy(menuErrorMsg, "Can't find a ROM for movie"); break;
413 case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
414 case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
421 ret = gp2x_menu_do();
422 if (ret == 1) break; // exit emu
423 if (ret == 2) { // reload ROM
429 gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
430 switch (Settings.scaling & 3) {
431 case 0: gp2x_video_RGB_setscaling(0, 320, 240); gp2x_video_set_offs(0); break;
432 case 1: gp2x_video_RGB_setscaling(0, 256, 240); gp2x_video_set_offs(32); break;
433 case 2: gp2x_video_RGB_setscaling(0, 256, 240); gp2x_video_set_offs(32); break; // TODO
434 case 3: gp2x_video_RGB_setscaling(0, 320, 240); gp2x_video_set_offs(32); break;
436 gp2x_start_sound(Settings.sound_rate, 16, 0);
448 static int DriverInitialize(void)
450 SetSignals((void *)CloseStuff);
454 if(!InitVideo()) return 0;
456 if(!InitKeyboard()) return 0;
461 static void DriverKill(void)
463 // SaveConfig(NULL); // done explicitly in menu now
479 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
481 if(!Count && !NoWaiting && !(eoptions&EO_NOTHROTTLE))
484 if(Count && !NoWaiting && !(eoptions&EO_NOTHROTTLE))
485 WriteSound(Buffer,Count);