

                        Ŀ
                                          
                            APPENDIX  A   
                                          
                        


           Ŀ
                                                        
                      CYCLIC REDUNDANCY CODES           
                                                        
           



1. GENERALITIES

   Coding theory is concerned with the transmission of information.
During transmission a message may be slightly changed (due to a
'noisy' channel). Building some appropriately chosen redundancy in
the message is a standard way to allow the receiver to detect some
transmission errors.
For example, to transmit a 'true' 200-byte message one can send
202 bytes, the two final bytes being used as a word whose value is
the sum of the 'true' message bytes.
We will not discuss general coding theory. Only one very special case
will be covered. Sorry, ... Some algebra is involved.

Let us denote the 2-element set  [0,1]  by  F[2]  (this is no standard
mathematical notation ; it is just easier to type in).

We need new addition and substraction operations for  F[2] (we want
a + b  and  a - b  to be in  F[2]  when  a  and  b  are).

We define a new  +  operation by the following table :


                    +     0     1
                   
                    0     0     1
                       
                    1     1     0

Note that this is just the truth-table for  XOR .

Now, the only reasonable way to define a new  -  (minus) operation is

          a - b  =  a  +  b     (for all a, b  in  F[2] ).

This is not an error; we really mean it!

What about multiplication ?
That is not difficult : we keep the standard one. Another way to put
it : the multiplication table is just the truth-table for  AND .
So, division does not deserve any explanation.

Let us say that a polynomial is  * over *  F[2]  whenever all its
coefficients are in  F[2] .

We can work with polynomials over  F[2]  just as we have learned in
high school algebra, except that the new  +  operation should be
used whenever coefficients are to be summed.

Here are a few examples :

(x^2 + 1) (x^2 + x + 1)  =  x^4 + x^3 + x + 1  ;

(x + 1) (x^15 + x^14 + x^13 + x^12 + x^4 + x^3 + x^2 + x + 1)  =

                x^16 + x^12 + x^5 + 1  ;



Dividing  x^3 + x^2 + x    by    x + 1 , we find :


                            x^2 + 1            <---  quotient
                   
           x + 1     x^3 + x^2 + x
                      x^3 + x^2
                    
                                  x
                                  x + 1
                    
                                      1        <---  remainder



The following polynomial (known as the CRC-CCITT polynomial) is very
important for diskette operations (as will appear in section 2) :


             CRC-CCITT polynomial  Ŀ
                                                        
                       x^16 + x^12 + x^5 + 1            
                                                        
           



2. GENERATING CYCLIC REDUNDANCY CHECK WORDS

   (a) Let us assume that  array_data  is an array[0..nn - 1] of
       bytes. We want to regard  array_data  as a polynomial over
F[2].
Writing in a row the binary representation of all bytes of array_data
provides us with a sequence of  nn * 8  bits:

                 b[i]      (0  <=  i  <=  8 * nn - 1) .

Now, the polynomial we are looking for is just the sum of all
monomials
                 b[i] . x^(8 * nn - 1 - i).

Let us give an example to show all this is much simpler than it looks.
Assuming that  array_data  is a 3-byte array and that

array_data[0] = F0h    array_data[1] = 10h    array_data[2] = 21h ,

we find that the binary representation sequence is

                F0h             10h             21h
         Ŀ
          1 1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1

and that the corresponding polynomial is

            x^23 + x^22 + x^21 + x^20 + x^12 + x^5 + 1 .


(b) Let array_data be as above. We denote the corresponding polynomial
    (as defined in (a)) by  p(x).
Let  r(x)  be the  * remainder *  of the division of

             x^16 . p(x)   by the CRC-CCITT polynomial.

The degree of r(x) is, of course, less than 16 and therefore  r(x)
corresponds to a word  w .
We call  w  the 'TCRC word for array_data' (T stands for temporary).

TCRC words may be evaluated quite easily : assembly language provides
SHL  and  XOR  instructions and nothing more is required!

Here is a skech of a TCRC program :


array_data	      db	..............
size_of_array	      dw	....

tcrc_word             dw	?

..............................................
..............................................


	        mov	cx,size_of_array
	        mov	ax,0                     ; (*) line marked for
                                                 ; further reference
		mov	bx,offset array_data
again:
		xor	ah,[bx]
		call	partial
		inc	bx
		loop	again

		mov	tcrc_word,ax

                ....................
                ....................


partial		proc	near
		push	cx
		mov	cx,8
p1:		shl	ax,1
		jnc	p2
		xor	ax,1021h
p2:		loop	p1
		pop	cx
		ret
partial		endp



(c)  The disk controller uses a variant of TCRC words : CRC words.

CRC words may be evaluated almost like TCRC words ; we only need to
change the line marked (*) in the program above into

                mov     ax,0ffffh

We also need to know that the disk controller writes and reads CRC
words in an unconventional way :

          *     Most  significant byte first    *
          *     Least significant byte last     *

We are now able to describe the bytes CRC1, CRC2, CRC3 and CRC4 in
the fine structure of a sector (Chapter I - section 7).

    (i)  CRC1 and CRC2 hold the CRC word of the 8-byte array :

                A1h  A1h  A1h  FEh  C  H  R  N     .

    (ii) CRC3 and CRC4 hold the CRC word of the
         (4 + (number of data bytes))-byte array:

                A1h  A1h  A1h  FBh   data bytes    .


That's all!

