home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / mkhexrec < prev    next >
Encoding:
AWK Script  |  1997-08-26  |  2.1 KB  |  85 lines

  1. #!/usr/local/bin/gawk -f
  2. #!/usr/bin/awk -f
  3. # @(#) mkhexrec.gawk 1.0 94/03/09
  4. # 92/02/13 john h. dubois iii (john@armory.com)
  5. # 92/10/01 Use gawk to get %X (awk only has %x); added +h option
  6. # 93/06/19 Replace %X w/%x and use toupper() to avoid need for gawk
  7. # 94/03/09 Use gawk so - options can be given
  8.  
  9. BEGIN {
  10.     if (ARGV[1] ~ "^[-+]h$") {
  11.     # Multipe lines per line because gawk has a limit on the number of
  12.     # continuations to a line
  13.     print \
  14. "mkhexrec: make a hex record\nInput format:\naddr xxxx\n" \
  15. "xx xx xx xx xx xx xx xx\nxx xx xx xx xx xx xx xx\nxx xx xx xx xx xx xx xx\n" \
  16. "addr xxxx\nxx xx xx xx xx xx xx xx\nxx xx xx xx xx xx xx xx\n\n" \
  17. "addr is the load address for the bytes that follow.\n\n" \
  18. "Output format (format of a hex file):\n"
  19.  
  20.     print \
  21. ":nnaaaa00xxxxxxxxxxxxxxxxxxxxxxxxxxcc\nnn: number of bytes to be loaded.\n" \
  22. "aaaa: address to load at.\n00: MCS80 record type.\n"\
  23. "xxxxxxxx: data to be loaded\n" \
  24. "cc: checksum byte to make sum % 256 of all bytes on line equal to 0.\n" \
  25. "Example:\nLoad 8 bytes starting at address 0010\n:0800108899aabbccddeeff"
  26.     Err = "0"
  27.     exit 0
  28.     }
  29.     addr = start = 0
  30. }
  31.  
  32. function hex2dec(hexval,  ret,len,i) {
  33.     hexval = tolower(hexval)
  34.     len = length(hexval)
  35.     for (i = 1; i <= len; i++)
  36.     ret = ret * 16 + index("0123456789abcdef",substr(hexval,i,1)) - 1
  37.     return ret
  38. }
  39.  
  40. function putmem(start,end,  len) {
  41.     for (; start <= end; start += 32)
  42.     putline(start,min(end,start + 31))
  43. }
  44.  
  45. function putline(start,end,  checksum,line) {
  46.     checksum = end - start + 1
  47.     line = sprintf(":%02x%04x00",checksum,start)
  48.     checksum += start % 256 + int(start / 256)
  49.     for (; start <= end; start++) {
  50.     line = line sprintf("%s",mem[start])
  51.     checksum += hex2dec(mem[start])
  52.     }
  53.     print toupper(line sprintf("%02x\n",256 - checksum % 256))
  54. }
  55.  
  56. function min(a,b) {
  57.     if (a < b)
  58.     return a
  59.     else
  60.     return b
  61. }
  62.  
  63. /^(#|$)/ {
  64.     putmem(start,addr - 1)
  65.     start = addr
  66.     next
  67. }
  68.  
  69. $1 == "addr" {
  70.     putmem(start,addr - 1)
  71.     start = addr = hex2dec($2)
  72.     next
  73. }
  74.  
  75. {
  76.     for (i = 1; i <= NF; i++)
  77.     mem[addr++] = $i
  78. }
  79.  
  80. END {
  81.     if (Err != "")
  82.     exit Err
  83.     putmem(start,addr - 1)
  84. }
  85.