/*
From owner-pc532%daver@mips.com Fri Jun  1 00:37:49 1990
X-Path: sibyl.eleceng.ua.oz.au!ian
From: ian@sibyl.eleceng.ua.oz.au
To: pc532@daver.bungi.com
Subject: intelhex
Date: Thu, 31 May 1990 12:09:32 +1000
Reply-To: pc532@bungi.com
Precedence: bulk

Someone (Dave Rand?) Posted a little program "intelhex.c". I don't know what
this generates, but it certainly isn't what our prom burner calls intel-hex
format. Here is a program which does generate a format acceptable to our prom
burner. The "unsigned" mightn't be necessary but this program is firmly in the
"quick hack" category so I haven't bothered to check.

*/
#include <stdio.h>

main()
{
  int i, nbytes = 0;
  unsigned char bytes[32];
  int offset = 0;
  do
    {
      int ret;
      int checksum;
      unsigned char type;
      ret = fread(bytes, 1, 32, stdin);
      if (ret == 0)
	{
	  type = 1;
	  offset -= nbytes;
	}
      else
	type = 0;
      nbytes = ret;
      checksum = nbytes;
      checksum += (offset & 0xff);
      checksum += ((offset >> 8) & 0xff);
      checksum += type;
      printf(":%2.2X%4.4X%2.2X", nbytes, offset, type);
      offset += nbytes;
      for (i = 0; i < nbytes; i++)
	{
	  printf("%2.2X", bytes[i]);
	  checksum += bytes[i];
	}
      printf("%2.2X\n", ((-checksum) & 0xff));
    }
  while (nbytes != 0);
  exit (0);
}
