#include "stdio.h"

main(argc,argv)
int argc;
char **argv;
{
FILE *fopen();
FILE *fopena();
FILE *fopenb();
FILE *fp;
int IoByte;
int Ret;
int cntz;
int InByte;
int fd;
int RecCnt;

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

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

*DLOADCTRL = 0xff ; /* rewind */

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

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

cntz=0x80;
RecCnt=0;

while (*DLOADSTS == 0)
{
	InByte= *DLOADDATA;
	if (*DLOADSTS == 0) 
	{
	 fputc(InByte,fp);
	 cntz--;
	 if (cntz==0){ cntz=0x80; RecCnt++; }
	};
}
while (cntz !=0 && cntz !=0x80) 
{
 fputc(0x1a,fp); cntz--; 
 if (cntz==0)RecCnt++;
}

fclose(fp);

printf("Finished\n%d Records Written\n",RecCnt);
}
 
