home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / MODEMS / MODEM2 / MODMPROT.003 < prev    next >
Text File  |  2000-06-30  |  15KB  |  382 lines

  1. MODEM PROTOCOL OVERVIEW
  2.  
  3. 1/1/82 by Ward Christensen.  I will maintain a master copy of
  4. this.  Please pass on changes or suggestions via CBBS/Chicago
  5. at (312) 545-8086, CBBS/CPMUG (312) 849-1132 or by voice at
  6. (312) 849-6279.
  7.  
  8. Rev: 08/09/82 Ward C. Change ACK to 06H (was error as 05H)
  9.  
  10. 1/13/85 by John Byrns -- CRC option.
  11. Please pass on any reports of errors in this document or suggestions
  12. for improvement to me via Ward's/CBBS at (312) 849-1132, or by voice
  13. at (312) 885-1105.
  14.  
  15. 4/17/85 by Keith Petersen -- Merge of the two above.
  16.  
  17.  
  18.         Table of Contents
  19.  
  20.  1. DEFINITIONS
  21.  2. TRANSMISSION MEDIUM LEVEL PROTOCOL
  22.  3. MESSAGE BLOCK LEVEL PROTOCOL
  23.  4. FILE LEVEL PROTOCOL
  24.  5. DATA FLOW EXAMPLE INCLUDING ERROR RECOVERY
  25.  6. PROGRAMMING TIPS.
  26.  7. OVERVIEW OF CRC OPTION
  27.  8. MESSAGE BLOCK LEVEL PROTOCOL, CRC MODE
  28.  9. CRC CALCULATION
  29. 10. FILE LEVEL PROTOCOL, CHANGES FOR COMPATIBILITY
  30. 11. DATA FLOW EXAMPLES WITH CRC OPTION
  31.  
  32. -------- 1. DEFINITIONS.
  33.  
  34.   <soh> 01H
  35.   <eot> 04H
  36. | <ack> 06H
  37.   <nak> 15H
  38.   <can> 18H
  39.   <C>    43H
  40.  
  41. -------- 2. TRANSMISSION MEDIUM LEVEL PROTOCOL
  42.  
  43. Asynchronous, 8 data bits, no parity, one stop bit.
  44.  
  45.     The protocol imposes no restrictions on the contents of the
  46. data being transmitted.  No control characters are looked for
  47. in the 128-byte data messages.  Absolutely any kind of data may
  48. be sent - binary, ASCII, etc.  The protocol has not formally
  49. been adopted to a 7-bit environment for the transmission of
  50. ASCII-only (or unpacked-hex) data , although it could be simply
  51. by having both ends agree to AND the protocol-dependent data
  52. with 7F hex before validating it.  I specifically am referring
  53. to the checksum, and the block numbers and their ones-
  54. complement.
  55.     Those wishing to maintain compatibility of the CP/M file
  56. structure, i.e. to allow modemming ASCII files to or from CP/M
  57. systems should follow this data format:
  58.   * ASCII tabs used (09H); tabs set every 8.
  59.   * Lines terminated by CR/LF (0DH 0AH)
  60.   * End-of-file indicated by ^Z, 1AH.  (one or more)
  61.   * Data is variable length, i.e. should be considered a
  62.     continuous stream of data bytes, broken into 128-byte
  63.     chunks purely for the purpose of transmission.
  64.   * A CP/M "peculiarity": If the data ends exactly on a
  65.     128-byte boundary, i.e. CR in 127, and LF in 128, a
  66.     subsequent sector containing the ^Z EOF character(s)
  67.     is optional, but is preferred.  Some utilities or
  68.     user programs still do not handle EOF without ^Zs.
  69.   * The last block sent is no different from others, i.e.
  70.     there is no "short block". 
  71.  
  72. -------- 3. MESSAGE BLOCK LEVEL PROTOCOL
  73.  
  74.  Each block of the transfer looks like:
  75. <SOH><blk #><255-blk #><--128 data bytes--><cksum>
  76.     in which:
  77. <SOH>        = 01 hex
  78. <blk #>     = binary number, starts at 01 increments by 1, and
  79.           wraps 0FFH to 00H (not to 01)
  80. <255-blk #> = blk # after going thru 8080 "CMA" instr, i.e.
  81.           each bit complemented in the 8-bit block number.
  82.           Formally, this is the "ones complement".
  83. <cksum>     = the sum of the data bytes only.  Toss any carry.
  84.  
  85. -------- 4. FILE LEVEL PROTOCOL
  86.  
  87. ---- 4A. COMMON TO BOTH SENDER AND RECEIVER:
  88.  
  89.     All errors are retried 10 times.  For versions running with
  90. an operator (i.e. NOT with XMODEM), a message is typed after 10
  91. errors asking the operator whether to "retry or quit".
  92.     Some versions of the protocol use <can>, ASCII ^X, to
  93. cancel transmission.  This was never adopted as a standard, as
  94. having a single "abort" character makes the transmission
  95. susceptible to false termination due to an <ack> <nak> or <soh>
  96. being corrupted into a <can> and cancelling transmission.
  97.     The protocol may be considered "receiver driven", that is,
  98. the sender need not automatically re-transmit, although it does
  99. in the current implementations.
  100.  
  101. ---- 4B. RECEIVE PROGRAM CONSIDERATIONS:
  102.  
  103.     The receiver has a 10-second timeout.  It sends a <nak>
  104. every time it times out.  The receiver's first timeout, which
  105. sends a <nak>, signals the transmitter to start.  Optionally,
  106. the receiver could send a <nak> immediately, in case the sender
  107. was ready.  This would save the initial 10 second timeout.
  108. However, the receiver MUST continue to timeout every 10 seconds
  109. in case the sender wasn't ready.
  110.     Once into a receiving a block, the receiver goes into a
  111. one-second timeout for each character and the checksum.  If the
  112. receiver wishes to <nak> a block for any reason (invalid
  113. header, timeout receiving data), it must wait for the line to
  114. clear.  See "programming tips" for ideas
  115.     Synchronizing:  If a valid block number is received, it
  116. will be: 1) the expected one, in which case everything is fine;
  117. or 2) a repeat of the previously received block.  This should
  118. be considered OK, and only indicates that the receivers <ack>
  119. got glitched, and the sender re-transmitted; 3) any other block
  120. number indicates a fatal loss of synchronization, such as the
  121. rare case of the sender getting a line-glitch that looked like
  122. an <ack>.  Abort the transmission, sending a <can>
  123.  
  124. ---- 4C. SENDING PROGRAM CONSIDERATIONS.
  125.  
  126.     While waiting for transmission to begin, the sender has
  127. only a single very long timeout, say one minute.  In the
  128. current protocol, the sender has a 10 second timeout before
  129. retrying.  I suggest NOT doing this, and letting the protocol
  130. be completely receiver-driven.  This will be compatible with
  131. existing programs.
  132.     When the sender has no more data, it sends an <eot>, and
  133. awaits an <ack>, resending the <eot> if it doesn't get one.
  134. Again, the protocol could be receiver-driven, with the sender
  135. only having the high-level 1-minute timeout to abort.
  136.  
  137. -------- 5. DATA FLOW EXAMPLE INCLUDING ERROR RECOVERY
  138.  
  139. Here is a sample of the data flow, sending a 3-block message.
  140. It includes the two most common line hits - a garbaged block,
  141. and an <ack> reply getting garbaged.  <xx> represents the
  142. checksum byte.
  143.  
  144. SENDER                    RECEIVER
  145.                 times out after 10 seconds,
  146.             <---        <nak>
  147. <soh> 01 FE -data- <xx> --->
  148.             <---        <ack>
  149. <soh> 02 FD -data- xx    --->    (data gets line hit)
  150.             <---        <nak>
  151. <soh> 02 FD -data- xx    --->
  152.             <---        <ack>
  153. <soh> 03 FC -data- xx    --->
  154.    (ack gets garbaged)  <---        <ack>
  155. <soh> 03 FC -data- xx    --->        <ack>
  156. <eot>            --->
  157.             <---        <ack>
  158.  
  159. -------- 6. PROGRAMMING TIPS.
  160.  
  161. * The character-receive subroutine should be called with a
  162. parameter specifying the number of seconds to wait.  The
  163. receiver should first call it with a time of 10, then <nak> and
  164. try again, 10 times.
  165.   After receiving the <soh>, the receiver should call the
  166. character receive subroutine with a 1-second timeout, for the
  167. remainder of the message and the <cksum>.  Since they are sent
  168. as a continuous stream, timing out of this implies a serious
  169. like glitch that caused, say, 127 characters to be seen instead
  170. of 128.
  171.  
  172. * When the receiver wishes to <nak>, it should call a "PURGE"
  173. subroutine, to wait for the line to clear.  Recall the sender
  174. tosses any characters in its UART buffer immediately upon
  175. completing sending a block, to ensure no glitches were mis-
  176. interpreted.
  177.   The most common technique is for "PURGE" to call the
  178. character receive subroutine, specifying a 1-second timeout,
  179. and looping back to PURGE until a timeout occurs.  The <nak> is
  180. then sent, ensuring the other end will see it.
  181.  
  182. * You may wish to add code recommended by John Mahr to your
  183. character receive routine - to set an error flag if the UART
  184. shows framing error, or overrun.  This will help catch a few
  185. more glitches - the most common of which is a hit in the high
  186. bits of the byte in two consecutive bytes.  The <cksum> comes
  187. out OK since counting in 1-byte produces the same result of
  188. adding 80H + 80H as with adding 00H + 00H.
  189.  
  190. -------- 7. OVERVIEW OF CRC OPTION
  191.  
  192. The CRC used in the Modem Protocol is an alternate form of block check
  193. which provides more robust error detection than the original checksum.
  194. Andrew S. Tanenbaum says in his book, Computer Networks, that the
  195. CRC-CCITT used by the Modem Protocol will detect all single and double
  196. bit errors, all errors with an odd number of bits, all burst errors of
  197. length 16 or less, 99.997% of 17-bit error bursts, and 99.998% of
  198. 18-bit and longer bursts.
  199.  
  200. The changes to the Modem Protocol to replace the checksum with the CRC
  201. are straight forward. If that were all that we did we would not be
  202. able to communicate between a program using the old checksum protocol
  203. and one using the new CRC protocol. An initial handshake was added to
  204. solve this problem. The handshake allows a receiving program with CRC
  205. capability to determine whether the sending program supports the CRC
  206. option, and to switch it to CRC mode if it does. This handshake is
  207. designed so that it will work properly with programs which implement
  208. only the original protocol. A description of this handshake is
  209. presented in section 10.
  210.  
  211. -------- 8. MESSAGE BLOCK LEVEL PROTOCOL, CRC MODE
  212.  
  213.  Each block of the transfer in CRC mode looks like:
  214. <SOH><blk #><255-blk #><--128 data bytes--><CRC hi><CRC lo>
  215.     in which:
  216. <SOH>          = 01 hex
  217. <blk #>     = binary number, starts at 01 increments by 1, and
  218.           wraps 0FFH to 00H (not to 01)
  219. <255-blk #> = ones complement of blk #.
  220. <CRC hi>    = byte containing the 8 hi order coefficients of the CRC.
  221. <CRC lo>    = byte containing the 8 lo order coefficients of the CRC.
  222.           See the next section for CRC calculation.
  223.  
  224. -------- 9. CRC CALCULATION
  225.  
  226. ---- 9A. FORMAL DEFINITION OF THE CRC CALCULATION
  227.  
  228. To calculate the 16 bit CRC the message bits are considered to be the
  229. coefficients of a polynomial. This message polynomial is first
  230. multiplied by X^16 and then divided by the generator polynomial
  231. (X^16 + X^12 + X^5 + 1) using modulo two arithemetic. The remainder
  232. left after the division is the desired CRC. Since a message block in
  233. the Modem Protocol is 128 bytes or 1024 bits, the message polynomial
  234. will be of order X^1023. The hi order bit of the first byte of the
  235. message block is the coefficient of X^1023 in the message polynomial.
  236. The lo order bit of the last byte of the message block is the
  237. coefficient of X^0 in the message polynomial.
  238.  
  239. ---- 9B. EXAMPLE OF CRC CALCULATION WRITTEN IN C
  240.  
  241. /*
  242. This function calculates the CRC used by the "Modem Protocol"
  243. The first argument is a pointer to the message block. The second
  244. argument is the number of bytes in the message block. The message
  245. block used by the Modem Protocol contains 128 bytes.
  246. The function return value is an integer which contains the CRC. The
  247. lo order 16 bits of this integer are the coefficients of the CRC. The
  248. The lo order bit is the lo order coefficient of the CRC.
  249. */
  250.  
  251. int calcrc(ptr, count) char *ptr; int count; {
  252.  
  253.     int crc, i;
  254.  
  255.     crc = 0;
  256.     while(--count >= 0) {
  257.     crc = crc ^ (int)*ptr++ << 8;
  258.     for(i = 0; i < 8; ++i)
  259.         if(crc & 0x8000)
  260.         crc = crc << 1 ^ 0x1021;
  261.         else
  262.         crc = crc << 1;
  263.     }
  264.     return (crc & 0xFFFF);
  265.     }
  266.  
  267. -------- 10. FILE LEVEL PROTOCOL, CHANGES FOR COMPATIBILITY
  268.  
  269. ---- 10A. COMMON TO BOTH SENDER AND RECEIVER:
  270.  
  271. The only change to the File Level Protocol for the CRC option is the
  272. initial handshake which is used to determine if both the sending and
  273. the receiving programs support the CRC mode. All Modem Programs should
  274. support the checksum mode for compatibility with older versions.
  275. A receiving program that wishes to receive in CRC mode implements the
  276. mode setting handshake by sending a <C> in place of the initial <nak>.
  277. If the sending program supports CRC mode it will recognize the <C> and
  278. will set itself into CRC mode, and respond by sending the first block
  279. as if a <nak> had been received. If the sending program does not
  280. support CRC mode it will not respond to the <C> at all. After the
  281. receiver has sent the <C> it will wait up to 3 seconds for the <soh>
  282. that starts the first block. If it receives a <soh> within 3 seconds
  283. it will assume the sender supports CRC mode and will proceed with the
  284. file exchange in CRC mode. If no <soh> is received within 3 seconds
  285. the receiver will switch to checksum mode, send a <nak>, and proceed
  286. in checksum mode. If the receiver wishes to use checksum mode it
  287. should send an initial <nak> and the sending program should respond to
  288. the <nak> as defined in the original Modem Protocol. After the mode
  289. has been set by the initial <C> or <nak> the protocol follows the
  290. original Modem Protocol and is identical whether the checksum or CRC
  291. is being used.
  292.  
  293. ---- 10B. RECEIVE PROGRAM CONSIDERATIONS:
  294.  
  295. There are at least 4 things that can go wrong with the mode setting
  296. handshake.
  297.   1. the initial <C> can be garbled or lost.
  298.   2. the initial <soh> can be garbled.
  299.   3. the initial <C> can be changed to a <nak>.
  300.   4. the initial <nak> from a receiver which wants to receive in
  301.      checksum can be changed to a <C>.
  302.  
  303. The first problem can be solved if the receiver sends a second <C>
  304. after it times out the first time. This process can be repeated
  305. several times. It must not be repeated a too many times before sending
  306. a <nak> and switching to checksum mode or a sending program without
  307. CRC support may time out and abort. Repeating the <C> will also fix
  308. the second problem if the sending program cooperates by responding as
  309. if a <nak> were received instead of ignoring the extra <C>.
  310.  
  311. It is possible to fix problems 3 and 4 but probably not worth the
  312. trouble since they will occur very infrequently. They could be fixed
  313. by switching modes in either the sending or the receiving program
  314. after a large number of successive <nak>s. This solution would risk
  315. other problems however.
  316.  
  317. ---- 10C. SENDING PROGRAM CONSIDERATIONS.
  318.  
  319. The sending program should start in the checksum mode. This will
  320. insure compatibility with checksum only receiving programs. Anytime a
  321. <C> is received before the first <nak> or <ack> the sending program
  322. should set itself into CRC mode and respond as if a <nak> were
  323. received. The sender should respond to additional <C>s as if they were
  324. <nak>s until the first <ack> is received. This will assist the
  325. receiving program in determining the correct mode when the <soh> is
  326. lost or garbled. After the first <ack> is received the sending program
  327. should ignore <C>s.
  328.  
  329. -------- 11. DATA FLOW EXAMPLES WITH CRC OPTION
  330.  
  331. ---- 11A. RECEIVER HAS CRC OPTION, SENDER DOESN'T
  332.  
  333. Here is a data flow example for the case where the receiver requests
  334. transmission in the CRC mode but the sender does not support the CRC
  335. option. This example also includes various transmission errors.
  336. <xx> represents the checksum byte.
  337.  
  338. SENDER                    RECEIVER
  339.             <---        <C>
  340.                 times out after 3 seconds,
  341.             <---        <nak>
  342. <soh> 01 FE -data- <xx> --->
  343.             <---        <ack>
  344. <soh> 02 FD -data- <xx> --->    (data gets line hit)
  345.             <---        <nak>
  346. <soh> 02 FD -data- <xx> --->
  347.             <---        <ack>
  348. <soh> 03 FC -data- <xx> --->
  349.    (ack gets garbaged)  <---        <ack>
  350.                 times out after 10 seconds,
  351.             <---        <nak>
  352. <soh> 03 FC -data- <xx> --->
  353.             <---        <ack>
  354. <eot>            --->
  355.             <---        <ack>
  356.  
  357. ---- 11B. RECEIVER AND SENDER BOTH HAVE CRC OPTION
  358.  
  359. Here is a data flow example for the case where the receiver requests
  360. transmission in the CRC mode and the sender supports the CRC option.
  361. This example also includes various transmission errors.
  362. <xxxx> represents the 2 CRC bytes.
  363.  
  364. SENDER                      RECEIVER
  365.               <---          <C>
  366. <soh> 01 FE -data- <xxxx> --->
  367.               <---          <ack>
  368. <soh> 02 FD -data- <xxxx> --->      (data gets line hit)
  369.               <---          <nak>
  370. <soh> 02 FD -data- <xxxx> --->
  371.               <---          <ack>
  372. <soh> 03 FC -data- <xxxx> --->
  373.    (ack gets garbaged)      <---          <ack>
  374.                   times out after 10 seconds,
  375.               <---          <nak>
  376. <soh> 03 FC -data- <xxxx> --->
  377.               <---          <ack>
  378. <eot>              --->
  379.               <---          <ack>
  380.  
  381.             -----END-----
  382.