// dl.c
// Downloader program for HC11
// Use with loader.asm

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>

int main(int argc, char **argv)
{
	int sp;
	struct termios tios;
	int buff;
	int s19file;
	int s19byte;
	char* filename;

	if(argc <= 1)
	{
		printf("usage: dl filename.s19\n");
		return 0;
	}

	filename = argv[1];

	printf("Opening serial port...\n");
	sp = open("/dev/ttyS0", O_RDWR|O_NOCTTY);
	if(sp == -1)
	{
		printf("Error opening serial port.\n");
		return 1;
	}

	printf("Opening %s...\n", filename);
	s19file = open(filename, O_RDONLY);
	if(s19file == -1)
	{
		printf("Error opening %s.\n", filename);
		return 1;
	}

	tios.c_iflag = IGNPAR;
	tios.c_oflag = 0;
	tios.c_cflag = CS8|CREAD|CLOCAL;
	tios.c_lflag = 0;
	tios.c_cc[VTIME] = 0;
	tios.c_cc[VMIN] = 1;

	printf("Setting 1200 baud...\n");
	cfsetospeed(&tios, B1200);
	printf("Flushing buffers...\n");
	tcflush(sp, TCIOFLUSH);
	printf("Setting serial communications settings...\n");
	if(tcsetattr(sp, TCSANOW, &tios))
	{
		printf("Error applying settings");
		return 1;
	}

	printf("Sending S19 file...\n");
	while(read(s19file, &s19byte, 1))
	{
		if(s19byte != '\n')
		{
			write(sp, &s19byte, 1);
			while(!read(sp, &buff, 1)) {}
			printf("%c%c ", s19byte, buff);
			fflush(stdout);
		}
	}
	return 0;
}


syntax highlighted by Code2HTML, v. 0.8.11