release r2, update credits
[fceu.git] / drivers / common / 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
4a2a617a 30#include "main.h"
35868d35 31#include "throttle.h"
4a2a617a 32#include "config.h"
33#include "args.h"
34#include "unixdsp.h"
35#include "cheat.h"
36#include "dface.h"
37#include "platform.h"
b2b95d2e 38#include "menu.h"
4a2a617a 39#include "settings.h"
e4434f79 40#include "../libpicofe/config_file.h"
35868d35 41
21afaa36 42#include "../../fce.h"
9b451455 43#include "../../cart.h"
21afaa36 44
35868d35 45
4a2a617a 46// these are now here to try to make compatible configs
47// between different versions of the emu
48DSETTINGS Settings;
49CFGSTRUCT DriverConfig[]={
50 AC(Settings.turbo_rate_add),
51 AC(Settings.sound_rate),
52 AC(Settings.showfps),
53 AC(Settings.scaling),
54 AC(Settings.frameskip),
55 AC(Settings.sstate_confirm),
56 AC(Settings.region_force),
57 AC(Settings.cpuclock),
58 AC(Settings.mmuhack),
59 AC(Settings.ramtimings),
60 AC(Settings.gamma),
61 AC(Settings.perfect_vsync),
62 AC(Settings.accurate_mode),
88e59df3 63 AC(Settings.sw_filter),
e4434f79 64 AC(Settings.hw_filter),
4a2a617a 65 ENDCFGSTRUCT
66};
21afaa36 67
21afaa36 68void CleanSurface(void);
6587f346 69
b2b95d2e 70// internals
b2b95d2e 71extern uint8 Exit; // exit emu loop flag
7b356ee3 72extern int FSkip;
b2b95d2e 73void CloseGame(void);
74
75FCEUGI *fceugi = NULL;
21afaa36 76int ntsccol=0,ntschue=-1,ntsctint=-1;
35868d35 77int soundvol=70;
78int inited=0;
35868d35 79
21afaa36 80int srendlinev[2]={8,0};
81int erendlinev[2]={231,239};
35868d35 82int srendline,erendline;
83
84
85static char BaseDirectory[2048];
86
87int eoptions=0;
88
89static void DriverKill(void);
90static int DriverInitialize(void);
91
92static int gametype;
93#include "input.c"
94
95static void ParseGI(FCEUGI *gi)
96{
97 gametype=gi->type;
98
99 InputType[0]=UsrInputType[0];
100 InputType[1]=UsrInputType[1];
101 InputTypeFC=UsrInputTypeFC;
102
103 if(gi->input[0]>=0)
104 InputType[0]=gi->input[0];
105 if(gi->input[1]>=0)
106 InputType[1]=gi->input[1];
107 if(gi->inputfc>=0)
108 InputTypeFC=gi->inputfc;
35868d35 109}
110
111void FCEUD_PrintError(char *s)
112{
113 puts(s);
114}
115
6f6bc6fa 116char *cpalette=0;
117void LoadCPalette(void)
35868d35 118{
119 char tmpp[192];
120 FILE *fp;
121
122 if(!(fp=fopen(cpalette,"rb")))
123 {
124 printf(" Error loading custom palette from file: %s\n",cpalette);
6f6bc6fa 125 free(cpalette);
126 cpalette=0;
35868d35 127 return;
128 }
129 fread(tmpp,1,192,fp);
130 FCEUI_SetPaletteArray((uint8 *)tmpp);
131 fclose(fp);
132}
133
134static CFGSTRUCT fceuconfig[]={
135 AC(soundvol),
136 ACS(cpalette),
137 AC(ntsctint),
138 AC(ntschue),
139 AC(ntsccol),
140 AC(UsrInputTypeFC),
141 ACA(UsrInputType),
142 AC(powerpadside),
143 AC(powerpadsc),
144 AC(eoptions),
145 ACA(srendlinev),
146 ACA(erendlinev),
147 ADDCFGSTRUCT(DriverConfig),
148 ENDCFGSTRUCT
149};
150
642070a9 151static const char *skip_path(const char *path)
35868d35 152{
642070a9 153 const char *p;
154 if (path == NULL) return NULL;
155 for (p = path+strlen(path)-1; p > path && *p != '/'; p--);
156 if (*p == '/') p++;
157 return p;
158}
159
160int SaveConfig(const char *llgn_path)
161{
162 const char *name = skip_path(llgn_path);
35868d35 163 char tdir[2048];
e4434f79 164 FILE *f;
642070a9 165 int ret;
e4434f79 166
b547bda7 167 if (name)
168 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
169 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
642070a9 170 printf("saving cfg to %s ... ", tdir); fflush(stdout);
21afaa36 171 FCEUI_GetNTSCTH(&ntsctint, &ntschue);
642070a9 172 ret=SaveFCEUConfig(tdir,fceuconfig);
173 printf(ret == 0 ? "done\n" : "failed\n");
e4434f79 174
175 // keys
176 if (name)
177 sprintf(tdir,"%s"PSS"cfg"PSS"%s_keys.cfg",BaseDirectory,name);
178 else sprintf(tdir,"%s"PSS"fceu_keys.cfg",BaseDirectory);
179 f=fopen(tdir, "w");
180 if (f!=NULL) {
181 config_write_keys(f);
182 fclose(f);
183 }
184
642070a9 185 return ret;
35868d35 186}
187
eec2623f 188static void LoadKeys(const char *llgn_path)
35868d35 189{
642070a9 190 const char *name = skip_path(llgn_path);
35868d35 191 char tdir[2048];
e4434f79 192 FILE *f;
eec2623f 193 int l;
e4434f79 194
e4434f79 195 if (name)
196 sprintf(tdir,"%s"PSS"cfg"PSS"%s_keys.cfg",BaseDirectory,name);
197 else sprintf(tdir,"%s"PSS"fceu_keys.cfg",BaseDirectory);
198 f=fopen(tdir, "r");
199 if (f!=NULL) {
200 l=fread(tdir,1,sizeof(tdir)-1,f);
201 tdir[l]=0;
202 config_read_keys(tdir);
203 fclose(f);
204 }
eec2623f 205}
206
207static int LoadConfig(const char *llgn_path)
208{
209 const char *name = skip_path(llgn_path);
210 char tdir[2048];
211 int ret;
212
213 if (name)
214 sprintf(tdir,"%s"PSS"cfg"PSS"%s.cfg",BaseDirectory,name);
215 else sprintf(tdir,"%s"PSS"fceu2.cfg",BaseDirectory);
216 printf("loading cfg from %s ... ", tdir); fflush(stdout);
217 FCEUI_GetNTSCTH(&ntsctint, &ntschue); /* Get default settings for if no config file exists. */
218 ret=LoadFCEUConfig(tdir,fceuconfig);
219 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
220 printf(ret == 0 ? "done\n" : "failed\n");
221
222 LoadKeys(llgn_path);
e4434f79 223
642070a9 224 return ret;
35868d35 225}
226
989672f4 227static void LoadLLGN(void)
228{
229 char tdir[2048];
230 FILE *f;
231 int len;
232 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
233 f=fopen(tdir, "r");
234 if(f)
235 {
236 len = fread(lastLoadedGameName, 1, sizeof(lastLoadedGameName)-1, f);
237 lastLoadedGameName[len] = 0;
238 fclose(f);
239 }
158d88ba 240 else
241 {
242 platform_get_def_rompath(lastLoadedGameName, sizeof(lastLoadedGameName));
243 }
989672f4 244}
245
246static void SaveLLGN(void)
247{
248 // save last loaded game name
7b356ee3 249 if (lastLoadedGameName[0] && !(eoptions&EO_NOAUTOWRITE))
989672f4 250 {
251 char tdir[2048];
252 FILE *f;
253 sprintf(tdir,"%s"PSS"last_rom.txt",BaseDirectory);
254 f=fopen(tdir, "w");
255 if(f)
256 {
257 fwrite(lastLoadedGameName, 1, strlen(lastLoadedGameName), f);
258 fclose(f);
259 sync();
260 }
261 }
262}
263
264
35868d35 265static void CreateDirs(void)
266{
6f6bc6fa 267 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg","pal"};
35868d35 268 char tdir[2048];
269 int x;
270
271 mkdir(BaseDirectory,S_IRWXU);
b547bda7 272 for(x=0;x<sizeof(subs)/sizeof(subs[0]);x++)
35868d35 273 {
274 sprintf(tdir,"%s"PSS"%s",BaseDirectory,subs[x]);
275 mkdir(tdir,S_IRWXU);
276 }
277}
278
279static void SetSignals(void (*t)(int))
280{
281 int sigs[11]={SIGINT,SIGTERM,SIGHUP,SIGPIPE,SIGSEGV,SIGFPE,SIGKILL,SIGALRM,SIGABRT,SIGUSR1,SIGUSR2};
282 int x;
283 for(x=0;x<11;x++)
284 signal(sigs[x],t);
285}
286
287static void CloseStuff(int signum)
288{
289 DriverKill();
290 printf("\nSignal %d has been caught and dealt with...\n",signum);
291 switch(signum)
292 {
293 case SIGINT:printf("How DARE you interrupt me!\n");break;
294 case SIGTERM:printf("MUST TERMINATE ALL HUMANS\n");break;
295 case SIGHUP:printf("Reach out and hang-up on someone.\n");break;
296 case SIGPIPE:printf("The pipe has broken! Better watch out for floods...\n");break;
297 case SIGSEGV:printf("Iyeeeeeeeee!!! A segmentation fault has occurred. Have a fluffy day.\n");break;
35868d35 298 #if(SIGBUS!=SIGSEGV)
299 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
300 #endif
35868d35 301 case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break;
302 case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
303 case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
304 case SIGUSR1:
305 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
306 }
307 exit(1);
308}
309
b2b95d2e 310static int DoArgs(int argc, char *argv[])
35868d35 311{
312 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
313 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
d97315ac 314 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
35868d35 315 static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
316 static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
317 SIFC_4PLAYER,SIFC_FKB};
318
b2b95d2e 319 int x, ret;
35868d35 320 static char *inputa[2]={0,0};
321 static char *fcexp=0;
322 static int docheckie[4];
4a2a617a 323#ifdef NETWORK
324 static int docheckie2[2]={0,0};
325#endif
35868d35 326
327 static ARGPSTRUCT FCEUArgs[]={
328 {"-soundvol",0,&soundvol,0},
329 {"-cpalette",0,&cpalette,0x4001},
330
331 {"-ntsccol",0,&ntsccol,0},
332 {"-pal",&docheckie[0],0,0},
333 {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
334 {"-fcexp",0,&fcexp,0x4001},
335
9b451455 336 {"-gg",0,&eoptions,0x8000|EO_GG},
6f6bc6fa 337 {"-no8lim",0,&eoptions,0x8000|EO_NO8LIM},
35868d35 338 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
339 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
340 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
341 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
7b356ee3 342 {"-noautowrite",0,&eoptions,0x8000|EO_NOAUTOWRITE},
35868d35 343 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
344 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
4a2a617a 345 {"-sound",0,&Settings.sound_rate,0},
346 {"-showfps",0,&Settings.showfps,0},
347 #ifdef NETWORK
348 {"-connect",&docheckie2[0],&netplayhost,0x4001},
349 {"-server",&docheckie2[1],0,0},
350 {"-netport",0,&Port,0},
351 #endif
352 {0,(void *)DriverArgs,0,0},
353 {0,0,0,0}
35868d35 354 };
355
356 memset(docheckie,0,sizeof(docheckie));
b2b95d2e 357 ret=ParseArguments(argc, argv, FCEUArgs);
35868d35 358 if(cpalette)
359 {
360 if(cpalette[0]=='0')
361 if(cpalette[1]==0)
362 {
363 free(cpalette);
364 cpalette=0;
365 }
366 }
367 if(docheckie[0])
989672f4 368 Settings.region_force=2;
9b451455 369 FCEUI_SetGameGenie(eoptions&EO_GG);
6f6bc6fa 370 FCEUI_DisableSpriteLimitation(eoptions&EO_NO8LIM);
35868d35 371 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
372
373 for(x=0;x<2;x++)
374 {
375 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
376 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
377 }
378
35868d35 379 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
7b356ee3 380 FCEUI_SetSoundVolume(80);
4a2a617a 381 #ifdef NETWORK
382 if(docheckie2[0])
383 netplay=2;
384 else if(docheckie2[1])
385 netplay=1;
386
387 if(netplay)
388 FCEUI_SetNetworkPlay(netplay);
389 #endif
35868d35 390 DoDriverArgs();
391
392 if(fcexp)
393 {
394 int y;
395 for(y=0;y<5;y++)
396 {
397 if(!strncmp(fccortab[y],fcexp,8))
398 {
399 UsrInputTypeFC=fccortabi[y];
400 break;
401 }
402 }
403 free(fcexp);
404 }
405 for(x=0;x<2;x++)
406 {
407 int y;
408
409 if(!inputa[x])
410 continue;
411
412 for(y=0;y<5;y++)
413 {
414 if(!strncmp(cortab[y],inputa[x],8))
415 {
416 UsrInputType[x]=cortabi[y];
417 if(y==3)
418 {
419 powerpadside&=~(1<<x);
420 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
421 }
422 free(inputa[x]);
423 }
424 }
425 }
b2b95d2e 426 return ret;
35868d35 427}
428
b547bda7 429
35868d35 430#include "usage.h"
431
4a2a617a 432int main(int argc, char *argv[])
35868d35 433{
642070a9 434 int last_arg_parsed, ret;
b2b95d2e 435 /* TODO if(argc<=1)
937bf65b 436 {
437 ShowUsage(argv[0]);
438 return 1;
b2b95d2e 439 }*/
937bf65b 440
4a2a617a 441 platform_init();
937bf65b 442 if(!DriverInitialize())
443 {
444 return 1;
445 }
446
b2b95d2e 447 if(!FCEUI_Initialize())
35868d35 448 return(1);
449 GetBaseDirectory(BaseDirectory);
450 FCEUI_SetBaseDirectory(BaseDirectory);
b2b95d2e 451 lastLoadedGameName[0] = 0;
4a2a617a 452 menu_init();
453 in_init();
35868d35 454
455 CreateDirs();
eec2623f 456 LoadConfig(NULL);
457 last_arg_parsed=DoArgs(argc-1,&argv[1]);
4a2a617a 458 platform_late_init();
eec2623f 459 LoadKeys(NULL);
4a2a617a 460
989672f4 461 LoadLLGN();
21afaa36 462 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
35868d35 463 if(cpalette)
6f6bc6fa 464 LoadCPalette();
35868d35 465 if(InitSound())
466 inited|=1;
467
b2b95d2e 468 if (argc > 1 && !last_arg_parsed)
6587f346 469 {
b2b95d2e 470 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
471 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
472 Exit = 0;
473 }
474 else
475 {
b2b95d2e 476 Exit = 1;
6587f346 477 }
478
b2b95d2e 479 while (1)
480 {
481 if(!Exit)
482 {
483 if (fceugi)
484 CloseGame();
642070a9 485 ret=LoadConfig(lastLoadedGameName);
486 if (ret != 0)
487 {
488 LoadConfig(NULL);
489 }
6244011f 490 FCEUI_SetEmuMode(Settings.accurate_mode);
b2b95d2e 491 fceugi=FCEUI_LoadGame(lastLoadedGameName);
492 if (fceugi)
493 {
c4980f9e 494 char infostring[32];
989672f4 495 if (Settings.region_force)
496 FCEUI_SetVidSystem(Settings.region_force - 1);
b2b95d2e 497 ParseGI(fceugi);
b2b95d2e 498 InitOtherInput();
499
9b451455 500 if ((eoptions&EO_GG) && geniestage == 0) {
501 strcpy(infostring, "gg.rom is missing, GG disabled");
502 eoptions&=~EO_GG;
503 FCEUI_SetGameGenie(0);
504 } else
505 GameInterface(GI_INFOSTRING, infostring);
c4980f9e 506 FCEU_DispMessage("%s", infostring);
b2b95d2e 507 }
508 else
989672f4 509 {
510 switch(LoadGameLastError) {
4a2a617a 511 default: menu_update_msg("failed to load ROM"); break;
512 case 2: menu_update_msg("Can't find a ROM for ips/movie"); break;
513 case 10: menu_update_msg("FDS BIOS ROM is missing, read docs"); break;
514 case 11: menu_update_msg("Error reading auxillary FDS file"); break;
989672f4 515 }
516 }
b2b95d2e 517 }
518 if(Exit || !fceugi)
519 {
520 int ret;
4a2a617a 521 ret = menu_loop();
b2b95d2e 522 if (ret == 1) break; // exit emu
523 if (ret == 2) { // reload ROM
e4434f79 524 SaveLLGN();
b2b95d2e 525 Exit = 0;
526 continue;
527 }
528 }
529
971a1d07 530 PrepareOtherInput();
21afaa36 531 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
4a2a617a 532 platform_apply_config();
21afaa36 533 CleanSurface();
4a2a617a 534 StartSound();
7b356ee3 535 RefreshThrottleFPS();
b2b95d2e 536 FCEUI_Emulate();
537 }
35868d35 538
b547bda7 539 if (fceugi)
540 CloseGame();
541
6244011f 542 FCEUI_Kill();
35868d35 543 DriverKill();
4a2a617a 544 platform_finish();
545 return 0;
35868d35 546}
547
548static int DriverInitialize(void)
549{
550 SetSignals((void *)CloseStuff);
551
35868d35 552 if(!InitVideo()) return 0;
553 inited|=4;
35868d35 554 return 1;
555}
556
557static void DriverKill(void)
558{
989672f4 559 // SaveConfig(NULL); // done explicitly in menu now
cc981841 560 SetSignals(SIG_DFL);
35868d35 561
6f6bc6fa 562 if(cpalette) free(cpalette);
563 cpalette=0;
564
35868d35 565 if(inited&4)
566 KillVideo();
567 if(inited&1)
568 KillSound();
35868d35 569 inited=0;
570}
571
937bf65b 572void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
35868d35 573{
7b356ee3 574 FCEUD_UpdateInput(); // must update input before blitting because of save confirmation stuff
575 BlitPrepare(xbuf == NULL);
0bb3fe12 576 if(!(eoptions&EO_NOTHROTTLE))
577 {
578 if(Count)
579 WriteSound(Buffer,Count);
35868d35 580 SpeedThrottle();
0bb3fe12 581 }
7b356ee3 582 BlitScreen(xbuf == NULL);
583 // make sure last frame won't get skipped, because we need it for menu bg
584 if (Exit) FSkip=0;
35868d35 585}
586
4a2a617a 587