ips patches, 0.4 r162 rel?
[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 #include "../../cart.h"
43
44 #include "dface.h"
45
46
47 void CleanSurface(void);
48
49 // internals
50 extern char lastLoadedGameName[2048];
51 extern uint8 Exit; // exit emu loop flag
52 extern int FSkip;
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 char *cpalette=0;
97 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   free(cpalette);
106   cpalette=0;
107   return;
108  }
109  fread(tmpp,1,192,fp);
110  FCEUI_SetPaletteArray((uint8 *)tmpp);
111  fclose(fp);
112 }
113
114 static CFGSTRUCT fceuconfig[]={
115         AC(soundvol),
116         ACS(cpalette),
117         AC(ntsctint),
118         AC(ntschue),
119         AC(ntsccol),
120         AC(UsrInputTypeFC),
121         ACA(UsrInputType),
122         AC(powerpadside),
123         AC(powerpadsc),
124         AC(eoptions),
125         ACA(srendlinev),
126         ACA(erendlinev),
127         ADDCFGSTRUCT(DriverConfig),
128         ENDCFGSTRUCT
129 };
130
131 void SaveConfig(const char *name)
132 {
133         char tdir[2048];
134         if (name)
135              sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
136         else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
137         FCEUI_GetNTSCTH(&ntsctint, &ntschue);
138         SaveFCEUConfig(tdir,fceuconfig);
139 }
140
141 static void LoadConfig(const char *name)
142 {
143         char tdir[2048];
144         if (name)
145              sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
146         else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
147         FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
148         LoadFCEUConfig(tdir,fceuconfig);
149         FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
150 }
151
152 static void LoadLLGN(void)
153 {
154  char tdir[2048];
155  FILE *f;
156  int len;
157  sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
158  f=fopen(tdir, "r");
159  if(f)
160  {
161   len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
162   lastLoadedGameName[len] = 0;
163   fclose(f);
164  }
165 }
166
167 static void SaveLLGN(void)
168 {
169  // save last loaded game name
170  if (lastLoadedGameName[0] && !(eoptions&EO_NOAUTOWRITE))
171  {
172   char tdir[2048];
173   FILE *f;
174   sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
175   f=fopen(tdir, "w");
176   if(f)
177   {
178    fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
179    fclose(f);
180    sync();
181   }
182  }
183 }
184
185
186 static void CreateDirs(void)
187 {
188  char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
189  char tdir[2048];
190  int x;
191
192  mkdir(BaseDirectory,S_IRWXU);
193  for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
194  {
195   sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
196   mkdir(tdir,S_IRWXU);
197  }
198 }
199
200 static void SetSignals(void (*t)(int))
201 {
202   int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
203   int x;
204   for(x=0;x<11;x++)
205    signal(sigs[x],t);
206 }
207
208 static void CloseStuff(int signum)
209 {
210         DriverKill();
211         printf("\nSignal %d has been caught and dealt with...\n",signum);
212         switch(signum)
213         {
214          case SIGINT:printf("How DARE you interrupt me!\n");break;
215          case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
216          case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
217          case SIGPIPE:printf("The pipe has broken!  Better watch out for floods...\n");break;
218          case SIGSEGV:printf("Iyeeeeeeeee!!!  A segmentation fault has occurred.  Have a fluffy day.\n");break;
219          #if(SIGBUS!=SIGSEGV)
220          case SIGBUS:printf("I told you to be nice to the driver.\n");break;
221          #endif
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;
225          case SIGUSR1:
226          case SIGUSR2:printf("Killing your processes is not nice.\n");break;
227         }
228         exit(1);
229 }
230
231 static int DoArgs(int argc, char *argv[])
232 {
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};
239
240         int x, ret;
241         static char *inputa[2]={0,0};
242         static char *fcexp=0;
243         static int docheckie[4];
244
245         static ARGPSTRUCT FCEUArgs[]={
246          {"-soundvol",0,&soundvol,0},
247          {"-cpalette",0,&cpalette,0x4001},
248
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},
253
254          {"-gg",0,&eoptions,0x8000|EO_GG},
255          {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
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          {"-noautowrite",0,&eoptions,0x8000|EO_NOAUTOWRITE},
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},
264          {0,0,0,0}
265         };
266
267         memset(docheckie,0,sizeof(docheckie));
268         ret=ParseArguments(argc, argv, FCEUArgs);
269         if(cpalette)
270         {
271          if(cpalette[0]=='0')
272           if(cpalette[1]==0)
273           {
274            free(cpalette);
275            cpalette=0;
276           }
277         }
278         if(docheckie[0])
279          Settings.region_force=2;
280         FCEUI_SetGameGenie(eoptions&EO_GG);
281         FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);
282         FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
283
284         for(x=0;x<2;x++)
285         {
286          if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
287          if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
288         }
289
290         FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
291         FCEUI_SetSoundVolume(80);
292         DoDriverArgs();
293
294         if(fcexp)
295         {
296          int y;
297          for(y=0;y<5;y++)
298          {
299           if(!strncmp(fccortab[y],fcexp,8))
300           {
301            UsrInputTypeFC=fccortabi[y];
302            break;
303           }
304          }
305          free(fcexp);
306         }
307         for(x=0;x<2;x++)
308         {
309          int y;
310
311          if(!inputa[x])
312           continue;
313
314          for(y=0;y<5;y++)
315          {
316           if(!strncmp(cortab[y],inputa[x],8))
317           {
318            UsrInputType[x]=cortabi[y];
319            if(y==3)
320            {
321             powerpadside&=~(1<<x);
322             powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
323            }
324            free(inputa[x]);
325           }
326          }
327         }
328         return ret;
329 }
330
331
332 #include "usage.h"
333
334 int CLImain(int argc, char *argv[])
335 {
336         int last_arg_parsed;
337         /* TODO if(argc<=1)
338         {
339          ShowUsage(argv[0]);
340          return 1;
341         }*/
342
343         if(!DriverInitialize())
344         {
345          return 1;
346         }
347
348         if(!FCEUI_Initialize())
349          return(1);
350         GetBaseDirectory(BaseDirectory);
351         FCEUI_SetBaseDirectory(BaseDirectory);
352         lastLoadedGameName[0] = 0;
353
354         CreateDirs();
355         LoadConfig(NULL);
356         last_arg_parsed=DoArgs(argc-1,&argv[1]);
357         gp2x_opt_setup();
358         LoadLLGN();
359         FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
360         if(cpalette)
361          LoadCPalette();
362         if(InitSound())
363          inited|=1;
364
365         if (argc > 1 && !last_arg_parsed)
366         {
367          strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
368          lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
369          Exit = 0;
370         }
371         else
372         {
373          Exit = 1;
374         }
375
376         while (1)
377         {
378          if(!Exit)
379          {
380           if (fceugi)
381            CloseGame();
382           fceugi=FCEUI_LoadGame(lastLoadedGameName);
383           if (fceugi)
384           {
385            char infostring[32];
386            LoadConfig(lastLoadedGameName);
387            if (Settings.region_force)
388             FCEUI_SetVidSystem(Settings.region_force - 1);
389            ParseGI(fceugi);
390            InitOtherInput();
391
392            if ((eoptions&EO_GG) && geniestage == 0) {
393             strcpy(infostring, "gg.rom is missing, GG disabled");
394             eoptions&=~EO_GG;
395             FCEUI_SetGameGenie(0);
396            } else
397             GameInterface(GI_INFOSTRING, infostring);
398            FCEU_DispMessage("%s", infostring);
399           }
400           else
401           {
402            switch(LoadGameLastError) {
403             default: strcpy(menuErrorMsg, "failed to load ROM"); break;
404             case  2: strcpy(menuErrorMsg, "Can't find a ROM for ips/movie"); break;
405             case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
406             case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
407            }
408           }
409          }
410          if(Exit || !fceugi)
411          {
412           int ret;
413           ret = gp2x_menu_do();
414           if (ret == 1) break;          // exit emu
415           if (ret == 2) {               // reload ROM
416            Exit = 0;
417            continue;
418           }
419          }
420
421          gp2x_opt_update();
422          PrepareOtherInput();
423          FCEUI_GetCurrentVidSystem(&srendline,&erendline);
424          gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
425          switch (Settings.scaling & 3) {
426                  case 0: gp2x_video_set_offs(0);  gp2x_video_RGB_setscaling(320, 240); break;
427                  case 1: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(256, 240); break;
428                  case 2: gp2x_video_set_offs(32+srendline*320); gp2x_video_RGB_setscaling(256, erendline-srendline); break;
429                  case 3: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(320, 240); break;
430          }
431          CleanSurface();
432          gp2x_start_sound(Settings.sound_rate, 16, 0);
433          RefreshThrottleFPS();
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_DFL);
458
459  if(cpalette) free(cpalette);
460  cpalette=0;
461
462  if(inited&4)
463   KillVideo();
464  if(inited&1)
465   KillSound();
466  inited=0;
467 }
468
469 void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
470 {
471  FCEUD_UpdateInput();   // must update input before blitting because of save confirmation stuff
472  BlitPrepare(xbuf == NULL);
473  if(!(eoptions&EO_NOTHROTTLE))
474  {
475   if(Count)
476    WriteSound(Buffer,Count);
477   SpeedThrottle();
478  }
479  BlitScreen(xbuf == NULL);
480  // make sure last frame won't get skipped, because we need it for menu bg
481  if (Exit) FSkip=0;
482 }
483