'  cload
'
'  adopted from cload.c, which was written by Bill Bolton 76244,127
'    sysop of the Software Tools RCPM   Kenmore, Australia
'  Amigabasic implementation by Richard Webb 74005,1265
'
'  syntax:  the program is run from Amigabasic, and will prompt for the
'           input and output file names
'
'  input:   an ascii file in either of the two hex formats used by CIS,
'           checksum and no checksum -- the program recognizes which
'           type is being used
'
'  output:  a binary image of the file as it was originally uploaded to CIS
'
'  bugs:    - if a character is missing from a hex line (as from a phone
'             line dropout), the program quits with the error message
'             "Illegal function call" when it tries to "ASC" a null string;
'             the action is proper but not very informative
'           - it's SLOOWWWW -- but hopefully, you'll only need it once, to
'             get an Xmodem terminal program up and running.
'
'  version 1.0, 19 Jan 86

version$ = "1"
revision$ = "0"
working% = 10                    ' number of lines between progress reports
next.line% = working% * 32       ' number of progress chars/screen line


PRINT "Hex to binary converter for CIS downloads, ";version$;".";revision$
INPUT "Name of hex file to convert:  ",in.file$
INPUT "  Binary file to be created:  ",out.file$
PRINT

CALL convert(in.file$, out.file$)

END


 
SUB convert(hex.file$, bin.file$) STATIC
SHARED working%, next.line%

OPEN hex.file$ FOR INPUT AS #1 LEN = 10240
OPEN bin.file$ FOR OUTPUT AS #2 LEN = 10240

line.count% = 0
line.length% = -1    ' forces one trip thru the WHILE loop (no DO-WHILE!)

WHILE (line.length% <> 0)
  line.count% = line.count% + 1
  IF line.count% MOD working% = 0 THEN
    PRINT "*";    ' still alive
    IF line.count% MOD next.line% = 0 THEN
      PRINT
    END IF
  END IF
 
  INPUT #1, input.line$
  IF EOF(1) THEN
    CLOSE #1 : CLOSE #2
    PRINT : PRINT "Conversion completed " : BEEP
    END
  END IF
 
  in.string.pos% = 2                ' skip over leading colon
  CALL read.hex(line.length%,2,in.string.pos%,input.line$)
                                    ' get number of chars in this line
  FOR i% = 1 TO 3
    CALL read.hex(address%,2,in.string.pos%,input.line$)
    sum% = sum% + address%          ' add address to chechsum
  NEXT i%
 
  FOR i% = 1 TO line.length%          ' where the rubber meets the road
    CALL read.hex(char%,2,in.string.pos%,input.line$)
    char$ = CHR$(char%)
    PRINT #2, char$;
    sum% = sum% + char%
  NEXT i%

' end of line processing
  IF LEN(input.line$) > 2 * line.length% + 9 THEN     ' meaning room for
    CALL read.hex(char%,2,in.string.pos%,input.line$) ' the checksum byte
    sum% = ( (sum% + char% + line.length%) AND &HFF)  ' so this is the
    IF sum% <> 0 THEN                                 ' hex format with
      PRINT                                           ' checksums
      PRINT "Checksum error on line ";line.count%
      BEEP
    END IF
  END IF

WEND

END SUB



SUB read.hex(c%,qty%,posit%,in.line$) STATIC

partial% = 0
FOR i% = 1 TO qty%
  partial% = partial% * 16
  in.str$ = MID$(in.line$,posit%,1)
  posit% = posit% + 1
  in.val% = ASC(in.str$)
  IF in.val% < 58 THEN
    partial% = partial% + in.val% - 48
  ELSE
    partial% = partial% + in.val% - 55
  END IF
NEXT i%
c% = partial%

END SUB
