# Convert to Intel hex format.  Input lines should have location first
# and then one-byte decimal value, separated by tabs; any further stuff
# on the line is ignored.  Blank lines are ignored, as are lines starting
# with white space followed by a '#'.  If the awk variable "offset" is
# non-empty, it's the (decimal) offset for the location counter (so code
# assembled to go at location 010000 can start at 0 for purposes of
# PROM blowing and such).  If "startaddr" is non-empty, it's the start
# address to go in the terminator record.
#
# Annoying botch:  to avoid repeating all the buffer-flush code twice
# (laziness on my part), the last line of input should be "0\t0" or
# something like that (i.e., valid-looking input with an out-of-sequence
# location) to cause a flush.
BEGIN {
	FS = "\t"
	nbytes = 0
	loc = 0
	if (offset == "")
		offset = 0
	if (start == "")
		start = 0
	hex[0] = "0"
	hex[1] = "1"
	hex[2] = "2"
	hex[3] = "3"
	hex[4] = "4"
	hex[5] = "5"
	hex[6] = "6"
	hex[7] = "7"
	hex[8] = "8"
	hex[9] = "9"
	hex[10] = "A"
	hex[11] = "B"
	hex[12] = "C"
	hex[13] = "D"
	hex[14] = "E"
	hex[15] = "F"
}
/^$/ { next }
/^[ 	]*#/ { next }
{
	byteloc = substr($1, 1, length($1)-1)
	if (byteloc != loc+nbytes || nbytes >= 8) {
		if (nbytes > 0) {
			lochi = int((loc+offset)/256 + 0.001)
			loclo = int((loc+offset)%256 + 0.001)
			lochx = hex[int(lochi/16+0.01)] hex[int(lochi%16+0.01)]
			loclx = hex[int(loclo/16+0.01)] hex[int(loclo%16+0.01)]
			locx = lochx loclx
			cs += nbytes + lochi + loclo
			while (cs > 255)
				cs -= 256
			cs = -cs
			if (cs < 0)
				cs += 256
			csx = hex[int(cs/16+0.01)] hex[int(cs%16+0.01)]
			print ":0" hex[nbytes] locx "00" datax csx
		}
		nbytes = 0
		datax = ""
		loc = byteloc
		cs = 0
	}
	nbytes++
	it = $2
	if (it < 0)
		it += 256
	cs += it
	datax = datax hex[int(it/16+0.01)] hex[int(it%16+0.01)]
}
END {
	starthi = int((start+offset)/256 + 0.001)
	startlo = int((start+offset)%256 + 0.001)
	starthx = hex[int(starthi/16+0.01)] hex[int(starthi%16+0.01)]
	startlx = hex[int(startlo/16+0.01)] hex[int(startlo%16+0.01)]
	startx = starthx startlx
	cs = starthi + startlo + 1
	while (cs > 255)
		cs -= 256
	cs = -cs
	if (cs < 0)
		cs += 256
	csx = hex[int(cs/16+0.01)] hex[int(cs%16+0.01)]
	print ":00" startx "01" csx
}
