home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / code / forthmacs / !Forthmacs / lib / srec < prev    next >
Encoding:
Text File  |  1994-07-08  |  2.0 KB  |  76 lines

  1. \ Convert a binary file to a file containing S-records.
  2. \
  3. \ To use from the command line:
  4. \
  5. \    forth srec.fth -s "to-srecs input-filename output-filename"
  6. \
  7. \ to-srecs  \ input-filename output-filename  ( -- )
  8. \    "input-filename" is the name of a file containing binary data
  9. \    (just data; no headers or anything else).  to-srec creates a
  10. \    file named "output-filename", and stores in it the S-record
  11. \    representation of the binary data in the input file.
  12.  
  13. h# 20 constant max-srec
  14.  
  15. variable checksum
  16. variable load-address
  17.  
  18. create outchars ," 0123456789ABCDEF"
  19.  
  20. : fcr  ( fd -- )  newline-pstring count rot fputs  ;
  21.  
  22. \ Split a longword into its constituent bytes
  23. : lbsplit ( l -- lowbyte lmbtye hmbyte highbyte )
  24.    dup  h# ff and  swap 8 >>
  25.    dup  h# ff and  swap 8 >>
  26.    dup  h# ff and  swap 8 >>
  27.         h# ff and
  28. ;
  29.  
  30. : .nibble ( n -- )  \ Output a hex nibble
  31.    h# 0f and  outchars 1+ swap ca+ c@  ofd @ fputc
  32. ;
  33.  
  34. : .byte ( byte -- ) \ output a hex byte and accumulate the checksum
  35.    dup checksum +!
  36.    dup 4 >> .nibble .nibble
  37. ;
  38.  
  39. : .addr ( l.address -- )  \ Output a 24-bit hex address and checksum
  40.    lbsplit drop .byte .byte .byte
  41. ;
  42.  
  43. : .data ( address count -- )  \ Output the count data bytes starting at addr
  44.    bounds  do  i c@ .byte  loop
  45. ;
  46.  
  47. : .srec ( mem-address count -- )  \ Output a data S-record
  48.    " S2" ofd @ fputs
  49.    0 checksum !
  50.    dup 4 + .byte ( load-address mem-address count )  \ output the count field
  51.    load-address l@ .addr ( address count )
  52.    dup n->l load-address l+!
  53.    .data
  54.    checksum @  not  .byte
  55.    ofd @ fcr
  56. ;
  57.  
  58. max-srec buffer: ibuf
  59.  
  60. \ Output data S-records until the count is exhausted
  61. : .srecs  ( load-adr -- )
  62.    load-address l!
  63.    " S204000000FB" ofd @ fputs  ofd @ fcr   \ Header record
  64.    begin
  65.       ibuf max-srec  ifd @ fgets  dup 0>
  66.    while                                       ( actual-length )
  67.       ibuf swap .srec
  68.    repeat
  69.    drop
  70.    " S804000000FB" ofd @ fputs  ofd @ fcr   \ Trailer record
  71.    ofd @ fclose
  72. ;
  73. : to-srecs  \ input-filename output-filename  ( -- )
  74.    reading  writing  0 .srecs
  75. ;
  76.