4
Hamming Error Correcting Code Michael Wimble 6026 Underwood Av SW Cedar Rapids IA 52404 One of the most frustrating aspects of computers is that they make errors. Large computers have ample redundancy and error correcting hardware to make these errors virtually nonexistent, but owners of smaller computers must typically live with the problem. These errors are not all due to unreliable hardware; theyare also caused by noisy environments, line crosstalk, power fluctuations, thermal variations and so on. To meet these problems several tech niques have been developed. One of them is called Hamming codes. The use of Hamming codes is analogous to the use of the common parity bit. A single parity bit merely provides detection of single bit errors, however. In contrast, the Hamming code described herein cor- rects single bit errors and detects double bit errors. An ideal use for Hamming codes is in cassette recording where single bit drop- outs due to tape inconsistencies are common. Larger computers also use Hamming codes to detect and correct memory errors. Alas, as with all real systems, you don't get something for nothing. To record eight bits of data without any error detection or correction scheme requires only eight bits of memory space. To use the common parity bit approach adds only one more bit of data: nine bits ofmemory space. But the Hamming code described here requires eight extra bits of memory space for every eight bits of data recorded. There are other Hamming codes available which use con- siderably less extra memory space per data space, but this one is particularly appro- priate for m icrocom pu tersand for cassette recording, the most frequent source of errors in a typical hobbyist microcomputer system. No attempt will be made to discuss the mathematics behind Hamming codes. The reader is referred to any good book on data transmission or error correcting codes for more information. Building the Hamming Code Building the Hamming code information is as simple as a table lookup. Every byte (eight bits) of data is recorded a nybble (four bits) at a time. Also, each group of four bits of data has four bits of Hamming dataappended so that each byte of data to be recorded actually requires two bytes of storage. Recording a byte of data is straight- forward. Take the leftmost (most significant) nybble of data and record the corresponding byte of data shown in table 1. Next take the rightmost orleast significant nybble of data and, again, record instead the corresponding byte fromtable 1. For example, to record the data byte, hexadecimal 1F, perform the following: Extract the leftmost nybble (hexa- decimaI01). Replace with corresponding byte from table 1 (hexadecimal E1). Record the byte. Extract rightmost nybble(hexadecimal OF) Replace with corresponding byte from table 1 (hexadecimal FF). Record the byte. Thus the single hexadecimal byte 1F is actually recorded as the two hexadecimal bytes E1 FF. If it is not already obvious, eachbyte in table 1 has the actual data in its rightmost nybble and error correcting data in its leftmost nybble. Reconstructing the Data We are now ready to see how the Ham- ming code functions. As each byte of stored data is retrieved, with or without errors, four parity bits are constructed which give information as to the correctness of the retrieved byte. Using these parity bits, any required corrections are made to the re-

Hamming ErrorCorrecting Code

  • Upload
    others

  • View
    35

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Hamming ErrorCorrecting Code

Hamming Error Correcting Code

Michael Wimble

6026 Underwood Av SW

Cedar Rapids IA 52404

One of the most frustrating aspects ofcomputers is that they make errors. Largecomputers have ample redundancy and errorcorrecting hardware to make these errorsvirtually nonexistent, but owners of smallercomputers must typically live with theproblem. These errors are not all due tounreliable hardware; they are also caused bynoisy environments, line crosstalk, powerfluctuations, thermal variations and so on.To meet these problems several tech niqueshave been developed. One of them is calledHamming codes.

The use of Hamming codes is analogousto the use of the common parity bit. Asingle parity bit merely provides detectionof single bit errors, however. In contrast,the Hamming code described herein cor-rects single bit errors and detects doublebit errors. An ideal use for Hamming codes isin cassette recording where single bit drop-outs due to tape inconsistencies are common.Larger computers also use Hamming codesto detect and correct memory errors.

Alas, as with all real systems, you don'tget something for nothing. To record eightbits of data without any error detection orcorrection scheme requires only eight bitsof memory space. To use the commonparity bit approach adds only one more bitof data: nine bits of memory space. Butthe Hamming code described here requireseight extra bits of memory space for everyeight bits of data recorded. There are otherHamming codes available which use con-siderably less extra memory space per dataspace, but this one is particularly appro-priate for m icrocom pu ters and for cassetterecording, the most frequent source of errorsin a typical hobbyist microcomputer system.

No attempt will be made to discuss themathematics behind Hamming codes. Thereader is referred to any good book on datatransmission or error correcting codes formore information.

Building the Hamming Code

Building the Hamming code informationis as simple as a table lookup. Every byte(eight bits) of data is recorded a nybble(four bits) at a time. Also, each group offour bits of data has four bits of Hammingdata appended so that each byte of data tobe recorded actually requires two bytes ofstorage.

Recording a byte of data is straight-forward. Take the leftmost (most significant)nybble of data and record the correspondingbyte of data shown in table 1. Next take therightmost or least significant nybble of dataand, again, record instead the correspondingbyte from table 1.

For example, to record the data byte,hexadecimal 1 F, perform the following:

• Extract the leftmost nybble (hexa-decimaI01).

• Replace with corresponding byte fromtable 1 (hexadecimal E1).

• Record the byte.• Extract rightmost nybble (hexadecimal

OF)• Replace with corresponding byte from

table 1 (hexadecimal FF).• Record the byte.

Thus the single hexadecimal byte 1 F isactually recorded as the two hexadecimalbytes E1 FF. If it is not already obvious,each byte in table 1 has the actual data inits rightmost nybble and error correctingdata in its leftmost nybble.

Reconstructing the Data

We are now ready to see how the Ham-ming code functions. As each byte of storeddata is retrieved, with or without errors,four parity bits are constructed which giveinformation as to the correctness of theretrieved byte. Using these parity bits, anyrequired corrections are made to the re-

Page 2: Hamming ErrorCorrecting Code

The timer processor scans the file ofdelay timer records (table 7) to processthose records which are active. The func-tion of the delay timer record is to providea delay of a preset number of seconds beforeprocessing an event record. To demonstratehow this function works, let's assume thatone wishes to activate an audible alarm 50seconds after a particular sensor has beentripped. You would structure the event andresponse records so that the tripping of thesensor would activate the delay timer recordyou were associating with this event. Theactivation of the delay timer causes theactive flag to be reset and the timer activa-tion value (bytes 2 and 3) to be transferredto the delay time. In our example this willcause the value 50 to be loaded into bytes 0and 1 of our delay timer value.

As each real time clock interrupt causesthe timer processor to scan the file of delaytime records, it will find that our record isactive. Once the processor determines that arecord is active it will decrement the currentdelay time remaining and then check to seeif the time remaining is zero. If there is stilldelay time remaining (time greater thanzero) no further action is taken.

However, if there is no time remaining(time equals zero) the active flag is set, thecurrent registers saved, the address of theevent record is extracted from the delaytime record, and control transferred to theevent processor. The sequence of operationsperformed from this point will directlyresult in the audible alarm being turned on.

The processing of the records in the timeof day file (see table 6 for record format) isperformed upon the completion of all delaytime records. Time of day records are proc-essed in a manner very similar to thatdescribed for delay timer records. When anactive record is encountered during the scanof the time of day file, the current time ofday being maintained by the system will becompared to the time of day specified inbytes 0 and 1 of the record. Should thesetimes be identical, the records flag is set to 1and the processing of the event record speci-fied in bytes 2 and 3 is initiated as describedabove. After servicing all records in the timeof day file, control is returned to the inter-rupted modules.-

Next Month: Part 3 of the Compu-terized Security System illustrates thedesign of some of the sensors, giveslistings of a few of the control modules,and describes the design of the remotesensor display panel.

LIST SPECIAL

PRICE PRICE

Solid State Music $149.95 $127.458080 CPU Board Kit

Mountain Hardware$329.00 $279.00Introl System

IMC Keyboard $169.00 $143.65

All Versions of Electric Pencil 15%off

Problem Solver - All Products 15% off

Vector Graphics15% offAll Products

Imsai - All Products 15%off

COMPUTER LAB OF NEW JERSEY141 ROUTE 46

BUDD LAKE, NEW JERSEY 07828(201) 691-1984

SURPLUS PRINTER FOR TRS80 *OKIDATA CP110LINE PRINTERWITH INTERFACEBOARD.

NO SOFTWARE OR HARDWARE CHANGESREQUIRED. JUST PLUG IN AND RUNI

• 5x7 Impact Dot Matrix• 80 Char/Line• 64 Char ASCII (Upper Case)• 110 Char/Sec.• 66 Lines/Min.• Accepts 8·1 /2" TTY

Roll paper

PRINTER $650.00INTERFACE: BUILT $100.00

KIT $ 60.00INFOR& SCHEMATIC $ 5.00

Shipped Freight collect. Send check,

M.a.; ~.

INCLUDES· Power Supply, Built inSelftest, Parallel Interface, Line Buf-fer and Cables. Housed in a threepiece plastic cabinet with all controlelectronics. Retail for over $1 ,100.PRINTER BRAND NEW NEVER USEDIN FACTORY SEALED CARTON.Operating Manual Included. Supplies

Limited

Guaranteed to be in good workingorder at time of delivery.Write for Interface Info on Heath,Apple, Imsai, Sol, Northstar

INTERNA T10NAL ELECTRONICSEQUIPMENT CORP.

P.O. Box 522542, Miami, Florida 33152

Page 3: Hamming ErrorCorrecting Code

Data Nybbleo1234567

Hamming Byte00E172938455C627D839AA486C8D1EFF

Table 7: Table for transforming data nybblesinto Hamming code bytes for storage.

trieved byte after which the original nybbleof data is extracted.

Parity bit 4 (P4), the most significantparity bit, is simply the parity of the entire

bit retrieved byte. If the 8 bit data wordnas an odd number of 1s, then P4 is setto 1. Similarly, if the 8 bit data word has aneven number of 1s, P4 is set to O. To formparity bit 3 (P3), the 8 bit data word is firstANDed with hexadecimal 27 and the 8 bitparity of the result determines P3 just as P4was calculated. Likewise parity bit 2 (P2) isformed by ANDing the retrieved dataword with hexadecimal 4B; parity bit 1 (Pl),the least sign ificant parity bit, is formedfrom ANDing the retrieved data word withhexadecimal 80. Table 2 shows a samplecalculation of the parity bits when hexa-decimal is retrieved.

Error detection and correction is per-formed by detecting one of three cases:

• If all four parity bits are 0, the re-trieved data word is correct. Theoriginal nybble of data is correctlyformed in the rightmost nybble of theretrieved data word.

• If P4 is 0 but one or more of theparity bits P1, P2, or P3 is not 0, thena double bit error has occurred. Theprogram should probably inform theoperator and then halt.

• If P4 is not 0, a single bit error hasoccurred, which can be corrected.

To correct single bit errors, a byte chosenfrom table 3 is exclusive-ORed with the'"etrieved data byte. Parity bits P3, P2, and1 (P3 is most significant and P1 is least'oificant) determine which byte to selectn table 3. Table 4 demonstrates the cor-')n and detection process for several~nt cases.

Data Word01100111011001110110011101101111

AND Result Parity Bit01100111 P4 = 1

00100111 00100111 P3 = 001001011 01000011 P2 = 110001101 00000101 P1 = 0

Table 2: A sample calculation of parity bits for the binary data word07 7007 7 7 (hexadecimal 67). Since P4 is not 0, a single bit error has occurred,which can be detected (see text).

P3 P2 P1 Error Correction Byte0 0 0 100 0 1 800 1 0 400 1 1 081 0 0 201 0 1 041 1 0 021 1 1 01

Table 3: Single bit error correction table.

Data ANDE1E1 27E1 48E1 8D

ParityP4 = 0P3 = 0P2 = 0P1 = 0

CommentsAll parity bits 0 implies no error.

Case2: Single bit error.

Data AND Parity CommentsE3 P4 = 1 P4 not 0 implies single bitE3 27 P3 = 1 correctable error.E3 48 P2 = 1 P3P2P1 = 110E3 8D P1 = 0 Corrector from table 3 = 02

Data E3Exclusive OR 02Correct data E1

Case3: Single bit error.

Data AND Parity CommentsC1 P4 = 1 P4 not 0 implies single bitC1 27 P3 = 1 correctable error.C1 48 P2 = 0 P3P2P1 = 100C1 8D P1 = 0 Corrector from table 3 = 20

Data C1Exclusive OR 20Correct data E1

Case4: Double bit error.

Data AND Parity CommentsE2 P4 = 0 P4 equal to 0 and P3, P2, or P1E2 27 P3 = 0 not 0 implies a double bit error.E2 48 P2 = 0 Note: data in P3, P2, and P1 forE2 8D P1 = 1 a double bit error does not

imply any information aboutwhich bits are in error,

Table 4: Examples of uses of the Hamming code. For each ca~pthe transmittedinformation is hexadecimal £7. When 0.7 bit error is detected, the parity bitsP3, P2, and P7 are used to look up a correcting factor from table 3.

Page 4: Hamming ErrorCorrecting Code

ProfessionalEquipment

• Z80A cpu, 32 KRA,\1 and PerSciS" dual drives.

• Iligh speed line printer*,150 characters per second.

• Word processing printer* with metalprint wheel.

• Video terminal with capacitive keyhoard,24 x SO display. *Choice of one.

TEXT 2000tm Word Processing CPA 2000tm Business Accounting• True proportional spacing • Accounts Receivable• Full-screen text editing with • Accounts Payable

simple commands. • General Ledger -(multi-co.)• Bidirectional, bold face, under- • User defined formats for bal.

lining, tab, right and left justi- sheet, income statement, chan-fication, centering, auto. pag- ges in financial position.ination, name & address list. • Integrated A/R, A/P, G/L.

Complete program development software - Basic, Fortran,Cobol, Pascal, macro-assembler, dehugger- available.INFO 2000 Business System, complete with TEXT 2000 andCPA 2000 software, can be purchased for less than $13,000.90-day warranty with system. Extended warranty available. Writefor complete information packet. Dealer inquiries welcome.

Conclusion

The algorithm described here has beenprogrammed on several different micro-computers. Not many bytes of program areactually needed and the benefits are great.One need only read a large program fromtape into main memory several times torealize the utility of the approach. If yourcurrent read in software informs you ofany errors encountered, you probably muststill find the errors and correct them, as-suming you even know what the correctdata should be.

Hamming codes can be extended to cor-rect double bit errors, or to perform anypractical n bit detection and m bit cor-rection, but the extra memory costs canclimb fast. Also it is very easy to builda hardware Hamming generator and de-tector using only a few discrete integratedcircuits.

The Hamming code described here isboth practical and valuable. Manufacturersshould seriously consider incorporating thistechnique in hardware. The cassette enthu-siast should incorporate this technique i

any standardization or interchange effor •.Even the everyday experimenter will findthat the hour spent programming the algo-rithm will save many hours of frustrationin the future.-

COMPARETHESEUNIQUE FEATURES:.PerSci Dual Disk Drives• Full size S" floppy diskettes.

• $hundreds less than comparablesystems.

• Intelligent controller boards for eachtype of bus increases computer capabilities.

• Additional I/O ports and drivers.·Cp/M * Disk Operating System.• Voice-coil positioning 8 times faster than others..1 BM 3740 compatible.• Takes less than 1/2 the space of others.·Includes cabinet, power supply, fan and cables.

• Factory assembled andtested for immediateplug-in-and-gooperation.

• Extensive softwarelibrary available:Basic, Fortran, Cobol,Pascal, macro-assem-bler, debugger.

• Backed by 90-daywarranty.

Write for complete informa-tion packet. Dealer inquiries

we/come.

*Trademark ofDigital Research

INF.!•••CORPORA nON(213) 532-1702

20620 S.Leapwood Ave., Carson, California 90746