JCrenshaw ImplementingCRCs
JCrenshaw ImplementingCRCs
t an unrecorded event
A
lost in the mists of time,
a caveman named Og
sent a message propos- Most of you have
ing peace, via smoke
,ignal. to the signal officer of the neigh- already heard of
boring tribe. Unfortunately, a gust of
~ ind disrupted the pattern of a particu- the CRC, and
htrly important part of the message, and
the message was misunderstood. Sadly, you've almost
Og's tribe was wiped out.
Ever since then, people have been certainly used it,
sending messages by methods ranging
from smoke signals to laser beams to whether you are
spread-spectrum communications sat-
ellites, and messages have been arriving aware of it or not.
garbled, with more or less disastrous re-
sults. It's not surprising, then, that peo- We'll begin with some background as to
ple have long sought ways to make sure the motivation and power of the CRC,
that their messages were received prop- then continue on to the gory details. I'll
erly. Lots of methods have been tried spend just enough time discussing poly-
over the years. nomials to convince you that they're not
Today, one of the most effective and needed, and then we'll get on to some
popular error-detection methods is the efficient code implementations. I'll also
cyclic redundancy check (CRC). It's discuss some of the gotchas you should
used in virtually every field where trans- beware of.
mitting serial data is involved. It's even With any kind of luck and some per-
built into your disk-drive controller. sistence on your part, you'll leave this
Most of you have already heard of the article with a real understanding of how
CRC, and you've almost certainly used and why the CRC works. At the very
it. whether you are aware of it or not. least, you should be able to say, "Search
Though ubiquitious, the CRC algo- me. I just copied it from Crenshaw.~
rithm is surrounded by an inordinate
amount of fog. We can't even seem to SOME BACKGROUND
agree on what the acronym stands for ver since the landmark Og af-
(some say it's cyclic redundancy code).
Ask hardware people how it works, and
they're likely to say, "It's done with shift
E fair, people have been seeking
ways to make sure that their
messages get across without being mis-
registers and exclusive-OR gates." Ask understood. In today's information age,
mathematicians, and they'll say, "It's that's more important than ever. We
done using a polynomial division, mod- can accomplish this goal in many ways,
ulo two, with the generating polynomial but they all boil down to these three
chosen from a closed Galois field." (Oh. requirements:
Thank you very much. That clears ev-
erything right up.) Ask honest software • Along with the message, provide the
types, and they'll tell you, "Search me recipient with some way to know if it got
how it works. I just copied it from the there correctly.
XMODEM code." • The recipient should send a return
[n this article, I'm going to try to cut message, acknowledging receipt or ask-
through some of that obscuring fog. ing for a retry.
• Continue to send the message until it magic decoder rings to the body of the
gets to its destination ungarbled. message, and compare the result to the
code that was received. If they don't
Effective data transmission involves match, the message must be in error. To
two aspects: error detection and error be efficient, the code should be small
transmission. The CRC is an error de- compared to the message body. or
tection mechanism. Yes, I know that course, the smaller the code. the more
some error detection and correction the possibility of aliasing, where t\\()
methods such as the Hamming codes different messages can generate the
exist, but today, almost everyone still same code. But with modern methods
separates the two aspects and counts on such as the CRC, this scenario is ex-
retransmission for the correction half of tremely unlikely, as we shall see.
the system. The smallest code is a single bit.
How can the recipient of a message That's the idea behind the parity bit,
tell whether it's been garbled? The most which is a measure of the number of
obvious way is to send every message ones in the message's binary represen ta-
twice. But that method involves a lot of tion-zero for an even number, one for
overhead-at least double the trans- an odd number. Since it's short, it's also
mission time; much more if the error easy to get aliasing, so parity bits only
rate is high enough so that errors are work for very small message blocks
likely. And that's bad. Time is money. -typically one byte.
We can make things a little better by Another choice is the checksum. It's
splitting the message up into smaller a popular one, because it's so easy to
blocks, and seeking confirmation on understand and compute. We simply
each block. This way, we don't have to add all the bytes in the message as
retransmit the whole message, but only though they were binary numbers (ig-
the blocks that were garbled. But we're noring carries). Typically, the resulting
still limited to a minimum of twice the sum is appended to the message as its
transmission time. What we really need twos complement, so that the recipient.
is a method to test the message for cor- adding the same characters plus the
rectness, without using many bits of the checksum, should get zero. Intel hex
message. and Motorola S-record formats use ei-
Of course, the ancient kings knew ther a 1- or 2-byte checksum.
how to authenticate messages. They Finally, we have the CRe. As nearly
used a courier and sealed the messages as I can tell, it arrived on the scene in
with their private seal, carefully mono- 1961. The idea behind it is exac{ ir the
grammed to discourage forgery. No one same as for the checksum: compute <.l 1-
could tamper with the message without to-4-byte BCC, calculated from the
breaking the seal. We can't do that with message body, and append it to the mes-
messages that are sent electronically, sage. The only difference is the algo-
but we can steal the general idea. You rithm embodied in the magic dccoder
append some kind of code to the mes- ring.
sage that's virtually certain to be al-
tered if the message is tampered with or WHY USE A CRC?
otherwise garbled. little reflection should con-
Early on, someone got the idea to use
a code (sometimes called the block-
check code, or BCC) that could some-
A vince you that the reliability of
any error-detection 1l1\,;c...ha-
W
• 99.9969% of all bursts 17 bits wide
.99.9985% of all bursts wider than 17 explain the CRC to me, I
bit'>(the same as a checksum) was told, "It's the remain-
der of a polynomial division, modulo
I-ur extreme reliability, using 32 bits two." I was then given the generating
improves this performance even more, polynomial:
by five orders of magnitude! Those stat-
istics are pretty impressive, which is
\\ hat makes the CRC so popular.
The nature of the checksum is such Huh? What the heck does that mean?
that errors tend to be localized, some- And what is x, anyway? A data bit?
times changing only a single bit. Offset- Shall I give it a value?
ting or systematic errors (say, a stuck A more meaningful way to under-
bit on the UART) can often be missed. stand what's going on with CRCs must
B) contrast. any error in tHe message exist. It's easier if we first digress into
4uid,l:- propagates into all the bits of the related field of psuedo-random
numbers. Let's playa little game, of the It is a shift register, with one exclusive-
kind'often seen in I.Q. tests or math puz- ORgate added. Place any number in the
zle books. Look at this a sequence of sequence into the bits of this register,
numbers: mentally cycle it, and you will soon con-
vince yourself that it does indeed gener-
ate the IS-number sequence. It's a
source of psuedo-random numbers, bet-
Quick! What's the next number in the ter known as white noise, and you'll find
sequence? one in every Nintendo game. Of course,
in the real world, these generators use
If you didn't say 15, please move on to more than four bits. In general, an N-bit
the next article. This one's not for you. psuedo-random number generator can
For the rest of you, please give me the generate 2N-1 unique numbers before
next number in this sequence: repeating. The -1 is there because we
can't allow the value zero. If the gener-
ator ever gets all zero bits, no feedback
can occur, so it will remain at zero. Un-
Not so easy, is it? (The answer is one.) fortunately, not just any old set of feed-
It's not supposed to be easy, because the back ta ps (the exc1usive-OR ga tes) wiII
second sequence is a set of what are do. For example, if the gate were at Bit I
called pseudo-random numbers. The instead of Bit 0, we would end up with
"psuedo" part is because they are not three shorter sequences:
really random. The sequence will repeat
over and over forever. But by every oth- a.l,10,5,8,4,2
er measure of randomness you care to b.3, 11, 15, 13, 12,6
apply (average, standard deviation, and c. 7,9,14
so on), the sequence is random. Hence
the name. The sequence covers all the 4- Now, you begin to see the tricky part
bit integers, except zero. about this process. We want a maxi-
How did we get this sequence? In mal-length sequence, and to do get it,
this case, the magic decoder ring is a the taps must be carefully chosen. And
hardware circuit, as shown in Figure 1. that's where the polynomials come in.
Figure 1
A hardware circuit.
Implementing
eRes For a maximal-
length pseudo-
random number
We look for factors of polynomials the
sequence, you same way as with integers: by trial and
POLY-CONfUSED error, using division. We learned poly-
y foray into the theory be- need a polynomial nomial division (sometimes called syn-
.. '::~~'~,T~leclzallellge Hamilton!Avnet 0
~11;~~tt1,~ lowest cost, alld Frallkllll IS,tf',~rF:.wg~I,"9~'fQols t~lat IlClp YOll meet It." systems engineerin
our extensive valm
capabilities.
:-ranklin Introduces 8051 "e" Introducing RTX -51 Operating System!
It's the first real time multi-tasker designed specially for
Put them all toget
result is superior Sl
Professional Developers Kit! the 8051. RTX-51 is very small, as little as 800 bytes for
code, so it works even on single chip versions of the 8051.
Hamilton! Avnet 1II
building a reputatil
And task switches are very fast! With a full set of as fea- So see the differenc
landle Bigger Jobs. tures, and support for all 8051 variants, RTX-51 opens up For details, call Hal
)ur Best in the Industry "C" compiler now supports new vistas for 8051 development! free, 1 (800) 442-64!
llulti-tasking and bank switching with up to 16 banks of
temory for code and data. No other bank management Move up to the Professional Developers Kit!
:heme is as code and execution time efficient. Call (408) 296-8051 or FAX us at (408) 296-8061
for your FREE evaluation package!
'he Fastest Just Got Faster.
v'ith all its new features, the C-51 Compiler is IARlArchlrnede.
FRANKUN BSOI
ill the fastest, most code efficient "C' PROFESSIONAL TASKING 1CC8051
8,753
Jmpiler available. And for big jobs, C-51 Total Execution Time
(large model)
6.744s9C
8.287 see 10,98
8,388
17,295
. faster than ever for both compile and Module Code Size 188 bytes 266 238
(large model) 188by1es 303 343
xecute times! dYNlmlc Datil Sin 41 bytes 126 96
Total Code Size 1223 bylas 1936 1626
.-
(large model) 1292 bytes 3467 2088
WhetstoneSlSecond
Ohryatonel!Second
13
--,., 3
203 163 90
Figure 3
The easy way.
111
down all those polynomial terms gets form. Looks a lot easier, doesn't it? In
tedious, especially if we're talking about fact, it's simply binary division, but us-
16th-order polynomials! Fortunately, ing exclusive ORsinstead of subtraction.
once I'd done this problem a few times, I Since it's binary, of course, you don't
finally figured out that it's not neces- really need to perform a multiplication.
sary to write the xs down at all A t each step, we either su btractthe divi-
-just the coefficients. Figure 3 shows sor or we don't. We don't need to com-
the same division in this simplified pare the two terms (we don't worry if
one polynomial is bigger than the other.
Since addition and subtraction are the
same, the concept of "bigger" does not
On Target with Turbo Debug! exist). We only have to look at the lead-
ing terms. If they have the same order,
we subtract. YOl
Now, we get rid of the second fog corrun
factor. From now on, when someone solvinf
starts talking to you about polynomial
division, you will know that what they
embed
really mean is ordinary binary division proble:
-modulo two. HUI
In Figure 3, each product to be sub-
tracted is written down one place to the
money
right, which is equivalent to shifting the neers n
dividend to the left. So, what we're real- worke<
ly talking about is shifting, then sub- scale p:
tracting (or exclusive OR-ing)a constant
Place your Microsoft or Borland C Code in the target system and debug value. But that's exactly what's going on
it often
it as it runs with Datalight's C_thru_ROM ... in Figure I. The operations are entii'Cly of the r
C _thru _ROM eases ROM development with all versions of Borland-C, C++. and Micro-
soft C. New features include Turbo Debug support and remote debugging via a ROM socket.
equivalent. today's
We only have a couple of annoying,
C _thru _ROM is a complete ROM development package that works with your choice of
compiler to quickly get your application running in ROM. Includes: full 80x86 locator but minor, complications. First, the
and 32-
which outputs in Intel OMF, hex, and binary; ROMabie startup code; remote debugging via shift register shifts right, while the divi- AtA
Turbo remote debugger or Code View-like debugger; full floating point support; ROMabie
library (prinrj, mal/oc, etc.); and much more. Call, write, or fax today for full details.
sion operation shifts the dividend left. we're h
We'll just have to remember to reverse
Free Demo Disk! Call Today Toll-Free 1-800-221-6630 the bit order.
hardw2
Second, the division subtracts first, betterE
and then shifts for the next round. In the
17455 68th Ave NE. Suite 304. Bothell, WA 98011, USA· 206-486-8086' fax 206-486-0253 shift register, we look at the least signifi- In the u.s. and C
44-(O}~2S16
CIRCLE # 15 ON READER SERVICE CARD
cant bit (LSB) shift, then perform the tact Appllt.'d Micro
regiSiered lndema
responds to the lowest-order term in the
Implementing polynomial; that is, XO or one. The three
representations are shown together in
(617) 828-5588
to make sure the message is 0, K. ROM, datil breilkpoints. stepping.
Remember that when a checksum
(rather than CRC) is used, we usually
tracing. view Imodify registers and
memory, and system diagnostics,
at a fraction of the price of a full -
Embedded
Support
Tools Corp.
III
Wilfii.iili.iWyQv I
transmit its twos complement, so the fi-
blown ICE.
nal sum is zero for an error-free mes-
sage. The CRC works in a similar way,
337
though we don't even have to comple-
ODS ment. Suppose that we've sent a mes-
~uipment
IC.,
l6-bit CRCs is the tap positions, which
Implementing are embodied in the variable FeedBack.
The SendString procedure sends a
We're not really
CKCs sending the
string of bytes. The SendMessageproce-
dure initializes the BCC and sends its
final value. We have to save the value,
since it will continue to be updated as its
message value is sent.
We're not really sending the mes-
anywhere, so I sage anywhere, so I added a little debug
clusive ORis zero. Nothing gets fed back output to give the value of the computed
during the shift. If no errors have oc- added a little CRe. The final value printed should al-
curred, the same will be true for every ways be zero.
succeeding bit. Nodata is ever fed back, debug output to By the way, one of the things sorely
so the BCC just gets shifted out, leaving needed in working with this algorithm is
a zero value. In this case, zero is the give the value of some test-case data. Fortunately, Greg
winning score! Morse was kind enough to give us some
the computed CRC. test cases, which I've supplemented a
PROGRAMMERS DO IT IN SOFTWARE bit in Table I (p.33). They're an invalu-
P until now, I've emphasized ing I (p.31), written in Turbo Pascal. able aid for debugging.
Listing 2
A table-driven algorithm.
Procedure MakeTable;
var L Integer;
begin
for 1 ;" 0 to 255 do begin
CRC ;= 0;
ProcessByte(i) ;
Table[i) ;" CRC;
end;
end;
'0
Affordable Pe ormance
for the 64180 & Z180
Listing 4 Price and performance are no programs that mysteriously "wander
Bjtewlse clIc without a table. longer mutually exclusive. Softaid's off." In C or assembly, the UEM
I This Procedure PernUs Byte.1se 8-bit UEM Emulators find bugs fast. automatically tracks the MMU.
CRCWithout the Need for a table. The 'table
And only Softaid has the Fiber-Optic Collect real-time trace data with
Link that reduces download time to pre, post, or center triggers. Display it
entry" is bul1t 'on the fly" Note that the
virtually zero. Spend a lot r-P-ro-c-e-s-s-o-r--S--d-' in C source or
algor1ttll is spec1f1c to the CCITYCRC. less time waiting, and a lot pee intermixed with the
Other codes can be done in s1Ml1ar I18nner, l more time debugging. 64180 10MHz disassembled trace
All of Softaid's UEM data. Or, use your own
Emulators include built-in 641808 10MHz clock to collect logic
JOklet Procedure UpdateCRC(B: 8yte);
'(be 386", performance analysis and ..,cZ:1=8:l10B::IlIllZ:J1EOsz::M:lHczd analyzer data.
begin a source-level debugger Is there a better
8 :" 8 Xor Lo(CRC); for C, C++ and PUM. Our real-time solution than Softaid? At $5.495,this
Memory Access Monitor instantly may be the best 8-bits of advice you
8 :·8 Xor (8 Shl 4);
captures "stack creep" and those ever get,
CRC :' (CRCShr 8) Xor (B Shl 8) Xor
(8 Shl 3) Xor (8 Shr 4);
end;
8300 Guilford Rd.• Columbia, MD 21046
(800) 433-8812' FAX: (301) 381-3253
"OFIAID, Inc.
--J
CIRCLE # 26 ON READER SERVICE CARD
shown in Figure 7 (p.44). The dotted
line is to emphasize that the resulting
BCC really consists of two parts:
Listing 5
The XMOD~ algorithm.
STOP WASTING
{ The XMODEMAlgor1thll shUts left TIME & MONEY
<\ND, { instead of right. This impacts most of the BSO/TASKING'S new toolkit
{ procedures. for developingsoftware for
NT, the 68000 family,called
TAsKTOOLS~, beats all others
on the market today. Check out
these features and benefits.
for Procedure Send8yte(B, Byte): CCOMPILER
lIyzer begin •ANSIC
CRC := (CRC Shl 8) Xor Table[B Xor H1(CRCl]: •Highly optimized code
rs
·Reentrant code
a end:
•Direct control of I/O BENEFITS
·Interrupt handlers may be TAsKTOOLS~ offersyou the
written in C followingbenefits:
·Floatingpoint support ·Reduced code size
Procedure NakeTable, ·68040and 68332 support ·Increasedproductivity
var 1: Integer: CROSSVIEW~ DEBUGGER ·Great documentation
z: Byte, ·All major emulators supported •Hot line support
begin ·Multi-window interface • For PC, Sun, Vax,HP,Apollo,
•Code & data breakpoints ffiM&DECRISC
for 1 :" D to 255 do begin •Multi object formats
• Sourcelevel tracing
z :" 1 Xor (1 Shr 4),
·Stack tracing OTHER PROCESSORS
Table[1] := z Xor (z Shl 5) Xor (z Shl 12), ·I/O simulation WITH SIMILAR SUPPORT
end: ·Motorola 68HClJ
AsSEMBLER &. LINKER
end:
·Motorola compatible ·Intel80X86
·FPU & MMU support •Intel 8051& derivatives
{ Message Protocol for XMODEM.Note the two ·Fully featured •TMS32OC25
{ null bytes. and the way the BCC is sent.
·Siemens 80Cl66
CALLUS
1-8llO-458·8276.
Procedure SendMessage(S, String): Outside U.S.A.617-894-7800.
var OldCRC: Word: Fax 617-894-055l.
begin
CRC ,= 0:
SendStr1ng(S) :
OldCRC :' CRC: BSO
Wr1tel" CRC COlIPuted is ')' TASKING
WordLn( CRC):
SendByte(OldCRC) :
SendByte(H1(OldCRC)) :
systems.
Implementing In this case, we can still get the bene-
fits of the bytewise approach, by com-
. ~' 76543210 :
G(x) = x8 + J
Input
Feedback = 80 h
LRCC-16 X "
15 14 13 12 II 10 9 8 7 6 5 4 3 2 I 0 +
G(x)=xI6+ I
Input
Feedback = 8000h
T~
knowingh
fu
tant to you
rithms we
the state 0
Just re.
ject with I
someone t
sure that
whatthea
\\,.
..... .1 ~~_
- I've seen e
comes ano
. -:-~ . --... plain wror
done diffe
WINDING DOWN
hat about wraps up the story for