ips patches, 0.4 r162 rel?
[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
7b356ee3 52extern int FSkip;
b2b95d2e 53void CloseGame(void);
54
55FCEUGI *fceugi = NULL;
21afaa36 56int ntsccol=0,ntschue=-1,ntsctint=-1;
35868d35 57int soundvol=70;
58int inited=0;
35868d35 59
21afaa36 60int srendlinev[2]={8,0};
61int erendlinev[2]={231,239};
35868d35 62int srendline,erendline;
63
64
65static char BaseDirectory[2048];
66
67int eoptions=0;
68
69static void DriverKill(void);
70static int DriverInitialize(void);
71
72static int gametype;
73#include "input.c"
74
75static 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;
35868d35 89}
90
91void FCEUD_PrintError(char *s)
92{
93 puts(s);
94}
95
6f6bc6fa 96char *cpalette=0;
97void LoadCPalette(void)
35868d35 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);
6f6bc6fa 105 free(cpalette);
106 cpalette=0;
35868d35 107 return;
108 }
109 fread(tmpp,1,192,fp);
110 FCEUI_SetPaletteArray((uint8 *)tmpp);
111 fclose(fp);
112}
113
114static 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
b547bda7 131void SaveConfig(const char *name)
35868d35 132{
133 char tdir[2048];
b547bda7 134 if (name)
135 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
136 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
21afaa36 137 FCEUI_GetNTSCTH(&ntsctint, &ntschue);
35868d35 138 SaveFCEUConfig(tdir,fceuconfig);
139}
140
b547bda7 141static void LoadConfig(const char *name)
35868d35 142{
143 char tdir[2048];
b547bda7 144 if (name)
145 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
146 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
21afaa36 147 FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
35868d35 148 LoadFCEUConfig(tdir,fceuconfig);
21afaa36 149 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
35868d35 150}
151
989672f4 152static 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
167static void SaveLLGN(void)
168{
169 // save last loaded game name
7b356ee3 170 if (lastLoadedGameName[0] && !(eoptions&EO_NOAUTOWRITE))
989672f4 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
35868d35 186static void CreateDirs(void)
187{
6f6bc6fa 188 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
35868d35 189 char tdir[2048];
190 int x;
191
192 mkdir(BaseDirectory,S_IRWXU);
b547bda7 193 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
35868d35 194 {
195 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
196 mkdir(tdir,S_IRWXU);
197 }
198}
199
200static 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
208static 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;
35868d35 219 #if(SIGBUS!=SIGSEGV)
220 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
221 #endif
35868d35 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
b2b95d2e 231static int DoArgs(int argc, char *argv[])
35868d35 232{
233 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
234 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
d97315ac 235 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
35868d35 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
b2b95d2e 240 int x, ret;
35868d35 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
9b451455 254 {"-gg",0,&eoptions,0x8000|EO_GG},
6f6bc6fa 255 {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
35868d35 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},
7b356ee3 260 {"-noautowrite",0,&eoptions,0x8000|EO_NOAUTOWRITE},
35868d35 261 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
262 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
35868d35 263 {0,(void *)DriverArgs,0,0},
264 {0,0,0,0}
265 };
266
267 memset(docheckie,0,sizeof(docheckie));
b2b95d2e 268 ret=ParseArguments(argc, argv, FCEUArgs);
35868d35 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])
989672f4 279 Settings.region_force=2;
9b451455 280 FCEUI_SetGameGenie(eoptions&EO_GG);
6f6bc6fa 281 FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);
35868d35 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
35868d35 290 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
7b356ee3 291 FCEUI_SetSoundVolume(80);
35868d35 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 }
b2b95d2e 328 return ret;
35868d35 329}
330
b547bda7 331
35868d35 332#include "usage.h"
333
334int CLImain(int argc, char *argv[])
335{
b2b95d2e 336 int last_arg_parsed;
337 /* TODO if(argc<=1)
937bf65b 338 {
339 ShowUsage(argv[0]);
340 return 1;
b2b95d2e 341 }*/
937bf65b 342
343 if(!DriverInitialize())
344 {
345 return 1;
346 }
347
b2b95d2e 348 if(!FCEUI_Initialize())
35868d35 349 return(1);
350 GetBaseDirectory(BaseDirectory);
351 FCEUI_SetBaseDirectory(BaseDirectory);
b2b95d2e 352 lastLoadedGameName[0] = 0;
35868d35 353
354 CreateDirs();
b547bda7 355 LoadConfig(NULL);
b2b95d2e 356 last_arg_parsed=DoArgs(argc-1,&argv[1]);
b547bda7 357 gp2x_opt_setup();
989672f4 358 LoadLLGN();
21afaa36 359 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
35868d35 360 if(cpalette)
6f6bc6fa 361 LoadCPalette();
35868d35 362 if(InitSound())
363 inited|=1;
364
b2b95d2e 365 if (argc > 1 && !last_arg_parsed)
6587f346 366 {
b2b95d2e 367 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
368 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
369 Exit = 0;
370 }
371 else
372 {
b2b95d2e 373 Exit = 1;
6587f346 374 }
375
b2b95d2e 376 while (1)
377 {
378 if(!Exit)
379 {
380 if (fceugi)
381 CloseGame();
382 fceugi=FCEUI_LoadGame(lastLoadedGameName);
383 if (fceugi)
384 {
c4980f9e 385 char infostring[32];
b547bda7 386 LoadConfig(lastLoadedGameName);
989672f4 387 if (Settings.region_force)
388 FCEUI_SetVidSystem(Settings.region_force - 1);
b2b95d2e 389 ParseGI(fceugi);
b2b95d2e 390 InitOtherInput();
391
9b451455 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);
c4980f9e 398 FCEU_DispMessage("%s", infostring);
b2b95d2e 399 }
400 else
989672f4 401 {
402 switch(LoadGameLastError) {
403 default: strcpy(menuErrorMsg, "failed to load ROM"); break;
892b1f6c 404 case 2: strcpy(menuErrorMsg, "Can't find a ROM for ips/movie"); break;
989672f4 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 }
b2b95d2e 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
7b356ee3 421 gp2x_opt_update();
971a1d07 422 PrepareOtherInput();
21afaa36 423 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
b2b95d2e 424 gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
989672f4 425 switch (Settings.scaling & 3) {
7b356ee3 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;
989672f4 430 }
21afaa36 431 CleanSurface();
989672f4 432 gp2x_start_sound(Settings.sound_rate, 16, 0);
7b356ee3 433 RefreshThrottleFPS();
b2b95d2e 434 FCEUI_Emulate();
435 }
35868d35 436
b547bda7 437 if (fceugi)
438 CloseGame();
439
989672f4 440 SaveLLGN();
35868d35 441 DriverKill();
b2b95d2e 442 return 0;
35868d35 443}
444
445static int DriverInitialize(void)
446{
447 SetSignals((void *)CloseStuff);
448
35868d35 449 if(!InitVideo()) return 0;
450 inited|=4;
35868d35 451 return 1;
452}
453
454static void DriverKill(void)
455{
989672f4 456 // SaveConfig(NULL); // done explicitly in menu now
cc981841 457 SetSignals(SIG_DFL);
35868d35 458
6f6bc6fa 459 if(cpalette) free(cpalette);
460 cpalette=0;
461
35868d35 462 if(inited&4)
463 KillVideo();
464 if(inited&1)
465 KillSound();
35868d35 466 inited=0;
467}
468
937bf65b 469void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
35868d35 470{
7b356ee3 471 FCEUD_UpdateInput(); // must update input before blitting because of save confirmation stuff
472 BlitPrepare(xbuf == NULL);
0bb3fe12 473 if(!(eoptions&EO_NOTHROTTLE))
474 {
475 if(Count)
476 WriteSound(Buffer,Count);
35868d35 477 SpeedThrottle();
0bb3fe12 478 }
7b356ee3 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;
35868d35 482}
483