<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Fri, 02 Oct 1987 09:56:48 ULG
From   : <UZ32112%BLIULG12.BITNET@WISCVM.WISC.EDU>
Subject: Intel hex format

I am not sure this made through to the list, so I repost.

Here is an algorithm for Intel Hex files generation:

\ COMHEX: produce .HEX file out of .COM file

HEX

100 CONSTANT START \ input file starting address, 100 specific for CP/M & MSDOS
10 CONSTANT SLICE \ max number of bytes in one HEX line

VARIABLE CHECKSUM \ output record checksum
VARIABLE ADDRESS  \ running address

VARIABLE IFILE \ pointer to input file
VARIABLE OFILE \ pointer to output file

: ASK.FILENAME \ prompt-addr id-count pointer -- fname-addr fname-count fcba
   >R FCBSIZE RESERVE R@ !     \ get fcb storage, set pointer
   CR TYPE SPACE  QUERY "TOKEN \ issue prompt, input filename
   R> @ ;                      \ return filename and fcb address

: CLOSE.FILE \ pointer --
   @ CLOSE CLOSED?  FCBSIZE FREE ;

: PUT.CHAR \ char -- \ write one character to output file
   OFILE @ PUT ;

: PUT.BYTE \ byte -- \ write 2 hex digits, compute checksum
   DUP CHECKSUM -! 0 <# # # #> OFILE @ WRITE ;

: PUT.WORD \ word -- \ write 4 hex digits, compute checksum
   DUP -8 SHIFT PUT.BYTE   FF AND PUT.BYTE ;

: COMHEX \ main word
   HEX \ all stuff done in hexadecimal
   " INPUT FILE?:"  IFILE ASK.FILENAME OPENI OPEN? \ open input
   " OUTPUT FILE?:" OFILE ASK.FILENAME OPENO OPEN? \ and output
   CR
   START ADDRESS !            \ init address pointer
   BEGIN IFILE @ INDATA WHILE \ loop until at end-of-file
      ASCII : PUT.CHAR        \ record mark
      CHECKSUM OFF            \ new checksum for new record
      PAD SLICE IFILE @ READ  \ read up to SLICE bytes
      DUP PUT.BYTE            \ output byte count
      ADDRESS @ PUT.WORD      \ output bytes address
      0 PUT.BYTE              \ plus one more byte
      DUP ADDRESS +!          \ progress address
      PAD SWAP BOUNDS ?DO     \ loop for each byte read
         I C@ PUT.BYTE LOOP   \ and output it with checksum
      CHECKSUM @ PUT.BYTE     \ output checksum
      OFILE @ PUTEOR          \ put end-of-record mark
   REPEAT
   ASCII : PUT.CHAR  0 PUT.WORD \ add end-of-file record
    0 PUT.WORD  0 PUT.BYTE  OFILE @ PUTEOR
   OFILE @ PUTEOF             \ put end-of-file mark
   OFILE CLOSE.FILE
   IFILE CLOSE.FILE ;

DECIMAL
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>