Yet Another Hex to bin converter

It can handle the extended Intel hex format in segmented and linear address
modes. Records need not be sorted and there can be gaps between records. 

Some hex files are produced by compilers. They generate objects files for each
module in a project, and when the linker generates the final hex file, the
object files are stored within the hex files, but modules can appear not
necessary in order of address.

How does it work?

It allocates a 4 MBytes buffer and just place the converted bytes in its
buffer. At the end, the buffer is written to disk. Using a buffer eliminates
the need to sort records.


1. Compiling on Linux or other unix platforms

	make
	
	then 
	
	make install
	
	This will install the program to /usr/local/bin.
	
1a. Compiling for Windows on Msys, Cygwin or DOS prompt

	The programs can be compiled as follows:
	  gcc -O2 -Wall -o hex2bin.exe hex2bin.c
	  gcc -O2 -Wall -o mot2bin.exe mot2bin.c

2. Using hex2bin

	hex2bin example.hex
	
	hex2bin will generate a binary file example.bin starting at the 
	lowest address in the hex file.
	
3. Notes

	If the lowest address isn't 0000, 
	    ex: 0100: (the first record begins with :nn010000xxx ) 
	    
	there will be problems when using the binary file to program a EPROM 
	since the first byte supposed to be at 0100 is stored in the binary file
	at 0000.

	you can specify a starting address on the command line:
		
	hex2bin -s 0000 start_at_0100.hex

		This start address is not the same thing as the start address record in
		the hex file. The start address record is used to specify the starting
		address for execution of the binary code.		

	The bytes will be stored in the binary file with a padding from 0000
	to the lowest address minus 1 (00FF in this case). Padding bytes are all FF by
	default	so an EPROM programmer can skip these bytes when programming. The padding
	value can be changed with the -p option.

	EPROM, EEPROM and Flash memories contain all FF when erased.

	This program does minimal error checking since many hex files are
	generated by known good assemblers.

	When the source file name is 
		for-example.test.hex
	the binary created will have the name 
		for-example.bin
	the ".test" part will be dropped.

	Hex2bin/mot2bin assume the source file doesn't contain overlapping records,
	if so, overlaps will be silently executed, causing some esoteric bugs in your
	application.
	
	Since the code for testing for overlapping records migh be quite elaborate, 
	I prefer to leave this as it is for now, to minimize maintenance of the code.

4. Checksum of source file

	By default, it ignores checksum errors, so that someone can change
	by hand some bytes allowing quick and dirty changes.
	If you want checksum error reporting, specify the option -c.

	hex2bin -c example.hex

	If there is a checksum error somewhere, the program will continue the
	conversion anyway.
	
	The example file example.hex contains some records with checksum errors.

5.	Checksum inserted inside binary file

	A checksum value can be inserted in the resulting binary file.

	hex2bin -k [0|1|2] -r [start] [end] -f [address] [value]

	-k  Select checksum type:
	
		0 = 8-bit checksum
		1 = 16-bit checksum, little endian
		2 = 16-bit checskum, big endian

    -r	Range to compute checksum over (default is min and max addresses)
	
    -f	Address and value of checksum to force

6. Motorola S files
	
	mot2bin example.s19

	Options for mot2bin are the same as hex2bin. Executing the program
	without argument will display available options. Some are specific to
	Motorola files.

	This program will handle S19 files generated for Motorola micropro-
	cessors. Since I use this program for an EPROM programmer, I will 
	rarely need to have more than 4M, I limited the source program for
	24 bits or 16 bits address records.
	
	32 bits records are now supported, but obviously I can't allocate all
	the memory for the binary target. What I did is simply assume that the
	binary file will occupy less than 4M.

8. Goodies

	Description of the file formats is included.
	Added examples files for extended addressing.

9. Error messages

	"0 byte length data record ignored"
	
	This means that an empty data record was read. Since it's empty, it's simply
	ignored and should have no impact on the binary file.

	"Data record skipped at ..."
	
	This means that the records are falling outside my 1M buffer. You may 
	try to increase the buffer size to 2M or 4M. There is a define at the
	beginning of the file.

10. History

        See ChangeLog

11. Other hex tool

	There is a program that supports more formats and has more features.
	See SRecord at http://srecord.sourceforge.net/
