home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Programming / Complete Applications / Telecom / MT Special 3 / XMODEM DOCS / WXMODEM DOC next >
Encoding:
Text File  |  1988-01-01  |  59.0 KB  |  1,281 lines  |  [TEXT/ttxt]

  1. . . . . .  23
  2.           7.2. BASIC Implementation of Bit Shift Method  . . . . . . . .  23
  3.           7.3. BASIC Implementation of the Table Method  . . . . . . . .  26
  4.  
  5.      8.   NOTES AND COMMENTS . . . . . . . . . . . . . . . . . . . . . .  28
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.           Xmodem, CRC Xmodem, WXmodem
  14.      June 20, 1986                                                 Page 3
  15.      ----------------------------------------------------------------------
  16.  
  17.      1.   PREFACE
  18.  
  19.      In the years that have past since Xmodem was first developed as a file
  20.      transfer protocol, many thousands of people have been involved in
  21.      finding reasonable ways to move data via asynchronous telephone commun-
  22.      ications.  I appreciate the opportunity that I have had to meet and
  23.      learn from many of these people.  There is nothing in this document
  24.      that did not actually come from someone else.  Indeed, whether it is
  25.      WXMODEM, X.PC, Synchronous dial-up X.25, SNA, ZMODEM, Blast, Kermit or
  26.      any other protocol that becomes the dominant dial-up file transfer
  27.      protocol for personal and home computers is just not important.  What
  28.      is important is that the public domain have a high speed file transfer
  29.      protocol that is reasonably popular and  commonly available for many
  30.      types of personal computers, for bulletin boards and for services such
  31.      as People/Link, Delphi, CompuServe, GEnie and The Source.
  32.  
  33.      Here are a few people that all of us should thank and I would espec-
  34.      ially like to recognize:
  35.  
  36.           Ward Christensen  Ward, a true pioneer in the microcomputer
  37.           communications area, is the author of the original Checksum
  38.           Xmodem protocol.  Thanks for reminding me to "keep it simple
  39.           stupid".
  40.  
  41.           Chuck Forsberg  Chuck has edited perhaps the best work on
  42.           Xmodem and has provided both YMODEM (1K Xmodem) and ZMODEM
  43.           (Windowed YMODEM) to the public domain.  Thanks for showing
  44.           me a protocol which would deal with the X-On/X-Off problem
  45.           and reminding me that there is such a thing as a DLE char-
  46.           acter.
  47.  
  48.           Richard (Scott) McGinnis  Scott is the architect, the moving
  49.           force, for the People/Link software system.  His ideas,
  50.           comments and encouragement have been wonderful.  Wait until
  51.           you see his visual conference program for the IBM PC!
  52.           Thanks for showing me how to use a DLE.
  53.  
  54.           Gene Plantz  Gene operates a major IBM PC bulletin board in
  55.           the Chicago area and has been active in the National SYSOP
  56.           Association.  Thanks for pushing me to do something about
  57.           performance.
  58.  
  59.      In a historical perspective, there seems to be a common pattern in all
  60.      computer systems development that can shed some light on where we stand
  61.      and how we got here.  The pattern is function first, then integrity and
  62.      finally performance.
  63.  
  64.      Any kind of software must first do something worthwhile.  There is no
  65.      point in being error free, or inexpensive to operate if we do not want
  66.      the function.  Back in 1977, Ward Christensen had a need to move data
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.           Xmodem, CRC Xmodem, WXmodem
  74.      June 20, 1986                                                 Page 4
  75.      ----------------------------------------------------------------------
  76.  
  77.      between microcomputers.  Within a year it became obvious that the
  78.      function Xmodem provided met a real need to many microcomputer users.
  79.  
  80.      Once we have a new function and we accept it, there is a normal desire
  81.      for the function to be correct.  No one can't count the times that new
  82.      software users have pointed out ... "that new function is super, but
  83.      the results are wrong".  The effort changes from providing new function
  84.      to providing integrity.  The development of CRC Xmodem is a clear
  85.      response to the integrity phase of a service as it reduced undetected
  86.      transmission errors by many orders of magnitude.
  87.  
  88.      After the integrity has been accepted, people begin to look toward cost
  89.      and performance.  XMODEM entered this phase in 1984-1985.  Chuck
  90.      Forsberg's YMODEM is a major step in this effort providing larger
  91.      block sizes, batch mode and more.  His ZMODEM is a major step toward
  92.      making XMODEM derivative protocols work effectively with Public Data
  93.      Networks and most importantly, provides for restart of a file transfer
  94.      at the point of failure.  WXMODEM, presented here, is an alternate
  95.      solution to ZMODEM which is, hopefully, an easier solution to the most
  96.      important performance problems.
  97.  
  98.      No one really knows where XMODEM and the file transfer function will go
  99.      in the coming years.  Perhaps X.PC from Tymnet, MNP from Microcom or
  100.      Synchronous X.25 will slowly push XMODEM, et. al, into history.  I
  101.      think this will happen, but not for maybe 5 to 10 years.  Perhaps when
  102.      50% of the households outgrow the Commodore 64, or when modem manufac-
  103.      turers can provide a $50 synchronous modem we will see the beginning of
  104.      the end for XMODEM, but not today.
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.           Xmodem, CRC Xmodem, WXmodem
  112.      June 20, 1986                                                 Page 5
  113.      ----------------------------------------------------------------------
  114.  
  115.      2.   INTRODUCTION
  116.  
  117.      XMODEM and its derivatives have become the primary method for file
  118.      transfer for personal computers.  Hopefully this document will help
  119.      people to understand these protocols and to implement them on their
  120.      own.  In particular, this document presents an additional XMODEM
  121.      derivation to the public domain: WXMODEM.
  122.  
  123.      Why develop another file transfer protocol?
  124.  
  125.      After working with bulletin boards, Public Data Networks such as Tymnet
  126.      and Telenet, and commercial host systems such as People/Link, Delphi,
  127.      CompuServe and others, a number of people came to believe that hobby-
  128.      ist, home and business users would benefit significantly from a new,
  129.      conceptually simple file transfer protocol which would provide improved
  130.      performance and fully support the public data networks such as Tymnet,
  131.      Telenet and Datapac.
  132.  
  133.      But before WXMODEM can be presented, XMODEM and CRC XMODEM must be
  134.      described in detail.
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.           Xmodem, CRC Xmodem, WXmodem
  143.      June 20, 1986                                                 Page 6
  144.      ----------------------------------------------------------------------
  145.  
  146.      3.   TERMINOLOGY
  147.  
  148.      I've elected to use two special terms: transmitter and receiver.  The
  149.      transmitter is the computer/software which is transmitting data packets
  150.      and receiving acknowledgement characters.  The receiver is the com-
  151.      puter/software receiving the data packets and transmitting acknowledge-
  152.      ment characters.
  153.  
  154.      Here is a table of special ASCII characters that are used throughout
  155.      this paper:
  156.  
  157.           Name      Decimal        Hexadecimal    Description
  158.  
  159.           SOH          01           H001          Start Of Header
  160.           EOT          04           H004          End Of Transmission
  161.           ACK          06           H006          Acknowledge (positive)
  162.           DLE          16           H010          Data Link Escape
  163.           X-On (DC1)   17           H011          Transmit On
  164.           X-Off(DC3)   19           H013          Transmit Off
  165.           NAK          21           H015          Negative Acknowledge
  166.           SYN          22           H016          Synchronous idle
  167.           CAN          24           H018          Cancel
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.           Xmodem, CRC Xmodem, WXmodem
  176.      June 20, 1986                                                 Page 7
  177.      ----------------------------------------------------------------------
  178.  
  179.      4.   XMODEM
  180.  
  181.      Xmodem is a popular error recovery type protocol for transferring files
  182.      between computers via serial, asynchronous communications.   Before
  183.      learning more about Xmodem, it is important to hear what its author has
  184.      to say:
  185.  
  186.           "It was a quick hack I threw together, very unplanned (like
  187.           everything I do), to satisfy a personal need to communicate
  188.           with some other people.  ONLY the fact that it was done in
  189.           8/77, and that I put it in the public domain immediately,
  190.           made it become the standard that it is"....."People who
  191.           suggest I make SIGNIFICANT changes to the protocol, such as
  192.           'full duplex', 'multiple outstanding blocks', 'multiple
  193.           destinations', etc etc don't understand that the incredible
  194.           simplicity of the protocol is one of the reasons it survived
  195.           to this day in as many machines and programs as it may be
  196.           found in!"a
  197.  
  198.           4.1. Xmodem Hardware Level Protocol
  199.  
  200.           The protocol is Asynchronous, 8 data bits, no parity bit, one stop
  201.           bit.  Modems which are commonly used are AT&T 103 (300 baud), AT&T
  202.           212A (1200 baud) and CCITT V.22 (2400 baud).
  203.  
  204.           Typically, the data in a file is transmitted without change (if a
  205.           7 bit machine, the left most, high order, bit is always zero)
  206.           except that CP/M and MS/DOS operating systems want a ^Z (decimal
  207.           26) to represent end-of-file.
  208.  
  209.           4.2. Xmodem Initiation
  210.  
  211.           Prior to entering the protocol, both the transmitting and receiv-
  212.           ing computer must know where to get the data (what file is to be
  213.           transmitted) and where to put the data (file to store the data or
  214.           buffer area).  In Xmodem one side of the file transmission is
  215.           always in charge (local computer), asking the other side (remote
  216.           computer) to either transmit a file or to accept a file.  Through
  217.           a dialog outside of Xmodem the local computer (your PC) first
  218.           sends commands to the remote computer to select a file name
  219.           to prepare to transmit or receive a file via XMODEM.  Once this is
  220.           completed the remote computer enters the XMODEM protocol.  Now the
  221.           local computer must be told what file to transmit or receive and
  222.           it enters the XMODEM protocol, and hopefully data starts moving.
  223.  
  224.  
  225.           a    Ward Christensen, quoted from a message posted on CompuServe
  226.                in 1985.  Edited by Chuck Forsberg, "X/Ymodem Protocol
  227.                Reference", unpublished, 10/20/1985.
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.           Xmodem, CRC Xmodem, WXmodem
  235.      June 20, 1986                                                 Page 8
  236.      ----------------------------------------------------------------------
  237.  
  238.           Upon entering the Xmodem protocol, the transmitting computer waits
  239.           between 10 seconds and a minute to receive an NAK character from
  240.           the receiving computer.  The receiving computer is said to drive
  241.           the protocol.  The transmitter may retry any number of times.  If
  242.           any character other than a NAK or CAN is read by the transmitter,
  243.           it is ignored.  The CAN character implies cancellation of the
  244.           Xmodem file transfer and that the transmitter should leave the
  245.           Xmodem protocol.  Once the receiver has sent a NAK, it will wait
  246.           10 seconds for data to begin to arrive.  If none arrives in 10
  247.           seconds, the receiver will send another NAK and continue to repeat
  248.           10 times at which point the receiver will leave the Xmodem
  249.           protocol (typically with a super cryptic error message such as
  250.           "aborted", "NAK retry maximum exceeded").
  251.  
  252.           Transmitter                        Receiver
  253.  
  254.           [wait for one minute]         <    [NAK]
  255.  
  256.           [begin block transmission]    >
  257.  
  258.           4.3. Xmodem Data Transmission
  259.  
  260.           The transmitter takes the data, divides it into 128, 8 bit byte
  261.           pieces and places it in an Xmodem Packet.
  262.  
  263.           The Xmodem Packet looks like this:
  264.  
  265.                [SOH] [seq] [cmpl [seq] [128 data bytes] [csum]
  266.  
  267.                SOH       Start of header character (decimal 1).
  268.  
  269.                seq       one byte sequence number which starts at 1, and
  270.                          increments by one until it reaches 255 and then
  271.                          wraps around to zero.
  272.  
  273.                cmpl seq  one byte 1's complement of seq.  This can be
  274.                          calculated as cmpl = 255 - (255 and seq) or using
  275.                          xor as cmpl = (255 and seq) xor 255.
  276.  
  277.                data      128, 8 bit bytes of data.  Note than when sending
  278.                          CP/M and MS/DOS files a ^Z (decimal 26) must be
  279.                          added to then end of the file.  If the last block
  280.                          of data is less than 128 bytes, the Xmodem packet
  281.                          must be padded with characters, usually ^Z's.
  282.  
  283.                csum      one byte sum of all of the data bytes where any
  284.                          overflow or carry is discarded immediately.  For
  285.                          example, if the first 3 bytes are 255, 5 and 6 the
  286.                          checksum after the first 3 bytes will be 10.
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.           Xmodem, CRC Xmodem, WXmodem
  295.      June 20, 1986                                                 Page 9
  296.      ----------------------------------------------------------------------
  297.  
  298.           Once Xmodem Initiation has completed, the transmitter sends the
  299.           first Xmodem packet and then waits.  After the receiver has the
  300.           full packet, it will compare its own checksum calculation with the
  301.           checksum that was sent by the transmitter.  If the checksums
  302.           match, the receiver will send an ACK.  If the checksums are
  303.           different, the receiver will send a NAK.
  304.  
  305.           After receiving an ACK the transmitter will send the next Xmodem
  306.           packet.  If a NAK is received, the transmitter will resend the
  307.           same XMODEM packet again.
  308.  
  309.           Once the transmitter has sent the last Xmodem packet and has
  310.           received an ACK, the transmitter will send an EOT and then wait
  311.           for a final ACK from the receiver before leaving the Xmodem
  312.           protocol.  When the receiver sees an EOT instead of an SOH (the
  313.           first character the next packet), the receiver transmits an ACK
  314.           character, closes its files and leaves the Xmodem protocol.
  315.  
  316.           Let's look at a three block file transfer:
  317.  
  318.                Transmitter                                  Receiver
  319.  
  320.                                              <<<<<          [NAK]
  321.                [SOH][001][255][...][csum]    >>>>>
  322.                                              <<<<<          [ACK]
  323.                [SOH][002][254][...][csum]    >>>>>
  324.                                              <<<<<          [ACK]
  325.                [SOH][003][253][...][csum]    >>>>>
  326.                                              <<<<<          [ACK]
  327.                [EOT]                         >>>>>
  328.                                              <<<<<          [ACK]
  329.  
  330.           Seems easy, right?  And it is, until something goes wrong.
  331.  
  332.           4.4. Xmodem Cancellation
  333.  
  334.           It has become a defacto standard that the receiver may cancel the
  335.           file transfer by sending a CAN character and then leaving the
  336.           protocol.  If the transmitter receives a CAN character when
  337.           expecting either a NAK or ACK, the transmitter is to termin-
  338.           ate and leave the protocol.  Likewise, if the receiver sees a CAN
  339.           when expecting an SOH (start of packet) it should terminate the
  340.           file transfer.  Many implementations now require two CAN char-
  341.           acters before recognizing a cancel condition.
  342.  
  343.           4.5. Xmodem Error Recovery and Timing
  344.  
  345.           Error detection and recovery are the primary purposes of the
  346.           Xmodem protocol.  The transmitter and receiver should continue to
  347.           retry until 10 errors in a row have occurred.  Some of the common
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.           Xmodem, CRC Xmodem, WXmodem
  355.      June 20, 1986                                                 Page 10
  356.      ----------------------------------------------------------------------
  357.  
  358.           error conditions are listed below:
  359.  
  360.                4.5.1.    Complement Error
  361.  
  362.                If the sequence number does not match the complement
  363.                sequence number, the packet must be discarded and a NAK
  364.                sent to the transmitter.
  365.  
  366.                4.5.2.    Duplicate packet condition
  367.  
  368.                If the sequence number is the same as the sequence
  369.                number of the last packet received, the packet should be
  370.                discarded and an ACK sent to the transmitter.
  371.  
  372.                4.5.3.    Out of sequence error
  373.  
  374.                If the sequence number matches the complement sequence
  375.                number and it is neither the expected sequence number
  376.                nor the last sequence number, the receiver should send
  377.                two CAN characters and leave the Xmodem protocol
  378.                (e. g. abort the file transfer).
  379.  
  380.                4.5.4.    Receive timeout errors
  381.  
  382.                When expecting data, if 10 seconds ever pass without
  383.                receipt of a character, the receiver should send another
  384.                NAK.  This should be repeated 10 times.  Some implement-
  385.                ations will timeout after 10 seconds waiting for the
  386.                first character of a packet, SOH, and then reduce the
  387.                timeout for characters in a packet.  The timeout should
  388.                not go below 5 seconds if the protocol is to be used
  389.                with public data networks.
  390.  
  391.                4.5.5.    Transmit timeout errors
  392.  
  393.                In the original protocol, the transmitter would wait 10
  394.                seconds for an ACK, NAK or CAN and then retransmit the
  395.                last Xmodem packet as if a NAK had been received.  Most
  396.                implementations either have the transmitter wait for a
  397.                very long time (30 seconds to a minute) and then
  398.                terminate the file transfer if an ACK, NAK or CAN has
  399.                not been receive or wait about 30 seconds and retransmit
  400.                the last packet.
  401.  
  402.                4.5.6.    Packet synchronization errors
  403.  
  404.                Since extraneous characters are frequently generated
  405.                when using asynchronous communications, the receiver
  406.                should not count on receiving exactly 132 characters for
  407.                each Xmodem packet.  One algorithm for re-synchroniz-
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.           Xmodem, CRC Xmodem, WXmodem
  415.      June 20, 1986                                                 Page 11
  416.      ----------------------------------------------------------------------
  417.  
  418.                ation goes as follows:
  419.  
  420.                     Assume that the checksum algorithm will cause re-trans-
  421.                     mission of Xmodem packets which contain extraneous
  422.                     characters.
  423.  
  424.                     If the character received when expecting the start of a
  425.                     packet is not a SOH then ignore the character and
  426.                     continue to search for a SOH.
  427.  
  428.                     Once a SOH is found, assume that the next two characters
  429.                     will be a valid sequence number and complement.  If they
  430.                     are complements then assume that the packet has begun.
  431.                     If they are not complements, continue to search for a
  432.                     SOH.
  433.  
  434.                     Send a NAK if a timeout occurs while attempting to
  435.                     re-synchronize (e.g. continue to process timeouts as
  436.                     described above).
  437.  
  438.                     If no re-synchronization occurs within 135 characters
  439.                     then send a NAK character and retry receiving the
  440.                     packet.
  441.  
  442.                4.5.7.    False EOT condition
  443.  
  444.                When the receiver sees an EOT (which was not sent by the
  445.                transmitter, but generated out of a communications error)
  446.                instead of a SOH character, the receiver assumes incorrectly
  447.                that the complete file has been transmitted.  This is
  448.                typically an unrecoverable error and it does occur especially
  449.                when the transmitting and receiving UARTs are clocked
  450.                slightly differently.  An algorithm to detect false EOT might
  451.                return a NAK for the first EOT received and only assume true
  452.                end of transmission after receiving two EOT's.
  453.  
  454.                     Transmitter                   Receiver
  455.  
  456.                     [last block .. ]    >>>>>
  457.                                         <<<<<     [ACK]
  458.                     [EOT]               >>>>>
  459.                                         <<<<<     [NAK]
  460.                     [EOT]               >>>>>
  461.                                         <<<<<     [ACK]
  462.  
  463.                Just in case the transmitter was not prepared to resend the
  464.                EOT, it might be wise to set the timeout to about 3 seconds
  465.                and retransmit the NAK up to 3 times and then issue a warning
  466.                message but assume end of transmission.
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.           Xmodem, CRC Xmodem, WXmodem
  475.      June 20, 1986                                                 Page 12
  476.      ----------------------------------------------------------------------
  477.  
  478.                4.5.8.    False CAN condition
  479.  
  480.                Some Xmodem implementations will terminate on a single CAN
  481.                character.  Occasionally a CAN character will be generated by
  482.                a communications error and if this occurs and is seen by the
  483.                receiver between packets or is ever seen by the transmitter,
  484.                the file transfer will be incorrectly canceled.  Many
  485.                implementations now require two CAN characters in a row
  486.                before assuming that the file transfer is to be aborted.
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.           Xmodem, CRC Xmodem, WXmodem
  495.      June 20, 1986                                                 Page 13
  496.      ----------------------------------------------------------------------
  497.  
  498.      5.   CRC XMODEM
  499.  
  500.      CRC Xmodem is very similar to Checksum Xmodem.  The protocol initiation
  501.      has changed and the 8 bit checksum has been replaced by a 16 bit CRC.
  502.      Only theses changes are presented.
  503.  
  504.      One of the earliest and most persistent problems with Xmodem were
  505.      transmission errors which were not caught by the checksum algorithm.
  506.      Assuming that there is no bias in asynchronous communications errors,
  507.      we would expect that 1 out of every 256 erroneous complete or oversized
  508.      Xmodem packets to have a valid checksum.  With the same assumption, if
  509.      the checksum were 16 bits, we would expect 1 out of every 65,536
  510.      erroneous complete or oversized packets would have a valid checksum.
  511.  
  512.           5.1. CRC Calculation Rules
  513.  
  514.           Considerable theoretical research has shown that a 16 bit cyclical
  515.           redundancy check character (CRC/16) will detect a much higher
  516.           percent of errors such that it would only allow 1 undetected
  517.           bit in error for every 10^14 bits transmitted.  That's 1 un-
  518.           detected error per 30 years of constant transmission at 1 mega-
  519.           bit per second.  However, my personal experience indicates that
  520.           something around 10^9 to 10^10 is more realistic.  Why such a vast
  521.           improvement over the checksum algorithm?  It is caused by the
  522.           unique properties that prime numbers have when being divided into
  523.           integers.  Simply stated, if an integer is divided by a prime
  524.           number, the remainder is unique.  The CRC/16 algorithm treats all
  525.           1024 data bits in an Xmodem packet as an integer, multiples that
  526.           integer by 2^16 and then divides that 1040 bit number by a 17 bit
  527.           prime number.  The low order 16 bits of the remainder becomes the
  528.           16 bit CRC.
  529.  
  530.           The 17 bit prime number in CRC Xmodem is 2^16 + 2^12 + 2^5 + 1 or
  531.           65536 + 4096 + 32 + 1 = 69665.  So calculating the CRC is simple,
  532.           just multiply the 128 byte data number by 65536, divide by 69665
  533.           and the low order 16 bits of the remainder are the CRC.  The only
  534.           problem is, I've never seen a computer which has instructions to
  535.           support 130 byte integer arithmetic!  Fortunately for us, Seephan
  536.           Satchell, Satchell Evaluations, published a specification a very
  537.           efficient algorithm to calculate the CRC without either 130 byte
  538.           arithmetic or bit manipulation.  Appendix A contains the source
  539.           code, in IBM/PC BASIC, for the calculation of a CRC.
  540.  
  541.           Other methods of calculating CRC's for Xmodem involve bit level
  542.           logic. a.
  543.  
  544.  
  545.           a    Chuck Forsberg, "X/Ymodem Protocol Reference", available on
  546.                many bulletin boards.
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.           Xmodem, CRC Xmodem, WXmodem
  554.      June 20, 1986                                                 Page 14
  555.      ----------------------------------------------------------------------
  556.  
  557.           5.2. CRC Xmodem Initiation
  558.  
  559.           The initiation of CRC Xmodem was designed to provide for automatic
  560.           fall back to Checksum Xmodem if the transmitter does not support
  561.           the CRC version.
  562.  
  563.           The receiver requests CRC Xmodem by sending the letter C (decimal
  564.           67) instead of a NAK.  If the transmitter supports CRC Xmodem, it
  565.           will begin transmission of the first Xmodem packet upon receipt of
  566.           the C.  If the transmitter does not support CRC Xmodem, it will
  567.           ignore the C.  The receiver should timeout after 3 seconds and
  568.           repeat sending the C.  After 3 timeouts, the receiver should fall
  569.           back to the checksum Xmodem protocol and send a NAK.
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.           Xmodem, CRC Xmodem, WXmodem
  579.      June 20, 1986                                                 Page 15
  580.      ----------------------------------------------------------------------
  581.  
  582.      6.   WINDOWED XMODEM (WXMODEM)a
  583.  
  584.      First, Xmodem provided the basic file transfer function, then CRC
  585.      Xmodem improved the data integrity, now we come to WXmodem which
  586.      provides better cost/performance.
  587.  
  588.           6.1. WXmodem Design Criteria
  589.  
  590.           A few people began discussing improvements to Xmodem with me in
  591.           late 1985, over time we developed the following criteria:
  592.  
  593.                6.1.1.    The protocol must be as similar as possible to the
  594.                          XMODEM originally developed by Ward Christensen.
  595.                          The popularity of XMODEM, I believe, is based on
  596.                          its conceptual simplicity.  More software writers
  597.                          are familiar with this protocol than any other.
  598.                          More files are transferred everyday by this
  599.                          protocol than any other asynchronous protocol.
  600.                          Simplicity here implies a limited number of rules
  601.                          for timing, error recovery and initiation.
  602.  
  603.                6.1.2.    The protocol must overcome the propagation delay
  604.                          that is characteristic of the public data net-
  605.                          works.  While the cost of long distance communi-
  606.                          cation is 50 to 90% less via the public data
  607.                          networks than via the public voice networks, the
  608.                          propagation delays inherent in public data networks
  609.                          both reduces the cost savings and increases the
  610.                          aggravation that occurs while watching a computer
  611.                          slowly perform a file transfer.
  612.  
  613.                6.1.3.    The protocol must overcome the flow control
  614.                          problems which are characteristic in many public
  615.                          data network situations.  Basically, in most
  616.                          situations, the X-On and X-Off characters must
  617.                          always be used for flow control and only for flow
  618.                          control when using public data networks.
  619.  
  620.                6.1.4.    The protocol should improve error recovery by
  621.                          simplifying the manner in which a programmer can
  622.                          determine the beginning of an XMODEM block.  Since
  623.                          the Start of Header character (SOH) can appear in
  624.                          the data or CRC, the programmer must use a rela-
  625.                          tively sophisticated method to determine if a SOH
  626.                          actually represents the beginning of a XMODEM
  627.                          block.
  628.  
  629.  
  630.  
  631.           a    This section assumes that the reader is already familiar with
  632.                Xmodem and CRC Xmodem presented above.
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.           Xmodem, CRC Xmodem, WXmodem
  640.      June 20, 1986                                                 Page 16
  641.      ----------------------------------------------------------------------
  642.  
  643.  
  644.           6.2. Transparency and Flow Control Rules (Byte Level Rules)
  645.  
  646.           This protocol provides special public data network support for
  647.           non-X.25 hosts and PC-Pursuit access to bulletin boards.  In order
  648.           to accomplish this, the transmitter is not permitted to transmit
  649.           the X-On and X-Off characters in the Xmodem packets.  The reason
  650.           for this restriction is simple:
  651.  
  652.                By the very nature of X.25 public data networks, without
  653.                flow control, buffer overruns and lost data are inevit-
  654.                able from time to time at any baud rate.
  655.  
  656.                To avoid data loss public data networks must always
  657.                assume that any X-Off and X-On character is a flow
  658.                control character when supporting PC-Pursuit for
  659.                bulletin boards and when supporting non-X.25 host
  660.                systems.
  661.  
  662.           Since many non-X.25 hosts, bulletin boards and communications
  663.           programs use X-On and X-Off as flow control characters, public
  664.           data networks must support those X-Off and X-On requests at the
  665.           point of connection where the X-Off is received by the public data
  666.           network.  Otherwise, as many as several hundred characters backed
  667.           up in the network would be transmitted by the public data network
  668.           before the X-Off used for flow control reached the transmitter.
  669.           The public data network has no way to know whether an X-On/X-Off
  670.           protocol or Xmodem is operational at any point in time.  Therefore
  671.           a Xmodem packet which contains an X-Off character and no succeed-
  672.           ing X-On character will cause the public data network to stop
  673.           forwarding the ACK or NAK.
  674.  
  675.           In addition, error recovery requires sophisticated programming for
  676.           the receiver to determine the start of an XMODEM packet.  This
  677.           protocol simplifies this task by dedicating a special character as
  678.           an indicator that an XMODEM packet is about to begin.  The
  679.           SYN (synch, decimal 22) character is used for this purpose.
  680.           The presence of one or more SYN characters in a data stream always
  681.           indicates that the next non SYN character is the beginning of an
  682.           XMODEM packet (e.g. SOH).
  683.  
  684.           The method used here to handle these situations is through the
  685.           insertion of the DLE character (H010, decimal 16, data link escape
  686.           character) as an indicator that the character following the DLE is
  687.           in fact a modified DLE, SYN, X-On, or X-Off character.
  688.  
  689.           Rules:
  690.  
  691.                6.2.1.    Whenever an X-On, X-Off, SYN or DLE character is
  692.                          about to be transmitted as any part of an actual
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.           Xmodem, CRC Xmodem, WXmodem
  700.      June 20, 1986                                                 Page 17
  701.      ----------------------------------------------------------------------
  702.  
  703.                          XMODEM packet including the CRC, the transmitter
  704.                          will transmit instead a DLE character followed by
  705.                          the original character which has been modified by
  706.                          exclusive or'ing it with 64 to its value. 1
  707.  
  708.                6.2.2.    The inserted DLE characters are not counted in the
  709.                          128 byte data length of the data portion of the
  710.                          XMODEM packet.  Indeed, it would be possible to
  711.                          have a packet which is physically 264 bytes in
  712.                          length because the Xmodem block sequence number
  713.                          (or its complement), all of the 128 data characters
  714.                          and two CRC characters are all either X-On, X-Off,
  715.                          SYN or DLE characters.
  716.  
  717.                6.2.3.    Neither the DLE nor the adjusted characters are
  718.                          used in the CRC calculation, rather the original
  719.                          character is always used in the CRC calculation.
  720.  
  721.  
  722.                6.2.4.    When the receiver sees a DLE character, it does not
  723.                          count it in the XMODEM block length calculation,
  724.                          nor compute it in the CRC calculation but discards
  725.                          it and then remembers to exclusive or the next
  726.                          character with 64 and to verify that the result
  727.                          character is either a DLE, SYN, X-On or X-Off (the
  728.                          receiver will reject the packet unconditionally, if
  729.                          not one of those four characters) and then include
  730.                          the character as part of the packet.
  731.  
  732.                6.2.5.    Prior to transmission of a XMODEM packet, the
  733.                          transmitter will send one or more SYN characters
  734.                          (recommend two) as a positive indicator to the
  735.                          receiver of the beginning of a Xmodem packet.2
  736.  
  737.                6.2.6.    Except for the character received after a DLE, the
  738.                          receiver will test each incoming character to see
  739.                          if it is a SYN character.  If it is, it will
  740.                          discard the character and assume that the next
  741.                          character will be another SYN or SOH.  If a SYN
  742.                          character is received in the middle of a packet,
  743.                          the receiver will NAK that packet.  The purpose of
  744.                          the SYN character is to simplify recognition of the
  745.                          beginning of a XMODEM packet by the receiver.  Once
  746.                          an out of synch condition occurs on incoming
  747.                          data, the receiver can just ignore every incoming
  748.                          character until it sees a SYN.  Existing XMODEM
  749.                          code which already properly deals with this
  750.                          situation could just always discard any SYN
  751.                          character at time of receipt with no further
  752.                          action.
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.           Xmodem, CRC Xmodem, WXmodem
  760.      June 20, 1986                                                 Page 18
  761.      ----------------------------------------------------------------------
  762.  
  763.  
  764.                6.2.7.    The transmitter must support flow control char-
  765.                          acters (X-On, and X-Off) during transmission of
  766.                          packets.  Upon receipt of an X-Off it will wait 10
  767.                          seconds for an X-On and will start transmission
  768.                          again after 10 seconds or an X-On is received,
  769.                          whichever occurs first.  Any extraneous X-On
  770.                          characters received by the transmitter will be
  771.                          ignored and discarded.  (Note that this does NOT
  772.                          apply to X.25 host computers which use X.25 L2 and
  773.                          L3 windows for flow control.)
  774.  
  775.           6.3. Initial Handshake Rules
  776.  
  777.           An initial handshake is provided to permit the receiver to
  778.           indicate to the transmitter whether it can support checksum
  779.      Xmodem, CRC Xmodem, or Windowed Xmodem:
  780.  
  781.                6.3.1.    WXMODEM - The receiver will send a character W
  782.                          (decimal 87) and wait 3 seconds for the beginning
  783.                          of a Xmodem packet.  This will be repeated 3 times
  784.                          and then the receiver will drop down to CRC Xmodem.
  785.  
  786.                6.3.2.    CRC XMODEM - The receiver will send a character C
  787.                          (decimal 67) and wait 3 seconds for the beginning
  788.                          of a Xmodem packet.  This will be repeated 3 times
  789.                          and then the receiver will drop down to Checksum
  790.                          Xmodem.
  791.  
  792.                6.3.3.    Checksum XMODEM -  The receivers will send a NAK
  793.                          and wait up to 3 seconds for the beginning of a
  794.                          Xmodem packet.  This will be repeated 4 times and
  795.                          if no valid SOH is received, the receiver will
  796.                          abort the file transfer request.
  797.  
  798.           6.4. Window Packet Transmission Rules
  799.  
  800.           In order to overcome the propagation delays inherent with public
  801.           data networks such as Tymnet, Telenet, Datapac, IPSS, Transpac and
  802.           dozens more, the protocol must permit the transmitter to send more
  803.           than one packet before receiving an acknowledgement from the
  804.           receiver.  The number of packets that the transmitter will send
  805.           before stopping transmission if an acknowledgement has not been
  806.           received is called the "window".  WXmodem uses a window of 4
  807.           packets for several reasons.  Most importantly, it uses a single
  808.           set of timing rules which would deal reasonably well with a wide
  809.           range of baud rates (that implied keeping the window fairly
  810.           small).  Secondly, the window sequence number is directly related
  811.           to the Xmodem packet sequence number which, hopefully, will
  812.           simplify implementation of windowing.
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.           Xmodem, CRC Xmodem, WXmodem
  820.      June 20, 1986                                                 Page 19
  821.      ----------------------------------------------------------------------
  822.  
  823.  
  824.           Rules:
  825.  
  826.                6.4.1.    The window is always 4 Xmodem packets.  That is,
  827.                          the transmitter will send 4 unacknowledged pack-
  828.                          ets.  Transmission will not cease and the time out
  829.                          interval will not begin until 4 unacknowledged
  830.                          packets have been transmitted.  Note that the
  831.                          window may be less than 4 Xmodem packets for short
  832.                          files or at end-of-file.
  833.  
  834.                6.4.2.    The receiver will transmit acknowledgements in the
  835.                          form:
  836.  
  837.                               ACK[sequence]
  838.  
  839.                          The [sequence] field is an 8 bit number where the
  840.                          high order or most significant 6 bits are always
  841.                          zero and the low order or least significant 2 bits
  842.                          are always the same as the low order 2 bits of the
  843.                          XMODEM block sequence number of the XMODEM packet
  844.                          being acknowledged (value in decimal may range
  845.                          from 0 to 3).
  846.  
  847.                6.4.3.    The receiver does not have to acknowledge every
  848.                          packet, but must acknowledge at minimum every
  849.                          fourth packet.  The transmitter will accept one
  850.                          ACK[sequence] for multiple XMODEM packets.  For
  851.                          example, after an unknown number of packets:
  852.  
  853.                          Transmitter                             Receiver
  854.  
  855.                          ....
  856.                          ....
  857.                          ....
  858.                          [Block Sequence Number H0FE]
  859.                          [Block Sequence Number H0FF]            ACK[H002]
  860.                          [Block Sequence Number H000]            ACK[H003]
  861.                          [Block Sequence Number H001]
  862.                          [Block Sequence Number H002]            ACK[H001]
  863.                          .....
  864.  
  865.                          Since some transmitters must close the window and
  866.                          cease all communications before doing disk I/O to
  867.                          read more data, it is suggested that acknowledge-
  868.                          ments be sent for every packet (except when the
  869.                          receiver can easily determine that another packet
  870.                          is already being received at the point in time that
  871.                          the ACK[sequence] is about to be sent).3
  872.  
  873.  
  874.  
  875.  
  876.  
  877.  
  878.  
  879.           Xmodem, CRC Xmodem, WXmodem
  880.      June 20, 1986                                                 Page 20
  881.      ----------------------------------------------------------------------
  882.  
  883.                6.4.4.    The receiver will reject a packet (request re-
  884.                          transmission) by sending:
  885.  
  886.                               NAK[sequence]
  887.  
  888.                          Where [sequence] is then next window sequence
  889.                          number (between H000 and H003) after the [sequence]
  890.                          of the last good block.  The receiver will discard
  891.                          up to 3 Xmodem packets received after the NAK is
  892.                          transmitted until it receives the packet with the
  893.                          sequence number that had previously been nak'ed by
  894.                          the receiver.  The receiver will not send a second
  895.                          NAK until another packet with the same sequence
  896.                          number is received which is also invalid or a
  897.                          timeout has occurred.
  898.  
  899.                6.4.5.    When the transmitter receives a NAK[sequence], it
  900.                          will complete transmission of any XMODEM block
  901.                          currently being transmitted and then begin re-
  902.                          transmission starting with the block which was
  903.                          nak'ed.
  904.  
  905.                6.4.6.    The receiver will discard duplicate packets but
  906.                          count them in the window for purposes of deter-
  907.                          mining the maximum receive window without an ACK in
  908.                          response.  For example, if the receiver gets packet
  909.                          sequence number 127 four times in a row, it must
  910.                          send an ACK H003 even if the receiver has previous-
  911.                          ly acked that block.
  912.  
  913.                6.4.7.    The timeout intervals at various points in process-
  914.                          ing are:
  915.  
  916.                          Waiting for a character on receive, start of packet
  917.                          not yet recognized:      15 seconds
  918.  
  919.                          Waiting for a character on receive, start of packet
  920.                          has been recognized:     15 seconds
  921.  
  922.                          Waiting for an Ack or Nak on transmit side after
  923.                          the window has closed:   15 seconds4
  924.  
  925.                          Waiting for an X-On after receipt of an X-Off by
  926.                          the transmitter:         10 seconds
  927.  
  928.                6.4.8.    When the transmitter times out waiting for an ACK
  929.                          or NAK when the window is closed (e.g. four blocks
  930.                          have been transmitted), the transmitter will
  931.                          retransmit the last block transmitted and wait
  932.                          again.  Only after 10 consecutive timeouts have
  933.  
  934.  
  935.  
  936.  
  937.  
  938.  
  939.           Xmodem, CRC Xmodem, WXmodem
  940.      June 20, 1986                                                 Page 21
  941.      ----------------------------------------------------------------------
  942.  
  943.                          occurred will the transmitter cancel the trans-
  944.                          mission.
  945.  
  946.                6.4.9.    Where possible, it is recommended that the receiver
  947.                          return an ACK[sequence] for every packet or at
  948.                          least 50% of the Xmodem packets.  When the receiver
  949.                          must wait for the window to close (e.g. receive 4
  950.                          Xmodem packets without an acknowledgement),
  951.                          some performance benefit will be lost.
  952.  
  953.           If the receiver cannot overlap disk I/O and communications
  954.           I/O, the receiver can temporarily stop transmission by either:
  955.  
  956.                "Closing the window" (e.g. receiving 4 blocks without sending
  957.                an ACK[sequence]) performing the disk I/O and then sending an
  958.                ACK[sequence].
  959.  
  960.                Transmitting an X-Off followed by an X-On when the receiver
  961.                is ready to resume accepting data.  Note that the receiver
  962.                should be prepared to accept data for about a 1/4 of a second
  963.                after the X-Off is sent to cover situations where satellite
  964.                propagation delay may occur.  One possible implementation
  965.                would let the computer user set the "X-Off delay time" so
  966.                that in the normal case the X-Off delay could be set to 25
  967.                milleseconds.  A sophisticated implementation might set the
  968.                initial X-Off delay at 250 milleseconds and then reduce it
  969.                based on experience during the file transfer.
  970.  
  971.                Each approach has its advantages, but the X-Off approach will
  972.                provide the best performance in most cases especially when
  973.                using a public data network.  Note, however, that some
  974.                computers, notably the Commodore 64 and the IBM PC Jr cannot
  975.                receive communications data while writing to disk.
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.           Xmodem, CRC Xmodem, WXmodem
  984.      June 20, 1986                                                 Page 22
  985.      ----------------------------------------------------------------------
  986.  
  987.           6.5. Notes for X.25 Hosts
  988.  
  989.           Host computer systems which utilize the X.25 protocol
  990.           (examples: People/Link, Delphi, CompuServe, The Source) to
  991.           interface with the various public data networks may send special
  992.           control packets which change the manner in which the network will
  993.           communicate with the remote personal computer, bulletin board or
  994.           terminal.  For the purposes of this paper, it is assumed that the
  995.           X.25 host can already support CRC and/or Checksum Xmodem and
  996.           present only the changes for WXMODEM.
  997.  
  998.                6.5.1.    When an X.25 Host is the transmitter, it must be
  999.                          sure to set the distant X.3 PAD parameters to
  1000.                          assure that the receiver can use X-Off/X-On for
  1001.                          flow control.  This is accomplished by sending a
  1002.                          Q-Bit command packet to set X.3 parameter 12 to a 1
  1003.                          prior to the initial handshake.  Note that if the
  1004.                          receiver cannot support WXMODEM, the X.25 Host must
  1005.                          send the appropriate Q-Bit packet to reset para-
  1006.                          meter 12 to a 0 before transmitting the first CRC
  1007.                          or Checksum Xmodem packet.
  1008.  
  1009.                6.5.2.    When an X.25 Host is the receiver and in WXMODEM
  1010.                          mode, it must be sure to set the distant X.3 PAD
  1011.                          parameters to assure that the network will use
  1012.                          X-Off/X-On for flow control between the network and
  1013.                          the transmitter to prevent its buffers from
  1014.                          overflowing.  This is accomplished by sending a
  1015.                          Q-Bit command packet to set X.3 parameter 5 to a 1
  1016.                          prior to the initial handshake.
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.           Xmodem, CRC Xmodem, WXmodem
  1025.      June 20, 1986                                                 Page 23
  1026.      ----------------------------------------------------------------------
  1027.  
  1028.      7.   APPENDIX A - CRC CALCULATION RULES
  1029.  
  1030.      The purpose of this appendix is to give non-technical and non mathema-
  1031.      tical software writers a cook book approach to calculating the CRC-16
  1032.      used in Xmodem.  We have half accomplished that goal.  The BASIC code
  1033.      in the examples below has been tested on an IBM PC and found to work
  1034.      effectively even at 9600 with compiled Basic.  Some BASIC languages do
  1035.      not offer an XOR function and others do not have MKI$ and CVI functions
  1036.      which simplified the movement of data between data types.  Someday we
  1037.      hope to provide a Commodore C-64/C-128 implementation which simulates
  1038.      XOR, but not today!
  1039.  
  1040.      My thanks go to Chuck Forsberg, Joe Noonan, John Byrns and Stephen
  1041.      Satchell.  Without their help and public domain documents, this would
  1042.      have never been possible.
  1043.  
  1044.           7.1. IBM PC - 8088/8086 Data Structure
  1045.  
  1046.           The Intel 8080 and upward has a feature, convenient only to some
  1047.           electrical engineer somewhere, which places 2 byte (16) bit
  1048.           integers in BYTE REVERSE order in memory.  That is, the least
  1049.           significant byte is placed in memory before the most significant
  1050.           byte for integer operations.  If A$ is one byte containing the
  1051.           number 52 and it is assigned to I% using the ASC function, the
  1052.           binary value (52) ends up in the first byte of I% and the second
  1053.           byte is zero.
  1054.                                    Result
  1055.  
  1056.                I%=0                [x'0000']
  1057.                I%=1                [x'0100']
  1058.                A$="A"              [x'41']
  1059.                I%=ASC(A$)          [x'4100']
  1060.                B$=MKI$(I%)         [x'4100']  letter "A" then binary zero
  1061.                I%=CVI(CHR$(0)+A$)  [x'0041']
  1062.                A$=CHR$(65)         [x'41']
  1063.  
  1064.           Once this is understood, many problems with these algorithms goes
  1065.           away.
  1066.  
  1067.           7.2. BASIC Implementation of Bit Shift Method
  1068.  
  1069.           The bit shift method here was converted from the "C" logic
  1070.           presented in Chuck Forsberg's "Xmodem/Ymodem" protocol reference
  1071.           and from an old IBM two page reference guide that Joe Noonan
  1072.           carries with him in his appointment calendar!
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.           Xmodem, CRC Xmodem, WXmodem
  1081.      June 20, 1986                                                 Page 24
  1082.      ----------------------------------------------------------------------
  1083.  
  1084.  
  1085.           Chucks' "C" code:
  1086.  
  1087.      /*
  1088.       * This function calculates the CRC used by the XMODEM/CRC Protocol
  1089.       * The first argument is a pointer to the message block.
  1090.       * The second argument is the number of bytes in the message block.
  1091.       * The function returns an integer which contains the CRC.
  1092.       * The low order 16 bits are the coefficients of the CRC.
  1093.       */
  1094.      int calcrc(ptr, count)
  1095.      char *ptr;
  1096.      int count;
  1097.      {
  1098.          int crc, i;
  1099.  
  1100.          crc = 0;
  1101.          while (--count >= 0) {
  1102.           crc = crc ^ (int)*ptr++ << 8;
  1103.           for (i = 0; i < 8; ++i)
  1104.               if (crc & 0x8000)
  1105.                crc = crc << 1 ^ 0x1021;
  1106.               else
  1107.                crc = crc << 1;
  1108.           }
  1109.          return (crc & 0xFFFF);
  1110.      }
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.           Xmodem, CRC Xmodem, WXmodem
  1119.      June 20, 1986                                                 Page 25
  1120.      ----------------------------------------------------------------------
  1121.  
  1122.  
  1123.           But in IBM PC BASIC, our implementation looks like:
  1124.  
  1125.      100 DEFINT A-Z 'DEFAULT IS TWO BYTE INTEGERS
  1126.      2000 REM * V$ CONTAINS 133 CHARACTER COMPLETE XMODEM PACKET
  1127.      2010 REM * CRC$ IS TWO BYTE CRC WITH MOST SIGNIFICANT BYTE FIRST
  1128.      2020 CRC$=CHR$(0)+CHR$(0)                      'START AT ZERO
  1129.      2030 FOR I2=4 TO 131
  1130.      2040   A$=MID$(V$,I2,1)
  1131.      2050   GOSUB 4000
  1132.      2060 NEXT I2
  1133.      2070 REM * CRC$ CONTAINS CALCULATED CRC!
  1134.  
  1135.      3000 IF CRC$=MID$(V$,132,2) THEN ....    'IT'S GOOD!!!
  1136.  
  1137.      4000 REM * CRC BITWISE CALCULATION (WHAT A JOKE!)
  1138.      4010 CRCH1=ASC(LEFT$(CRC$,1)) XOR ASC(A$)
  1139.      4020 CRCL1=ASC(RIGHT$(CRC$,1))
  1140.      4030 FOR I3 = 0 TO 7
  1141.      4040   CARRY=0 : IF CRCH1 > 127 THEN CARRY=-1  'IS HIGH BIT ON IN CRC?
  1142.      4050   CRCH1=(CRCH1*2) AND 255                 'CRCH << 1 AND 255
  1143.      4060   IF CRCL1>127 THEN CRCH1=CRCH1+1 'IF CRCL CARRIES THEN INCR CRCH
  1144.      4070   CRCL1=(CRCL1*2) AND 255                 'CRCL << 1 AND 255
  1145.      4080   IF CARRY=0 THEN GOTO 4105               'IF HIGH BIT WAS ON,
  1146.      4090   CRCH1=CRCH1 XOR 16                      'XOR WITH &H1021
  1147.      4100   CRCL1=CRCL1 XOR 33
  1148.      4110 NEXT I3
  1149.      4130 CRC$=CHR$(CRCH1)+CHR$(CRCL1)
  1150.      4140 RETURN 'WHEW
  1151.  
  1152.  
  1153.           That routine will execute 128 * 7 + 128 * 9 * 8 BASIC statements
  1154.           for each Xmodem packet or 10112 statements per Xmodem packet!  It
  1155.           will work for low baud rates in compiled BASIC, but just is too
  1156.           much for interpretive BASIC.
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.           Xmodem, CRC Xmodem, WXmodem
  1165.      June 20, 1986                                                 Page 26
  1166.      ----------------------------------------------------------------------
  1167.  
  1168.           7.3. BASIC Implementation of the Table Method
  1169.  
  1170.           This method is based on routine M4 in Steven Satchell's paper,
  1171.           "Test of CRC Routines for CRC-CCITT", but has some very signifi-
  1172.           cant differences.  A table of 256 CRC's, originally calculated
  1173.           with the bit shift method is used to avoid performing the bit
  1174.           shift during communications.  The table contains the CRC's for
  1175.           each byte value from 0 to 255 when the original CRC is zero.  The
  1176.           result of this calculation is included in the DATA statements in
  1177.           the code.
  1178.  
  1179.           The comments are intended to show what is logically happening
  1180.           rather than physically.  Because of the "byte reverse" nature of
  1181.           integers in the 8088, a logical shift of 8 bits to the left is a
  1182.           physical shift of eight bits to the right!
  1183.  
  1184.  
  1185.      200 DEFINT A-Z  'ALL INTEGERS
  1186.      210 DIM CRCTB(256)
  1187.  
  1188.      300 GOSUB 9000 'INITIALIZE CRC TABLES
  1189.  
  1190.      6200 REM * CRC CALCULATION USING TABLE METHOD, V$=XMODEM PACKET
  1191.      6210 CRC$=CHR$(0)+CHR$(0)                 'INITIALIZE TO ZERO
  1192.      6220 FOR Q=4 TO 131
  1193.      6230   CRCH1=ASC(LEFT$(CRC$,1))           'CRC >> 8 AND 255
  1194.      6240   CRCL2=CVI(CHR$(0)+RIGHT$(CRC$,1))  'CRC << 8 AND 255
  1195.      6250   CRC1$=MKI$(CRCTB(CRCH1 XOR ASC(MID$(V$,Q,1))) XOR CRCL2)
  1196.      6260   CRC$=RIGHT$(CRC1$,1)+LEFT$(CRC1$,1) 'SET IT BACK!
  1197.      6270 NEXT Q
  1198.      6280 IF CRC$ <> MID$(V$,N,2) THEN ....... 'GOTO ERROR ROUTINE
  1199.      6290 REM * END OF CRC CALC
  1200.  
  1201.      9000 FOR I%=0 TO 255 ' INITIALIZE CRC TABLE
  1202.      9010   READ CRCTB(I%)
  1203.      9020 NEXT I%
  1204.      9025 RETURN
  1205.      9030 DATA 0, 4129, 8258, 12387, 16516, 20645, 24774, 28903
  1206.      9040 DATA -32504,-28375,-24246,-20117,-15988,-11859,-7730,-3601
  1207.      9050 DATA 4657, 528, 12915, 8786, 21173, 17044, 29431, 25302
  1208.      9060 DATA -27847,-31976,-19589,-23718,-11331,-15460,-3073,-7202
  1209.      9070 DATA 9314, 13379, 1056, 5121, 25830, 29895, 17572, 21637
  1210.      9080 DATA -23190,-19125,-31448,-27383,-6674,-2609,-14932,-10867
  1211.      9090 DATA 13907, 9842, 5649, 1584, 30423, 26358, 22165, 18100
  1212.      9100 DATA -18597,-22662,-26855,-30920,-2081,-6146,-10339,-14404
  1213.      9110 DATA 18628, 22757, 26758, 30887, 2112, 6241, 10242, 14371
  1214.      9120 DATA -13876,-9747,-5746,-1617,-30392,-26263,-22262,-18133
  1215.      9130 DATA 23285, 19156, 31415, 27286, 6769, 2640, 14899, 10770
  1216.      9140 DATA -9219,-13348,-1089,-5218,-25735,-29864,-17605,-21734
  1217.      9150 DATA 27814, 31879, 19684, 23749, 11298, 15363, 3168, 7233
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.           Xmodem, CRC Xmodem, WXmodem
  1225.      June 20, 1986                                                 Page 27
  1226.      ----------------------------------------------------------------------
  1227.  
  1228.      9160 DATA -4690,-625,-12820,-8755,-21206,-17141,-29336,-25271
  1229.      9170 DATA 32407, 28342, 24277, 20212, 15891, 11826, 7761, 3696
  1230.      9180 DATA -97,-4162,-8227,-12292,-16613,-20678,-24743,-28808
  1231.      9190 DATA -28280,-32343,-20022,-24085,-12020,-16083,-3762,-7825
  1232.      9200 DATA 4224, 161, 12482, 8419, 20484, 16421, 28742, 24679
  1233.      9210 DATA -31815,-27752,-23557,-19494,-15555,-11492,-7297,-3234
  1234.      9300 DATA 689, 4752, 8947, 13010, 16949, 21012, 25207, 29270
  1235.      9310 DATA -18966,-23093,-27224,-31351,-2706,-6833,-10964,-15091
  1236.      9320 DATA 13538, 9411, 5280, 1153, 29798, 25671, 21540, 17413
  1237.      9330 DATA -22565,-18438,-30823,-26696,-6305,-2178,-14563,-10436
  1238.      9340 DATA 9939, 14066, 1681, 5808, 26199, 30326, 17941, 22068
  1239.      9350 DATA -9908,-13971,-1778,-5841,-26168,-30231,-18038,-22101
  1240.      9360 DATA 22596, 18533, 30726, 26663, 6336, 2273, 14466, 10403
  1241.      9370 DATA -13443,-9380,-5313,-1250,-29703,-25640,-21573,-17510
  1242.      9380 DATA 19061, 23124, 27191, 31254, 2801, 6864, 10931, 14994
  1243.      9390 DATA -722,-4849,-8852,-12979,-16982,-21109,-25112,-29239
  1244.      9400 DATA 31782, 27655, 23652, 19525, 15522, 11395, 7392, 3265
  1245.      9410 DATA -4321,-194,-12451,-8324,-20581,-16454,-28711,-24584
  1246.      9420 DATA 28183, 32310, 20053, 24180, 11923, 16050, 3793, 7920
  1247.  
  1248.  
  1249.           This method uses 128 * 6 BASIC statements per Xmodem packet or a
  1250.           miserly 768 BASIC statements per packet.  And, if you want, the
  1251.           code can be tightened still more.  Unfortunately, any further
  1252.           tightening that we could see would eliminate most of the already
  1253.           limited readability of the code.
  1254.  
  1255.      Xmodem, CRC Xmodem, WXmodem
  1256.      June 20, 1986                                                 Page 28
  1257.      ----------------------------------------------------------------------
  1258.  
  1259.      8.   NOTES AND COMMENTS
  1260.  
  1261.      Please add your notes and comments here or send them to me and I'll get
  1262.      them added to the current copy on People/Link.
  1263.  
  1264.      1.   This was originally set up to ADD 32 to the character on transmit
  1265.           and SUBTRACT 32 on receive.  By using exclusive or with 64, the
  1266.           logic is the same on transmit and receive.
  1267.  
  1268.      2.   The use of the SYN character was added at the request of several
  1269.           people who have coded Xmodem routines and have struggled valiantly
  1270.           to improve their error recovery routines.  Peter Boswell 6/10/86
  1271.  
  1272.      3.   The suggestion that ACK[sequence] be sent for every block received
  1273.           was added.          Peter Boswell       6/10/86
  1274.  
  1275.      4.   The original value for the ACK/NAK timeout was 10 seconds.  This
  1276.           was changed to 15 seconds the situation where the receiver is
  1277.           operating at 300 baud and using X-Off to stop receipt of characters
  1278.           during disk I/O.  Peter Boswell, 6/10/86
  1279.  
  1280.         result of this calculation is included in the DATA statements in
  1281.           th