FSkip behaviour changed, RefreshLine_PPU_hook optimized
[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
46// TODO! broken fs0, sram saves
47
48void CleanSurface(void);
6587f346 49
b2b95d2e 50// internals
51extern char lastLoadedGameName[2048];
52extern uint8 Exit; // exit emu loop flag
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
96static char *cpalette=0;
97static 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 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{
b547bda7 186 char *subs[]={"fcs","snaps","gameinfo","sav","cheats","cfg"};
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;
217 /* So much SIGBUS evil. */
218 #ifdef SIGBUS
219 #if(SIGBUS!=SIGSEGV)
220 case SIGBUS:printf("I told you to be nice to the driver.\n");break;
221 #endif
222 #endif
223 case SIGFPE:printf("Those darn floating points. Ne'er know when they'll bite!\n");break;
224 case SIGALRM:printf("Don't throw your clock at the meowing cats!\n");break;
225 case SIGABRT:printf("Abort, Retry, Ignore, Fail?\n");break;
226 case SIGUSR1:
227 case SIGUSR2:printf("Killing your processes is not nice.\n");break;
228 }
229 exit(1);
230}
231
b2b95d2e 232static int DoArgs(int argc, char *argv[])
35868d35 233{
234 static char *cortab[5]={"none","gamepad","zapper","powerpad","arkanoid"};
235 static int cortabi[5]={SI_NONE,SI_GAMEPAD,
d97315ac 236 SI_ZAPPER,SI_POWERPADA,SI_ARKANOID};
35868d35 237 static char *fccortab[5]={"none","arkanoid","shadow","4player","fkb"};
238 static int fccortabi[5]={SIFC_NONE,SIFC_ARKANOID,SIFC_SHADOW,
239 SIFC_4PLAYER,SIFC_FKB};
240
b2b95d2e 241 int x, ret;
35868d35 242 static char *inputa[2]={0,0};
243 static char *fcexp=0;
244 static int docheckie[4];
245
246 static ARGPSTRUCT FCEUArgs[]={
247 {"-soundvol",0,&soundvol,0},
248 {"-cpalette",0,&cpalette,0x4001},
249
250 {"-ntsccol",0,&ntsccol,0},
251 {"-pal",&docheckie[0],0,0},
252 {"-input1",0,&inputa[0],0x4001},{"-input2",0,&inputa[1],0x4001},
253 {"-fcexp",0,&fcexp,0x4001},
254
255 {"-gg",&docheckie[1],0,0},
256 {"-no8lim",0,&eoptions,0x8001},
257 {"-subase",0,&eoptions,0x8002},
258 {"-snapname",0,&eoptions,0x8000|EO_SNAPNAME},
259 {"-nofs",0,&eoptions,0x8000|EO_NOFOURSCORE},
260 {"-clipsides",0,&eoptions,0x8000|EO_CLIPSIDES},
261 {"-nothrottle",0,&eoptions,0x8000|EO_NOTHROTTLE},
262 {"-slstart",0,&srendlinev[0],0},{"-slend",0,&erendlinev[0],0},
263 {"-slstartp",0,&srendlinev[1],0},{"-slendp",0,&erendlinev[1],0},
35868d35 264 {0,(void *)DriverArgs,0,0},
265 {0,0,0,0}
266 };
267
268 memset(docheckie,0,sizeof(docheckie));
b2b95d2e 269 ret=ParseArguments(argc, argv, FCEUArgs);
35868d35 270 if(cpalette)
271 {
272 if(cpalette[0]=='0')
273 if(cpalette[1]==0)
274 {
275 free(cpalette);
276 cpalette=0;
277 }
278 }
279 if(docheckie[0])
989672f4 280 Settings.region_force=2;
35868d35 281 if(docheckie[1])
282 FCEUI_SetGameGenie(1);
283 FCEUI_DisableSpriteLimitation(1);
284 FCEUI_SaveExtraDataUnderBase(eoptions&2);
285 FCEUI_SetSnapName(eoptions&EO_SNAPNAME);
286
287 for(x=0;x<2;x++)
288 {
289 if(srendlinev[x]<0 || srendlinev[x]>239) srendlinev[x]=0;
290 if(erendlinev[x]<srendlinev[x] || erendlinev[x]>239) erendlinev[x]=239;
291 }
292
21afaa36 293 printf("FCEUI_SetRenderedLines: %d, %d, %d, %d\n",srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
294 printf("clip sides: %d\n", eoptions&EO_CLIPSIDES);
35868d35 295 FCEUI_SetRenderedLines(srendlinev[0],erendlinev[0],srendlinev[1],erendlinev[1]);
35868d35 296 FCEUI_SetSoundVolume(soundvol);
35868d35 297 DoDriverArgs();
298
299 if(fcexp)
300 {
301 int y;
302 for(y=0;y<5;y++)
303 {
304 if(!strncmp(fccortab[y],fcexp,8))
305 {
306 UsrInputTypeFC=fccortabi[y];
307 break;
308 }
309 }
310 free(fcexp);
311 }
312 for(x=0;x<2;x++)
313 {
314 int y;
315
316 if(!inputa[x])
317 continue;
318
319 for(y=0;y<5;y++)
320 {
321 if(!strncmp(cortab[y],inputa[x],8))
322 {
323 UsrInputType[x]=cortabi[y];
324 if(y==3)
325 {
326 powerpadside&=~(1<<x);
327 powerpadside|=((((inputa[x][8])-'a')&1)^1)<<x;
328 }
329 free(inputa[x]);
330 }
331 }
332 }
b2b95d2e 333 return ret;
35868d35 334}
335
b547bda7 336
35868d35 337#include "usage.h"
338
339int CLImain(int argc, char *argv[])
340{
b2b95d2e 341 int last_arg_parsed;
342 /* TODO if(argc<=1)
937bf65b 343 {
344 ShowUsage(argv[0]);
345 return 1;
b2b95d2e 346 }*/
937bf65b 347
348 if(!DriverInitialize())
349 {
350 return 1;
351 }
352
b2b95d2e 353 if(!FCEUI_Initialize())
35868d35 354 return(1);
355 GetBaseDirectory(BaseDirectory);
356 FCEUI_SetBaseDirectory(BaseDirectory);
b2b95d2e 357 lastLoadedGameName[0] = 0;
35868d35 358
359 CreateDirs();
b547bda7 360 LoadConfig(NULL);
b2b95d2e 361 last_arg_parsed=DoArgs(argc-1,&argv[1]);
b547bda7 362 gp2x_opt_setup();
989672f4 363 gp2x_cpuclock_gamma_update();
364 LoadLLGN();
21afaa36 365 FCEUI_SetNTSCTH(ntsccol, ntsctint, ntschue);
35868d35 366 if(cpalette)
21afaa36 367 LoadCPalette(); // TODO
35868d35 368 if(InitSound())
369 inited|=1;
370
b2b95d2e 371 if (argc > 1 && !last_arg_parsed)
6587f346 372 {
b2b95d2e 373 strncpy(lastLoadedGameName, argv[argc-1], sizeof(lastLoadedGameName));
374 lastLoadedGameName[sizeof(lastLoadedGameName)-1] = 0;
375 Exit = 0;
376 }
377 else
378 {
b2b95d2e 379 Exit = 1;
6587f346 380 }
381
b2b95d2e 382 while (1)
383 {
384 if(!Exit)
385 {
386 if (fceugi)
387 CloseGame();
388 fceugi=FCEUI_LoadGame(lastLoadedGameName);
389 if (fceugi)
390 {
c4980f9e 391 char infostring[32];
b547bda7 392 LoadConfig(lastLoadedGameName);
989672f4 393 if (Settings.region_force)
394 FCEUI_SetVidSystem(Settings.region_force - 1);
b2b95d2e 395 ParseGI(fceugi);
396 //RefreshThrottleFPS();
397 InitOtherInput();
398
c4980f9e 399 GameInterface(GI_INFOSTRING, infostring);
400 FCEU_DispMessage("%s", infostring);
b2b95d2e 401 }
402 else
989672f4 403 {
404 switch(LoadGameLastError) {
405 default: strcpy(menuErrorMsg, "failed to load ROM"); break;
406 case 2: strcpy(menuErrorMsg, "Can't find a ROM for movie"); break;
407 case 10: strcpy(menuErrorMsg, "FDS BIOS ROM is missing, read docs"); break;
408 case 11: strcpy(menuErrorMsg, "Error reading auxillary FDS file"); break;
409 }
410 }
b2b95d2e 411 }
412 if(Exit || !fceugi)
413 {
414 int ret;
415 ret = gp2x_menu_do();
416 if (ret == 1) break; // exit emu
417 if (ret == 2) { // reload ROM
418 Exit = 0;
419 continue;
420 }
421 }
422
971a1d07 423 PrepareOtherInput();
21afaa36 424 FCEUI_GetCurrentVidSystem(&srendline,&erendline);
b2b95d2e 425 gp2x_video_changemode(Settings.scaling == 3 ? 15 : 8);
989672f4 426 switch (Settings.scaling & 3) {
21afaa36 427 case 0: gp2x_video_set_offs(0); gp2x_video_RGB_setscaling(0, 320, 240); break;
428 case 1: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(0, 256, 240); break;
429 case 2: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(srendline, 256, erendline-srendline); break;
430 case 3: gp2x_video_set_offs(32); gp2x_video_RGB_setscaling(0, 320, 240); break;
989672f4 431 }
21afaa36 432 CleanSurface();
989672f4 433 gp2x_start_sound(Settings.sound_rate, 16, 0);
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
35868d35 457 SetSignals(SIG_IGN);
458
35868d35 459 if(inited&4)
460 KillVideo();
461 if(inited&1)
462 KillSound();
35868d35 463 inited=0;
464}
465
937bf65b 466void FCEUD_Update(uint8 *xbuf, int16 *Buffer, int Count)
35868d35 467{
971a1d07 468 if(!Count && !(eoptions&EO_NOTHROTTLE))
35868d35 469 SpeedThrottle();
937bf65b 470 BlitScreen(xbuf);
971a1d07 471 if(Count && !(eoptions&EO_NOTHROTTLE))
937bf65b 472 WriteSound(Buffer,Count);
35868d35 473 FCEUD_UpdateInput();
474}
475