Return to Main Page
GeSHi © 2004, Nigel McNie.
  1. #include "hamming.h"
  2.  
  3. /*
  4. * This function generates a 7-bit hamming correction code for a 32-bit input.
  5. * The output is based on the DP8405 Hamming code. The input is taken as an
  6. * integer (32-bit) and the returned value is also an iteger, with the code in
  7. * the low 7 bits.
  8. *
  9. * Input: int in The Value to be encoded
  10. * Output: int ret Bits [7...0] contain the codeword
  11. */
  12. int getECC(int in)
  13. {
  14. int cb[7] = {0,0,0,0,0,0,0};
  15. int i, j, ret;
  16.  
  17. /*Set up the matrix for parity bit creation.*/
  18. int g0[32] = {1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1};
  19. int g1[32] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0};
  20. int g2[32] = {1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1};
  21. int g3[32] = {1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0};
  22. int g4[32] = {0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1};
  23. int g5[32] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1};
  24. int g6[32] = {1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1};
  25.  
  26. int* g;
  27.  
  28. /*Initialize return value to 0*/
  29. ret = 0;
  30.  
  31. for(j = 0; j < 7; j++)
  32. {
  33. /*Choose the correct row of the matrix, based on current bit in code*/
  34. switch(j)
  35. {
  36. case 0:
  37. g = g0;
  38. break;
  39. case 1:
  40. g = g1;
  41. break;
  42. case 2:
  43. g = g2;
  44. break;
  45. case 3:
  46. g = g3;
  47. break;
  48. case 4:
  49. g = g4;
  50. break;
  51. case 5:
  52. g = g5;
  53. break;
  54. case 6:
  55. g = g6;
  56. break;
  57. }
  58.  
  59. /*Go through row of matrix, and generate the parity bit*/
  60. for(i = 0; i < 32; i++)
  61. {
  62. /*for values 3, 4, 5, 6, and 7 the correct code is XORed*/
  63. if (j >= 3)
  64. cb[j] = cb[j] ^ (g[i]*((in & (1 << i)) >> i));
  65. /*for all other values, the correct code is NORed*/
  66. else
  67. cb[j] = (cb[j] | (g[i]*((in & (1 << i)) >> i)));
  68. }
  69.  
  70. /*Add the newly generated bit to the return value*/
  71. if ( j >= 3)
  72. ret = ret | (cb[j] << j);
  73. else
  74. ret = ret | (!cb[j] << j);
  75. }
  76.  
  77. /*Return hammond code word*/
  78. return ret;
  79. }
  80.  
Parsed in 0.009 seconds, using GeSHi 1.0.7.20