/*****************************************************************************
 *								  geta.c     *
 *****************************************************************************
 * DESCRIPTION: This program loads EDITAS assembler data from the spectrum   *
 *              over the RS232 link                                          *
 *									     *
 * REVISIONS:   18 AUG 91 - HDG - Initial setup.			     *
 *****************************************************************************/

#include		<stdio.h>
#include		<signal.h>
#include		<string.h>
#include		"ibmcom.h"
#include		"patch.h"

STARTPATCH
int PORT=1;
ENDPATCH

#define VARNAME(i)	((i & 0x1f) + 0x60)
#define SPEED   9600

union header_u {
	char in[9];
	struct {
		char type;
		unsigned int  length;
		unsigned int  start;
		char var;
		char res1;
		int  line;
	} header;
} h;

char loadimage[32768];
int  loadp=0;

void catch();

get_key()
{
	int		 c;
	if ((c = getch())!=0) return c;
	else return getch() + 256;
}

main(argc,argv) 
int argc;
char *argv[];
{
	int status;
	unsigned int i,js;

	SETUP;

	if(argc != 2)
	{
		fprintf(stderr,"Usage: geta <output-file>\n");
		return 1;
	}

	if ((status = com_install(PORT))!=0)
	{
		fprintf(stderr,"com_install() error: %d\n", status);
		return 1;
	}

	signal(SIGINT,catch);

	com_set_speed(SPEED);
	com_set_parity(COM_NONE, 1);
	com_raise_dtr();
	printf("Load from ZX Spectrum to file %s -- ",argv[1]);
	printf("Stop with ALT-X\n\n");
	
	i=0;
	js=0;
	while(i<9)
	{
		if (kbhit())
		{
			status = get_key();
			if (status == 301 /* Alt-X */) 
			{
				com_deinstall();
				return 1;
			}
		}
		if ((status = com_rx()) != -1)
		{
			h.in[i] = (char) status;
			/* this fixes the failure that would occur if there */
			/* is a junk character left in the UART (not 100%)  */
			/* "js" will prevent a second call of the fix       */
			if((js==0) && (i==0) && ((unsigned int) h.in[0] >= 4))
			{	
				js=1;	
			}	
			else
			{
				puti((char) status);
				i++;
			}	
		}
	}

	if(h.header.type==3)
	{
		printf("Code - start %u - length %u\n",
			h.header.start,h.header.length);
	}
	else
	{
		fprintf(stderr,"\"CODE\" header expected, received someting else!\n");
		com_deinstall();
		return 1;
	}		

	i = 0;
	while (i < h.header.length) 
	{
		if (kbhit()) 
		{
			status = get_key();
			if (status == 301 /* Alt-X */) 
			{
				com_deinstall();
				return 1;
			}
		}
		if ((status = com_rx()) != -1)
		{
			puti((char) status);
			i++;
		}
	}
	com_deinstall();
	convert(argv[1]);
	return 0;
}

void catch()
{
	printf("\nBREAK - program terminated\n");
	com_deinstall();
	exit(1);
}

puti(c)
char c;
{
	loadimage[loadp]=c;
	loadp++;
}

convert(name)
char *name;
{
	FILE *sid;
	int i;
	int quote;
	int newline;
	char c;
	int imgp;

	sid=fopen(name,"w");
	if(sid == NULL)
	{
		perror(name);
		exit(1);
	}
	
	imgp=0;
	for(i=0; i<10; i++) imgp++;
	
	quote=0;
	newline=2;

	while(imgp<loadp)
	{
		i=loadimage[imgp];
		imgp++;
		
		if(i==0x0ff)
		{
			break;
		}
		c=(char)i;
		if(c=='\r')
		{
			newline=1;
			fputc('\n',sid);
		}
		else if(newline!=0)
		{
			/* skip line number */
			imgp++;
			imgp++;
			newline=0;
		}
		else
		{
			if((c==' ') && (quote==0))
			{
				fputc('\t',sid);
			}
			else
			{
				fputc(c,sid);
				if(c=='"')
					quote = !quote;
			}	
		}
	}

	fclose(sid);
}

void setup()
{
	printf("Setup\n\n");
	printf("Port    Address    IRQ\n");
	printf("COM1      3f8       4\n");
	printf("COM2      2f8       3\n");
	printf("COM3      3e8       4\n");
	printf("COM4      2e8       3\n\n");
	printf("Attention if you want to use COM3 or COM4:\n\n");
	printf("COM1 and COM3 cannot be used simultaneously\n");
	printf("COM2 and COM4 cannot be used simultaneously\n");
	printf("If your mouse is connected to COM1 you should not use COM3\n");
	printf("If your mouse is connected to COM2 you should not use COM4\n\n");
	printf("If your modem is connected to COM1 you can use COM3 if this program\n");
	printf("and the communication program are not running at the same time\n\n");
	printf("If your modem is connected to COM2 you can use COM4 if this program\n");
	printf("and the communication program are not running at the same time\n\n");
	printf("Give comport number: ");
	fflush(stdout);
	scanf("%d",&PORT);
	printf("\n\nSetup finished\n\n");
}