/* Easy echoer. v0.1   Programed by s.kamio   20 June 1991 */
/* for fm-towns all series   /   for gnu-c 1.36 */

#include <stdio.h>
#include <tbios.h>	/* my header   call tbios system */

void main (int argc,char *argv[])
{
	byte c[65536+1];		/* dual buffers */
	byte b[65536+1];
	uint i,s,ns;

	ns=argc*4096;		/* buffer size */
	if (ns>65536) ns=65536;

	printf ("Easy echo version 0.1　　BIOS使用版\n");
	printf ("For All fm-towns  Programed by s.kamio June 1991\n");
	printf ("Buffer size = %u\n",ns);

	for (i=0;i<ns;i++)		/* clear memory */
	{
		c[i]=0;
		b[i]=0;
	}

	/* FM-TOWNS 2F (A mode)= 1831 */
	/* FM-TOWNS 2F (N mode)= 1758 */

	c[24]=1758 % 256;	/* pcm frequency/62h */
	c[25]=1758 / 256;

	c[12]=ns % 256;		/* buffer size */
	c[13]=ns / 256;
	c[14]=c[15]=0;

	c[26]=0;
	c[27]=0;
	c[20]=0;			/* no loop */
	c[21]=0;
	c[22]=0;
	c[23]=0;
	c[28]=60;			/* Oct=4,C */

	for (i=0;i<32;i++)			/* header copy (voice mode) */
		b[i]=c[i];

	SND_init ();				/* sound bios initilize */
	SND_mute (0xff);			/* mute off */
	SND_evolume (0,127,127);	/* line on */
	SND_evolume (1,127,127);	/* cd on */
	SND_voicechannel (2);		/* dual buffer */
	waite (10);
	while ((SND_pad (1) & 0x10)!=0)	/* end when mouse both button click */
	{
		for (i=32;i<ns;i++)
		{
			while ((inpb (0x4e8) & 1)==0) {}
			s=inpb (0x4e7);
			if (s==0xff)
				s=0xfe;
			if (s<0x80)
				s=0x7f-s;
			c[i]=s;
		}
		SND_voiceplay (&c[0],71,60,45);
		for (i=32;i<ns;i++)
		{
			while ((inpb (0x4e8) & 1)==0)
			{
			}
			s=inpb (0x4e7);
			if (s==0xff)
				s=0xfe;
			if (s<0x80)
				s=0x7f-s;
			b[i]=s;
		}
		SND_voiceplay (&b[0],70,60,45);
	}
	SND_mute (0x00);
	SND_pcmstop ();
	SND_bye ();			/* initilize interval interapt system */
	exit (0);
}
