minor adjustments
[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"
42
35868d35 43#include "dface.h"
44
21afaa36 45
21afaa36 46void CleanSurface(void);
6587f346 47
b2b95d2e 48// internals
49extern char lastLoadedGameName[2048];
50extern uint8 Exit; // exit emu loop flag
51void CloseGame(void);
52
53FCEUGI *fceugi = NULL;
21afaa36 54int ntsccol=0,ntschue=-1,ntsctint=-1;
35868d35 55int soundvol=70;
56int inited=0;
35868d35 57
21afaa36 58int srendlinev[2]={8,0};
59int erendlinev[2]={231,239};
35868d35 60int srendline,erendline;
61
62
63static char BaseDirectory[2048];
64
65int eoptions=0;
66
67static void DriverKill(void);
68static int DriverInitialize(void);
69
70static int gametype;
71#include "input.c"
72
73static void ParseGI(FCEUGI *gi)
74{
75 gametype=gi->type;
76
77 InputType[0]=UsrInputType[0];
78 InputType[1]=UsrInputType[1];
79 InputTypeFC=UsrInputTypeFC;
80
81 if(gi->input[0]>=0)
82 InputType[0]=gi->input[0];
83 if(gi->input[1]>=0)
84 InputType[1]=gi->input[1];
85 if(gi->inputfc>=0)
86 InputTypeFC=gi->inputfc;
35868d35 87}
88
89void FCEUD_PrintError(char *s)
90{
91 puts(s);
92}
93
6f6bc6fa 94char *cpalette=0;
95void LoadCPalette(void)
35868d35 96{
97 char tmpp[192];
98 FILE *fp;
99
100 if(!(fp=fopen(cpalette,"rb")))
101 {
102 printf(" Error loading custom palette from file: %s\n",cpalette);
6f6bc6fa 103 free(cpalette);
104 cpalette=0;
35868d35 105 return;
106 }
107 fread(tmpp,1,192,fp);
108 FCEUI_SetPaletteArray((uint8 *)tmpp);
109 fclose(fp);
110}
111
112static 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
b547bda7 129void SaveConfig(const char *name)
35868d35 130{
131 char tdir[2048];
b547bda7 132 if (name)
133 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
134 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
21afaa36 135 FCEUI_GetNTSCTH(&ntsctint, &ntschue);
35868d35 136 SaveFCEUConfig(tdir,fceuconfig);
137}
138
b547bda7 139static void LoadConfig(const char *name)
35868d35 140{
141 char tdir[2048];
b547bda7 142 if (name)
143 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
144 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
21afaa36 145 FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
35868d35 146 LoadFCEUConfig(tdir,fceuconfig);
21afaa36 147 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
35868d35 148}
149
989672f4 150static 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
165static 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
35868d35 184static void CreateDirs(void)
185{
6f6bc6fa 186 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
35868d35 187 char tdir[2048];
188 int x;
189
190 mkdir(BaseDirectory,S_IRWXU);
b547bda7 191 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
35868d35 192 {
193 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
194 mkdir(tdir,S_IRWXU);
195 }
196}
197
198static 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
206static 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;
35868d35 217 #if(SIGBUS!=SIGSEGV)
218 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
219 #endif
35868d35 220 case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break;
221 case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
222 case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
223 case SIGUSR1:
224 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
225 }
226 exit(1);
227}
228
b2b95d2e 229static int DoArgs(int argc, char *argv[])
35868d35 230{
231 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
232 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
d97315ac 233 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
35868d35 234 static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
235 static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
236 SIFC_4PLAYER,SIFC_FKB};
237
b2b95d2e 238 int x, ret;
35868d35 239 static char *inputa[2]={0,0};
240 static char *fcexp=0;
241 static int docheckie[4];
242
243 static ARGPSTRUCT FCEUArgs[]={
244 {"-soundvol",0,&soundvol,0},
245 {"-cpalette",0,&cpalette,0x4001},
246
247 {"-ntsccol",0,&ntsccol,0},
248 {"-pal",&docheckie[0],0,0},
249 {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
250 {"-fcexp",0,&fcexp,0x4001},
251
252 {"-gg",&docheckie[1],0,0},
6f6bc6fa 253 {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
35868d35 254 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
255 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
256 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
257 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
258 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
259 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
35868d35 260 {0,(void *)DriverArgs,0,0},
261 {0,0,0,0}
262 };
263
264 memset(docheckie,0,sizeof(docheckie));
b2b95d2e 265 ret=ParseArguments(argc, argv, FCEUArgs);
35868d35 266 if(cpalette)
267 {
268 if(cpalette[0]=='0')
269 if(cpalette[1]==0)
270 {
271 free(cpalette);
272 cpalette=0;
273 }
274 }
275 if(docheckie[0])
989672f4 276 Settings.region_force=2;
35868d35 277 if(docheckie[1])
278 FCEUI_SetGameGenie(1);
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
c4980f9e 391 GameInterface(GI_INFOSTRING, infostring);
392 FCEU_DispMessage("%s", infostring);
b2b95d2e 393 }
394 else
989672f4 395 {
396 switch(LoadGameLastError) {
397 default: strcpy(menuErrorMsg, "failed to load ROM"); break;
398 case 2: strcpy(menuErrorMsg, "Can't find a ROM for movie"); break;
399 case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
400 case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
401 }
402 }
b2b95d2e 403 }
404 if(Exit || !fceugi)
405 {
406 int ret;
407 ret = gp2x_menu_do();
408 if (ret == 1) break; // exit emu
409 if (ret == 2) { // reload ROM
410 Exit = 0;
411 continue;
412 }
413 }
414
971a1d07 415 PrepareOtherInput();
2a6855a3 416 RefreshThrottleFPS();
21afaa36 417 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
b2b95d2e 418 gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
989672f4 419 switch (Settings.scaling & 3) {
21afaa36 420 case 0: gp2x_video_set_offs(0); gp2x_video_RGB_setscaling(0, 320, 240); break;
421 case 1: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(0, 256, 240); break;
422 case 2: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(srendline, 256, erendline-srendline); break;
423 case 3: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(0, 320, 240); break;
989672f4 424 }
21afaa36 425 CleanSurface();
989672f4 426 gp2x_start_sound(Settings.sound_rate, 16, 0);
b2b95d2e 427 FCEUI_Emulate();
428 }
35868d35 429
b547bda7 430 if (fceugi)
431 CloseGame();
432
989672f4 433 SaveLLGN();
35868d35 434 DriverKill();
b2b95d2e 435 return 0;
35868d35 436}
437
438static int DriverInitialize(void)
439{
440 SetSignals((void *)CloseStuff);
441
35868d35 442 if(!InitVideo()) return 0;
443 inited|=4;
35868d35 444 return 1;
445}
446
447static void DriverKill(void)
448{
989672f4 449 // SaveConfig(NULL); // done explicitly in menu now
35868d35 450 SetSignals(SIG_IGN);
451
6f6bc6fa 452 if(cpalette) free(cpalette);
453 cpalette=0;
454
35868d35 455 if(inited&4)
456 KillVideo();
457 if(inited&1)
458 KillSound();
35868d35 459 inited=0;
460}
461
937bf65b 462void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
35868d35 463{
971a1d07 464 if(!Count && !(eoptions&EO_NOTHROTTLE))
35868d35 465 SpeedThrottle();
937bf65b 466 BlitScreen(xbuf);
971a1d07 467 if(Count && !(eoptions&EO_NOTHROTTLE))
937bf65b 468 WriteSound(Buffer,Count);
35868d35 469 FCEUD_UpdateInput();
470}
471