Verilog Code for Hamming Code Detector


Hamming Code Detector


Hola Amigos,
Today I am going to discuss about Hamming Code, its parity working and its code detection. Hamming code detection is basically used when there is a single bit error. It is quite fast and efficient method to determine the error bit and replace it with the opposite bit.


Let us take data as – 01001101 and with parity it will be xx0x100x1101. How are the parity bit set here comes in mind first. 




Parity bit is always set at positions which are power of 2 like 1, 2, 4, 8, 16 etc. So

 As you can see the parity bits are set as X at positions of power of 2. Follow same process for any data.

Now what will be at these positions ?

Note down position 1 as P1 position 2 as P2 position 4 as P4 and position 8 as P8
Now for P1 start from position 1. Take first bit and ignore next and again take another and ignore next. So P1 will be x01010

Similarly for P2 starting bit will be at position 2 so take two bits at once then ignore next two bit and agin take two and ignore next two. So P2 will be x00010

Similarly for P4 starting bit position will be 4 so take 4 bits at once then ignore next 4 then take another 4 and ignore next 4. So P4 will be x1001

Similarly for P8 we will have P8 as x1101 since we only have 5 bits left from position 8.

Now P1 has even number of 1s hence its parity is 0 thus P1 = 0.
Similarly P2 has odd number of 1s so odd parity hence P2 = 1.
Similarly P4 has even number of 1s so P4 = 0
And P8 = 1

Now the data with parity become by replacing P1-8 as 010010011101.

This is our Hamming Code.
Now let there be an error and the data with parity become this 010010011111. The red bit was 0 but by noise it becomes 1.

Now follow same procedure to find P1-8
Now P1 has position 1, P2 has position 2, P4 has position 4, and P8 has 8.

So add the positions whose match is “NO”

1 + 2 + 8 = 11 Thus 11th bit has the error and we have to replace that bit. Now go and check the position of the colored red bit. Voila its 11

Here is the code for Hamming code

module Hamming(data,data_parity,clk,out,bit_error,k);
  input [7:0]data;
  input [11:0]data_parity;
  input clk;
  output [11:0]out,k;
  output [3:0]bit_error;
  reg [5:0]p1;
  reg [5:0]p2;
  reg [4:0]p3;
  reg [4:0]p4;
  reg [11:0]copy1;
  integer i;
  initial i = 1;
  always @(posedge clk)begin
    if(i==1)
                     copy1 = data_parity;
                     i = 2;
  end
  always @(posedge clk)begin
    p1 = {copy1[11],copy1[9],copy1[7],copy1[5],copy1[3],copy1[1]};
                     p2 = {copy1[10:9],copy1[6:5],copy1[2:1]};
                     p3 = {copy1[8:5],copy1[0]};
                     p4 = copy1[4:0];
  end
 
  wire o11,o10,o8,o4;
  wire [31:0]noise;
  count c1(p1,o11,clk);
  count c2(p2,o10,clk);
  count c3(p3,o8,clk);
  count c4(p4,o4,clk);
  assign out ={o11,o10,data_parity[9],o8,data_parity[7:5],o4,data_parity[3:0]};
  error_detect err(out,clk,p1,p2,p3,p4,bit_error,k);
endmodule

Code for Count i.e even or odd Parity

module count(in,out,clk);
input [5:0]in;
input clk;
output reg out;
reg [5:0]stor;
reg j;
reg [2:0]k;
integer i,m;
initial begin
i = 0;
m = 0;
k = 3'b0;
end
always @(in)begin
stor <= in;
end
always @(posedge clk)begin
if(k<7)begin
j = stor[5];
if(j==1)
i = i + 1;
k = k + 1;
stor = stor<<1;
end
if(k==7)begin
if(i%2==0)
out = 1'b0;
else
out = 1'b1;
end
end
endmodule

Code for generating error bit 

module error_detect(input [11:0]in,input clk,input [5:0]p1,p2,input [4:0]p3,p4,output reg [3:0]bit_error,output reg[11:0]k);
reg [3:0]num;
reg [31:0]num1;
//integer bit_error;
initial  begin
bit_error = 0;
num1 = $random();
num = num1[31:28];
end
always @(in)
k <= in;
reg [5:0]p1x,p2x;reg [4:0]p3x,p4x;
always @(posedge clk)begin
k[num] = 1;
                     p1x = {k[11],k[9],k[7],k[5],k[3],k[1]};
                     p2x = {k[10:9],k[6:5],k[2:1]};
                     p3x = {k[8:5],k[0]};
                     p4x = k[4:0];
end
always @(p1)begin
if(p1!=p1x)
bit_error = bit_error + 1;
if(p2!=p2x)
bit_error = bit_error + 2;
if(p3!=p3x)
bit_error = bit_error + 4;
if(p4!=p4x)
bit_error = bit_error + 8;
end
endmodule

TestBench

module test();
  reg [7:0]Data;
  reg [11:0]Unknown_Data_Parity;
  reg clk;
  wire [11:0]Known_Data_Parity;
  wire [11:0]Data_With_Noise;
  wire [3:0]Bit_Error;
  initial begin
    clk = 0;
    Data = 8'b01001101;
    Unknown_Data_Parity = 12'bxx0x100x1101;
  end
  always #1 clk=~clk;
  Hamming ham(Data,Unknown_Data_Parity,clk,Known_Data_Parity,Bit_Error,Data_With_Noise);
endmodule

Output





So Long

2 comments: