Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

SOFTWARE

SECTION
EO
SANI TH

DESIGN OF HAMMING CODE


USING VERILOG HDL
VARUN JINDAL ticularly those used in military, need Of these, one state means no error, and
to perform accurately and reliably even n states indicate the location of an

H
amming code is an error- in the presence of noise and interfer- error in each of the n positions.
correction code that can be ence. Among many possible ways to So n+1 states must be discover-
used to detect single and achieve this goal, forward error-cor- able by r bits; and r bits can indi-
double-bit errors and correct single-bit rection coding is the most effective and cate 2r different states. Therefore, 2r
errors that can occur when binary data economical. must be equal to or greater than n+1:
is transmitted from one device into an- Forward error-correction coding 2r e n +1
other. (also called channel coding) is a type or
This article presents design and de- of digital signal processing that im- 2r e k + r +1
velopment of (11, 7, 1) Hamming code proves reliability of the data by intro- The value of r can be determined
using Verilog hardware description ducing a known structure into the data by substituting the value of k (the
language (HDL). Here, 11 corre- sequence prior to transmission. This original length of the data to be trans-
sponds to the total number of Ham- structure enables the receiving system mitted). For example, if the value of
ming code bits in a transmittable unit to detect and possibly correct errors k is 7, the smallest r value that can
comprising data bits and redundancy caused by corruption from the channel satisfy this constraint is 4:
bits, 7 is the number of data bits while and the receiver. As the name implies, 24 e 7+4+1
1 denotes the maximum number of this coding technique enables the de-
error bits in the transmittable unit. This coder to correct errors without request- The (11, 7, 1) Hamming
code fits well into small field-program- ing retransmission of the original in- code
mable gate arrays (FPGAs), complex formation. Hamming code is a typical The Hamming code can be applied to
programmable logic devices (CPLDs) example of forward error correction. data units of any length. It uses the
and application-specific integrated cir- In a communication system that relationship between data and redun-
cuits (ASICs) and is ideally suited to employs forward error-correction cod- dancy bits discussed above, and has
communication applications that need ing, the digital information source the capability of correcting single-bit
error-control. sends a data sequence to an encoder. errors.
The encoder inserts redundant (or par- For example, a 7-bit ASCII code re-
Error correction ity) bits, thereby outputting a longer quires four redundancy bits that can
Use of simple parity allows detection sequence of code bits, called a code be added at the end of the data unit or
of single-bit errors in a received mes- word. These code words can then be interspersed with the original data bits
sage. Correction of these errors re- transmitted to a receiver, which uses a to form the (11, 7, 1) Hamming code.
quires more information, since the po- suitable decoder to extract the origi- In Fig. 1, these redundancy bits are
sition of the corrupted bit must be nal data sequence. placed in positions 1, 2, 4 and 8 (the
identified if it is to be corrected. (If a positions in an 11-bit sequence that are
corrupted bit can be detected, it can Designing (n, k, t) powers of 2). For clarity in the ex-
be corrected by simply complement- Hamming code amples below, these bits are referred
ing its value.) Correction is not pos- The (n, k, t) code refers to an n-bit to as r1, r2, r4 and r8.
sible with one parity bit since any bit code word having k data bits (where In the Hamming code, each r bit
error in any position produces exactly n > k) and r (=nk) error-control bits is the parity bit for one combination
the same information, i.e., error. If called redundant or redundancy bits of data bits as shown below:
more bits are included in a message, with the code having the capability of r1: bits 1, 3, 5, 7, 9, 11
and if those bits can be arranged such correcting t bits in the error (i.e., t r2: bits 2, 3, 6, 7, 10, 11
that different corrupted bits produce corrupted bits). r4: bits 4, 5, 6, 7
different error results, then corrupted If the total number of bits in a r8: bits 8, 9, 10, 11
bits could be identified. transmittable unit (i.e., code word) is Each data bit may be included in
Forward error correction (FEC). n (=k+r), r must be able to indicate more than one calculation. In the se-
Digital communication systems, par- at least n+1 (=k+r+1) different states. quences above, for example, each of

94 FEBRUARY 2006 ELECTRONICS FOR YOU WWW.EFYMAG.COM

CMYK
SOFTWARE
SECTION
the original data bits is included in at is the (11, 7, 1) Hamming code encoder
least two sets, while the r bits are in- that converts a 7-bit ASCII code into
cluded in only one set (see Fig. 2). an 11-bit code word and the Ham-
Calculation of r values. Fig. 3 Fig. 1: Positions of redundancy bits in ming_ Decode.v is the (11, 7, 1) Ham-
hamming code
shows the Hamming code implemen- ming code decoder that converts an
tation for an ASCII character. In the 11-bit code word back into a 7-bit
first step, each bit of the original char- ASCII code after correcting the single
acter is placed in its appropriate posi- bit error, if any. Both these programs
tion in the 11-bit unit. In the subse- have been developed in Verilog HDL
quent steps, the even parities for the and simulated using ModelSim XE III
various bit combinations are calculated. 6.0a. The simulated outputs of the en-
The parity value for each combination coder and the decoder are shown in
is the value of the corresponding r bit. Figs 5 and 6, respectively.
Error detection and correction. Sup-
pose that by the time the above trans- Testing procedure
mission is received, the seventh bit has 1. Download ModelSim XE III starter
changed from 1 to 0. The receiver 6.0a from the Xilinx website and in-
takes the transmission and recalculates stall it in, say, C drive of your com-
four new parity bits, using the same puter. This simulation software is
sets of bits used by the sender plus the Fig. 2: Redundancy bits calculation available free of cost from
relevant parity r bit for each set (see www.xilinx.com/ise/logic_design_
Fig. 4). Then it assembles the new par- prod/webpack.htm
ity values into a binary number in the 2. Create a folder, say, EFYProject
descending order of r position (r8, r4, in C drive and copy Hamming_
r2, r1). In the given example, this step Encode.v and Hamming_Decode.v
gives us the binary number 0111 (7 files from the EFY-CD into this folder.
decimal), which is the precise location 3. Launch ModelSim XE III 6.0a
of the corrupted bit. from the desktop and select New
Once the bit is identified, the re- Project option under File menu bar.
ceiver can complement its value and Now Create Project screen appears.
correct the error. The beauty of the 4. In Create Project window, en-
technique is that it can be easily imple- ter the project name (say, Hamm), se-
mented in hardware and the code is lect the directory path as
corrected before the receiver knows C:\EFYProject, then click OK but-
Fig. 3: Example of redundancy bits calculation
about it. ton. A new dialogue box Add Items
to the Project appears.
Verilog HDL 5. Click Add Existing
program File option and open
Verilog is a general-purpose Hamming_Encode.v and
hardware description lan- Hamming_Decode.v files
guage that is easy to learn and from EFYProject folder.
use. It is similar in syntax to These file names are dis-
the C programming lan- played in the
guage. Verilog allows differ- Workspace window.
ent levels of abstraction to be 6. Select each file by
mixed in the same model. right-clicking it and com-
Thus a designer can define a pile it by selecting Com-
hardware model in terms of pile option from the pop-
switches, gates, register trans- down menu.
fer level (RTL) or algorithmic/ 7. Select Simulate
behavioural code. Verilog option from the main
should not be confused with menu bar and select Start
VHDL, which is yet another Simulation.
HDL whose first letter stands 8. Open the selected
for very high-speed inte- file from Work library in
grated circuit (VHSIC). Start Simulation win-
The Hamming_Encode.v Fig. 4: Error detection and correction using hamming code dow, then click OK but-

WWW.EFYMAG.COM ELECTRONICS FOR YOU FEBRUARY 2006 95

CMYK
SOFTWARE
SECTION
output for Hamming_
Encode.v is shown in
Fig. 5. Repeat steps 6
through 11 by enter-
ing appropriate inputs
for Hamming_
decode.v. The de-
coded output is
shown in Fig. 6.
EFY note. The
source codes, execut-
able files and
Fig. 5: Verilog functional simulation output of Fig. 6: Verilog functional simulation output of
screenshots for this ar-
Hamming_Encode.v Hamming_Decode.v ticle have been in-
cluded in this months
ton. 10. Right-click in option and se- EFY-CD. When only simulation is to
9. The Objects window appears lect Force option. be carried out, ModelSim XE alone is
as shown in Fig. 5. The zzzzzzz cor- 11. Enter the input value as sufficient. However, for synthesis and
responding to the in input has to be 1001101 and click OK button. Im- implementation (on Xilinx FPGAs),
replaced by 1001101 as per our de- mediately, zzzzzzz is replaced by Xilinx ISE is required. The steps for
sign. (z and x denote high-imped- forced input digits. set-up of ModelSim XE III 6.0a in con-
ance and unknown states, respectively, 12. Select Run-All option from junction with Xilinx ISE 7.1i Founda-
in Verilog.) Simulate menu bar. The simulated tion Series are also included in the CD.

HAMMING_ENCODE.V
// Hamming Code Encoder if(reset) j=j+1;
// Developed By VARUN JINDAL out = 0; end
else if(^(out & 11b101_0101_0101))
module hamm_enc(out,in,reset); begin out[0] = ~out[0];
i=0; j=0; if(^(out & 11b110_0110_0110))
parameter n=11,k=7; while((i<n) || (j<k)) out[1] = ~out[1];
output [n-1:0] out; begin if(^(out & 11b000_0111_1000))
input [k-1:0] in; while(i==0 || i==1 || i==3 || i==7) out[3] = ~out[3];
input reset; begin if(^(out & 11b111_1000_0000))
reg [n-1:0] out; out[i] = 0; out[7] = ~out[7];
integer i,j; i=i+1; end
end end
always @(in or reset) out[i] = in[j]; endmodule
begin i=i+1;

HAMMING_DECODE.V
// Hamming Code Decoder IN[r-1] = ~IN[r-1];
// Developed By VARUN JINDAL always @(in or reset)
begin i=0; j=0;
module hamm_dec(out,in,reset); if(reset) while((i<n) || (j<k))
out=0; begin
parameter n=11,k=7; else while(i==0 || i==1 || i==3 || i==7)
output [k-1:0] out; begin i=i+1;
input [n-1:0] in; r1 = ^(in & 11b101_0101_0101); out[j]=IN[i];
input reset; r2 = ^(in & 11b110_0110_0110); i=i+1;
reg [k-1:0] out; r4 = ^(in & 11b000_0111_1000); j=j+1;
reg r1,r2,r4,r8; r8 = ^(in & 11b111_1000_0000); end
reg [3:0] r; r = {r8,r4,r2,r1}; end
reg [n-1:0] IN; end
integer i,j; IN = in; endmodule z

AVAILABLE
Please add 4% tax for local sale in Delhi
or 10% C.S.T. for orders from outside Delhi
Postage charges Rs 40 per kit
Please send your remittance by DD/MO (not cheque) to:

COMPLETE KITS OF:


INFRARED OBJECT COUNTER For Rs 250 KitsnSpares
303, Dohil Chambers, 46 Nehru Place
LONG-RANGE BURGLAR ALARM USING New Delhi 110019
LASER TORCH For Rs 280 Phone: 26430523, 26449577
E-mail: kits@efyindia.com

96 FEBRUARY 2006 ELECTRONICS FOR YOU WWW.EFYMAG.COM

CMYK

You might also like