Advertisement
captmicro

Untitled

Mar 14th, 2011
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.26 KB | None | 0 0
  1. KCALLT uint32 debug_prng(void)
  2. {
  3.     if (DPRNG->LFSR1 == 0) DPRNG->LFSR1 = 0x9D0A31FC;
  4.     if (DPRNG->LFSR2 == 0) DPRNG->LFSR2 = 0xF91D72E9;
  5.     if (DPRNG->LFSR3 == 0) DPRNG->LFSR3 = 0x8B90DF09;
  6.     if (DPRNG->LFSR1_TAPS[7] != 0xFF) {
  7.         /* {29, 26, 18, 17, 14, 8, 0xFF} */
  8.         DPRNG->LFSR1_TAPS[0] = 29; DPRNG->LFSR1_TAPS[1] = 26;
  9.         DPRNG->LFSR1_TAPS[2] = 18; DPRNG->LFSR1_TAPS[3] = 17;
  10.         DPRNG->LFSR1_TAPS[4] = 14; DPRNG->LFSR1_TAPS[5] = 8;
  11.         DPRNG->LFSR1_TAPS[6] = 0xFF;
  12.     }
  13.     if (DPRNG->LFSR2_TAPS[9] != 0xFF) {
  14.         /* {30, 25, 23, 16, 15, 13, 11, 5, 0xFF} */
  15.         DPRNG->LFSR2_TAPS[0] = 30; DPRNG->LFSR2_TAPS[1] = 25;
  16.         DPRNG->LFSR2_TAPS[2] = 23; DPRNG->LFSR2_TAPS[3] = 16;
  17.         DPRNG->LFSR2_TAPS[4] = 15; DPRNG->LFSR2_TAPS[5] = 13;
  18.         DPRNG->LFSR2_TAPS[6] = 11; DPRNG->LFSR2_TAPS[7] = 5;
  19.         DPRNG->LFSR2_TAPS[8] = 0xFF;
  20.     }
  21.     if (DPRNG->LFSR3_TAPS[6] != 0xFF) {
  22.         /* {28, 24, 21, 19, 12, 0xFF} */
  23.         DPRNG->LFSR3_TAPS[0] = 28; DPRNG->LFSR3_TAPS[1] = 24;
  24.         DPRNG->LFSR3_TAPS[2] = 21; DPRNG->LFSR3_TAPS[3] = 19;
  25.         DPRNG->LFSR3_TAPS[4] = 12; DPRNG->LFSR3_TAPS[5] = 0xFF;
  26.     }
  27.     if (DPRNG->MIXLOOPS[3] == 0) {
  28.         /* {5, 7, 3} */
  29.         DPRNG->MIXLOOPS[0] = 5; DPRNG->MIXLOOPS[1] = 7;
  30.         DPRNG->MIXLOOPS[2] = 3;
  31.     }
  32.    
  33.     uint32 rnd = 0; uint8 bit = 0, fi = 0;
  34.     uint8 *btr = 0, *bLFSR = 0, *bMIX = 0;
  35.    
  36.     /* SHIFT LFSR1 (XOR) */
  37.     bit = 0; btr = &DPRNG->LFSR1_TAPS[0];
  38.     for (fi = 0; *btr != 0xFF; *btr++, fi++)
  39.         bit = XOR(bit, DPRNG->LFSR1 >> (32 - btr[fi]));
  40.     bit &= 0x1; DPRNG->LFSR1 = (DPRNG->LFSR1 >> 1) | (bit << 15);
  41.    
  42.     /* SHIFT LFSR2 (XNOR) */
  43.     bit = 0; btr = &DPRNG->LFSR2_TAPS[0];
  44.     for (fi = 0; *btr != 0xFF; *btr++, fi++)
  45.         bit = XNOR(bit, DPRNG->LFSR2 >> (32 - btr[fi]));
  46.     bit &= 0x1; DPRNG->LFSR2 = (DPRNG->LFSR2 >> 1) | (bit << 15);
  47.    
  48.     /* SHIFT LFSR3 (XNOR) */
  49.     bit = 0; btr = &DPRNG->LFSR3_TAPS[0];
  50.     for (fi = 0; *btr != 0xFF; *btr++, fi++)
  51.         bit = XNOR(bit, DPRNG->LFSR3 >> (32 - btr[fi]));
  52.     bit &= 0x1; DPRNG->LFSR3 = (DPRNG->LFSR3 >> 1) | (bit << 15);
  53.    
  54.     /* MIX MIX1 (LSHIFT) */
  55.     if (DPRNG->MIX1 == 0) DPRNG->MIX1 = DPRNG->LFSR1;
  56.     bMIX = (uint8*)&DPRNG->MIX1; bLFSR = (uint8*)&DPRNG->LFSR1;
  57.     for (fi = 0; fi < DPRNG->MIXLOOPS[0]; fi++) {
  58.         bMIX[0]^=bLFSR[1]<<11; bMIX[3]+=bLFSR[0]; bMIX[1]+=bMIX[2];
  59.         bMIX[1]^=bLFSR[2]>>2;  bMIX[4]+=bLFSR[1]; bMIX[2]+=bMIX[3];
  60.         bMIX[2]^=bLFSR[3]<<8;  bMIX[5]+=bLFSR[2]; bMIX[3]+=bMIX[4];
  61.         bMIX[3]^=bLFSR[4]>>16; bMIX[6]+=bLFSR[3]; bMIX[4]+=bMIX[5];
  62.         bMIX[4]^=bLFSR[5]<<10; bMIX[7]+=bLFSR[4]; bMIX[5]+=bMIX[6];
  63.         bMIX[5]^=bLFSR[6]>>4;  bMIX[0]+=bLFSR[5]; bMIX[6]+=bMIX[7];
  64.         bMIX[6]^=bLFSR[7]<<8;  bMIX[1]+=bLFSR[6]; bMIX[7]+=bMIX[0];
  65.         bMIX[7]^=bLFSR[0]>>9;  bMIX[2]+=bLFSR[7]; bMIX[0]+=bMIX[1];
  66.         DPRNG->MIX1 = DPRNG->MIX1 >> 1;
  67.     }
  68.    
  69.     /* MIX MIX2 (LSHIFT) */
  70.     if (DPRNG->MIX2 == 2) DPRNG->MIX2 = DPRNG->LFSR2;
  71.     bMIX = (uint8*)&DPRNG->MIX2; bLFSR = (uint8*)&DPRNG->LFSR2;
  72.     for (fi = 0; fi < DPRNG->MIXLOOPS[1]; fi++) {
  73.         bMIX[0]^=bLFSR[1]<<11; bMIX[3]+=bLFSR[0]; bMIX[1]+=bLFSR[2];
  74.         bMIX[1]^=bLFSR[2]>>2;  bMIX[4]+=bLFSR[1]; bMIX[2]+=bLFSR[3];
  75.         bMIX[2]^=bLFSR[3]<<8;  bMIX[5]+=bLFSR[2]; bMIX[3]+=bLFSR[4];
  76.         bMIX[3]^=bLFSR[4]>>16; bMIX[6]+=bLFSR[3]; bMIX[4]+=bLFSR[5];
  77.         bMIX[4]^=bLFSR[5]<<10; bMIX[7]+=bLFSR[4]; bMIX[5]+=bLFSR[6];
  78.         bMIX[5]^=bLFSR[6]>>4;  bMIX[0]+=bLFSR[5]; bMIX[6]+=bLFSR[7];
  79.         bMIX[6]^=bLFSR[7]<<8;  bMIX[1]+=bLFSR[6]; bMIX[7]+=bLFSR[0];
  80.         bMIX[7]^=bLFSR[0]>>9;  bMIX[2]+=bLFSR[7]; bMIX[0]+=bLFSR[1];
  81.         DPRNG->MIX2 = DPRNG->MIX2 >> 1;
  82.     }
  83.    
  84.     /* MIX MIX3 (RSHIFT) */
  85.     if (DPRNG->MIX3 == 2) DPRNG->MIX3 = DPRNG->LFSR3;
  86.     bMIX = (uint8*)&DPRNG->MIX3; bLFSR = (uint8*)&DPRNG->LFSR3;
  87.     for (fi = 0; fi < DPRNG->MIXLOOPS[2]; fi++) {
  88.         bMIX[0]^=bLFSR[1]<<11; bMIX[3]+=bLFSR[0]; bMIX[1]+=bMIX[2];
  89.         bMIX[1]^=bLFSR[2]>>2;  bMIX[4]+=bLFSR[1]; bMIX[2]+=bMIX[3];
  90.         bMIX[2]^=bLFSR[3]<<8;  bMIX[5]+=bLFSR[2]; bMIX[3]+=bMIX[4];
  91.         bMIX[3]^=bLFSR[4]>>16; bMIX[6]+=bLFSR[3]; bMIX[4]+=bMIX[5];
  92.         bMIX[4]^=bLFSR[5]<<10; bMIX[7]+=bLFSR[4]; bMIX[5]+=bMIX[6];
  93.         bMIX[5]^=bLFSR[6]>>4;  bMIX[0]+=bLFSR[5]; bMIX[6]+=bMIX[7];
  94.         bMIX[6]^=bLFSR[7]<<8;  bMIX[1]+=bLFSR[6]; bMIX[7]+=bMIX[0];
  95.         bMIX[7]^=bLFSR[0]>>9;  bMIX[2]+=bLFSR[7]; bMIX[0]+=bMIX[1];
  96.         DPRNG->MIX3 = DPRNG->MIX3 << 1;
  97.     }
  98.    
  99.     /* GET 32 BIT RANDOM VALUE */
  100.     rnd = 0x00000000;
  101.     rnd |= DPRNG->MIX1 & 0x0F000F00;
  102.     rnd |= DPRNG->MIX2 & 0xF0F0F0F0;
  103.     rnd |= DPRNG->MIX3 & 0x000F000F;
  104.    
  105.     return rnd;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement