DECLARE SUB ReadFile2Buffer (file$, pblock AS INTEGER)
'----------------------------------------------------------------
' file: com11.bas
' description:  This basic program is the interface to the
'               Embedded Micro Software CANTEC11 project providing the
'               capability to download and execute programs.
' last update:  November 11, 1997 rpr
'----------------------------------------------------------------
DECLARE SUB SendCommand (trasmit$, bytesback AS INTEGER, receive$)
DECLARE SUB ReadRAM (addr AS INTEGER, l AS INTEGER, RAM$)
DECLARE SUB WriteRAM (addr AS INTEGER, l AS INTEGER, d AS INTEGER, RAM$)
DECLARE SUB ReadByte (addr AS INTEGER, RAM$)
DECLARE SUB WriteByte (addr AS INTEGER, d AS INTEGER, RAM$)
DECLARE SUB FillRAM (addr AS INTEGER, l AS INTEGER, d AS INTEGER, RAM$)
DECLARE SUB PrintRAM (addr AS INTEGER, RAM$)
DECLARE SUB program (addr AS INTEGER, v AS INTEGER)
DECLARE SUB DownloadFile (file$)
DEFINT A-Z


        addr = 0
'----------------------------------------------
' main program starts here
'----------------------------------------------
        CLS : PRINT : PRINT
        PRINT "*****************************************"
        PRINT "*      EMICROS CANTEC11 Interface       *"
        PRINT "*****************************************"
        PRINT "* Version 1.3, April 18, 1999 - Fixed   *"
        PRINT "* boundary error.                       *"
        PRINT "* Version 1.2, March 11, 1999 - Fixed   *"
        PRINT "* error processing 8000 to dfff blocks. *"
        PRINT "*****************************************"
       
   cport = 0
   WHILE (cport < 1) OR (cport > 4)
      INPUT "Enter comm port (1,2,3,4): "; cport
   WEND
'----------------------------------------------
' execute the bootloader program that downloads
' the 68HC11 bootload program to the CANTEC11.
'----------------------------------------------
   s$ = ""
   INPUT "Do you want to execute bootloader (enter=no) "; s$
   IF s$ <> "" THEN
      GOSUB boot11
'      SHELL "boot11"
   END IF
'----------------------------------------------
' Bootload complete. Open communications and
' prompt for operator input.
'----------------------------------------------
   IF cport = 1 THEN OPEN "COM1:9600,N,8,1,CS,DS,CD" FOR RANDOM AS #4
   IF cport = 2 THEN OPEN "COM2:9600,N,8,1,CS,DS,CD" FOR RANDOM AS #4
   IF cport = 3 THEN OPEN "COM3:9600,N,8,1,CS,DS,CD" FOR RANDOM AS #4
   IF cport = 4 THEN OPEN "COM4:9600,N,8,1,CS,DS,CD" FOR RANDOM AS #4
          
   ak$ = ""
   WHILE ak$ <> "E"
      '-----------------------------------------
      ' empty the receive buffer
      '-----------------------------------------
      x = LOC(4)
      PRINT "Receive buffer contains "; x; " characters."
      IF x > 0 THEN
         r$ = INPUT$(x, #4)
         PRINT LEN(r$)
      END IF
      PRINT
      PRINT "*********************************************"
      PRINT "* EMICROS CANTEC11 Interface Program        *"
      PRINT "*********************************************"
      PRINT "D = Download file"
      PRINT "R = Read location"
      PRINT "E = Exit COM11 program"
      PRINT
      PRINT "Press D or E: ";
      PRINT
      ak$ = UCASE$(INPUT$(1))
     
      IF ak$ = "T" THEN
         PRINT #4, "T";
      END IF
     
      IF ak$ = "F" THEN
         t$ = "f" + CHR$(&H80) + CHR$(0)
         CALL SendCommand(t$, 1, r$)
         PRINT "Len = "; LEN(r$)
      END IF
     
      IF ak$ = "D" THEN
         INPUT "Enter file name to send: "; file$
         PRINT "1 = Program from 0xE000 to 0xFFFF"
         PRINT "2 = Program from 0xC000 to 0xFFFF"
         PRINT "3 = Program from 0xA000 to 0xFFFF"
         PRINT "4 = Program from 0x8000 to 0xFFFF";
         INPUT pblock
         CALL ReadFile2Buffer(file$, pblock)
      END IF
    
   
      IF ak$ = "A" THEN
         msb = &H10
         lsb = &H3F
         t$ = "A" + CHR$(msb) + CHR$(lsb) + CHR$(&HD)
         CALL SendCommand(t$, 1, r$)
         PRINT "Len = "; LEN(r$)
      END IF

   
      IF ak$ = "W" THEN
         b = &H6000
         msb = (b / &H100) AND &HFF
         lsb = b AND &HFF
         l = 64
         t$ = "w" + CHR$(msb) + CHR$(lsb) + CHR$(l)
         FOR x = 0 TO 63
            t$ = t$ + CHR$(&HAD)
         NEXT x
         CALL SendCommand(t$, 1, r$)
         PRINT "Len = "; LEN(r$)
      END IF

      IF ak$ = "R" THEN
         PRINT "Enter start address in hex: ";
         INPUT s$
         b = VAL("&h" + s$)
         msb = (b AND &HFF00) / &H100
         msb = msb AND &HFF
         lsb = b AND &HFF
         l = 64
         PRINT HEX$(msb), HEX$(lsb)
         t$ = "R" + CHR$(msb) + CHR$(lsb) + CHR$(l)
         CALL SendCommand(t$, l, r$)
         PRINT "Len = "; LEN(r$)
         col = 0
         FOR x = 1 TO LEN(r$)
            IF col = 0 THEN
               PRINT
               FOR y = 1 TO 4 - LEN(HEX$(b))
                  PRINT "0";
               NEXT y
               PRINT HEX$(b); "...";
            END IF
            a$ = HEX$(ASC(MID$(r$, x, 1)))
            IF LEN(a$) = 1 THEN PRINT "0";
            PRINT a$; " ";
            col = col + 1
            IF col >= 16 THEN
               col = 0
               b = b + 16
            END IF
         NEXT x
         PRINT
      END IF
      

   WEND
   PRINT "*********************************************"
   PRINT "* COM11 Interface Program Terminated        *"
   PRINT "* For Embedded Systems Software/Hardware    *"
   PRINT "* contact ";
   COLOR 10: PRINT "EMICROS - Embedded Micro Software"; : COLOR 7
   PRINT " *"
   PRINT "* Email; ";
   COLOR 10: PRINT " info@emicros.com"; : COLOR 7
   PRINT "                  *"
   PRINT "*********************************************"
   PRINT
   SLEEP 1
END

'----------------------------------------------------------------
' file: boot11.bas
' description:  This basic program downloads the bootloader
'               program to the AJR Circuits DEV11 project.
' last update:  January 8, 1996 rpr
'----------------------------------------------------------------
boot11: DIM RAMLoadByte(300)
        DIM GoodBoard(10)
        Debug = 0
        HexConvert$ = "0123456789ABCDEF"
        ComPort = 1
        ON ERROR GOTO ErrorTrap
     
        IF LEN(COMMAND$) > 0 THEN
           Filename$ = COMMAND$
        ELSE
           Filename$ = "boot.s19"
        END IF

        OptionChoice$ = "5"
      
        OptionChoice = VAL(OptionChoice$)
        NoBoards = 1 '' 8
        PartType$ = "E9"
        RAMAddressLow$ = "0000": RAMAddressHigh$ = "01FF"
        EEPromAddressLow$ = "B600": EEPromAddress$ = "B7FF"
        EPromAddressLow$ = "D000": EPromAddressHigh$ = "FFFF"
        MSBBootAddress$ = "BF": LSBBootAddress$ = "4D"
        PRINT "Place CANTEC11 board in bootloader mode."
        PRINT "    1. Set switch to bootload mode"
        PRINT "    2. Press and release reset button"
        PRINT "    3. Hit any key to continue."
        PRINT
        INPUT a$
        GOSUB TestWithRAM
PickMainOption:
        CLOSE
        RETURN
'       end

TestWithRAM:
        BootLoad = 1: FirstRecordSent = 0: BlankCheck = 0
        CheckSum = 0: Verify = 0
        CLS
        LOCATE 1, 1
        PRINT "Downloading file '"; Filename$; "' into RAM."
        GOSUB ConvertFile4RAM
        GOSUB RAMSendFile
        RETURN

'*************************************************************************
' Convert from S-records for fast download to RAM using Bootloader       *
'*************************************************************************
ConvertFile4RAM:
        RAMLoadByte$ = CHR$(255)
        RecordCount = 0
        OPEN Filename$ FOR INPUT AS #2
RAMNoS1Records:
        INPUT #2, record$
        IF LEFT$(record$, 2) = "S0" THEN GOTO RAMNoS1Records
        IF LEFT$(record$, 2) = "S1" THEN
           record$ = RIGHT$(record$, LEN(record$) - 6)
           RecordCount = RecordCount + 1
NextConvertByte:
           record$ = RIGHT$(record$, LEN(record$) - 2)
           IF LEN(record$) > 2 THEN
              ConvertByte$ = LEFT$(record$, 2)
              Nibble$ = LEFT$(ConvertByte$, 1)
              Nibble = INSTR(HexConvert$, Nibble$) - 1
              IF Nibble = -1 THEN
                 CLS
                 PRINT "Error converting number1 to hex"
                 INPUT CR$
                 RETURN
              END IF
              MSBNibble = Nibble * 16
              Nibble$ = RIGHT$(ConvertByte$, 1)
              Nibble = INSTR(HexConvert$, Nibble$) - 1
              IF Nibble = -1 THEN
                 CLS
                 PRINT "Error converting number1 to hex"
                 INPUT CR$
                 RETURN
              END IF
              value = MSBNibble + Nibble
              RAMLoadByte$ = RAMLoadByte$ + CHR$(value)
              GOTO NextConvertByte
            END IF
            GOTO RAMNoS1Records
        END IF
        IF LEFT$(record$, 2) = "S9" THEN
           IF RecordCount = 0 THEN
              CLOSE
              CLS
              LOCATE 12, 1
              PRINT "There are no S1 records in this file."
              INPUT "Hit enter to continue"; CR$
              RETURN PickMainOption
           ELSE
              CLOSE
              'cls
              RETURN
           END IF
        END IF
        CLOSE
        CLS
        LOCATE 12, 1
        PRINT "This file contains one or more none S19 records."
        INPUT "Hit enter to continue"; CR$
        RETURN PickMainOption

'***************************************************************
' This routine programs RAM using bootloader                   *
'***************************************************************
RAMSendFile:
   PRINT "Sending boot.s19 file to HC11 RAM."
   I = 0
   IF cport = 1 THEN OPEN "COM1:1200,N,8,1,CS,DS,CD" FOR RANDOM AS #1
   IF cport = 2 THEN OPEN "COM2:1200,N,8,1,CS,DS,CD" FOR RANDOM AS #1
   IF cport = 3 THEN OPEN "COM3:1200,N,8,1,CS,DS,CD" FOR RANDOM AS #1
   IF cport = 4 THEN OPEN "COM4:1200,N,8,1,CS,DS,CD" FOR RANDOM AS #1
   PRINT #1, RAMLoadByte$;
   PRINT "Total bytes sent = "; LEN(RAMLoadByte$)
   a$ = ""
   ll = POS(0)
   yy = CSRLIN
   DO
      LOCATE yy: PRINT LOC(1)
   LOOP WHILE (INKEY$ = "") AND (LOC(1) < (LEN(RAMLoadByte$) - 1))
   CLOSE
   RETURN
'********************************************************
' Converts Val1 back to Hex                             *
'********************************************************
Back2Hex:
High = INT(VAL1 \ 16)
High$ = HEX$(High)
Low = VAL1 - (High * 16)
Low$ = HEX$(Low)
Byte$ = High$ + Low$
RETURN

'*
'*
ErrorTrap:
CLS
PRINT "File Name does not exist."
PRINT
PRINT "Enter another file name or"
INPUT "Reset HC11 then enter RETURN to return to main menu."; Filename$
IF Filename$ = "" THEN
   RESUME PickMainOption
ELSE
   RESUME
END IF
'*
END

DEFSNG A-Z
SUB DownloadFile (file$)
'
' inputs:       Comm port opened as #4
'
   hexstr$ = "0123456789ABCDEF"
   CLS
   PRINT
   CLOSE #1
   OPEN file$ FOR INPUT AS #1
   recordno = 0
   CLS

   WHILE EOF(1) <> -1
nextline:
      LINE INPUT #1, a$
      recordno = recordno + 1
      LOCATE 10, 10
      PRINT "Downloading record #"; recordno

      ' record read, check for "S" as first character
     
      IF MID$(a$, 1, 1) <> "S" GOTO BADS
     
      ' check the record type for 0, 1 or 9, if 9 then end, error if not

      recordtype = VAL(MID$(a$, 2, 1))
      'PRINT "Record type = "; recordtype
      IF recordtype = 0 THEN
         'PRINT "Header.........";
      ELSEIF recordtype = 1 THEN
         'PRINT "Data record....";
      ELSEIF recordtype = 9 THEN
         'PRINT "Terminator.....";
         GOTO termfound
      ELSE GOTO BADTYPE
      END IF
      'PRINT a$
      IF recordtype = 1 THEN

         ' get the number of bytes in the record, record value in hex

         m1 = INSTR(hexstr$, MID$(a$, 3, 1)) - 1
         m2 = INSTR(hexstr$, MID$(a$, 4, 1)) - 1
         nobytes = m1 * &H10 + m2

         ' get the address

         addr$ = MID$(a$, 5, 4)
         m1 = INSTR(hexstr$, LEFT$(addr$, 1)) - 1
         m2 = INSTR(hexstr$, MID$(addr$, 2, 1)) - 1
         m3 = INSTR(hexstr$, MID$(addr$, 3, 1)) - 1
         m4 = INSTR(hexstr$, RIGHT$(addr$, 1)) - 1
         IF m1 = -1 OR m2 = -1 OR m3 = -1 OR m4 = -1 THEN
            error$ = "Illegal character in address."
            GOTO showerror
         END IF
         addr = m1 * &H1000 + m2 * &H100 + m3 * &H10 + m4
         LOCATE 11, 10
         PRINT "Address="; HEX$(addr)
       
         IF addr < &H200 THEN
            PRINT "RAM address ignored."
            GOTO nextline
         END IF

         'PRINT "No="; HEX$(nobytes); " Addr="; HEX$(addr); "...........";
         send$ = ""
         msb = (m1 * &H10 + m2) AND &HFF
         lsb = (m3 * &H10 + m4) AND &HFF
         send$ = "w" + CHR$(msb) + CHR$(lsb) + CHR$(nobytes - 3)
         csum = 0
         FOR x = 3 TO 7 STEP 2
            m1 = INSTR(hexstr$, MID$(a$, x, 1)) - 1
            m2 = INSTR(hexstr$, MID$(a$, x + 1, 1)) - 1
            dta = m1 * &H10 + m2
            csum = csum + dta
         NEXT x

         FOR x = 9 TO nobytes * 2 + 2 STEP 2
            m1 = INSTR(hexstr$, MID$(a$, x, 1)) - 1
            m2 = INSTR(hexstr$, MID$(a$, x + 1, 1)) - 1
            dta = m1 * &H10 + m2
            csum = csum + dta
            send$ = send$ + CHR$(dta)
            'IF LEN(HEX$(dta)) = 1 THEN PRINT "0";
            'PRINT HEX$(dta);
         NEXT x
         csum = -1 - csum AND &HFF
         'PRINT "..csum="; HEX$(csum)
         'PRINT

         ' check the checksum
          
         m1 = INSTR(hexstr$, MID$(a$, x, 1)) - 1
         m2 = INSTR(hexstr$, MID$(a$, x + 1, 1)) - 1
         dta = m1 * &H10 + m2
         IF csum <> dta THEN
            error$ = "Invalid checksum"
            GOTO showerror
         END IF
         'PRINT "Checksum valid"

         'FOR x = 1 TO LEN(send$)
            'm1 = ASC(MID$(send$, x, 1))
            'IF LEN(HEX$(m1)) = 1 THEN PRINT "0";
            'PRINT HEX$(m1);
         'NEXT x
         'PRINT
           PRINT #4, send$;
           WHILE LOC(4) < LEN(send$) + 1
              'PRINT ".";
           WEND
           zin$ = INPUT$(LEN(send$) + 1, #4)
           zout$ = zin$
           PRINT "Characters transfered .. "; LEN(send$)
           PRINT "Characters received .... "; LEN(zin$)
      END IF
   WEND
   GOTO theend

BADS:
   PRINT
   PRINT "Bad record read. First character not an 'S'!"
   GOTO theend
BADTYPE:
   PRINT
   PRINT "Bad record type, not 0, 1 or 9!"
   GOTO theend

showerror:
   PRINT
   PRINT error$
   GOTO theend

termfound:
   LOCATE 10, 10
   PRINT "File download completed..."
theend:

END SUB

SUB ReadFile2Buffer (file$, pblock AS INTEGER)
 
DIM bufE(&H2000)
DIM bufC(&H2000)
DIM bufA(&H2000)
DIM buf8(&H2000)

   PRINT "Clearing Buffers..."
   FOR x = 0 TO &H1FFF
      bufE(x) = &HFF
      bufC(x) = &HFF
      bufA(x) = &HFF
      buf8(x) = &HFF
   NEXT x
   hexstr$ = "0123456789ABCDEF"
   CLS
   PRINT
   CLOSE #1
   OPEN file$ FOR INPUT AS #1
   recordno = 0
   CLS

   WHILE EOF(1) <> -1
znextline:

      LINE INPUT #1, a$
      recordno = recordno + 1
      LOCATE 10, 10
      PRINT "#"; recordno; ".."; a$

      ' record read, check for "S" as first character
    
      IF MID$(a$, 1, 1) <> "S" GOTO zBADS
    
      ' check the record type for 0, 1 or 9, if 9 then end, error if not

      recordtype = VAL(MID$(a$, 2, 1))
      'PRINT "Record type = "; recordtype
      IF recordtype = 0 THEN
         'PRINT "Header.........";
      ELSEIF recordtype = 1 THEN
         'PRINT "Data record....";
      ELSEIF recordtype = 9 THEN
         'PRINT "Terminator.....";
         GOTO ztermfound
      ELSE GOTO zBADTYPE
      END IF
      'PRINT a$
      IF recordtype = 1 THEN

         ' get the number of bytes in the record, record value in hex

         m1 = INSTR(hexstr$, MID$(a$, 3, 1)) - 1
         m2 = INSTR(hexstr$, MID$(a$, 4, 1)) - 1
         nobytes = m1 * &H10 + m2

         ' get the address

         addr$ = MID$(a$, 5, 4)
         m1 = INSTR(hexstr$, LEFT$(addr$, 1)) - 1
         m2 = INSTR(hexstr$, MID$(addr$, 2, 1)) - 1
         m3 = INSTR(hexstr$, MID$(addr$, 3, 1)) - 1
         m4 = INSTR(hexstr$, RIGHT$(addr$, 1)) - 1
         IF m1 = -1 OR m2 = -1 OR m3 = -1 OR m4 = -1 THEN
            error$ = "Illegal character in address."
            GOTO zshowerror
         END IF
         msb = (m1 * &H10 + m2) AND &HFF
         lsb = (m3 * &H10 + m4) AND &HFF
         addr = msb * &H100 + lsb
         LOCATE 11, 10
         PRINT "Address="; HEX$(addr)
      
         IF addr < &H200 THEN
            PRINT "RAM address ignored."
            GOTO znextline
         END IF

         'PRINT "No="; HEX$(nobytes); " Addr="; HEX$(addr); "...........";
         send$ = ""
         csum = 0
         FOR x = 3 TO 7 STEP 2
            m1 = INSTR(hexstr$, MID$(a$, x, 1)) - 1
            m2 = INSTR(hexstr$, MID$(a$, x + 1, 1)) - 1
            dta = m1 * &H10 + m2
            csum = csum + dta
         NEXT x

         FOR x = 9 TO nobytes * 2 + 2 STEP 2
            m1 = INSTR(hexstr$, MID$(a$, x, 1)) - 1
            m2 = INSTR(hexstr$, MID$(a$, x + 1, 1)) - 1
            dta = m1 * &H10 + m2
            csum = csum + dta
            send$ = send$ + CHR$(dta)
            IF msb >= &HE0 THEN
               bufE(addr AND &H1FFF) = dta
               GOTO rt
            END IF
            IF msb >= &HC0 THEN
               bufC(addr AND &H1FFF) = dta
               GOTO rt
            END IF
            IF msb >= &HA0 THEN
               bufA(addr AND &H1FFF) = dta
               GOTO rt
            END IF
            IF msb >= &H80 THEN
               buf8(addr AND &H1FFF) = dta
            END IF
rt:
'            IF addr >= &HE000 THEN
'               bufE(addr AND &H1FFF) = dta
'            ELSEIF addr >= &HC000 THEN
'               bufC(addr AND &H1FFF) = dta
'            ELSEIF addr >= &HA000 THEN
'               bufA(addr AND &H1FFF) = dta
'            ELSEIF addr >= &H8000 THEN
'               buf8(addr AND &H1FFF) = dta
'            END IF
            addr = addr + 1
            lsb = lsb + 1
            IF lsb = &H100 THEN
               lsb = 0
               msb = msb + 1
            END IF
         NEXT x
         csum = -1 - csum AND &HFF
         'PRINT "..csum="; HEX$(csum)
         'PRINT

         ' check the checksum
         
         m1 = INSTR(hexstr$, MID$(a$, x, 1)) - 1
         m2 = INSTR(hexstr$, MID$(a$, x + 1, 1)) - 1
         dta = m1 * &H10 + m2
         IF csum <> dta THEN
            error$ = "Invalid checksum"
            GOTO zshowerror
         END IF
         'PRINT "Checksum valid"

         'FOR x = 1 TO LEN(send$)
            'm1 = ASC(MID$(send$, x, 1))
            'IF LEN(HEX$(m1)) = 1 THEN PRINT "0";
            'PRINT HEX$(m1);
         'NEXT x
         'PRINT
'           PRINT #4, send$;
'           WHILE LOC(4) < LEN(send$) + 1
'              'PRINT ".";
'           WEND
'           zin$ = INPUT$(LEN(send$) + 1, #4)
'           zout$ = zin$
'           PRINT "Characters transfered .. "; LEN(send$)
'           PRINT "Characters received .... "; LEN(zin$)
      END IF
   WEND
   GOTO ztheend

zBADS:
   PRINT
   PRINT "Bad record read. First character not an 'S'!"
   GOTO ztheend
zBADTYPE:
   PRINT
   PRINT "Bad record type, not 0, 1 or 9!"
   GOTO ztheend

zshowerror:
   PRINT
   PRINT error$
   GOTO ztheend

ztermfound:
   LOCATE 10, 10
   PRINT "File download completed..."
   y = 0
   OPEN "out.txt" FOR OUTPUT AS #5
  
   IF pblock = 1 GOTO be000
   IF pblock = 2 GOTO bc000
   IF pblock = 3 GOTO ba000

   '-------------------------------------------------
   ' print the block at 8000
   '-------------------------------------------------
   FOR x = 0 TO &H1FFF
      IF y = 0 THEN
         p$ = HEX$((&H8000 + x) AND &HFFFF) + "..."
      END IF
      IF LEN(HEX$(buf8(x))) = 1 THEN p$ = p$ + "0"
      p$ = p$ + HEX$(buf8(x)) + " "
      y = y + 1
      IF y = 16 THEN
         y = 0
         PRINT p$
         PRINT #5, p$
      END IF
   NEXT x
ba000:
   '-------------------------------------------------
   ' print the block at a000
   '-------------------------------------------------
   FOR x = 0 TO &H1FFF
      IF y = 0 THEN
         p$ = HEX$((&HA000 + x) AND &HFFFF) + "..."
      END IF
      IF LEN(HEX$(bufA(x))) = 1 THEN p$ = p$ + "0"
      p$ = p$ + HEX$(bufA(x)) + " "
      y = y + 1
      IF y = 16 THEN
         y = 0
         PRINT p$
         PRINT #5, p$
      END IF
   NEXT x
bc000:
   '-------------------------------------------------
   ' print the block at C000
   '-------------------------------------------------
   FOR x = 0 TO &H1FFF
      IF y = 0 THEN
         p$ = HEX$((&HC000 + x) AND &HFFFF) + "..."
      END IF
      IF LEN(HEX$(bufC(x))) = 1 THEN p$ = p$ + "0"
      p$ = p$ + HEX$(bufC(x)) + " "
      y = y + 1
      IF y = 16 THEN
         y = 0
         PRINT p$
         PRINT #5, p$
      END IF
   NEXT x
   '-------------------------------------------------
   ' print the block at E000
   '-------------------------------------------------
be000:
   FOR x = 0 TO &H1FFF
      IF y = 0 THEN
         p$ = HEX$((&HE000 + x) AND &HFFFF) + "..."
      END IF
      IF LEN(HEX$(bufE(x))) = 1 THEN p$ = p$ + "0"
      p$ = p$ + HEX$(bufE(x)) + " "
      y = y + 1
      IF y = 16 THEN
         y = 0
         PRINT p$
         PRINT #5, p$
      END IF
   NEXT x

  IF pblock = 1 GOTO pe000
  IF pblock = 2 GOTO pc000
  IF pblock = 3 GOTO pa000
  y = 0
  l = 64
  b = &H8000
  FOR x = 0 TO &H1FFF
     IF y = 0 THEN
        t$ = "w" + CHR$(&H60) + CHR$(0) + CHR$(l)
     END IF
     t$ = t$ + CHR$(buf8(x))
     y = y + 1
     IF y = 64 THEN
        y = 0
        CALL SendCommand(t$, 1, r$)
        msb = (b AND &HFF00) / &H100
        lsb = b AND &HFF
        t$ = "f" + CHR$(msb AND &HFF) + CHR$(lsb)
        PRINT "Programming block at address: "; RIGHT$(HEX$(b), 4)
        CALL SendCommand(t$, 1, r$)
        b = b + 64
     END IF
  NEXT x
pa000:
  y = 0
  l = 64
  b = &HA000
  FOR x = 0 TO &H1FFF
     IF y = 0 THEN
        t$ = "w" + CHR$(&H60) + CHR$(0) + CHR$(l)
     END IF
     t$ = t$ + CHR$(bufA(x))
     y = y + 1
     IF y = 64 THEN
        y = 0
        CALL SendCommand(t$, 1, r$)
        msb = (b AND &HFF00) / &H100
        lsb = b AND &HFF
        t$ = "f" + CHR$(msb AND &HFF) + CHR$(lsb)
        PRINT "Programming block at address: "; RIGHT$(HEX$(b), 4)
        CALL SendCommand(t$, 1, r$)
        b = b + 64
     END IF
  NEXT x
 
pc000:
  y = 0
  l = 64
  b = &HC000
  FOR x = 0 TO &H1FFF
     IF y = 0 THEN
        t$ = "w" + CHR$(&H60) + CHR$(0) + CHR$(l)
     END IF
     t$ = t$ + CHR$(bufC(x))
     y = y + 1
     IF y = 64 THEN
        y = 0
        CALL SendCommand(t$, 1, r$)
        msb = (b AND &HFF00) / &H100
        lsb = b AND &HFF
        t$ = "f" + CHR$(msb AND &HFF) + CHR$(lsb)
        PRINT "Programming block at address: "; RIGHT$(HEX$(b), 4)
        CALL SendCommand(t$, 1, r$)
        b = b + 64
     END IF
  NEXT x

pe000:
  y = 0
  l = 64
  b = &HE000
  FOR x = 0 TO &H1FFF
     IF y = 0 THEN
        t$ = "w" + CHR$(&H60) + CHR$(0) + CHR$(l)
     END IF
     t$ = t$ + CHR$(bufE(x))
     y = y + 1
     IF y = 64 THEN
        y = 0
        CALL SendCommand(t$, 1, r$)
        msb = (b AND &HFF00) / &H100
        lsb = b AND &HFF
        t$ = "f" + CHR$(msb AND &HFF) + CHR$(lsb)
        PRINT "Programming block at address: "; RIGHT$(HEX$(b), 4)
        CALL SendCommand(t$, 1, r$)
        b = b + 64
     END IF
  NEXT x



   CLOSE #5
   PRINT
ztheend:


END SUB

SUB SendCommand (transmit$, bytesback AS INTEGER, receive$)
'==============================================================
' The bytes in transmit are sent to comm port opened as #4.
' The command is echoed by the CANTEC11.
' READ commands returns 'bytesback' number of bytes
'==============================================================
   receive$ = ""
  
   PRINT #4, transmit$;
'++++++++++++++++++++++++++++++++++++++++++
' wait for the echo of the send command
'++++++++++++++++++++++++++++++++++++++++++
   WHILE LOC(4) < LEN(transmit$)
'   PRINT LEN(transmit$), LOC(4)
   WEND
   receive$ = INPUT$(LEN(transmit$), #4)
'++++++++++++++++++++++++++++++++++++++++++
' wait for the bytes sent back from the module
'++++++++++++++++++++++++++++++++++++++++++
   WHILE LOC(4) < bytesback
      'PRINT LOC(4)
   WEND
   receive$ = INPUT$(LOC(4), #4)

END SUB

