[comp.sources.atari.st] v02i027: format83 -- Make Intel ASCII Hex files

koreth@ssyx.ucsc.edu (Steven Grimm) (03/12/89)

Submitted-by: sun.com!sunpix!matthew (Matthew Lee Stier)
Posting-number: Volume 2, Issue 27
Archive-name: format83

This program will read a binary file and output in Intel ASCII Hex
format to standard out. This is one of the most common translation formats
for transfer to emulators and PROM programmers.

-- 
Matthew Lee Stier     (919) 469-8300|
Sun Microsystems ---  RTP, NC  27560|          "Wisconsin   Escapee"
uucp: {sun, rti}!sunpix!matthew     |

---- Cut Here and unpack ----
#!/bin/sh
# shar:	Shell Archiver  (v1.22)
#	Packed Mon Dec 19 15:35:52 EST 1988 by friar-taac!matthew
#	from directory /biscuits/home/matthew/IBM/Format83
#
#	Run the following text with /bin/sh to create:
#	  FORMAT83.C
#	  FORMAT83.DOC
#
if test -f FORMAT83.C; then echo "File FORMAT83.C exists"; else
sed 's/^X//' << 'SHAR_EOF' > FORMAT83.C &&
X/*
XAnyway, here is a simple little program which can be used on any machine
Xwith a C compiler and redirectable output (even 8 bitters!) and will work
Xwith any of the multitude of Data I/O programmers I am aware of, and most
Xlikely any programmer by any manufacturer, for that matter, since I think
Xthey all support this format (Intel Hex format).
X
XI don't deal with byte order here, but that should only be a concern if
Xyou are doing word-wide programming on a gang-banger (in which case, just
Xswap the devices in their sockets, if necessary :-) And don't forget Intel
XHex format only has a 16 bit address field, so no files > 64k are allowed.
X*/
X/*_ format83.c   Thu Sep 17 1987   Modified by: Erik Lindberg */
X/*
X      This program is released to the public domain. It can be
X      used for any purpose whatsoever. If you can get some poor
X      sucker to pay money for it, tell me your secret.
X
X      Prints a file to stdout in Intel HEX 83 format. I wrote this
X      to be easy to understand, modify, and *portable*. If you want
X      elegant, you can get there from here.
X*/
X
X#include <stdio.h>
X
X#define REC   0x10            /* Size of a record. */
X
Xchar *line, buffer[128];
XFILE *infile;
X
Xextern char hex();
X
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X      int c=1, address=0;
X      int sum, i;
X      i=0;
X      if (!(infile = fopen(argv[++i],"rb"))) {
X              fprintf(stderr, "Error on open of file %s\n",argv[i]);
X              exit(1);
X      }
X
X      while (c != EOF) {
X              sum = 0;
X              line = buffer;
X              for (i=0; i<REC && (c=getc(infile)) != EOF; i++) {
X                      *line++ = hex(c>>4);
X                      *line++ = hex(c);
X                      sum += c;               /* Checksum each character. */
X              }
X              if (i) {
X                      sum += address >> 8;    /* Checksum high address byte.*/
X                      sum += address & 0xff;  /* Checksum low address byte.*/
X                      sum += i;               /* Checksum record byte count.*/
X                      line = buffer;          /* Now output the line! */
X                      putchar(':');
X                      puthex(i,2);            /* Byte count. */
X                      puthex(address,4);      /* Do address and increment */
X                      address += i;           /*    by bytes in record.   */
X                      puthex(0,2);            /* Record type.            */
X                      for(i*=2;i;i--)         /* Then the actual data.   */
X                              putchar(*line++);
X                      puthex(0-sum,2);        /* Checksum is 1byte 2's comp.*/
X                      printf("\n");
X              }
X      }
X      printf(":00000001FF\n");                /* End record. */
X}
X
X/* Return ASCII hex character for binary value. */
X
Xchar hex(c)
Xint c;
X{
X      if((c &= 0x000f)<10)
X              c += '0';
X      else
X              c += 'A'-10;
X      return((char) c);
X}
X
X/* Put specified number of digits in ASCII hex. */
X
Xputhex(val,digits)
Xint val,digits;
X{
X      if (--digits)
X              puthex(val>>4,digits);
X      putchar(hex(val & 0x0f));
X}
X
SHAR_EOF
chmod 0644 FORMAT83.C || echo "restore of FORMAT83.C fails"
fi
if test -f FORMAT83.DOC; then echo "File FORMAT83.DOC exists"; else
sed 's/^X//' << 'SHAR_EOF' > FORMAT83.DOC &&
X      This program will read a binary file and output in Intel ASCII Hex
Xformat to standard out. This is one of the most common translation formats
Xfor transfer to emulators and PROM programmers. This format can also be read
Xdirectly by the debugger supplied with both MSDOS and CPM if the extension
Xis ".HEX". I.E. if you use "format83 filename.bin> filename.hex" and then
Xload filename.hex with the debugger, you will be looking at your original
Xbinary file. If you modify the program, this is an excellent way to test
Xyour modifications.
X      This program was not written to be efficient, tricky, or elegant.
XIt was written to be easily understood, modified, and/or ported to any
Xsystem, including 8 bit systems with C compilers. Oh yeah, and it also
Xwas written because it was useful. It is free and I am releasing it to
Xthe public domain. You can do whatever you want with it. If you can get
Xsomeone to pay you for it, you have my blessing.
X      This program will not solve all your problems! One of the trickiest
Xparts about using the serial port of your garden variety PC is getting the
Xcabling and parameters right on the serial port. I have included a small
Xhex file (contents "hello world\n" :-) which I suggest you use to verify
Xyour connection with. Until you can "copy test.hex com1:" without getting
Xerrors, do not expect to be able to transfer anything meaningful to your
Xprogrammer. This program *does* work, I use it exclusively for tranferring
Xbinary files to the Data I/O programmers I use here, but it was written by
Xme personally for my own use and is not supported by Data I/O. If you do use
Xthis with a Data I/O programmer, select transfer format 83 before starting.
XIf you don't rename this program, it will impossible for you to forget :-)
SHAR_EOF
chmod 0644 FORMAT83.DOC || echo "restore of FORMAT83.DOC fails"
fi
exit 0
--