home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / aaa / aux / hex < prev    next >
Encoding:
Text File  |  1986-11-30  |  2.0 KB  |  82 lines

  1. # Convert to Intel hex format.  Input lines should have location first
  2. # and then one-byte decimal value, separated by tabs; any further stuff
  3. # on the line is ignored.  Blank lines are ignored, as are lines starting
  4. # with white space followed by a '#'.  If the awk variable "offset" is
  5. # non-empty, it's the (decimal) offset for the location counter (so code
  6. # assembled to go at location 010000 can start at 0 for purposes of
  7. # PROM blowing and such).  If "startaddr" is non-empty, it's the start
  8. # address to go in the terminator record.
  9. #
  10. # Annoying botch:  to avoid repeating all the buffer-flush code twice
  11. # (laziness on my part), the last line of input should be "0\t0" or
  12. # something like that (i.e., valid-looking input with an out-of-sequence
  13. # location) to cause a flush.
  14. BEGIN {
  15.     FS = "\t"
  16.     nbytes = 0
  17.     loc = 0
  18.     if (offset == "")
  19.         offset = 0
  20.     if (start == "")
  21.         start = 0
  22.     hex[0] = "0"
  23.     hex[1] = "1"
  24.     hex[2] = "2"
  25.     hex[3] = "3"
  26.     hex[4] = "4"
  27.     hex[5] = "5"
  28.     hex[6] = "6"
  29.     hex[7] = "7"
  30.     hex[8] = "8"
  31.     hex[9] = "9"
  32.     hex[10] = "A"
  33.     hex[11] = "B"
  34.     hex[12] = "C"
  35.     hex[13] = "D"
  36.     hex[14] = "E"
  37.     hex[15] = "F"
  38. }
  39. /^$/ { next }
  40. /^[     ]*#/ { next }
  41. {
  42.     byteloc = substr($1, 1, length($1)-1)
  43.     if (byteloc != loc+nbytes || nbytes >= 8) {
  44.         if (nbytes > 0) {
  45.             lochi = int((loc+offset)/256 + 0.001)
  46.             loclo = int((loc+offset)%256 + 0.001)
  47.             locx = sprintf("%02x%02x", lochi, loclo)
  48.             cs += nbytes + lochi + loclo
  49.             while (cs > 255)
  50.                 cs -= 256
  51.             cs = -cs
  52.             if (cs < 0)
  53.                 cs += 256
  54.             csx = sprintf("%02x", cs)
  55.             print ":0" hex[nbytes] locx "00" datax csx
  56.         }
  57.         nbytes = 0
  58.         datax = ""
  59.         loc = byteloc
  60.         cs = 0
  61.     }
  62.     nbytes++
  63.     it = $2
  64.     if (it < 0)
  65.         it += 256
  66.     cs += it
  67.     datax = sprintf("%s%02x", datax, it);
  68. }
  69. END {
  70.     starthi = int((start+offset)/256 + 0.001)
  71.     startlo = int((start+offset)%256 + 0.001)
  72.     startx = sprintf("%02x%02x", starthi, startlo)
  73.     cs = starthi + startlo + 1
  74.     while (cs > 255)
  75.         cs -= 256
  76.     cs = -cs
  77.     if (cs < 0)
  78.         cs += 256
  79.     csx = sprintf("%02x", cs)
  80.     print ":00" startx "01" csx
  81. }
  82.