/** 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;
BPTR lock;

main(void)
{
LONG ArgPtrs[5]={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",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);}

		FreeArgs(Args);
		if((filename_buf = ReqFile()) == NULL) exit(0);

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

		FreeVec(filename_buf);
		UnLock(lock);

		return(0);
	}
}


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

	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))
		{
			if(file = AllocVec(strlen(AslReq->fr_File)+1,MEMF_ANY))
			{
				strcpy(file,AslReq->fr_File);
				if(strlen(AslReq->fr_Drawer))
				{
					lock = Lock(AslReq->fr_Drawer,ACCESS_READ);
					CurrentDir(lock);
				}
			}
		}
		FreeAslRequest((APTR)AslReq);
	}
	return file;
}
