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