zmatt

omap3_mlc_bch.c

May 3rd, 2022
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #if 1
  2. // try this one first
  3. #define BCH_POLY 0b001110010101
  4. #else
  5. // other possibility
  6. #define BCH_POLY 0b010100111001
  7. #endif
  8.  
  9.  
  10. // we use the top 12 bits since it's more convenient for left-shifting
  11. #if 1
  12. unsigned omap3_mlc_bch_shl1( unsigned x )
  13. {
  14.     if( x >> 31 )
  15.         return ( x << 1 ) ^ ( BCH_POLY << 20 );
  16.     else
  17.         return ( x << 1 );
  18. }
  19. #else
  20. // maybe more efficient version?
  21. unsigned omap3_mlc_bch_shl1( unsigned x )
  22. {
  23.     return ( x << 1 ) ^ ( ( BCH_POLY << 20 ) & -( x >> 31 ) );
  24. }
  25. #endif
  26.  
  27. unsigned omap3_mlc_bch_word( uint16_t w )
  28. {
  29.     // put 20-bit data word (incl padding) into top 20 bits
  30.     unsigned x = w << 16;
  31.  
  32.     // shift left and reduce to 12 bits (again in top 12 bits)
  33.     for( int i = 0; i < 20; ++i )
  34.         x = omap3_mlc_bch_shl1( x );
  35.  
  36.     return w << 16 | x >> 20;
  37. }
  38.  
  39. void omap3_mlc_bch( unsigned encoded[260], uint16_t const data[256] )
  40. {
  41.     unsigned checksum = 0;
  42.     for( int i = 0; i < 256; ++i ) {
  43.         checksum += data[i];
  44.         encoded[i] = omap3_mlc_bch_word( data[i] );
  45.     }
  46.     encoded[256] = omap3_mlc_bch_word( checksum & 0xffff );
  47.     encoded[257] = 0;
  48.     encoded[258] = 0;
  49.     encoded[259] = 0;
  50. }
  51.  
  52. void omap3_mlc_bch_verify( unsigned const encoded[260] )
  53. {
  54.     for( int i = 0; i < 160; ++i ) {
  55.         unsigned x = encoded[i];
  56.         for( int i = 0; i < 32; ++i )
  57.             x = omap3_mlc_bch_shl1( x );
  58.         if( x != 0 )
  59.             printf( "encoded[%3d] = 0x%08x  verification failed (0x%03x != 0)\n", i, encoded[i], x >> 20 );
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment