#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>

extern char peekb ();
extern short peekw();
extern int peekl();
extern void pokeb();

main(argc,argv)
int argc;
char **argv;
{

FILE *fp;
int IoByte;
int Ret;

int InByte;
int fd;

#define DLOADDATA ((char *)0xf1a071)
#define DLOADCTRL ((char *)0xf1a073)
#define DLOADSTS ((char *)0xf1a073)

if(argc != 2) 	{	
		printf("\n   Usage: dload <filename> <cr>\n");
		exit(0);
		}
printf("\n Dload UNIX Version 1.0 for VMEs10 Emulator\n");

pokeb(DLOADCTRL, (unsigned char)0xff) ; /* rewind */

if (peekb(DLOADSTS) == 1)
{
   printf("\nNo Dload file open\n");
   exit(0);
}

if( (fd = open(argv[1],0))!=-1 )
{
   close(fd);
   printf("\nFile Exists - Overwrite? (Y or N) - ");
   InByte=getchar();
   if ( (InByte != 'Y') && (InByte != 'y' ) ) exit(0);
}

if ((fp = fopen(argv[1],"w")) == NULL ) 	
{
   printf("\nCan't open %s\n",argv[1]);
   exit(0);
}
printf("\nDownloading...");


while (peekb(DLOADSTS) == 0)
{
	InByte= peekb(DLOADDATA);
	if (peekb(DLOADSTS) == 0) 
	{
	 fputc(InByte,fp);
	};
}

fclose(fp);

printf("Finished\n");
}
 
