home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tcom96 / samples / hexcom.seq < prev    next >
Encoding:
Text File  |  1991-03-21  |  3.3 KB  |  85 lines

  1. \\ HEXCOM.SEQ     read in a HEX file and convert it to a .COM
  2. ─────────────────────────────────────────────────────────────────────────────
  3. $Header:$
  4. ─────────────────────────────────────────────────────────────────────────────
  5. This program reads in an Intel-HEX format file with extension .HEX
  6. It creates a .COM file with the same name as the .HEX file.
  7.  
  8. This program is used as follows e.g.
  9.         hexcom - hexcom LCDT
  10. i.e.    LCDT.HEX is converted to LCDT.COM
  11.  
  12. ─────────────────────────────────────────────────────────────────────────────
  13.   $Log:$
  14. ─────────────────────────────────────────────────────────────────────────────
  15. {
  16. anew hexwords
  17. decimal
  18. 2variable FLEN
  19. -1 value lastshow
  20. $2000 value ROMaddr     \ lowest address in .COM image
  21. 0 value topaddr
  22.  
  23. : hex># ( h -- n ) \ convert hex digit to a number
  24.         dup '9' > if  $37 -  else  '0' -  then  ;
  25. 0 value sadr
  26. 0 value scnt
  27. : stash ( a c -- ) \ hide away the pointer and count of the line string
  28.                    \  but first skip over the :
  29.         1- !> scnt  1+ !> sadr ;
  30. : nbyte ( -- n ) \ get the next byte from the HEX line stash'ed
  31.         sadr c@  hex>#  16 *   incr> sadr  decr> scnt
  32.         sadr c@  hex>#  +      incr> sadr  decr> scnt   ;
  33.  
  34. : line<hex ( a c -- )   \ convert a hex line and
  35.                         \ load it to the target image at pad
  36.         stash                   \ hide away the line pointers
  37.         nbyte                   \ byte count
  38.         nbyte 256 *  nbyte +    \ address of a byte in the target
  39.         ROMaddr -               \ offset for ROM start address
  40.         2dup +  topaddr max   !> topaddr    \ record highest target address
  41.         pad +                   \ address of a byte in the image at pad
  42.         2 +!> sadr  -2 +!> scnt \ skip next byte
  43.         swap 0 ?do      nbyte
  44.                         over i +  c!    \ move bytes into target image
  45.                         loop
  46. \               ( ignore the checksum )
  47.         drop ;
  48.  
  49.  
  50. : read-in ( -- ) \ read the HEX file and download to target
  51.         cr ." Reading " seqhandle count type
  52.         seqhandle endfile FLEN 2!
  53.         0 0 seqhandle movepointer ibreset  \ reset to beginning of file
  54.         begin
  55.                 lineread count ( a+1 c )             \ read a line
  56.                 dup FLEN 2@ rot 0 d-
  57.                 lastshow 0<
  58.                 if  at? 2over 7 d.r at 2 =: lastshow  then  decr> lastshow
  59.                 FLEN 2!                              \ show bytes remaining
  60.                 2dup 0<>  swap 1+ @ $3030 <>  and while
  61.                 over c@ ':' = if line<hex else 2drop then \ download line
  62.         repeat
  63.         2drop   7 spaces cr ;
  64.  
  65.  
  66. : hexcom ( -- ) \ read the .HEX file
  67.                 \ and translate it into a target image in a .com file
  68.         off> topaddr
  69.         seqhandle hclose drop
  70.         bl word seqhandle $>handle
  71.         " hex" ">$ seqhandle $>ext
  72.         read-only  seqhandle hopen abort" cannot open .HEX file"
  73.         read-in                 \ read and make image for high bytes
  74.         seqhandle hclose drop
  75.         " COM" ">$ seqhandle $>ext     \ make .com image file
  76.         seqhandle hcreate abort" cannot create .COM"
  77.         pad topaddr 1+ 2* seqhandle hwrite
  78.         topaddr 1+ 2* - .
  79.         bye
  80. \        cr ." enter BYE to exit" cr
  81.         ;
  82.  
  83. save-exe hexcom
  84.  
  85.