home *** CD-ROM | disk | FTP | other *** search
/ The First Hungarian Family / The_First_Hungarian_Family_CD-ROM.bin / internet / offlread / protocol / wxmd / wxmodem.doc < prev    next >
Text File  |  1990-09-22  |  52KB  |  1,187 lines

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