/** VGB: portable GameBoy emulator ***************************/
/**                                                         **/
/**                           VGB.c                         **/
/**                                                         **/
/** This file contains generic main() procedure statrting   **/
/** the emulation.                                          **/
/**                                                         **/
/** Copyright (C) Marat Fayzullin 1995                      **/
/**     You are not allowed to distribute this software     **/
/**     commercially. Please, notify me, if you make any    **/
/**     changes to this file.                               **/
/*************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

#include "GB.h"
#include "Help.h"

char *Options[]=
{
  "verbose","vperiod","uperiod","help","cheat",
  "delay","nodelay","crc","nocrc","autoa","noautoa",
  "autob","noautob","logsnd","trap","shm","noshm",
  "saver","nosaver","color0","color1","color2","color3",
  "bcolor0","bcolor1","bcolor2","bcolor3","scolor0",
  "scolor1","scolor2","scolor3","wcolor0","wcolor1",
  "wcolor2","wcolor3",
  NULL
};

extern char *Title;
extern int   UseSHM;
extern int   SaveCPU;
extern char *ColorNames[];

main(int argc,char *argv[])
{
  char *P;
  int N,J,I;

  TrapBadOps=1;LineDelay=0;CheckCRC=1; 
  VPeriod=69905;UPeriod=2;IntSync=1;
  Verbose=5;P="CART.GB";SndName=NULL;
 
  for(N=1,I=0;N<argc;N++)
    if(*argv[N]!='-')
      if(I++) printf("%s: Excessive filename '%s'\n",argv[0],argv[N]);
      else    P=argv[N];
    else
    {    
      for(J=0;Options[J];J++)
        if(!strcmp(argv[N]+1,Options[J])) break;
      switch(J)
      {
        case 0:  N++;
                 if(N<argc)
                 { Verbose=atoi(argv[N]);TrapBadOps=Verbose&0x04; }
                 else printf("%s: No verbose level supplied\n",argv[0]);
                 break;
        case 1:  N++;
                 if(N>=argc)
                   printf("%s: No VBlank period supplied\n",argv[0]);
                 else
                 {
                   IntSync=1;VPeriod=atoi(argv[N]);
                   if((VPeriod<10000)||(VPeriod>199999)) VPeriod=69905;
                 }
                 break;
        case 2:  N++;
                 if(N>=argc)
                   printf("%s: No screen update period supplied\n",argv[0]);
                 else
                 {
                   UPeriod=atoi(argv[N]);
                   if((UPeriod<1)||(UPeriod>20)) UPeriod=2;
                 }
                 break;
        case 3:  printf("%s by Marat Fayzullin    (C)FMS 1995,1996\n",Title);
                 for(J=0;HelpText[J];J++) puts(HelpText[J]);
                 return(0);
        case 4:  N++;
                 if(N>=argc)
                   printf("%s: No cheat data supplied\n",argv[0]);
                 else
                   if(!AddCheat(argv[N]))
                     printf("%s: Wrong cheat data supplied\n",argv[0]);
                 break;
        case 5:  LineDelay=1;break;
        case 6:  LineDelay=0;break;
        case 7:  CheckCRC=1;break;
        case 8:  CheckCRC=0;break;
        case 9:  AutoA=1;break;
        case 10: AutoA=0;break;
        case 11: AutoB=1;break;
        case 12: AutoB=0;break;
        case 13: N++;
                 if(N<argc) SndName=argv[N];
                 else printf("%s: No file for soundtrack logging\n",argv[0]);
                 break;
#ifdef DEBUG
        case 14: N++;
                 if(N>=argc)
                   printf("%s: No trap address supplied\n",argv[0]);
                 else
                   if(!strcmp(argv[N],"now")) Trace=1;
                   else sscanf(argv[N],"%hX",&Trap);
                 break;
#endif
#ifdef MITSHM
        case 15: UseSHM=1;break;
        case 16: UseSHM=0;break;
#endif
#ifdef UNIX
        case 17: SaveCPU=1;break;
        case 18: SaveCPU=0;break;
        case 19: case 20: case 21: case 22:
        case 23: case 24: case 25: case 26:
        case 27: case 28: case 29: case 30:
        case 31: case 32: case 33: case 34:
                 N++;J-=19;
                 if(N<argc)
                   if(J>3) ColorNames[J-4]=argv[N];
                   else ColorNames[J+4]=ColorNames[J+8]=ColorNames[J]=argv[N];
                 else printf("%s: No color name supplied\n",argv[0]);
                 break;
#endif
        default: printf("%s: Wrong option '%s'\n",argv[0],argv[N]);
      }
    }

  if(!InitMachine()) return(1);
  StartGB(P);TrashGB();
  TrashMachine();
  return(0);
}
