CRC
CRC
CRC
SPRA530
C5000
Abstract
Cyclic redundancy check (CRC) code provides a simple, yet powerful, method for the detection of
burst errors during digital data transmission and storage. CRC implementation can use either
hardware or software methods. This application report presents different software algorithms and
compares them in terms of memory and speed using the Texas Instruments (TI) TMS320C54x
digital signal processor (DSP). Various common CRC codes will be used.
April 1999
Application Report
SPRA530
Contents
Introduction ......................................................................................................................................................3
Coding Theory Behind CRC.............................................................................................................................3
Fundamentals of Block Coding.............................................................................................................3
CRC Coding .........................................................................................................................................5
CRC Code Examples............................................................................................................................5
Algorithms for CRC Computation .....................................................................................................................6
Bitwise Algorithm ..................................................................................................................................6
Lookup Table Algorithms ......................................................................................................................7
Standard Lookup Table Algorithm ........................................................................................................7
Reduced Lookup Table Algorithm ........................................................................................................8
TMS320C54x Implementation..........................................................................................................................9
General Considerations ........................................................................................................................9
Results..................................................................................................................................................9
CRC-CCITT ................................................................................................................................9
CRC-32 10
CRC for GSM/TCH ...................................................................................................................10
CRC for GSM/TCH/EFS Precoder............................................................................................11
GSM FIRE Code.......................................................................................................................11
Summary........................................................................................................................................................12
Code Availability.............................................................................................................................................12
References.....................................................................................................................................................12
Figures
Figure 1.
Tables
Table 1.
Table 2.
Table 3.
Table 4.
Table 5.
Table 6.
Application Report
SPRA530
Introduction
Error correction codes provide a means to detect and correct errors introduced by a
transmission channel. Two main categories of code exist: block codes and convolutional
codes. They both introduce redundancy by adding parity symbols to the message data.
Cyclic redundancy check (CRC) codes are a subset of cyclic codes that are also a subset
of linear block codes. The theory behind block coding and more specifically CRC coding
is briefly discussed in this application report as well as most common CRC codes.
CRC implementation can use either hardware or software methods. In the traditional
hardware implementation, a simple shift register circuit performs the computations by
handling the data one bit at a time. In software implementations, handling data as bytes
or words becomes more convenient and faster. You choose a particular algorithm
depending on which memory and speed constraints are required. Different types of
algorithms and the results are presented later in this application report.
There are 2 possible code words in a binary block code of length n. From these 2 code
k
words you may select M = 2 code words (k < n) to form a code. Thus a block of k
k
information bits is mapped into a code word of length n selected from the set of M = 2
code words. This is called an (n,k) code and the ratio k/n is defined to be the rate of the
code.
The encoding and decoding functions involve the arithmetic operations of addition and
multiplication performed on code words. These arithmetic operations are performed
according to the conventions of the algebraic field that has, as its elements, the symbols
contained in the alphabet. For binary codes, the field is finite and has 2 elements, 0 and
1, and is called GF(2) (Galois Field).
A code is linear if the addition of any two-code vectors forms another code word. Code
linearity simplifies implementation of coding operations.
The minimum distance dmin is the smallest hamming distance between two code words.
The hamming distance is the number of symbols (bits in the binary case) in which they
differ. The minimum distance is closely linked to the capacity of the code to detect and
correct errors and is a function of the code characteristics. An (n,k) block code is capable
1
of detecting dmin 1 errors and correcting (d min 1) errors.
2
Application Report
SPRA530
Suppose m i is the k-bits information word, the output of the encoder will result in an
n-bits code word c i , defined as c i = m i G (i = 0,1,....,2 k 1) where G is called the
generator matrix of dimension k n . Every linear block code is equivalent to a systematic
code; therefore, it is always possible to find a matrix G generating code words formed by
the k information bits followed by the n-k parity check bits, for example, G = [I k ,P ] where
systematic, then H = P T ,I n k . Since all code words are linear sums of the rows in G,
we have c i H = 0 for all I, (i = 0,1,....,2 k 1) . If a code word c is corrupted during
transmission so that the receive word is c' = c + e , where e is a non-zero error pattern,
then we call syndrome s the result of following multiplication,
s = c ' H T = (c + e )H T = cH T + eH T = eH T , where s is (n k ) - dimensional vector. The
syndrome s is dependent on the error pattern. If the error pattern is a code vector, the
errors go undetected. For all other error patterns, however, the syndrome is nonzero.
Decoding then uses standard array decoders that are based on lookup tables to
associate each syndrome with an error pattern. This method becomes impractical for
many interesting and powerful codes as (n k ) increases.
T
Cyclic codes are a subclass of linear block codes with an algebraic structure that enables
encoding to be implemented with a linear feedback shift register and decoding to be
implemented without using standard array decoders. Therefore, most block codes in use
today are cyclic or are closely related to cyclic codes. These codes are best described if
vectors are interpreted as polynomials. In a cyclic code, all code word polynomials are
multiples of a generator polynomial g ( x ) of degree n k . This polynomial is chosen to
be a divisor of x n + 1 so that a cyclic shift of a code vector yields another code vector. A
message polynomial m i ( x ) can be mapped to a code word polynomial
g( x ) . This is
Application Report
SPRA530
CRC Coding
CRC codes are a subset of cyclic codes and use a binary alphabet, 0 and 1. Arithmetic is
based on GF(2), for example, modulo-2 addition (logical XOR) and modulo-2
multiplication (logical AND).
In a typical coding scheme, systematic codes are used. Assume the convention that the
m( x ) to be the
leftmost bit represents the highest degree in the polynomial. Suppose
c( x ) the code word polynomial and g ( x ) the generator polynomial,
message polynomial,
c( x ) = m( x )g ( x ) which can also be written using the systematic form
we have
c( x ) = m( x )x n k + r ( x ) , where r ( x ) is the remainder of the division of m( x )x n k by g ( x )
r ( x ) represents the CRC bits. The transmitted message c( x ) contains k-information
and
bits followed by n k CRC bits, for example,
c( x ) = m k 1 x n 1 + ... + m 0 x n k + r n k 1 x n k 1 + r 0 . So encoding is straightforward:
n k
multiply m( x ) by x
, that is, append n k bits to the message, calculate the CRC bits
by dividing
c ' ( x ) is a multiple of
message, then no error or undetectable errors have occurred if
n k
g ( x ) , which is equivalent to determining that if c ' ( x )x
g ( x ) , that is, if
is a multiple of
n k
c ' ( x )x
g ( x ) is 0.
the remainder of the division from
by
Table 1.
CRC Code
CRC-CCITT (X25)
Generator Polynomial
CRC-32 (Ethernet)
GSM TCH/FS-HS-EFS
(Channel coding for speech traffic
channels)
GSM TCH/EFS pre-coding
(Preliminary channel coding for Enhanced
Full Rate)
GSM control channels FIRE code
(Channel coding for control channels)
x16 + x12 + x 5 + 1
x8 + x 4 + x3 + x2 + 1
x 40 + x 26 + x 23 + x17 + x 3 + 1
Application Report
SPRA530
g1
r0
gnk1
g2
r1
r2
rnk1
m(x)
c(x
1)
CRC
2)
if the CRC left-most bit is equal to 1, shift in the next message bit, and XOR the CRC
register with the generator polynomial; otherwise, only shift in the next message bit
3)
Repeat step 2 until all bits of the augmented message have been shifted in
Faster implementations can be achieved by handling the data as larger units than bits, as
long as the size does not exceed the degree of the generator polynomial. However, the
speed gain corresponds to a memory increase, since precomputed values (lookup tables)
will be used.
Application Report
SPRA530
-bit word. The CRC of the augmented message is the remainder of x n k m' ( x )
]
[x b( x ) + x r ( x )]. Expanding the result, we have
[(b + r )x
]+ r
+ ... + (b + r
)x
x n k m' ( x ) = x n k x c( x ) + b( x ) = x n k b( x ) + x a( x )g ( x ) + x r ( x ) , so that,
r ' ( x ) = Rg( x)
r ' (x) = Rg(x )
n k
n k 1+
n k 1
n k
n k
n k 1 x
n k 1
+ r x
0
The last equation relates the check bits of the augmented message with the check bits of
the original message. Note that if n k = , then
r ' ( x ) = Rg ( x ) (b 1 + rn k 1)x n k 1+ + ... + (b0 + rn k )x n k . Different algorithms for CRC
n k 1,..., r 0
) bits to 0
1)
CRC
2)
XOR the input bits with the CRC register content shifted right by n k bits,
that is, XOR with the
(r
n k 1
,..., rn k ) bits
3)
find the corresponding value in the lookup table and XOR the CRC register content
shifted left by bits, that is, XOR with the (r 1,..., r0 ) bits. This is the new CRC
value.
4)
2)
XOR the input bits with the CRC register, that is, XOR with the
3)
Find the corresponding value in the lookup table. This is the new CRC value.
Application Report
SPRA530
[
]+ ... + R [(b
R g ( x ) (b 1 + r n k 1 )x n k 1+
g( x)
n k 1,..., r 0
) bits to 0
1)
CRC
2)
XOR the input bits with the CRC register content shifted right by bits, XOR with
the (r n k 1,..., r n k ) bits
3)
on each bit of this value ( bits), if it is equal to 1 then find the corresponding value in
the lookup table and XOR the CRC register content with it
4)
XOR this value with the CRC shifted left by bits, that is, XOR with the
(r
,..., r0 )
2)
XOR the input bits with the CRC e.g. XOR with the
4)
Note that only step 3 differs from the standard lookup table algorithm. As expected, there
is an increase of processing due to the fact that each bit must be tested and if it is equal
to 1 then a XOR operation must be performed.
Application Report
SPRA530
TMS320C54x Implementation
General Considerations
The TMS320C54x DSP is a 16-bit fixed-point DSP. Interesting for the implementation of
the examples given:
CRC codes with a size smaller than 40 bits are relatively easy to implement. Depending
on the type of algorithm used, 8-, 16-, or up to 32-bit data is used.
Results
Tables 2 to 6 illustrate the results obtained from simulation done on 256 input words,
which were randomly generated by a C-program using the rand() function. CRCB, CRCT,
and CRCR are the function names corresponding, respectively, to the bit-wise algorithm,
standard lookup table algorithm, and reduced lookup table algorithm. You can refer to the
assembly code in the appendixes.
As expected from the theory, we have a trade-off between memory requirement and
processing speed between the different algorithms. One trade-off is that the reduced
look-up table algorithm does not offer any advantage over the two others; this is
understandable, since in both cases, testing of all input bits must be performed.
CRC-CCITT
The standard lookup table algorithm is 2.7 times faster than the bit-wise
implementation, but requires 16.5 times more memory, due to the lookup table
essentially (see Table 2).
Table 2.
Cycles
91
(25343 )
12
(9472 )
91
(25602 )
Program
10
(17 )
10
(23 )
15
(26 )
Data
Tables
256
16
Application Report
SPRA530
CRC-32
The standard lookup table algorithm is 3.8 times faster than the bit-wise
implementation, but requires 10.7 times more memory, due to the lookup table
essentially (see Table 3).
Table 3.
Cycles
139
(37693 )
13
(9984 )
16
(42750 )
Program
12
(34 )
11
(24 )
22
(28 )
Data
16
Tables
512
32
Table 4.
CRCT
CRCR
Cycles
89
(24859 )
Program
10
(21 )
Data
Tables
10
Application Report
SPRA530
Table 5.
Cycles
91
(25343 )
(8448 )
53
(31486 )
Program
10
(17 )
6
(19 )
14
(25 )
Data
Tables
256
Table 6.
CRCR
Cycles
137
(37258 )
20
(13570 )
Program
12
(40 )
18
(33 )
Data
Tables
768
11
Application Report
SPRA530
Summary
Three CRC computation algorithms and their implementation using the TMS320C54x
DSP have been considered in this application report, as well as various commonly used
CRC codes.
The bit-wise algorithm and the standard lookup table algorithm appear to be the most
appropriate depending on the application requirements, that is, memory usage or
computation time optimization. The results of the simulation based on five CRC codes
show that the standard lookup table algorithm is about 3 times faster, but requires about
14 times more memory than the bit-wise algorithms.
The code provided in this application report is easily adaptable to other CRC codes and
should provide you a good startup point, when considering implementation of cyclic
redundancy check.
Code Availability
The associated program files are available from Texas Instruments TMS320 Bulletin
Board System (BBS). Internet users can access the BBS via anonymous ftp.
References
1. Ramabadran T.V., Gaitonde S.S., A tutorial on CRC computations, IEEE Micro, Aug
1988
2. ETSI GSM 05.03 Specification, Channel Coding , August 1996, Version 5.2.0.
3. John G. Proakis, Digital Communications, 3rd edition.
12
Application Report
SPRA530
.set
1021h
inport
.set
0h
.asg
NOP,PIPELINE
************************************************************
*
*
Stack setup
Reset vector
************************************************************
BOS
.usect "stack",0fh
; setup stack
TOS
.usect "stack",1
; Top of stack at reset
.sect
"vectors"
bd
stm
Entry
#TOS,SP
Reset:
; reset vector
; Setup stack
************************************************************
*
Main Program
************************************************************
.sect
"program"
Entry
.include "c5xx.inc"
ld
#crc,DP
portr
addm
#inport,@nbDataIn
#-1,@nbDataIn
; nb words
; for repeat
13
Application Report
SPRA530
************************************************************
*
CRCB (word wise)
************************************************************
stm
#input,AR2
; copy inputwords in RAM
rpt
@nbDataIn
;
portr #inport,*AR2+
; read them from IO space
next
ld
mvdm
stm
#genCRC,16,B
@nbDataIn,AR1
#input,AR2
ld
calld
or
nop
banz
*AR2+,16,A
CRCB
*AR2+,A
next,*AR1-
sth
A,@crc
************************************************************
*
************************************************************
next1
stm
mvdm
ld
rsbx
#input,AR2
@nbDataIn,AR1
#0,A
SXM
;
;
;
;
stm
calld
ld
ld
calld
and
banz
#t_start,AR3
CRCT
*AR2,-8,B
*AR2+,B
CRCT
#0FFh,B
next1,*AR1-
stl
A,@crc
; store result
; process MSByte
; BL = MSByte
************************************************************
*
************************************************************
next2
stm
mvdm
ld
#input,AR2
@nbDataIn,AR1
#0,A
stm
xor
calld
and
banzd
14
Application Report
SPRA530
stm
done
stl
A,@crc
done
; store result
************************************************************
* CRCB routine : bit-wise CRC calculation
* input :
*
*
* ouput :
************************************************************
* CRCT routine : standard lookup table CRC calculation
*
with bytes as input
* input :
*
*
*
* ouput :
1) 8-bit words in AL
2) AR3 = LTU start address
3) DP = 0 = DP(temp)
4) SXM = 0
CRC value in AL
* A, B, AR4, SP modified
* code size = 10 words
* cycles = 12 cycles
15
Application Report
SPRA530
* A = |A31.............A16 A15................A0|
*
<------- CRC ------->
* B = |B31........................B7........B0|
*
<-- input-->
************************************************************
CRCT
; AL (low part of Acc A) = CRC bits
stl
A,-8,@AR4
; AR4 = CRC >> 8
stl
A,8,@temp
; temp = CRC << 8
xor
@AR4,B ; B = (inputByte) XOR (CRC>>8)
;
= Offset in LTU
adds
@AR3,B ; B = Offset + LTU start address
stlm
B,AR4
; AR4 = absolute address in LTU
retd
ld
@temp,A
; AL = CRC << 8
xor
*AR4,A
; AL = new CRC
************************************************************
* CRCRW
*
.bss
input,257
; augmented message
; (16 bits appended)
16
Application Report
SPRA530
crc
.usect
temp
.set
nbDataIn .set
.sect
rtw_start
.word
.word
.word
.word
.word
.word
.word
.word
.word
.word
.word
.word
.word
.word
.word
.word
rtw_len .set
.sect
.word
.word
.word
.word
00h
01021h
02042h
03063h
.word
.word
.set
0ED1h
01EF0h
256
t_start
t_len
17
Application Report
SPRA530
.set
0h
.asg
NOP,PIPELINE
; IO address
************************************************************
*
Stack setup
*
Reset vector
************************************************************
BOS
.usect "stack",0fh
; setup stack
TOS
.usect "stack",1
; Top of stack at reset
.sect
"vectors"
bd
stm
Entry
#TOS,SP
Reset:
; reset vector
; Setup stack
************************************************************
*
Main Program
************************************************************
.sect
"program"
Entry
.include "c5xx.inc"
ld
#crc,DP
portr
addm
#inport,@nbDataIn
#-1,@nbDataIn
stm
rpt
portr
#input,AR2
@nbDataIn
#inport,*AR2+
; reads in nb words
; for repeat
18
Application Report
SPRA530
************************************************************
*
************************************************************
ld
#hgenCRC,16,B
; BH = CRC gen. poly. MSB's
or
#lgenCRC,B
; BL = CRC gen. poly. LSB's
mvdm
@nbDataIn,AR1
; AR1 = length of message
stm
#input,AR2
; AR2 points to input word
stm
ld
stm
rptbd
stm
stl
add
initbitpos
mar
stm
dld
next
call
banzd
mar
nop
dst
#16,BK
#0,A
#16-1,BRC
initbitpos-1
#bitpos,AR3
A,*AR3+%
#1,A
*AR3+%
#0,T
*AR2+,A
CRCB
next,*AR1*AR2+
A,@crc
;
;
;
;
;
;
;
;
;
;
;
;
end of repeat
Begin with bit #0
init T register
CRC register intialized
perform CRC computation
process all input words
************************************************************
*
************************************************************
stm
#input,AR2
; AR2 points to inputword
mvdm
@nbDataIn,AR1
; AR1 = length of message
ld
#0,A
; clear Acc A
rsbx
SXM
; no sign extend
next1
stm
calld
ld
ld
calld
and
banz
#t_start,AR3
CRCT
*AR2,-8,B
*AR2+,B
CRCT
#0FFh,B
next1,*AR1-
dst
A,@crc
; store result
; process MSByte
; BL = MSByte
************************************************************
*
************************************************************
stm
#2,AR0
; index = 2
stm
#input,AR2
; AR2 points to inputword
mvdm
@nbDataIn,AR1
; AR1 = length of message
ld
#0,A
; clear Acc A
19
Application Report
SPRA530
next2
done
calld
ld
nop
banz
CRCRW
*AR2+,B
dst
A,@crc
done
next2,*AR1;store result
************************************************************
* CRCB routine : bit-wise CRC calculation
* input :
*
*
* ouput :
************************************************************
* CRCT routine : standard lookup table CRC calculation
*
with bytes as input
* input :
*
*
*
* ouput :
1) 8-bit words in B
2) AR3 = LTU start address
3) DP = 0
4) SXM = 0
CRC value in A (32 bits)
20
Application Report
SPRA530
* A, B, AR4, SP modified
* code size = 11 words
* cycles = 13 cycles
* A = |A31.............A16 A15................A0|
*
<---------------- CRC ------------------>
* B = |B31.............B16..........B7........B0|
*
<---input-->
************************************************************
CRCT
; AL (low part of Acc A) = low part of CRC
; AG (high part of Acc A) = high part of CRC
sth
A,-8,@AR4 ; AR4 = CRC >> 24
xor
@AR4,B
; B = (inputByte) XOR (CRC>>24)
;
= Offset in LTU
sftl
B,1,B
; multiply by 2 because 32 bits
add
@AR3,B
; B = Offset + LTU start address
stlm
B,AR4
; AR4 = absolute address in LTU
sftl
A,8,A
; A = A << 8
retd
dld
*AR4,B
; B = LTU[inputbyte XOR (CRC>>24)]
xor
B,A
; A = new CRC
************************************************************
* CRCRW
*
* input :
*
*
* ouput :
1) input word in B
2) DP = DP(temp) = DP(temp1)
3) SXM = 0
CRC value in A (32 bits)
=
=
=
=
22 words
16 + 11*N = 192 for N = 16 bits (worst case)
16 + 7*N = 128 for N = 16 bits (best case)
160 average
* A = |A31.............A16 A15................A0|
*
<---------------- CRC ------------------>
************************************************************
CRCRW
stm
#16-1,BRC
sth
A,@temp1
; temp1 = CRC >> 16
stl
A,@temp
; temp = A = CRC << 16
ld
@temp1,A
; A = CRC >> 16
xor
B,A
; A = (CRC >> 16) XOR inputByte
ld
#0,B
; clear Acc B
rptbd
#loop-1
stm
#rtw_start,AR3 ; AR3 = LTU start address
ror
A
; get A(LSB) in Carry bit
21
Application Report
SPRA530
noCarry
loop
bc
dst
dld
xor
dld
mar
retd
ld
xor
noCarry,NC
A,@temp1
*AR3,A ;if C=1 then B = B XOR table[i++]
A,B
@temp1,A
*AR3+0
@temp,16,A
B,A
.bss
crc
"scratchPad",32 ; scratch pad memory
crc+2
temp+2
temp1+2
.sect
"table"
.long
.long
.long
.long
00h
04C11DB7h
09823B6Eh
0D4326D9h
.long
.long
.long
.set
0B8757BDAh
0B5365D03h
0B1F740B4h
512
t_start
t_len
.sect
rtw_start
.long
.long
.long
.long
.long
.long
.long
.long
.long
.long
.long
.long
.long
.long
.long
.long
rtw_len .set
22
Application Report
SPRA530
.set
6000h
inport
.set
.asg
0h
NOP,PIPELINE
************************************************************
*
*
Stack setup
Reset vector
************************************************************
BOS
.usect "stack",0fh
; setup stack
TOS
.usect "stack",1
; Top of stack at reset
.sect
"vectors"
bd
stm
Entry
#TOS,SP
Reset:
; reset vector
; Setup stack
************************************************************
*
Main Program
************************************************************
.sect
"program"
Entry
.include "c5xx.inc"
ld
#crc,DP
portr
addm
#inport,@nbDataIn
;reads in nb words
#-1,@nbDataIn
;for repeat
************************************************************
*
************************************************************
stm
#input,AR2
; copy inputwords in RAM
rpt
@nbDataIn
;
portr #inport,*AR2+
; read them from IO space
ld
#genCRC,16,B
23
Application Report
SPRA530
next
done
mvdm
stm
@nbDataIn,AR1
#input,AR2
ld
calld
stm
banz
calld
stm
*AR2+,16,A
CRCB
#16-1,BRC
next,*AR1CRCB
#3-1,BRC
;
;
;
;
sth
A,@crc
done
; trailing bits
************************************************************
* CRCB routine : bit-wise CRC calculation
* input :
*
*
*
* ouput :
1) 16-bit words in AL
2) CRC generator polynomial in BH (left justified)
3) OVM = 1
4) number of bits to test in BRC
CRC value in AH (left justified)
.bss
crc
.usect
nbDataIn .set
input,257
; augmented message
; (3 bits appended)
24
Application Report
SPRA530
.set
1D00h
inport
.set
0h
; IO space address
.asg
NOP,PIPELINE
************************************************************
*
*
Stack setup
Reset vector
************************************************************
BOS
.usect "stack",0fh
; setup stack
TOS
.usect "stack",1
; Top of stack at reset
.sect
"vectors"
bd
stm
Entry
#TOS,SP
Reset:
; reset vector
; Setup stack
************************************************************
*
Main Program
************************************************************
.sect
"program"
Entry
.include "c5xx.inc"
ld
#crc,DP
portr
addm
#inport,@nbDataIn
#-1,@nbDataIn
stm
rpt
portr
#input,AR2
@nbDataIn
#inport,*AR2+
; reads in nb words
; for repeat
25
Application Report
SPRA530
************************************************************
*
************************************************************
ld
#genCRC,16,B
; BH = CRC gen. poly.
mvdm
@nbDataIn,AR1
; AR1 = length of message.
stm
#input,AR2
; AR2 points to input word
next
ld
calld
or
nop
banz
*AR2+,16,A
CRCB
*AR2+,A
next,*AR1-
sth
A,-8,@crc
************************************************************
*
************************************************************
stm
#input,AR2
; AR2 points to inputword
mvdm
@nbDataIn,AR1
; AR1 = length of message
ld
#0,A
; clear Acc A
ld
#genCRC,B
; init Acc B with gen. poly.
sftl
B,-8,B
; right justify it
rsbx
SXM
; no sign extension
next1
stm
ld
call
ld
and
call
banz
#t_start,AR3
*AR2,-8,B
CRCT
*AR2+,B
#00FFh,B
CRCT
next1,*AR1-
;
;
;
;
;
;
stl
A,@crc
;store result
BL = LSByte
process LSByte
************************************************************
*
************************************************************
stm
#input,AR2
; AR2 points to inputword
mvdm
@nbDataIn,AR1
; AR1 = length of message
ld
#0,A
; clear Acc A
next2
done
ld
call
ld
and
call
banz
*AR2,-8,B
CRCR
*AR2+,B
#00FFh,B
CRCR
next2,*AR1-
; BL = MSByte
; process MSByte
;
; BL = LSByte
stl
b
A,@crc
done
;store result
26
Application Report
SPRA530
************************************************************
* CRCB routine : bit-wise CRC calculation
* input : 1) 16-bit words in AL
*
2) CRC generator polynomial in BH (left justified)
* ouput : CRC value in AH (left justified)
* BRC, REA, RSA, A, C, SP modified
* Code size = 10 words
* Cycles
= 11 + 5*N = 91 for N = 16
* A = |A31......A24....A16 A15................A0|
*
<-- CRC --->
<---- input bits---->
* B = |B31......B24...........................B0|
*
<-- poly -->
************************************************************
CRCB
stm
#16-1,BRC
; BRC = 16-1
rptb
CRC_end-1
; repeat block
sftl A,1,A
; A = A << 1, get MSB in carry
PIPELINE
PIPELINE
xc
1,C
; test on Carry
xor
B,A
; if C=1 then A = old_CRC XOR g(x)
CRC_end ret
************************************************************
* CRCT routine : standard lookup table CRC calculation
*
with bytes as input
* input :
*
*
* ouput :
1) 8-bit words in AL
2) AR3 = LTU start address
3) DP = 0 = DP(temp)
CRC value in AL
* A, B, AR4, SP modified
* code size = 6 words
* cycles = 8 cycles
* A = |A31.............A16 A15...... A7.......A0|
*
<--- CRC-->
************************************************************
CRCT
; AL (low part of Acc A) = CRC bits
xor
A,B
; B = (inputByte) XOR (CRC)
;
= Offset in LTU
adds
@AR3,B
; B = Offset + LTU start address
stlm
B,AR4
; AR4 = absolute address in LTU
retd
nop
ldu
*AR4,A
; AL = new CRC
;
= LTU[inputbyte XOR CRC]
27
Application Report
SPRA530
************************************************************
* CRCR routine : reduced lookup table CRC calculation
*
with bytes as input
* input : 1) 8-bit words in AL
*
4) SXM = 0
* ouput : CRC value in AL
* A, B, AR3, C, SP modified
* code size = 14 words
* Cycles
= 13 + 5*N = 53 for N = 8 bits
* A = |A31.............A16 A15...... A7.......A0|
*
<--- CRC-->
************************************************************
CRCR
xor
A,B ; B = (inputByte) XOR (CRC)
;
= Offset in LTU
ld
#0h,A
stm
#8-1,BRC
rptbd
#loop-1
stm
#rt_start-1,AR3 ; AR3=LTU start address-1
ror
B
mar
*AR3+
nop
xc
1,C
xor
*AR3,A ; if C=1 then B = B XOR table[i++]
loop
ret
.bss
crc
.usect "scratchPad",32 ;scratch pad memory
temp
.set
crc+1
nbDataIn .set temp+1
.sect
"table"
.word
.word
.word
00h
01Dh
03Ah
.word
.word
.word
.set
0FEh
0D9h
0C4h
256
.sect
"reducedTable"
.word
.word
.word
.word
01Dh
03Ah
074h
0E8h
t_start
t_len
rt_start
28
Application Report
SPRA530
rt_len
.word
.word
.word
.word
.set
0CDh
087h
013h
026h
8
29
Application Report
SPRA530
hgenCRC .set
lgenCRC .set
inport
.set
0482h
0009h
0h
.asg
NOP,PIPELINE
************************************************************
*
*
Stack setup
Reset vector
************************************************************
BOS
.usect "stack",0fh
; setup stack
TOS
.usect "stack",1
; Top of stack at reset
.sect
"vectors"
bd
stm
Entry
#TOS,SP
Reset:
; reset vector
; Setup stack
************************************************************
*
Main Program
************************************************************
.sect
"program"
Entry
.include "c5xx.inc"
rsbx
rsbx
ld
ovm
sxm
#crc,DP ; scratch pad mem -> DP=0
portr
#inport,@nbDataIn
; nb words
addm
#-1,@nbDataIn
; for repeat
30
Application Report
SPRA530
************************************************************
*
************************************************************
stm
#input,AR2
; copy inputwords in RAM
rpt
@nbDataIn
;
portr #inport,*AR2+
; read them from IO space
ld
or
mvdm
stm
#hgenCRC,16,B
#lgenCRC,B
@nbDataIn,AR1
#input,AR2
;
;
;
;
stm
ld
stm
rptbd
stm
stl
#16,BK
#0,A
#16-1,BRC
initbitpos-1
#bitpos,AR3
A,*AR3+%
add
initbitpos
mar
stm
dld
next
calld
stm
banzd
mar
nop
calld
stm
mvdk
mvdk
mvdk
#1,A
*AR3+%
#0,T
*AR2+,A
CRCB
#16-1,BRC
next,*AR1*AR2+
;
;
;
;
;
;
;
;
end of repeat
CRCB
#8-1,BRC
; last 8 bits
; BRC = 8-1
@AG,@crc
@AH,@crc+1
@AL,@crc+2
init T reg
CRC register intialized
perform CRC computation
BRC = 16 - 1
process all input words
AR2 points to next input
************************************************************
*
************************************************************
stm
#input,AR2
; AR2 points to inputword
mvdm
@nbDataIn,AR1
; AR1 = length of message
ld
#0,A
; clear ACC A
next1
stm
stm
#t_start,AR3
#3,T
calld
ld
ld
calld
and
banz
CRCT
*AR2,-8,B
*AR2+,B
CRCT
#0FFh,B
next1,*AR1-
;
;
;
;
;
;
; process MSByte
; BL = MSByte
31
Application Report
SPRA530
done
mvdk
mvdk
mvdk
@AG,@crc
@AH,@crc+1
@AL,@crc+2
done
************************************************************
* CRCB routine : bit-wise CRC calculation
* input :
*
*
* ouput :
CRCB_end-1
*AR2
A,1,A
*AR3+%,T
1,TC
#1,A
1,C
B,A
; repeat block
; Test bit #(15-T)
; A = A << 1
; T = next bit position
; if TC=1, A(LSB) = 1
; if C=1, A = new CRC
CRCB_end
ret
************************************************************
* CRCT routine : standard lookup table CRC calculation
*
with bytes as input
* input :
*
*
*
*
* ouput :
1) 8-bit words in AL
2) AR3 = LTU start address
3) DP = 0 = DP(temp)
4) SXM = 0
4) FRCT = 0
CRC value in AL
* A, B, AR4, SP modified
* code size = 18 words
* cycles = 20 cycles
32
Application Report
SPRA530
* A = |A31.............A16 A15................A0|
*
<------- CRC ------->
* B = |B31........................B7........B0|
*
<-- input-->
************************************************************
CRCT
mvmd
AG,@AR4 ; AR4 = CRC >> 32
xor
@AR4,B ; B = (inputByte) XOR (CRC>>32)
;
= Offset in LTU
sfta
A,8,A
; A = A << 8
mpy
@BL,B
add
@AR3,B ; B = Offset + LTU start address
stlm
B,AR4
; AR4 = absolute address in LTU
PIPELINE
PIPELINE
mvdk
*AR4+,@BG
mvdk
*AR4+,@BH
mvdk
*AR4+,@BL
retd
xor
B,A
; A = new CRC
nop
.bss
bitpos
input,259
; augmented message
; (40 bits appended)
.usect "circ",16
; contain bit position
crc
.usect
nbDataIn .set
temp
.set
.sect
t_start .word
.word
.word
.word
.word
.word
.word
.word
.word
.word
.word
.word
t_len
.set
"table"
00h
00h
00h
00h
0482h
09h
03h
086FCh
070Eh
03h
0827Eh
0707h
768
33
Application Report
SPRA530
TI Contact Numbers
INTERNET
TI Semiconductor Home Page
www.ti.com/sc
TI Distributors
www.ti.com/sc/docs/distmenu.htm
PRODUCT INFORMATION CENTERS
Americas
Phone
+1(972) 644-5580
Fax
+1(972) 480-7800
Email
sc-infomaster@ti.com
Europe, Middle East, and Africa
Phone
Deutsch
+49-(0) 8161 80 3311
English
+44-(0) 1604 66 3399
Espaol
+34-(0) 90 23 54 0 28
Francais
+33-(0) 1-30 70 11 64
Italiano
+33-(0) 1-30 70 11 67
Fax
+44-(0) 1604 66 33 34
Email
epic@ti.com
Japan
Phone
International
+81-3-3457-0972
Domestic
0120-81-0026
Fax
International
+81-3-3457-1259
Domestic
0120-81-0036
Email
pic-japan@ti.com
Asia
Phone
International
+886-2-23786800
Domestic
Australia
1-800-881-011
TI Number -800-800-1450
China
10810
TI Number -800-800-1450
Hong Kong 800-96-1111
TI Number -800-800-1450
India
000-117
TI Number -800-800-1450
Indonesia
001-801-10
TI Number -800-800-1450
Korea
080-551-2804
Malaysia
1-800-800-011
TI Number -800-800-1450
New Zealand 000-911
TI Number -800-800-1450
Philippines 105-11
TI Number -800-800-1450
Singapore
800-0111-111
TI Number -800-800-1450
Taiwan
080-006800
Thailand
0019-991-1111
TI Number -800-800-1450
Fax
886-2-2378-6808
Email
tiasia@ti.com
34
Application Report
SPRA530
IMPORTANT NOTICE
Texas Instruments and its subsidiaries (TI) reserve the right to make changes to their
products or to discontinue any product or service without notice, and advise customers to
obtain the latest version of relevant information to verify, before placing orders, that
information being relied on is current and complete. All products are sold subject to the
terms and conditions of sale supplied at the time of order acknowledgement, including
those pertaining to warranty, patent infringement, and limitation of liability.
TI warrants performance of its semiconductor products to the specifications applicable at
the time of sale in accordance with TI's standard warranty. Testing and other quality
control techniques are utilized to the extent TI deems necessary to support this warranty.
Specific testing of all parameters of each device is not necessarily performed, except
those mandated by government requirements.
CERTAIN APPLICATIONS USING SEMICONDUCTOR PRODUCTS MAY INVOLVE
POTENTIAL RISKS OF DEATH, PERSONAL INJURY, OR SEVERE PROPERTY OR
ENVIRONMENTAL DAMAGE (CRITICAL APPLICATIONS"). TI SEMICONDUCTOR
PRODUCTS ARE NOT DESIGNED, AUTHORIZED, OR WARRANTED TO BE
SUITABLE FOR USE IN LIFE-SUPPORT DEVICES OR SYSTEMS OR OTHER
CRITICAL APPLICATIONS. INCLUSION OF TI PRODUCTS IN SUCH APPLICATIONS
IS UNDERSTOOD TO BE FULLY AT THE CUSTOMER'S RISK.
In order to minimize risks associated with the customer's applications, adequate design
and operating safeguards must be provided by the customer to minimize inherent or
procedural hazards.
TI assumes no liability for applications assistance or customer product design. TI does
not warrant or represent that any license, either express or implied, is granted under any
patent right, copyright, mask work right, or other intellectual property right of TI covering
or relating to any combination, machine, or process in which such semiconductor
products or services might be or are used. TI's publication of information regarding any
third party's products or services does not constitute TI's approval, warranty, or
endorsement thereof.
Copyright 1999 Texas Instruments Incorporated
35