fceu options + support code
[fceu.git] / drivers / gp2x / main.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
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.
7  *
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.
12  *
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
16  */
17
18
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <signal.h>
22 #include <sys/time.h>
23 #include <sys/stat.h>
24 #include <string.h>
25 #include <strings.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "main.h"
31 #include "minimal.h"
32 #include "throttle.h"
33 #include "menu.h"
34 #include "gp2x.h"
35
36 #include "../common/config.h"
37 #include "../common/args.h"
38 #include "../common/unixdsp.h"
39 #include "../common/cheat.h"
40
41 #include "../../fce.h"
42
43 #include "dface.h"
44
45
46 // TODO! broken fs0, sram saves
47
48 void CleanSurface(void);
49
50 // internals
51 extern char lastLoadedGameName[2048];
52 extern uint8 Exit; // exit emu loop flag
53 void CloseGame(void);
54
55 FCEUGI *fceugi = NULL;
56 int ntsccol=0,ntschue=-1,ntsctint=-1;
57 int soundvol=70;
58 int inited=0;
59
60 int srendlinev[2]={8,0};
61 int erendlinev[2]={231,239};
62 int srendline,erendline;
63
64
65 static char BaseDirectory[2048];
66
67 int eoptions=0;
68
69 static void DriverKill(void);
70 static int DriverInitialize(void);
71
72 static int gametype;
73 #include "input.c"
74
75 static void ParseGI(FCEUGI *gi)
76 {
77  gametype=gi->type;
78
79  InputType[0]=UsrInputType[0];
80  InputType[1]=UsrInputType[1];
81  InputTypeFC=UsrInputTypeFC;
82
83  if(gi->input[0]>=0)
84   InputType[0]=gi->input[0];
85  if(gi->input[1]>=0)
86   InputType[1]=gi->input[1];
87  if(gi->inputfc>=0)
88   InputTypeFC=gi->inputfc;
89 }
90
91 void FCEUD_PrintError(char *s)
92 {
93  puts(s);
94 }
95
96 static char *cpalette=0;
97 static void LoadCPalette(void)
98 {
99  char tmpp[192];
100  FILE *fp;
101
102  if(!(fp=fopen(cpalette,"rb")))
103  {
104   printf(" Error loading custom palette from file: %s\n",cpalette);
105   return;
106  }
107  fread(tmpp,1,192,fp);
108  FCEUI_SetPaletteArray((uint8 *)tmpp);
109  fclose(fp);
110 }
111
112 static CFGSTRUCT fceuconfig[]={
113         AC(soundvol),
114         ACS(cpalette),
115         AC(ntsctint),
116         AC(ntschue),
117         AC(ntsccol),
118         AC(UsrInputTypeFC),
119         ACA(UsrInputType),
120         AC(powerpadside),
121         AC(powerpadsc),
122         AC(eoptions),
123         ACA(srendlinev),
124         ACA(erendlinev),
125         ADDCFGSTRUCT(DriverConfig),
126         ENDCFGSTRUCT
127 };
128
129 void SaveConfig(const char *name)
130 {
131         char tdir[2048];
132         if (name)
133              sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
134         else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
135         FCEUI_GetNTSCTH(&ntsctint, &ntschue);
136         SaveFCEUConfig(tdir,fceuconfig);
137 }
138
139 static void LoadConfig(const char *name)
140 {
141         char tdir[2048];
142         if (name)
143              sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
144         else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
145         FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
146         LoadFCEUConfig(tdir,fceuconfig);
147         FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
148 }
149
150 static void LoadLLGN(void)
151 {
152  char tdir[2048];
153  FILE *f;
154  int len;
155  sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
156  f=fopen(tdir, "r");
157  if(f)
158  {
159   len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
160   lastLoadedGameName[len] = 0;
161   fclose(f);
162  }
163 }
164
165 static void SaveLLGN(void)
166 {
167  // save last loaded game name
168  if (lastLoadedGameName[0])
169  {
170   char tdir[2048];
171   FILE *f;
172   sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
173   f=fopen(tdir, "w");
174   if(f)
175   {
176    fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
177    fclose(f);
178    sync();
179   }
180  }
181 }
182
183
184 static void CreateDirs(void)
185 {
186  char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg"};
187  char tdir[2048];
188  int x;
189
190  mkdir(BaseDirectory,S_IRWXU);
191  for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
192  {
193   sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
194   mkdir(tdir,S_IRWXU);
195  }
196 }
197
198 static void SetSignals(void (*t)(int))
199 {
200   int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
201   int x;
202   for(x=0;x<11;x++)
203    signal(sigs[x],t);
204 }
205
206 static void CloseStuff(int signum)
207 {
208         DriverKill();
209         printf("\nSignal %d has been caught and dealt with...\n",signum);
210         switch(signum)
211         {
212          case SIGINT:printf("How DARE you interrupt me!\n");break;
213          case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
214          case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
215          case SIGPIPE:printf("The pipe has broken!  Better watch out for floods...\n");break;
216          case SIGSEGV:printf("Iyeeeeeeeee!!!  A segmentation fault has occurred.  Have a fluffy day.\n");break;
217          /* So much SIGBUS evil. */
218          #ifdef SIGBUS
219          #if(SIGBUS!=SIGSEGV)
220          case SIGBUS:printf("I told you to be nice to the driver.\n");break;
221          #endif
222          #endif
223          case SIGFPE:printf("Those darn floating points.  Ne'er know when they'll bite!\n");break;
224          case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
225          case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
226          case SIGUSR1:
227          case SIGUSR2:printf("Killing your processes is not nice.\n");break;
228         }
229         exit(1);
230 }
231
232 static int DoArgs(int argc, char *argv[])
233 {
234         static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
235         static int cortabi[5]={SI_NONE,SI_GAMEPAD,
236                                SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
237         static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
238         static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
239                                  SIFC_4PLAYER,SIFC_FKB};
240
241         int x, ret;
242         static char *inputa[2]={0,0};
243         static char *fcexp=0;
244         static int docheckie[4];
245
246         static ARGPSTRUCT FCEUArgs[]={
247          {"-soundvol",0,&soundvol,0},
248          {"-cpalette",0,&cpalette,0x4001},
249
250          {"-ntsccol",0,&ntsccol,0},
251          {"-pal",&docheckie[0],0,0},
252          {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
253          {"-fcexp",0,&fcexp,0x4001},
254
255          {"-gg",&docheckie[1],0,0},
256          {"-no8lim",0,&eoptions,0x8001},
257          {"-subase",0,&eoptions,0x8002},
258          {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
259          {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
260          {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
261          {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
262          {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
263          {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
264          {0,(void *)DriverArgs,0,0},
265          {0,0,0,0}
266         };
267
268         memset(docheckie,0,sizeof(docheckie));
269         ret=ParseArguments(argc, argv, FCEUArgs);
270         if(cpalette)
271         {
272          if(cpalette[0]=='0')
273           if(cpalette[1]==0)
274           {
275            free(cpalette);
276            cpalette=0;
277           }
278         }
279         if(docheckie[0])
280          Settings.region_force=2;
281         if(docheckie[1])
282          FCEUI_SetGameGenie(1);
283         FCEUI_DisableSpriteLimitation(1);
284         FCEUI_SaveExtraDataUnderBase(eoptions&2);
285         FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
286
287         for(x=0;x<2;x++)
288         {
289          if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
290          if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
291         }
292
293         printf("FCEUI_SetRenderedLines: %d, %d, %d, %d\n",srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
294         printf("clip sides: %d\n", eoptions&EO_CLIPSIDES);
295         FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
296         FCEUI_SetSoundVolume(soundvol);
297         DoDriverArgs();
298
299         if(fcexp)
300         {
301          int y;
302          for(y=0;y<5;y++)
303          {
304           if(!strncmp(fccortab[y],fcexp,8))
305           {
306            UsrInputTypeFC=fccortabi[y];
307            break;
308           }
309          }
310          free(fcexp);
311         }
312         for(x=0;x<2;x++)
313         {
314          int y;
315
316          if(!inputa[x])
317           continue;
318
319          for(y=0;y<5;y++)
320          {
321           if(!strncmp(cortab[y],inputa[x],8))
322           {
323            UsrInputType[x]=cortabi[y];
324            if(y==3)
325            {
326             powerpadside&=~(1<<x);
327             powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
328            }
329            free(inputa[x]);
330           }
331          }
332         }
333         return ret;
334 }
335
336
337 #include "usage.h"
338
339 int CLImain(int argc, char *argv[])
340 {
341         int last_arg_parsed;
342         /* TODO if(argc<=1)
343         {
344          ShowUsage(argv[0]);
345          return 1;
346         }*/
347
348         if(!DriverInitialize())
349         {
350          return 1;
351         }
352
353         if(!FCEUI_Initialize())
354          return(1);
355         GetBaseDirectory(BaseDirectory);
356         FCEUI_SetBaseDirectory(BaseDirectory);
357         lastLoadedGameName[0] = 0;
358
359         CreateDirs();
360         LoadConfig(NULL);
361         last_arg_parsed=DoArgs(argc-1,&argv[1]);
362         gp2x_opt_setup();
363         gp2x_cpuclock_gamma_update();
364         LoadLLGN();
365         FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
366         if(cpalette)
367          LoadCPalette(); // TODO
368         if(InitSound())
369          inited|=1;
370
371         if (argc > 1 && !last_arg_parsed)
372         {
373          strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
374          lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
375          Exit = 0;
376         }
377         else
378         {
379          Exit = 1;
380         }
381
382         while (1)
383         {
384          if(!Exit)
385          {
386           if (fceugi)
387            CloseGame();
388           fceugi=FCEUI_LoadGame(lastLoadedGameName);
389           if (fceugi)
390           {
391            char infostring[32];
392            LoadConfig(lastLoadedGameName);
393            if (Settings.region_force)
394             FCEUI_SetVidSystem(Settings.region_force - 1);
395            ParseGI(fceugi);
396            //RefreshThrottleFPS();
397            InitOtherInput();
398
399            GameInterface(GI_INFOSTRING, infostring);
400            FCEU_DispMessage("%s", infostring);
401           }
402           else
403           {
404            switch(LoadGameLastError) {
405             default: strcpy(menuErrorMsg, "failed to load ROM"); break;
406             case  2: strcpy(menuErrorMsg, "Can't find a ROM for movie"); break;
407             case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
408             case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
409            }
410           }
411          }
412          if(Exit || !fceugi)
413          {
414           int ret;
415           ret = gp2x_menu_do();
416           if (ret == 1) break;          // exit emu
417           if (ret == 2) {               // reload ROM
418            Exit = 0;
419            continue;
420           }
421          }
422
423          PrepareOtherInput();
424          FCEUI_GetCurrentVidSystem(&srendline,&erendline);
425          gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
426          switch (Settings.scaling & 3) {
427                  case 0: gp2x_video_set_offs(0);  gp2x_video_RGB_setscaling(0, 320, 240); break;
428                  case 1: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(0, 256, 240); break;
429                  case 2: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(srendline, 256, erendline-srendline); break;
430                  case 3: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(0, 320, 240); break;
431          }
432          CleanSurface();
433          gp2x_start_sound(Settings.sound_rate, 16, 0);
434          FCEUI_Emulate();
435         }
436
437         if (fceugi)
438          CloseGame();
439
440         SaveLLGN();
441         DriverKill();
442         return 0;
443 }
444
445 static int DriverInitialize(void)
446 {
447    SetSignals((void *)CloseStuff);
448
449    if(!InitVideo()) return 0;
450    inited|=4;
451    return 1;
452 }
453
454 static void DriverKill(void)
455 {
456  // SaveConfig(NULL); // done explicitly in menu now
457  SetSignals(SIG_IGN);
458
459  if(inited&4)
460   KillVideo();
461  if(inited&1)
462   KillSound();
463  inited=0;
464 }
465
466 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
467 {
468  if(!Count && !(eoptions&EO_NOTHROTTLE))
469   SpeedThrottle();
470  BlitScreen(xbuf);
471  if(Count && !(eoptions&EO_NOTHROTTLE))
472   WriteSound(Buffer,Count);
473  FCEUD_UpdateInput();
474 }
475