/** 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"


extern char *Title;
//extern int   UseSHM;
extern int   SaveCPU;
extern word OldTrap;
BOOL ScaleUp;

main(void)
{
LONG ArgPtrs[6]={0};
struct RDArgs *Args;
STRPTR filename_buf;

  TrapBadOps=1;LineDelay=0; 
  VPeriod=10000;UPeriod=2;IntSync=1;
  Verbose=5;
 
	if(Args = ReadArgs("Verbose/K/N,VP=VPeriod/K/N,UP=UPeriod/K/N,Trap/K,Help/S,DS=DoubleSize/S",ArgPtrs,NULL))
	{
		if(OPT_VERBOSE) Verbose = (int)(*OPT_VERBOSE);
		if(OPT_VPER) VPeriod = (int)(*OPT_VPER);
		if(OPT_UPER) UPeriod = (int)(*OPT_UPER);
		if(OPT_TRAP) sscanf(OPT_TRAP,"%hx",&Trap);
		if(OPT_HELP) {puts(HelpText); FreeArgs(Args); exit(0);}
		ScaleUp = (OPT_SCALEUP);
		FreeArgs(Args);
		if((filename_buf = ReqFile()) == NULL) exit(0);

		if(!InitMachine()) return(1);
		OldTrap=Trap;
		StartGB(filename_buf);
		TrashGB();
		TrashMachine();

		FreeVec(filename_buf);
		return(0);
	}
}

STRPTR ReqFile(void)
{
struct FileRequester *AslReq;
STRPTR file=NULL;
WORD namelen;

	if(AslReq = (struct FileRequester*)AllocAslRequestTags(ASL_FileRequest,
			ASLFR_TitleText,"Select a cartridge!",
			ASLFR_PositiveText,"Play!",
			ASLFR_NegativeText,"Suxx!",
			ASLFR_RejectIcons,TRUE,
			TAG_END))
	{
		if(AslRequest(AslReq,NULL))
		{
			namelen = strlen(AslReq->fr_File) + strlen(AslReq->fr_Drawer) + 2;
			if(file = AllocVec(namelen,MEMF_ANY | MEMF_CLEAR))
			{
				strcpy(file,AslReq->fr_Drawer);
				AddPart(file,AslReq->fr_File,namelen);
				printf("Looking for file: '%s'\n",file);
			}
		}
		FreeAslRequest((APTR)AslReq);
	}
	return file;
}
