/** VGB: portable GameBoy emulator ***************************/
/**                                                         **/
/**                        AmigaVGB.c                       **/
/**                                                         **/
/** This file contains Amiga main() procedure starting      **/
/** the emulation.                                          **/
/**                                                         **/
/** Copyright (C) Lars Malmborg 1996                        **/
/** based on a version by Marat Fayzullin 1995              **/
/**     You are not allowed to distribute this software     **/
/**     commercially. Please, notify me, if you make any    **/
/**     changes to this file.                               **/
/*************************************************************/

/** Yo! Amiga coder! *****************************************/
/**                                                         **/
/**    You can compile with or without GameBoy debugging.   **/
/** To turn it on, define DEBUG. There is also another flag **/
/** you can set, if you want to generate a version that     **/
/** uses CyberGraphX directlly. I didn't notice any speed   **/
/** improvement, but you might if you are using another     **/
/** graphics card than I do (PicassoII). Define CYBER to    **/
/** enable this.                                            **/
/**    The port is made quite clean, so you can take the    **/
/** Unix distribution and copy smakefile, SCOPTIONS and the **/
/** Amiga#? into it without collision, and type smake.      **/
/**                                                         **/
/*************************************************************/

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


/* Amiga includes */
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/asl.h>
#include <dos/rdargs.h>
#include <exec/memory.h>
//#include <devices/audio.h>

#include "GB.h"
#include "AmigaHelp.h"


extern char *Title;

extern word Trap;
BPTR lock;

extern BOOL ScaleUp;
extern STRPTR PubScreen;

void SetColors(int,char*);

/****************************************************************/
/*** Amiga specific functions and defines                     ***/
/************************************** TO BE WRITTEN BY USER ***/
STRPTR ReqFile(void);

#define OPT_CARTRIDGE ((STRPTR)(ArgPtrs[0]))
#define OPT_VERBOSE ((ULONG*)(ArgPtrs[1]))
#define OPT_VPERIOD ((ULONG*)(ArgPtrs[2]))
#define OPT_UPERIOD ((ULONG*)(ArgPtrs[3]))
#define OPT_HELP ((BOOL)(ArgPtrs[4]))
#define OPT_CHEAT ((STRPTR)(ArgPtrs[5]))

#define OPT_DELAY ((BOOL)(ArgPtrs[6]))
#define OPT_NOCRC ((BOOL)(ArgPtrs[7]))
#define OPT_AUTOA ((BOOL)(ArgPtrs[8]))
#define OPT_AUTOB ((BOOL)(ArgPtrs[9]))

#define OPT_TRAP ((STRPTR)(ArgPtrs[10]))
#define OPT_COLORS ((STRPTR)(ArgPtrs[11]))
#define OPT_BCOLORS ((STRPTR)(ArgPtrs[12]))
#define OPT_SCOLORS ((STRPTR)(ArgPtrs[13]))
#define OPT_WCOLORS ((STRPTR)(ArgPtrs[14]))
#define OPT_SCALEUP ((BOOL)(ArgPtrs[15]))
#define OPT_PSCREEN ((STRPTR)(ArgPtrs[16]))

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

  TrapBadOps=1;LineDelay=0;CheckCRC=1; 
  VPeriod=69905;UPeriod=2;IntSync=1;
  Verbose=5;SndName=NULL;
	ScaleUp=FALSE;PubScreen=NULL;

	if(Args = ReadArgs("CR=Cartridge/K,V=Verbose/K/N,VP=VPeriod/K/N,UP=UPeriod/K/N,Help/S,Cheat/K,Delay/S,NoCRC/S,AutoA/S,AutoB/S,Trap/K,C=Colors/K,BC=BColors/K,SC=SColors/K,WC=WColors/K,2=ScaleUp/S,PS=PublicScreen/K",ArgPtrs,NULL))
	{
		if(OPT_CARTRIDGE)
		{
			if (filename_buf=AllocVec(strlen(OPT_CARTRIDGE)+1,MEMF_ANY|MEMF_CLEAR))
				strcpy(filename_buf,OPT_CARTRIDGE);
		}

		if(OPT_VERBOSE)
		{
			Verbose = (int)(*OPT_VERBOSE);
		};

		if(OPT_VPERIOD)
		{
			VPeriod = (int)(*OPT_VPERIOD);
		};

		if(OPT_UPERIOD)
		{
			UPeriod = (int)(*OPT_UPERIOD);
		};

		if(OPT_HELP)
		{
			puts(HelpText);
			FreeArgs(Args);
			exit(0);
		};

		if(OPT_CHEAT)
		{
			if(!AddCheat(OPT_CHEAT))
				printf("Wrong cheat data supplied\n");
		};

		if(OPT_DELAY)
		{
			LineDelay=1;
		};

		if(OPT_NOCRC)
		{
			CheckCRC=0;
		};

		if(OPT_AUTOA)
		{
			AutoA=1;
		};

		if(OPT_AUTOB)
		{
			AutoB=1;
		};

#ifdef DEBUG
		if(OPT_TRAP)
		{
			if(!stricmp("now",OPT_TRAP))
			{
				Trace=1;
			}
			else
			{
				sscanf(OPT_TRAP,"%hx",&Trap);
			}
		}
#endif

		if(OPT_COLORS)
		{
			SetColors(0,OPT_COLORS);
			SetColors(4,OPT_COLORS);
			SetColors(8,OPT_COLORS);
		}

		if(OPT_BCOLORS)
		{
			SetColors(0,OPT_BCOLORS);
		}

		if(OPT_SCOLORS)
		{
			SetColors(4,OPT_SCOLORS);
		}

		if(OPT_WCOLORS)
		{
			SetColors(8,OPT_WCOLORS);
		}

		if(OPT_SCALEUP)
		{
			ScaleUp=TRUE;
		};

		if(OPT_PSCREEN)
		{
			if (PubScreen=AllocVec(strlen(OPT_PSCREEN)+1,MEMF_ANY|MEMF_CLEAR))
				strcpy(PubScreen,OPT_PSCREEN);
		}

		FreeArgs(Args);

		if (!filename_buf)
		{
			if((filename_buf = ReqFile()) == NULL) exit(0);
		}

  if(!InitMachine())
	{
	  TrashMachine();
		if (filename_buf)
			FreeVec(filename_buf);
		if (PubScreen)
			FreeVec(PubScreen);
		return(1);
	}
	StartGB(filename_buf);
	TrashGB();
  TrashMachine();
	if (filename_buf)
		FreeVec(filename_buf);
	if (PubScreen)
		FreeVec(PubScreen);
  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,"Trash",
			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;
}
