home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR10 / WXYZMODM.ZIP / WXMODEM.DOC < prev    next >
Text File  |  1990-10-02  |  62KB  |  1,373 lines

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